From 938d15c2f4862e77be8591cba480f085c54e28ab Mon Sep 17 00:00:00 2001 From: BuildTools Date: Wed, 25 Oct 2023 00:37:49 +0500 Subject: [PATCH] PassiveEnchantsTask fix --- .../excellentenchants/ExcellentEnchants.java | 2 +- .../api/enchantment/meta/Periodic.java | 8 ++-- .../enchantment/impl/ExcellentEnchant.java | 2 +- .../impl/meta/PeriodImplementation.java | 8 ++-- .../impl/meta/PotionImplementation.java | 4 +- .../enchantment/task/PassiveEnchantsTask.java | 38 +++++++++++++++++-- .../enchantment/util/EnchantUtils.java | 24 ++++-------- 7 files changed, 55 insertions(+), 31 deletions(-) diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/ExcellentEnchants.java b/Core/src/main/java/su/nightexpress/excellentenchants/ExcellentEnchants.java index 3beadac..ad15f58 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/ExcellentEnchants.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/ExcellentEnchants.java @@ -96,7 +96,7 @@ public class ExcellentEnchants extends NexPlugin { case V1_20_R2 -> new V1_20_R2(); default -> null; }; - return this.enchantManager != null; + return this.enchantNMS != null; } @Override diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/api/enchantment/meta/Periodic.java b/Core/src/main/java/su/nightexpress/excellentenchants/api/enchantment/meta/Periodic.java index 426a1b2..f2194ce 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/api/enchantment/meta/Periodic.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/api/enchantment/meta/Periodic.java @@ -6,8 +6,8 @@ public interface Periodic { @NotNull Periodic getPeriodImplementation(); - default long getInterval(int level) { - return this.getPeriodImplementation().getInterval(level); + default long getInterval() { + return this.getPeriodImplementation().getInterval(); } default long getNextTriggerTime() { @@ -18,7 +18,7 @@ public interface Periodic { return this.getPeriodImplementation().isTriggerTime(); } - default void updateTriggerTime(int level) { - this.getPeriodImplementation().updateTriggerTime(level); + default void updateTriggerTime() { + this.getPeriodImplementation().updateTriggerTime(); } } diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/ExcellentEnchant.java b/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/ExcellentEnchant.java index 1ebdc34..a879594 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/ExcellentEnchant.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/ExcellentEnchant.java @@ -85,7 +85,7 @@ public abstract class ExcellentEnchant extends Enchantment implements IEnchantme map.add(Placeholders.ENCHANTMENT_CHANCE, () -> NumberUtil.format(chanced.getTriggerChance(level))); } if (this instanceof Periodic periodic) { - map.add(Placeholders.ENCHANTMENT_INTERVAL, () -> NumberUtil.format(periodic.getInterval(level) / 20D)); + map.add(Placeholders.ENCHANTMENT_INTERVAL, () -> NumberUtil.format(periodic.getInterval() / 20D)); } if (this instanceof Potioned potioned) { map.add(Placeholders.ENCHANTMENT_POTION_LEVEL, () -> NumberUtil.toRoman(potioned.getEffectAmplifier(level))); diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/meta/PeriodImplementation.java b/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/meta/PeriodImplementation.java index b0d0327..e9c449b 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/meta/PeriodImplementation.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/meta/PeriodImplementation.java @@ -34,8 +34,8 @@ public class PeriodImplementation implements Periodic { } @Override - public long getInterval(int level) { - return (long) this.triggerInterval.getValue(level); + public long getInterval() { + return (long) this.triggerInterval.getValue(1); } @Override @@ -49,7 +49,7 @@ public class PeriodImplementation implements Periodic { } @Override - public void updateTriggerTime(int level) { - this.nextTriggerTime = System.currentTimeMillis() + this.getInterval(level) / 20L * 1000L; + public void updateTriggerTime() { + this.nextTriggerTime = System.currentTimeMillis() + this.getInterval() * 50L - 100L; } } diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/meta/PotionImplementation.java b/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/meta/PotionImplementation.java index 45db877..d6ca37d 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/meta/PotionImplementation.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/meta/PotionImplementation.java @@ -64,8 +64,8 @@ public final class PotionImplementation implements Potioned { public int getEffectDuration(int level) { if (this.isPermanent()) { - int duration = Config.TASKS_PASSIVE_ENCHANTS_TRIGGER_INTERVAL.get().intValue() + 30; - if (this.getEffectType().getName().equalsIgnoreCase(PotionEffectType.NIGHT_VISION.getName())) { + int duration = Config.TASKS_PASSIVE_ENCHANTS_TRIGGER_INTERVAL.get().intValue() * 2; + if (this.getEffectType().getName().equalsIgnoreCase(PotionEffectType.NIGHT_VISION.getName()) && duration < 600) { duration += 30 * 20; } return duration; diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/task/PassiveEnchantsTask.java b/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/task/PassiveEnchantsTask.java index b10289f..4ef6467 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/task/PassiveEnchantsTask.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/task/PassiveEnchantsTask.java @@ -3,8 +3,12 @@ package su.nightexpress.excellentenchants.enchantment.task; import org.bukkit.entity.LivingEntity; import org.jetbrains.annotations.NotNull; import su.nexmedia.engine.api.server.AbstractTask; +import su.nexmedia.engine.utils.Pair; import su.nightexpress.excellentenchants.ExcellentEnchants; +import su.nightexpress.excellentenchants.api.enchantment.type.PassiveEnchant; import su.nightexpress.excellentenchants.config.Config; +import su.nightexpress.excellentenchants.enchantment.impl.ExcellentEnchant; +import su.nightexpress.excellentenchants.enchantment.registry.EnchantRegistry; import su.nightexpress.excellentenchants.enchantment.util.EnchantUtils; import java.util.Collection; @@ -13,15 +17,43 @@ import java.util.Set; public class PassiveEnchantsTask extends AbstractTask { + private final Set> enchants; + public PassiveEnchantsTask(@NotNull ExcellentEnchants plugin) { super(plugin, Config.TASKS_PASSIVE_ENCHANTS_TRIGGER_INTERVAL.get(), false); + this.enchants = new HashSet<>(); + + EnchantRegistry.getEnchantments(PassiveEnchant.class).forEach(enchant -> { + ExcellentEnchant excellent = EnchantRegistry.getByKey(enchant.getKey()); + if (excellent == null) return; + + this.enchants.add(Pair.of(enchant, excellent)); + }); } @Override public void action() { - for (LivingEntity entity : this.getEntities()) { - EnchantUtils.triggerPassiveEnchants(entity); - } + if (this.enchants.isEmpty()) return; + + var entities = this.getEntities(); + + this.enchants.forEach(pair -> { + PassiveEnchant enchant = pair.getFirst(); + ExcellentEnchant excellent = pair.getSecond(); + if (!enchant.isTriggerTime()) return; + + for (LivingEntity entity : entities) { + EnchantUtils.getEquipped(entity, excellent).forEach((item, level) -> { + if (!enchant.isAvailableToUse(entity)) return; + if (enchant.isOutOfCharges(item)) return; + if (enchant.onTrigger(entity, item, level)) { + enchant.consumeCharges(item, level); + } + }); + } + + enchant.updateTriggerTime(); + }); } @NotNull diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/util/EnchantUtils.java b/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/util/EnchantUtils.java index 95072e8..2cc39a1 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/util/EnchantUtils.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/util/EnchantUtils.java @@ -24,7 +24,6 @@ import su.nexmedia.engine.utils.ItemUtil; import su.nexmedia.engine.utils.PDCUtil; import su.nightexpress.excellentenchants.ExcellentEnchantsAPI; import su.nightexpress.excellentenchants.api.enchantment.IEnchantment; -import su.nightexpress.excellentenchants.api.enchantment.type.PassiveEnchant; import su.nightexpress.excellentenchants.config.Config; import su.nightexpress.excellentenchants.enchantment.impl.ExcellentEnchant; import su.nightexpress.excellentenchants.enchantment.registry.EnchantRegistry; @@ -355,23 +354,16 @@ public class EnchantUtils { return map; } - public static void triggerPassiveEnchants(@NotNull LivingEntity entity) { - Player player = entity instanceof Player p1 ? p1 : null; - - getEquipped(entity, PassiveEnchant.class).forEach((item, enchants) -> { - enchants.forEach((enchant, level) -> { - if (!enchant.isAvailableToUse(entity)) return; - if (!enchant.isTriggerTime()) return; - if (enchant.isOutOfCharges(item)) return; - if (enchant.onTrigger(entity, item, level)) { - enchant.consumeChargesNoUpdate(item, level); - enchant.updateTriggerTime(level); - } - }); - if (Config.ENCHANTMENTS_CHARGES_ENABLED.get() && player != null) { - EnchantUtils.updateDisplay(item); + @NotNull + public static Map getEquipped(@NotNull LivingEntity entity, @NotNull ExcellentEnchant enchantment) { + Map map = new HashMap<>(); + getEnchantedEquipment(entity).values().forEach(item -> { + int level = getLevel(item, enchantment); + if (level > 0) { + map.put(item, level); } }); + return map; } public static void setSourceWeapon(@NotNull Projectile projectile, @Nullable ItemStack item) {