From 2155e386fd8ecfc8776c2d4a5f1b720d16278c15 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 20 Sep 2020 12:01:12 +0200 Subject: [PATCH] Bungee GUI Improvements --- .../src/de/steamwar/comms/BungeeReceiver.java | 51 +++++++++++++++ .../comms/handlers/BungeeHandler.java | 8 +++ .../comms/handlers/InventoryHandler.java | 64 +++++++++++++++++++ .../packets/InventoryCallbackPacket.java | 28 +++----- .../steamwar/comms/packets/SpigotPacket.java | 20 ++++++ .../de/steamwar/coms/receiver/Handler.java | 10 --- .../steamwar/coms/receiver/PacketHandler.java | 33 ---------- .../handlers/CloseInventoryHandler.java | 21 ------ .../receiver/handlers/InventoryHandler.java | 62 ------------------ .../coms/receiver/handlers/PingHandler.java | 29 --------- .../src/de/steamwar/coms/sender/Packet.java | 17 ----- .../de/steamwar/coms/sender/PacketSender.java | 16 ----- .../src/de/steamwar/core/Core.java | 6 +- .../src/de/steamwar/inventory/SWItem.java | 23 +------ 14 files changed, 157 insertions(+), 231 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java create mode 100644 SpigotCore_Main/src/de/steamwar/comms/handlers/BungeeHandler.java create mode 100644 SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java rename SpigotCore_Main/src/de/steamwar/{coms/sender => comms}/packets/InventoryCallbackPacket.java (59%) create mode 100644 SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java delete mode 100644 SpigotCore_Main/src/de/steamwar/coms/receiver/Handler.java delete mode 100644 SpigotCore_Main/src/de/steamwar/coms/receiver/PacketHandler.java delete mode 100644 SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/CloseInventoryHandler.java delete mode 100644 SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/InventoryHandler.java delete mode 100644 SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/PingHandler.java delete mode 100644 SpigotCore_Main/src/de/steamwar/coms/sender/Packet.java delete mode 100644 SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java diff --git a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java new file mode 100644 index 0000000..377c372 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java @@ -0,0 +1,51 @@ +package de.steamwar.comms; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteStreams; +import de.steamwar.comms.handlers.BungeeHandler; +import de.steamwar.comms.handlers.InventoryHandler; +import de.steamwar.sql.SteamwarUser; +import org.bukkit.Bukkit; +import org.bukkit.Sound; +import org.bukkit.SoundCategory; +import org.bukkit.entity.Player; +import org.bukkit.plugin.messaging.PluginMessageListener; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class BungeeReceiver implements PluginMessageListener { + + private static Map handlerMap = new HashMap<>(); + + public static void registerHandler(int code, BungeeHandler handler) { + handlerMap.put(code, handler); + } + + public static void registerHandler(String name, BungeeHandler handler) { + handlerMap.put(name.hashCode(), handler); + } + + static { + registerHandler("PingHandler", byteArrayDataInput -> { + UUID uuid = SteamwarUser.get(byteArrayDataInput.readInt()).getUUID(); + if(Bukkit.getPlayer(uuid).isOnline()) { + Player player = Bukkit.getPlayer(uuid); + player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.MASTER, 1, 1); + } + + }); + registerHandler("InventoryHandler", new InventoryHandler()); + registerHandler("CloseInventory", byteArrayDataInput -> { + Player player = Bukkit.getPlayer(SteamwarUser.get(byteArrayDataInput.readInt()).getUUID()); + player.closeInventory(); + }); + } + + @Override + public void onPluginMessageReceived(String s, Player player, byte[] bytes) { + ByteArrayDataInput in = ByteStreams.newDataInput(bytes); + handlerMap.get(in.readInt()).handle(in); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/comms/handlers/BungeeHandler.java b/SpigotCore_Main/src/de/steamwar/comms/handlers/BungeeHandler.java new file mode 100644 index 0000000..474ca5e --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/comms/handlers/BungeeHandler.java @@ -0,0 +1,8 @@ +package de.steamwar.comms.handlers; + +import com.google.common.io.ByteArrayDataInput; + +public interface BungeeHandler { + + void handle(ByteArrayDataInput byteArrayDataInput); +} \ No newline at end of file diff --git a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java new file mode 100644 index 0000000..2883eea --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java @@ -0,0 +1,64 @@ +package de.steamwar.comms.handlers; + +import com.google.common.io.ByteArrayDataInput; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import de.steamwar.inventory.SWInventory; +import de.steamwar.inventory.SWItem; +import de.steamwar.sql.SteamwarUser; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import de.steamwar.comms.packets.*; + +import java.util.*; + +public class InventoryHandler implements BungeeHandler { + + @Override + public void handle(ByteArrayDataInput byteArrayDataInput) { + JsonObject object = new JsonParser().parse(byteArrayDataInput.readUTF()).getAsJsonObject(); + System.out.println(object.toString()); + UUID player = SteamwarUser.get(object.get("id").getAsInt()).getUUID(); + String title = object.get("title").getAsString(); + int size = object.get("size").getAsInt(); + int length = object.get("itemcount").getAsInt(); + Map items = new HashMap<>(); + JsonArray array = object.get("items").getAsJsonArray(); + for (int i = 0; i < length; i++) { + JsonObject itemJson = array.get(i).getAsJsonObject(); + SWItem item = null; + try { + item = new SWItem(Material.valueOf(itemJson.get("material").getAsString()), itemJson.get("title").getAsString()); + }catch (IllegalArgumentException e) { + item = new SWItem(Material.STONE, itemJson.get("title").getAsString()); + } + if(itemJson.has("skullOwner")) { + item = SWItem.getPlayerSkull(itemJson.get("skullOwner").getAsString()); + item.setName(itemJson.get("title").getAsString()); + } + if(itemJson.has("color")) { + item.getItemStack().setType(SWItem.getDye(itemJson.get("color").getAsInt())); + } + if(itemJson.has("enchanted")) + item.setEnchanted(true); + if(itemJson.has("lore")) { + List lore = new ArrayList<>(); + JsonArray loreArray = itemJson.getAsJsonArray("lore"); + loreArray.forEach(jsonElement -> lore.add(jsonElement.getAsString())); + item.setLore(lore); + } + item.setCallback(click -> { + new InventoryCallbackPacket().setPosition(itemJson.get("position").getAsInt()).setClick(click) + .setCallback(InventoryCallbackPacket.CallbackType.CLICK) + .setOwner(SteamwarUser.get(player).getId()).send(Bukkit.getPlayer(player)); + }); + items.put(itemJson.get("position").getAsInt(), item); + } + + SWInventory inventory = new SWInventory(Bukkit.getPlayer(player), size, title, items); + inventory.addCloseCallback(click -> new InventoryCallbackPacket() + .setCallback(InventoryCallbackPacket.CallbackType.CLOSE).setOwner(SteamwarUser.get(player).getId()).send(Bukkit.getPlayer(player))); + inventory.open(); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/coms/sender/packets/InventoryCallbackPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java similarity index 59% rename from SpigotCore_Main/src/de/steamwar/coms/sender/packets/InventoryCallbackPacket.java rename to SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java index 97aa2bc..254b15d 100644 --- a/SpigotCore_Main/src/de/steamwar/coms/sender/packets/InventoryCallbackPacket.java +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java @@ -1,29 +1,26 @@ -package de.steamwar.coms.sender.packets; +package de.steamwar.comms.packets; import com.google.common.io.ByteArrayDataOutput; -import de.steamwar.coms.sender.Packet; -import de.steamwar.inventory.SWItem; import org.bukkit.event.inventory.ClickType; -public class InventoryCallbackPacket extends Packet { +public class InventoryCallbackPacket extends SpigotPacket { int position; - SWItem item; ClickType clickType; CallbackType callbackType; - String owner; + int owner; @Override - public String getName() { - return "InventoryCallback"; + public int getName() { + return "InventoryCallback".hashCode(); } @Override public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - byteArrayDataOutput.writeUTF(owner); + byteArrayDataOutput.writeInt(owner); byteArrayDataOutput.writeUTF(callbackType.name()); - if(item != null){ - byteArrayDataOutput.writeUTF(item.parseToJson(position)); + if(position != 0){ + byteArrayDataOutput.writeInt(position); byteArrayDataOutput.writeUTF(clickType.name()); } } @@ -33,11 +30,6 @@ public class InventoryCallbackPacket extends Packet { return this; } - public InventoryCallbackPacket setItem(SWItem item) { - this.item = item; - return this; - } - public InventoryCallbackPacket setClick(ClickType type){ this.clickType = type; return this; @@ -48,8 +40,8 @@ public class InventoryCallbackPacket extends Packet { return this; } - public InventoryCallbackPacket setOwner(String uuid) { - this.owner = uuid; + public InventoryCallbackPacket setOwner(int id) { + this.owner = id; return this; } diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java new file mode 100644 index 0000000..e602717 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java @@ -0,0 +1,20 @@ +package de.steamwar.comms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; +import de.steamwar.core.Core; +import org.bukkit.entity.Player; + +public abstract class SpigotPacket { + + public void send(Player player) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeInt(getName()); + writeVars(out); + player.sendPluginMessage(Core.getInstance(), "sw:bridge", out.toByteArray()); + } + + public abstract int getName(); + + public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput); +} diff --git a/SpigotCore_Main/src/de/steamwar/coms/receiver/Handler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/Handler.java deleted file mode 100644 index 311facb..0000000 --- a/SpigotCore_Main/src/de/steamwar/coms/receiver/Handler.java +++ /dev/null @@ -1,10 +0,0 @@ -package de.steamwar.coms.receiver; - -import com.google.common.io.ByteArrayDataInput; - -public abstract class Handler { - - protected abstract String getName(); - - 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 deleted file mode 100644 index 1f04ad5..0000000 --- a/SpigotCore_Main/src/de/steamwar/coms/receiver/PacketHandler.java +++ /dev/null @@ -1,33 +0,0 @@ -package de.steamwar.coms.receiver; - -import com.google.common.io.ByteArrayDataInput; -import com.google.common.io.ByteStreams; -import de.steamwar.coms.receiver.handlers.CloseInventoryHandler; -import de.steamwar.coms.receiver.handlers.InventoryHandler; -import de.steamwar.coms.receiver.handlers.PingHandler; -import org.bukkit.entity.Player; -import org.bukkit.plugin.messaging.PluginMessageListener; - -import java.util.HashMap; -import java.util.Map; - -public class PacketHandler implements PluginMessageListener { - - private static Map handlerMap = new HashMap<>(); - - public static void registerHandler(Handler handler) { - handlerMap.put(handler.getName(), handler); - } - - static { - registerHandler(new PingHandler()); - registerHandler(new InventoryHandler()); - registerHandler(new CloseInventoryHandler()); - } - - @Override - public void onPluginMessageReceived(String s, Player player, byte[] bytes) { - ByteArrayDataInput in = ByteStreams.newDataInput(bytes); - handlerMap.get(in.readUTF()).handle(in); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/CloseInventoryHandler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/CloseInventoryHandler.java deleted file mode 100644 index 7843a24..0000000 --- a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/CloseInventoryHandler.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.steamwar.coms.receiver.handlers; - -import com.google.common.io.ByteArrayDataInput; -import de.steamwar.coms.receiver.Handler; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; - -import java.util.UUID; - -public class CloseInventoryHandler extends Handler { - @Override - protected String getName() { - return "InvClose"; - } - - @Override - public void handle(ByteArrayDataInput byteArrayDataInput) { - Player player = Bukkit.getPlayer(UUID.fromString(byteArrayDataInput.readUTF())); - player.closeInventory(); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/InventoryHandler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/InventoryHandler.java deleted file mode 100644 index 3eeb8e8..0000000 --- a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/InventoryHandler.java +++ /dev/null @@ -1,62 +0,0 @@ -package de.steamwar.coms.receiver.handlers; - -import com.google.common.io.ByteArrayDataInput; -import com.google.gson.JsonArray; -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 { - - @Override - protected String getName() { - return "Inventory"; - } - - @Override - public void handle(ByteArrayDataInput byteArrayDataInput) { - String player = byteArrayDataInput.readUTF(); - String title = byteArrayDataInput.readUTF(); - int size = byteArrayDataInput.readInt(); - int length = byteArrayDataInput.readInt(); - Map items = new HashMap<>(); - for (int i = 0; i < length; 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(); - if(object.has("lore")) { - List lore = new ArrayList<>(); - JsonArray array = object.getAsJsonArray("lore"); - array.forEach(jsonElement -> lore.add(jsonElement.getAsString())); - item.setLore(lore); - } - SWItem finalItem = item; - item.setCallback(click -> { - new InventoryCallbackPacket().setItem(finalItem).setClick(click) - .setPosition(object.get("position").getAsInt()) - .setCallback(InventoryCallbackPacket.CallbackType.CLICK) - .setOwner(player).send(Bukkit.getPlayer(UUID.fromString(player))); - }); - items.put(object.get("position").getAsInt(), item); - } - - SWInventory inventory = new SWInventory(Bukkit.getPlayer(UUID.fromString(player)), size, title, items); - inventory.addCloseCallback(click -> new InventoryCallbackPacket() - .setCallback(InventoryCallbackPacket.CallbackType.CLOSE).setOwner(player).send(Bukkit.getPlayer(UUID.fromString(player)))); - 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 deleted file mode 100644 index b6e218e..0000000 --- a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/PingHandler.java +++ /dev/null @@ -1,29 +0,0 @@ -package de.steamwar.coms.receiver.handlers; - -import com.google.common.io.ByteArrayDataInput; -import de.steamwar.coms.receiver.Handler; -import org.bukkit.Bukkit; -import org.bukkit.Sound; -import org.bukkit.SoundCategory; -import org.bukkit.entity.Player; - -import java.util.UUID; - -public class PingHandler extends Handler { - - @Override - protected String getName() { - return "Ping"; - } - - @Override - public void handle(ByteArrayDataInput byteArrayDataInput) { - String uuid = byteArrayDataInput.readUTF(); - System.out.println(uuid); - if(Bukkit.getPlayer(UUID.fromString(uuid)).isOnline()) { - Player player = Bukkit.getPlayer(UUID.fromString(uuid)); - player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.MASTER, 1, 1); - } - - } -} diff --git a/SpigotCore_Main/src/de/steamwar/coms/sender/Packet.java b/SpigotCore_Main/src/de/steamwar/coms/sender/Packet.java deleted file mode 100644 index f3c56d0..0000000 --- a/SpigotCore_Main/src/de/steamwar/coms/sender/Packet.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.steamwar.coms.sender; - -import com.google.common.io.ByteArrayDataOutput; -import org.bukkit.entity.Player; - -import java.io.Serializable; - -public abstract class Packet implements Serializable { - - public void send(Player player) { - PacketSender.sendPacket(this, player); - } - - public abstract String getName(); - - public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput); -} diff --git a/SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java b/SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java deleted file mode 100644 index 5a78040..0000000 --- a/SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java +++ /dev/null @@ -1,16 +0,0 @@ -package de.steamwar.coms.sender; - -import com.google.common.io.ByteArrayDataOutput; -import com.google.common.io.ByteStreams; -import de.steamwar.core.Core; -import org.bukkit.entity.Player; - -public class PacketSender { - - public static void sendPacket(Packet packet, Player player) { - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF(packet.getName()); - packet.writeVars(out); - player.sendPluginMessage(Core.getInstance(), "sw:return", out.toByteArray()); - } -} \ No newline at end of file diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index cf2b126..893e53a 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -19,7 +19,7 @@ package de.steamwar.core; -import de.steamwar.coms.receiver.PacketHandler; +import de.steamwar.comms.BungeeReceiver; import de.steamwar.core.events.ChattingEvent; import de.steamwar.core.events.ChunkListener; import de.steamwar.core.events.PlayerJoinedEvent; @@ -62,8 +62,8 @@ public class Core extends JavaPlugin{ ChunkListener.init(); if(version >= 12) ErrorLogger.init(); - getServer().getMessenger().registerIncomingPluginChannel(this, "sw:bridge", new PacketHandler()); - getServer().getMessenger().registerOutgoingPluginChannel(this, "sw:return"); + getServer().getMessenger().registerIncomingPluginChannel(this, "sw:bridge", new BungeeReceiver()); + getServer().getMessenger().registerOutgoingPluginChannel(this, "sw:bridge"); } diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java index 250d71e..d62d57a 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java @@ -139,7 +139,7 @@ public class SWItem { callback = c; } - public void hideAttributes() { + private void hideAttributes() { if (itemMeta == null) return; itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); itemMeta.addItemFlags(ItemFlag.HIDE_DESTROYS); @@ -195,25 +195,4 @@ 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() != null) { - JsonArray array = new JsonArray(); - for (String lores: - itemMeta.getLore()) { - array.add(lores); - } - object.add("lore", array); - } - - return object.toString(); - } }