From 7fa71e06a0688816c6dcfd4f21617c16d1886517 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 7 Jan 2022 00:21:27 +0100 Subject: [PATCH] Half converted buy ui to use confirmation --- src/main/java/com/alttd/GUI/GUI.java | 4 ++- src/main/java/com/alttd/GUI/GUIInventory.java | 16 +++++++---- src/main/java/com/alttd/GUI/GUIListener.java | 4 +-- src/main/java/com/alttd/GUI/GUIMerchant.java | 28 ++++++++++++++----- .../java/com/alttd/GUI/windows/BuyGUI.java | 14 ++++++++-- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/alttd/GUI/GUI.java b/src/main/java/com/alttd/GUI/GUI.java index 150d66c..4f0fc9d 100644 --- a/src/main/java/com/alttd/GUI/GUI.java +++ b/src/main/java/com/alttd/GUI/GUI.java @@ -12,7 +12,9 @@ public interface GUI { void open(Player player); - GUIAction getAction(int slot); + GUIAction getTradeAction(int slot); + + GUIAction getGuiAction(int slot); Inventory getInventory(); diff --git a/src/main/java/com/alttd/GUI/GUIInventory.java b/src/main/java/com/alttd/GUI/GUIInventory.java index 16ea63a..f53caec 100644 --- a/src/main/java/com/alttd/GUI/GUIInventory.java +++ b/src/main/java/com/alttd/GUI/GUIInventory.java @@ -13,11 +13,13 @@ import java.util.HashMap; public abstract class GUIInventory implements GUI { protected final Inventory inventory; - protected final HashMap actions; + protected final HashMap tradeActions; + protected final HashMap guiActions; public GUIInventory(InventoryType type, Component name) { inventory = Bukkit.createInventory(null, type, name); - actions = new HashMap<>(); + tradeActions = new HashMap<>(); + guiActions = new HashMap<>(); } public Merchant getMerchant() { @@ -31,7 +33,7 @@ public abstract class GUIInventory implements GUI { public void setItem(int slot, ItemStack stack, GUIAction action){ inventory.setItem(slot, stack); if (action != null){ - actions.put(slot, action); + guiActions.put(slot, action); } } @@ -44,7 +46,11 @@ public abstract class GUIInventory implements GUI { GUIByUUID.put(player.getUniqueId(), this); } - public GUIAction getAction(int slot) { - return actions.get(slot); + public GUIAction getTradeAction(int slot) { + return tradeActions.get(slot); + } + + public GUIAction getGuiAction(int slot) { + return guiActions.get(slot); } } diff --git a/src/main/java/com/alttd/GUI/GUIListener.java b/src/main/java/com/alttd/GUI/GUIListener.java index 8a05086..91de3e0 100644 --- a/src/main/java/com/alttd/GUI/GUIListener.java +++ b/src/main/java/com/alttd/GUI/GUIListener.java @@ -26,7 +26,7 @@ public class GUIListener implements Listener { if (!gui.getInventory().equals(event.getInventory())) return; event.setCancelled(true); - GUIAction action = gui.getAction(event.getSlot()); + GUIAction action = gui.getGuiAction(event.getSlot()); if (action != null){ action.click(player); @@ -46,7 +46,7 @@ public class GUIListener implements Listener { return; } event.setCancelled(true); - GUIAction action = guiMerchant.getAction(event.getIndex()); + GUIAction action = guiMerchant.getTradeAction(event.getIndex()); if (action != null){ action.click(player); diff --git a/src/main/java/com/alttd/GUI/GUIMerchant.java b/src/main/java/com/alttd/GUI/GUIMerchant.java index c999083..3cfb832 100644 --- a/src/main/java/com/alttd/GUI/GUIMerchant.java +++ b/src/main/java/com/alttd/GUI/GUIMerchant.java @@ -3,23 +3,27 @@ package com.alttd.GUI; import com.alttd.objects.VillagerType; import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; +import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.inventory.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; public abstract class GUIMerchant implements GUI{ protected final Merchant merchant; - protected final HashMap actions; + protected final HashMap tradeActions; + protected final HashMap guiActions; private final VillagerType villagerType; public GUIMerchant(Component name, VillagerType villagerType) { merchant = Bukkit.createMerchant(name); - actions = new HashMap<>(); + tradeActions = new HashMap<>(); + guiActions = new HashMap<>(); this.villagerType = villagerType; } @@ -41,9 +45,15 @@ public abstract class GUIMerchant implements GUI{ recipes.add(merchantRecipe); merchant.setRecipes(recipes); - if (action != null){ - actions.put(recipes.size() - 1, action); - } + if (action != null) + tradeActions.put(recipes.size() - 1, action); + } + + public void setItem(int slot, @NotNull ItemStack itemStack, @Nullable GUIAction action) { + Inventory inventory = (Inventory) merchant; + inventory.setItem(slot, itemStack); + if (action != null) + guiActions.put(slot, action); } public void open(Player player){ @@ -51,8 +61,12 @@ public abstract class GUIMerchant implements GUI{ GUIByUUID.put(player.getUniqueId(), this); } - public GUIAction getAction(int slot) { - return actions.get(slot); + public GUIAction getTradeAction(int slot) { + return tradeActions.get(slot); + } + + public GUIAction getGuiAction(int slot) { + return guiActions.get(slot); } public VillagerType getVillagerType() { diff --git a/src/main/java/com/alttd/GUI/windows/BuyGUI.java b/src/main/java/com/alttd/GUI/windows/BuyGUI.java index a813515..bae409a 100644 --- a/src/main/java/com/alttd/GUI/windows/BuyGUI.java +++ b/src/main/java/com/alttd/GUI/windows/BuyGUI.java @@ -66,7 +66,16 @@ public class BuyGUI extends GUIMerchant { )); return; } + buy2(player, amount, cost, material, econUser, villagerType, transPts, oldPoints, price); +// setItem(0, new ItemStack(material), null); +// setItem(1, new ItemStack(Material.CANDLE), null); +// setItem(2, new ItemStack(Material.EMERALD_BLOCK), player1 -> +// buy2(player1, amount, cost, material, econUser, villagerType, transPts, oldPoints, price)); +// player.updateInventory(); + } + private void buy2(Player player, int amount, double cost, Material material, EconUser econUser, VillagerType villagerType, int transPts, int oldPoints, Price price) { + Economy econ = VillagerUI.getInstance().getEconomy(); var ref = new Object() { int space = 0; }; @@ -95,7 +104,7 @@ public class BuyGUI extends GUIMerchant { Template.template("amount", String.valueOf(amount)), Template.template("item", StringUtils.capitalize(material.name() .toLowerCase().replaceAll("_", " "))), - Template.template("price", String.valueOf(cost)), + Template.template("price", "-" + cost), Template.template("points", String.valueOf(transPts)), Template.template("total_points", String.valueOf(newPoints)), Template.template("villager_name", villagerType.getDisplayName()) @@ -104,6 +113,7 @@ public class BuyGUI extends GUIMerchant { Bukkit.getServer().getPluginManager() .callEvent(new SpawnShopEvent(player, amount, cost, material, oldPoints, newPoints, true)); +// buy(villagerType, player, material, amount, price); } private ItemStack getPriceItem(double price) { @@ -116,7 +126,7 @@ public class BuyGUI extends GUIMerchant { private ItemStack nameItem(ItemStack itemStack, double price) { ItemMeta itemMeta = itemStack.getItemMeta(); - itemMeta.displayName(miniMessage.deserialize("" + price + "")); //TODO configurable + itemMeta.displayName(miniMessage.deserialize("-" + price + "")); //TODO configurable itemStack.setItemMeta(itemMeta); return itemStack; }