PassiveEnchantsTask fix

This commit is contained in:
BuildTools 2023-10-25 00:37:49 +05:00
parent d5537829eb
commit 938d15c2f4
7 changed files with 55 additions and 31 deletions

View File

@ -96,7 +96,7 @@ public class ExcellentEnchants extends NexPlugin<ExcellentEnchants> {
case V1_20_R2 -> new V1_20_R2(); case V1_20_R2 -> new V1_20_R2();
default -> null; default -> null;
}; };
return this.enchantManager != null; return this.enchantNMS != null;
} }
@Override @Override

View File

@ -6,8 +6,8 @@ public interface Periodic {
@NotNull Periodic getPeriodImplementation(); @NotNull Periodic getPeriodImplementation();
default long getInterval(int level) { default long getInterval() {
return this.getPeriodImplementation().getInterval(level); return this.getPeriodImplementation().getInterval();
} }
default long getNextTriggerTime() { default long getNextTriggerTime() {
@ -18,7 +18,7 @@ public interface Periodic {
return this.getPeriodImplementation().isTriggerTime(); return this.getPeriodImplementation().isTriggerTime();
} }
default void updateTriggerTime(int level) { default void updateTriggerTime() {
this.getPeriodImplementation().updateTriggerTime(level); this.getPeriodImplementation().updateTriggerTime();
} }
} }

View File

@ -85,7 +85,7 @@ public abstract class ExcellentEnchant extends Enchantment implements IEnchantme
map.add(Placeholders.ENCHANTMENT_CHANCE, () -> NumberUtil.format(chanced.getTriggerChance(level))); map.add(Placeholders.ENCHANTMENT_CHANCE, () -> NumberUtil.format(chanced.getTriggerChance(level)));
} }
if (this instanceof Periodic periodic) { 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) { if (this instanceof Potioned potioned) {
map.add(Placeholders.ENCHANTMENT_POTION_LEVEL, () -> NumberUtil.toRoman(potioned.getEffectAmplifier(level))); map.add(Placeholders.ENCHANTMENT_POTION_LEVEL, () -> NumberUtil.toRoman(potioned.getEffectAmplifier(level)));

View File

@ -34,8 +34,8 @@ public class PeriodImplementation implements Periodic {
} }
@Override @Override
public long getInterval(int level) { public long getInterval() {
return (long) this.triggerInterval.getValue(level); return (long) this.triggerInterval.getValue(1);
} }
@Override @Override
@ -49,7 +49,7 @@ public class PeriodImplementation implements Periodic {
} }
@Override @Override
public void updateTriggerTime(int level) { public void updateTriggerTime() {
this.nextTriggerTime = System.currentTimeMillis() + this.getInterval(level) / 20L * 1000L; this.nextTriggerTime = System.currentTimeMillis() + this.getInterval() * 50L - 100L;
} }
} }

View File

@ -64,8 +64,8 @@ public final class PotionImplementation implements Potioned {
public int getEffectDuration(int level) { public int getEffectDuration(int level) {
if (this.isPermanent()) { if (this.isPermanent()) {
int duration = Config.TASKS_PASSIVE_ENCHANTS_TRIGGER_INTERVAL.get().intValue() + 30; int duration = Config.TASKS_PASSIVE_ENCHANTS_TRIGGER_INTERVAL.get().intValue() * 2;
if (this.getEffectType().getName().equalsIgnoreCase(PotionEffectType.NIGHT_VISION.getName())) { if (this.getEffectType().getName().equalsIgnoreCase(PotionEffectType.NIGHT_VISION.getName()) && duration < 600) {
duration += 30 * 20; duration += 30 * 20;
} }
return duration; return duration;

View File

@ -3,8 +3,12 @@ package su.nightexpress.excellentenchants.enchantment.task;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import su.nexmedia.engine.api.server.AbstractTask; import su.nexmedia.engine.api.server.AbstractTask;
import su.nexmedia.engine.utils.Pair;
import su.nightexpress.excellentenchants.ExcellentEnchants; import su.nightexpress.excellentenchants.ExcellentEnchants;
import su.nightexpress.excellentenchants.api.enchantment.type.PassiveEnchant;
import su.nightexpress.excellentenchants.config.Config; 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 su.nightexpress.excellentenchants.enchantment.util.EnchantUtils;
import java.util.Collection; import java.util.Collection;
@ -13,15 +17,43 @@ import java.util.Set;
public class PassiveEnchantsTask extends AbstractTask<ExcellentEnchants> { public class PassiveEnchantsTask extends AbstractTask<ExcellentEnchants> {
private final Set<Pair<PassiveEnchant, ExcellentEnchant>> enchants;
public PassiveEnchantsTask(@NotNull ExcellentEnchants plugin) { public PassiveEnchantsTask(@NotNull ExcellentEnchants plugin) {
super(plugin, Config.TASKS_PASSIVE_ENCHANTS_TRIGGER_INTERVAL.get(), false); 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 @Override
public void action() { public void action() {
for (LivingEntity entity : this.getEntities()) { if (this.enchants.isEmpty()) return;
EnchantUtils.triggerPassiveEnchants(entity);
} 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 @NotNull

View File

@ -24,7 +24,6 @@ import su.nexmedia.engine.utils.ItemUtil;
import su.nexmedia.engine.utils.PDCUtil; import su.nexmedia.engine.utils.PDCUtil;
import su.nightexpress.excellentenchants.ExcellentEnchantsAPI; import su.nightexpress.excellentenchants.ExcellentEnchantsAPI;
import su.nightexpress.excellentenchants.api.enchantment.IEnchantment; 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.config.Config;
import su.nightexpress.excellentenchants.enchantment.impl.ExcellentEnchant; import su.nightexpress.excellentenchants.enchantment.impl.ExcellentEnchant;
import su.nightexpress.excellentenchants.enchantment.registry.EnchantRegistry; import su.nightexpress.excellentenchants.enchantment.registry.EnchantRegistry;
@ -355,23 +354,16 @@ public class EnchantUtils {
return map; return map;
} }
public static void triggerPassiveEnchants(@NotNull LivingEntity entity) { @NotNull
Player player = entity instanceof Player p1 ? p1 : null; public static Map<ItemStack, Integer> getEquipped(@NotNull LivingEntity entity, @NotNull ExcellentEnchant enchantment) {
Map<ItemStack, Integer> map = new HashMap<>();
getEquipped(entity, PassiveEnchant.class).forEach((item, enchants) -> { getEnchantedEquipment(entity).values().forEach(item -> {
enchants.forEach((enchant, level) -> { int level = getLevel(item, enchantment);
if (!enchant.isAvailableToUse(entity)) return; if (level > 0) {
if (!enchant.isTriggerTime()) return; map.put(item, level);
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);
} }
}); });
return map;
} }
public static void setSourceWeapon(@NotNull Projectile projectile, @Nullable ItemStack item) { public static void setSourceWeapon(@NotNull Projectile projectile, @Nullable ItemStack item) {