From 2ce87936c77c495a9b2eee33487eb3348b196ff6 Mon Sep 17 00:00:00 2001 From: Flowsqy <47575244+Flowsqy@users.noreply.github.com> Date: Sun, 13 Feb 2022 22:11:57 +0100 Subject: [PATCH] End comments for understanding purpose. Add a lot of TODO comments. (HologramFormat) --- .../config/hologram/HologramFormat.java | 17 ++++++++++++++++- .../java/de/epiceric/shopchest/shop/Shop.java | 3 +++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/plugin/src/main/java/de/epiceric/shopchest/config/hologram/HologramFormat.java b/plugin/src/main/java/de/epiceric/shopchest/config/hologram/HologramFormat.java index 042527c..6079ef4 100644 --- a/plugin/src/main/java/de/epiceric/shopchest/config/hologram/HologramFormat.java +++ b/plugin/src/main/java/de/epiceric/shopchest/config/hologram/HologramFormat.java @@ -18,6 +18,12 @@ import de.epiceric.shopchest.ShopChest; public class HologramFormat { + /* + TODO Change implementation of this class + -> Deserialize from the configuration and load it some way to avoid String manipulation at each method invocation + -> Rework the current String manipulation process to add complex expression evaluation WITHOUT nashorn engine + */ + public enum Requirement { VENDOR, AMOUNT, ITEM_TYPE, ITEM_NAME, HAS_ENCHANTMENT, BUY_PRICE, SELL_PRICE, HAS_POTION_EFFECT, IS_MUSIC_DISC, IS_POTION_EXTENDED, IS_BANNER_PATTERN, @@ -63,6 +69,7 @@ public class HologramFormat { // Check every requirement for (String sReq : requirements) { + //TODO Maybe remove some loops as every requirements are re evaluated in the #evalRequirement for (Requirement req : reqMap.keySet()) { // If the configuration requirement contain a requirement specified by the shop if (sReq.contains(req.toString())) { @@ -97,6 +104,9 @@ public class HologramFormat { * @return Whether the hologram text has to change dynamically without reloading */ public boolean isDynamic() { + // Return whether an option contains STOCK or CHEST_SPACE : + // - In the format + // - In one of its requirement int count = getLineCount(); for (int i = 0; i < count; i++) { ConfigurationSection options = config.getConfigurationSection("lines." + i + ".options"); @@ -127,7 +137,7 @@ public class HologramFormat { return config.getConfigurationSection("lines").getKeys(false).size(); } - /** + /** * @return Configuration of the "hologram-format.yml" file */ public YamlConfiguration getConfig() { @@ -143,6 +153,7 @@ public class HologramFormat { public boolean evalRequirement(String condition, Map values) { String cond = condition; + // Double-check the presence of a requirement (WTF ?) for (HologramFormat.Requirement req : HologramFormat.Requirement.values()) { if (cond.contains(req.toString()) && values.containsKey(req)) { Object val = values.get(req); @@ -156,6 +167,8 @@ public class HologramFormat { } } + // Evaluate three basic condition : Direct Boolean, Math comparison and String equality + if (cond.equals("true")) { // e.g.: ADMIN_SHOP return true; @@ -203,6 +216,7 @@ public class HologramFormat { } // complex comparison + // Like && and || or other arithmetic operations try { return (boolean) engine.eval(cond); } catch (ScriptException e) { @@ -220,6 +234,7 @@ public class HologramFormat { * @return Result of the condition */ public String evalPlaceholder(String string, Map values) { + // Detect and evaluate accolade inner parts try { Matcher matcher = Pattern.compile("\\{([^}]+)}").matcher(string); String newString = string; diff --git a/plugin/src/main/java/de/epiceric/shopchest/shop/Shop.java b/plugin/src/main/java/de/epiceric/shopchest/shop/Shop.java index 60852cd..7ebe88e 100644 --- a/plugin/src/main/java/de/epiceric/shopchest/shop/Shop.java +++ b/plugin/src/main/java/de/epiceric/shopchest/shop/Shop.java @@ -260,6 +260,8 @@ public class Shop { ItemStack itemStack = getProduct().getItemStack(); + // Create requirements base on the shop value + // (As requirements are always the same, only set requirements to the shop value) Map requirements = new EnumMap<>(HologramFormat.Requirement.class); requirements.put(HologramFormat.Requirement.VENDOR, getVendor().getName()); requirements.put(HologramFormat.Requirement.AMOUNT, getProduct().getAmount()); @@ -280,6 +282,7 @@ public class Shop { requirements.put(HologramFormat.Requirement.CHEST_SPACE, Utils.getFreeSpaceForItem(inventory, itemStack)); requirements.put(HologramFormat.Requirement.DURABILITY, itemStack.getDurability()); + // Same as requirements Map placeholders = new EnumMap<>(Placeholder.class); placeholders.put(Placeholder.VENDOR, getVendor().getName()); placeholders.put(Placeholder.AMOUNT, getProduct().getAmount());