From 239e3c9eff9d3060ec5b7ae38399d545e2308742 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Sun, 20 Sep 2020 12:51:15 +0200 Subject: [PATCH] !Templates are now loaded when MI loads for extra compatibility --- .../java/net/Indyuce/mmoitems/MMOItems.java | 95 ++++++++++++------ .../net/Indyuce/mmoitems/api/UpdaterData.java | 30 +++--- .../mmoitems/api/edition/StatEdition.java | 2 +- .../api/item/template/MMOItemTemplate.java | 10 +- .../mmoitems/manager/TemplateManager.java | 67 ++++++++++--- .../mmoitems/manager/UpdaterManager.java | 99 +++++++++---------- 6 files changed, 187 insertions(+), 116 deletions(-) diff --git a/src/main/java/net/Indyuce/mmoitems/MMOItems.java b/src/main/java/net/Indyuce/mmoitems/MMOItems.java index 44a53e77..cab796c5 100644 --- a/src/main/java/net/Indyuce/mmoitems/MMOItems.java +++ b/src/main/java/net/Indyuce/mmoitems/MMOItems.java @@ -1,5 +1,21 @@ package net.Indyuce.mmoitems; +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +import javax.annotation.Nullable; + +import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.java.JavaPlugin; + import net.Indyuce.mmoitems.api.ConfigFile; import net.Indyuce.mmoitems.api.ItemTier; import net.Indyuce.mmoitems.api.SoulboundInfo; @@ -9,13 +25,21 @@ import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.command.MMOItemsCommandTreeRoot; import net.Indyuce.mmoitems.command.UpdateItemCommand; import net.Indyuce.mmoitems.command.completion.UpdateItemCompletion; -import net.Indyuce.mmoitems.comp.*; +import net.Indyuce.mmoitems.comp.AdvancedEnchantmentsHook; +import net.Indyuce.mmoitems.comp.MMOItemsMetrics; +import net.Indyuce.mmoitems.comp.MMOItemsRewardTypes; +import net.Indyuce.mmoitems.comp.RealDualWieldHook; +import net.Indyuce.mmoitems.comp.WorldEditSupport; import net.Indyuce.mmoitems.comp.eco.VaultSupport; import net.Indyuce.mmoitems.comp.flags.DefaultFlags; import net.Indyuce.mmoitems.comp.flags.FlagPlugin; import net.Indyuce.mmoitems.comp.flags.ResidenceFlags; import net.Indyuce.mmoitems.comp.flags.WorldGuardFlags; -import net.Indyuce.mmoitems.comp.holograms.*; +import net.Indyuce.mmoitems.comp.holograms.CMIPlugin; +import net.Indyuce.mmoitems.comp.holograms.HologramSupport; +import net.Indyuce.mmoitems.comp.holograms.HologramsPlugin; +import net.Indyuce.mmoitems.comp.holograms.HolographicDisplaysPlugin; +import net.Indyuce.mmoitems.comp.holograms.TrHologramPlugin; import net.Indyuce.mmoitems.comp.inventory.DefaultPlayerInventory; import net.Indyuce.mmoitems.comp.inventory.OrnamentPlayerInventory; import net.Indyuce.mmoitems.comp.inventory.PlayerInventory; @@ -34,24 +58,34 @@ import net.Indyuce.mmoitems.comp.rpg.DefaultHook; import net.Indyuce.mmoitems.comp.rpg.RPGHandler; import net.Indyuce.mmoitems.gui.PluginInventory; import net.Indyuce.mmoitems.gui.listener.GuiListener; -import net.Indyuce.mmoitems.listener.*; -import net.Indyuce.mmoitems.manager.*; +import net.Indyuce.mmoitems.listener.CraftingListener; +import net.Indyuce.mmoitems.listener.CustomBlockListener; +import net.Indyuce.mmoitems.listener.CustomSoundListener; +import net.Indyuce.mmoitems.listener.DisableInteractions; +import net.Indyuce.mmoitems.listener.DurabilityListener; +import net.Indyuce.mmoitems.listener.ElementListener; +import net.Indyuce.mmoitems.listener.ItemUse; +import net.Indyuce.mmoitems.listener.PlayerListener; +import net.Indyuce.mmoitems.manager.AbilityManager; +import net.Indyuce.mmoitems.manager.BlockManager; +import net.Indyuce.mmoitems.manager.ConfigManager; +import net.Indyuce.mmoitems.manager.CraftingManager; +import net.Indyuce.mmoitems.manager.DropTableManager; +import net.Indyuce.mmoitems.manager.EntityManager; +import net.Indyuce.mmoitems.manager.ItemManager; +import net.Indyuce.mmoitems.manager.LayoutManager; +import net.Indyuce.mmoitems.manager.PluginUpdateManager; +import net.Indyuce.mmoitems.manager.RecipeManager; +import net.Indyuce.mmoitems.manager.SetManager; +import net.Indyuce.mmoitems.manager.StatManager; +import net.Indyuce.mmoitems.manager.TemplateManager; +import net.Indyuce.mmoitems.manager.TierManager; +import net.Indyuce.mmoitems.manager.TypeManager; +import net.Indyuce.mmoitems.manager.UpdaterManager; +import net.Indyuce.mmoitems.manager.UpgradeManager; +import net.Indyuce.mmoitems.manager.WorldGenManager; import net.mmogroup.mmolib.api.player.MMOPlayerData; import net.mmogroup.mmolib.version.SpigotPlugin; -import org.apache.commons.lang.Validate; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.entity.Player; -import org.bukkit.event.HandlerList; -import org.bukkit.event.Listener; -import org.bukkit.inventory.ItemStack; -import org.bukkit.plugin.java.JavaPlugin; - -import javax.annotation.Nullable; -import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; public class MMOItems extends JavaPlugin { public static MMOItems plugin; @@ -102,17 +136,17 @@ public class MMOItems extends JavaPlugin { getLogger().log(Level.WARNING, "Could not initialize support with WorldEdit 7: " + exception.getMessage()); } - /* - * stat manager must be initialized before MMOCore compatibility - * initializes so that MMOCore can register its stats - */ - saveDefaultConfig(); + + /* + * Stat manager must be initialized before MMOCore compatibility + * initializes so that MMOCore and other plugins can register their + * stats. After types are loaded, templates are loaded so that other + * plugins when enabling can load MI template references + */ statManager = new StatManager(); - typeManager.reload(); - - templateManager.loadCompatibility(); // explained why in method + templateManager.preloadTemplates(); if (Bukkit.getPluginManager().getPlugin("MMOCore") != null) new MMOCoreMMOLoader(); @@ -137,14 +171,17 @@ public class MMOItems extends JavaPlugin { getLogger().log(Level.INFO, "Hooked onto MMOInventory"); } - findRpgPlugin(); - templateManager.reload(); - + /* + * After tiers, sets and upgrade templates are loaded, MI template data + * CAN be fully loaded + */ tierManager = new TierManager(); setManager = new SetManager(); upgradeManager = new UpgradeManager(); + templateManager.postloadTemplates(); + dropTableManager = new DropTableManager(); dynamicUpdater = new UpdaterManager(); worldGenManager = new WorldGenManager(); diff --git a/src/main/java/net/Indyuce/mmoitems/api/UpdaterData.java b/src/main/java/net/Indyuce/mmoitems/api/UpdaterData.java index 9552786b..d1beced3 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/UpdaterData.java +++ b/src/main/java/net/Indyuce/mmoitems/api/UpdaterData.java @@ -1,31 +1,31 @@ package net.Indyuce.mmoitems.api; -import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate; -import net.Indyuce.mmoitems.manager.UpdaterManager.KeepOption; -import net.mmogroup.mmolib.api.item.NBTItem; -import org.apache.commons.lang.Validate; -import org.bukkit.configuration.ConfigurationSection; - import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.UUID; +import org.apache.commons.lang.Validate; +import org.bukkit.configuration.ConfigurationSection; + +import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate; +import net.Indyuce.mmoitems.manager.UpdaterManager.KeepOption; +import net.mmogroup.mmolib.api.item.NBTItem; + public class UpdaterData { - // TODO change this to MMOItemTemplate - - /* - * two UUIDs can be found : one on the itemStack in the nbttags, and one in - * the UpdaterData instance. if the two match, the item is up to date. if - * they don't match, the item needs to be updated. UUID not final because it - * must be changed + /** + * The item reference */ - private final Type type; - private final String id; + /** + * Two UUIDs can be found: one on the itemStack in the NBTTags, and one in + * the UpdaterData instance. If the UUIDs match, the item is up to date. If + * they don't match, the item needs to be updated. UUID not final because it + * is updated everytime the item is edited using the editor GUI + */ private UUID uuid; private final Set options = new HashSet<>(); diff --git a/src/main/java/net/Indyuce/mmoitems/api/edition/StatEdition.java b/src/main/java/net/Indyuce/mmoitems/api/edition/StatEdition.java index 36440d0a..7985adc9 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/edition/StatEdition.java +++ b/src/main/java/net/Indyuce/mmoitems/api/edition/StatEdition.java @@ -47,7 +47,7 @@ public class StatEdition implements Edition { inv.getPlayer().sendMessage(ChatColor.YELLOW + "" + ChatColor.STRIKETHROUGH + "-----------------------------------------------------"); for (String line : message) - inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + line); + inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + ChatColor.translateAlternateColorCodes('&', line)); inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Type 'cancel' to abort editing."); /* diff --git a/src/main/java/net/Indyuce/mmoitems/api/item/template/MMOItemTemplate.java b/src/main/java/net/Indyuce/mmoitems/api/item/template/MMOItemTemplate.java index 10942f0c..45133607 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/item/template/MMOItemTemplate.java +++ b/src/main/java/net/Indyuce/mmoitems/api/item/template/MMOItemTemplate.java @@ -8,6 +8,8 @@ import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder; import net.Indyuce.mmoitems.api.player.RPGPlayer; import net.Indyuce.mmoitems.stat.data.random.RandomStatData; import net.Indyuce.mmoitems.stat.type.ItemStat; +import net.mmogroup.mmolib.api.util.PostLoadObject; + import org.apache.commons.lang.Validate; import org.bukkit.configuration.ConfigurationSection; @@ -15,7 +17,7 @@ import javax.annotation.Nullable; import java.util.*; import java.util.logging.Level; -public class MMOItemTemplate implements ItemReference { +public class MMOItemTemplate extends PostLoadObject implements ItemReference { private final Type type; private final String id; @@ -36,6 +38,8 @@ public class MMOItemTemplate implements ItemReference { * different item types share the same ID */ public MMOItemTemplate(Type type, String id) { + super(null); + this.type = type; this.id = id; } @@ -49,11 +53,15 @@ public class MMOItemTemplate implements ItemReference { * 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(" ", "_"); + } + @Override + protected void whenPostLoaded(ConfigurationSection config) { if (config.contains("option")) for (TemplateOption option : TemplateOption.values()) if (config.getBoolean("option." + option.name().toLowerCase().replace("_", "-"))) diff --git a/src/main/java/net/Indyuce/mmoitems/manager/TemplateManager.java b/src/main/java/net/Indyuce/mmoitems/manager/TemplateManager.java index 960afef1..62316f82 100644 --- a/src/main/java/net/Indyuce/mmoitems/manager/TemplateManager.java +++ b/src/main/java/net/Indyuce/mmoitems/manager/TemplateManager.java @@ -184,6 +184,57 @@ public class TemplateManager { return (int) found; } + /** + * Templates must be loaded whenever MMOItems enables so that other plugins + * like MMOCore can load template references in drop items or other objects. + * Template data is only loaded when MMOItems enables, once sets, tiers.. + * are initialized + */ + public void preloadTemplates() { + for (Type type : MMOItems.plugin.getTypes().getAll()) { + FileConfiguration config = type.getConfigFile().getConfig(); + for (String key : config.getKeys(false)) + try { + registerTemplate(new MMOItemTemplate(type, config.getConfigurationSection(key))); + } catch (IllegalArgumentException exception) { + MMOItems.plugin.getLogger().log(Level.INFO, "Could not preload item template '" + key + "': " + exception.getMessage()); + } + } + } + + /** + * Loads item generator modifiers and post load item templates. + */ + public void postloadTemplates() { + + MMOItems.plugin.getLogger().log(Level.INFO, "Loading template modifiers, please wait.."); + for (File file : new File(MMOItems.plugin.getDataFolder() + "/modifiers").listFiles()) { + FileConfiguration config = YamlConfiguration.loadConfiguration(file); + for (String key : config.getKeys(false)) + try { + TemplateModifier modifier = new TemplateModifier(config.getConfigurationSection(key)); + modifiers.put(modifier.getId(), modifier); + } catch (IllegalArgumentException exception) { + MMOItems.plugin.getLogger().log(Level.INFO, "Could not load template modifier '" + key + "': " + exception.getMessage()); + } + } + + MMOItems.plugin.getLogger().log(Level.INFO, "Loading item templates, please wait.."); + templates.forEach(template -> { + try { + template.postLoad(); + } catch (IllegalArgumentException exception) { + MMOItems.plugin.getLogger().log(Level.INFO, "Could not load item template '" + template.getId() + "': " + exception.getMessage()); + } + }); + } + + /** + * Reloads the item templates. This is the method used to reload the manager + * when the server is already running. It clears all the maps and loads + * everything again. Template references in other plugins like MMOCore must + * be refreshed afterwards. + */ public void reload() { templates.clear(); modifiers.clear(); @@ -211,21 +262,5 @@ public class TemplateManager { } } } - // this loads dummy items for on load so - // plugins that enable before mmoitems that use - // items (mmocore) don't error out and need - // a reload - public void loadCompatibility() { - templates.clear(); - for (Type type : MMOItems.plugin.getTypes().getAll()) { - FileConfiguration config = type.getConfigFile().getConfig(); - for (String key : config.getKeys(false)) - try { - registerTemplate(new MMOItemTemplate(type, key)); - } catch (IllegalArgumentException ignored) { - - } - } - } } diff --git a/src/main/java/net/Indyuce/mmoitems/manager/UpdaterManager.java b/src/main/java/net/Indyuce/mmoitems/manager/UpdaterManager.java index 6cee4716..26234dce 100644 --- a/src/main/java/net/Indyuce/mmoitems/manager/UpdaterManager.java +++ b/src/main/java/net/Indyuce/mmoitems/manager/UpdaterManager.java @@ -43,7 +43,7 @@ public class UpdaterManager implements Listener { MMOItemTemplate template = MMOItems.plugin.getTemplates().getTemplateOrThrow(type, id); enable(new UpdaterData(template, config.getConfigurationSection(typeFormat + "." + id))); } - } catch (IllegalArgumentException | NullPointerException exception) { + } catch (IllegalArgumentException | NullPointerException exception) { MMOItems.plugin.getLogger().log(Level.WARNING, "An issue occurred while trying to load dynamic updater data: " + exception.getMessage()); } @@ -62,8 +62,8 @@ public class UpdaterManager implements Listener { } public void enable(ItemReference template) { - this.data.setValue(template.getType(), template.getId(), new UpdaterData(MMOItems.plugin.getTemplates() - .getTemplate(template.getType(), template.getId()), UUID.randomUUID())); + this.data.setValue(template.getType(), template.getId(), + new UpdaterData(MMOItems.plugin.getTemplates().getTemplate(template.getType(), template.getId()), UUID.randomUUID())); } public void enable(Type type, String id) { @@ -124,22 +124,18 @@ public class UpdaterManager implements Listener { public ItemStack getUpdated(NBTItem item, Player target) { /* - * If the item type is null, then it is not an mmoitem and it does not - * need to be updated - */ + * If the item type is null, then it is not an mmoitem and it does not + * need to be updated + */ Type type = item.getType(); - if (type == null) + if (type == null || !data.hasValue(type, item.getString("MMOITEMS_ITEM_ID"))) return item.getItem(); - if (!data.hasValue(type, item.getString("MMOITEMS_ITEM_ID"))) - return item.getItem(); - - /* - * check the internal UUID of the item, if it does not make the one - * stored in the item updater data then the item is outdated. - */ + * check the internal UUID of the item, if it does not make the one + * stored in the item updater data then the item is outdated. + */ UpdaterData did = data.getValue(type, item.getString("MMOITEMS_ITEM_ID")); if (did.matches(item)) return item.getItem(); @@ -147,69 +143,64 @@ public class UpdaterManager implements Listener { MMOItemTemplate template = MMOItems.plugin.getTemplates().getTemplate(type, item.getString("MMOITEMS_ITEM_ID")); MMOItem newMMOItem = template.newBuilder(PlayerData.get(target).getRPG()).build(); - /* + /* * apply older gem stones, using a light MMOItem so the item does not * calculate every stat data from the older item. */ - MMOItem volatileItem = new VolatileMMOItem(item); - if (did.hasOption(KeepOption.KEEP_GEMS) && volatileItem.hasData(ItemStat.GEM_SOCKETS)) - newMMOItem.replaceData(ItemStat.GEM_SOCKETS, volatileItem.getData(ItemStat.GEM_SOCKETS)); + MMOItem volatileItem = new VolatileMMOItem(item); + if (did.hasOption(KeepOption.KEEP_GEMS) && volatileItem.hasData(ItemStat.GEM_SOCKETS)) + newMMOItem.replaceData(ItemStat.GEM_SOCKETS, volatileItem.getData(ItemStat.GEM_SOCKETS)); - if (did.hasOption(KeepOption.KEEP_SOULBOUND) && volatileItem.hasData(ItemStat.SOULBOUND)) - newMMOItem.replaceData(ItemStat.SOULBOUND, volatileItem.getData(ItemStat.SOULBOUND)); + if (did.hasOption(KeepOption.KEEP_SOULBOUND) && volatileItem.hasData(ItemStat.SOULBOUND)) + newMMOItem.replaceData(ItemStat.SOULBOUND, volatileItem.getData(ItemStat.SOULBOUND)); - // if (did.hasOption(KeepOption.KEEP_SKIN) && itemMMO.hasData(stat)) + // if (did.hasOption(KeepOption.KEEP_SKIN) && itemMMO.hasData(stat)) - // apply amount - ItemStack newItem = newMMOItem.newBuilder().build(); - newItem.setAmount(item.getItem().getAmount()); + // apply amount + ItemStack newItem = newMMOItem.newBuilder().build(); + newItem.setAmount(item.getItem().getAmount()); - ItemMeta newItemMeta = newItem.getItemMeta(); - List lore = newItemMeta.getLore(); + ItemMeta newItemMeta = newItem.getItemMeta(); + List lore = newItemMeta.getLore(); - /* + /* * add old enchants to the item. warning - if enabled the item will - * remember of ANY enchant on the old item, even the enchants that - were + * remember of ANY enchant on the old item, even the enchants that were * removed! */ - if (did.hasOption(KeepOption.KEEP_ENCHANTS)) - item.getItem().getItemMeta().getEnchants().forEach((enchant, level) -> newItemMeta.addEnchant(enchant, level, true)); + if (did.hasOption(KeepOption.KEEP_ENCHANTS)) + item.getItem().getItemMeta().getEnchants().forEach((enchant, level) -> newItemMeta.addEnchant(enchant, level, true)); - /* + /* * keepLore is used to save enchants from custom enchants plugins that * only use lore to save enchant data */ - if (did.hasOption(KeepOption.KEEP_LORE)) { - int n = 0; - for (String s : item.getItem().getItemMeta().getLore()) { - if (!s.startsWith(ChatColor.GRAY + "")) - break; - lore.add(n++, s); - } - } + if (did.hasOption(KeepOption.KEEP_LORE)) { + int n = 0; + for (String s : item.getItem().getItemMeta().getLore()) { + if (!s.startsWith(ChatColor.GRAY + "")) + break; + lore.add(n++, s); + } + } - /* + /* * keep durability can be used for tools to save their durability so * users do not get extra durability when the item is updated */ - ; - if (did.hasOption(KeepOption.KEEP_DURABILITY) && item.getItem().getItemMeta() instanceof Damageable && newItemMeta instanceof Damageable) { - ((Damageable) newItemMeta).setDamage(((Damageable) item.getItem().getItemMeta()).getDamage()); - } + if (did.hasOption(KeepOption.KEEP_DURABILITY) && item.getItem().getItemMeta() instanceof Damageable && newItemMeta instanceof Damageable) + ((Damageable) newItemMeta).setDamage(((Damageable) item.getItem().getItemMeta()).getDamage()); - - /* - * keep name so players who renamed the item in the anvil does not - have + /* + * keep name so players who renamed the item in the anvil does not have * to rename it again */ - if (did.hasOption(KeepOption.KEEP_NAME) && item.getItem().getItemMeta().hasDisplayName()) - newItemMeta.setDisplayName(item.getItem().getItemMeta().getDisplayName()); + if (did.hasOption(KeepOption.KEEP_NAME) && item.getItem().getItemMeta().hasDisplayName()) + newItemMeta.setDisplayName(item.getItem().getItemMeta().getDisplayName()); - newItemMeta.setLore(lore); - newItem.setItemMeta(newItemMeta); - return newItem; + newItemMeta.setLore(lore); + newItem.setItemMeta(newItemMeta); + return newItem; } public enum KeepOption {