Added parent crafting stations

This commit is contained in:
Jules 2020-07-29 11:25:03 +02:00
parent 2e99b03387
commit 2b955de729
8 changed files with 120 additions and 44 deletions

View File

@ -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<String, Recipe> 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<Recipe> getRecipes() {
return recipes.values();
if (parent == null)
return recipes.values();
// collect recipes from station inheritance tree
List<Recipe> 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);

View File

@ -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()));
}
}

View File

@ -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));
}
}

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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();
}
}

View File

@ -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);
}
}
}

View File

@ -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: