From 2dcecc5a62d773df012bcfdd4f84aadbfd406b5e Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 15 Sep 2020 22:54:39 +0200 Subject: [PATCH 1/9] Add Basic Packet System --- .../src/de/steamwar/coms/Packet.java | 18 ++++++++++ .../de/steamwar/coms/receiver/Handler.java | 10 ++++++ .../steamwar/coms/receiver/PacketHandler.java | 35 +++++++++++++++++++ .../coms/receiver/handlers/PingHandler.java | 29 +++++++++++++++ .../coms/receiver/handlers/TestHandler.java | 17 +++++++++ .../de/steamwar/coms/sender/PacketSender.java | 30 ++++++++++++++++ .../src/de/steamwar/core/Core.java | 4 +++ .../de/steamwar/sql/DownloadSchematic.java | 3 +- 8 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 SpigotCore_Main/src/de/steamwar/coms/Packet.java create mode 100644 SpigotCore_Main/src/de/steamwar/coms/receiver/Handler.java create mode 100644 SpigotCore_Main/src/de/steamwar/coms/receiver/PacketHandler.java create mode 100644 SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/PingHandler.java create mode 100644 SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/TestHandler.java create mode 100644 SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java diff --git a/SpigotCore_Main/src/de/steamwar/coms/Packet.java b/SpigotCore_Main/src/de/steamwar/coms/Packet.java new file mode 100644 index 0000000..00320bf --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/coms/Packet.java @@ -0,0 +1,18 @@ +package de.steamwar.coms; + +import de.steamwar.coms.sender.PacketSender; + +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +public abstract class Packet implements Serializable { + + public void send() { + PacketSender.sendPacket(this); + } + + public abstract String getName(); + + public abstract void writeVars(ObjectOutputStream objectOutputStream) throws IOException; +} diff --git a/SpigotCore_Main/src/de/steamwar/coms/receiver/Handler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/Handler.java new file mode 100644 index 0000000..a049560 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/coms/receiver/Handler.java @@ -0,0 +1,10 @@ +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) throws Exception; +} \ 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 new file mode 100644 index 0000000..8edb0d9 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/coms/receiver/PacketHandler.java @@ -0,0 +1,35 @@ +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; + +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 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(); + } + } +} diff --git a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/PingHandler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/PingHandler.java new file mode 100644 index 0000000..9ed52e6 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/PingHandler.java @@ -0,0 +1,29 @@ +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) throws Exception { + 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/receiver/handlers/TestHandler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/TestHandler.java new file mode 100644 index 0000000..93676c2 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/TestHandler.java @@ -0,0 +1,17 @@ +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/sender/PacketSender.java b/SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java new file mode 100644 index 0000000..a3d36f3 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java @@ -0,0 +1,30 @@ +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; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.util.stream.Collectors; + +public class PacketSender { + + public static void sendPacket(Packet packet) { + try { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF(packet.getName()); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); + packet.writeVars(objectOutputStream); + objectOutputStream.flush(); + out.writeUTF(outputStream.toByteArray().toString()); + Bukkit.getOnlinePlayers().stream().limit(1).collect(Collectors.toList()).get(0).sendPluginMessage(Core.getInstance(), "sw:bridge", out.toByteArray()); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ 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 27d799c..9412be6 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -19,6 +19,7 @@ package de.steamwar.core; +import de.steamwar.coms.receiver.PacketHandler; import de.steamwar.core.events.ChattingEvent; import de.steamwar.core.events.ChunkListener; import de.steamwar.core.events.PlayerJoinedEvent; @@ -61,6 +62,9 @@ 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:bridge" ); + } @Override diff --git a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java index 167e5ce..3fd468b 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/DownloadSchematic.java @@ -19,13 +19,14 @@ package de.steamwar.sql; -import javax.xml.bind.DatatypeConverter; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.sql.ResultSet; import java.sql.SQLException; import java.time.Instant; +import javax.xml.bind.DatatypeConverter; + public class DownloadSchematic { private DownloadSchematic(){} -- 2.39.2 From 691abd0deae36930ce14b01c365d517ff6c8c78c Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 16 Sep 2020 21:22:39 +0200 Subject: [PATCH 2/9] 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(); + } } -- 2.39.2 From 00c08fc8add0e4cd987b3df2be5665b87ea52400 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 19 Sep 2020 12:39:22 +0200 Subject: [PATCH 3/9] Add Bungee GUI Prototype --- .../steamwar/coms/receiver/PacketHandler.java | 4 ++ .../handlers/CloseInventoryHandler.java | 21 ++++++++++ .../receiver/handlers/InventoryHandler.java | 23 ++++++---- .../src/de/steamwar/coms/sender/Packet.java | 11 +++-- .../de/steamwar/coms/sender/PacketSender.java | 25 +++-------- .../packets/InventoryCallbackPacket.java | 42 ++++++++++++++----- .../src/de/steamwar/core/Core.java | 4 +- .../src/de/steamwar/inventory/SWItem.java | 4 +- 8 files changed, 88 insertions(+), 46 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/CloseInventoryHandler.java diff --git a/SpigotCore_Main/src/de/steamwar/coms/receiver/PacketHandler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/PacketHandler.java index dd01da8..1f04ad5 100644 --- a/SpigotCore_Main/src/de/steamwar/coms/receiver/PacketHandler.java +++ b/SpigotCore_Main/src/de/steamwar/coms/receiver/PacketHandler.java @@ -2,6 +2,8 @@ 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; @@ -19,6 +21,8 @@ public class PacketHandler implements PluginMessageListener { static { registerHandler(new PingHandler()); + registerHandler(new InventoryHandler()); + registerHandler(new CloseInventoryHandler()); } @Override diff --git a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/CloseInventoryHandler.java b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/CloseInventoryHandler.java new file mode 100644 index 0000000..7843a24 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/CloseInventoryHandler.java @@ -0,0 +1,21 @@ +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 index 346be0e..3eeb8e8 100644 --- a/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/InventoryHandler.java +++ b/SpigotCore_Main/src/de/steamwar/coms/receiver/handlers/InventoryHandler.java @@ -1,6 +1,7 @@ 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; @@ -14,8 +15,6 @@ import java.util.*; public class InventoryHandler extends Handler { - private static HashMap callbacks = new HashMap<>(); - @Override protected String getName() { return "Inventory"; @@ -23,13 +22,12 @@ public class InventoryHandler extends Handler { @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(); + int length = byteArrayDataInput.readInt(); Map items = new HashMap<>(); - for (int i = 0; i < lenght; i++) { + 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")) { @@ -40,14 +38,25 @@ public class InventoryHandler extends Handler { 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).setPosition(object.get("position").getAsInt()).setTitle(title).send(); + 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); + 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/sender/Packet.java b/SpigotCore_Main/src/de/steamwar/coms/sender/Packet.java index fa75de1..f3c56d0 100644 --- a/SpigotCore_Main/src/de/steamwar/coms/sender/Packet.java +++ b/SpigotCore_Main/src/de/steamwar/coms/sender/Packet.java @@ -1,18 +1,17 @@ package de.steamwar.coms.sender; -import de.steamwar.coms.sender.PacketSender; +import com.google.common.io.ByteArrayDataOutput; +import org.bukkit.entity.Player; -import java.io.IOException; -import java.io.ObjectOutputStream; import java.io.Serializable; public abstract class Packet implements Serializable { - public void send() { - PacketSender.sendPacket(this); + public void send(Player player) { + PacketSender.sendPacket(this, player); } public abstract String getName(); - public abstract void writeVars(ObjectOutputStream objectOutputStream) throws IOException; + 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 index d2b75c8..5a78040 100644 --- a/SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java +++ b/SpigotCore_Main/src/de/steamwar/coms/sender/PacketSender.java @@ -3,27 +3,14 @@ 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.Bukkit; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectOutputStream; -import java.util.stream.Collectors; +import org.bukkit.entity.Player; public class PacketSender { - public static void sendPacket(Packet packet) { - try { - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF(packet.getName()); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); - packet.writeVars(objectOutputStream); - objectOutputStream.flush(); - out.writeUTF(outputStream.toByteArray().toString()); - Bukkit.getOnlinePlayers().stream().limit(1).collect(Collectors.toList()).get(0).sendPluginMessage(Core.getInstance(), "sw:bridge", out.toByteArray()); - } catch (IOException e) { - e.printStackTrace(); - } + 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/coms/sender/packets/InventoryCallbackPacket.java b/SpigotCore_Main/src/de/steamwar/coms/sender/packets/InventoryCallbackPacket.java index 698f535..97aa2bc 100644 --- a/SpigotCore_Main/src/de/steamwar/coms/sender/packets/InventoryCallbackPacket.java +++ b/SpigotCore_Main/src/de/steamwar/coms/sender/packets/InventoryCallbackPacket.java @@ -1,15 +1,17 @@ package de.steamwar.coms.sender.packets; +import com.google.common.io.ByteArrayDataOutput; import de.steamwar.coms.sender.Packet; import de.steamwar.inventory.SWItem; - -import java.io.ObjectOutputStream; +import org.bukkit.event.inventory.ClickType; public class InventoryCallbackPacket extends Packet { - String title; int position; SWItem item; + ClickType clickType; + CallbackType callbackType; + String owner; @Override public String getName() { @@ -17,13 +19,13 @@ public class InventoryCallbackPacket extends Packet { } @Override - public void writeVars(ObjectOutputStream objectOutputStream) { - - } - - public InventoryCallbackPacket setTitle(String title) { - this.title = title; - return this; + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + byteArrayDataOutput.writeUTF(owner); + byteArrayDataOutput.writeUTF(callbackType.name()); + if(item != null){ + byteArrayDataOutput.writeUTF(item.parseToJson(position)); + byteArrayDataOutput.writeUTF(clickType.name()); + } } public InventoryCallbackPacket setPosition(int position) { @@ -35,4 +37,24 @@ public class InventoryCallbackPacket extends Packet { this.item = item; return this; } + + public InventoryCallbackPacket setClick(ClickType type){ + this.clickType = type; + return this; + } + + public InventoryCallbackPacket setCallback(CallbackType callback) { + callbackType = callback; + return this; + } + + public InventoryCallbackPacket setOwner(String uuid) { + this.owner = uuid; + return this; + } + + public enum CallbackType { + CLICK, + CLOSE, + } } diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index 9412be6..cf2b126 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -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:bridge" ); + getServer().getMessenger().registerIncomingPluginChannel(this, "sw:bridge", new PacketHandler()); + getServer().getMessenger().registerOutgoingPluginChannel(this, "sw:return"); } diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java index c241792..250d71e 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java @@ -205,7 +205,7 @@ public class SWItem { object.addProperty("enchanted", true); if(itemMeta.hasItemFlag(ItemFlag.HIDE_ENCHANTS)) object.addProperty("hideAttributes", true); - if(!itemMeta.getLore().isEmpty()) { + if(itemMeta.getLore() != null) { JsonArray array = new JsonArray(); for (String lores: itemMeta.getLore()) { @@ -214,6 +214,6 @@ public class SWItem { object.add("lore", array); } - return object.getAsString(); + return object.toString(); } } -- 2.39.2 From 2155e386fd8ecfc8776c2d4a5f1b720d16278c15 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 20 Sep 2020 12:01:12 +0200 Subject: [PATCH 4/9] 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(); - } } -- 2.39.2 From 09aba7244e1471a74b41a3e2526f4272a2bb7a64 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 20 Sep 2020 12:01:50 +0200 Subject: [PATCH 5/9] Remove Debug Message --- .../src/de/steamwar/comms/handlers/InventoryHandler.java | 1 - 1 file changed, 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java index 2883eea..7c217e6 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java +++ b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java @@ -18,7 +18,6 @@ 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(); -- 2.39.2 From fc284037507a2aed709433fcc649beb47d58a1a5 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 20 Sep 2020 12:14:36 +0200 Subject: [PATCH 6/9] Add License --- .../src/de/steamwar/comms/BungeeReceiver.java | 19 +++++++++++++++++++ .../comms/handlers/BungeeHandler.java | 19 +++++++++++++++++++ .../comms/handlers/InventoryHandler.java | 19 +++++++++++++++++++ .../packets/InventoryCallbackPacket.java | 19 +++++++++++++++++++ .../steamwar/comms/packets/SpigotPacket.java | 19 +++++++++++++++++++ .../src/de/steamwar/inventory/SWItem.java | 2 -- 6 files changed, 95 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java index 377c372..8666ad0 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java +++ b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java @@ -1,3 +1,22 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package de.steamwar.comms; import com.google.common.io.ByteArrayDataInput; diff --git a/SpigotCore_Main/src/de/steamwar/comms/handlers/BungeeHandler.java b/SpigotCore_Main/src/de/steamwar/comms/handlers/BungeeHandler.java index 474ca5e..8907fee 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/handlers/BungeeHandler.java +++ b/SpigotCore_Main/src/de/steamwar/comms/handlers/BungeeHandler.java @@ -1,3 +1,22 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package de.steamwar.comms.handlers; import com.google.common.io.ByteArrayDataInput; diff --git a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java index 7c217e6..9c3ccd6 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java +++ b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java @@ -1,3 +1,22 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package de.steamwar.comms.handlers; import com.google.common.io.ByteArrayDataInput; diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java index 254b15d..d78f398 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java @@ -1,3 +1,22 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package de.steamwar.comms.packets; import com.google.common.io.ByteArrayDataOutput; diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java index e602717..b286c13 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java @@ -1,3 +1,22 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package de.steamwar.comms.packets; import com.google.common.io.ByteArrayDataOutput; diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java index d62d57a..55aa8aa 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java @@ -19,8 +19,6 @@ 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; -- 2.39.2 From a8f88935df6d63f008251778fa9c61a5da25d78f Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 21 Sep 2020 23:22:50 +0200 Subject: [PATCH 7/9] Code Cleanup --- .../src/de/steamwar/comms/BungeeReceiver.java | 17 +++++++---------- .../src/de/steamwar/comms/PacketIdManager.java | 11 +++++++++++ .../comms/packets/InventoryCallbackPacket.java | 7 ++++--- .../de/steamwar/comms/packets/SpigotPacket.java | 2 +- 4 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java diff --git a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java index 8666ad0..7c1ba11 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java +++ b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java @@ -36,18 +36,14 @@ import java.util.UUID; public class BungeeReceiver implements PluginMessageListener { - private static Map handlerMap = new HashMap<>(); + private static Map handlerMap = new HashMap<>(); - public static void registerHandler(int code, BungeeHandler handler) { + public static void registerHandler(Byte code, BungeeHandler handler) { handlerMap.put(code, handler); } - public static void registerHandler(String name, BungeeHandler handler) { - handlerMap.put(name.hashCode(), handler); - } - static { - registerHandler("PingHandler", byteArrayDataInput -> { + registerHandler(PacketIdManager.PING_PACKET, byteArrayDataInput -> { UUID uuid = SteamwarUser.get(byteArrayDataInput.readInt()).getUUID(); if(Bukkit.getPlayer(uuid).isOnline()) { Player player = Bukkit.getPlayer(uuid); @@ -55,8 +51,8 @@ public class BungeeReceiver implements PluginMessageListener { } }); - registerHandler("InventoryHandler", new InventoryHandler()); - registerHandler("CloseInventory", byteArrayDataInput -> { + registerHandler(PacketIdManager.INVENTORY_PACKET, new InventoryHandler()); + registerHandler(PacketIdManager.INVENTORY_CLOSE_PACKET, byteArrayDataInput -> { Player player = Bukkit.getPlayer(SteamwarUser.get(byteArrayDataInput.readInt()).getUUID()); player.closeInventory(); }); @@ -65,6 +61,7 @@ public class BungeeReceiver implements PluginMessageListener { @Override public void onPluginMessageReceived(String s, Player player, byte[] bytes) { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); - handlerMap.get(in.readInt()).handle(in); + Byte handler = in.readByte(); + handlerMap.get(handler).handle(in); } } diff --git a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java new file mode 100644 index 0000000..fb12d5c --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java @@ -0,0 +1,11 @@ +package de.steamwar.comms; + +public class PacketIdManager { + + //0x0(X) Standalone Packets + public final static byte PING_PACKET = 0x01; + //0x1(X) Bungee Inventory + public final static byte INVENTORY_PACKET = 0x10; + public final static byte INVENTORY_CALLBACK_PACKET = 0x11; + public final static byte INVENTORY_CLOSE_PACKET = 0x12; +} diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java index d78f398..0883033 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java @@ -20,25 +20,26 @@ package de.steamwar.comms.packets; import com.google.common.io.ByteArrayDataOutput; +import de.steamwar.comms.PacketIdManager; import org.bukkit.event.inventory.ClickType; public class InventoryCallbackPacket extends SpigotPacket { - int position; + int position = -1; ClickType clickType; CallbackType callbackType; int owner; @Override public int getName() { - return "InventoryCallback".hashCode(); + return PacketIdManager.INVENTORY_CALLBACK_PACKET; } @Override public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { byteArrayDataOutput.writeInt(owner); byteArrayDataOutput.writeUTF(callbackType.name()); - if(position != 0){ + if(position != -1){ byteArrayDataOutput.writeInt(position); byteArrayDataOutput.writeUTF(clickType.name()); } diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java index b286c13..25b5ecb 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java @@ -28,7 +28,7 @@ public abstract class SpigotPacket { public void send(Player player) { ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeInt(getName()); + out.writeByte(getName()); writeVars(out); player.sendPluginMessage(Core.getInstance(), "sw:bridge", out.toByteArray()); } -- 2.39.2 From ee7e68eaaecfd821da5f82012723a595b6f35b05 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 24 Sep 2020 14:09:19 +0200 Subject: [PATCH 8/9] Code Cleanup --- .../de/steamwar/comms/PacketIdManager.java | 19 ++++++++++++ .../comms/handlers/InventoryHandler.java | 30 ++----------------- .../packets/InventoryCallbackPacket.java | 18 +++++++++++ .../src/de/steamwar/inventory/SWItem.java | 27 +++++++++++++++++ 4 files changed, 67 insertions(+), 27 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java index fb12d5c..27dbd81 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java +++ b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java @@ -1,3 +1,22 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package de.steamwar.comms; public class PacketIdManager { diff --git a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java index 9c3ccd6..74dd9e4 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java +++ b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java @@ -27,7 +27,6 @@ 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.*; @@ -45,38 +44,15 @@ public class InventoryHandler implements BungeeHandler { 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); - } + SWItem item = SWItem.getItemFromJson(itemJson); 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)); + new InventoryCallbackPacket(itemJson, click, player).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.addCloseCallback(click -> new InventoryCallbackPacket(player).send(Bukkit.getPlayer(player))); inventory.open(); } } diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java index 0883033..6ba1c42 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java @@ -20,9 +20,13 @@ package de.steamwar.comms.packets; import com.google.common.io.ByteArrayDataOutput; +import com.google.gson.JsonObject; import de.steamwar.comms.PacketIdManager; +import de.steamwar.sql.SteamwarUser; import org.bukkit.event.inventory.ClickType; +import java.util.UUID; + public class InventoryCallbackPacket extends SpigotPacket { int position = -1; @@ -45,6 +49,20 @@ public class InventoryCallbackPacket extends SpigotPacket { } } + public InventoryCallbackPacket() {} + + public InventoryCallbackPacket(JsonObject itemJson, ClickType click, UUID player) { + setPosition(itemJson.get("position").getAsInt()); + setClick(click); + setCallback(InventoryCallbackPacket.CallbackType.CLICK); + setOwner(SteamwarUser.get(player).getId()); + } + + public InventoryCallbackPacket(UUID player) { + setCallback(CallbackType.CLOSE); + setOwner(SteamwarUser.get(player).getId()); + } + public InventoryCallbackPacket setPosition(int position) { this.position = position; return this; diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java index 55aa8aa..d77ffab 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,6 +139,31 @@ public class SWItem { callback = c; } + public static SWItem getItemFromJson(JsonObject itemJson) { + 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); + } + return item; + } + private void hideAttributes() { if (itemMeta == null) return; itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); -- 2.39.2 From fddfe288dcd1b409734abb1860233eec93521be4 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 24 Sep 2020 14:11:51 +0200 Subject: [PATCH 9/9] Code Cleanup --- .../src/de/steamwar/comms/handlers/InventoryHandler.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java index 74dd9e4..433aa86 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java +++ b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java @@ -45,9 +45,7 @@ public class InventoryHandler implements BungeeHandler { for (int i = 0; i < length; i++) { JsonObject itemJson = array.get(i).getAsJsonObject(); SWItem item = SWItem.getItemFromJson(itemJson); - item.setCallback(click -> { - new InventoryCallbackPacket(itemJson, click, player).send(Bukkit.getPlayer(player)); - }); + item.setCallback(click -> new InventoryCallbackPacket(itemJson, click, player).send(Bukkit.getPlayer(player))); items.put(itemJson.get("position").getAsInt(), item); } -- 2.39.2