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/interaction/GemStone.java b/src/main/java/net/Indyuce/mmoitems/api/interaction/GemStone.java index 7fab6a8f..e2974b5c 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/interaction/GemStone.java +++ b/src/main/java/net/Indyuce/mmoitems/api/interaction/GemStone.java @@ -71,7 +71,7 @@ public class GemStone extends UseItem { * permanent effects. also REGISTER gem stone in the item gem stone * list. */ - LiveMMOItem mmo = new LiveMMOItem(nbt); + LiveMMOItem mmo = new LiveMMOItem(getNBTItem()); GemstoneData gemData = new GemstoneData(mmo); sockets.apply(gemType, gemData); diff --git a/src/main/java/net/Indyuce/mmoitems/api/interaction/Tool.java b/src/main/java/net/Indyuce/mmoitems/api/interaction/Tool.java index 9b1c62eb..8848bd27 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/interaction/Tool.java +++ b/src/main/java/net/Indyuce/mmoitems/api/interaction/Tool.java @@ -28,7 +28,7 @@ public class Tool extends UseItem { public boolean miningEffects(Block block) { boolean cancel = false; - if (nbt.getBoolean("MMOITEMS_AUTOSMELT")) + if (getNBTItem().getBoolean("MMOITEMS_AUTOSMELT")) if (block.getType() == Material.IRON_ORE || block.getType() == Material.GOLD_ORE) { ItemStack item = new ItemStack(Material.valueOf(block.getType().name().replace("_ORE", "") + "_INGOT")); @@ -39,7 +39,7 @@ public class Tool extends UseItem { cancel = true; } - if (nbt.getBoolean("MMOITEMS_BOUNCING_CRACK")) + if (getNBTItem().getBoolean("MMOITEMS_BOUNCING_CRACK")) new BukkitRunnable() { Vector v = player.getEyeLocation().getDirection().multiply(.5); Location loc = block.getLocation().clone().add(.5, .5, .5); @@ -60,8 +60,8 @@ public class Tool extends UseItem { } }.runTaskTimer(MMOItems.plugin, 0, 1); - if (nbt.hasTag("MMOITEMS_BREAK_SIZE")) { - int breakSize = nbt.getInteger("MMOITEMS_BREAK_SIZE"); + if (getNBTItem().hasTag("MMOITEMS_BREAK_SIZE")) { + int breakSize = getNBTItem().getInteger("MMOITEMS_BREAK_SIZE"); if(breakSize % 2 != 0) { BlockFace face = player.getFacing(); System.out.println("Debug: Facing - " + face); diff --git a/src/main/java/net/Indyuce/mmoitems/api/interaction/UseItem.java b/src/main/java/net/Indyuce/mmoitems/api/interaction/UseItem.java index ffd68d1a..500bbf45 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/interaction/UseItem.java +++ b/src/main/java/net/Indyuce/mmoitems/api/interaction/UseItem.java @@ -28,7 +28,6 @@ public class UseItem { protected final Player player; protected final PlayerData playerData; protected final VolatileMMOItem mmoitem; - protected final NBTItem nbt; protected static final Random random = new Random(); @@ -40,7 +39,6 @@ public class UseItem { this.player = playerData.getPlayer(); this.playerData = playerData; this.mmoitem = new VolatileMMOItem(nbtItem); - this.nbt = nbtItem; } public Player getPlayer() { @@ -56,15 +54,15 @@ public class UseItem { } public NBTItem getNBTItem() { - return nbt; + return mmoitem.getNBT(); } public ItemStack getItem() { - return nbt.getItem(); + return mmoitem.getNBT().getItem(); } public boolean canBeUsed() { - return playerData.getRPG().canUse(nbt, true); + return playerData.getRPG().canUse(mmoitem.getNBT(), true); } public void executeCommands() { 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/api/util/SoundReader.java b/src/main/java/net/Indyuce/mmoitems/api/util/SoundReader.java index 6278d72e..25c1ac5b 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/util/SoundReader.java +++ b/src/main/java/net/Indyuce/mmoitems/api/util/SoundReader.java @@ -4,21 +4,16 @@ import org.bukkit.Sound; import org.bukkit.entity.Player; public class SoundReader { - private Sound sound; - private boolean valid; + private final Sound sound; public SoundReader(String tag, Sound defaultSound) { - if (tag.equals("")) { - sound = defaultSound; - return; - } - + Sound sound; try { sound = Sound.valueOf(tag); - valid = true; } catch (Exception e) { sound = defaultSound; } + this.sound = sound; } public Sound getSound() { @@ -30,7 +25,6 @@ public class SoundReader { } public void play(Player player, float vol, float pitch) { - if (valid) - player.playSound(player.getLocation(), sound, vol, pitch); + player.playSound(player.getLocation(), sound, vol, pitch); } } diff --git a/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java b/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java index d5dc1299..097d6ccb 100644 --- a/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java +++ b/src/main/java/net/Indyuce/mmoitems/listener/ItemUse.java @@ -172,7 +172,7 @@ public class ItemUse implements Listener { event.setDamage(result.getDamage()); } - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void c(BlockBreakEvent event) { Player player = event.getPlayer(); Block block = event.getBlock(); diff --git a/src/main/java/net/Indyuce/mmoitems/manager/RecipeManager.java b/src/main/java/net/Indyuce/mmoitems/manager/RecipeManager.java index bf33d140..d3a5a961 100644 --- a/src/main/java/net/Indyuce/mmoitems/manager/RecipeManager.java +++ b/src/main/java/net/Indyuce/mmoitems/manager/RecipeManager.java @@ -217,7 +217,7 @@ public class RecipeManager { } /** - * Used to handle furnace/smoker/campifire/furnace extra crafting recipe + * Used to handle furnace/smoker/campfire/furnace extra crafting recipe * parameters * * @author ASangarin 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) { - - } - } - } - }