From 1043cb754533b25dc2adc5015cb9559a5a381970 Mon Sep 17 00:00:00 2001 From: MaksyKun <77341370+MaksyKun@users.noreply.github.com> Date: Tue, 21 Jan 2025 18:48:40 +0100 Subject: [PATCH] added lightweight possibility to add enchants into existing entries --- .../object/VanillaWrapperListener.java | 25 +++ .../itemgenerator/ItemGeneratorManager.java | 158 +++++++++++------- 2 files changed, 126 insertions(+), 57 deletions(-) diff --git a/src/main/java/studio/magemonkey/divinity/manager/listener/object/VanillaWrapperListener.java b/src/main/java/studio/magemonkey/divinity/manager/listener/object/VanillaWrapperListener.java index a582a4d3..215766d5 100644 --- a/src/main/java/studio/magemonkey/divinity/manager/listener/object/VanillaWrapperListener.java +++ b/src/main/java/studio/magemonkey/divinity/manager/listener/object/VanillaWrapperListener.java @@ -5,12 +5,16 @@ import org.bukkit.entity.*; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; +import org.bukkit.event.enchantment.EnchantItemEvent; +import org.bukkit.event.enchantment.PrepareItemEnchantEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.event.entity.EntityDamageEvent.DamageModifier; import org.bukkit.event.entity.EntityShootBowEvent; import org.bukkit.event.entity.ProjectileLaunchEvent; +import org.bukkit.event.inventory.PrepareAnvilEvent; +import org.bukkit.event.inventory.PrepareGrindstoneEvent; import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.ItemStack; import org.bukkit.metadata.FixedMetadataValue; @@ -33,6 +37,7 @@ import studio.magemonkey.divinity.manager.damage.DamageMeta; import studio.magemonkey.divinity.modules.list.arrows.ArrowManager; import studio.magemonkey.divinity.modules.list.arrows.ArrowManager.QArrow; +import studio.magemonkey.divinity.modules.list.itemgenerator.ItemGeneratorManager; import studio.magemonkey.divinity.stats.EntityStats; import studio.magemonkey.divinity.stats.ProjectileStats; import studio.magemonkey.divinity.stats.items.ItemStats; @@ -471,4 +476,24 @@ private static void scaleValuesWithCore(LivingEntity damager, }); } } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onAnvilUse(PrepareAnvilEvent e) { + ItemStack result = e.getResult(); + if(result == null) return; + ItemGeneratorManager.updateGeneratorItemLore(result); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onEnchantingTable(PrepareItemEnchantEvent e) { + ItemStack result = e.getItem(); + ItemGeneratorManager.updateGeneratorItemLore(result); + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onGrindStone(PrepareGrindstoneEvent e) { + ItemStack result = e.getResult(); + if(result == null) return; + ItemGeneratorManager.updateGeneratorItemLore(result); + } } diff --git a/src/main/java/studio/magemonkey/divinity/modules/list/itemgenerator/ItemGeneratorManager.java b/src/main/java/studio/magemonkey/divinity/modules/list/itemgenerator/ItemGeneratorManager.java index a87f3581..8fee539b 100644 --- a/src/main/java/studio/magemonkey/divinity/modules/list/itemgenerator/ItemGeneratorManager.java +++ b/src/main/java/studio/magemonkey/divinity/modules/list/itemgenerator/ItemGeneratorManager.java @@ -10,6 +10,8 @@ import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BlockStateMeta; import org.bukkit.inventory.meta.ItemMeta; @@ -25,12 +27,14 @@ import studio.magemonkey.codex.config.api.JYML; import studio.magemonkey.codex.core.Version; import studio.magemonkey.codex.util.ItemUT; +import studio.magemonkey.codex.util.NumberUT; import studio.magemonkey.codex.util.Reflex; import studio.magemonkey.codex.util.StringUT; import studio.magemonkey.codex.util.constants.JStrings; import studio.magemonkey.codex.util.random.Rnd; import studio.magemonkey.divinity.Divinity; import studio.magemonkey.divinity.config.Config; +import studio.magemonkey.divinity.config.EngineCfg; import studio.magemonkey.divinity.hooks.EHook; import studio.magemonkey.divinity.hooks.external.FabledHook; import studio.magemonkey.divinity.modules.EModule; @@ -78,16 +82,16 @@ public class ItemGeneratorManager extends QModuleDrop { - public static YamlConfiguration commonItemGenerator; + public static YamlConfiguration commonItemGenerator; @Getter - private static ResourceManager resourceManager; - private ItemAbilityHandler abilityHandler; - - public static final String PLACE_GEN_DAMAGE = "%GENERATOR_DAMAGE%"; - public static final String PLACE_GEN_DEFENSE = "%GENERATOR_DEFENSE%"; - public static final String PLACE_GEN_STATS = "%GENERATOR_STATS%"; - public static final String PLACE_GEN_SOCKETS = "%GENERATOR_SOCKETS_%TYPE%%"; - public static final String PLACE_GEN_ABILITY = "%GENERATOR_SKILLS%"; + private static ResourceManager resourceManager; + private ItemAbilityHandler abilityHandler; + + public static final String PLACE_GEN_DAMAGE = "%GENERATOR_DAMAGE%"; + public static final String PLACE_GEN_DEFENSE = "%GENERATOR_DEFENSE%"; + public static final String PLACE_GEN_STATS = "%GENERATOR_STATS%"; + public static final String PLACE_GEN_SOCKETS = "%GENERATOR_SOCKETS_%TYPE%%"; + public static final String PLACE_GEN_ABILITY = "%GENERATOR_SKILLS%"; public static final String PLACE_GEN_FABLED_ATTR = "%GENERATOR_FABLED_ATTR%"; public ItemGeneratorManager(@NotNull Divinity plugin) { @@ -179,13 +183,13 @@ public class GeneratorItem extends LimitedItem { @Getter private double suffixChance; - private boolean materialsWhitelist; + private boolean materialsWhitelist; private Set materialsList; - private List modelDataList; + private List modelDataList; private Map> modelDataSpecial; - private Map materialModifiers; + private Map materialModifiers; private Map, String>> materialBonuses; private Map, String>> classBonuses; @@ -193,16 +197,16 @@ public class GeneratorItem extends LimitedItem { private TreeMap reqUserClass; private TreeMap reqBannedUserClass; - private int enchantsMinAmount; - private int enchantsMaxAmount; - private boolean enchantsSafeOnly; + private int enchantsMinAmount; + private int enchantsMaxAmount; + private boolean enchantsSafeOnly; @Getter - private boolean enchantsSafeLevels; - private Map enchantsList; - private final TreeMap armorTrims = new TreeMap<>(); + private boolean enchantsSafeLevels; + private Map enchantsList; + private final TreeMap armorTrims = new TreeMap<>(); private Set attributeGenerators; - private AbilityGenerator abilityGenerator; + private AbilityGenerator abilityGenerator; public GeneratorItem(@NotNull Divinity plugin, @NotNull JYML cfg) { super(plugin, cfg, ItemGeneratorManager.this); @@ -216,7 +220,7 @@ public GeneratorItem(@NotNull Divinity plugin, @NotNull JYML cfg) { if (this.materialsWhitelist) { this.materialsList = new HashSet<>(); Set startWildcards = new HashSet<>(); - Set endWildcards = new HashSet<>(); + Set endWildcards = new HashSet<>(); for (String mat : cfg.getStringList(path + "materials.black-list")) { String[] split = mat.split('\\' + JStrings.MASK_ANY, 2); @@ -245,7 +249,7 @@ public GeneratorItem(@NotNull Divinity plugin, @NotNull JYML cfg) { this.materialsList = new HashSet<>(Config.getAllRegisteredMaterials()); Set materials = new HashSet<>(cfg.getStringList(path + "materials.black-list")); this.materialsList.removeIf(matAll -> { - String namespacedID = matAll.getNamespacedID(); + String namespacedID = matAll.getNamespacedID(); String upperCaseNamespacedID = namespacedID.toUpperCase(); for (String mat : materials) { String[] split = mat.split('\\' + JStrings.MASK_ANY, 2); @@ -288,8 +292,8 @@ public GeneratorItem(@NotNull Divinity plugin, @NotNull JYML cfg) { continue; } - BonusMap bMap = new BonusMap(); - String path2 = path + group + "."; + BonusMap bMap = new BonusMap(); + String path2 = path + group + "."; bMap.loadDamages(cfg, path2 + "damage-types"); bMap.loadDefenses(cfg, path2 + "defense-types"); bMap.loadStats(cfg, path2 + "item-stats"); @@ -315,8 +319,8 @@ public GeneratorItem(@NotNull Divinity plugin, @NotNull JYML cfg) { if (sVal == null) continue; String[] split = sVal.split("%", 2); - boolean perc = split.length == 2 && split[1].isEmpty(); - double val = StringUT.getDouble(split[0], 0, true); + boolean perc = split.length == 2 && split[1].isEmpty(); + double val = StringUT.getDouble(split[0], 0, true); statMap.put(dt, val + (perc ? "%" : "")); } @@ -330,8 +334,8 @@ public GeneratorItem(@NotNull Divinity plugin, @NotNull JYML cfg) { if (sVal == null) continue; String[] split = sVal.split("%", 2); - boolean perc = split.length == 2 && split[1].isEmpty(); - double val = StringUT.getDouble(split[0], 0, true); + boolean perc = split.length == 2 && split[1].isEmpty(); + double val = StringUT.getDouble(split[0], 0, true); statMap.put(dt, val + (perc ? "%" : "")); } @@ -347,8 +351,8 @@ public GeneratorItem(@NotNull Divinity plugin, @NotNull JYML cfg) { if (sVal == null) continue; String[] split = sVal.split("%", 2); - boolean perc = split.length == 2 && split[1].isEmpty(); - double val = StringUT.getDouble(split[0], 0, true); + boolean perc = split.length == 2 && split[1].isEmpty(); + double val = StringUT.getDouble(split[0], 0, true); statMap.put(mainStat, val + (perc ? "%" : "")); } @@ -371,8 +375,8 @@ public GeneratorItem(@NotNull Divinity plugin, @NotNull JYML cfg) { if (sVal == null) continue; String[] split = sVal.split("%", 2); - boolean perc = split.length == 2 && split[1].isEmpty(); - double val = StringUT.getDouble(split[0], 0, true); + boolean perc = split.length == 2 && split[1].isEmpty(); + double val = StringUT.getDouble(split[0], 0, true); statMap.put(dt, val + (perc ? "%" : "")); } @@ -386,8 +390,8 @@ public GeneratorItem(@NotNull Divinity plugin, @NotNull JYML cfg) { if (sVal == null) continue; String[] split = sVal.split("%", 2); - boolean perc = split.length == 2 && split[1].isEmpty(); - double val = StringUT.getDouble(split[0], 0, true); + boolean perc = split.length == 2 && split[1].isEmpty(); + double val = StringUT.getDouble(split[0], 0, true); statMap.put(dt, val + (perc ? "%" : "")); } @@ -403,8 +407,8 @@ public GeneratorItem(@NotNull Divinity plugin, @NotNull JYML cfg) { if (sVal == null) continue; String[] split = sVal.split("%", 2); - boolean perc = split.length == 2 && split[1].isEmpty(); - double val = StringUT.getDouble(split[0], 0, true); + boolean perc = split.length == 2 && split[1].isEmpty(); + double val = StringUT.getDouble(split[0], 0, true); statMap.put(mainStat, val + (perc ? "%" : "")); } @@ -623,8 +627,8 @@ public BiFunction getMaterialModifiers(@NotNull ItemSta @NotNull ItemLoreStat stat) { for (Map.Entry e : this.materialModifiers.entrySet()) { if (ItemUtils.compareItemGroup(item, e.getKey())) { - BonusMap bMap = e.getValue(); - BiFunction bif = bMap.getBonus(stat); + BonusMap bMap = e.getValue(); + BiFunction bif = bMap.getBonus(stat); if (bif == null) { continue; } @@ -641,7 +645,7 @@ public Collection getMaterialBonuses(ItemStack item, ItemLoreStat if (!ItemUtils.compareItemGroup(item, entry.getKey())) continue; for (Map.Entry, String> entry1 : entry.getValue().entrySet()) { if (entry1.getKey().equals(stat)) { - String sVal = entry1.getValue(); + String sVal = entry1.getValue(); String[] split = sVal.split("%", 2); list.add(new StatBonus( new double[]{Double.parseDouble(split[0])}, @@ -658,7 +662,7 @@ public Collection getClassBonuses(ItemLoreStat stat) { for (Map.Entry, String>> entry : this.classBonuses.entrySet()) { for (Map.Entry, String> entry1 : entry.getValue().entrySet()) { if (entry1.getKey().equals(stat)) { - String sVal = entry1.getValue(); + String sVal = entry1.getValue(); String[] split = sVal.split("%", 2); list.add(new StatBonus( new double[]{Double.parseDouble(split[0])}, @@ -742,7 +746,7 @@ protected ItemStack build(int itemLvl, int uses, @Nullable ItemType mat) { String prefixItemType = ""; String suffixItemType = ""; - String itemGroupId = ItemUtils.getItemGroupIdFor(item); + String itemGroupId = ItemUtils.getItemGroupIdFor(item); String itemGroupName = ItemUtils.getItemGroupNameFor(item); String itemMaterial = CodexEngine.get().getItemManager().getItemTypes(item).stream() @@ -789,10 +793,10 @@ protected ItemStack build(int itemLvl, int uses, @Nullable ItemType mat) { // +-------------------------+ // TODO More options, mb generator? if (meta instanceof BlockStateMeta) { - BlockStateMeta bmeta = (BlockStateMeta) meta; - Banner banner = (Banner) bmeta.getBlockState(); + BlockStateMeta bmeta = (BlockStateMeta) meta; + Banner banner = (Banner) bmeta.getBlockState(); - DyeColor bBaseColor = Rnd.get(DyeColor.values()); + DyeColor bBaseColor = Rnd.get(DyeColor.values()); DyeColor bPatternColor = Rnd.get(DyeColor.values()); banner.setBaseColor(bBaseColor); @@ -801,9 +805,9 @@ protected ItemStack build(int itemLvl, int uses, @Nullable ItemType mat) { banner.addPattern(new Pattern(bPatternColor, bPattern)); } catch (IncompatibleClassChangeError ignored) { try { - Class pattern = Reflex.getClass("org.bukkit.block.banner.PatternType"); + Class pattern = Reflex.getClass("org.bukkit.block.banner.PatternType"); Object[] patterns = (Object[]) pattern.getMethod("values").invoke(null); - Object bPattern = Rnd.get(patterns); + Object bPattern = Rnd.get(patterns); banner.addPattern(Pattern.class.getConstructor(DyeColor.class, pattern) .newInstance(bPatternColor, bPattern)); } catch (InvocationTargetException | InstantiationException | NoSuchMethodException | @@ -827,8 +831,8 @@ protected ItemStack build(int itemLvl, int uses, @Nullable ItemType mat) { // Add enchants int enchRoll = Rnd.get(this.getMinEnchantments(), this.getMaxEnchantments()); - int enchCount = 0; - List> enchants = new ArrayList<>(this.enchantsList.entrySet()); + int enchCount = 0; + List> enchants = new ArrayList<>(this.enchantsList.entrySet()); Collections.shuffle(enchants); for ( @@ -836,9 +840,9 @@ protected ItemStack build(int itemLvl, int uses, @Nullable ItemType mat) { if (enchCount >= enchRoll) { break; } - Enchantment enchant = e.getKey(); - int[] enchLevels = this.doMathExpression(itemLvl, e.getValue()); - int enchLevel = Rnd.get(enchLevels[0], enchLevels[1]); + Enchantment enchant = e.getKey(); + int[] enchLevels = this.doMathExpression(itemLvl, e.getValue()); + int enchLevel = Rnd.get(enchLevels[0], enchLevels[1]); if (enchLevel < 1) continue; if (this.isSafeEnchant()) { @@ -873,8 +877,8 @@ protected ItemStack build(int itemLvl, int uses, @Nullable ItemType mat) { reqLevel.add(item, this.getUserLevelRequirement(itemLvl), -1); } - String[] userClass = this.getUserClassRequirement(itemLvl); - DynamicUserRequirement reqClass = null; + String[] userClass = this.getUserClassRequirement(itemLvl); + DynamicUserRequirement reqClass = null; if (userClass == null || userClass.length == 0) { userClass = this.getUserBannedClassRequirement(itemLvl); if (userClass != null && userClass.length > 0) { @@ -999,11 +1003,11 @@ public static Map> getMatchingSuffixes(Material m /* Kind of a method to play around for all combinations since those might be required on third party plugins (Also on Fusion) */ public static List getAllCombinations(GeneratorItem item, Material material) { - String name = item.getName(); - String itemGroupName = ItemUtils.getItemGroupNameFor(new ItemStack(material)); - List names = new ArrayList<>(); - Map> prefixes = getMatchingPrefixes(material, Config.getTiers()); - Map> suffixes = getMatchingSuffixes(material, Config.getTiers()); + String name = item.getName(); + String itemGroupName = ItemUtils.getItemGroupNameFor(new ItemStack(material)); + List names = new ArrayList<>(); + Map> prefixes = getMatchingPrefixes(material, Config.getTiers()); + Map> suffixes = getMatchingSuffixes(material, Config.getTiers()); names.add(name.replace("%item_type%", itemGroupName) .replace("%prefix_tier%", "") @@ -1292,4 +1296,44 @@ private static List interpolatePrefixWithAllSuffixes(String modifiedName return names; } + + public static void updateGeneratorItemLore(ItemStack item) { + if (item.getItemMeta() == null) return; + String itemId = Divinity.getInstance().getModuleCache().getTierManager().getItemId(item); + if (itemId == null) return; + ItemGeneratorManager.GeneratorItem reference = Divinity.getInstance().getModuleCache().getTierManager().getItemById(itemId); + if (reference == null) return; + + ItemMeta meta = item.getItemMeta(); + List lore = meta.getLore(); + if (lore == null) return; + + // Iterate through all enchantments and make sure we delete all existent lore entries + // Note: If there was no %ENCHANTS% before, we add it here since we need it later + for (Enchantment enchantment : Enchantment.values()) { + String value = EngineCfg.LORE_STYLE_ENCHANTMENTS_FORMAT_MAIN + .replace("%name%", Divinity.getInstance().lang().getEnchantment(enchantment)) + .replace("%value%", ""); + lore.removeIf(line -> { + if (!lore.contains("%ENCHANTS%") && line.contains(value)) { + if (lore.contains(line)) + lore.set(lore.indexOf(line), "%ENCHANTS%"); + return false; + } + return line.contains(value); + }); + } + + // If the item had no enchantments before, but the itemflag is no set either, add %ENCHANTS% to the lore with ignored order + if (!lore.contains("%ENCHANTS%") + && !reference.getFlags().contains(ItemFlag.HIDE_ENCHANTS) + && reference.getLore().contains("%ENCHANTS%")) + lore.add("%ENCHANTS%"); + + meta.setLore(lore); + item.setItemMeta(meta); + + // If everythings fine now, the lore should contain %ENCHANTS% which can be replaced by the enchantments now + LoreUT.replaceEnchants(item); + } }