Made changes and didn't push inbetween so i forgot it all
This commit is contained in:
parent
18a75f86c0
commit
4fbc195607
|
|
@ -10,10 +10,13 @@ import org.bukkit.event.inventory.InventoryType;
|
||||||
import org.bukkit.event.inventory.TradeSelectEvent;
|
import org.bukkit.event.inventory.TradeSelectEvent;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.MerchantRecipe;
|
|
||||||
|
|
||||||
public class GUIListener implements Listener {
|
public class GUIListener implements Listener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles clicking inside a gui
|
||||||
|
* @param event gui click event
|
||||||
|
*/
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onClick(InventoryClickEvent event){
|
public void onClick(InventoryClickEvent event){
|
||||||
if (!(event.getWhoClicked() instanceof Player player)){
|
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) {
|
private void onResultSlotClick(InventoryClickEvent event, GUI gui) {
|
||||||
ItemStack currentItem = event.getCurrentItem();
|
ItemStack currentItem = event.getCurrentItem();
|
||||||
if (currentItem == null)
|
if (currentItem == null)
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,17 @@
|
||||||
package com.alttd.util;
|
package com.alttd.util;
|
||||||
|
|
||||||
import com.alttd.config.WorthConfig;
|
import com.alttd.config.WorthConfig;
|
||||||
|
import jdk.swing.interop.SwingInterOpUtils;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.inventory.*;
|
import org.bukkit.inventory.*;
|
||||||
|
import org.bukkit.material.MaterialData;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class Utilities {
|
public class Utilities {
|
||||||
/**
|
/**
|
||||||
|
|
@ -48,33 +53,50 @@ public class Utilities {
|
||||||
/**
|
/**
|
||||||
* Get the worth of the material an ItemStack consists of
|
* 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
|
* @param blockedMaterial Material to ignore set to null on initial call
|
||||||
* @return Worth of the item as a double
|
* @return Worth of the item as a double
|
||||||
*/
|
*/
|
||||||
private static double getWorth(ItemStack item, Material blockedMaterial) {
|
private static double getWorth(ItemStack item, Material blockedMaterial) {
|
||||||
|
if (item == null)
|
||||||
|
return -1;
|
||||||
if (WorthConfig.prices.containsKey(item.getType()))
|
if (WorthConfig.prices.containsKey(item.getType()))
|
||||||
return WorthConfig.prices.getDouble(item.getType());
|
return WorthConfig.prices.getDouble(item.getType());
|
||||||
double price = -1;
|
double price = -1;
|
||||||
List<Recipe> recipes = Bukkit.getRecipesFor(item);
|
List<Recipe> recipes = Bukkit.getRecipesFor(item);
|
||||||
|
System.out.println(item.getType());
|
||||||
for (Recipe recipe : recipes) {
|
for (Recipe recipe : recipes) {
|
||||||
double possiblePrice;
|
double possiblePrice;
|
||||||
if (recipe instanceof ShapedRecipe shapedRecipe) {
|
if (recipe instanceof ShapedRecipe shapedRecipe) {
|
||||||
Collection<ItemStack> values = shapedRecipe.getIngredientMap().values();
|
List<ItemStack> values = shapedRecipe.getIngredientMap().values().stream().toList();
|
||||||
if (values.stream().anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial)))
|
if (!values.isEmpty() && blockedMaterial != null && values.stream()
|
||||||
|
.anyMatch(itemStack -> itemStack != null && itemStack.getType().equals(blockedMaterial)))
|
||||||
continue;
|
continue;
|
||||||
possiblePrice = getWorth(values.stream().toList(), item.getType());
|
possiblePrice = getWorth(values, item.getType());
|
||||||
if (price == -1 || price > possiblePrice)
|
if (price < possiblePrice)
|
||||||
price = possiblePrice;
|
price = possiblePrice;
|
||||||
} else if (recipe instanceof ShapelessRecipe shapelessRecipe) {
|
} 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;
|
continue;
|
||||||
possiblePrice = getWorth(shapelessRecipe.getIngredientList(), item.getType());
|
possiblePrice = getWorth(shapelessRecipe.getIngredientList(), item.getType());
|
||||||
if (price == -1 || price > possiblePrice)
|
if (price < possiblePrice)
|
||||||
price = possiblePrice;
|
price = possiblePrice;
|
||||||
} else if (recipe instanceof FurnaceRecipe furnaceRecipe) {
|
} else if (recipe instanceof CampfireRecipe campfireRecipe) {
|
||||||
possiblePrice = getWorth(furnaceRecipe.getInput(), item.getType());
|
possiblePrice = getWorth(campfireRecipe.getInput(), item.getType());
|
||||||
if (price == -1 || price > possiblePrice)
|
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;
|
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)
|
* 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
|
* @param blockedMaterial Material to ignore set to null on initial call
|
||||||
* @return Worth of ItemStack as a double
|
* @return Worth of ItemStack as a double
|
||||||
*/
|
*/
|
||||||
private static double getWorth(List<ItemStack> items, Material blockedMaterial) {
|
private static double getWorth(List<ItemStack> items, Material blockedMaterial) {
|
||||||
double price = 0;
|
double price = 0;
|
||||||
for (ItemStack item : items) {
|
for (ItemStack item : items) {
|
||||||
|
if (item == null)
|
||||||
|
continue;
|
||||||
double tmp = getWorth(item, blockedMaterial);
|
double tmp = getWorth(item, blockedMaterial);
|
||||||
if (tmp == -1)
|
if (tmp == -1)
|
||||||
return -1;
|
return -1;
|
||||||
|
WorthConfig.prices.put(item.getType(), Utilities.round(tmp, 2));
|
||||||
price += tmp;
|
price += tmp;
|
||||||
}
|
}
|
||||||
return price;
|
return price;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user