Created a point system to be used for prices

This commit is contained in:
Teriuihi 2021-10-23 14:42:55 +02:00
parent d72930cb67
commit 4bb26035d6
3 changed files with 74 additions and 38 deletions

View File

@ -1,13 +1,16 @@
package com.alttd.config; package com.alttd.config;
import com.alttd.VillagerUI; import com.alttd.VillagerUI;
import com.alttd.objects.Price;
import com.alttd.util.Utilities; import com.alttd.util.Utilities;
import it.unimi.dsi.fastutil.objects.Object2DoubleMap; import it.unimi.dsi.fastutil.objects.Object2DoubleMap;
import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.Set; import java.util.Set;
public class WorthConfig extends AbstractConfig { public class WorthConfig extends AbstractConfig {
@ -27,19 +30,23 @@ public class WorthConfig extends AbstractConfig {
config.readConfig(WorthConfig.class, null); config.readConfig(WorthConfig.class, null);
} }
public static Object2DoubleMap<Material> prices = new Object2DoubleOpenHashMap<>(); public static Object2ObjectOpenHashMap<Material, Price> prices = new Object2ObjectOpenHashMap<>();
private static void loadWorth() { private static void loadWorth() {
prices.clear(); prices.clear();
ConfigurationSection worth = config.getConfigurationSection("worth"); ConfigurationSection worth = config.getConfigurationSection("worth");
Set<String> keys = worth.getKeys(false); Set<String> points = worth.getKeys(false);
for (String key : keys) { for (String point : points) {
Material material = Material.getMaterial(key); ConfigurationSection pointSection = worth.getConfigurationSection(point);
if (material == null) { Set<String> materials = worth.getConfigurationSection(point).getKeys(false);
VillagerUI.getInstance().getLogger().warning("Invalid key in worth.yml -> " + key); for (String key : materials) {
continue; 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));
} }
} }
} }

View File

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

View File

@ -1,17 +1,12 @@
package com.alttd.util; package com.alttd.util;
import com.alttd.config.WorthConfig; import com.alttd.config.WorthConfig;
import jdk.swing.interop.SwingInterOpUtils; import com.alttd.objects.Price;
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.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class Utilities { public class Utilities {
/** /**
@ -41,13 +36,14 @@ public class Utilities {
* @param item to calculate price for * @param item to calculate price for
* @return price or int < 0 for error * @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())) if (WorthConfig.prices.containsKey(item.getType()))
return Utilities.round(WorthConfig.prices.getDouble(item.getType()) * item.getAmount(), 2); return (WorthConfig.prices.get(item.getType()));
Price price = getWorth(item, null);
WorthConfig.prices.put(item.getType(), Utilities.round(getWorth(item, null), 2)); if (price == null)
return (null);
return Utilities.round(WorthConfig.prices.getDouble(item.getType()) * item.getAmount(), 2); 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 * @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 Price getWorth(ItemStack item, Material blockedMaterial) {
Price price = null;
if (item == null) if (item == null)
return -1; return (null);
if (WorthConfig.prices.containsKey(item.getType())) if (WorthConfig.prices.containsKey(item.getType()))
return WorthConfig.prices.getDouble(item.getType()); return (WorthConfig.prices.get(item.getType()));
double price = -1;
List<Recipe> recipes = Bukkit.getRecipesFor(item); List<Recipe> recipes = Bukkit.getRecipesFor(item);
for (Recipe recipe : recipes) { for (Recipe recipe : recipes) {
double possiblePrice; Price possiblePrice;
if (recipe instanceof ShapedRecipe shapedRecipe) { if (recipe instanceof ShapedRecipe shapedRecipe) {
List<ItemStack> values = shapedRecipe.getIngredientMap().values().stream().toList(); List<ItemStack> values = shapedRecipe.getIngredientMap().values().stream().toList();
if (!values.isEmpty() && blockedMaterial != null && values.stream() if (!values.isEmpty() && blockedMaterial != null && values.stream()
.anyMatch(itemStack -> itemStack != null && itemStack.getType().equals(blockedMaterial))) .anyMatch(itemStack -> itemStack != null && itemStack.getType().equals(blockedMaterial)))
continue; continue;
possiblePrice = getWorth(values, item.getType()); possiblePrice = getWorth(values, item.getType());
if (price < possiblePrice) if (possiblePrice == null)
continue;
if (price == null || price.price() > possiblePrice.price())
price = possiblePrice; price = possiblePrice;
} else if (recipe instanceof ShapelessRecipe shapelessRecipe) { } else if (recipe instanceof ShapelessRecipe shapelessRecipe) {
if (shapelessRecipe.getIngredientList().stream() if (shapelessRecipe.getIngredientList().stream()
.anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial))) .anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial)))
continue; continue;
possiblePrice = getWorth(shapelessRecipe.getIngredientList(), item.getType()); possiblePrice = getWorth(shapelessRecipe.getIngredientList(), item.getType());
if (price < possiblePrice) if (possiblePrice == null)
continue;
if (price == null || price.price() > possiblePrice.price())
price = possiblePrice; price = possiblePrice;
} else if (recipe instanceof CampfireRecipe campfireRecipe) { } else if (recipe instanceof CampfireRecipe campfireRecipe) {
possiblePrice = getWorth(campfireRecipe.getInput(), item.getType()); possiblePrice = getWorth(campfireRecipe.getInput(), item.getType());
if (price < possiblePrice) if (possiblePrice == null)
continue;
if (price == null || price.price() > possiblePrice.price())
price = possiblePrice; price = possiblePrice;
} else if (recipe instanceof StonecuttingRecipe stonecuttingRecipe) { } else if (recipe instanceof StonecuttingRecipe stonecuttingRecipe) {
possiblePrice = getWorth(stonecuttingRecipe.getInput(), item.getType()); possiblePrice = getWorth(stonecuttingRecipe.getInput(), item.getType());
if (price < possiblePrice) if (possiblePrice == null)
continue;
if (price == null || price.price() > possiblePrice.price())
price = possiblePrice; price = possiblePrice;
} else if (recipe instanceof CookingRecipe cookingRecipe) { } else if (recipe instanceof CookingRecipe cookingRecipe) {
if ((recipe instanceof FurnaceRecipe || recipe instanceof BlastingRecipe ) && 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... !cookingRecipe.getInput().getType().equals(Material.CLAY_BALL)) //Needs exception for clay ball idk a better way to do it...
continue; continue;
possiblePrice = getWorth(cookingRecipe.getInput(), item.getType()); possiblePrice = getWorth(cookingRecipe.getInput(), item.getType());
if (price < possiblePrice) if (possiblePrice == null)
continue;
if (price == null || price.price() > possiblePrice.price())
price = possiblePrice; price = possiblePrice;
} }
} }
@ -109,17 +118,20 @@ public class Utilities {
* @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 Price getWorth(List<ItemStack> items, Material blockedMaterial) {
double price = 0; Price price = null;
for (ItemStack item : items) { for (ItemStack item : items) {
if (item == null) if (item == null)
continue; continue;
double tmp = getWorth(new ItemStack(item.getType()), blockedMaterial); Price tmp = getWorth(new ItemStack(item.getType()), blockedMaterial);
if (tmp == -1) if (tmp == null || tmp.price() == -1)
return -1; return null;
WorthConfig.prices.put(item.getType(), Utilities.round(tmp, 2)); WorthConfig.prices.put(item.getType(), tmp);
price += tmp; if (price == null)
price = tmp;
else
price = Price.addPrice(price, tmp);
} }
return price; return (price);
} }
} }