From a4cc0bfd88628339eba8207871e527d88ac186c0 Mon Sep 17 00:00:00 2001 From: nulli0n Date: Wed, 30 Nov 2022 19:18:05 +0600 Subject: [PATCH] v3.2.8 --- Core/pom.xml | 10 +- .../excellentenchants/ExcellentEnchants.java | 24 +-- .../ExcellentEnchantsAPI.java | 6 + .../api/enchantment/ExcellentEnchant.java | 18 +- .../enchantment/IEnchantPotionTemplate.java | 11 +- .../excellentenchants/config/Config.java | 5 +- .../manager/EnchantManager.java | 193 ++++++++++++++---- .../armor/EnchantElementalProtection.java | 3 +- .../enchants/armor/EnchantFlameWalker.java | 6 +- .../enchants/armor/EnchantRegrowth.java | 11 +- .../enchants/armor/EnchantSaturation.java | 14 +- .../manager/enchants/bow/EnchantGhast.java | 3 +- .../enchants/tool/EnchantCurseOfBreaking.java | 3 +- .../enchants/tool/EnchantSilkChest.java | 24 ++- .../enchants/tool/EnchantTelekinesis.java | 14 +- .../enchants/weapon/EnchantInfernus.java | 5 +- .../listeners/EnchantGenericListener.java | 78 +++---- .../manager/object/EnchantPopulator.java | 77 +++++++ .../manager/object/EnchantTier.java | 21 +- .../tasks/AbstractEnchantPassiveTask.java | 31 +-- .../tasks/EnchantEffectPassiveTask.java | 20 +- Core/src/main/resources/config.yml | 3 - NMS/pom.xml | 2 +- .../excellentenchants/nms/EnchantNMS.java | 27 +++ V1_17_R1/pom.xml | 4 +- .../nms/v1_17_R1/CustomEffectInstance.java | 39 ++++ .../nms/{ => v1_17_R1}/V1_17_R1.java | 26 ++- V1_18_R2/pom.xml | 4 +- .../nms/v1_18_R2/CustomEffectInstance.java | 39 ++++ .../nms/{ => v1_18_R2}/V1_18_R2.java | 26 ++- V1_19_R1/pom.xml | 4 +- .../nms/v1_19_R1/CustomEffectInstance.java | 39 ++++ .../nms/{ => v1_19_R1}/V1_19_R1.java | 26 ++- pom.xml | 4 +- 34 files changed, 607 insertions(+), 213 deletions(-) create mode 100644 Core/src/main/java/su/nightexpress/excellentenchants/ExcellentEnchantsAPI.java create mode 100644 Core/src/main/java/su/nightexpress/excellentenchants/manager/object/EnchantPopulator.java create mode 100644 V1_17_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_17_R1/CustomEffectInstance.java rename V1_17_R1/src/main/java/su/nightexpress/excellentenchants/nms/{ => v1_17_R1}/V1_17_R1.java (66%) create mode 100644 V1_18_R2/src/main/java/su/nightexpress/excellentenchants/nms/v1_18_R2/CustomEffectInstance.java rename V1_18_R2/src/main/java/su/nightexpress/excellentenchants/nms/{ => v1_18_R2}/V1_18_R2.java (66%) create mode 100644 V1_19_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_19_R1/CustomEffectInstance.java rename V1_19_R1/src/main/java/su/nightexpress/excellentenchants/nms/{ => v1_19_R1}/V1_19_R1.java (66%) diff --git a/Core/pom.xml b/Core/pom.xml index ccffb67..20aaa2e 100644 --- a/Core/pom.xml +++ b/Core/pom.xml @@ -5,7 +5,7 @@ ExcellentEnchants su.nightexpress.excellentenchants - 3.2.7 + 3.2.8 4.0.0 @@ -36,22 +36,22 @@ su.nightexpress.excellentenchants NMS - 3.2.7 + 3.2.8 su.nightexpress.excellentenchants V1_17_R1 - 3.2.7 + 3.2.8 su.nightexpress.excellentenchants V1_18_R2 - 3.2.7 + 3.2.8 su.nightexpress.excellentenchants V1_19_R1 - 3.2.7 + 3.2.8 fr.neatmonster diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/ExcellentEnchants.java b/Core/src/main/java/su/nightexpress/excellentenchants/ExcellentEnchants.java index 526ac4d..9ec1a86 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/ExcellentEnchants.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/ExcellentEnchants.java @@ -6,7 +6,6 @@ import su.nexmedia.engine.NexPlugin; import su.nexmedia.engine.Version; import su.nexmedia.engine.api.command.GeneralCommand; import su.nexmedia.engine.command.list.ReloadSubCommand; -import su.nexmedia.engine.utils.Reflex; import su.nightexpress.excellentenchants.command.BookCommand; import su.nightexpress.excellentenchants.command.EnchantCommand; import su.nightexpress.excellentenchants.command.ListCommand; @@ -16,6 +15,9 @@ import su.nightexpress.excellentenchants.config.Lang; import su.nightexpress.excellentenchants.manager.EnchantManager; import su.nightexpress.excellentenchants.manager.type.FitItemType; import su.nightexpress.excellentenchants.nms.EnchantNMS; +import su.nightexpress.excellentenchants.nms.v1_17_R1.V1_17_R1; +import su.nightexpress.excellentenchants.nms.v1_18_R2.V1_18_R2; +import su.nightexpress.excellentenchants.nms.v1_19_R1.V1_19_R1; public class ExcellentEnchants extends NexPlugin { @@ -51,20 +53,12 @@ public class ExcellentEnchants extends NexPlugin { } private boolean setNMS() { - Version current = Version.CURRENT; - if (current == null) return false; - - String pack = EnchantNMS.class.getPackage().getName(); - Class clazz = Reflex.getClass(pack, current.name()); - if (clazz == null) return false; - - try { - this.enchantNMS = (EnchantNMS) clazz.getConstructor().newInstance(); - } - catch (Exception e) { - e.printStackTrace(); - } - return this.enchantNMS != null; + this.enchantNMS = switch (Version.CURRENT) { + case V1_17_R1 -> new V1_17_R1(); + case V1_18_R2 -> new V1_18_R2(); + case V1_19_R1 -> new V1_19_R1(); + }; + return true; } @Override diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/ExcellentEnchantsAPI.java b/Core/src/main/java/su/nightexpress/excellentenchants/ExcellentEnchantsAPI.java new file mode 100644 index 0000000..f690fb8 --- /dev/null +++ b/Core/src/main/java/su/nightexpress/excellentenchants/ExcellentEnchantsAPI.java @@ -0,0 +1,6 @@ +package su.nightexpress.excellentenchants; + +public class ExcellentEnchantsAPI { + + public static final ExcellentEnchants PLUGIN = ExcellentEnchants.getPlugin(ExcellentEnchants.class); +} diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/api/enchantment/ExcellentEnchant.java b/Core/src/main/java/su/nightexpress/excellentenchants/api/enchantment/ExcellentEnchant.java index 5eaaff3..68f7443 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/api/enchantment/ExcellentEnchant.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/api/enchantment/ExcellentEnchant.java @@ -316,9 +316,11 @@ public abstract class ExcellentEnchant extends Enchantment implements IListener } public int getLevelByEnchantCost(int expLevel) { - Optional> opt = this.levelByEnchantCost.getValues().entrySet().stream() - .filter(en -> expLevel >= en.getValue().intValue()).max(Comparator.comparingInt(Map.Entry::getKey)); - return opt.isPresent() ? opt.get().getKey() : Rnd.get(this.getStartLevel(), this.getMaxLevel()); + int get = this.levelByEnchantCost.getValues().entrySet().stream() + .filter(en -> expLevel >= en.getValue().intValue()).max(Comparator.comparingInt(Map.Entry::getKey)) + .map(Map.Entry::getKey).orElse(0); + + return get != 0 ? this.fineLevel(get, ObtainType.ENCHANTING) : 0; } public double getObtainChance(@NotNull ObtainType obtainType) { @@ -333,6 +335,16 @@ public abstract class ExcellentEnchant extends Enchantment implements IListener return this.obtainLevelCap.getOrDefault(obtainType, Pair.of(-1, -1)).getSecond(); } + public int fineLevel(int level, @NotNull ObtainType obtainType) { + int levelCapMin = this.getObtainLevelMin(obtainType); + int levelCapMax = this.getObtainLevelMax(obtainType); + + if (levelCapMin > 0 && level < levelCapMin) level = levelCapMin; + if (levelCapMax > 0 && level > levelCapMax) level = levelCapMax; + + return level; + } + public int generateLevel() { return Rnd.get(this.getStartLevel(), this.getMaxLevel()); } diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/api/enchantment/IEnchantPotionTemplate.java b/Core/src/main/java/su/nightexpress/excellentenchants/api/enchantment/IEnchantPotionTemplate.java index 256aa42..bf6b103 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/api/enchantment/IEnchantPotionTemplate.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/api/enchantment/IEnchantPotionTemplate.java @@ -72,8 +72,17 @@ public abstract class IEnchantPotionTemplate extends IEnchantChanceTemplate { return new PotionEffect(this.potionEffectType, duration, amplifier, false, this.potionParticles); } + public final boolean hasEffect(@NotNull LivingEntity entity) { + return EnchantManager.hasEnchantmentEffect(entity, this); + } + public final boolean addEffect(@NotNull LivingEntity target, int level) { - EnchantManager.addPotionEffect(target, this.getEffect(level), true); + if (this instanceof PassiveEnchant) { + this.plugin.getEnchantNMS().addEnchantmentEffect(target, this, this.getEffect(level)); + } + else { + target.addPotionEffect(this.getEffect(level)); + } return true; } } \ No newline at end of file diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/config/Config.java b/Core/src/main/java/su/nightexpress/excellentenchants/config/Config.java index c48b5ba..6b7d84c 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/config/Config.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/config/Config.java @@ -2,6 +2,7 @@ package su.nightexpress.excellentenchants.config; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import su.nexmedia.engine.api.config.JOption; import su.nexmedia.engine.api.config.JYML; import su.nexmedia.engine.utils.Placeholders; import su.nexmedia.engine.utils.StringUtil; @@ -17,7 +18,7 @@ import java.util.stream.Collectors; public class Config { public static long TASKS_ARROW_TRAIL_TICKS_INTERVAL; - public static long TASKS_PASSIVE_ENCHANTS_TICKS_INTERVAL; + public static final JOption TASKS_PASSIVE_POTION_EFFECTS_APPLY_INTERVAL = JOption.create("General.Tasks.Passive_Potion_Effects.Apply_Interval", "Sets how often (in ticks) the plugin will apply permanent potion effects from enchanted items to an entity who wear them.\nThis setting does NOT refreshes currently active effects, but only attempts to add them if absent.", 150); public static Set ENCHANTMENTS_DISABLED; public static Map> ENCHANTMENTS_DISABLED_IN_WORLDS; @@ -35,10 +36,10 @@ public class Config { public static void load(@NotNull ExcellentEnchants plugin) { JYML cfg = plugin.getConfig(); + cfg.initializeOptions(Config.class); String path = "General.Tasks."; TASKS_ARROW_TRAIL_TICKS_INTERVAL = cfg.getLong(path + "Arrow_Trails.Ticks_Interval", 1); - TASKS_PASSIVE_ENCHANTS_TICKS_INTERVAL = cfg.getLong(path + "Passive_Enchants.Ticks_Interval", 100); path = "General.Enchantments."; cfg.addMissing(path + "Disabled_In_Worlds.my_world", Collections.singletonList(Placeholders.MASK_ANY)); diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/EnchantManager.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/EnchantManager.java index e65f2d7..e412912 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/EnchantManager.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/EnchantManager.java @@ -4,6 +4,7 @@ import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.EnchantmentTarget; import org.bukkit.entity.LivingEntity; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.ItemMeta; @@ -11,21 +12,28 @@ import org.bukkit.potion.PotionEffect; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import su.nexmedia.engine.api.manager.AbstractManager; +import su.nexmedia.engine.utils.EntityUtil; import su.nexmedia.engine.utils.ItemUtil; import su.nexmedia.engine.utils.random.Rnd; import su.nightexpress.excellentenchants.ExcellentEnchants; +import su.nightexpress.excellentenchants.ExcellentEnchantsAPI; import su.nightexpress.excellentenchants.api.enchantment.ExcellentEnchant; +import su.nightexpress.excellentenchants.api.enchantment.IEnchantPotionTemplate; +import su.nightexpress.excellentenchants.api.enchantment.type.PassiveEnchant; import su.nightexpress.excellentenchants.config.Config; import su.nightexpress.excellentenchants.config.ObtainSettings; import su.nightexpress.excellentenchants.manager.listeners.EnchantGenericListener; import su.nightexpress.excellentenchants.manager.listeners.EnchantHandlerListener; import su.nightexpress.excellentenchants.manager.object.EnchantListGUI; +import su.nightexpress.excellentenchants.manager.object.EnchantPopulator; import su.nightexpress.excellentenchants.manager.object.EnchantTier; import su.nightexpress.excellentenchants.manager.tasks.ArrowTrailsTask; import su.nightexpress.excellentenchants.manager.tasks.EnchantEffectPassiveTask; import su.nightexpress.excellentenchants.manager.type.ObtainType; import java.util.*; +import java.util.function.BiFunction; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -76,19 +84,6 @@ public class EnchantManager extends AbstractManager { return enchantListGUI; } - public static void addPotionEffect(@NotNull LivingEntity entity, @NotNull PotionEffect effect, boolean compat) { - if (compat) { - PotionEffect has = entity.getPotionEffect(effect.getType()); - if (has != null && has.getAmplifier() > effect.getAmplifier()) { - return; - } - } - else { - entity.removePotionEffect(effect.getType()); - } - entity.addPotionEffect(effect); - } - public static boolean isEnchantable(@NotNull ItemStack item) { if (item.getType().isAir()) return false; @@ -96,28 +91,73 @@ public class EnchantManager extends AbstractManager { /*|| ItemUtil.isWeapon(item) || ItemUtil.isArmor(item) || ItemUtil.isTool(item) || ItemUtil.isBow(item)*/; } - public static boolean populateEnchantments(@NotNull ItemStack item, @NotNull ObtainType obtainType) { + @NotNull + public static Map getEnchantsToPopulate(@NotNull ItemStack item, @NotNull ObtainType obtainType) { + return getEnchantsToPopulate(item, obtainType, new HashMap<>(), (enchant) -> enchant.generateLevel(obtainType)); + } + + @NotNull + public static Map getEnchantsToPopulate(@NotNull ItemStack item, @NotNull ObtainType obtainType, + @NotNull Map enchantsPrepared, + @NotNull Function levelFunc) { + Map enchantsToAdd = new HashMap<>(enchantsPrepared); + ObtainSettings settings = Config.getObtainSettings(obtainType); - if (settings == null) return false; + if (settings == null || !Rnd.chance(settings.getEnchantsCustomGenerationChance())) return enchantsToAdd; - if (Rnd.get(true) > settings.getEnchantsCustomGenerationChance()) return false; - - int enchHas = EnchantManager.getItemEnchantsAmount(item); int enchMax = settings.getEnchantsTotalMax(); int enchRoll = Rnd.get(settings.getEnchantsCustomMin(), settings.getEnchantsCustomMax()); - for (int count = 0; (count < enchRoll && count + enchHas < enchMax); count++) { - EnchantTier tier = EnchantManager.getTierByChance(obtainType); - if (tier == null) continue; + // Класс для исключения неудачных попыток. + EnchantPopulator populator = new EnchantPopulator(obtainType, item); - ExcellentEnchant enchant = tier.getEnchant(obtainType, item); - if (enchant == null) continue; + // Херачим до талого, пока нужное количество не будет добавлено + // или не закончатся чары и/или тиры. + while (!populator.isEmpty() && enchRoll > 0) { + // Достигнут максимум чар (любых) для итема, заканчиваем. + if (enchantsToAdd.size() >= enchMax) break; - int level = enchant.generateLevel(obtainType); - EnchantManager.addEnchant(item, enchant, level, false); + EnchantTier tier = populator.getTierByChance(); + if (tier == null) break; // Нет тира? + + ExcellentEnchant enchant = populator.getEnchantByChance(tier); + // В тире нет подходящих чар (вообще) для итема, исключаем и идем дальше. + if (enchant == null) { + populator.getEnchants().remove(tier); + continue; + } + + // Среди уже добавленных чар есть конфликты с тем, что нашли. + // Исключаем, идем дальше. + if (enchantsToAdd.keySet().stream().anyMatch(has -> has.conflictsWith(enchant) || enchant.conflictsWith(has))) { + populator.getEnchants(tier).remove(enchant); + continue; + } + + // Не получилось сгенерировать подходящий уровень. + // Исключаем, идем дальше. + int level = levelFunc.apply(enchant); + if (level < enchant.getStartLevel()) { + populator.getEnchants(tier).remove(enchant); + continue; + } + + // Добавляем чар, засчитываем попытку. + populator.getEnchants(tier).remove(enchant); + enchantsToAdd.put(enchant, level); + enchRoll--; } - EnchantManager.updateItemLoreEnchants(item); - return true; + return enchantsToAdd; + } + + public static boolean populateEnchantments(@NotNull ItemStack item, @NotNull ObtainType obtainType) { + int enchantsHad = EnchantManager.getItemCustomEnchantsAmount(item); + + EnchantManager.getEnchantsToPopulate(item, obtainType).forEach((enchantment, level) -> { + EnchantManager.addEnchant(item, enchantment, level, false); + }); + + return EnchantManager.getItemCustomEnchantsAmount(item) != enchantsHad; } public static void updateItemLoreEnchants(@NotNull ItemStack item) { @@ -149,35 +189,40 @@ public class EnchantManager extends AbstractManager { } } - public static boolean addEnchant(@NotNull ItemStack item, @NotNull ExcellentEnchant ench, int level, boolean force) { - if (!force && !ench.canEnchantItem(item)) return false; + public static boolean addEnchant(@NotNull ItemStack item, @NotNull Enchantment enchantment, int level, boolean force) { + if (!force && !enchantment.canEnchantItem(item)) return false; - EnchantManager.removeEnchant(item, ench); - ItemUtil.addLore(item, ench.getId(), ench.getNameFormatted(level), 0); + EnchantManager.removeEnchant(item, enchantment); + + if (enchantment instanceof ExcellentEnchant excellentEnchant) { + ItemUtil.addLore(item, excellentEnchant.getId(), excellentEnchant.getNameFormatted(level), 0); + } ItemMeta meta = item.getItemMeta(); if (meta == null) return false; - if (meta instanceof EnchantmentStorageMeta meta2) { - if (!meta2.addStoredEnchant(ench, level, true)) return false; + if (meta instanceof EnchantmentStorageMeta storageMeta) { + if (!storageMeta.addStoredEnchant(enchantment, level, true)) return false; } else { - if (!meta.addEnchant(ench, level, true)) return false; + if (!meta.addEnchant(enchantment, level, true)) return false; } item.setItemMeta(meta); return true; } - public static void removeEnchant(@NotNull ItemStack item, @NotNull ExcellentEnchant en) { - ItemUtil.delLore(item, en.getId()); + public static void removeEnchant(@NotNull ItemStack item, @NotNull Enchantment enchantment) { + if (enchantment instanceof ExcellentEnchant excellentEnchant) { + ItemUtil.delLore(item, excellentEnchant.getId()); + } ItemMeta meta = item.getItemMeta(); - if (meta instanceof EnchantmentStorageMeta meta2) { - meta2.removeStoredEnchant(en); + if (meta instanceof EnchantmentStorageMeta storageMeta) { + storageMeta.removeStoredEnchant(enchantment); } else { - meta.removeEnchant(en); + meta.removeEnchant(enchantment); } item.setItemMeta(meta); } @@ -204,9 +249,15 @@ public class EnchantManager extends AbstractManager { return EnchantManager.getItemCustomEnchants(item).size(); } + public static int getItemEnchantLevel(@NotNull ItemStack item, @NotNull Enchantment enchantment) { + return getItemEnchants(item).getOrDefault(enchantment, 0); + } + @NotNull public static Map getItemEnchants(@NotNull ItemStack item) { ItemMeta meta = item.getItemMeta(); + if (meta == null) return Collections.emptyMap(); + return (meta instanceof EnchantmentStorageMeta meta2) ? meta2.getStoredEnchants() : meta.getEnchants(); } @@ -214,6 +265,70 @@ public class EnchantManager extends AbstractManager { return EnchantManager.getItemEnchants(item).size(); } + public static int getEnchantmentLevel(@NotNull ItemStack item, @NotNull Enchantment enchant) { + ItemMeta meta = item.getItemMeta(); + if (meta == null) return 0; + + return meta.getEnchantLevel(enchant); + } + + @Nullable + public static ExcellentEnchant getEnchantmentByEffect(@NotNull LivingEntity entity, @NotNull PotionEffect effect) { + Enchantment enchantment = ExcellentEnchantsAPI.PLUGIN.getEnchantNMS().getEnchantmentByEffect(entity, effect); + if (enchantment instanceof ExcellentEnchant enchant) return enchant; + + return null; + } + + public static boolean isEnchantmentEffect(@NotNull LivingEntity entity, @NotNull PotionEffect effect) { + return getEnchantmentByEffect(entity, effect) != null; + } + + public static boolean hasEnchantmentEffect(@NotNull LivingEntity entity, @NotNull ExcellentEnchant enchant) { + return entity.getActivePotionEffects().stream().anyMatch(effect -> enchant.equals(getEnchantmentByEffect(entity, effect))); + } + + @NotNull + public static Map getEquippedEnchantsMax(@NotNull LivingEntity entity) { + return getEquippedEnchants(entity, Math::max); + } + + @NotNull + public static Map getEquippedEnchantsTotal(@NotNull LivingEntity entity) { + return getEquippedEnchants(entity, Integer::sum); + } + + @NotNull + private static Map getEquippedEnchants(@NotNull LivingEntity entity, @NotNull BiFunction remap) { + Map map = new HashMap<>(); + + Map equipment = EntityUtil.getEquippedItems(entity); + equipment.entrySet().stream().filter(entry -> { + if (entry.getValue().getType() == Material.ENCHANTED_BOOK) return false; + if ((entry.getKey() == EquipmentSlot.HAND || entry.getKey() == EquipmentSlot.OFF_HAND) && ItemUtil.isArmor(entry.getValue())) return false; + return true; + }).map(Map.Entry::getValue).map(EnchantManager::getItemCustomEnchants).forEach(itemEnchants -> { + itemEnchants.forEach((enchant, level) -> map.merge(enchant, level, remap)); + }); + return map; + } + + public static int getEquippedEnchantLevelMax(@NotNull LivingEntity entity, @NotNull ExcellentEnchant enchant) { + return getEquippedEnchantsMax(entity).getOrDefault(enchant, 0); + } + + public static int getEquippedEnchantLevelTotal(@NotNull LivingEntity entity, @NotNull ExcellentEnchant enchant) { + return getEquippedEnchantsTotal(entity).getOrDefault(enchant, 0); + } + + public static void updateEquippedEnchantEffects(@NotNull LivingEntity entity) { + getEquippedEnchantsMax(entity).forEach((enchant, level) -> { + if (enchant instanceof PassiveEnchant passiveEnchant && enchant instanceof IEnchantPotionTemplate) { + passiveEnchant.use(entity, level); + } + }); + } + @Nullable public static EnchantTier getTierById(@NotNull String id) { return Config.getTierById(id); diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantElementalProtection.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantElementalProtection.java index b58911c..a6be031 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantElementalProtection.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantElementalProtection.java @@ -15,6 +15,7 @@ import su.nexmedia.engine.utils.NumberUtil; import su.nightexpress.excellentenchants.ExcellentEnchants; import su.nightexpress.excellentenchants.api.enchantment.EnchantPriority; import su.nightexpress.excellentenchants.api.enchantment.IEnchantChanceTemplate; +import su.nightexpress.excellentenchants.manager.EnchantManager; import su.nightexpress.excellentenchants.manager.object.EnchantScaler; import java.util.function.UnaryOperator; @@ -85,7 +86,7 @@ public class EnchantElementalProtection extends IEnchantChanceTemplate { for (ItemStack armor : EntityUtil.getArmor(victim)) { if (armor == null || armor.getType().isAir()) continue; - int level = armor.getEnchantmentLevel(this); + int level = EnchantManager.getItemEnchantLevel(armor, this); if (this.checkTriggerChance(level)) { protectionAmount += this.getProtectionAmount(level); } diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantFlameWalker.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantFlameWalker.java index 0d716d8..1008afa 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantFlameWalker.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantFlameWalker.java @@ -26,6 +26,7 @@ import su.nightexpress.excellentenchants.ExcellentEnchants; import su.nightexpress.excellentenchants.api.enchantment.EnchantPriority; import su.nightexpress.excellentenchants.api.enchantment.IEnchantChanceTemplate; import su.nightexpress.excellentenchants.api.enchantment.type.MoveEnchant; +import su.nightexpress.excellentenchants.manager.EnchantManager; import su.nightexpress.excellentenchants.manager.object.EnchantScaler; import java.util.HashMap; @@ -112,7 +113,8 @@ public class EnchantFlameWalker extends IEnchantChanceTemplate implements MoveEn ItemStack boots = player.getInventory().getBoots(); if (boots == null || boots.getType().isAir()) return; - int level = boots.getEnchantmentLevel(this); + //int level = boots.getEnchantmentLevel(this); + int level = EnchantManager.getItemEnchants(boots).getOrDefault(this, 0); if (level < 1) return; Block bTo = to.getBlock().getRelative(BlockFace.DOWN); @@ -143,7 +145,7 @@ public class EnchantFlameWalker extends IEnchantChanceTemplate implements MoveEn ItemStack boots = equipment.getBoots(); if (boots == null || boots.getType().isAir()) return; - int level = boots.getEnchantmentLevel(this); + int level = EnchantManager.getItemEnchantLevel(boots, this); if (level < 1) return; if (!this.checkTriggerChance(level)) return; diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantRegrowth.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantRegrowth.java index cc6f91b..decce70 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantRegrowth.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantRegrowth.java @@ -4,8 +4,6 @@ import org.bukkit.Particle; import org.bukkit.attribute.Attribute; import org.bukkit.enchantments.EnchantmentTarget; import org.bukkit.entity.LivingEntity; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import su.nexmedia.engine.api.config.JYML; import su.nexmedia.engine.api.manager.ICleanable; @@ -15,11 +13,13 @@ import su.nexmedia.engine.utils.EntityUtil; import su.nexmedia.engine.utils.NumberUtil; import su.nightexpress.excellentenchants.ExcellentEnchants; import su.nightexpress.excellentenchants.api.enchantment.EnchantPriority; +import su.nightexpress.excellentenchants.api.enchantment.ExcellentEnchant; import su.nightexpress.excellentenchants.api.enchantment.IEnchantChanceTemplate; import su.nightexpress.excellentenchants.api.enchantment.type.PassiveEnchant; import su.nightexpress.excellentenchants.manager.object.EnchantScaler; import su.nightexpress.excellentenchants.manager.tasks.AbstractEnchantPassiveTask; +import java.util.Map; import java.util.function.UnaryOperator; public class EnchantRegrowth extends IEnchantChanceTemplate implements PassiveEnchant, ICleanable { @@ -115,11 +115,8 @@ public class EnchantRegrowth extends IEnchantChanceTemplate implements PassiveEn } @Override - protected void apply(@NotNull LivingEntity entity, @NotNull ItemStack armor, @NotNull ItemMeta meta) { - int level = meta.getEnchantLevel(EnchantRegrowth.this); - if (level < 1) return; - - use(entity, level); + protected void apply(@NotNull LivingEntity entity, @NotNull Map enchants) { + use(entity, enchants.getOrDefault(EnchantRegrowth.this, 0)); } } } diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantSaturation.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantSaturation.java index 8733caa..1a2dd17 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantSaturation.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/armor/EnchantSaturation.java @@ -3,8 +3,6 @@ package su.nightexpress.excellentenchants.manager.enchants.armor; import org.bukkit.enchantments.EnchantmentTarget; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import su.nexmedia.engine.api.config.JYML; import su.nexmedia.engine.api.manager.ICleanable; @@ -12,11 +10,13 @@ import su.nexmedia.engine.manager.leveling.Scaler; import su.nexmedia.engine.utils.NumberUtil; import su.nightexpress.excellentenchants.ExcellentEnchants; import su.nightexpress.excellentenchants.api.enchantment.EnchantPriority; +import su.nightexpress.excellentenchants.api.enchantment.ExcellentEnchant; import su.nightexpress.excellentenchants.api.enchantment.IEnchantChanceTemplate; import su.nightexpress.excellentenchants.api.enchantment.type.PassiveEnchant; import su.nightexpress.excellentenchants.manager.object.EnchantScaler; import su.nightexpress.excellentenchants.manager.tasks.AbstractEnchantPassiveTask; +import java.util.Map; import java.util.function.UnaryOperator; public class EnchantSaturation extends IEnchantChanceTemplate implements PassiveEnchant, ICleanable { @@ -86,8 +86,8 @@ public class EnchantSaturation extends IEnchantChanceTemplate implements Passive public boolean use(@NotNull LivingEntity entity, int level) { if (!this.isEnchantmentAvailable(entity)) return false; if (!(entity instanceof Player player)) return false; - if (!this.checkTriggerChance(level)) return false; if (player.getFoodLevel() >= 20) return false; + if (!this.checkTriggerChance(level)) return false; if (!this.takeCostItem(player)) return false; float amount = (float) this.getSaturationAmount(level); @@ -103,11 +103,9 @@ public class EnchantSaturation extends IEnchantChanceTemplate implements Passive } @Override - protected void apply(@NotNull LivingEntity entity, @NotNull ItemStack armor, @NotNull ItemMeta meta) { - int level = meta.getEnchantLevel(EnchantSaturation.this); - if (level < 1) return; - - use(entity, level); + protected void apply(@NotNull LivingEntity entity, @NotNull Map enchants) { + // TODO Need to use SUM level and bypass the Scaler cap + use(entity, enchants.getOrDefault(EnchantSaturation.this, 0)); } } } diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/bow/EnchantGhast.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/bow/EnchantGhast.java index eeed82d..bd3471d 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/bow/EnchantGhast.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/bow/EnchantGhast.java @@ -17,6 +17,7 @@ import su.nightexpress.excellentenchants.ExcellentEnchants; import su.nightexpress.excellentenchants.api.enchantment.EnchantPriority; import su.nightexpress.excellentenchants.api.enchantment.IEnchantChanceTemplate; import su.nightexpress.excellentenchants.api.enchantment.type.BowEnchant; +import su.nightexpress.excellentenchants.manager.EnchantManager; import su.nightexpress.excellentenchants.manager.object.EnchantScaler; public class EnchantGhast extends IEnchantChanceTemplate implements BowEnchant { @@ -93,7 +94,7 @@ public class EnchantGhast extends IEnchantChanceTemplate implements BowEnchant { @Override public boolean use(@NotNull EntityDamageByEntityEvent e, @NotNull LivingEntity damager, @NotNull LivingEntity victim, @NotNull ItemStack weapon, int level) { // Support for the 'Power' enchantment. - int power = weapon.getEnchantmentLevel(Enchantment.ARROW_DAMAGE); + int power = EnchantManager.getEnchantmentLevel(weapon, Enchantment.ARROW_DAMAGE); if (power < 1) return false; double damagePower = 0.5 + power * 0.5; diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/tool/EnchantCurseOfBreaking.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/tool/EnchantCurseOfBreaking.java index 21785fe..8ca176b 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/tool/EnchantCurseOfBreaking.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/tool/EnchantCurseOfBreaking.java @@ -13,6 +13,7 @@ import su.nexmedia.engine.utils.NumberUtil; import su.nightexpress.excellentenchants.ExcellentEnchants; import su.nightexpress.excellentenchants.api.enchantment.EnchantPriority; import su.nightexpress.excellentenchants.api.enchantment.IEnchantChanceTemplate; +import su.nightexpress.excellentenchants.manager.EnchantManager; import su.nightexpress.excellentenchants.manager.object.EnchantScaler; import java.util.function.UnaryOperator; @@ -63,7 +64,7 @@ public class EnchantCurseOfBreaking extends IEnchantChanceTemplate { if (!this.isEnchantmentAvailable(player)) return; ItemStack item = e.getItem(); - int level = item.getEnchantmentLevel(this); + int level = EnchantManager.getItemEnchantLevel(item, this); if (level < 1) return; if (!this.checkTriggerChance(level)) return; diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/tool/EnchantSilkChest.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/tool/EnchantSilkChest.java index 3b7c9a6..a5aea8e 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/tool/EnchantSilkChest.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/tool/EnchantSilkChest.java @@ -12,6 +12,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.block.BlockDropItemEvent; import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.inventory.InventoryType; @@ -160,15 +161,24 @@ public class EnchantSilkChest extends IEnchantChanceTemplate implements CustomDr @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onSilkChestStore(InventoryClickEvent e) { - Inventory inv = e.getInventory(); - if (inv.getType() == InventoryType.CRAFTING || inv.getType() == InventoryType.CREATIVE) return; + Inventory inventory = e.getInventory(); + if (inventory.getType() == InventoryType.CRAFTING || inventory.getType() == InventoryType.CREATIVE) return; - ItemStack item = e.getCurrentItem(); - if (item == null || item.getType().isAir()) return; - - if (this.isSilkChest(item)) { - e.setCancelled(true); + Player player = (Player) e.getWhoClicked(); + ItemStack item; + if (e.getHotbarButton() >= 0) { + item = player.getInventory().getItem(e.getHotbarButton()); } + else item = e.getCurrentItem(); + + if (item == null || item.getType().isAir() || !this.isSilkChest(item)) return; + + Inventory clicked = e.getClickedInventory(); + if (e.getClick() != ClickType.NUMBER_KEY) { + if (clicked != null && clicked.equals(e.getView().getTopInventory())) return; + } + + e.setCancelled(true); } @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/tool/EnchantTelekinesis.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/tool/EnchantTelekinesis.java index cfa79f6..2fdae73 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/tool/EnchantTelekinesis.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/tool/EnchantTelekinesis.java @@ -75,17 +75,17 @@ public class EnchantTelekinesis extends IEnchantChanceTemplate implements Custom } @Override - public void handleDrop(@NotNull EnchantDropContainer e, @NotNull Player player, @NotNull ItemStack item, int level) { - BlockDropItemEvent parent = e.getParent(); - Block block = parent.getBlockState().getBlock(); + public void handleDrop(@NotNull EnchantDropContainer container, @NotNull Player player, @NotNull ItemStack item, int level) { + BlockDropItemEvent dropItemEvent = container.getParent(); + Block block = dropItemEvent.getBlockState().getBlock(); if (!this.isEnchantmentAvailable(player)) return; //if (block.getState() instanceof Container) return; if (!this.checkTriggerChance(level)) return; List drops = new ArrayList<>(); - drops.addAll(parent.getItems().stream().map(Item::getItemStack).toList()); - drops.addAll(e.getDrop()); + drops.addAll(dropItemEvent.getItems().stream().map(Item::getItemStack).toList()); + drops.addAll(container.getDrop()); drops.removeIf(Objects::isNull); StringBuilder builder = new StringBuilder(); @@ -99,7 +99,7 @@ public class EnchantTelekinesis extends IEnchantChanceTemplate implements Custom }); this.messageDropReceived.replace("%items%", builder.toString()).send(player); - e.getDrop().clear(); - parent.getItems().clear(); + container.getDrop().clear(); + dropItemEvent.getItems().clear(); } } diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/weapon/EnchantInfernus.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/weapon/EnchantInfernus.java index c4be70b..720188c 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/weapon/EnchantInfernus.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/enchants/weapon/EnchantInfernus.java @@ -16,6 +16,7 @@ import su.nexmedia.engine.utils.NumberUtil; import su.nightexpress.excellentenchants.ExcellentEnchants; import su.nightexpress.excellentenchants.api.enchantment.EnchantPriority; import su.nightexpress.excellentenchants.api.enchantment.IEnchantChanceTemplate; +import su.nightexpress.excellentenchants.manager.EnchantManager; import su.nightexpress.excellentenchants.manager.object.EnchantScaler; import java.util.function.UnaryOperator; @@ -69,7 +70,7 @@ public class EnchantInfernus extends IEnchantChanceTemplate { ItemStack item = trident.getItem(); - int level = item.getEnchantmentLevel(this); + int level = EnchantManager.getItemEnchantLevel(item, this); if (level <= 0) return; if (!this.checkTriggerChance(level)) return; @@ -84,7 +85,7 @@ public class EnchantInfernus extends IEnchantChanceTemplate { ItemStack item = trident.getItem(); - int level = item.getEnchantmentLevel(this); + int level = EnchantManager.getItemEnchantLevel(item, this); if (level <= 0 || trident.getFireTicks() <= 0) return; int ticks = this.getFireTicks(level); diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/listeners/EnchantGenericListener.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/listeners/EnchantGenericListener.java index 1002087..c5552a6 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/listeners/EnchantGenericListener.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/listeners/EnchantGenericListener.java @@ -2,6 +2,7 @@ package su.nightexpress.excellentenchants.manager.listeners; import org.bukkit.Material; import org.bukkit.block.Chest; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.*; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -13,19 +14,17 @@ import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.PrepareAnvilEvent; import org.bukkit.event.player.PlayerFishEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.world.LootGenerateEvent; import org.bukkit.inventory.*; import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import su.nexmedia.engine.api.manager.AbstractListener; -import su.nexmedia.engine.utils.random.Rnd; import su.nightexpress.excellentenchants.ExcellentEnchants; import su.nightexpress.excellentenchants.api.enchantment.ExcellentEnchant; import su.nightexpress.excellentenchants.config.Config; -import su.nightexpress.excellentenchants.config.ObtainSettings; import su.nightexpress.excellentenchants.manager.EnchantManager; -import su.nightexpress.excellentenchants.manager.object.EnchantTier; import su.nightexpress.excellentenchants.manager.type.ObtainType; import java.util.HashMap; @@ -37,6 +36,16 @@ public class EnchantGenericListener extends AbstractListener super(enchantManager.plugin()); } + @EventHandler(priority = EventPriority.MONITOR) + public void onEnchantPotionEffectQuit(PlayerQuitEvent e) { + Player player = e.getPlayer(); + + player.getActivePotionEffects().stream() + .filter(effect -> EnchantManager.isEnchantmentEffect(player, effect)).forEach(effect -> { + player.removePotionEffect(effect.getType()); + }); + } + // --------------------------------------------------------------- // Handle Anvil // --------------------------------------------------------------- @@ -149,53 +158,32 @@ public class EnchantGenericListener extends AbstractListener // --------------------------------------------------------------- @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onEnchantPopulateEnchantingTable(final EnchantItemEvent e) { - ObtainSettings settings = Config.getObtainSettings(ObtainType.ENCHANTING); - if (settings == null || Rnd.get(true) > settings.getEnchantsCustomGenerationChance()) return; - ItemStack target = e.getItem(); - boolean enchantAdded = false; + Map enchantsPrepared = e.getEnchantsToAdd(); + Map enchantsToPopulate = EnchantManager.getEnchantsToPopulate(target, ObtainType.ENCHANTING, enchantsPrepared, enchant -> enchant.getLevelByEnchantCost(e.getExpLevelCost())); - int enchMax = settings.getEnchantsTotalMax(); - int enchRoll = Rnd.get(settings.getEnchantsCustomMin(), settings.getEnchantsCustomMax()); + enchantsPrepared.putAll(enchantsToPopulate); - for (int count = 0; (count < enchRoll && e.getEnchantsToAdd().size() < enchMax); count++) { - EnchantTier tier = EnchantManager.getTierByChance(ObtainType.ENCHANTING); - if (tier == null) continue; + plugin.getServer().getScheduler().runTask(plugin, () -> { + ItemStack result = e.getInventory().getItem(0); + if (result == null) return; - ExcellentEnchant enchant = tier.getEnchant(ObtainType.ENCHANTING, target); - if (enchant == null) continue; - if (e.getEnchantsToAdd().keySet().stream().anyMatch(add -> add.conflictsWith(enchant) || enchant.conflictsWith(add))) - continue; + // Fix enchantments for Enchant Books. + // Enchants are not added on book because they do not exists in NMS. + // Server gets enchants from NMS to apply it on Book NBT tags. + ItemMeta meta = result.getItemMeta(); + if (meta instanceof EnchantmentStorageMeta storageMeta) { + e.getEnchantsToAdd().forEach((enchantment, level) -> { + if (!storageMeta.hasStoredEnchant(enchantment)) { + storageMeta.addStoredEnchant(enchantment, level, true); + } + }); + result.setItemMeta(storageMeta); + } - int level = enchant.getLevelByEnchantCost(e.getExpLevelCost()); - if (level < 1) continue; - - e.getEnchantsToAdd().put(enchant, level); - enchantAdded = true; - } - - if (enchantAdded) { - plugin.getServer().getScheduler().runTask(plugin, () -> { - ItemStack result = e.getInventory().getItem(0); - if (result == null) return; - - // Fix enchantments for Enchant Books. - // Enchants are not added on book because they do not exists in NMS. - // Server gets enchants from NMS to apply it on Book NBT tags. - ItemMeta meta = result.getItemMeta(); - if (meta instanceof EnchantmentStorageMeta meta2) { - e.getEnchantsToAdd().forEach((en, lvl) -> { - if (!meta2.hasStoredEnchant(en)) { - meta2.addStoredEnchant(en, lvl, true); - } - }); - result.setItemMeta(meta2); - } - - EnchantManager.updateItemLoreEnchants(result); - e.getInventory().setItem(0, result); - }); - } + EnchantManager.updateItemLoreEnchants(result); + e.getInventory().setItem(0, result); + }); } // --------------------------------------------------------------- diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/object/EnchantPopulator.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/object/EnchantPopulator.java new file mode 100644 index 0000000..5b831c6 --- /dev/null +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/object/EnchantPopulator.java @@ -0,0 +1,77 @@ +package su.nightexpress.excellentenchants.manager.object; + +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import su.nexmedia.engine.utils.random.Rnd; +import su.nightexpress.excellentenchants.api.enchantment.ExcellentEnchant; +import su.nightexpress.excellentenchants.config.Config; +import su.nightexpress.excellentenchants.manager.type.ObtainType; + +import java.util.*; +import java.util.stream.Collectors; + +public class EnchantPopulator { + + private final ObtainType obtainType; + private final ItemStack item; + private final Map> enchants; + + public EnchantPopulator(@NotNull ObtainType obtainType, @NotNull ItemStack item) { + this.obtainType = obtainType; + this.item = item; + this.enchants = Config.getTiers().stream() + .collect(Collectors.toMap(k -> k, v -> v.getEnchants(obtainType, item), (prev, add) -> add, HashMap::new)); + } + + public boolean isEmpty() { + return this.getEnchants().isEmpty() || this.getEnchants().values().stream().allMatch(Set::isEmpty); + } + + public boolean isEmpty(@NotNull EnchantTier tier) { + return this.getEnchants(tier).isEmpty(); + } + + public void purge(@NotNull EnchantTier tier) { + this.getEnchants().remove(tier); + } + + public void purge(@NotNull EnchantTier tier, @NotNull ExcellentEnchant enchant) { + this.getEnchants(tier).remove(enchant); + this.getEnchants().keySet().removeIf(this::isEmpty); + } + + @NotNull + public ObtainType getObtainType() { + return obtainType; + } + + @NotNull + public ItemStack getItem() { + return item; + } + + @NotNull + public Map> getEnchants() { + return this.enchants; + } + + @NotNull + public Set getEnchants(@NotNull EnchantTier tier) { + return this.getEnchants().getOrDefault(tier, new HashSet<>()); + } + + @Nullable + public EnchantTier getTierByChance() { + Map map = this.getEnchants().keySet().stream() + .collect(Collectors.toMap(k -> k, v -> v.getChance(this.getObtainType()))); + return Rnd.get(map); + } + + @Nullable + public ExcellentEnchant getEnchantByChance(@NotNull EnchantTier tier) { + Map map = this.getEnchants(tier).stream() + .collect(Collectors.toMap(k -> k, v -> v.getObtainChance(this.getObtainType()))); + return map.isEmpty() ? null : Rnd.get(map); + } +} diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/object/EnchantTier.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/object/EnchantTier.java index 5b7f144..3fb1ea0 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/object/EnchantTier.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/object/EnchantTier.java @@ -4,7 +4,6 @@ import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import su.nexmedia.engine.utils.StringUtil; -import su.nexmedia.engine.utils.random.Rnd; import su.nightexpress.excellentenchants.api.enchantment.ExcellentEnchant; import su.nightexpress.excellentenchants.manager.type.ObtainType; @@ -73,22 +72,10 @@ public class EnchantTier { @NotNull public Set getEnchants(@NotNull ObtainType obtainType, @Nullable ItemStack item) { Set set = this.getEnchants().stream() - .filter(en -> en.getObtainChance(obtainType) > 0) - .filter(en -> item == null || en.canEnchantItem(item)).collect(Collectors.toSet()); - set.removeIf(en -> obtainType == ObtainType.ENCHANTING && en.isTreasure()); + .filter(enchant -> enchant.getObtainChance(obtainType) > 0) + .filter(enchant -> item == null || enchant.canEnchantItem(item)) + .collect(Collectors.toCollection(HashSet::new)); + set.removeIf(enchant -> obtainType == ObtainType.ENCHANTING && enchant.isTreasure()); return set; } - - @Nullable - public ExcellentEnchant getEnchant(@NotNull ObtainType obtainType) { - return getEnchant(obtainType, null); - } - - @Nullable - public ExcellentEnchant getEnchant(@NotNull ObtainType obtainType, @Nullable ItemStack item) { - Map map = this.getEnchants(obtainType).stream() - .filter(en -> item == null || en.canEnchantItem(item)) - .collect(Collectors.toMap(k -> k, v -> v.getObtainChance(obtainType))); - return map.isEmpty() ? null : Rnd.get(map); - } } diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/tasks/AbstractEnchantPassiveTask.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/tasks/AbstractEnchantPassiveTask.java index 9cf72ed..93647b4 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/tasks/AbstractEnchantPassiveTask.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/tasks/AbstractEnchantPassiveTask.java @@ -1,44 +1,31 @@ package su.nightexpress.excellentenchants.manager.tasks; -import org.bukkit.Material; import org.bukkit.entity.LivingEntity; -import org.bukkit.inventory.EntityEquipment; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import su.nexmedia.engine.api.task.AbstractTask; -import su.nexmedia.engine.utils.EntityUtil; -import su.nexmedia.engine.utils.ItemUtil; import su.nightexpress.excellentenchants.ExcellentEnchants; +import su.nightexpress.excellentenchants.api.enchantment.ExcellentEnchant; import su.nightexpress.excellentenchants.config.Config; +import su.nightexpress.excellentenchants.manager.EnchantManager; -import java.util.*; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +@Deprecated public abstract class AbstractEnchantPassiveTask extends AbstractTask { public AbstractEnchantPassiveTask(@NotNull ExcellentEnchants plugin, long interval, boolean async) { super(plugin, interval, async); } - protected abstract void apply(@NotNull LivingEntity entity, @NotNull ItemStack armor, @NotNull ItemMeta meta); + protected abstract void apply(@NotNull LivingEntity entity, @NotNull Map enchants); @Override public void action() { for (LivingEntity entity : this.getEntities()) { - - List list = new ArrayList<>(Arrays.asList(EntityUtil.getArmor(entity))); - EntityEquipment equipment = entity.getEquipment(); - if (equipment != null && !ItemUtil.isArmor(equipment.getItemInMainHand())) { - list.add(equipment.getItemInMainHand()); - } - list.removeIf(armor -> armor == null || armor.getType().isAir() || armor.getType() == Material.ENCHANTED_BOOK); - - for (ItemStack armor : list) { - ItemMeta meta = armor.getItemMeta(); - if (meta == null) continue; - - this.apply(entity, armor, meta); - } + this.apply(entity, EnchantManager.getEquippedEnchantsMax(entity)); } } diff --git a/Core/src/main/java/su/nightexpress/excellentenchants/manager/tasks/EnchantEffectPassiveTask.java b/Core/src/main/java/su/nightexpress/excellentenchants/manager/tasks/EnchantEffectPassiveTask.java index d861dcb..bd5f123 100644 --- a/Core/src/main/java/su/nightexpress/excellentenchants/manager/tasks/EnchantEffectPassiveTask.java +++ b/Core/src/main/java/su/nightexpress/excellentenchants/manager/tasks/EnchantEffectPassiveTask.java @@ -1,28 +1,22 @@ package su.nightexpress.excellentenchants.manager.tasks; import org.bukkit.entity.LivingEntity; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import su.nightexpress.excellentenchants.ExcellentEnchants; -import su.nightexpress.excellentenchants.api.enchantment.IEnchantPotionTemplate; -import su.nightexpress.excellentenchants.api.enchantment.type.PassiveEnchant; +import su.nightexpress.excellentenchants.api.enchantment.ExcellentEnchant; import su.nightexpress.excellentenchants.config.Config; +import su.nightexpress.excellentenchants.manager.EnchantManager; + +import java.util.Map; public class EnchantEffectPassiveTask extends AbstractEnchantPassiveTask { public EnchantEffectPassiveTask(@NotNull ExcellentEnchants plugin) { - super(plugin, Config.TASKS_PASSIVE_ENCHANTS_TICKS_INTERVAL, false); + super(plugin, Config.TASKS_PASSIVE_POTION_EFFECTS_APPLY_INTERVAL.get(), false); } @Override - protected void apply(@NotNull LivingEntity entity, @NotNull ItemStack armor, @NotNull ItemMeta meta) { - meta.getEnchants().forEach((enchantment, level) -> { - if (level < 1) return; - if (!(enchantment instanceof PassiveEnchant passiveEnchant)) return; - if (!(enchantment instanceof IEnchantPotionTemplate)) return; - - passiveEnchant.use(entity, level); - }); + protected void apply(@NotNull LivingEntity entity, @NotNull Map enchants) { + EnchantManager.updateEquippedEnchantEffects(entity); } } diff --git a/Core/src/main/resources/config.yml b/Core/src/main/resources/config.yml index dc7d9fd..df80ae3 100644 --- a/Core/src/main/resources/config.yml +++ b/Core/src/main/resources/config.yml @@ -20,9 +20,6 @@ General: Arrow_Trails: # Defines the ticks interval (20 ticks = 1 second) for the arrow particle effect spawning. Ticks_Interval: 1 - Passive_Enchants: - # Defines the ticks interval (20 ticks = 1 second) for the passive enchantment effects to trigger. - Ticks_Interval: 100 # Enchantment global settings. Enchantments: # List of disabled custom enchantments. diff --git a/NMS/pom.xml b/NMS/pom.xml index 7d1f2eb..82cd19c 100644 --- a/NMS/pom.xml +++ b/NMS/pom.xml @@ -5,7 +5,7 @@ ExcellentEnchants su.nightexpress.excellentenchants - 3.2.7 + 3.2.8 4.0.0 diff --git a/NMS/src/main/java/su/nightexpress/excellentenchants/nms/EnchantNMS.java b/NMS/src/main/java/su/nightexpress/excellentenchants/nms/EnchantNMS.java index c675ddc..8a40271 100644 --- a/NMS/src/main/java/su/nightexpress/excellentenchants/nms/EnchantNMS.java +++ b/NMS/src/main/java/su/nightexpress/excellentenchants/nms/EnchantNMS.java @@ -2,12 +2,39 @@ package su.nightexpress.excellentenchants.nms; import org.bukkit.Location; import org.bukkit.block.Block; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.LivingEntity; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.potion.PotionEffect; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import su.nexmedia.engine.utils.EntityUtil; +import java.util.Arrays; import java.util.Set; public interface EnchantNMS { + // TODO Move in 'API' module? + @Deprecated + static int getEnchantmentLevel(@NotNull ItemStack item, @NotNull Enchantment enchant) { + ItemMeta meta = item.getItemMeta(); + if (meta == null) return 0; + + return meta.getEnchantLevel(enchant); + } + + // TODO Move in 'API' module? + @Deprecated + static int getEquippedEnchantLevel(@NotNull LivingEntity entity, @NotNull Enchantment enchant) { + return Arrays.stream(EntityUtil.getEquipment(entity)).filter(enchant::canEnchantItem) + .map(item -> getEnchantmentLevel(item, enchant)).max(Integer::compareTo).orElse(0); + } + + void addEnchantmentEffect(@NotNull LivingEntity entity, @NotNull Enchantment enchant, @NotNull PotionEffect effect); + + @Nullable Enchantment getEnchantmentByEffect(@NotNull LivingEntity entity, @NotNull PotionEffect type); + @NotNull Set handleFlameWalker(@NotNull LivingEntity entity, @NotNull Location location, int level); } diff --git a/V1_17_R1/pom.xml b/V1_17_R1/pom.xml index 9df79f3..a7f8cc0 100644 --- a/V1_17_R1/pom.xml +++ b/V1_17_R1/pom.xml @@ -5,7 +5,7 @@ ExcellentEnchants su.nightexpress.excellentenchants - 3.2.7 + 3.2.8 4.0.0 @@ -26,7 +26,7 @@ su.nightexpress.excellentenchants NMS - 3.2.7 + 3.2.8 diff --git a/V1_17_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_17_R1/CustomEffectInstance.java b/V1_17_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_17_R1/CustomEffectInstance.java new file mode 100644 index 0000000..c0d7909 --- /dev/null +++ b/V1_17_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_17_R1/CustomEffectInstance.java @@ -0,0 +1,39 @@ +package su.nightexpress.excellentenchants.nms.v1_17_R1; + +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.effect.MobEffectInstance; +import net.minecraft.world.entity.LivingEntity; +import org.bukkit.enchantments.Enchantment; +import org.jetbrains.annotations.NotNull; +import su.nexmedia.engine.utils.Reflex; +import su.nightexpress.excellentenchants.nms.EnchantNMS; + +public class CustomEffectInstance extends MobEffectInstance { + + private static final int MIN = 60 * 60 * 20; + + private final Enchantment enchantment; + + public CustomEffectInstance(MobEffect effect, int amplifier, @NotNull Enchantment enchantment) { + super(effect, Integer.MAX_VALUE, amplifier); + this.enchantment = enchantment; + } + + @NotNull + public Enchantment getEnchantment() { + return enchantment; + } + + public boolean tick(LivingEntity entity, Runnable runnable) { + if (EnchantNMS.getEquippedEnchantLevel((org.bukkit.entity.LivingEntity) entity.getBukkitEntity(), this.getEnchantment()) <= 0) { + return false; + } + if (super.tick(entity, runnable)) { + if (this.getDuration() <= MIN) { + Reflex.setFieldValue(this, "c", Integer.MAX_VALUE); + } + return true; + } + return false; + } +} diff --git a/V1_17_R1/src/main/java/su/nightexpress/excellentenchants/nms/V1_17_R1.java b/V1_17_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_17_R1/V1_17_R1.java similarity index 66% rename from V1_17_R1/src/main/java/su/nightexpress/excellentenchants/nms/V1_17_R1.java rename to V1_17_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_17_R1/V1_17_R1.java index afdee7a..bc6ced2 100644 --- a/V1_17_R1/src/main/java/su/nightexpress/excellentenchants/nms/V1_17_R1.java +++ b/V1_17_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_17_R1/V1_17_R1.java @@ -1,7 +1,9 @@ -package su.nightexpress.excellentenchants.nms; +package su.nightexpress.excellentenchants.nms.v1_17_R1; import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.entity.Entity; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.LiquidBlock; @@ -12,14 +14,36 @@ import org.bukkit.block.Block; import org.bukkit.craftbukkit.v1_17_R1.CraftWorld; import org.bukkit.craftbukkit.v1_17_R1.entity.CraftLivingEntity; import org.bukkit.craftbukkit.v1_17_R1.event.CraftEventFactory; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.LivingEntity; +import org.bukkit.event.entity.EntityPotionEffectEvent; +import org.bukkit.potion.PotionEffect; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import su.nightexpress.excellentenchants.nms.EnchantNMS; import java.util.HashSet; import java.util.Set; public class V1_17_R1 implements EnchantNMS { + @Override + public void addEnchantmentEffect(@NotNull LivingEntity entity, @NotNull Enchantment enchant, @NotNull PotionEffect effect) { + net.minecraft.world.entity.LivingEntity entity1 = ((CraftLivingEntity)entity).getHandle(); + entity1.addEffect(new CustomEffectInstance(MobEffect.byId(effect.getType().getId()), effect.getAmplifier(), enchant), EntityPotionEffectEvent.Cause.PLUGIN); + } + + @Override + @Nullable + public Enchantment getEnchantmentByEffect(@NotNull LivingEntity entity, @NotNull PotionEffect type) { + net.minecraft.world.entity.LivingEntity entity1 = ((CraftLivingEntity)entity).getHandle(); + MobEffectInstance handle = entity1.getEffect(MobEffect.byId(type.getType().getId())); + if (handle instanceof CustomEffectInstance instance) { + return instance.getEnchantment(); + } + return null; + } + @Override @NotNull public Set handleFlameWalker(@NotNull LivingEntity bukkitEntity, @NotNull Location location, int level) { diff --git a/V1_18_R2/pom.xml b/V1_18_R2/pom.xml index 74cd5c7..1eb72be 100644 --- a/V1_18_R2/pom.xml +++ b/V1_18_R2/pom.xml @@ -5,7 +5,7 @@ ExcellentEnchants su.nightexpress.excellentenchants - 3.2.7 + 3.2.8 4.0.0 @@ -26,7 +26,7 @@ su.nightexpress.excellentenchants NMS - 3.2.7 + 3.2.8 diff --git a/V1_18_R2/src/main/java/su/nightexpress/excellentenchants/nms/v1_18_R2/CustomEffectInstance.java b/V1_18_R2/src/main/java/su/nightexpress/excellentenchants/nms/v1_18_R2/CustomEffectInstance.java new file mode 100644 index 0000000..a644d72 --- /dev/null +++ b/V1_18_R2/src/main/java/su/nightexpress/excellentenchants/nms/v1_18_R2/CustomEffectInstance.java @@ -0,0 +1,39 @@ +package su.nightexpress.excellentenchants.nms.v1_18_R2; + +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.effect.MobEffectInstance; +import net.minecraft.world.entity.LivingEntity; +import org.bukkit.enchantments.Enchantment; +import org.jetbrains.annotations.NotNull; +import su.nexmedia.engine.utils.Reflex; +import su.nightexpress.excellentenchants.nms.EnchantNMS; + +public class CustomEffectInstance extends MobEffectInstance { + + private static final int MIN = 60 * 60 * 20; + + private final Enchantment enchantment; + + public CustomEffectInstance(MobEffect effect, int amplifier, @NotNull Enchantment enchantment) { + super(effect, Integer.MAX_VALUE, amplifier); + this.enchantment = enchantment; + } + + @NotNull + public Enchantment getEnchantment() { + return enchantment; + } + + public boolean tick(LivingEntity entity, Runnable runnable) { + if (EnchantNMS.getEquippedEnchantLevel((org.bukkit.entity.LivingEntity) entity.getBukkitEntity(), this.getEnchantment()) <= 0) { + return false; + } + if (super.tick(entity, runnable)) { + if (this.getDuration() <= MIN) { + Reflex.setFieldValue(this, "c", Integer.MAX_VALUE); + } + return true; + } + return false; + } +} diff --git a/V1_18_R2/src/main/java/su/nightexpress/excellentenchants/nms/V1_18_R2.java b/V1_18_R2/src/main/java/su/nightexpress/excellentenchants/nms/v1_18_R2/V1_18_R2.java similarity index 66% rename from V1_18_R2/src/main/java/su/nightexpress/excellentenchants/nms/V1_18_R2.java rename to V1_18_R2/src/main/java/su/nightexpress/excellentenchants/nms/v1_18_R2/V1_18_R2.java index abf9ab5..15dc5af 100644 --- a/V1_18_R2/src/main/java/su/nightexpress/excellentenchants/nms/V1_18_R2.java +++ b/V1_18_R2/src/main/java/su/nightexpress/excellentenchants/nms/v1_18_R2/V1_18_R2.java @@ -1,7 +1,9 @@ -package su.nightexpress.excellentenchants.nms; +package su.nightexpress.excellentenchants.nms.v1_18_R2; import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.entity.Entity; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.LiquidBlock; @@ -12,14 +14,36 @@ import org.bukkit.block.Block; import org.bukkit.craftbukkit.v1_18_R2.CraftWorld; import org.bukkit.craftbukkit.v1_18_R2.entity.CraftLivingEntity; import org.bukkit.craftbukkit.v1_18_R2.event.CraftEventFactory; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.LivingEntity; +import org.bukkit.event.entity.EntityPotionEffectEvent; +import org.bukkit.potion.PotionEffect; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import su.nightexpress.excellentenchants.nms.EnchantNMS; import java.util.HashSet; import java.util.Set; public class V1_18_R2 implements EnchantNMS { + @Override + public void addEnchantmentEffect(@NotNull LivingEntity entity, @NotNull Enchantment enchant, @NotNull PotionEffect effect) { + net.minecraft.world.entity.LivingEntity entity1 = ((CraftLivingEntity)entity).getHandle(); + entity1.addEffect(new CustomEffectInstance(MobEffect.byId(effect.getType().getId()), effect.getAmplifier(), enchant), EntityPotionEffectEvent.Cause.PLUGIN); + } + + @Override + @Nullable + public Enchantment getEnchantmentByEffect(@NotNull LivingEntity entity, @NotNull PotionEffect type) { + net.minecraft.world.entity.LivingEntity entity1 = ((CraftLivingEntity)entity).getHandle(); + MobEffectInstance handle = entity1.getEffect(MobEffect.byId(type.getType().getId())); + if (handle instanceof CustomEffectInstance instance) { + return instance.getEnchantment(); + } + return null; + } + @Override @NotNull public Set handleFlameWalker(@NotNull LivingEntity bukkitEntity, @NotNull Location location, int level) { diff --git a/V1_19_R1/pom.xml b/V1_19_R1/pom.xml index fd308ef..e4f3bf3 100644 --- a/V1_19_R1/pom.xml +++ b/V1_19_R1/pom.xml @@ -5,7 +5,7 @@ ExcellentEnchants su.nightexpress.excellentenchants - 3.2.7 + 3.2.8 4.0.0 @@ -26,7 +26,7 @@ su.nightexpress.excellentenchants NMS - 3.2.7 + 3.2.8 diff --git a/V1_19_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_19_R1/CustomEffectInstance.java b/V1_19_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_19_R1/CustomEffectInstance.java new file mode 100644 index 0000000..facfcdf --- /dev/null +++ b/V1_19_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_19_R1/CustomEffectInstance.java @@ -0,0 +1,39 @@ +package su.nightexpress.excellentenchants.nms.v1_19_R1; + +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.effect.MobEffectInstance; +import net.minecraft.world.entity.LivingEntity; +import org.bukkit.enchantments.Enchantment; +import org.jetbrains.annotations.NotNull; +import su.nexmedia.engine.utils.Reflex; +import su.nightexpress.excellentenchants.nms.EnchantNMS; + +public class CustomEffectInstance extends MobEffectInstance { + + private static final int MIN = 60 * 60 * 20; + + private final Enchantment enchantment; + + public CustomEffectInstance(MobEffect effect, int amplifier, @NotNull Enchantment enchantment) { + super(effect, Integer.MAX_VALUE, amplifier); + this.enchantment = enchantment; + } + + @NotNull + public Enchantment getEnchantment() { + return enchantment; + } + + public boolean tick(LivingEntity entity, Runnable runnable) { + if (EnchantNMS.getEquippedEnchantLevel((org.bukkit.entity.LivingEntity) entity.getBukkitEntity(), this.getEnchantment()) <= 0) { + return false; + } + if (super.tick(entity, runnable)) { + if (this.getDuration() <= MIN) { + Reflex.setFieldValue(this, "c", Integer.MAX_VALUE); + } + return true; + } + return false; + } +} diff --git a/V1_19_R1/src/main/java/su/nightexpress/excellentenchants/nms/V1_19_R1.java b/V1_19_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_19_R1/V1_19_R1.java similarity index 66% rename from V1_19_R1/src/main/java/su/nightexpress/excellentenchants/nms/V1_19_R1.java rename to V1_19_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_19_R1/V1_19_R1.java index a76d34a..88e9977 100644 --- a/V1_19_R1/src/main/java/su/nightexpress/excellentenchants/nms/V1_19_R1.java +++ b/V1_19_R1/src/main/java/su/nightexpress/excellentenchants/nms/v1_19_R1/V1_19_R1.java @@ -1,7 +1,9 @@ -package su.nightexpress.excellentenchants.nms; +package su.nightexpress.excellentenchants.nms.v1_19_R1; import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.entity.Entity; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.LiquidBlock; @@ -12,14 +14,36 @@ import org.bukkit.block.Block; import org.bukkit.craftbukkit.v1_19_R1.CraftWorld; import org.bukkit.craftbukkit.v1_19_R1.entity.CraftLivingEntity; import org.bukkit.craftbukkit.v1_19_R1.event.CraftEventFactory; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.LivingEntity; +import org.bukkit.event.entity.EntityPotionEffectEvent; +import org.bukkit.potion.PotionEffect; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import su.nightexpress.excellentenchants.nms.EnchantNMS; import java.util.HashSet; import java.util.Set; public class V1_19_R1 implements EnchantNMS { + @Override + public void addEnchantmentEffect(@NotNull LivingEntity entity, @NotNull Enchantment enchant, @NotNull PotionEffect effect) { + net.minecraft.world.entity.LivingEntity entity1 = ((CraftLivingEntity)entity).getHandle(); + entity1.addEffect(new CustomEffectInstance(MobEffect.byId(effect.getType().getId()), effect.getAmplifier(), enchant), EntityPotionEffectEvent.Cause.PLUGIN); + } + + @Override + @Nullable + public Enchantment getEnchantmentByEffect(@NotNull LivingEntity entity, @NotNull PotionEffect type) { + net.minecraft.world.entity.LivingEntity entity1 = ((CraftLivingEntity)entity).getHandle(); + MobEffectInstance handle = entity1.getEffect(MobEffect.byId(type.getType().getId())); + if (handle instanceof CustomEffectInstance instance) { + return instance.getEnchantment(); + } + return null; + } + @Override @NotNull public Set handleFlameWalker(@NotNull LivingEntity bukkitEntity, @NotNull Location location, int level) { diff --git a/pom.xml b/pom.xml index 45364b1..4e37986 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ su.nightexpress.excellentenchants ExcellentEnchants pom - 3.2.7 + 3.2.8 Core NMS @@ -25,7 +25,7 @@ su.nexmedia NexEngine - 2.2.6 + 2.2.7