Skip to content

Commit

Permalink
Fix slimes and groups of mobs attacking rapidly with no cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
Archy-X committed Dec 15, 2024
1 parent 344f4d7 commit 8e2b087
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package dev.aurelium.auraskills.bukkit.listeners;

import dev.aurelium.auraskills.api.damage.DamageType;
import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.bukkit.damage.DamageHandler;
import dev.aurelium.auraskills.api.damage.DamageType;
import dev.aurelium.auraskills.bukkit.damage.DamageResult;
import dev.aurelium.auraskills.common.config.Option;
import dev.aurelium.auraskills.common.user.User;
import org.bukkit.Material;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
Expand Down Expand Up @@ -61,6 +63,22 @@ public void onDamage(EntityDamageByEntityEvent event) {
event.setCancelled(true);
} else {
event.setDamage(result.damage());
// Correct last damage to fix repeated attacks that bypass the invulnerable frame
if (event.getEntity() instanceof Player damaged && plugin.configBoolean(Option.DAMAGE_CORRECT_LAST_DAMAGE)) {
plugin.getScheduler().executeSync(() -> {
User user = plugin.getUser(damaged);
double lastDamage = Math.max(event.getFinalDamage(), user.getCurrentOriginalDamage());
damaged.setLastDamage(lastDamage);
});
}
}
}

@EventHandler(priority = EventPriority.LOWEST)
public void onDamageLow(EntityDamageByEntityEvent event) {
if (event.getEntity() instanceof Player player && plugin.configBoolean(Option.DAMAGE_CORRECT_LAST_DAMAGE)) {
User user = plugin.getUser(player);
user.setCurrentOriginalDamage(event.getDamage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,29 @@ public void updateUserFile(String path) throws IOException {

public void saveConfigIfUpdated(File file, ConfigurationNode embedded, ConfigurationNode user, ConfigurationNode merged) throws IOException {
// Save if the number of config values in embedded is greater than user file
int embeddedCount = countChildren(embedded);
int userCount = countChildren(user);
if (embeddedCount > userCount) {
int missing = countMissing(embedded, user);
if (missing > 0) {
FileUtil.saveYamlFile(file, merged);
String path = plugin.getPluginFolder().toPath().relativize(file.toPath()).toString();
int updated = embeddedCount - userCount;
plugin.logger().info("Updated " + path + " with " + updated + " new key" + (updated != 1 ? "s" : ""));
plugin.logger().info("Updated " + path + " with " + missing + " new key" + (missing != 1 ? "s" : ""));
}
}

public int countMissing(ConfigurationNode embedded, ConfigurationNode user) {
int count = 0;
Stack<ConfigurationNode> stack = new Stack<>();
stack.addAll(embedded.childrenMap().values());
while (!stack.isEmpty()) {
ConfigurationNode node = stack.pop();
if (node.isMap()) { // A section node, push children to search
stack.addAll(node.childrenMap().values());
} else if (user.node(node.path()).virtual()) {
count++;
}
}
return count;
}

public int countChildren(ConfigurationNode root) {
int count = 0;
Stack<ConfigurationNode> stack = new Stack<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public enum Option {
REQUIREMENT_ITEM_PREVENT_BLOCK_PLACE("requirement.item.prevent_block_place", OptionType.BOOLEAN),
REQUIREMENT_ITEM_PREVENT_INTERACT("requirement.item.prevent_interact", OptionType.BOOLEAN),
REQUIREMENT_ARMOR_PREVENT_ARMOR_EQUIP("requirement.armor.prevent_armor_equip", OptionType.BOOLEAN),
// Damage options
DAMAGE_CORRECT_LAST_DAMAGE("damage.correct_last_damage", OptionType.BOOLEAN),
// Critical options
CRITICAL_ENABLED_SWORD("critical.enabled.sword", OptionType.BOOLEAN),
CRITICAL_ENABLED_BOW("critical.enabled.bow", OptionType.BOOLEAN),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public abstract class User {
private final JobsBatchData jobsBatchData;
@Nullable
private List<AntiAfkLog> storedAntiAfkLogs;
private double currentOriginalDamage;

public User(UUID uuid, AuraSkillsPlugin plugin) {
this.plugin = plugin;
Expand Down Expand Up @@ -535,6 +536,14 @@ public void setStoredAntiAfkLogs(@NotNull List<AntiAfkLog> logs) {
this.storedAntiAfkLogs = logs;
}

public double getCurrentOriginalDamage() {
return currentOriginalDamage;
}

public void setCurrentOriginalDamage(double currentOriginalDamage) {
this.currentOriginalDamage = currentOriginalDamage;
}

/**
* Checks if the profile has not had any changes since creation
* @return True if profile has not been modified, false if player has leveled profile
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ requirement:
armor:
prevent_armor_equip: true
global: [ ]
damage:
correct_last_damage: true
critical:
enabled:
sword: true
Expand Down

0 comments on commit 8e2b087

Please sign in to comment.