From 22e05d140fdd0d1bc4b46ad97d45823b911de4d9 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sat, 25 Sep 2021 03:20:25 +0200 Subject: [PATCH] Moved price and it's methods to Utilities --- .../java/com/alttd/GUI/windows/BuyGUI.java | 4 +- .../java/com/alttd/GUI/windows/SellGUI.java | 4 +- .../java/com/alttd/economy/Calculation.java | 67 ------------------- src/main/java/com/alttd/util/Utilities.java | 63 +++++++++++++++++ 4 files changed, 67 insertions(+), 71 deletions(-) delete mode 100644 src/main/java/com/alttd/economy/Calculation.java diff --git a/src/main/java/com/alttd/GUI/windows/BuyGUI.java b/src/main/java/com/alttd/GUI/windows/BuyGUI.java index 968549c..9a5b963 100644 --- a/src/main/java/com/alttd/GUI/windows/BuyGUI.java +++ b/src/main/java/com/alttd/GUI/windows/BuyGUI.java @@ -2,8 +2,8 @@ package com.alttd.GUI.windows; import com.alttd.GUI.GUIMerchant; import com.alttd.config.Config; -import com.alttd.economy.Calculation; import com.alttd.objects.VillagerType; +import com.alttd.util.Utilities; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.Template; import org.bukkit.Material; @@ -19,7 +19,7 @@ public class BuyGUI extends GUIMerchant { Template.of("trader", villagerType.getDisplayName()), Template.of("percentage", "100")), villagerType); //TODO get percentage from player somehow for (ItemStack itemStack : villagerType.getBuying()) { - double price = Calculation.price(itemStack); + double price = Utilities.price(itemStack); addItem(itemStack, getPriceItem(price), null, diff --git a/src/main/java/com/alttd/GUI/windows/SellGUI.java b/src/main/java/com/alttd/GUI/windows/SellGUI.java index 1235859..5ac015c 100644 --- a/src/main/java/com/alttd/GUI/windows/SellGUI.java +++ b/src/main/java/com/alttd/GUI/windows/SellGUI.java @@ -2,8 +2,8 @@ package com.alttd.GUI.windows; import com.alttd.GUI.GUIMerchant; import com.alttd.config.Config; -import com.alttd.economy.Calculation; import com.alttd.objects.VillagerType; +import com.alttd.util.Utilities; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.Template; import org.bukkit.Material; @@ -19,7 +19,7 @@ public class SellGUI extends GUIMerchant { Template.of("trader", villagerType.getDisplayName()), Template.of("percentage", "100")), villagerType); //TODO get percentage from player somehow for (ItemStack itemStack : villagerType.getSelling()) { - double price = Calculation.price(itemStack); + double price = Utilities.price(itemStack); addItem(itemStack, getPriceItem(price), null, diff --git a/src/main/java/com/alttd/economy/Calculation.java b/src/main/java/com/alttd/economy/Calculation.java deleted file mode 100644 index c3e31f5..0000000 --- a/src/main/java/com/alttd/economy/Calculation.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.alttd.economy; - -import com.alttd.config.WorthConfig; -import com.alttd.util.Utilities; -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.inventory.*; - -import java.util.Collection; -import java.util.List; - -public class Calculation { - - /** - * Calculate the price for an item - * @param item to calculate price for - * @return price or int < 0 for error - */ - public static double price(ItemStack item) { - if (WorthConfig.prices.containsKey(item.getType())) - return Utilities.round(WorthConfig.prices.getDouble(item.getType()) * item.getAmount(), 2); - - WorthConfig.prices.put(item.getType(), Utilities.round(getPrice(item, null), 2)); - - return WorthConfig.prices.getDouble(item.getType()) * item.getAmount(); - } - - private static double getPrice(ItemStack item, Material blockedMaterial) { - if (WorthConfig.prices.containsKey(item.getType())) - return WorthConfig.prices.getDouble(item.getType()); - double price = -1; - List recipes = Bukkit.getRecipesFor(item); - for (Recipe recipe : recipes) { - double possiblePrice; - if (recipe instanceof ShapedRecipe shapedRecipe) { - Collection values = shapedRecipe.getIngredientMap().values(); - if (values.stream().anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial))) - continue; - possiblePrice = getPrice(values.stream().toList(), item.getType()); - if (price == -1 || price > possiblePrice) - price = possiblePrice; - } else if (recipe instanceof ShapelessRecipe shapelessRecipe) { - if (shapelessRecipe.getIngredientList().stream().anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial))) - continue; - possiblePrice = getPrice(shapelessRecipe.getIngredientList(), item.getType()); - if (price == -1 || price > possiblePrice) - price = possiblePrice; - } else if (recipe instanceof FurnaceRecipe furnaceRecipe) { - possiblePrice = getPrice(furnaceRecipe.getInput(), item.getType()); - if (price == -1 || price > possiblePrice) - price = possiblePrice; - } - } - return price; - } - - private static double getPrice(List items, Material blockedMaterial) { - double price = 0; - for (ItemStack item : items) { - double tmp = getPrice(item, blockedMaterial); - if (tmp == -1) - return -1; - price += tmp; - } - return price; - } -} diff --git a/src/main/java/com/alttd/util/Utilities.java b/src/main/java/com/alttd/util/Utilities.java index f47c449..34dd7f2 100644 --- a/src/main/java/com/alttd/util/Utilities.java +++ b/src/main/java/com/alttd/util/Utilities.java @@ -1,5 +1,13 @@ package com.alttd.util; +import com.alttd.config.WorthConfig; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.inventory.*; + +import java.util.Collection; +import java.util.List; + public class Utilities { /** * Rounds num down to precision (rounds up if last cut off decimal is bigger than 4) @@ -22,4 +30,59 @@ public class Utilities { return total; } + + /** + * Calculate the price for an ItemStack (this considers stack size) + * + * @param item to calculate price for + * @return price or int < 0 for error + */ + public static double price(ItemStack item) { + if (WorthConfig.prices.containsKey(item.getType())) + return Utilities.round(WorthConfig.prices.getDouble(item.getType()) * item.getAmount(), 2); + + WorthConfig.prices.put(item.getType(), Utilities.round(getPrice(item, null), 2)); + + return WorthConfig.prices.getDouble(item.getType()) * item.getAmount(); + } + + private static double getPrice(ItemStack item, Material blockedMaterial) { + if (WorthConfig.prices.containsKey(item.getType())) + return WorthConfig.prices.getDouble(item.getType()); + double price = -1; + List recipes = Bukkit.getRecipesFor(item); + for (Recipe recipe : recipes) { + double possiblePrice; + if (recipe instanceof ShapedRecipe shapedRecipe) { + Collection values = shapedRecipe.getIngredientMap().values(); + if (values.stream().anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial))) + continue; + possiblePrice = getPrice(values.stream().toList(), item.getType()); + if (price == -1 || price > possiblePrice) + price = possiblePrice; + } else if (recipe instanceof ShapelessRecipe shapelessRecipe) { + if (shapelessRecipe.getIngredientList().stream().anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial))) + continue; + possiblePrice = getPrice(shapelessRecipe.getIngredientList(), item.getType()); + if (price == -1 || price > possiblePrice) + price = possiblePrice; + } else if (recipe instanceof FurnaceRecipe furnaceRecipe) { + possiblePrice = getPrice(furnaceRecipe.getInput(), item.getType()); + if (price == -1 || price > possiblePrice) + price = possiblePrice; + } + } + return price; + } + + private static double getPrice(List items, Material blockedMaterial) { + double price = 0; + for (ItemStack item : items) { + double tmp = getPrice(item, blockedMaterial); + if (tmp == -1) + return -1; + price += tmp; + } + return price; + } }