Particles/src/main/java/com/alttd/util/Utilities.java

102 lines
3.7 KiB
Java
Raw Normal View History

2021-09-24 23:56:42 +00:00
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;
2021-09-24 23:56:42 +00:00
public class Utilities {
/**
* Rounds num down to precision (rounds up if last cut off decimal is bigger than 4)
*
* @param num value to be rounded
* @param precision length to round to
* @return num rounded
*/
public static double round(double num, int precision) {
double scale = Math.pow(10, precision);
double total = (double) (Math.round(num * scale)) / scale;
scale = (int) Math.pow(10, precision + 1);
long tmp = (Math.round(num * scale));
2021-09-25 18:22:23 +00:00
tmp %= 10;
2021-09-24 23:56:42 +00:00
if (tmp > 4)
total += 0.01;
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
*/
2021-09-25 01:23:45 +00:00
public static double getWorth(ItemStack item) {
if (WorthConfig.prices.containsKey(item.getType()))
return Utilities.round(WorthConfig.prices.getDouble(item.getType()) * item.getAmount(), 2);
2021-09-25 01:23:45 +00:00
WorthConfig.prices.put(item.getType(), Utilities.round(getWorth(item, null), 2));
return WorthConfig.prices.getDouble(item.getType()) * item.getAmount();
}
2021-09-25 01:23:45 +00:00
/**
* Get the worth of the material an ItemStack consists 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 (WorthConfig.prices.containsKey(item.getType()))
return WorthConfig.prices.getDouble(item.getType());
double price = -1;
List<Recipe> recipes = Bukkit.getRecipesFor(item);
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)))
continue;
2021-09-25 01:23:45 +00:00
possiblePrice = getWorth(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;
2021-09-25 01:23:45 +00:00
possiblePrice = getWorth(shapelessRecipe.getIngredientList(), item.getType());
if (price == -1 || price > possiblePrice)
price = possiblePrice;
} else if (recipe instanceof FurnaceRecipe furnaceRecipe) {
2021-09-25 01:23:45 +00:00
possiblePrice = getWorth(furnaceRecipe.getInput(), item.getType());
if (price == -1 || price > possiblePrice)
price = possiblePrice;
}
}
return price;
}
2021-09-25 01:23:45 +00:00
/**
* 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 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) {
2021-09-25 01:23:45 +00:00
double tmp = getWorth(item, blockedMaterial);
if (tmp == -1)
return -1;
price += tmp;
}
return price;
}
2021-09-24 23:56:42 +00:00
}