From 7559012a9bfc707088f30d408e4c511e329dcdc9 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 20 Sep 2020 12:02:56 +0200 Subject: [PATCH] Improve Bungee GUI Add SWListInv for Schem GUI --- src/de/steamwar/bungeecore/BungeeCore.java | 8 +- .../bungeecore/commands/TestCommand.java | 6 +- .../BungeePacket.java} | 30 +++-- .../bungeecore/comms/SpigotHandler.java | 8 ++ .../bungeecore/comms/SpigotReceiver.java | 37 +++++ .../handlers/InventoryCallbackHandler.java | 33 +++++ .../packets/CloseInventoryPacket.java | 13 +- .../comms/packets/InventoryPacket.java | 51 +++++++ .../bungeecore/comms/packets/PingPacket.java | 24 ++++ .../packets/inventory/InvCallback.java | 5 +- .../comms/packets/inventory/SWInventory.java | 90 +++++++++++++ .../packets/inventory/SWItem.java | 67 +++++----- .../comms/packets/inventory/SWListInv.java | 126 ++++++++++++++++++ .../handlers/InventoryCallbackHandler.java | 38 ------ .../coms/packets/InventoryPacket.java | 53 -------- .../bungeecore/coms/packets/PingPacket.java | 24 ---- .../coms/packets/inventory/SWInventory.java | 75 ----------- .../bungeecore/coms/receiver/Handler.java | 10 -- .../coms/receiver/PacketHandler.java | 33 ----- .../bungeecore/coms/sender/Packet.java | 21 --- .../bungeecore/listeners/ChatListener.java | 4 +- src/de/steamwar/bungeecore/sql/Schematic.java | 26 ++++ .../bungeecore/sql/SchematicMember.java | 115 ++++++++++++++++ 23 files changed, 579 insertions(+), 318 deletions(-) rename src/de/steamwar/bungeecore/{coms/sender/PacketSender.java => comms/BungeePacket.java} (66%) create mode 100644 src/de/steamwar/bungeecore/comms/SpigotHandler.java create mode 100644 src/de/steamwar/bungeecore/comms/SpigotReceiver.java create mode 100644 src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java rename src/de/steamwar/bungeecore/{coms => comms}/packets/CloseInventoryPacket.java (51%) create mode 100644 src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java create mode 100644 src/de/steamwar/bungeecore/comms/packets/PingPacket.java rename src/de/steamwar/bungeecore/{coms => comms}/packets/inventory/InvCallback.java (91%) create mode 100644 src/de/steamwar/bungeecore/comms/packets/inventory/SWInventory.java rename src/de/steamwar/bungeecore/{coms => comms}/packets/inventory/SWItem.java (68%) create mode 100644 src/de/steamwar/bungeecore/comms/packets/inventory/SWListInv.java delete mode 100644 src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java delete mode 100644 src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java delete mode 100644 src/de/steamwar/bungeecore/coms/packets/PingPacket.java delete mode 100644 src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java delete mode 100644 src/de/steamwar/bungeecore/coms/receiver/Handler.java delete mode 100644 src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java delete mode 100644 src/de/steamwar/bungeecore/coms/sender/Packet.java create mode 100644 src/de/steamwar/bungeecore/sql/SchematicMember.java diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index 036cfcf5..93076b76 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -20,7 +20,8 @@ package de.steamwar.bungeecore; import de.steamwar.bungeecore.commands.*; -import de.steamwar.bungeecore.coms.receiver.PacketHandler; +import de.steamwar.bungeecore.comms.handlers.InventoryCallbackHandler; +import de.steamwar.bungeecore.comms.SpigotReceiver; import de.steamwar.bungeecore.listeners.*; import de.steamwar.bungeecore.listeners.mods.Fabric; import de.steamwar.bungeecore.listeners.mods.Forge; @@ -43,9 +44,7 @@ import net.md_5.bungee.config.YamlConfiguration; import org.yaml.snakeyaml.error.YAMLException; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.nio.charset.MalformedInputException; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.List; @@ -75,7 +74,6 @@ public class BungeeCore extends Plugin { public void onEnable(){ getProxy().registerChannel("sw:bridge"); - getProxy().registerChannel("sw:return"); setInstance(this); loadConfig(); @@ -134,7 +132,7 @@ public class BungeeCore extends Plugin { new EventStarter(); new Broadcaster(); new SessionManager(); - new PacketHandler(); + new SpigotReceiver(); new TablistManager(); diff --git a/src/de/steamwar/bungeecore/commands/TestCommand.java b/src/de/steamwar/bungeecore/commands/TestCommand.java index a7c18cad..77d8419f 100644 --- a/src/de/steamwar/bungeecore/commands/TestCommand.java +++ b/src/de/steamwar/bungeecore/commands/TestCommand.java @@ -1,7 +1,7 @@ package de.steamwar.bungeecore.commands; -import de.steamwar.bungeecore.coms.packets.inventory.SWInventory; -import de.steamwar.bungeecore.coms.packets.inventory.SWItem; +import de.steamwar.bungeecore.comms.packets.inventory.SWInventory; +import de.steamwar.bungeecore.comms.packets.inventory.SWItem; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -16,7 +16,7 @@ public class TestCommand extends BasicCommand { if(!(commandSender instanceof ProxiedPlayer)) return; SWInventory inventory = new SWInventory((ProxiedPlayer) commandSender, 3*9, "Test Inventory"); - SWItem item = new SWItem("DIRT", 13, "§e§lTest Item"); + SWItem item = new SWItem("DIRT1", 13, "§e§lTest Item"); item.setSkullOwner(commandSender.getName()); for (ProxiedPlayer player: ((ProxiedPlayer) commandSender).getServer().getInfo().getPlayers()) { diff --git a/src/de/steamwar/bungeecore/coms/sender/PacketSender.java b/src/de/steamwar/bungeecore/comms/BungeePacket.java similarity index 66% rename from src/de/steamwar/bungeecore/coms/sender/PacketSender.java rename to src/de/steamwar/bungeecore/comms/BungeePacket.java index 2a7e8da9..1233b302 100644 --- a/src/de/steamwar/bungeecore/coms/sender/PacketSender.java +++ b/src/de/steamwar/bungeecore/comms/BungeePacket.java @@ -1,4 +1,4 @@ -package de.steamwar.bungeecore.coms.sender; +package de.steamwar.bungeecore.comms; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; @@ -7,23 +7,27 @@ import net.md_5.bungee.api.connection.Server; import java.io.ByteArrayOutputStream; -public class PacketSender { +public abstract class BungeePacket { - public static void sendPacket(Packet packet, ProxiedPlayer player) { + public void send(Server server) { ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF(packet.getName()); + out.writeInt(getId()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - packet.writeVars(out); + writeVars(out); + out.writeUTF(outputStream.toByteArray().toString()); + server.sendData("sw:bridge", out.toByteArray()); + } + + public void send(ProxiedPlayer player) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeInt(getId()); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + writeVars(out); out.writeUTF(outputStream.toByteArray().toString()); player.getServer().sendData("sw:bridge", out.toByteArray()); } - public static void sendPacket(Packet packet, Server server) { - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF(packet.getName()); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - packet.writeVars(out); - out.writeUTF(outputStream.toByteArray().toString()); - server.sendData("sw:bridge", out.toByteArray()); - } + public abstract int getId(); + + public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput); } diff --git a/src/de/steamwar/bungeecore/comms/SpigotHandler.java b/src/de/steamwar/bungeecore/comms/SpigotHandler.java new file mode 100644 index 00000000..fafc9976 --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/SpigotHandler.java @@ -0,0 +1,8 @@ +package de.steamwar.bungeecore.comms; + +import com.google.common.io.ByteArrayDataInput; + +public interface SpigotHandler { + + void handle(ByteArrayDataInput byteArrayDataInput); +} diff --git a/src/de/steamwar/bungeecore/comms/SpigotReceiver.java b/src/de/steamwar/bungeecore/comms/SpigotReceiver.java new file mode 100644 index 00000000..2a5521ce --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/SpigotReceiver.java @@ -0,0 +1,37 @@ +package de.steamwar.bungeecore.comms; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteStreams; +import de.steamwar.bungeecore.comms.handlers.InventoryCallbackHandler; +import de.steamwar.bungeecore.listeners.BasicListener; +import net.md_5.bungee.api.event.PluginMessageEvent; +import net.md_5.bungee.event.EventHandler; + +import java.util.HashMap; +import java.util.Map; + +public class SpigotReceiver extends BasicListener { + + private static final Map handlerMap = new HashMap<>(); + + @EventHandler + public void onPluginMessage(PluginMessageEvent event) { + if(!event.getTag().equalsIgnoreCase("sw:bridge")) + return; + ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); + int handler = in.readInt(); + handlerMap.get(handler).handle(in); + } + + public static void registerHandler(int id, SpigotHandler handler) { + handlerMap.put(id, handler); + } + + public static void registerHandler(String name, SpigotHandler handler) { + handlerMap.put(name.hashCode(), handler); + } + + static { + registerHandler("InventoryCallback", new InventoryCallbackHandler()); + } +} diff --git a/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java b/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java new file mode 100644 index 00000000..15eb08b8 --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java @@ -0,0 +1,33 @@ +package de.steamwar.bungeecore.comms.handlers; + +import com.google.common.io.ByteArrayDataInput; +import de.steamwar.bungeecore.comms.SpigotHandler; +import de.steamwar.bungeecore.comms.packets.inventory.InvCallback; +import de.steamwar.bungeecore.comms.packets.inventory.SWInventory; +import de.steamwar.bungeecore.sql.SteamwarUser; + +import java.util.HashMap; +import java.util.Map; + +public class InventoryCallbackHandler implements SpigotHandler { + + public static final Map inventoryHashMap = new HashMap<>(); + + @Override + public void handle(ByteArrayDataInput byteArrayDataInput) { + SteamwarUser owner = SteamwarUser.get(byteArrayDataInput.readInt()); + CallbackType type = CallbackType.valueOf(byteArrayDataInput.readUTF()); + if(type == CallbackType.CLICK) { + int pos = byteArrayDataInput.readInt(); + InvCallback.ClickType clickType = InvCallback.ClickType.valueOf(byteArrayDataInput.readUTF()); + inventoryHashMap.get(owner.getId()).handleCallback(clickType, pos); + }else if(type == CallbackType.CLOSE) { + inventoryHashMap.get(owner.getId()).handleClose(); + } + } + + public enum CallbackType { + CLICK, + CLOSE, + } +} diff --git a/src/de/steamwar/bungeecore/coms/packets/CloseInventoryPacket.java b/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java similarity index 51% rename from src/de/steamwar/bungeecore/coms/packets/CloseInventoryPacket.java rename to src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java index 4fb85170..e84f6dae 100644 --- a/src/de/steamwar/bungeecore/coms/packets/CloseInventoryPacket.java +++ b/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java @@ -1,21 +1,22 @@ -package de.steamwar.bungeecore.coms.packets; +package de.steamwar.bungeecore.comms.packets; import com.google.common.io.ByteArrayDataOutput; -import de.steamwar.bungeecore.coms.sender.Packet; +import de.steamwar.bungeecore.comms.BungeePacket; +import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.connection.ProxiedPlayer; -public class CloseInventoryPacket extends Packet { +public class CloseInventoryPacket extends BungeePacket { ProxiedPlayer player; @Override - public String getName() { - return "InvClose"; + public int getId() { + return "CloseInventory".hashCode(); } @Override public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - byteArrayDataOutput.writeUTF(player.getUniqueId().toString()); + byteArrayDataOutput.writeInt(SteamwarUser.get(player).getId()); } public CloseInventoryPacket setPlayer(ProxiedPlayer player) { diff --git a/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java b/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java new file mode 100644 index 00000000..16417905 --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java @@ -0,0 +1,51 @@ +package de.steamwar.bungeecore.comms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import de.steamwar.bungeecore.comms.packets.inventory.SWItem; +import de.steamwar.bungeecore.comms.BungeePacket; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.HashMap; + +public class InventoryPacket extends BungeePacket { + + String title; + int player; + int size; + HashMap items; + + public InventoryPacket(int size, String title, ProxiedPlayer player) { + items = new HashMap<>(); + this.title = title; + this.size = size; + this.player = SteamwarUser.get(player).getId(); + } + + @Override + public int getId() { + return "InventoryHandler".hashCode(); + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + JsonObject object = new JsonObject(); + object.addProperty("id", player); + object.addProperty("title", title); + object.addProperty("size", size); + object.addProperty("itemcount", items.size()); + JsonArray array = new JsonArray(); + for (int i = 0; i < size; i++) { + if(items.get(i) != null) + array.add(items.get(i).writeToString()); + } + object.add("items", array); + byteArrayDataOutput.writeUTF(object.toString()); + } + + public void setItem(int index, SWItem item) { + items.put(index, item); + } +} diff --git a/src/de/steamwar/bungeecore/comms/packets/PingPacket.java b/src/de/steamwar/bungeecore/comms/packets/PingPacket.java new file mode 100644 index 00000000..ba435e67 --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/packets/PingPacket.java @@ -0,0 +1,24 @@ +package de.steamwar.bungeecore.comms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import de.steamwar.bungeecore.comms.BungeePacket; + +public class PingPacket extends BungeePacket { + + int id; + + @Override + public int getId() { + return "PingHandler".hashCode(); + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + byteArrayDataOutput.writeInt(id); + } + + public PingPacket setUser(int id) { + this.id = id; + return this; + } +} diff --git a/src/de/steamwar/bungeecore/coms/packets/inventory/InvCallback.java b/src/de/steamwar/bungeecore/comms/packets/inventory/InvCallback.java similarity index 91% rename from src/de/steamwar/bungeecore/coms/packets/inventory/InvCallback.java rename to src/de/steamwar/bungeecore/comms/packets/inventory/InvCallback.java index a3a03658..71fd7672 100644 --- a/src/de/steamwar/bungeecore/coms/packets/inventory/InvCallback.java +++ b/src/de/steamwar/bungeecore/comms/packets/inventory/InvCallback.java @@ -1,4 +1,4 @@ -package de.steamwar.bungeecore.coms.packets.inventory; +package de.steamwar.bungeecore.comms.packets.inventory; public interface InvCallback { @@ -19,9 +19,6 @@ public interface InvCallback { CREATIVE, UNKNOWN; - private ClickType() { - } - public boolean isKeyboardClick() { return this == NUMBER_KEY || this == DROP || this == CONTROL_DROP; } diff --git a/src/de/steamwar/bungeecore/comms/packets/inventory/SWInventory.java b/src/de/steamwar/bungeecore/comms/packets/inventory/SWInventory.java new file mode 100644 index 00000000..7b4c800a --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/packets/inventory/SWInventory.java @@ -0,0 +1,90 @@ +package de.steamwar.bungeecore.comms.packets.inventory; + +import de.steamwar.bungeecore.comms.handlers.InventoryCallbackHandler; +import de.steamwar.bungeecore.comms.packets.CloseInventoryPacket; +import de.steamwar.bungeecore.comms.packets.InventoryPacket; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.HashMap; +import java.util.Map; + +public class SWInventory { + + private final Map itemMap; + private InvCallback close; + private ProxiedPlayer player; + private int size; + private String title; + + public SWInventory(ProxiedPlayer proxiedPlayer, int size, String title) { + itemMap = new HashMap<>(); + InventoryCallbackHandler.inventoryHashMap.put(SteamwarUser.get(proxiedPlayer).getId(), this); + this.player = proxiedPlayer; + this.size = size; + this.title = title; + } + + public void addItem(SWItem item) { + itemMap.put(item.getPosition(), item); + } + + public void addItem(int position, SWItem item) { + itemMap.put(position, item); + } + + public void addItem(int pos, SWItem item, InvCallback callback) { + item.setCallback(callback); + itemMap.put(pos, item); + } + + public void addItem(int pos, SWItem item, String name, InvCallback callback) { + item.setCallback(callback); + itemMap.put(pos, item); + } + + public void removeItem(SWItem item) { + itemMap.remove(item.getPosition()); + } + + public void removeItem(int position) { + itemMap.remove(position); + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setCallback(int pos, InvCallback callback) { + itemMap.get(pos).setCallback(callback); + } + + public void handleCallback(InvCallback.ClickType type, int pos) { + itemMap.get(pos).getCallback().clicked(type); + } + + public void handleClose() { + if(close != null) + close.clicked(null); + } + + public void open() { + InventoryPacket inv = new InventoryPacket(size, title, player); + for (Map.Entry entry : itemMap.entrySet()) { + inv.setItem(entry.getKey(), entry.getValue()); + } + inv.send(player); + } + + public void close() { + new CloseInventoryPacket().setPlayer(player).send(player); + } +} diff --git a/src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java b/src/de/steamwar/bungeecore/comms/packets/inventory/SWItem.java similarity index 68% rename from src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java rename to src/de/steamwar/bungeecore/comms/packets/inventory/SWItem.java index 276c06d6..e2b58421 100644 --- a/src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java +++ b/src/de/steamwar/bungeecore/comms/packets/inventory/SWItem.java @@ -1,51 +1,50 @@ -package de.steamwar.bungeecore.coms.packets.inventory; +package de.steamwar.bungeecore.comms.packets.inventory; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import java.util.ArrayList; import java.util.List; public class SWItem { - String material, title, skullOwner; - boolean enchanted, hideAttributes; - int position; - List lore; - InvCallback callback; + private String material, title, skullOwner; + private boolean enchanted, hideAttributes; + private int position; + private List lore; + private InvCallback callback; + private int color; public SWItem(String material, int position, String title) { this.material = material.toUpperCase(); lore = new ArrayList<>(); this.position = position; this.title = title; + color = 0; } - private SWItem(String json) { - JsonObject object = new JsonParser().parse(json).getAsJsonObject(); - material = object.get("material").getAsString(); - position = object.get("position").getAsInt(); - title = object.get("title").getAsString(); - if(object.has("skullOwner")) { - skullOwner = object.get("skullOwner").getAsString(); - } - if(object.has("enchanted")) - enchanted = true; - if(object.has("hideAttributes")) - hideAttributes = true; - if(object.has("lore")) { - List lore = new ArrayList<>(); - JsonArray array = object.getAsJsonArray("lore"); - array.forEach(jsonElement -> lore.add(jsonElement.getAsString())); - this.lore = lore; - } + public SWItem(int position, String title, int color) { + this.material = "DYE"; + lore = new ArrayList<>(); + this.position = position; + this.title = title; + this.color = color; + } + + public static SWItem getSkull(String skullOwner) { + SWItem item = new SWItem("SKULL", 1, skullOwner); + item.setSkullOwner(skullOwner); + return item; } public void setCallback(InvCallback callback) { this.callback = callback; } + public void setColor(int color) { + this.color = color; + } + public InvCallback getCallback() { return callback; } @@ -82,11 +81,19 @@ public class SWItem { this.hideAttributes = hideAttributes; } + public int getPosition() { + return position; + } + + public void setName(String name) { + title = name; + } + public void addLore(String lore) { this.lore.add(lore); } - public String writeToString() { + public JsonObject writeToString() { JsonObject object = new JsonObject(); object.addProperty("material", material); object.addProperty("position", position); @@ -97,6 +104,8 @@ public class SWItem { object.addProperty("enchanted", true); if(hideAttributes) object.addProperty("hideAttributes", true); + if(color != 0) + object.addProperty("color", color); if(lore != null) { JsonArray array = new JsonArray(); for (String lores: @@ -106,11 +115,7 @@ public class SWItem { object.add("lore", array); } - return object.toString(); - } - - public static SWItem getFromJson(String json) { - return new SWItem(json); + return object; } @Override diff --git a/src/de/steamwar/bungeecore/comms/packets/inventory/SWListInv.java b/src/de/steamwar/bungeecore/comms/packets/inventory/SWListInv.java new file mode 100644 index 00000000..b3e866be --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/packets/inventory/SWListInv.java @@ -0,0 +1,126 @@ +package de.steamwar.bungeecore.comms.packets.inventory; + +import de.steamwar.bungeecore.sql.Schematic; +import de.steamwar.bungeecore.sql.SchematicType; +import net.md_5.bungee.BungeeCord; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.*; + +public class SWListInv extends SWInventory { + private ListCallback callback; + private List> elements; + private int page; + + public SWListInv(ProxiedPlayer p, String t, List> l, ListCallback c){ + super(p, (l.size()>45) ? 54 : (l.size() + 9-l.size()%9), t); + callback = c; + elements = l; + page = 0; + } + + @Override + public void open(){ + setCallback(-999, (InvCallback.ClickType click) -> close()); + if(elements.size() > 54){ + if(page != 0) + addItem(45, new SWItem(45, "§eSeite zurück", 10), (InvCallback.ClickType click) -> { + page--; + open(); + }); + else + addItem(45, new SWItem(45, "§7Seite zurück", 8), (InvCallback.ClickType click) -> {}); + if(page < elements.size()/45) + addItem(53, new SWItem( 53, "§eSeite vor", 10), "§eSeite vor", (InvCallback.ClickType click) -> { + page++; + open(); + }); + else + addItem(53, new SWItem(53, "§eSeite vor", 8), (InvCallback.ClickType click) -> {}); + } + + int ipageLimit = elements.size() - page*45; + if(ipageLimit > 45 && elements.size() > 54){ + ipageLimit = 45; + } + int i = page*45; + for(int ipage=0; ipage < ipageLimit; ipage++ ){ + SWItem e = elements.get(i).getItem(); + + final int pos = i; + addItem(ipage, e); + setCallback(ipage, (InvCallback.ClickType click) -> callback.clicked(click, elements.get(pos).getObject())); + i++; + } + super.open(); + } + + public void setCallback(ListCallback c){ + callback = c; + } + + public interface ListCallback{ + void clicked(InvCallback.ClickType click, T element); + } + + public static List> createPlayerList(UUID without){ + List> onlinePlayers = new ArrayList<>(); + for(ProxiedPlayer player : BungeeCord.getInstance().getPlayer(without).getServer().getInfo().getPlayers()){ + if(without != null && player.getUniqueId().equals(without)) + continue; + + onlinePlayers.add(new SWListEntry<>(SWItem.getSkull(player.getName()), player.getUniqueId())); + } + return onlinePlayers; + } + + public static List> createGlobalPlayerList(UUID without){ + List> onlinePlayers = new ArrayList<>(); + for(ProxiedPlayer player : BungeeCord.getInstance().getPlayers()){ + if(without != null && player.getUniqueId().equals(without)) + continue; + + onlinePlayers.add(new SWListEntry<>(SWItem.getSkull(player.getName()), player.getUniqueId())); + } + return onlinePlayers; + } + + public static List> getSchemList(SchematicType type, int steamwarUserId){ + List> schemList = new ArrayList<>(); + + List schems; + if(type == null) + schems = Schematic.getSchemsAccessibleByUser(steamwarUserId); + else + schems = Schematic.getSchemsOfType(steamwarUserId, type); + + for(Schematic s : schems){ + String m; + if(s.getSchemItem().isEmpty()) + m = "CAULDRON_ITEM"; + else + m = s.getSchemItem(); + SWItem item = new SWItem(m, 0, "§e" + s.getSchemName()); + schemList.add(new SWListEntry<>(item, s)); + } + return schemList; + } + + public static class SWListEntry{ + final SWItem item; + final T object; + + public SWListEntry(SWItem item, T object){ + this.item = item; + this.object = object; + } + + public SWItem getItem(){ + return item; + } + + public T getObject(){ + return object; + } + } +} diff --git a/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java b/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java deleted file mode 100644 index 9a31f683..00000000 --- a/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java +++ /dev/null @@ -1,38 +0,0 @@ -package de.steamwar.bungeecore.coms.handlers; - -import com.google.common.io.ByteArrayDataInput; -import de.steamwar.bungeecore.coms.packets.inventory.InvCallback; -import de.steamwar.bungeecore.coms.packets.inventory.SWInventory; -import de.steamwar.bungeecore.coms.packets.inventory.SWItem; -import de.steamwar.bungeecore.coms.receiver.Handler; - -import java.util.HashMap; -import java.util.Map; - -public class InventoryCallbackHandler extends Handler { - - public static final Map inventoryHashMap = new HashMap<>(); - - @Override - protected String getName() { - return "InventoryCallback"; - } - - @Override - public void handle(ByteArrayDataInput byteArrayDataInput) { - String owner = byteArrayDataInput.readUTF(); - CallbackType type = CallbackType.valueOf(byteArrayDataInput.readUTF()); - if(type == CallbackType.CLICK) { - SWItem item = SWItem.getFromJson(byteArrayDataInput.readUTF()); - InvCallback.ClickType clickType = InvCallback.ClickType.valueOf(byteArrayDataInput.readUTF()); - inventoryHashMap.get(owner).handleCallback(clickType, item); - }else if(type == CallbackType.CLOSE) { - inventoryHashMap.get(owner).handleClose(); - } - } - - public enum CallbackType { - CLICK, - CLOSE, - } -} diff --git a/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java b/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java deleted file mode 100644 index 9becf90a..00000000 --- a/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java +++ /dev/null @@ -1,53 +0,0 @@ -package de.steamwar.bungeecore.coms.packets; - -import com.google.common.io.ByteArrayDataOutput; -import de.steamwar.bungeecore.coms.packets.inventory.SWItem; -import de.steamwar.bungeecore.coms.sender.Packet; -import net.md_5.bungee.api.connection.ProxiedPlayer; - -import java.util.HashMap; - -public class InventoryPacket extends Packet { - - String title; - String player; - int size; - HashMap items; - - public InventoryPacket(int size, String title, ProxiedPlayer player) { - items = new HashMap<>(); - this.title = title; - this.size = size; - this.player = player.getUniqueId().toString(); - } - - @Override - public String getName() { - return "Inventory"; - } - - @Override - public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - byteArrayDataOutput.writeUTF(player); - byteArrayDataOutput.writeUTF(title); - byteArrayDataOutput.writeInt(size); - byteArrayDataOutput.writeInt(items.size()); - for (int i = 0; i < size; i++) { - if(items.get(i) != null) - byteArrayDataOutput.writeUTF(items.get(i).writeToString()); - } - } - - public void addItem(SWItem item) { - for (int i = 0; i < items.size(); i++) { - if (items.get(i) != null) { - items.put(i, item); - return; - } - } - } - - public void setItem(int index, SWItem item) { - items.put(index, item); - } -} diff --git a/src/de/steamwar/bungeecore/coms/packets/PingPacket.java b/src/de/steamwar/bungeecore/coms/packets/PingPacket.java deleted file mode 100644 index fe982327..00000000 --- a/src/de/steamwar/bungeecore/coms/packets/PingPacket.java +++ /dev/null @@ -1,24 +0,0 @@ -package de.steamwar.bungeecore.coms.packets; - -import com.google.common.io.ByteArrayDataOutput; -import de.steamwar.bungeecore.coms.sender.Packet; - -public class PingPacket extends Packet { - - String UUID; - - @Override - public String getName() { - return "Ping"; - } - - @Override - public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - byteArrayDataOutput.writeUTF(UUID); - } - - public PingPacket setUUID(String UUID) { - this.UUID = UUID; - return this; - } -} diff --git a/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java b/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java deleted file mode 100644 index c681e585..00000000 --- a/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java +++ /dev/null @@ -1,75 +0,0 @@ -package de.steamwar.bungeecore.coms.packets.inventory; - -import de.steamwar.bungeecore.coms.handlers.InventoryCallbackHandler; -import de.steamwar.bungeecore.coms.packets.CloseInventoryPacket; -import de.steamwar.bungeecore.coms.packets.InventoryPacket; -import net.md_5.bungee.api.connection.ProxiedPlayer; - -import java.util.HashMap; -import java.util.Map; - -public class SWInventory { - - Map itemMap; - InvCallback close; - ProxiedPlayer player; - int size; - String title; - - public SWInventory(ProxiedPlayer proxiedPlayer, int size, String title) { - itemMap = new HashMap<>(); - InventoryCallbackHandler.inventoryHashMap.put(proxiedPlayer.getUniqueId().toString(), this); - this.player = proxiedPlayer; - this.size = size; - this.title = title; - } - - public void addItem(SWItem item) { - itemMap.put(item.position, item); - } - - public void removeItem(SWItem item) { - itemMap.remove(item.position); - } - - public void removeItem(int position) { - itemMap.remove(position); - } - - public int getSize() { - return size; - } - - public void setSize(int size) { - this.size = size; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public void handleCallback(InvCallback.ClickType type, SWItem item) { - itemMap.get(item.position).callback.clicked(type); - } - - public void handleClose() { - if(close != null) - close.clicked(null); - } - - public void open() { - InventoryPacket inv = new InventoryPacket(size, title, player); - for (Map.Entry entry : itemMap.entrySet()) { - inv.setItem(entry.getKey(), entry.getValue()); - } - inv.send(player); - } - - public void close() { - new CloseInventoryPacket().setPlayer(player).send(player); - } -} diff --git a/src/de/steamwar/bungeecore/coms/receiver/Handler.java b/src/de/steamwar/bungeecore/coms/receiver/Handler.java deleted file mode 100644 index bccaf850..00000000 --- a/src/de/steamwar/bungeecore/coms/receiver/Handler.java +++ /dev/null @@ -1,10 +0,0 @@ -package de.steamwar.bungeecore.coms.receiver; - -import com.google.common.io.ByteArrayDataInput; - -public abstract class Handler { - - protected abstract String getName(); - - public abstract void handle(ByteArrayDataInput byteArrayDataInput); -} diff --git a/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java b/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java deleted file mode 100644 index 6dc5e1ab..00000000 --- a/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java +++ /dev/null @@ -1,33 +0,0 @@ -package de.steamwar.bungeecore.coms.receiver; - -import com.google.common.io.ByteArrayDataInput; -import com.google.common.io.ByteStreams; -import de.steamwar.bungeecore.coms.handlers.InventoryCallbackHandler; -import de.steamwar.bungeecore.listeners.BasicListener; -import net.md_5.bungee.api.event.PluginMessageEvent; -import net.md_5.bungee.event.EventHandler; - -import java.util.HashMap; -import java.util.Map; - -public class PacketHandler extends BasicListener { - - private static final Map handlerMap = new HashMap<>(); - - @EventHandler - public void onPluginMessage(PluginMessageEvent event) { - if(!event.getTag().equalsIgnoreCase("sw:return")) - return; - ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); - String handler = in.readUTF(); - handlerMap.get(handler).handle(in); - } - - public static void registerHandler(Handler handler) { - handlerMap.put(handler.getName(), handler); - } - - static { - registerHandler(new InventoryCallbackHandler()); - } -} diff --git a/src/de/steamwar/bungeecore/coms/sender/Packet.java b/src/de/steamwar/bungeecore/coms/sender/Packet.java deleted file mode 100644 index 8d400d38..00000000 --- a/src/de/steamwar/bungeecore/coms/sender/Packet.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.steamwar.bungeecore.coms.sender; - -import com.google.common.io.ByteArrayDataOutput; -import net.md_5.bungee.api.connection.ProxiedPlayer; -import net.md_5.bungee.api.connection.Server; - -public abstract class Packet { - - public void send(Server server) { - PacketSender.sendPacket(this, server); - } - - public void send(ProxiedPlayer player) { - PacketSender.sendPacket(this, player); - } - - public abstract String getName(); - - //TODO Write Variables Automatic - public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput); -} diff --git a/src/de/steamwar/bungeecore/listeners/ChatListener.java b/src/de/steamwar/bungeecore/listeners/ChatListener.java index 3faf3110..070eb99b 100644 --- a/src/de/steamwar/bungeecore/listeners/ChatListener.java +++ b/src/de/steamwar/bungeecore/listeners/ChatListener.java @@ -24,7 +24,7 @@ import de.steamwar.bungeecore.Servertype; import de.steamwar.bungeecore.Subserver; import de.steamwar.bungeecore.commands.BauCommand; import de.steamwar.bungeecore.commands.TpCommand; -import de.steamwar.bungeecore.coms.packets.PingPacket; +import de.steamwar.bungeecore.comms.packets.PingPacket; import de.steamwar.bungeecore.sql.SteamwarUser; import de.steamwar.bungeecore.sql.Team; import net.md_5.bungee.api.ChatColor; @@ -189,7 +189,7 @@ public class ChatListener extends BasicListener { for (String curr : msgSplits) { if(curr.equalsIgnoreCase("@" + target.getName())) { builder.append("§e@").append(target.getName()).append(chatcolor).append(" "); - new PingPacket().setUUID(target.getUniqueId().toString()).send(target); + new PingPacket().setUser(SteamwarUser.get(target).getId()).send(target); }else { builder.append(curr).append(" "); } diff --git a/src/de/steamwar/bungeecore/sql/Schematic.java b/src/de/steamwar/bungeecore/sql/Schematic.java index c70e21d8..a36723cc 100644 --- a/src/de/steamwar/bungeecore/sql/Schematic.java +++ b/src/de/steamwar/bungeecore/sql/Schematic.java @@ -91,6 +91,23 @@ public class Schematic { } } + public static List getSchemsAccessibleByUser(int schemOwner){ + try{ + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, Rank, SchemFormat FROM Schematic WHERE SchemOwner = ? ORDER BY SchemName", schemOwner); + List schematics = new ArrayList<>(); + while(schematic.next()){ + schematics.add(new Schematic(schematic)); + } + List addedSchems = SchematicMember.getAccessibleSchems(schemOwner); + for(SchematicMember schem : addedSchems){ + schematics.add(getSchemFromDB(schem.getSchemName(), schem.getSchemOwner())); + } + return schematics; + }catch(SQLException e){ + throw new SecurityException("Failed listing schematics", e); + } + } + public int getSchemID() { return schemID; } @@ -107,6 +124,15 @@ public class Schematic { return schemType; } + public String getSchemItem() { + try { + ResultSet set = SQL.select("SELECT Item WHERE SchemID = ? AND SchemOwner = ?", schemID, schemOwner); + return set.getString("Item"); + } catch (SQLException throwables) { + return "CAULDRON_ITEM"; + } + } + public void setSchemType(SchematicType schemType) { this.schemType = schemType; SQL.update("UPDATE Schematic SET SchemType = ? WHERE SchemID = ?", schemType.toDB(), schemID); diff --git a/src/de/steamwar/bungeecore/sql/SchematicMember.java b/src/de/steamwar/bungeecore/sql/SchematicMember.java new file mode 100644 index 00000000..7d3c9a5d --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/SchematicMember.java @@ -0,0 +1,115 @@ +package de.steamwar.bungeecore.sql; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class SchematicMember { + private final int schemOwner; + private final String schemName; + private final int member; + + private SchematicMember(String schemName, int schemOwner, int schemMember, boolean updateDB){ + this.schemOwner = schemOwner; + member = schemMember; + this.schemName = schemName; + if(updateDB) + updateDB(); + } + + public SchematicMember(String schemName, int schemOwner, int schemMember){ + this(schemName, schemOwner, schemMember, true); + } + + public SchematicMember(String schemName, UUID schemOwner, UUID schemMember){ + this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId(), true); + } + + private void updateDB(){ + SQL.update("INSERT INTO SchemMember (SchemName, SchemOwner, Member) VALUES (?, ?, ?)", schemName, schemOwner, member); + } + + public static SchematicMember getSchemMemberFromDB(String schemName, UUID schemOwner, UUID schemMember){ + return getSchemMemberFromDB(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId()); + } + + public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember){ + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ? AND Member = ?", schemName, schemOwner, schemMember); + try { + if(schematicMember == null || !schematicMember.next()){ + return null; + } + return new SchematicMember(schemName, schemOwner, schemMember, false); + } catch (SQLException e) { + throw new SecurityException("Could not get schemmember", e); + } + } + + public static SchematicMember getMemberBySchematic(String schemName, int schemMember){ + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND Member = ?", schemName, schemMember); + try { + if(schematicMember == null || !schematicMember.next()){ + return null; + } + int schemOwner = schematicMember.getInt("SchemOwner"); + return new SchematicMember(schemName, schemOwner, schemMember, false); + } catch (SQLException e) { + throw new SecurityException("Could not get member", e); + } + } + + public static List getSchemMembers(String schemName, UUID schemOwner){ + return getSchemMembers(schemName, SteamwarUser.get(schemOwner).getId()); + } + + public static List getSchemMembers(String schemName, int schemOwner){ + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner); + try { + List schematicMembers = new ArrayList<>(); + while(schematicMember.next()){ + int schemMember = schematicMember.getInt("Member"); + schematicMembers.add(new SchematicMember(schemName, schemOwner, schemMember, false)); + } + return schematicMembers; + } catch (SQLException e) { + throw new SecurityException("Could not get schemmembers", e); + } + } + + public static List getAccessibleSchems(UUID schemMember){ + return getAccessibleSchems(SteamwarUser.get(schemMember).getId()); + } + + public static List getAccessibleSchems(int schemMember){ + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE Member = ?", schemMember); + try { + List schematicMembers = new ArrayList<>(); + while(schematicMember.next()){ + String schemName = schematicMember.getString("SchemName"); + int schemOwner = schematicMember.getInt("SchemOwner"); + schematicMembers.add(new SchematicMember(schemName, schemOwner, schemMember, false)); + } + return schematicMembers; + } catch (SQLException e) { + throw new SecurityException("Could not get accessible schems", e); + } + } + + public int getSchemOwner() { + return schemOwner; + } + + public String getSchemName() { + return schemName; + } + + public int getMember() { + return member; + } + + public void remove(){ + SQL.update("DELETE FROM SchemMember WHERE SchemOwner = ? AND SchemName = ? AND Member = ?", schemOwner, schemName, member); + } +}