From 34534b49d850b8b6b6059f12cba2ed1d2cb90c65 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 19 Sep 2020 12:37:26 +0200 Subject: [PATCH] Add Bungee GUI Prototype --- src/de/steamwar/bungeecore/BungeeCore.java | 10 ++- .../bungeecore/commands/TestCommand.java | 33 +++++++++ .../handlers/InventoryCallbackHandler.java | 23 ++++++ .../coms/packets/CloseInventoryPacket.java | 25 +++++++ .../coms/packets/InventoryPacket.java | 28 ++++---- .../coms/packets/inventory/InvCallback.java | 45 ++++++++++++ .../coms/packets/inventory/SWInventory.java | 71 +++++++++++++++++++ .../coms/packets/inventory/SWItem.java | 62 ++++++++++++++-- .../coms/receiver/PacketHandler.java | 13 ++-- 9 files changed, 284 insertions(+), 26 deletions(-) create mode 100644 src/de/steamwar/bungeecore/commands/TestCommand.java create mode 100644 src/de/steamwar/bungeecore/coms/packets/CloseInventoryPacket.java create mode 100644 src/de/steamwar/bungeecore/coms/packets/inventory/InvCallback.java diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index 518848b..036cfcf 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -20,6 +20,7 @@ package de.steamwar.bungeecore; import de.steamwar.bungeecore.commands.*; +import de.steamwar.bungeecore.coms.receiver.PacketHandler; import de.steamwar.bungeecore.listeners.*; import de.steamwar.bungeecore.listeners.mods.Fabric; import de.steamwar.bungeecore.listeners.mods.Forge; @@ -72,6 +73,10 @@ public class BungeeCore extends Plugin { @Override public void onEnable(){ + + getProxy().registerChannel("sw:bridge"); + getProxy().registerChannel("sw:return"); + setInstance(this); loadConfig(); @@ -124,14 +129,15 @@ public class BungeeCore extends Plugin { new IgnoreCommand(); new UnIgnoreCommand(); + new TestCommand(); + new EventStarter(); new Broadcaster(); new SessionManager(); + new PacketHandler(); new TablistManager(); - getProxy().registerChannel("sw:bridge"); - getProxy().getScheduler().schedule(this, () -> { SteamwarUser.clearCache(); Team.clearCache(); diff --git a/src/de/steamwar/bungeecore/commands/TestCommand.java b/src/de/steamwar/bungeecore/commands/TestCommand.java new file mode 100644 index 0000000..a7c18ca --- /dev/null +++ b/src/de/steamwar/bungeecore/commands/TestCommand.java @@ -0,0 +1,33 @@ +package de.steamwar.bungeecore.commands; + +import de.steamwar.bungeecore.coms.packets.inventory.SWInventory; +import de.steamwar.bungeecore.coms.packets.inventory.SWItem; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +public class TestCommand extends BasicCommand { + + public TestCommand() { + super("test", null); + } + + @Override + public void execute(CommandSender commandSender, String[] strings) { + if(!(commandSender instanceof ProxiedPlayer)) + return; + SWInventory inventory = new SWInventory((ProxiedPlayer) commandSender, 3*9, "Test Inventory"); + SWItem item = new SWItem("DIRT", 13, "§e§lTest Item"); + item.setSkullOwner(commandSender.getName()); + for (ProxiedPlayer player: + ((ProxiedPlayer) commandSender).getServer().getInfo().getPlayers()) { + item.addLore("§6" + player.getDisplayName()); + } + item.setEnchanted(true); + item.setCallback(click -> { + inventory.close(); + commandSender.sendMessage("TEST!!! FJOnbds"); + }); + inventory.addItem(item); + inventory.open(); + } +} diff --git a/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java b/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java index ca9d072..666a5a8 100644 --- a/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java +++ b/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java @@ -1,9 +1,18 @@ package de.steamwar.bungeecore.coms.handlers; import com.google.common.io.ByteArrayDataInput; +import de.steamwar.bungeecore.coms.packets.inventory.InvCallback; +import de.steamwar.bungeecore.coms.packets.inventory.SWInventory; +import de.steamwar.bungeecore.coms.packets.inventory.SWItem; import de.steamwar.bungeecore.coms.receiver.Handler; +import java.util.HashMap; +import java.util.Map; + public class InventoryCallbackHandler extends Handler { + + public static final Map inventoryHashMap = new HashMap<>(); + @Override protected String getName() { return "InventoryCallback"; @@ -11,6 +20,20 @@ public class InventoryCallbackHandler extends Handler { @Override public void handle(ByteArrayDataInput byteArrayDataInput) { + String owner = byteArrayDataInput.readUTF(); + CallbackType type = CallbackType.valueOf(byteArrayDataInput.readUTF()); + if(type == CallbackType.CLICK) { + SWItem item = SWItem.getFromJson(byteArrayDataInput.readUTF()); + InvCallback.ClickType clickType = InvCallback.ClickType.valueOf(byteArrayDataInput.readUTF()); + System.out.println(item.writeToString()); + inventoryHashMap.get(owner).handleCallback(clickType, item); + }else if(type == CallbackType.CLOSE) { + inventoryHashMap.get(owner).handleClose(); + } + } + public enum CallbackType { + CLICK, + CLOSE, } } diff --git a/src/de/steamwar/bungeecore/coms/packets/CloseInventoryPacket.java b/src/de/steamwar/bungeecore/coms/packets/CloseInventoryPacket.java new file mode 100644 index 0000000..4fb8517 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/packets/CloseInventoryPacket.java @@ -0,0 +1,25 @@ +package de.steamwar.bungeecore.coms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import de.steamwar.bungeecore.coms.sender.Packet; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +public class CloseInventoryPacket extends Packet { + + ProxiedPlayer player; + + @Override + public String getName() { + return "InvClose"; + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + byteArrayDataOutput.writeUTF(player.getUniqueId().toString()); + } + + public CloseInventoryPacket setPlayer(ProxiedPlayer player) { + this.player = player; + return this; + } +} diff --git a/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java b/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java index 64466a5..9becf90 100644 --- a/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java +++ b/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java @@ -5,19 +5,19 @@ import de.steamwar.bungeecore.coms.packets.inventory.SWItem; import de.steamwar.bungeecore.coms.sender.Packet; import net.md_5.bungee.api.connection.ProxiedPlayer; +import java.util.HashMap; + public class InventoryPacket extends Packet { String title; - String name; String player; int size; - SWItem[] items; + HashMap items; - public InventoryPacket(int size, String title, String name, ProxiedPlayer player) { - items = new SWItem[size]; + public InventoryPacket(int size, String title, ProxiedPlayer player) { + items = new HashMap<>(); this.title = title; this.size = size; - this.name = name; this.player = player.getUniqueId().toString(); } @@ -28,28 +28,26 @@ public class InventoryPacket extends Packet { @Override public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - byteArrayDataOutput.writeUTF(name); byteArrayDataOutput.writeUTF(player); byteArrayDataOutput.writeUTF(title); byteArrayDataOutput.writeInt(size); - byteArrayDataOutput.writeInt(items.length); - for (SWItem item: - items) { - if(item == null) - byteArrayDataOutput.writeUTF(item.writeToString()); + byteArrayDataOutput.writeInt(items.size()); + for (int i = 0; i < size; i++) { + if(items.get(i) != null) + byteArrayDataOutput.writeUTF(items.get(i).writeToString()); } } public void addItem(SWItem item) { - for (int i = 0; i < items.length; i++) { - if (items[i] != null) { - items[i] = item; + for (int i = 0; i < items.size(); i++) { + if (items.get(i) != null) { + items.put(i, item); return; } } } public void setItem(int index, SWItem item) { - items[index] = item; + items.put(index, item); } } diff --git a/src/de/steamwar/bungeecore/coms/packets/inventory/InvCallback.java b/src/de/steamwar/bungeecore/coms/packets/inventory/InvCallback.java new file mode 100644 index 0000000..a3a0365 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/packets/inventory/InvCallback.java @@ -0,0 +1,45 @@ +package de.steamwar.bungeecore.coms.packets.inventory; + +public interface InvCallback { + + void clicked(ClickType click); + + public static enum ClickType { + LEFT, + SHIFT_LEFT, + RIGHT, + SHIFT_RIGHT, + WINDOW_BORDER_LEFT, + WINDOW_BORDER_RIGHT, + MIDDLE, + NUMBER_KEY, + DOUBLE_CLICK, + DROP, + CONTROL_DROP, + CREATIVE, + UNKNOWN; + + private ClickType() { + } + + public boolean isKeyboardClick() { + return this == NUMBER_KEY || this == DROP || this == CONTROL_DROP; + } + + public boolean isCreativeAction() { + return this == MIDDLE || this == CREATIVE; + } + + public boolean isRightClick() { + return this == RIGHT || this == SHIFT_RIGHT; + } + + public boolean isLeftClick() { + return this == LEFT || this == SHIFT_LEFT || this == DOUBLE_CLICK || this == CREATIVE; + } + + public boolean isShiftClick() { + return this == SHIFT_LEFT || this == SHIFT_RIGHT || this == CONTROL_DROP; + } + } +} diff --git a/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java b/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java index c155bcf..c681e58 100644 --- a/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java +++ b/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java @@ -1,4 +1,75 @@ package de.steamwar.bungeecore.coms.packets.inventory; +import de.steamwar.bungeecore.coms.handlers.InventoryCallbackHandler; +import de.steamwar.bungeecore.coms.packets.CloseInventoryPacket; +import de.steamwar.bungeecore.coms.packets.InventoryPacket; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.HashMap; +import java.util.Map; + public class SWInventory { + + Map itemMap; + InvCallback close; + ProxiedPlayer player; + int size; + String title; + + public SWInventory(ProxiedPlayer proxiedPlayer, int size, String title) { + itemMap = new HashMap<>(); + InventoryCallbackHandler.inventoryHashMap.put(proxiedPlayer.getUniqueId().toString(), this); + this.player = proxiedPlayer; + this.size = size; + this.title = title; + } + + public void addItem(SWItem item) { + itemMap.put(item.position, item); + } + + public void removeItem(SWItem item) { + itemMap.remove(item.position); + } + + public void removeItem(int position) { + itemMap.remove(position); + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public void handleCallback(InvCallback.ClickType type, SWItem item) { + itemMap.get(item.position).callback.clicked(type); + } + + public void handleClose() { + if(close != null) + close.clicked(null); + } + + public void open() { + InventoryPacket inv = new InventoryPacket(size, title, player); + for (Map.Entry entry : itemMap.entrySet()) { + inv.setItem(entry.getKey(), entry.getValue()); + } + inv.send(player); + } + + public void close() { + new CloseInventoryPacket().setPlayer(player).send(player); + } } diff --git a/src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java b/src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java index 2ead66a..276c06d 100644 --- a/src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java +++ b/src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java @@ -2,6 +2,7 @@ package de.steamwar.bungeecore.coms.packets.inventory; import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import java.util.ArrayList; import java.util.List; @@ -12,20 +13,49 @@ public class SWItem { boolean enchanted, hideAttributes; int position; List lore; + InvCallback callback; public SWItem(String material, int position, String title) { - this.material = material; + this.material = material.toUpperCase(); lore = new ArrayList<>(); this.position = position; this.title = title; } + private SWItem(String json) { + JsonObject object = new JsonParser().parse(json).getAsJsonObject(); + material = object.get("material").getAsString(); + position = object.get("position").getAsInt(); + title = object.get("title").getAsString(); + if(object.has("skullOwner")) { + skullOwner = object.get("skullOwner").getAsString(); + } + if(object.has("enchanted")) + enchanted = true; + if(object.has("hideAttributes")) + hideAttributes = true; + if(object.has("lore")) { + List lore = new ArrayList<>(); + JsonArray array = object.getAsJsonArray("lore"); + array.forEach(jsonElement -> lore.add(jsonElement.getAsString())); + this.lore = lore; + } + } + + public void setCallback(InvCallback callback) { + this.callback = callback; + } + + public InvCallback getCallback() { + return callback; + } + public String getMaterial() { return material; } public void setMaterial(String material) { - this.material = material; + this.material = material.toUpperCase(); } public String getSkullOwner() { @@ -67,7 +97,7 @@ public class SWItem { object.addProperty("enchanted", true); if(hideAttributes) object.addProperty("hideAttributes", true); - if(!lore.isEmpty()) { + if(lore != null) { JsonArray array = new JsonArray(); for (String lores: lore) { @@ -76,6 +106,30 @@ public class SWItem { object.add("lore", array); } - return object.getAsString(); + return object.toString(); + } + + public static SWItem getFromJson(String json) { + return new SWItem(json); + } + + @Override + public boolean equals(Object o) { + if(!(o instanceof SWItem)) + return false; + SWItem item = (SWItem) o; + if(item.enchanted != enchanted) + return false; + if(item.hideAttributes != hideAttributes) + return false; + if(!item.material.equals(material)) + return false; + if(!item.lore.equals(lore)) + return false; + if(!item.skullOwner.equals(skullOwner)) + return false; + if(item.position != position) + return false; + return true; } } diff --git a/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java b/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java index 59d5c79..3971952 100644 --- a/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java +++ b/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java @@ -2,6 +2,7 @@ package de.steamwar.bungeecore.coms.receiver; import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteStreams; +import de.steamwar.bungeecore.coms.handlers.InventoryCallbackHandler; import de.steamwar.bungeecore.listeners.BasicListener; import net.md_5.bungee.api.event.PluginMessageEvent; import net.md_5.bungee.event.EventHandler; @@ -11,14 +12,16 @@ import java.util.Map; public class PacketHandler extends BasicListener { - private static Map handlerMap = new HashMap<>(); + private static final Map handlerMap = new HashMap<>(); @EventHandler public void onPluginMessage(PluginMessageEvent event) { - if(!event.getTag().equalsIgnoreCase("sw:bridge")) + System.out.println(event.toString()); + if(!event.getTag().equalsIgnoreCase("sw:return")) return; - ByteArrayDataInput in = ByteStreams.newDataInput( event.getData() ); - handlerMap.get(in.readUTF()).handle(in); + ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); + String handler = in.readUTF(); + handlerMap.get(handler).handle(in); } public static void registerHandler(Handler handler) { @@ -26,6 +29,6 @@ public class PacketHandler extends BasicListener { } static { - //TODO Register Handlers + registerHandler(new InventoryCallbackHandler()); } }