diff --git a/src/main/java/com/alttd/config/WorthConfig.java b/src/main/java/com/alttd/config/WorthConfig.java index 3b52593..a43e190 100644 --- a/src/main/java/com/alttd/config/WorthConfig.java +++ b/src/main/java/com/alttd/config/WorthConfig.java @@ -1,13 +1,16 @@ package com.alttd.config; import com.alttd.VillagerUI; +import com.alttd.objects.Price; import com.alttd.util.Utilities; import it.unimi.dsi.fastutil.objects.Object2DoubleMap; import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import java.io.File; +import java.util.HashMap; import java.util.Set; public class WorthConfig extends AbstractConfig { @@ -27,19 +30,23 @@ public class WorthConfig extends AbstractConfig { config.readConfig(WorthConfig.class, null); } - public static Object2DoubleMap prices = new Object2DoubleOpenHashMap<>(); + public static Object2ObjectOpenHashMap prices = new Object2ObjectOpenHashMap<>(); private static void loadWorth() { prices.clear(); ConfigurationSection worth = config.getConfigurationSection("worth"); - Set keys = worth.getKeys(false); - for (String key : keys) { - Material material = Material.getMaterial(key); - if (material == null) { - VillagerUI.getInstance().getLogger().warning("Invalid key in worth.yml -> " + key); - continue; + Set points = worth.getKeys(false); + for (String point : points) { + ConfigurationSection pointSection = worth.getConfigurationSection(point); + Set materials = worth.getConfigurationSection(point).getKeys(false); + for (String key : materials) { + Material material = Material.getMaterial(key); + if (material == null) { + VillagerUI.getInstance().getLogger().warning("Invalid key in worth.yml -> " + key); + continue; + } + prices.put(Material.getMaterial(key), new Price(Utilities.round(pointSection.getDouble(key), 2), Integer.parseInt(point))); } - prices.put(Material.getMaterial(key), Utilities.round(worth.getDouble(key), 2)); } } } diff --git a/src/main/java/com/alttd/objects/Price.java b/src/main/java/com/alttd/objects/Price.java new file mode 100644 index 0000000..93715a5 --- /dev/null +++ b/src/main/java/com/alttd/objects/Price.java @@ -0,0 +1,17 @@ +package com.alttd.objects; + +import com.alttd.util.Utilities; + +public record Price(double price, int points) { + public static Price addPrice(Price one, Price two) { + return (new Price(Utilities.round(one.price() + two.price(), 2), one.points() + two.points())); + } + + public double getPrice(int multiplier) { + return (Utilities.round(price * multiplier, 2)); + } + + public int getPoints(int multiplier) { + return (points * multiplier); + } +} diff --git a/src/main/java/com/alttd/util/Utilities.java b/src/main/java/com/alttd/util/Utilities.java index 62440a2..2a89fbb 100644 --- a/src/main/java/com/alttd/util/Utilities.java +++ b/src/main/java/com/alttd/util/Utilities.java @@ -1,17 +1,12 @@ package com.alttd.util; import com.alttd.config.WorthConfig; -import jdk.swing.interop.SwingInterOpUtils; +import com.alttd.objects.Price; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.inventory.*; -import org.bukkit.material.MaterialData; -import java.util.Collection; -import java.util.HashSet; import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; public class Utilities { /** @@ -41,13 +36,14 @@ public class Utilities { * @param item to calculate price for * @return price or int < 0 for error */ - public static double getWorth(ItemStack item) { + public static Price getPrice(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(getWorth(item, null), 2)); - - return Utilities.round(WorthConfig.prices.getDouble(item.getType()) * item.getAmount(), 2); + return (WorthConfig.prices.get(item.getType())); + Price price = getWorth(item, null); + if (price == null) + return (null); + WorthConfig.prices.put(item.getType(), price); + return (WorthConfig.prices.get(item.getType())); } /** @@ -57,37 +53,48 @@ public class Utilities { * @param blockedMaterial Material to ignore set to null on initial call * @return Worth of the item as a double */ - private static double getWorth(ItemStack item, Material blockedMaterial) { + private static Price getWorth(ItemStack item, Material blockedMaterial) { + Price price = null; + if (item == null) - return -1; + return (null); if (WorthConfig.prices.containsKey(item.getType())) - return WorthConfig.prices.getDouble(item.getType()); - double price = -1; + return (WorthConfig.prices.get(item.getType())); + List recipes = Bukkit.getRecipesFor(item); for (Recipe recipe : recipes) { - double possiblePrice; + Price possiblePrice; + if (recipe instanceof ShapedRecipe shapedRecipe) { List values = shapedRecipe.getIngredientMap().values().stream().toList(); if (!values.isEmpty() && blockedMaterial != null && values.stream() .anyMatch(itemStack -> itemStack != null && itemStack.getType().equals(blockedMaterial))) continue; possiblePrice = getWorth(values, item.getType()); - if (price < possiblePrice) + if (possiblePrice == null) + continue; + if (price == null || price.price() > possiblePrice.price()) price = possiblePrice; } else if (recipe instanceof ShapelessRecipe shapelessRecipe) { if (shapelessRecipe.getIngredientList().stream() .anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial))) continue; possiblePrice = getWorth(shapelessRecipe.getIngredientList(), item.getType()); - if (price < possiblePrice) + if (possiblePrice == null) + continue; + if (price == null || price.price() > possiblePrice.price()) price = possiblePrice; } else if (recipe instanceof CampfireRecipe campfireRecipe) { possiblePrice = getWorth(campfireRecipe.getInput(), item.getType()); - if (price < possiblePrice) + if (possiblePrice == null) + continue; + if (price == null || price.price() > possiblePrice.price()) price = possiblePrice; } else if (recipe instanceof StonecuttingRecipe stonecuttingRecipe) { possiblePrice = getWorth(stonecuttingRecipe.getInput(), item.getType()); - if (price < possiblePrice) + if (possiblePrice == null) + continue; + if (price == null || price.price() > possiblePrice.price()) price = possiblePrice; } else if (recipe instanceof CookingRecipe cookingRecipe) { if ((recipe instanceof FurnaceRecipe || recipe instanceof BlastingRecipe ) && @@ -95,7 +102,9 @@ public class Utilities { !cookingRecipe.getInput().getType().equals(Material.CLAY_BALL)) //Needs exception for clay ball idk a better way to do it... continue; possiblePrice = getWorth(cookingRecipe.getInput(), item.getType()); - if (price < possiblePrice) + if (possiblePrice == null) + continue; + if (price == null || price.price() > possiblePrice.price()) price = possiblePrice; } } @@ -109,17 +118,20 @@ public class Utilities { * @param blockedMaterial Material to ignore set to null on initial call * @return Worth of ItemStack as a double */ - private static double getWorth(List items, Material blockedMaterial) { - double price = 0; + private static Price getWorth(List items, Material blockedMaterial) { + Price price = null; for (ItemStack item : items) { if (item == null) continue; - double tmp = getWorth(new ItemStack(item.getType()), blockedMaterial); - if (tmp == -1) - return -1; - WorthConfig.prices.put(item.getType(), Utilities.round(tmp, 2)); - price += tmp; + Price tmp = getWorth(new ItemStack(item.getType()), blockedMaterial); + if (tmp == null || tmp.price() == -1) + return null; + WorthConfig.prices.put(item.getType(), tmp); + if (price == null) + price = tmp; + else + price = Price.addPrice(price, tmp); } - return price; + return (price); } }