From 464daef7a85623a25328cbab9d831cd00a5abca6 Mon Sep 17 00:00:00 2001 From: Jules Date: Sat, 10 Feb 2024 19:15:08 +0100 Subject: [PATCH] Fixed default item type scripts --- .../java/net/Indyuce/mmoitems/api/Type.java | 2 +- .../api/crafting/CraftingStation.java | 35 ++++---- .../api/interaction/weapon/Weapon.java | 9 +- .../api/item/template/MMOItemTemplate.java | 90 ++++++++++--------- .../api/item/template/TemplateModifier.java | 4 +- .../mmoitems/manager/CraftingManager.java | 2 +- .../mmoitems/manager/TemplateManager.java | 4 +- .../mmoitems/stat/ActionLeftClick.java | 34 ++----- .../src/main/resources/default/item-types.yml | 4 +- 9 files changed, 89 insertions(+), 95 deletions(-) diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java index f555556b..65792f37 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/Type.java @@ -36,7 +36,7 @@ public class Type implements CooldownObject, PreloadedObject { // Blunt public static final Type HAMMER = new Type("HAMMER", true, ModifierSource.MELEE_WEAPON); - public static final Type GAUNTLET = new Type("IRON_SHOVEL", true, ModifierSource.MELEE_WEAPON); + public static final Type GAUNTLET = new Type("GAUNTLET", true, ModifierSource.MELEE_WEAPON); // Range public static final Type WHIP = new Type("WHIP", true, ModifierSource.RANGED_WEAPON); diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/crafting/CraftingStation.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/crafting/CraftingStation.java index 2b341c60..c6a6960c 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/crafting/CraftingStation.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/crafting/CraftingStation.java @@ -1,6 +1,7 @@ package net.Indyuce.mmoitems.api.crafting; -import io.lumine.mythic.lib.api.util.PostLoadObject; +import io.lumine.mythic.lib.util.PostLoadAction; +import io.lumine.mythic.lib.util.PreloadedObject; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.api.crafting.ingredient.inventory.IngredientInventory; import net.Indyuce.mmoitems.api.crafting.recipe.CheckedRecipe; @@ -13,12 +14,13 @@ import org.apache.commons.lang.Validate; import org.bukkit.Sound; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.logging.Level; -public class CraftingStation extends PostLoadObject { +public class CraftingStation implements PreloadedObject { private final String id; private final String name; private final Layout layout; @@ -38,8 +40,17 @@ public class CraftingStation extends PostLoadObject { private CraftingStation parent; + private final PostLoadAction postLoadAction = new PostLoadAction(config -> { + if (!config.contains("parent")) return; + + String id = config.getString("parent").toLowerCase().replace(" ", "-").replace("_", "-"); + Validate.isTrue(!id.equals(CraftingStation.this.id), "Station cannot use itself as parent"); + Validate.isTrue(MMOItems.plugin.getCrafting().hasStation(id), "Could not find parent station with ID '" + id + "'"); + parent = MMOItems.plugin.getCrafting().getStation(id); + }); + public CraftingStation(String id, FileConfiguration config) { - super(config); + postLoadAction.cacheConfig(config); this.id = id.toLowerCase().replace("_", "-").replace(" ", "-"); this.name = config.getString("name", "Unnamed"); @@ -59,8 +70,6 @@ public class CraftingStation extends PostLoadObject { } public CraftingStation(String id, String name, Layout layout, Sound sound, StationItemOptions itemOptions, int maxQueueSize, CraftingStation parent) { - super(null); - Validate.notNull(id, "Crafting station ID must not be null"); Validate.notNull(name, "Crafting station name must not be null"); Validate.notNull(sound, "Crafting station sound must not be null"); @@ -74,6 +83,12 @@ public class CraftingStation extends PostLoadObject { this.parent = parent; } + @NotNull + @Override + public PostLoadAction getPostLoadAction() { + return postLoadAction; + } + public String getId() { return id; } @@ -169,16 +184,6 @@ public class CraftingStation extends PostLoadObject { return Math.max(1, (int) Math.ceil((double) recipes / getLayout().getRecipeSlots().size())); } - @Override - protected void whenPostLoaded(ConfigurationSection config) { - if (config.contains("parent")) { - String id = config.getString("parent").toLowerCase().replace(" ", "-").replace("_", "-"); - Validate.isTrue(!id.equals(this.id), "Station cannot use itself as parent"); - Validate.isTrue(MMOItems.plugin.getCrafting().hasStation(id), "Could not find parent station with ID '" + id + "'"); - parent = MMOItems.plugin.getCrafting().getStation(id); - } - } - /* * find type of crafting recipe based on section. there is no 'type' recipe * parameter because old files would be out of date, instead just looks for diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/weapon/Weapon.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/weapon/Weapon.java index 9bf6d69a..e92b0aa8 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/weapon/Weapon.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/weapon/Weapon.java @@ -7,7 +7,6 @@ import io.lumine.mythic.lib.comp.flags.CustomFlag; import io.lumine.mythic.lib.damage.AttackMetadata; import io.lumine.mythic.lib.player.PlayerMetadata; import io.lumine.mythic.lib.skill.SimpleSkill; -import io.lumine.mythic.lib.skill.Skill; import io.lumine.mythic.lib.skill.SkillMetadata; import io.lumine.mythic.lib.skill.handler.SkillHandler; import io.lumine.mythic.lib.skill.result.SkillResult; @@ -23,6 +22,7 @@ import net.Indyuce.mmoitems.api.interaction.util.UntargetedDurabilityItem; import net.Indyuce.mmoitems.api.interaction.weapon.untargeted.LegacyWeapon; import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.api.util.message.Message; +import net.Indyuce.mmoitems.stat.ActionLeftClick; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -134,7 +134,12 @@ public class Weapon extends UseItem { @Nullable private SkillHandler findClickSkill(boolean rightClick) { - final String skillId = getNBTItem().getString((rightClick ? ItemStats.RIGHT_CLICK_SCRIPT : ItemStats.LEFT_CLICK_SCRIPT).getNBTPath()); + String skillId = getNBTItem().getString((rightClick ? ItemStats.RIGHT_CLICK_SCRIPT : ItemStats.LEFT_CLICK_SCRIPT).getNBTPath()); + + // (Deprecated) Support for staff spirits on left-clicks + if (!rightClick && (skillId == null || skillId.isEmpty())) + skillId = getNBTItem().getString(ActionLeftClick.LEGACY_PATH); + if (skillId == null || skillId.isEmpty()) { // Find item type action diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/template/MMOItemTemplate.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/template/MMOItemTemplate.java index 644f8183..b309b4a8 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/template/MMOItemTemplate.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/template/MMOItemTemplate.java @@ -1,8 +1,10 @@ package net.Indyuce.mmoitems.api.item.template; -import io.lumine.mythic.lib.api.util.PostLoadObject; +import io.lumine.mythic.lib.UtilityMethods; import io.lumine.mythic.lib.api.util.ui.FriendlyFeedbackCategory; import io.lumine.mythic.lib.api.util.ui.FriendlyFeedbackProvider; +import io.lumine.mythic.lib.util.PostLoadAction; +import io.lumine.mythic.lib.util.PreloadedObject; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.api.ItemTier; import net.Indyuce.mmoitems.api.Type; @@ -25,7 +27,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -public class MMOItemTemplate extends PostLoadObject implements ItemReference { +public class MMOItemTemplate implements ItemReference, PreloadedObject { private final Type type; private final String id; private final int revId; @@ -42,40 +44,7 @@ public class MMOItemTemplate extends PostLoadObject implements ItemReference { private ModifierGroup modifierGroup; private final Set options = new HashSet<>(); - /** - * Public constructor which can be used to register extra item templates - * using other addons or plugins - * - * @param type The item type of your template - * @param id The template identifier, it's ok if two templates with - * different item types share the same ID - */ - public MMOItemTemplate(Type type, String id) { - super(null); - - this.type = type; - this.id = id; - this.revId = 1; - } - - /** - * Used to load MMOItem templates from config files - * - * @param type The item type of your template - * @param config The config file read to load the template - */ - public MMOItemTemplate(Type type, ConfigurationSection config) { - super(config); - Validate.notNull(config, "Could not load template config"); - - this.type = type; - this.id = config.getName().toUpperCase().replace("-", "_").replace(" ", "_"); - this.revId = config.getInt("base.revision-id", 1); - } - - @Override - protected void whenPostLoaded(ConfigurationSection config) { - + private final PostLoadAction postLoadAction = new PostLoadAction(config -> { FriendlyFeedbackProvider ffp = new FriendlyFeedbackProvider(FFPMMOItems.get()); ffp.activatePrefix(true, getType().getId() + " " + getId()); @@ -95,7 +64,7 @@ public class MMOItemTemplate extends PostLoadObject implements ItemReference { // Read modifiers try { - modifierGroup = config.contains("modifiers") ? new ModifierGroup(id, config) : null; + modifierGroup = config.contains("modifiers") ? new ModifierGroup(getId(), config) : null; if (modifierGroup != null) modifierGroup.getPostLoadAction().performAction(); } catch (Exception exception) { ffp.log(FriendlyFeedbackCategory.ERROR, "Could not load modifier group: {0}", exception.getMessage()); @@ -105,10 +74,9 @@ public class MMOItemTemplate extends PostLoadObject implements ItemReference { Validate.notNull(config.getConfigurationSection("base"), FriendlyFeedbackProvider.quickForConsole(FFPMMOItems.get(), "Could not find base item data")); for (String key : config.getConfigurationSection("base").getKeys(false)) try { - String id = key.toUpperCase().replace("-", "_"); - Validate.isTrue(MMOItems.plugin.getStats().has(id), FriendlyFeedbackProvider.quickForConsole(FFPMMOItems.get(), "Could not find stat with ID '$i{0}$b'", id)); - - ItemStat stat = MMOItems.plugin.getStats().get(id); + final String id = UtilityMethods.enumName(key); + final ItemStat stat = MMOItems.plugin.getStats().get(id); + Validate.notNull(stat, FriendlyFeedbackProvider.quickForConsole(FFPMMOItems.get(), "Could not find stat with ID '$i{0}$b'", id)); RandomStatData data = stat.whenInitialized(config.get("base." + key)); if (data != null) base.put(stat, data); @@ -121,6 +89,42 @@ public class MMOItemTemplate extends PostLoadObject implements ItemReference { // Print all failures ffp.sendTo(FriendlyFeedbackCategory.INFORMATION, MMOItems.getConsole()); + }); + + /** + * Public constructor which can be used to register extra item templates + * using other addons or plugins + * + * @param type The item type of your template + * @param id The template identifier, it's ok if two templates with + * different item types share the same ID + */ + public MMOItemTemplate(Type type, String id) { + this.type = type; + this.id = id; + this.revId = 1; + } + + /** + * Used to load MMOItem templates from config files + * + * @param type The item type of your template + * @param config The config file read to load the template + */ + public MMOItemTemplate(Type type, ConfigurationSection config) { + Validate.notNull(config, "Could not load template config"); + + postLoadAction.cacheConfig(config); + + this.type = type; + this.id = config.getName().toUpperCase().replace("-", "_").replace(" ", "_"); + this.revId = config.getInt("base.revision-id", 1); + } + + @NotNull + @Override + public PostLoadAction getPostLoadAction() { + return postLoadAction; } @NotNull @@ -281,8 +285,8 @@ public class MMOItemTemplate extends PostLoadObject implements ItemReference { /** * @return Attempts to get the crafted amount registered in the Stat. - *

- * Default is 1 obviously. + *

+ * Default is 1 obviously. * @deprecated Don't use this method, the Crafted Amount Stat will be deleted in the near future. */ @Deprecated diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/template/TemplateModifier.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/template/TemplateModifier.java index 385a8653..aabc20b7 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/template/TemplateModifier.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/item/template/TemplateModifier.java @@ -1,7 +1,7 @@ package net.Indyuce.mmoitems.api.item.template; +import io.lumine.mythic.lib.UtilityMethods; import io.lumine.mythic.lib.util.PostLoadAction; -import io.lumine.mythic.lib.util.PreloadedObject; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder; import net.Indyuce.mmoitems.stat.data.random.RandomStatData; @@ -22,7 +22,7 @@ public class TemplateModifier extends ModifierNode { Validate.notNull(config.getConfigurationSection("stats"), "Could not find base item data"); for (String key : config.getConfigurationSection("stats").getKeys(false)) try { - final String statId = key.toUpperCase().replace("-", "_"); + final String statId = UtilityMethods.enumName(key); final ItemStat stat = MMOItems.plugin.getStats().get(statId); Validate.notNull(stat, "Could not find stat with ID '" + statId + "'"); TemplateModifier.this.data.put(stat, stat.whenInitialized(config.get("stats." + key))); diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/CraftingManager.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/CraftingManager.java index 90440f19..9924836b 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/CraftingManager.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/CraftingManager.java @@ -109,7 +109,7 @@ public class CraftingManager implements Reloadable { for (CraftingStation station : stations.values()) try { - station.postLoad(); + station.getPostLoadAction().performAction(); } catch (IllegalArgumentException exception) { MMOItems.plugin.getLogger().log(Level.WARNING, "Could not post-load station '" + station.getId() + "': " + exception.getMessage()); diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/TemplateManager.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/TemplateManager.java index dd0e6ca9..9f671173 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/TemplateManager.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/TemplateManager.java @@ -172,7 +172,7 @@ public class TemplateManager implements Reloadable { try { MMOItemTemplate template = new MMOItemTemplate(type, type.getConfigFile().getConfig().getConfigurationSection(id)); - template.postLoad(); + template.getPostLoadAction().performAction(); registerTemplate(template); return template; @@ -327,7 +327,7 @@ public class TemplateManager implements Reloadable { ffp.log(FriendlyFeedbackCategory.INFORMATION, "Loading item templates, please wait..."); templates.forEach(template -> { try { - template.postLoad(); + template.getPostLoadAction().performAction(); } catch (IllegalArgumentException exception) { ffp.activatePrefix(true, "Item Templates \u00a78($r" + template.getType().getId() + "\u00a78)"); ffp.log(FriendlyFeedbackCategory.INFORMATION, "Could not post-load item template '" + template.getId() + "': " + exception.getMessage()); diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/ActionLeftClick.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/ActionLeftClick.java index 922d12cb..fa4d511f 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/ActionLeftClick.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/ActionLeftClick.java @@ -12,7 +12,6 @@ import net.Indyuce.mmoitems.stat.data.StringData; import net.Indyuce.mmoitems.stat.type.StringStat; import org.bukkit.Material; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.ArrayList; @@ -21,7 +20,7 @@ public class ActionLeftClick extends StringStat { super("ON_LEFT_CLICK", Material.COMMAND_BLOCK_MINECART, "Left Click Action", new String[]{"ID of skill ran when left clicking. When used,", "The item will naturally apply item costs like", "mana, stamina, cooldown. This option overrides the", "script provided by the item type."}, new String[]{"weapon"}); // Staff spirit set as alias - setAliases(LEGACY_PATH); + setAliases(LEGACY_ID); } @Override @@ -39,44 +38,25 @@ public class ActionLeftClick extends StringStat { // No lore insertion } - private static final String LEGACY_PATH = "STAFF_SPIRIT"; + public static final String LEGACY_ID = "STAFF_SPIRIT"; + public static final String LEGACY_PATH = "MMOITEMS_" + LEGACY_ID; // TODO refactor with stat categories @Override @Deprecated public void whenLoaded(@NotNull ReadMMOItem mmoitem) { ArrayList relevantTags = new ArrayList<>(); + // New path if (mmoitem.getNBT().hasTag(getNBTPath())) relevantTags.add(ItemTag.getTagAtPath(getNBTPath(), mmoitem.getNBT(), SupportedNBTTagValues.STRING)); + // Legacy path if (mmoitem.getNBT().hasTag(LEGACY_PATH)) - relevantTags.add(ItemTag.getTagAtPath(LEGACY_PATH, mmoitem.getNBT(), SupportedNBTTagValues.STRING)); + relevantTags.add(new ItemTag(getNBTPath(), ItemTag.getTagAtPath(LEGACY_PATH, mmoitem.getNBT(), SupportedNBTTagValues.STRING).getValue())); + StringData bakedData = getLoadedNBT(relevantTags); if (bakedData != null) mmoitem.setData(this, bakedData); } - - // TODO refactor with stat categories - @Nullable - @Override - @Deprecated - public StringData getLoadedNBT(@NotNull ArrayList storedTags) { - - // New path - ItemTag tg = ItemTag.getTagAtPath(getNBTPath(), storedTags); - if (tg != null) { - String value = (String) tg.getValue(); - return new StringData(value); - } - - // Legacy path - tg = ItemTag.getTagAtPath(LEGACY_PATH, storedTags); - if (tg != null) { - String value = (String) tg.getValue(); - return new StringData(value); - } - - return null; - } } diff --git a/MMOItems-Dist/src/main/resources/default/item-types.yml b/MMOItems-Dist/src/main/resources/default/item-types.yml index c2887291..09d623cf 100644 --- a/MMOItems-Dist/src/main/resources/default/item-types.yml +++ b/MMOItems-Dist/src/main/resources/default/item-types.yml @@ -80,7 +80,7 @@ HAMMER: on-attack: blunt_attack_effect GAUNTLET: - display: IRON_HORSE_ARMOR + display: IRON_SHOVEL name: 'Gauntlet' unident-item: name: '&f#prefix#Unidentified Gauntlet' @@ -109,7 +109,7 @@ WHIP: - '{tier}&8- &7Item Tier: #prefix##tier#' disable-melee-attacks: true - on-left-click: whip_attack_effect + on-left-click: whip_attack on-attack: slashing_attack_effect STAFF: