From 4fbc195607edb3d866d77199192aadea54ae221b Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Thu, 21 Oct 2021 17:13:21 +0200 Subject: [PATCH] Made changes and didn't push inbetween so i forgot it all --- src/main/java/com/alttd/GUI/GUIListener.java | 10 ++++- src/main/java/com/alttd/util/Utilities.java | 47 +++++++++++++++----- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/alttd/GUI/GUIListener.java b/src/main/java/com/alttd/GUI/GUIListener.java index c8c173c..6a81363 100644 --- a/src/main/java/com/alttd/GUI/GUIListener.java +++ b/src/main/java/com/alttd/GUI/GUIListener.java @@ -10,10 +10,13 @@ import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.TradeSelectEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.MerchantRecipe; public class GUIListener implements Listener { + /** + * Handles clicking inside a gui + * @param event gui click event + */ @EventHandler public void onClick(InventoryClickEvent event){ if (!(event.getWhoClicked() instanceof Player player)){ @@ -41,6 +44,11 @@ public class GUIListener implements Listener { } } + /** + * Handles clicking on an item in a gui result slot + * @param event click event + * @param gui gui that was clicked in + */ private void onResultSlotClick(InventoryClickEvent event, GUI gui) { ItemStack currentItem = event.getCurrentItem(); if (currentItem == null) diff --git a/src/main/java/com/alttd/util/Utilities.java b/src/main/java/com/alttd/util/Utilities.java index d2dd3b7..6965fa5 100644 --- a/src/main/java/com/alttd/util/Utilities.java +++ b/src/main/java/com/alttd/util/Utilities.java @@ -1,12 +1,17 @@ package com.alttd.util; import com.alttd.config.WorthConfig; +import jdk.swing.interop.SwingInterOpUtils; 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 { /** @@ -48,33 +53,50 @@ public class Utilities { /** * Get the worth of the material an ItemStack consists of * - * @param item to get the worth of + * @param item to get the worth of * @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) { + if (item == null) + return -1; if (WorthConfig.prices.containsKey(item.getType())) return WorthConfig.prices.getDouble(item.getType()); double price = -1; List recipes = Bukkit.getRecipesFor(item); + System.out.println(item.getType()); 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))) + 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.stream().toList(), item.getType()); - if (price == -1 || price > possiblePrice) + possiblePrice = getWorth(values, item.getType()); + if (price < possiblePrice) price = possiblePrice; } else if (recipe instanceof ShapelessRecipe shapelessRecipe) { - if (shapelessRecipe.getIngredientList().stream().anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial))) + if (shapelessRecipe.getIngredientList().stream() + .anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial))) continue; possiblePrice = getWorth(shapelessRecipe.getIngredientList(), item.getType()); - if (price == -1 || price > possiblePrice) + if (price < possiblePrice) price = possiblePrice; - } else if (recipe instanceof FurnaceRecipe furnaceRecipe) { - possiblePrice = getWorth(furnaceRecipe.getInput(), item.getType()); - if (price == -1 || price > possiblePrice) + } else if (recipe instanceof CampfireRecipe campfireRecipe) { + possiblePrice = getWorth(campfireRecipe.getInput(), item.getType()); + if (price < possiblePrice) + price = possiblePrice; + } else if (recipe instanceof StonecuttingRecipe stonecuttingRecipe) { + possiblePrice = getWorth(stonecuttingRecipe.getInput(), item.getType()); + if (price < possiblePrice) + price = possiblePrice; + } else if (recipe instanceof CookingRecipe cookingRecipe) { + if ((recipe instanceof FurnaceRecipe || recipe instanceof BlastingRecipe ) && + !cookingRecipe.getInput().getType().isBlock() && + !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) price = possiblePrice; } } @@ -84,16 +106,19 @@ public class Utilities { /** * Get the total worth of a list of ItemStack's (amount of items in ItemStack is ignored) * - * @param items Items to get the worth of + * @param items Items to get the worth of * @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; for (ItemStack item : items) { + if (item == null) + continue; double tmp = getWorth(item, blockedMaterial); if (tmp == -1) return -1; + WorthConfig.prices.put(item.getType(), Utilities.round(tmp, 2)); price += tmp; } return price;