Stations name update

This commit is contained in:
Roch Blonndiaux 2022-12-07 16:05:54 +01:00
parent 5e55049369
commit d2ad83e218

View File

@ -20,175 +20,179 @@ import java.util.*;
import java.util.logging.Level; import java.util.logging.Level;
public class CraftingStation extends PostLoadObject { public class CraftingStation extends PostLoadObject {
private final String id, name; private final String id;
private final Layout layout; private final String name;
private final Sound sound; private final String unparsedName;
private final StationItemOptions itemOptions; private final Layout layout;
private final int maxQueueSize; private final Sound sound;
private final StationItemOptions itemOptions;
private final int maxQueueSize;
/** /**
* This is not all the recipes of that crafting station. It only * This is not all the recipes of that crafting station. It only
* contains the recipes that are specific to that station, relative * contains the recipes that are specific to that station, relative
* to the position of that station in the station inheritance tree. * to the position of that station in the station inheritance tree.
* <p> * <p>
* In other words that map doesn't contain the crafting recipes of * In other words that map doesn't contain the crafting recipes of
* the parent crafting station saved in {@link #parent} * the parent crafting station saved in {@link #parent}
*/ */
private final Map<String, Recipe> recipes = new LinkedHashMap<>(); private final Map<String, Recipe> recipes = new LinkedHashMap<>();
private CraftingStation parent; private CraftingStation parent;
public CraftingStation(String id, FileConfiguration config) { public CraftingStation(String id, FileConfiguration config) {
super(config); super(config);
this.id = id.toLowerCase().replace("_", "-").replace(" ", "-"); this.id = id.toLowerCase().replace("_", "-").replace(" ", "-");
this.name = config.getString("name", "Unnamed"); this.unparsedName = config.getString("name", "Unnamed");
this.layout = MMOItems.plugin.getLayouts().getLayout(config.getString("layout", "default")); this.name = MythicLib.plugin.parseColors(unparsedName);
this.sound = Sound.valueOf(config.getString("sound", "ENTITY_EXPERIENCE_ORB_PICKUP").toUpperCase()); this.layout = MMOItems.plugin.getLayouts().getLayout(config.getString("layout", "default"));
this.sound = Sound.valueOf(config.getString("sound", "ENTITY_EXPERIENCE_ORB_PICKUP").toUpperCase());
for (String key : config.getConfigurationSection("recipes").getKeys(false)) for (String key : config.getConfigurationSection("recipes").getKeys(false))
try { try {
registerRecipe(loadRecipe(config.getConfigurationSection("recipes." + key))); registerRecipe(loadRecipe(config.getConfigurationSection("recipes." + key)));
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
MMOItems.plugin.getLogger().log(Level.INFO, MMOItems.plugin.getLogger().log(Level.INFO,
"An issue occurred registering recipe '" + key + "' from crafting station '" + id + "': " + exception.getMessage()); "An issue occurred registering recipe '" + key + "' from crafting station '" + id + "': " + exception.getMessage());
} }
itemOptions = new StationItemOptions(config.getConfigurationSection("items")); itemOptions = new StationItemOptions(config.getConfigurationSection("items"));
maxQueueSize = Math.max(1, Math.min(config.getInt("max-queue-size"), 64)); maxQueueSize = Math.max(1, Math.min(config.getInt("max-queue-size"), 64));
} }
public CraftingStation(String id, String name, Layout layout, Sound sound, StationItemOptions itemOptions, int maxQueueSize, CraftingStation parent) { public CraftingStation(String id, String name, Layout layout, Sound sound, StationItemOptions itemOptions, int maxQueueSize, CraftingStation parent) {
super(null); super(null);
Validate.notNull(id, "Crafting station ID must not be null"); Validate.notNull(id, "Crafting station ID must not be null");
Validate.notNull(name, "Crafting station name must not be null"); Validate.notNull(name, "Crafting station name must not be null");
Validate.notNull(sound, "Crafting station sound must not be null"); Validate.notNull(sound, "Crafting station sound must not be null");
this.id = id.toLowerCase().replace("_", "-").replace(" ", "-"); this.id = id.toLowerCase().replace("_", "-").replace(" ", "-");
this.name = name; this.unparsedName = name;
this.layout = layout; this.name = MythicLib.plugin.parseColors(name);
this.sound = sound; this.layout = layout;
this.itemOptions = itemOptions; this.sound = sound;
this.maxQueueSize = maxQueueSize; this.itemOptions = itemOptions;
this.parent = parent; this.maxQueueSize = maxQueueSize;
} this.parent = parent;
}
public String getId() { public String getId() {
return id; return id;
} }
@Deprecated @Deprecated
public String getName() { public String getName() {
return MythicLib.plugin.parseColors(name); return name;
} }
public String getUnformattedName() { public String getUnparsedName() {
return name; return unparsedName;
} }
public Layout getLayout() { public Layout getLayout() {
return layout; return layout;
} }
public Sound getSound() { public Sound getSound() {
return sound; return sound;
} }
@Nullable @Nullable
public CraftingStation getParent() { public CraftingStation getParent() {
return parent; return parent;
} }
/** /**
* @return Recursively collects all recipes from that station and from * @return Recursively collects all recipes from that station and from
* its parent station. * its parent station.
*/ */
public Collection<Recipe> getRecipes() { public Collection<Recipe> getRecipes() {
if (parent == null) if (parent == null)
return recipes.values(); return recipes.values();
// Collect recipes from station inheritance tree // Collect recipes from station inheritance tree
List<Recipe> collected = new ArrayList<>(recipes.values()); List<Recipe> collected = new ArrayList<>(recipes.values());
CraftingStation next = parent; CraftingStation next = parent;
while (next != null) { while (next != null) {
collected.addAll(next.recipes.values()); collected.addAll(next.recipes.values());
next = next.parent; next = next.parent;
} }
return collected; return collected;
} }
/** /**
* @param id Recipe identifier * @param id Recipe identifier
* @return Recursively checks if that station has the provided recipe. * @return Recursively checks if that station has the provided recipe.
*/ */
public boolean hasRecipe(String id) { public boolean hasRecipe(String id) {
return recipes.containsKey(id) || (parent != null && parent.hasRecipe(id)); return recipes.containsKey(id) || (parent != null && parent.hasRecipe(id));
} }
/** /**
* @param id Recipe identifier * @param id Recipe identifier
* @return Recursively finds the corresponding recipe * @return Recursively finds the corresponding recipe
*/ */
public Recipe getRecipe(String id) { public Recipe getRecipe(String id) {
Recipe found = recipes.get(id); Recipe found = recipes.get(id);
return found == null && parent != null ? parent.getRecipe(id) : found; return found == null && parent != null ? parent.getRecipe(id) : found;
} }
public int getMaxQueueSize() { public int getMaxQueueSize() {
return maxQueueSize; return maxQueueSize;
} }
public List<CheckedRecipe> getAvailableRecipes(PlayerData data, IngredientInventory inv) { public List<CheckedRecipe> getAvailableRecipes(PlayerData data, IngredientInventory inv) {
List<CheckedRecipe> infos = new ArrayList<>(); List<CheckedRecipe> infos = new ArrayList<>();
for (Recipe recipe : getRecipes()) { for (Recipe recipe : getRecipes()) {
CheckedRecipe info = recipe.evaluateRecipe(data, inv); CheckedRecipe info = recipe.evaluateRecipe(data, inv);
if ((info.areConditionsMet() || !info.getRecipe().hasOption(RecipeOption.HIDE_WHEN_LOCKED)) if ((info.areConditionsMet() || !info.getRecipe().hasOption(RecipeOption.HIDE_WHEN_LOCKED))
&& (info.allIngredientsHad() || !info.getRecipe().hasOption(RecipeOption.HIDE_WHEN_NO_INGREDIENTS))) && (info.allIngredientsHad() || !info.getRecipe().hasOption(RecipeOption.HIDE_WHEN_NO_INGREDIENTS)))
infos.add(info); infos.add(info);
} }
return infos; return infos;
} }
public StationItemOptions getItemOptions() { public StationItemOptions getItemOptions() {
return itemOptions; return itemOptions;
} }
/** /**
* Keep in mind this method also has the effect of register a recipe * Keep in mind this method also has the effect of register a recipe
* inside any crafting station that has the current station as child. * inside any crafting station that has the current station as child.
* *
* @param recipe Recipe being registered * @param recipe Recipe being registered
* @see {@link #hasRecipe(String)} * @see {@link #hasRecipe(String)}
*/ */
public void registerRecipe(Recipe recipe) { public void registerRecipe(Recipe recipe) {
recipes.put(recipe.getId(), recipe); recipes.put(recipe.getId(), recipe);
} }
public int getMaxPage() { public int getMaxPage() {
int recipes = getRecipes().size(); int recipes = getRecipes().size();
return Math.max(1, (int) Math.ceil((double) recipes / getLayout().getRecipeSlots().size())); return Math.max(1, (int) Math.ceil((double) recipes / getLayout().getRecipeSlots().size()));
} }
@Override @Override
protected void whenPostLoaded(ConfigurationSection config) { protected void whenPostLoaded(ConfigurationSection config) {
if (config.contains("parent")) { if (config.contains("parent")) {
String id = config.getString("parent").toLowerCase().replace(" ", "-").replace("_", "-"); String id = config.getString("parent").toLowerCase().replace(" ", "-").replace("_", "-");
Validate.isTrue(!id.equals(this.id), "Station cannot use itself as parent"); 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 + "'"); Validate.isTrue(MMOItems.plugin.getCrafting().hasStation(id), "Could not find parent station with ID '" + id + "'");
parent = MMOItems.plugin.getCrafting().getStation(id); parent = MMOItems.plugin.getCrafting().getStation(id);
} }
} }
/* /*
* find type of crafting recipe based on section. there is no 'type' recipe * 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 * parameter because old files would be out of date, instead just looks for
* a parameter of the crafting recipe which is 'output' * a parameter of the crafting recipe which is 'output'
*/ */
private Recipe loadRecipe(ConfigurationSection config) throws IllegalArgumentException { private Recipe loadRecipe(ConfigurationSection config) throws IllegalArgumentException {
return config.contains("output") ? new CraftingRecipe(config) : new UpgradingRecipe(config); return config.contains("output") ? new CraftingRecipe(config) : new UpgradingRecipe(config);
} }
} }