Made changes and didn't push inbetween so i forgot it all

This commit is contained in:
Teriuihi 2021-10-21 17:13:21 +02:00
parent 18a75f86c0
commit 4fbc195607
2 changed files with 45 additions and 12 deletions

View File

@ -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)

View File

@ -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<Recipe> recipes = Bukkit.getRecipesFor(item);
System.out.println(item.getType());
for (Recipe recipe : recipes) {
double possiblePrice;
if (recipe instanceof ShapedRecipe shapedRecipe) {
Collection<ItemStack> values = shapedRecipe.getIngredientMap().values();
if (values.stream().anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial)))
List<ItemStack> 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<ItemStack> 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;