diff --git a/src/main/java/net/Indyuce/mmoitems/api/crafting/CraftingStation.java b/src/main/java/net/Indyuce/mmoitems/api/crafting/CraftingStation.java index 01a57b4b..2e632087 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/crafting/CraftingStation.java +++ b/src/main/java/net/Indyuce/mmoitems/api/crafting/CraftingStation.java @@ -18,15 +18,20 @@ import net.Indyuce.mmoitems.api.crafting.recipe.Recipe.RecipeOption; import net.Indyuce.mmoitems.api.crafting.recipe.RecipeInfo; import net.Indyuce.mmoitems.api.crafting.recipe.UpgradingRecipe; import net.Indyuce.mmoitems.api.player.PlayerData; +import net.Indyuce.mmoitems.api.util.PostLoadObject; import net.asangarin.hexcolors.ColorParse; -public class CraftingStation { +public class CraftingStation extends PostLoadObject { private final String id, name; private final StationItemOptions itemOptions; private final int maxQueueSize; private final Map recipes = new LinkedHashMap<>(); + private CraftingStation parent; + public CraftingStation(String id, FileConfiguration config) { + super(config); + this.id = id.toLowerCase().replace("_", "-").replace(" ", "-"); this.name = new ColorParse('&', config.getString("name")).toChatColor(); @@ -34,14 +39,18 @@ public class CraftingStation { try { registerRecipe(loadRecipe(config.getConfigurationSection("recipes." + key))); } catch (IllegalArgumentException exception) { - MMOItems.plugin.getLogger().log(Level.INFO, "An issue occured registering recipe '" + key + "' from crafting station '" + id + "': " + exception.getMessage()); + MMOItems.plugin.getLogger().log(Level.INFO, "An issue occured registering recipe '" + key + + "' from crafting station '" + id + "': " + exception.getMessage()); } itemOptions = new StationItemOptions(config.getConfigurationSection("items")); maxQueueSize = Math.max(1, Math.min(config.getInt("max-queue-size"), 64)); } - public CraftingStation(String id, String name, StationItemOptions itemOptions, int maxQueueSize) { + public CraftingStation(String id, String name, 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"); @@ -49,6 +58,7 @@ public class CraftingStation { this.name = new ColorParse('&', name).toChatColor(); this.itemOptions = itemOptions; this.maxQueueSize = maxQueueSize; + this.parent = parent; } public String getId() { @@ -59,8 +69,23 @@ public class CraftingStation { return name; } + public CraftingStation getParent() { + return parent; + } + public Collection getRecipes() { - return recipes.values(); + if (parent == null) + return recipes.values(); + + // collect recipes from station inheritance tree + List collected = new ArrayList<>(recipes.values()); + CraftingStation next = parent; + while (next != null) { + collected.addAll(next.getRecipes()); + next = next.getParent(); + } + + return collected; } public boolean hasRecipe(String id) { @@ -99,10 +124,21 @@ public class CraftingStation { return Math.max(1, (int) Math.ceil((double) recipes.size() / 14)); } + @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 - * a parameter of the crafting recipe which is 'output' + * parameter because old files would be out of date, instead just looks for a + * parameter of the crafting recipe which is 'output' */ private Recipe loadRecipe(ConfigurationSection config) { return config.contains("output") ? new CraftingRecipe(config) : new UpgradingRecipe(config); diff --git a/src/main/java/net/Indyuce/mmoitems/api/crafting/trigger/CommandTrigger.java b/src/main/java/net/Indyuce/mmoitems/api/crafting/trigger/CommandTrigger.java index ece88d84..c62845c5 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/crafting/trigger/CommandTrigger.java +++ b/src/main/java/net/Indyuce/mmoitems/api/crafting/trigger/CommandTrigger.java @@ -5,17 +5,20 @@ import net.mmogroup.mmolib.api.MMOLineConfig; import org.bukkit.Bukkit; public class CommandTrigger extends Trigger { - private final String command; + private final String command; + private final boolean player; - public CommandTrigger(MMOLineConfig config) { - super("command"); + public CommandTrigger(MMOLineConfig config) { + super("command"); - config.validate("format"); - command = config.getString("format"); - } + config.validate("format"); + player = config.getBoolean("player"); + command = config.getString("format"); + } - @Override - public void whenCrafting(PlayerData data) { - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command.replace("%player%", data.getPlayer().getName())); - } + @Override + public void whenCrafting(PlayerData data) { + Bukkit.dispatchCommand(player ? data.getPlayer() : Bukkit.getConsoleSender(), + command.replace("%player%", data.getPlayer().getName())); + } } diff --git a/src/main/java/net/Indyuce/mmoitems/api/crafting/trigger/MessageTrigger.java b/src/main/java/net/Indyuce/mmoitems/api/crafting/trigger/MessageTrigger.java index c39e59c9..4eaa7026 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/crafting/trigger/MessageTrigger.java +++ b/src/main/java/net/Indyuce/mmoitems/api/crafting/trigger/MessageTrigger.java @@ -3,21 +3,19 @@ package net.Indyuce.mmoitems.api.crafting.trigger; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.api.player.PlayerData; import net.mmogroup.mmolib.api.MMOLineConfig; -import org.bukkit.Bukkit; public class MessageTrigger extends Trigger { - private final String message; + private final String message; - public MessageTrigger(MMOLineConfig config) { - super("message"); + public MessageTrigger(MMOLineConfig config) { + super("message"); - config.validate("format"); - message = config.getString("format"); - } + config.validate("format"); + message = config.getString("format"); + } - @Override - public void whenCrafting(PlayerData data) { - data.getPlayer().sendMessage(MMOItems.plugin.getPlaceholderParser().parse(data.getPlayer(), - message.replace("%player%", data.getPlayer().getName()))); - } + @Override + public void whenCrafting(PlayerData data) { + data.getPlayer().sendMessage(MMOItems.plugin.getPlaceholderParser().parse(data.getPlayer(), message)); + } } diff --git a/src/main/java/net/Indyuce/mmoitems/api/crafting/trigger/SoundTrigger.java b/src/main/java/net/Indyuce/mmoitems/api/crafting/trigger/SoundTrigger.java index 318c8aaa..cd1f91d1 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/crafting/trigger/SoundTrigger.java +++ b/src/main/java/net/Indyuce/mmoitems/api/crafting/trigger/SoundTrigger.java @@ -5,21 +5,21 @@ import net.mmogroup.mmolib.api.MMOLineConfig; import org.bukkit.Sound; public class SoundTrigger extends Trigger { - private final Sound sound; - private final float vol, pitch; + private final Sound sound; + private final float vol, pitch; - public SoundTrigger(MMOLineConfig config) { - super("sound"); + public SoundTrigger(MMOLineConfig config) { + super("sound"); - config.validate("sound"); + config.validate("sound"); - sound = Sound.valueOf(config.getString("sound").toUpperCase().replace("-", "_")); - vol = config.contains("volume") ? (float) config.getDouble("volume") : 1f; - pitch = config.contains("pitch") ? (float) config.getDouble("pitch") : 1f; - } + sound = Sound.valueOf(config.getString("sound").toUpperCase().replace("-", "_")); + vol = (float) config.getDouble("volume", 1); + pitch = (float) config.getDouble("pitch", 1); + } - @Override - public void whenCrafting(PlayerData player) { - player.getPlayer().playSound(player.getPlayer().getLocation(), sound, vol, pitch); - } + @Override + public void whenCrafting(PlayerData player) { + player.getPlayer().playSound(player.getPlayer().getLocation(), sound, vol, pitch); + } } diff --git a/src/main/java/net/Indyuce/mmoitems/api/util/PostLoadObject.java b/src/main/java/net/Indyuce/mmoitems/api/util/PostLoadObject.java new file mode 100644 index 00000000..3027c283 --- /dev/null +++ b/src/main/java/net/Indyuce/mmoitems/api/util/PostLoadObject.java @@ -0,0 +1,27 @@ +package net.Indyuce.mmoitems.api.util; + +import org.bukkit.configuration.ConfigurationSection; + +public abstract class PostLoadObject { + private ConfigurationSection config; + + /* + * objects which must load some data afterwards, like quests which must load + * their parent quests after all quests were initialized or classes which + * must load their subclasses + */ + public PostLoadObject(ConfigurationSection config) { + this.config = config; + } + + public void postLoad() { + whenPostLoaded(config); + + /* + * clean config object for garbage collection + */ + config = null; + } + + protected abstract void whenPostLoaded(ConfigurationSection config); +} \ No newline at end of file diff --git a/src/main/java/net/Indyuce/mmoitems/comp/mmocore/stat/Required_Profession.java b/src/main/java/net/Indyuce/mmoitems/comp/mmocore/stat/Required_Profession.java index 1eeb0352..9c38556f 100644 --- a/src/main/java/net/Indyuce/mmoitems/comp/mmocore/stat/Required_Profession.java +++ b/src/main/java/net/Indyuce/mmoitems/comp/mmocore/stat/Required_Profession.java @@ -1,5 +1,10 @@ package net.Indyuce.mmoitems.comp.mmocore.stat; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.inventory.ItemStack; + import net.Indyuce.mmocore.api.experience.Profession; import net.Indyuce.mmoitems.api.player.RPGPlayer; import net.Indyuce.mmoitems.api.util.message.AddonMessage; @@ -7,11 +12,6 @@ import net.Indyuce.mmoitems.comp.mmocore.MMOCoreHook; import net.Indyuce.mmoitems.stat.type.DoubleStat; import net.Indyuce.mmoitems.stat.type.ItemRestriction; import net.mmogroup.mmolib.api.item.NBTItem; -import net.mmogroup.mmolib.version.VersionMaterial; -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.Sound; -import org.bukkit.inventory.ItemStack; public class Required_Profession extends DoubleStat implements ItemRestriction { private final Profession profession; diff --git a/src/main/java/net/Indyuce/mmoitems/comp/parse/placeholders/PlaceholderAPIParser.java b/src/main/java/net/Indyuce/mmoitems/comp/parse/placeholders/PlaceholderAPIParser.java index 920e13ae..ad13d5a5 100644 --- a/src/main/java/net/Indyuce/mmoitems/comp/parse/placeholders/PlaceholderAPIParser.java +++ b/src/main/java/net/Indyuce/mmoitems/comp/parse/placeholders/PlaceholderAPIParser.java @@ -12,6 +12,7 @@ public class PlaceholderAPIParser implements PlaceholderParser { @Override public String parse(OfflinePlayer player, String string) { - return new ColorParse('&', PlaceholderAPI.setPlaceholders(player, string.replace("%player%", player.getName()))).toChatColor(); + return new ColorParse('&', PlaceholderAPI.setPlaceholders(player, string.replace("%player%", player.getName()))) + .toChatColor(); } } diff --git a/src/main/java/net/Indyuce/mmoitems/manager/CraftingManager.java b/src/main/java/net/Indyuce/mmoitems/manager/CraftingManager.java index ace1d387..a2ed25bf 100644 --- a/src/main/java/net/Indyuce/mmoitems/manager/CraftingManager.java +++ b/src/main/java/net/Indyuce/mmoitems/manager/CraftingManager.java @@ -108,7 +108,15 @@ public class CraftingManager { CraftingStation station = new CraftingStation(file.getName().substring(0, file.getName().length() - 4), YamlConfiguration.loadConfiguration(file)); stations.put(station.getId(), station); } catch (IllegalArgumentException exception) { - MMOItems.plugin.getLogger().log(Level.WARNING, "Could not load station " + file.getName() + ": " + exception.getMessage()); + MMOItems.plugin.getLogger().log(Level.WARNING, "Could not load station '" + file.getName() + "': " + exception.getMessage()); + } + + for (CraftingStation station : stations.values()) + try { + station.postLoad(); + } catch (IllegalArgumentException exception) { + MMOItems.plugin.getLogger().log(Level.WARNING, + "Could not post-load station '" + station.getId() + "': " + exception.getMessage()); } } @@ -251,5 +259,4 @@ public class CraftingManager { return read.apply(item); } } - } diff --git a/src/main/resources/default/crafting-stations/mythical-forge.yml b/src/main/resources/default/crafting-stations/mythical-forge.yml index e3ff4b04..e01b7b3e 100644 --- a/src/main/resources/default/crafting-stations/mythical-forge.yml +++ b/src/main/resources/default/crafting-stations/mythical-forge.yml @@ -7,6 +7,10 @@ name: 'Mythical Forge (#page#/#max#)' # max number of items players are able to craft simultaneously. max-queue-size: 10 +# Crafting station parent. This station will inherit of +# all the recipes of the specified crafting station +parent: arcane-forge + # Configure GUI items here items: fill: