Half converted buy ui to use confirmation

This commit is contained in:
Teriuihi 2022-01-07 00:21:27 +01:00
parent 0eb28ddf75
commit 7fa71e06a0
5 changed files with 49 additions and 17 deletions

View File

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

View File

@ -13,11 +13,13 @@ import java.util.HashMap;
public abstract class GUIInventory implements GUI {
protected final Inventory inventory;
protected final HashMap<Integer, GUIAction> actions;
protected final HashMap<Integer, GUIAction> tradeActions;
protected final HashMap<Integer, GUIAction> 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);
}
}

View File

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

View File

@ -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<Integer, GUIAction> actions;
protected final HashMap<Integer, GUIAction> tradeActions;
protected final HashMap<Integer, GUIAction> 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() {

View File

@ -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("<green>" + price + "</green>")); //TODO configurable
itemMeta.displayName(miniMessage.deserialize("<red>-" + price + "</red>")); //TODO configurable
itemStack.setItemMeta(itemMeta);
return itemStack;
}