From d45c2f9ace6e54b8ca0409f1647e5fc333dedb51 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sun, 2 Jan 2022 06:15:07 +0100 Subject: [PATCH] Fixed stuff and did stuff and i forgot sorry to anyone reading this --- src/main/java/com/alttd/GUI/GUIListener.java | 12 +++---- .../java/com/alttd/GUI/windows/BuyGUI.java | 2 +- .../java/com/alttd/GUI/windows/OpenGUI.java | 1 - .../java/com/alttd/GUI/windows/SellGUI.java | 2 +- .../com/alttd/commands/CommandManager.java | 6 ++-- .../commands/subcommands/CommandHelp.java | 5 ++- src/main/java/com/alttd/config/Config.java | 1 - .../java/com/alttd/config/WorthConfig.java | 16 ++++++--- .../java/com/alttd/events/LoginEvent.java | 6 ++-- src/main/java/com/alttd/objects/EconUser.java | 24 ++++++++----- src/main/java/com/alttd/objects/Price.java | 4 +-- .../java/com/alttd/objects/VillagerType.java | 8 +---- src/main/java/com/alttd/util/Utilities.java | 35 ++++++++++--------- 13 files changed, 64 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/alttd/GUI/GUIListener.java b/src/main/java/com/alttd/GUI/GUIListener.java index 75f3499..cf5d1b3 100644 --- a/src/main/java/com/alttd/GUI/GUIListener.java +++ b/src/main/java/com/alttd/GUI/GUIListener.java @@ -25,12 +25,12 @@ public class GUIListener implements Listener { GUI gui = GUI.GUIByUUID.get(player.getUniqueId()); if (gui == null || gui.getInventory() == null) { - if (event.getSlotType().equals(InventoryType.SlotType.CRAFTING) && event.getRawSlot() < 2) - event.setCancelled(true); - else if (event.getRawSlot() == 2 && event.getSlotType().equals(InventoryType.SlotType.RESULT)) { - event.setCancelled(true); - onResultSlotClick(event, gui); - } +// if (event.getSlotType().equals(InventoryType.SlotType.CRAFTING) && event.getRawSlot() < 2) +// event.setCancelled(true); +// else if (event.getRawSlot() == 2 && event.getSlotType().equals(InventoryType.SlotType.RESULT)) { +// event.setCancelled(true); +// onResultSlotClick(event, gui); +// } return; } if (!gui.getInventory().equals(event.getInventory())) { diff --git a/src/main/java/com/alttd/GUI/windows/BuyGUI.java b/src/main/java/com/alttd/GUI/windows/BuyGUI.java index 46df5d5..e307725 100644 --- a/src/main/java/com/alttd/GUI/windows/BuyGUI.java +++ b/src/main/java/com/alttd/GUI/windows/BuyGUI.java @@ -35,7 +35,7 @@ public class BuyGUI extends GUIMerchant { 0))) )), villagerType); for (ItemStack itemStack : villagerType.getBuying()) { - Price price = Utilities.getPrice(itemStack); + Price price = Utilities.getPrice(itemStack, WorthConfig.buy); if (price == null) continue; addItem(itemStack, diff --git a/src/main/java/com/alttd/GUI/windows/OpenGUI.java b/src/main/java/com/alttd/GUI/windows/OpenGUI.java index 13f8bc3..ec0988b 100644 --- a/src/main/java/com/alttd/GUI/windows/OpenGUI.java +++ b/src/main/java/com/alttd/GUI/windows/OpenGUI.java @@ -14,7 +14,6 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.scheduler.BukkitRunnable; -import java.util.List; import java.util.Objects; public class OpenGUI extends GUIInventory { diff --git a/src/main/java/com/alttd/GUI/windows/SellGUI.java b/src/main/java/com/alttd/GUI/windows/SellGUI.java index 8ff05c2..5e4d945 100644 --- a/src/main/java/com/alttd/GUI/windows/SellGUI.java +++ b/src/main/java/com/alttd/GUI/windows/SellGUI.java @@ -37,7 +37,7 @@ public class SellGUI extends GUIMerchant { .requireNonNullElse(econUser.getPointsMap().get(villagerType.getName()) , 0))))), villagerType); for (ItemStack itemStack : villagerType.getSelling()) { - Price price = Utilities.getPrice(itemStack); + Price price = Utilities.getPrice(itemStack, WorthConfig.sell); if (price == null) continue; addItem(itemStack, diff --git a/src/main/java/com/alttd/commands/CommandManager.java b/src/main/java/com/alttd/commands/CommandManager.java index 8e8744c..d9c5156 100644 --- a/src/main/java/com/alttd/commands/CommandManager.java +++ b/src/main/java/com/alttd/commands/CommandManager.java @@ -46,12 +46,10 @@ public class CommandManager implements CommandExecutor, TabExecutor { @Override public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) { if (args.length == 0) { - commandSender.sendMiniMessage(Config.HELP_MESSAGE_WRAPPER, List.of( - Template.template("commands", subCommands.stream() + commandSender.sendMiniMessage(Config.HELP_MESSAGE_WRAPPER.replaceAll("", subCommands.stream() .filter(subCommand -> commandSender.hasPermission(subCommand.getPermission())) .map(SubCommand::getHelpMessage) - .collect(Collectors.joining("\n"))) - )); + .collect(Collectors.joining("\n"))), null); return true; } diff --git a/src/main/java/com/alttd/commands/subcommands/CommandHelp.java b/src/main/java/com/alttd/commands/subcommands/CommandHelp.java index 5a36346..6d61427 100644 --- a/src/main/java/com/alttd/commands/subcommands/CommandHelp.java +++ b/src/main/java/com/alttd/commands/subcommands/CommandHelp.java @@ -22,12 +22,11 @@ public class CommandHelp extends SubCommand { @Override public boolean onCommand(CommandSender commandSender, String[] args) { - commandSender.sendMiniMessage(Config.HELP_MESSAGE_WRAPPER, - List.of(Template.template("commands", commandManager + commandSender.sendMiniMessage(Config.HELP_MESSAGE_WRAPPER.replaceAll("", commandManager .getSubCommands().stream() .filter(subCommand -> commandSender.hasPermission(subCommand.getPermission())) .map(SubCommand::getHelpMessage) - .collect(Collectors.joining("\n"))))); + .collect(Collectors.joining("\n"))), null); return true; } diff --git a/src/main/java/com/alttd/config/Config.java b/src/main/java/com/alttd/config/Config.java index c58694a..32b2309 100644 --- a/src/main/java/com/alttd/config/Config.java +++ b/src/main/java/com/alttd/config/Config.java @@ -128,7 +128,6 @@ public final class Config extends AbstractConfig { villagerType.getString("name"), loadProducts(villagerType.getConfigurationSection("buying")), loadProducts(villagerType.getConfigurationSection("selling")), - villagerType.getDouble("price-modifier"), villagerType.getString("profession")) ); }); diff --git a/src/main/java/com/alttd/config/WorthConfig.java b/src/main/java/com/alttd/config/WorthConfig.java index 0bcc48d..84b74f3 100644 --- a/src/main/java/com/alttd/config/WorthConfig.java +++ b/src/main/java/com/alttd/config/WorthConfig.java @@ -28,12 +28,18 @@ public class WorthConfig extends AbstractConfig { config.readConfig(WorthConfig.class, null); } - public static Object2ObjectOpenHashMap prices = new Object2ObjectOpenHashMap<>(); + public static Object2ObjectOpenHashMap buy = new Object2ObjectOpenHashMap<>(); + public static Object2ObjectOpenHashMap sell = new Object2ObjectOpenHashMap<>(); private static void loadWorth() { //TODO test after removing points - prices.clear(); - ConfigurationSection worth = config.getConfigurationSection("worth"); + loadWorth("buy", buy); + loadWorth("sell", sell); + } + + private static void loadWorth(String path, Object2ObjectOpenHashMap map) { + map.clear(); + ConfigurationSection worth = config.getConfigurationSection(path); if (worth == null) { - Logger.severe("No worth in worth.yml! Stopping VillagerUI."); + Logger.severe("No ? in worth.yml! Stopping VillagerUI.", path); VillagerUI.getInstance().getServer().getPluginManager().disablePlugin(VillagerUI.getInstance()); return; } @@ -43,7 +49,7 @@ public class WorthConfig extends AbstractConfig { Logger.severe("Null key in worth.yml?"); continue; } - prices.put(Material.getMaterial(key), new Price(Utilities.round(worth.getDouble(key), 2))); + map.put(Material.getMaterial(key), new Price(Utilities.round(worth.getDouble(key), 2))); } } diff --git a/src/main/java/com/alttd/events/LoginEvent.java b/src/main/java/com/alttd/events/LoginEvent.java index a5c5b86..4db8204 100644 --- a/src/main/java/com/alttd/events/LoginEvent.java +++ b/src/main/java/com/alttd/events/LoginEvent.java @@ -5,6 +5,7 @@ import com.alttd.config.Config; import com.alttd.database.Queries; import com.alttd.objects.EconUser; import com.alttd.util.Logger; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; @@ -15,17 +16,18 @@ import java.util.UUID; public class LoginEvent implements Listener { @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { + final Player player = event.getPlayer(); new BukkitRunnable() { @Override public void run() { - UUID uuid = event.getPlayer().getUniqueId(); + UUID uuid = player.getUniqueId(); EconUser user = EconUser.getUser(uuid); int minutes = Queries.getMinutesSinceUpdated(uuid); user.removePoints(minutes * 2); if (Config.DEBUG) Logger.info("Loaded EconUser for % and removed % points", - event.getPlayer().getName(), String.valueOf(minutes * 2)); + player.getName(), String.valueOf(minutes * 2)); } }.runTask(VillagerUI.getInstance()); } diff --git a/src/main/java/com/alttd/objects/EconUser.java b/src/main/java/com/alttd/objects/EconUser.java index c25a770..703e5f5 100644 --- a/src/main/java/com/alttd/objects/EconUser.java +++ b/src/main/java/com/alttd/objects/EconUser.java @@ -57,20 +57,27 @@ public class EconUser { { if (points == 0) return; - if (points > 0) + if (points > 0) { if (points < remove) points = 0; else points -= remove; - else - if (-points < remove) - points = 0; - else - points += remove; + } else { + if (-points < remove) + points = 0; + else + points += remove; + } pointsMap.put(villagerType, points); if (Config.DEBUG) Logger.info("Removed % points from villagerType: % for %", - String.valueOf(points), villagerType, uuid.toString()); + String.valueOf(remove), villagerType, uuid.toString()); + } + + private void setPoints(String villagerType, int points) { + if (pointsMap.get(villagerType) < 0) + points *= -1; + pointsMap.put(villagerType, points); } public void removePoints(int remove) { @@ -84,7 +91,8 @@ public class EconUser { int remove = points; if (remove < 0) remove *= -1; - removePoints(villagerType, points, (int) (0.9 * remove) - 10); + int i = (int) (0.9 * remove) - 10; + setPoints(villagerType, i < 10 && i > -10 ? 0 : i); }); } diff --git a/src/main/java/com/alttd/objects/Price.java b/src/main/java/com/alttd/objects/Price.java index f8487e0..56b49b8 100644 --- a/src/main/java/com/alttd/objects/Price.java +++ b/src/main/java/com/alttd/objects/Price.java @@ -7,8 +7,8 @@ public final class Price { private final int points; private static final double[] xMult = {Integer.MIN_VALUE, -4000, -2000, -500, 500, 2000, 4000, Integer.MAX_VALUE}; - private static final double[] yMultBuy = {2.5, 1.75, 1.25, 1, 1.5, 2.5, 5}; - private static final double[] yMultSell = {5, 2.5, 1.5, 1, 1.25, 1.75, 2.5}; + private static final double[] yMultBuy = {0.4, 0.65, 0.85, 1, 1.5, 2.5, 5}; + private static final double[] yMultSell = {5, 2.5, 1.5, 1, 0.85, 0.65, 0.4}; public Price(double price) { this.price = price; diff --git a/src/main/java/com/alttd/objects/VillagerType.java b/src/main/java/com/alttd/objects/VillagerType.java index b606bc3..69923cb 100644 --- a/src/main/java/com/alttd/objects/VillagerType.java +++ b/src/main/java/com/alttd/objects/VillagerType.java @@ -28,15 +28,13 @@ public class VillagerType { private final String displayName; private final Set buying; private final Set selling; - private final double priceModifier; private final Villager.Profession profession; - public VillagerType(String name, String displayName, Set buying, Set selling, double priceModifier, String profession) { + public VillagerType(String name, String displayName, Set buying, Set selling, String profession) { this.name = name; this.displayName = displayName; this.buying = buying; this.selling = selling; - this.priceModifier = priceModifier; this.profession = Villager.Profession.valueOf(profession.toUpperCase()); } @@ -56,10 +54,6 @@ public class VillagerType { return selling; } - public double getPriceModifier() { - return priceModifier; - } - public Villager.Profession getProfession() { return profession; } diff --git a/src/main/java/com/alttd/util/Utilities.java b/src/main/java/com/alttd/util/Utilities.java index abfbacf..b883ef2 100644 --- a/src/main/java/com/alttd/util/Utilities.java +++ b/src/main/java/com/alttd/util/Utilities.java @@ -2,6 +2,7 @@ package com.alttd.util; import com.alttd.config.WorthConfig; import com.alttd.objects.Price; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.inventory.*; @@ -36,14 +37,14 @@ public class Utilities { * @param item to calculate price for * @return price or int < 0 for error */ - public static Price getPrice(ItemStack item) { - if (WorthConfig.prices.containsKey(item.getType())) - return (WorthConfig.prices.get(item.getType())); - Price price = getWorth(item, null); + public static Price getPrice(ItemStack item, Object2ObjectOpenHashMap map) { + if (map.containsKey(item.getType())) + return (map.get(item.getType())); + Price price = getWorth(item, null, map); if (price == null) return (null); - WorthConfig.prices.put(item.getType(), price); - return (WorthConfig.prices.get(item.getType())); + map.put(item.getType(), price); + return (map.get(item.getType())); } /** @@ -53,13 +54,13 @@ public class Utilities { * @param blockedMaterial Material to ignore set to null on initial call * @return Worth of the item as a double */ - private static Price getWorth(ItemStack item, Material blockedMaterial) { + private static Price getWorth(ItemStack item, Material blockedMaterial, Object2ObjectOpenHashMap map) { Price price = null; if (item == null) return (null); - if (WorthConfig.prices.containsKey(item.getType())) - return (WorthConfig.prices.get(item.getType())); + if (map.containsKey(item.getType())) + return (map.get(item.getType())); List recipes = Bukkit.getRecipesFor(item); for (Recipe recipe : recipes) { @@ -70,7 +71,7 @@ public class Utilities { if (!values.isEmpty() && blockedMaterial != null && values.stream() .anyMatch(itemStack -> itemStack != null && itemStack.getType().equals(blockedMaterial))) continue; - possiblePrice = getWorth(values, item.getType()); + possiblePrice = getWorth(values, item.getType(), map); if (possiblePrice == null) continue; if (price == null || price.getPrice(1) > possiblePrice.getPrice(1)) @@ -79,19 +80,19 @@ public class Utilities { if (shapelessRecipe.getIngredientList().stream() .anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial))) continue; - possiblePrice = getWorth(shapelessRecipe.getIngredientList(), item.getType()); + possiblePrice = getWorth(shapelessRecipe.getIngredientList(), item.getType(), map); if (possiblePrice == null) continue; if (price == null || price.getPrice(1) > possiblePrice.getPrice(1)) price = possiblePrice; } else if (recipe instanceof CampfireRecipe campfireRecipe) { - possiblePrice = getWorth(campfireRecipe.getInput(), item.getType()); + possiblePrice = getWorth(campfireRecipe.getInput(), item.getType(), map); if (possiblePrice == null) continue; if (price == null || price.getPrice(1) > possiblePrice.getPrice(1)) price = possiblePrice; } else if (recipe instanceof StonecuttingRecipe stonecuttingRecipe) { - possiblePrice = getWorth(stonecuttingRecipe.getInput(), item.getType()); + possiblePrice = getWorth(stonecuttingRecipe.getInput(), item.getType(), map); if (possiblePrice == null) continue; if (price == null || price.getPrice(1) > possiblePrice.getPrice(1)) @@ -101,7 +102,7 @@ public class Utilities { !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()); + possiblePrice = getWorth(cookingRecipe.getInput(), item.getType(), map); if (possiblePrice == null) continue; if (price == null || price.getPrice(1) > possiblePrice.getPrice(1)) @@ -118,15 +119,15 @@ public class Utilities { * @param blockedMaterial Material to ignore set to null on initial call * @return Worth of ItemStack as a double */ - private static Price getWorth(List items, Material blockedMaterial) { + private static Price getWorth(List items, Material blockedMaterial, Object2ObjectOpenHashMap map) { Price price = null; for (ItemStack item : items) { if (item == null) continue; - Price tmp = getWorth(new ItemStack(item.getType()), blockedMaterial); + Price tmp = getWorth(new ItemStack(item.getType()), blockedMaterial, map); if (tmp == null || tmp.getPrice(1) == -1) return null; - WorthConfig.prices.put(item.getType(), tmp); + map.put(item.getType(), tmp); if (price == null) price = tmp; else