From da3bc90f6c0c616505d11486ba89751d07e5f2c3 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Sat, 26 Sep 2020 19:33:53 +0200 Subject: [PATCH] !Fixed template preloading and editing --- .../java/net/Indyuce/mmoitems/MMOItems.java | 19 ++--- .../api/item/template/MMOItemTemplate.java | 9 ++- .../mmoitems/manager/TemplateManager.java | 78 +++++++++++++------ 3 files changed, 73 insertions(+), 33 deletions(-) diff --git a/src/main/java/net/Indyuce/mmoitems/MMOItems.java b/src/main/java/net/Indyuce/mmoitems/MMOItems.java index 01de29a8..d15562d5 100644 --- a/src/main/java/net/Indyuce/mmoitems/MMOItems.java +++ b/src/main/java/net/Indyuce/mmoitems/MMOItems.java @@ -89,9 +89,10 @@ import net.mmogroup.mmolib.api.player.MMOPlayerData; import net.mmogroup.mmolib.version.SpigotPlugin; public class MMOItems extends JavaPlugin { - /* Introducing: The commit comment! - * Just change this a tiny bit each time you - * need to push a new build. It's convenient! + + /* + * Introducing: The commit comment! Just change this a tiny bit each time + * you need to push a new build. It's convenient! */ public static MMOItems plugin; @@ -149,7 +150,7 @@ public class MMOItems extends JavaPlugin { */ statManager = new StatManager(); typeManager.reload(); - templateManager.loadCompatibility(); + templateManager.preloadTemplates(); if (Bukkit.getPluginManager().getPlugin("MMOCore") != null) new MMOCoreMMOLoader(); @@ -177,14 +178,15 @@ public class MMOItems extends JavaPlugin { findRpgPlugin(); templateManager.reload(); - + /* * After tiers, sets and upgrade templates are loaded, MI template data - * CAN be fully loaded + * can be fully loaded */ tierManager = new TierManager(); setManager = new SetManager(); upgradeManager = new UpgradeManager(); + templateManager.postloadTemplates(); dropTableManager = new DropTableManager(); dynamicUpdater = new UpdaterManager(); @@ -294,14 +296,13 @@ public class MMOItems extends JavaPlugin { // compatibility with /reload Bukkit.getScheduler().runTask(this, () -> Bukkit.getOnlinePlayers().forEach(player -> PlayerData.load(player))); - if(getConfig().getBoolean("recipes.recipe-amounts")) { + if (getConfig().getBoolean("recipes.recipe-amounts")) { RecipeBookUtil.enableAmounts(); Bukkit.getPluginManager().registerEvents(new CraftingListener(), this); } - if(getConfig().getBoolean("recipes.use-recipe-book")) + if (getConfig().getBoolean("recipes.use-recipe-book")) RecipeBookUtil.enableBook(); - // amount and bukkit recipes getLogger().log(Level.INFO, "Loading recipes, please wait..."); recipeManager.loadRecipes(); 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 c98f4272..77c4e1b5 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 @@ -20,8 +20,9 @@ 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; -public class MMOItemTemplate implements ItemReference { +public class MMOItemTemplate extends PostLoadObject implements ItemReference { private final Type type; private final String id; @@ -42,6 +43,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; } @@ -55,11 +58,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 5d9ea490..6fa4a687 100644 --- a/src/main/java/net/Indyuce/mmoitems/manager/TemplateManager.java +++ b/src/main/java/net/Indyuce/mmoitems/manager/TemplateManager.java @@ -113,6 +113,7 @@ public class TemplateManager { try { MMOItemTemplate template = new MMOItemTemplate(type, type.getConfigFile().getConfig().getConfigurationSection(id)); + template.postLoad(); registerTemplate(template); return template; @@ -174,6 +175,58 @@ 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(); @@ -186,8 +239,7 @@ public class TemplateManager { 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, "Could not load template modifier '" + key + "': " + exception.getMessage()); } } @@ -198,28 +250,8 @@ public class TemplateManager { try { registerTemplate(new MMOItemTemplate(type, config.getConfigurationSection(key))); } catch (IllegalArgumentException exception) { - MMOItems.plugin.getLogger().log(Level.INFO, - "Could not load item template '" + key + "': " + exception.getMessage()); + MMOItems.plugin.getLogger().log(Level.INFO, "Could not load item template '" + key + "': " + exception.getMessage()); } } } - - // 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) { - - } - } - } - }