From 7dd8d707a58e0d76e0ab1761986d96716731dd76 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 21 Sep 2020 23:23:39 +0200 Subject: [PATCH] Code Cleanup --- src/de/steamwar/bungeecore/BungeeCore.java | 2 -- .../steamwar/bungeecore/commands/BauCommand.java | 10 +++++----- .../steamwar/bungeecore/comms/BungeePacket.java | 9 ++------- .../bungeecore/comms/PacketIdManager.java | 11 +++++++++++ .../steamwar/bungeecore/comms/SpigotReceiver.java | 15 +++++++-------- .../comms/handlers/InventoryCallbackHandler.java | 10 +++++++--- .../comms/packets/CloseInventoryPacket.java | 14 +++++++------- .../bungeecore/comms/packets/InventoryPacket.java | 13 +++++++------ .../bungeecore/comms/packets/PingPacket.java | 14 +++++++------- .../packets => }/inventory/InvCallback.java | 2 +- .../packets => }/inventory/SWInventory.java | 15 +++++++++++++-- .../{comms/packets => }/inventory/SWItem.java | 2 +- .../{comms/packets => }/inventory/SWListInv.java | 2 +- .../bungeecore/listeners/ChatListener.java | 2 +- src/de/steamwar/bungeecore/sql/Schematic.java | 2 +- 15 files changed, 71 insertions(+), 52 deletions(-) create mode 100644 src/de/steamwar/bungeecore/comms/PacketIdManager.java rename src/de/steamwar/bungeecore/{comms/packets => }/inventory/InvCallback.java (96%) rename src/de/steamwar/bungeecore/{comms/packets => }/inventory/SWInventory.java (89%) rename src/de/steamwar/bungeecore/{comms/packets => }/inventory/SWItem.java (98%) rename src/de/steamwar/bungeecore/{comms/packets => }/inventory/SWListInv.java (98%) diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index ab6d7f30..56760a1e 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -20,7 +20,6 @@ package de.steamwar.bungeecore; import de.steamwar.bungeecore.commands.*; -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; @@ -41,7 +40,6 @@ import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.config.Configuration; import net.md_5.bungee.config.ConfigurationProvider; import net.md_5.bungee.config.YamlConfiguration; -import org.yaml.snakeyaml.error.YAMLException; import java.io.File; import java.io.IOException; diff --git a/src/de/steamwar/bungeecore/commands/BauCommand.java b/src/de/steamwar/bungeecore/commands/BauCommand.java index f50d456a..1a323b37 100644 --- a/src/de/steamwar/bungeecore/commands/BauCommand.java +++ b/src/de/steamwar/bungeecore/commands/BauCommand.java @@ -20,8 +20,8 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.*; -import de.steamwar.bungeecore.comms.packets.inventory.SWInventory; -import de.steamwar.bungeecore.comms.packets.inventory.SWItem; +import de.steamwar.bungeecore.inventory.SWInventory; +import de.steamwar.bungeecore.inventory.SWItem; import de.steamwar.bungeecore.sql.BauweltMember; import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.ProxyServer; @@ -228,11 +228,11 @@ public class BauCommand { } private static void delete(ProxiedPlayer p, String[] command){ - SWInventory inventory = new SWInventory(p, 9, "§eDelete Confirm"); - inventory.addItem(1, new SWItem(1, "§cDecline", 1), click -> { + SWInventory inventory = new SWInventory(p, 9, "§e/Bau delete Bestätigen"); + inventory.addItem(0, new SWItem(0, "§cAbbrechen", 1), click -> { inventory.close(); }); - inventory.addItem(7, new SWItem(7, "§aAccept", 10), click -> { + inventory.addItem(8, new SWItem(8, "§aLöchen", 10), click -> { if(bau15(p, command, 2)){ BungeeCore.send(p, BungeeCore.CHAT_PREFIX + "§aDeine Welt wird zurückgesetzt."); ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { diff --git a/src/de/steamwar/bungeecore/comms/BungeePacket.java b/src/de/steamwar/bungeecore/comms/BungeePacket.java index 8ccf0b7f..b0417d20 100644 --- a/src/de/steamwar/bungeecore/comms/BungeePacket.java +++ b/src/de/steamwar/bungeecore/comms/BungeePacket.java @@ -30,7 +30,7 @@ public abstract class BungeePacket { public void send(Server server) { ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeInt(getId()); + out.writeByte(getId()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); writeVars(out); out.writeUTF(outputStream.toByteArray().toString()); @@ -38,12 +38,7 @@ public abstract class BungeePacket { } 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()); + send(player.getServer()); } public abstract int getId(); diff --git a/src/de/steamwar/bungeecore/comms/PacketIdManager.java b/src/de/steamwar/bungeecore/comms/PacketIdManager.java new file mode 100644 index 00000000..13f951a4 --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/PacketIdManager.java @@ -0,0 +1,11 @@ +package de.steamwar.bungeecore.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/src/de/steamwar/bungeecore/comms/SpigotReceiver.java b/src/de/steamwar/bungeecore/comms/SpigotReceiver.java index af2061e0..d747d154 100644 --- a/src/de/steamwar/bungeecore/comms/SpigotReceiver.java +++ b/src/de/steamwar/bungeecore/comms/SpigotReceiver.java @@ -31,26 +31,25 @@ import java.util.Map; public class SpigotReceiver extends BasicListener { - private static final Map handlerMap = new HashMap<>(); + private static final Map handlerMap = new HashMap<>(); @EventHandler public void onPluginMessage(PluginMessageEvent event) { if(!event.getTag().equalsIgnoreCase("sw:bridge")) return; + if(!event.getSender().getAddress().getHostName().equals("localhost")) + return; ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); - int handler = in.readInt(); + Byte handler = in.readByte(); handlerMap.get(handler).handle(in); + event.setCancelled(true); } - public static void registerHandler(int id, SpigotHandler handler) { + public static void registerHandler(Byte 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()); + registerHandler(PacketIdManager.INVENTORY_CALLBACK_PACKET, new InventoryCallbackHandler()); } } diff --git a/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java b/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java index 2e7fb17c..e39b668b 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java @@ -21,8 +21,8 @@ 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.inventory.InvCallback; +import de.steamwar.bungeecore.inventory.SWInventory; import de.steamwar.bungeecore.sql.SteamwarUser; import java.util.HashMap; @@ -41,7 +41,11 @@ public class InventoryCallbackHandler implements SpigotHandler { 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(); + if(inventoryHashMap.get(owner.getId()).isNext()) { + inventoryHashMap.get(owner.getId()).handleClose(); + return; + } + inventoryHashMap.get(owner.getId()).setNext(true); } } diff --git a/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java b/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java index fb8858b1..974661ab 100644 --- a/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java +++ b/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java @@ -21,25 +21,25 @@ package de.steamwar.bungeecore.comms.packets; import com.google.common.io.ByteArrayDataOutput; import de.steamwar.bungeecore.comms.BungeePacket; +import de.steamwar.bungeecore.comms.PacketIdManager; import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.connection.ProxiedPlayer; public class CloseInventoryPacket extends BungeePacket { - ProxiedPlayer player; + final ProxiedPlayer player; @Override public int getId() { - return "CloseInventory".hashCode(); + return PacketIdManager.INVENTORY_CLOSE_PACKET; + } + + public CloseInventoryPacket(ProxiedPlayer player) { + this.player = player; } @Override public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { byteArrayDataOutput.writeInt(SteamwarUser.get(player).getId()); } - - public CloseInventoryPacket setPlayer(ProxiedPlayer player) { - this.player = player; - return this; - } } diff --git a/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java b/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java index 6c46ef86..db5282a6 100644 --- a/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java +++ b/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java @@ -22,7 +22,8 @@ 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.PacketIdManager; +import de.steamwar.bungeecore.inventory.SWItem; import de.steamwar.bungeecore.comms.BungeePacket; import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -31,10 +32,10 @@ import java.util.HashMap; public class InventoryPacket extends BungeePacket { - String title; - int player; - int size; - HashMap items; + final String title; + final int player; + final int size; + final HashMap items; public InventoryPacket(int size, String title, ProxiedPlayer player) { items = new HashMap<>(); @@ -45,7 +46,7 @@ public class InventoryPacket extends BungeePacket { @Override public int getId() { - return "InventoryHandler".hashCode(); + return PacketIdManager.INVENTORY_PACKET; } @Override diff --git a/src/de/steamwar/bungeecore/comms/packets/PingPacket.java b/src/de/steamwar/bungeecore/comms/packets/PingPacket.java index dddb2727..47f2bd30 100644 --- a/src/de/steamwar/bungeecore/comms/packets/PingPacket.java +++ b/src/de/steamwar/bungeecore/comms/packets/PingPacket.java @@ -21,23 +21,23 @@ package de.steamwar.bungeecore.comms.packets; import com.google.common.io.ByteArrayDataOutput; import de.steamwar.bungeecore.comms.BungeePacket; +import de.steamwar.bungeecore.comms.PacketIdManager; public class PingPacket extends BungeePacket { - int id; + final int id; @Override public int getId() { - return "PingHandler".hashCode(); + return PacketIdManager.PING_PACKET; + } + + public PingPacket(int id) { + this.id = id; } @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/comms/packets/inventory/InvCallback.java b/src/de/steamwar/bungeecore/inventory/InvCallback.java similarity index 96% rename from src/de/steamwar/bungeecore/comms/packets/inventory/InvCallback.java rename to src/de/steamwar/bungeecore/inventory/InvCallback.java index 666e456e..1c52bb24 100644 --- a/src/de/steamwar/bungeecore/comms/packets/inventory/InvCallback.java +++ b/src/de/steamwar/bungeecore/inventory/InvCallback.java @@ -17,7 +17,7 @@ along with this program. If not, see . */ -package de.steamwar.bungeecore.comms.packets.inventory; +package de.steamwar.bungeecore.inventory; public interface InvCallback { diff --git a/src/de/steamwar/bungeecore/comms/packets/inventory/SWInventory.java b/src/de/steamwar/bungeecore/inventory/SWInventory.java similarity index 89% rename from src/de/steamwar/bungeecore/comms/packets/inventory/SWInventory.java rename to src/de/steamwar/bungeecore/inventory/SWInventory.java index 8cb07287..c76b6a4e 100644 --- a/src/de/steamwar/bungeecore/comms/packets/inventory/SWInventory.java +++ b/src/de/steamwar/bungeecore/inventory/SWInventory.java @@ -17,7 +17,7 @@ along with this program. If not, see . */ -package de.steamwar.bungeecore.comms.packets.inventory; +package de.steamwar.bungeecore.inventory; import de.steamwar.bungeecore.comms.handlers.InventoryCallbackHandler; import de.steamwar.bungeecore.comms.packets.CloseInventoryPacket; @@ -35,6 +35,7 @@ public class SWInventory { private ProxiedPlayer player; private int size; private String title; + private boolean next; public SWInventory(ProxiedPlayer proxiedPlayer, int size, String title) { itemMap = new HashMap<>(); @@ -42,6 +43,7 @@ public class SWInventory { this.player = proxiedPlayer; this.size = size; this.title = title; + next = false; } public void addItem(SWItem item) { @@ -86,11 +88,20 @@ public class SWInventory { itemMap.get(pos).setCallback(callback); } + public void setNext(boolean next) { + this.next = next; + } + + public boolean isNext() { + return next; + } + public void handleCallback(InvCallback.ClickType type, int pos) { itemMap.get(pos).getCallback().clicked(type); } public void handleClose() { + InventoryCallbackHandler.inventoryHashMap.remove(SteamwarUser.get(player).getId(), this); if(close != null) close.clicked(null); } @@ -104,6 +115,6 @@ public class SWInventory { } public void close() { - new CloseInventoryPacket().setPlayer(player).send(player); + new CloseInventoryPacket(player).send(player); } } diff --git a/src/de/steamwar/bungeecore/comms/packets/inventory/SWItem.java b/src/de/steamwar/bungeecore/inventory/SWItem.java similarity index 98% rename from src/de/steamwar/bungeecore/comms/packets/inventory/SWItem.java rename to src/de/steamwar/bungeecore/inventory/SWItem.java index 9a2a640c..aadd2d98 100644 --- a/src/de/steamwar/bungeecore/comms/packets/inventory/SWItem.java +++ b/src/de/steamwar/bungeecore/inventory/SWItem.java @@ -17,7 +17,7 @@ along with this program. If not, see . */ -package de.steamwar.bungeecore.comms.packets.inventory; +package de.steamwar.bungeecore.inventory; import com.google.gson.JsonArray; import com.google.gson.JsonObject; diff --git a/src/de/steamwar/bungeecore/comms/packets/inventory/SWListInv.java b/src/de/steamwar/bungeecore/inventory/SWListInv.java similarity index 98% rename from src/de/steamwar/bungeecore/comms/packets/inventory/SWListInv.java rename to src/de/steamwar/bungeecore/inventory/SWListInv.java index 5cd876a3..639e9536 100644 --- a/src/de/steamwar/bungeecore/comms/packets/inventory/SWListInv.java +++ b/src/de/steamwar/bungeecore/inventory/SWListInv.java @@ -17,7 +17,7 @@ along with this program. If not, see . */ -package de.steamwar.bungeecore.comms.packets.inventory; +package de.steamwar.bungeecore.inventory; import de.steamwar.bungeecore.sql.Schematic; import de.steamwar.bungeecore.sql.SchematicType; diff --git a/src/de/steamwar/bungeecore/listeners/ChatListener.java b/src/de/steamwar/bungeecore/listeners/ChatListener.java index 070eb99b..dcb95b95 100644 --- a/src/de/steamwar/bungeecore/listeners/ChatListener.java +++ b/src/de/steamwar/bungeecore/listeners/ChatListener.java @@ -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().setUser(SteamwarUser.get(target).getId()).send(target); + new PingPacket(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 a36723cc..2ae7e513 100644 --- a/src/de/steamwar/bungeecore/sql/Schematic.java +++ b/src/de/steamwar/bungeecore/sql/Schematic.java @@ -126,7 +126,7 @@ public class Schematic { public String getSchemItem() { try { - ResultSet set = SQL.select("SELECT Item WHERE SchemID = ? AND SchemOwner = ?", schemID, schemOwner); + ResultSet set = SQL.select("SELECT Item WHERE SchemID = ?", schemID); return set.getString("Item"); } catch (SQLException throwables) { return "CAULDRON_ITEM";