From 691abd0deae36930ce14b01c365d517ff6c8c78c Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 16 Sep 2020 21:22:39 +0200 Subject: [PATCH] Add Stuff for Bungee GUI --- .../de/steamwar/coms/receiver/Handler.java | 2 +- .../steamwar/coms/receiver/PacketHandler.java | 8 +-- .../receiver/handlers/InventoryHandler.java | 53 +++++++++++++++++++ .../coms/receiver/handlers/PingHandler.java | 2 +- .../coms/receiver/handlers/TestHandler.java | 17 ------ .../de/steamwar/coms/{ => sender}/Packet.java | 2 +- .../de/steamwar/coms/sender/PacketSender.java | 1 - .../packets/InventoryCallbackPacket.java | 38 +++++++++++++ .../src/de/steamwar/inventory/SWItem.java | 25 ++++++++- 9 files changed, 119 insertions(+), 29 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/InventoryHandler.java delete mode 100644 SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/TestHandler.java rename SpigotCore_Main/src/de/steamwar/coms/{ => sender}/Packet.java (92%) create mode 100644 SpigotCore_Main/src/de/steamwar/coms/sender/packets/InventoryCallbackPacket.java diff --git a/SpigotCore_Main/src/de/steamwar/coms/receiver/Handler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/Handler.java index a049560..311facb 100644 --- a/SpigotCore_Main/src/de/steamwar/coms/receiver/Handler.java +++ b/SpigotCore_Main/src/de/steamwar/coms/receiver/Handler.java @@ -6,5 +6,5 @@ public abstract class Handler { protected abstract String getName(); - public abstract void handle(ByteArrayDataInput byteArrayDataInput) throws Exception; + public abstract void handle(ByteArrayDataInput byteArrayDataInput); } \ No newline at end of file diff --git a/SpigotCore_Main/src/de/steamwar/coms/receiver/PacketHandler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/PacketHandler.java index 8edb0d9..dd01da8 100644 --- a/SpigotCore_Main/src/de/steamwar/coms/receiver/PacketHandler.java +++ b/SpigotCore_Main/src/de/steamwar/coms/receiver/PacketHandler.java @@ -3,7 +3,6 @@ package de.steamwar.coms.receiver; import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteStreams; import de.steamwar.coms.receiver.handlers.PingHandler; -import de.steamwar.coms.receiver.handlers.TestHandler; import org.bukkit.entity.Player; import org.bukkit.plugin.messaging.PluginMessageListener; @@ -19,17 +18,12 @@ public class PacketHandler implements PluginMessageListener { } static { - registerHandler(new TestHandler()); registerHandler(new PingHandler()); } @Override public void onPluginMessageReceived(String s, Player player, byte[] bytes) { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); - try { - handlerMap.get(in.readUTF()).handle(in); - } catch (Exception e) { - e.printStackTrace(); - } + handlerMap.get(in.readUTF()).handle(in); } } diff --git a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/InventoryHandler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/InventoryHandler.java new file mode 100644 index 0000000..346be0e --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/InventoryHandler.java @@ -0,0 +1,53 @@ +package de.steamwar.coms.receiver.handlers; + +import com.google.common.io.ByteArrayDataInput; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import de.steamwar.coms.receiver.Handler; +import de.steamwar.coms.sender.packets.InventoryCallbackPacket; +import de.steamwar.inventory.SWInventory; +import de.steamwar.inventory.SWItem; +import org.bukkit.Bukkit; +import org.bukkit.Material; + +import java.util.*; + +public class InventoryHandler extends Handler { + + private static HashMap callbacks = new HashMap<>(); + + @Override + protected String getName() { + return "Inventory"; + } + + @Override + public void handle(ByteArrayDataInput byteArrayDataInput) { + String name = byteArrayDataInput.readUTF(); + String player = byteArrayDataInput.readUTF(); + String title = byteArrayDataInput.readUTF(); + int size = byteArrayDataInput.readInt(); + int lenght = byteArrayDataInput.readInt(); + Map items = new HashMap<>(); + for (int i = 0; i < lenght; i++) { + JsonObject object = new JsonParser().parse(byteArrayDataInput.readUTF()).getAsJsonObject(); + SWItem item = new SWItem(Material.valueOf(object.get("material").getAsString()), object.get("title").getAsString()); + if(object.has("skullOwner")) { + item = SWItem.getPlayerSkull(object.get("skullOwner").getAsString()); + item.setName(object.get("title").getAsString()); + } + if(object.has("enchanted")) + item.setEnchanted(true); + if(object.has("hideAttributes")) + item.hideAttributes(); + SWItem finalItem = item; + item.setCallback(click -> { + new InventoryCallbackPacket().setItem(finalItem).setPosition(object.get("position").getAsInt()).setTitle(title).send(); + }); + items.put(object.get("position").getAsInt(), item); + } + + SWInventory inventory = new SWInventory(Bukkit.getPlayer(UUID.fromString(player)), size, title,items); + inventory.open(); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/PingHandler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/PingHandler.java index 9ed52e6..b6e218e 100644 --- a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/PingHandler.java +++ b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/PingHandler.java @@ -17,7 +17,7 @@ public class PingHandler extends Handler { } @Override - public void handle(ByteArrayDataInput byteArrayDataInput) throws Exception { + public void handle(ByteArrayDataInput byteArrayDataInput) { String uuid = byteArrayDataInput.readUTF(); System.out.println(uuid); if(Bukkit.getPlayer(UUID.fromString(uuid)).isOnline()) { diff --git a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/TestHandler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/TestHandler.java deleted file mode 100644 index 93676c2..0000000 --- a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/TestHandler.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.steamwar.coms.receiver.handlers; - -import com.google.common.io.ByteArrayDataInput; -import de.steamwar.coms.receiver.Handler; -import org.bukkit.Bukkit; - -public class TestHandler extends Handler { - @Override - protected String getName() { - return "Test"; - } - - @Override - public void handle(ByteArrayDataInput byteArrayDataInput) throws Exception { - Bukkit.getOnlinePlayers().stream().forEach(player -> player.sendMessage("TEST!")); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/coms/Packet.java b/SpigotCore_Main/src/de/steamwar/coms/sender/Packet.java similarity index 92% rename from SpigotCore_Main/src/de/steamwar/coms/Packet.java rename to SpigotCore_Main/src/de/steamwar/coms/sender/Packet.java index 00320bf..fa75de1 100644 --- a/SpigotCore_Main/src/de/steamwar/coms/Packet.java +++ b/SpigotCore_Main/src/de/steamwar/coms/sender/Packet.java @@ -1,4 +1,4 @@ -package de.steamwar.coms; +package de.steamwar.coms.sender; import de.steamwar.coms.sender.PacketSender; diff --git a/SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java b/SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java index a3d36f3..d2b75c8 100644 --- a/SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java +++ b/SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java @@ -2,7 +2,6 @@ package de.steamwar.coms.sender; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; -import de.steamwar.coms.Packet; import de.steamwar.core.Core; import org.bukkit.Bukkit; diff --git a/SpigotCore_Main/src/de/steamwar/coms/sender/packets/InventoryCallbackPacket.java b/SpigotCore_Main/src/de/steamwar/coms/sender/packets/InventoryCallbackPacket.java new file mode 100644 index 0000000..698f535 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/coms/sender/packets/InventoryCallbackPacket.java @@ -0,0 +1,38 @@ +package de.steamwar.coms.sender.packets; + +import de.steamwar.coms.sender.Packet; +import de.steamwar.inventory.SWItem; + +import java.io.ObjectOutputStream; + +public class InventoryCallbackPacket extends Packet { + + String title; + int position; + SWItem item; + + @Override + public String getName() { + return "InventoryCallback"; + } + + @Override + public void writeVars(ObjectOutputStream objectOutputStream) { + + } + + public InventoryCallbackPacket setTitle(String title) { + this.title = title; + return this; + } + + public InventoryCallbackPacket setPosition(int position) { + this.position = position; + return this; + } + + public InventoryCallbackPacket setItem(SWItem item) { + this.item = item; + return this; + } +} diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java index 55aa8aa..c241792 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java @@ -19,6 +19,8 @@ package de.steamwar.inventory; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import de.steamwar.core.Core; import org.bukkit.Material; import org.bukkit.OfflinePlayer; @@ -137,7 +139,7 @@ public class SWItem { callback = c; } - private void hideAttributes() { + public void hideAttributes() { if (itemMeta == null) return; itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); itemMeta.addItemFlags(ItemFlag.HIDE_DESTROYS); @@ -193,4 +195,25 @@ public class SWItem { } itemStack.setItemMeta(itemMeta); } + + public String parseToJson(int position) { + JsonObject object = new JsonObject(); + object.addProperty("material", itemStack.getType().toString()); + object.addProperty("position", position); + object.addProperty("title", itemMeta.getDisplayName()); + if(itemMeta.hasEnchant(Enchantment.DURABILITY)) + object.addProperty("enchanted", true); + if(itemMeta.hasItemFlag(ItemFlag.HIDE_ENCHANTS)) + object.addProperty("hideAttributes", true); + if(!itemMeta.getLore().isEmpty()) { + JsonArray array = new JsonArray(); + for (String lores: + itemMeta.getLore()) { + array.add(lores); + } + object.add("lore", array); + } + + return object.getAsString(); + } }