From 65e7a7bb739ecd4dc1652f4f467d2a5f6fabf96b Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 15 Sep 2020 22:47:58 +0200 Subject: [PATCH 01/11] Basic Packet System --- src/de/steamwar/bungeecore/BungeeCore.java | 28 ++++++++++++++++- .../bungeecore/commands/CheckCommand.java | 4 +-- .../bungeecore/coms/packets/PingPacket.java | 24 ++++++++++++++ .../bungeecore/coms/receiver/Handler.java | 10 ++++++ .../coms/receiver/PacketHandler.java | 31 +++++++++++++++++++ .../bungeecore/coms/sender/Packet.java | 20 ++++++++++++ .../bungeecore/coms/sender/PacketSender.java | 31 +++++++++++++++++++ .../bungeecore/listeners/ChatListener.java | 2 ++ 8 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 src/de/steamwar/bungeecore/coms/packets/PingPacket.java create mode 100644 src/de/steamwar/bungeecore/coms/receiver/Handler.java create mode 100644 src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java create mode 100644 src/de/steamwar/bungeecore/coms/sender/Packet.java create mode 100644 src/de/steamwar/bungeecore/coms/sender/PacketSender.java diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index ad9ced66..5d51f29c 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -39,9 +39,12 @@ 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.FileInputStream; import java.io.IOException; +import java.nio.charset.MalformedInputException; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.List; @@ -127,6 +130,8 @@ public class BungeeCore extends Plugin { new TablistManager(); + getProxy().registerChannel("sw:bridge"); + getProxy().getScheduler().schedule(this, () -> { SteamwarUser.clearCache(); Team.clearCache(); @@ -208,7 +213,28 @@ public class BungeeCore extends Plugin { ProxyServer.getInstance().stop("Could not save conig file"); return; } - config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile); + try { + config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile); + } + catch (YAMLException e) { + e.printStackTrace(); + CHAT_PREFIX = "§eSteam§8War» §r"; + WORLD_FOLDER = "/home/minecraft/userworlds/"; + BAUWELT_PROTOTYP = "/home/minecraft/backbone/server/UserBau/Bauwelt2"; + LOBBY_SERVER = "Lobby"; + USERWORLDS15 = "/home/minecraft/userworlds15/"; + BAUWELT15 = "/home/minecraft/backbone/server/Bau15/Bauwelt"; + Broadcaster.setBroadCastMsgs(new String[] {""}); + Persistent.setChatPrefix(CHAT_PREFIX); + Persistent.setLobbyServer(LOBBY_SERVER); + + SQL.connect( + "jdbc:mysql://127.0.0.1:3306/developer", + "", + "" + ); + return; + } }catch(IOException e){ log("Could not save/load config.yml", e); ProxyServer.getInstance().stop(); diff --git a/src/de/steamwar/bungeecore/commands/CheckCommand.java b/src/de/steamwar/bungeecore/commands/CheckCommand.java index 2d03f4ef..24a11566 100644 --- a/src/de/steamwar/bungeecore/commands/CheckCommand.java +++ b/src/de/steamwar/bungeecore/commands/CheckCommand.java @@ -67,8 +67,8 @@ public class CheckCommand extends BasicCommand { ProxyServer.getInstance().getScheduler().schedule(BungeeCore.get(), () -> { List schematics = getSchemsToCheck(); - if(!schematics.isEmpty()) - ServerTeamchatCommand.sendToTeam("§7Es sind §e" + schematics.size() + " §7Schematics zu prüfen§8!"); + if(schematics.size() != currentCheckers.size()) + ServerTeamchatCommand.sendToTeam("§7Es sind §e" + (schematics.size() - currentCheckers.size()) + " §7Schematics zu prüfen§8!"); }, 10, 10, TimeUnit.MINUTES); } diff --git a/src/de/steamwar/bungeecore/coms/packets/PingPacket.java b/src/de/steamwar/bungeecore/coms/packets/PingPacket.java new file mode 100644 index 00000000..fe982327 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/packets/PingPacket.java @@ -0,0 +1,24 @@ +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/receiver/Handler.java b/src/de/steamwar/bungeecore/coms/receiver/Handler.java new file mode 100644 index 00000000..bccaf850 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/receiver/Handler.java @@ -0,0 +1,10 @@ +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 new file mode 100644 index 00000000..59d5c795 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java @@ -0,0 +1,31 @@ +package de.steamwar.bungeecore.coms.receiver; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteStreams; +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 Map handlerMap = new HashMap<>(); + + @EventHandler + public void onPluginMessage(PluginMessageEvent event) { + if(!event.getTag().equalsIgnoreCase("sw:bridge")) + return; + ByteArrayDataInput in = ByteStreams.newDataInput( event.getData() ); + handlerMap.get(in.readUTF()).handle(in); + } + + public static void registerHandler(Handler handler) { + handlerMap.put(handler.getName(), handler); + } + + static { + //TODO Register Handlers + } +} diff --git a/src/de/steamwar/bungeecore/coms/sender/Packet.java b/src/de/steamwar/bungeecore/coms/sender/Packet.java new file mode 100644 index 00000000..a7d92b6b --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/sender/Packet.java @@ -0,0 +1,20 @@ +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(); + + public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput); +} diff --git a/src/de/steamwar/bungeecore/coms/sender/PacketSender.java b/src/de/steamwar/bungeecore/coms/sender/PacketSender.java new file mode 100644 index 00000000..5bd6f8df --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/sender/PacketSender.java @@ -0,0 +1,31 @@ +package de.steamwar.bungeecore.coms.sender; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.connection.Server; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; + +public class PacketSender { + + public static void sendPacket(Packet packet, ProxiedPlayer player) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF(packet.getName()); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + packet.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()); + } +} diff --git a/src/de/steamwar/bungeecore/listeners/ChatListener.java b/src/de/steamwar/bungeecore/listeners/ChatListener.java index 554db08b..3faf3110 100644 --- a/src/de/steamwar/bungeecore/listeners/ChatListener.java +++ b/src/de/steamwar/bungeecore/listeners/ChatListener.java @@ -24,6 +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.sql.SteamwarUser; import de.steamwar.bungeecore.sql.Team; import net.md_5.bungee.api.ChatColor; @@ -188,6 +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); }else { builder.append(curr).append(" "); } From 3fe222ae9bf0ab1ec5e2762b58d6c4c1db925371 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 15 Sep 2020 22:50:12 +0200 Subject: [PATCH 02/11] Basic Packet System --- src/de/steamwar/bungeecore/BungeeCore.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index 5d51f29c..518848bc 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -230,8 +230,8 @@ public class BungeeCore extends Plugin { SQL.connect( "jdbc:mysql://127.0.0.1:3306/developer", - "", - "" + Password.getUsername(), + Password.getPassword() ); return; } From 4565f7da7e9fb278de5dc0b6e9e65a1909a1830f Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 16 Sep 2020 21:22:13 +0200 Subject: [PATCH 03/11] Add Stuff for Bungee GUI --- .../handlers/InventoryCallbackHandler.java | 16 ++++ .../coms/packets/InventoryPacket.java | 55 +++++++++++++ .../coms/packets/inventory/SWInventory.java | 4 + .../coms/packets/inventory/SWItem.java | 81 +++++++++++++++++++ .../bungeecore/coms/sender/Packet.java | 1 + .../bungeecore/coms/sender/PacketSender.java | 2 - 6 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java create mode 100644 src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java create mode 100644 src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java create mode 100644 src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java diff --git a/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java b/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java new file mode 100644 index 00000000..ca9d0723 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java @@ -0,0 +1,16 @@ +package de.steamwar.bungeecore.coms.handlers; + +import com.google.common.io.ByteArrayDataInput; +import de.steamwar.bungeecore.coms.receiver.Handler; + +public class InventoryCallbackHandler extends Handler { + @Override + protected String getName() { + return "InventoryCallback"; + } + + @Override + public void handle(ByteArrayDataInput byteArrayDataInput) { + + } +} diff --git a/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java b/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java new file mode 100644 index 00000000..64466a55 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java @@ -0,0 +1,55 @@ +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; + +public class InventoryPacket extends Packet { + + String title; + String name; + String player; + int size; + SWItem[] items; + + public InventoryPacket(int size, String title, String name, ProxiedPlayer player) { + items = new SWItem[size]; + this.title = title; + this.size = size; + this.name = name; + this.player = player.getUniqueId().toString(); + } + + @Override + public String getName() { + return "Inventory"; + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + byteArrayDataOutput.writeUTF(name); + byteArrayDataOutput.writeUTF(player); + byteArrayDataOutput.writeUTF(title); + byteArrayDataOutput.writeInt(size); + byteArrayDataOutput.writeInt(items.length); + for (SWItem item: + items) { + if(item == null) + byteArrayDataOutput.writeUTF(item.writeToString()); + } + } + + public void addItem(SWItem item) { + for (int i = 0; i < items.length; i++) { + if (items[i] != null) { + items[i] = item; + return; + } + } + } + + public void setItem(int index, SWItem item) { + items[index] = item; + } +} diff --git a/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java b/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java new file mode 100644 index 00000000..c155bcf9 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java @@ -0,0 +1,4 @@ +package de.steamwar.bungeecore.coms.packets.inventory; + +public class SWInventory { +} diff --git a/src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java b/src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java new file mode 100644 index 00000000..2ead66a3 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java @@ -0,0 +1,81 @@ +package de.steamwar.bungeecore.coms.packets.inventory; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +import java.util.ArrayList; +import java.util.List; + +public class SWItem { + + String material, title, skullOwner; + boolean enchanted, hideAttributes; + int position; + List lore; + + public SWItem(String material, int position, String title) { + this.material = material; + lore = new ArrayList<>(); + this.position = position; + this.title = title; + } + + public String getMaterial() { + return material; + } + + public void setMaterial(String material) { + this.material = material; + } + + public String getSkullOwner() { + return skullOwner; + } + + public void setSkullOwner(String skullOwner) { + this.skullOwner = skullOwner; + } + + public boolean isEnchanted() { + return enchanted; + } + + public void setEnchanted(boolean enchanted) { + this.enchanted = enchanted; + } + + public boolean isHideAttributes() { + return hideAttributes; + } + + public void setHideAttributes(boolean hideAttributes) { + this.hideAttributes = hideAttributes; + } + + public void addLore(String lore) { + this.lore.add(lore); + } + + public String writeToString() { + JsonObject object = new JsonObject(); + object.addProperty("material", material); + object.addProperty("position", position); + object.addProperty("title", title); + if(skullOwner != null) + object.addProperty("skullOwner", skullOwner); + if(enchanted) + object.addProperty("enchanted", true); + if(hideAttributes) + object.addProperty("hideAttributes", true); + if(!lore.isEmpty()) { + JsonArray array = new JsonArray(); + for (String lores: + lore) { + array.add(lores); + } + object.add("lore", array); + } + + return object.getAsString(); + } +} diff --git a/src/de/steamwar/bungeecore/coms/sender/Packet.java b/src/de/steamwar/bungeecore/coms/sender/Packet.java index a7d92b6b..8d400d38 100644 --- a/src/de/steamwar/bungeecore/coms/sender/Packet.java +++ b/src/de/steamwar/bungeecore/coms/sender/Packet.java @@ -16,5 +16,6 @@ public abstract class Packet { public abstract String getName(); + //TODO Write Variables Automatic public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput); } diff --git a/src/de/steamwar/bungeecore/coms/sender/PacketSender.java b/src/de/steamwar/bungeecore/coms/sender/PacketSender.java index 5bd6f8df..2a7e8da9 100644 --- a/src/de/steamwar/bungeecore/coms/sender/PacketSender.java +++ b/src/de/steamwar/bungeecore/coms/sender/PacketSender.java @@ -6,8 +6,6 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.Server; import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectOutputStream; public class PacketSender { From 34534b49d850b8b6b6059f12cba2ed1d2cb90c65 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 19 Sep 2020 12:37:26 +0200 Subject: [PATCH 04/11] Add Bungee GUI Prototype --- src/de/steamwar/bungeecore/BungeeCore.java | 10 ++- .../bungeecore/commands/TestCommand.java | 33 +++++++++ .../handlers/InventoryCallbackHandler.java | 23 ++++++ .../coms/packets/CloseInventoryPacket.java | 25 +++++++ .../coms/packets/InventoryPacket.java | 28 ++++---- .../coms/packets/inventory/InvCallback.java | 45 ++++++++++++ .../coms/packets/inventory/SWInventory.java | 71 +++++++++++++++++++ .../coms/packets/inventory/SWItem.java | 62 ++++++++++++++-- .../coms/receiver/PacketHandler.java | 13 ++-- 9 files changed, 284 insertions(+), 26 deletions(-) create mode 100644 src/de/steamwar/bungeecore/commands/TestCommand.java create mode 100644 src/de/steamwar/bungeecore/coms/packets/CloseInventoryPacket.java create mode 100644 src/de/steamwar/bungeecore/coms/packets/inventory/InvCallback.java diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index 518848bc..036cfcf5 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -20,6 +20,7 @@ package de.steamwar.bungeecore; import de.steamwar.bungeecore.commands.*; +import de.steamwar.bungeecore.coms.receiver.PacketHandler; import de.steamwar.bungeecore.listeners.*; import de.steamwar.bungeecore.listeners.mods.Fabric; import de.steamwar.bungeecore.listeners.mods.Forge; @@ -72,6 +73,10 @@ public class BungeeCore extends Plugin { @Override public void onEnable(){ + + getProxy().registerChannel("sw:bridge"); + getProxy().registerChannel("sw:return"); + setInstance(this); loadConfig(); @@ -124,14 +129,15 @@ public class BungeeCore extends Plugin { new IgnoreCommand(); new UnIgnoreCommand(); + new TestCommand(); + new EventStarter(); new Broadcaster(); new SessionManager(); + new PacketHandler(); new TablistManager(); - getProxy().registerChannel("sw:bridge"); - getProxy().getScheduler().schedule(this, () -> { SteamwarUser.clearCache(); Team.clearCache(); diff --git a/src/de/steamwar/bungeecore/commands/TestCommand.java b/src/de/steamwar/bungeecore/commands/TestCommand.java new file mode 100644 index 00000000..a7c18cad --- /dev/null +++ b/src/de/steamwar/bungeecore/commands/TestCommand.java @@ -0,0 +1,33 @@ +package de.steamwar.bungeecore.commands; + +import de.steamwar.bungeecore.coms.packets.inventory.SWInventory; +import de.steamwar.bungeecore.coms.packets.inventory.SWItem; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +public class TestCommand extends BasicCommand { + + public TestCommand() { + super("test", null); + } + + @Override + public void execute(CommandSender commandSender, String[] strings) { + if(!(commandSender instanceof ProxiedPlayer)) + return; + SWInventory inventory = new SWInventory((ProxiedPlayer) commandSender, 3*9, "Test Inventory"); + SWItem item = new SWItem("DIRT", 13, "§e§lTest Item"); + item.setSkullOwner(commandSender.getName()); + for (ProxiedPlayer player: + ((ProxiedPlayer) commandSender).getServer().getInfo().getPlayers()) { + item.addLore("§6" + player.getDisplayName()); + } + item.setEnchanted(true); + item.setCallback(click -> { + inventory.close(); + commandSender.sendMessage("TEST!!! FJOnbds"); + }); + inventory.addItem(item); + inventory.open(); + } +} diff --git a/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java b/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java index ca9d0723..666a5a83 100644 --- a/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java +++ b/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java @@ -1,9 +1,18 @@ 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"; @@ -11,6 +20,20 @@ public class InventoryCallbackHandler extends Handler { @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()); + System.out.println(item.writeToString()); + 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/CloseInventoryPacket.java b/src/de/steamwar/bungeecore/coms/packets/CloseInventoryPacket.java new file mode 100644 index 00000000..4fb85170 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/packets/CloseInventoryPacket.java @@ -0,0 +1,25 @@ +package de.steamwar.bungeecore.coms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import de.steamwar.bungeecore.coms.sender.Packet; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +public class CloseInventoryPacket extends Packet { + + ProxiedPlayer player; + + @Override + public String getName() { + return "InvClose"; + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + byteArrayDataOutput.writeUTF(player.getUniqueId().toString()); + } + + public CloseInventoryPacket setPlayer(ProxiedPlayer player) { + this.player = player; + return this; + } +} diff --git a/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java b/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java index 64466a55..9becf90a 100644 --- a/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java +++ b/src/de/steamwar/bungeecore/coms/packets/InventoryPacket.java @@ -5,19 +5,19 @@ 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 name; String player; int size; - SWItem[] items; + HashMap items; - public InventoryPacket(int size, String title, String name, ProxiedPlayer player) { - items = new SWItem[size]; + public InventoryPacket(int size, String title, ProxiedPlayer player) { + items = new HashMap<>(); this.title = title; this.size = size; - this.name = name; this.player = player.getUniqueId().toString(); } @@ -28,28 +28,26 @@ public class InventoryPacket extends Packet { @Override public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - byteArrayDataOutput.writeUTF(name); byteArrayDataOutput.writeUTF(player); byteArrayDataOutput.writeUTF(title); byteArrayDataOutput.writeInt(size); - byteArrayDataOutput.writeInt(items.length); - for (SWItem item: - items) { - if(item == null) - byteArrayDataOutput.writeUTF(item.writeToString()); + 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.length; i++) { - if (items[i] != null) { - items[i] = 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[index] = item; + items.put(index, item); } } diff --git a/src/de/steamwar/bungeecore/coms/packets/inventory/InvCallback.java b/src/de/steamwar/bungeecore/coms/packets/inventory/InvCallback.java new file mode 100644 index 00000000..a3a03658 --- /dev/null +++ b/src/de/steamwar/bungeecore/coms/packets/inventory/InvCallback.java @@ -0,0 +1,45 @@ +package de.steamwar.bungeecore.coms.packets.inventory; + +public interface InvCallback { + + void clicked(ClickType click); + + public static enum ClickType { + LEFT, + SHIFT_LEFT, + RIGHT, + SHIFT_RIGHT, + WINDOW_BORDER_LEFT, + WINDOW_BORDER_RIGHT, + MIDDLE, + NUMBER_KEY, + DOUBLE_CLICK, + DROP, + CONTROL_DROP, + CREATIVE, + UNKNOWN; + + private ClickType() { + } + + public boolean isKeyboardClick() { + return this == NUMBER_KEY || this == DROP || this == CONTROL_DROP; + } + + public boolean isCreativeAction() { + return this == MIDDLE || this == CREATIVE; + } + + public boolean isRightClick() { + return this == RIGHT || this == SHIFT_RIGHT; + } + + public boolean isLeftClick() { + return this == LEFT || this == SHIFT_LEFT || this == DOUBLE_CLICK || this == CREATIVE; + } + + public boolean isShiftClick() { + return this == SHIFT_LEFT || this == SHIFT_RIGHT || this == CONTROL_DROP; + } + } +} diff --git a/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java b/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java index c155bcf9..c681e585 100644 --- a/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java +++ b/src/de/steamwar/bungeecore/coms/packets/inventory/SWInventory.java @@ -1,4 +1,75 @@ 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/packets/inventory/SWItem.java b/src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java index 2ead66a3..276c06d6 100644 --- a/src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java +++ b/src/de/steamwar/bungeecore/coms/packets/inventory/SWItem.java @@ -2,6 +2,7 @@ package de.steamwar.bungeecore.coms.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; @@ -12,20 +13,49 @@ public class SWItem { boolean enchanted, hideAttributes; int position; List lore; + InvCallback callback; public SWItem(String material, int position, String title) { - this.material = material; + this.material = material.toUpperCase(); lore = new ArrayList<>(); this.position = position; this.title = title; } + 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 void setCallback(InvCallback callback) { + this.callback = callback; + } + + public InvCallback getCallback() { + return callback; + } + public String getMaterial() { return material; } public void setMaterial(String material) { - this.material = material; + this.material = material.toUpperCase(); } public String getSkullOwner() { @@ -67,7 +97,7 @@ public class SWItem { object.addProperty("enchanted", true); if(hideAttributes) object.addProperty("hideAttributes", true); - if(!lore.isEmpty()) { + if(lore != null) { JsonArray array = new JsonArray(); for (String lores: lore) { @@ -76,6 +106,30 @@ public class SWItem { object.add("lore", array); } - return object.getAsString(); + return object.toString(); + } + + public static SWItem getFromJson(String json) { + return new SWItem(json); + } + + @Override + public boolean equals(Object o) { + if(!(o instanceof SWItem)) + return false; + SWItem item = (SWItem) o; + if(item.enchanted != enchanted) + return false; + if(item.hideAttributes != hideAttributes) + return false; + if(!item.material.equals(material)) + return false; + if(!item.lore.equals(lore)) + return false; + if(!item.skullOwner.equals(skullOwner)) + return false; + if(item.position != position) + return false; + return true; } } diff --git a/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java b/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java index 59d5c795..3971952f 100644 --- a/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java +++ b/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java @@ -2,6 +2,7 @@ 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; @@ -11,14 +12,16 @@ import java.util.Map; public class PacketHandler extends BasicListener { - private static Map handlerMap = new HashMap<>(); + private static final Map handlerMap = new HashMap<>(); @EventHandler public void onPluginMessage(PluginMessageEvent event) { - if(!event.getTag().equalsIgnoreCase("sw:bridge")) + System.out.println(event.toString()); + if(!event.getTag().equalsIgnoreCase("sw:return")) return; - ByteArrayDataInput in = ByteStreams.newDataInput( event.getData() ); - handlerMap.get(in.readUTF()).handle(in); + ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); + String handler = in.readUTF(); + handlerMap.get(handler).handle(in); } public static void registerHandler(Handler handler) { @@ -26,6 +29,6 @@ public class PacketHandler extends BasicListener { } static { - //TODO Register Handlers + registerHandler(new InventoryCallbackHandler()); } } From 51db5a8ad46701edc2d0284e7a9c0de531ce61ea Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 19 Sep 2020 12:38:14 +0200 Subject: [PATCH 05/11] Removed Debug Messages --- .../bungeecore/coms/handlers/InventoryCallbackHandler.java | 1 - src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java b/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java index 666a5a83..9a31f683 100644 --- a/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java +++ b/src/de/steamwar/bungeecore/coms/handlers/InventoryCallbackHandler.java @@ -25,7 +25,6 @@ public class InventoryCallbackHandler extends Handler { if(type == CallbackType.CLICK) { SWItem item = SWItem.getFromJson(byteArrayDataInput.readUTF()); InvCallback.ClickType clickType = InvCallback.ClickType.valueOf(byteArrayDataInput.readUTF()); - System.out.println(item.writeToString()); inventoryHashMap.get(owner).handleCallback(clickType, item); }else if(type == CallbackType.CLOSE) { inventoryHashMap.get(owner).handleClose(); diff --git a/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java b/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java index 3971952f..6dc5e1ab 100644 --- a/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java +++ b/src/de/steamwar/bungeecore/coms/receiver/PacketHandler.java @@ -16,7 +16,6 @@ public class PacketHandler extends BasicListener { @EventHandler public void onPluginMessage(PluginMessageEvent event) { - System.out.println(event.toString()); if(!event.getTag().equalsIgnoreCase("sw:return")) return; ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); From 7559012a9bfc707088f30d408e4c511e329dcdc9 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 20 Sep 2020 12:02:56 +0200 Subject: [PATCH 06/11] 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); + } +} From 70071893fa3b8ce1f6ded7cf3270669fd41f7dfa Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 20 Sep 2020 12:13:35 +0200 Subject: [PATCH 07/11] Add /bau delete Confirm GUI Add License --- src/de/steamwar/bungeecore/BungeeCore.java | 25 +------- .../bungeecore/commands/BauCommand.java | 57 +++++++++++-------- .../bungeecore/commands/TestCommand.java | 33 ----------- .../bungeecore/comms/BungeePacket.java | 19 +++++++ .../bungeecore/comms/SpigotHandler.java | 19 +++++++ .../bungeecore/comms/SpigotReceiver.java | 19 +++++++ .../handlers/InventoryCallbackHandler.java | 19 +++++++ .../comms/packets/CloseInventoryPacket.java | 19 +++++++ .../comms/packets/InventoryPacket.java | 19 +++++++ .../bungeecore/comms/packets/PingPacket.java | 19 +++++++ .../comms/packets/inventory/InvCallback.java | 19 +++++++ .../comms/packets/inventory/SWInventory.java | 19 +++++++ .../comms/packets/inventory/SWItem.java | 19 +++++++ .../comms/packets/inventory/SWListInv.java | 19 +++++++ .../bungeecore/sql/SchematicMember.java | 19 +++++++ 15 files changed, 262 insertions(+), 81 deletions(-) delete mode 100644 src/de/steamwar/bungeecore/commands/TestCommand.java diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index 93076b76..ab6d7f30 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -127,8 +127,6 @@ public class BungeeCore extends Plugin { new IgnoreCommand(); new UnIgnoreCommand(); - new TestCommand(); - new EventStarter(); new Broadcaster(); new SessionManager(); @@ -217,28 +215,7 @@ public class BungeeCore extends Plugin { ProxyServer.getInstance().stop("Could not save conig file"); return; } - try { - config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile); - } - catch (YAMLException e) { - e.printStackTrace(); - CHAT_PREFIX = "§eSteam§8War» §r"; - WORLD_FOLDER = "/home/minecraft/userworlds/"; - BAUWELT_PROTOTYP = "/home/minecraft/backbone/server/UserBau/Bauwelt2"; - LOBBY_SERVER = "Lobby"; - USERWORLDS15 = "/home/minecraft/userworlds15/"; - BAUWELT15 = "/home/minecraft/backbone/server/Bau15/Bauwelt"; - Broadcaster.setBroadCastMsgs(new String[] {""}); - Persistent.setChatPrefix(CHAT_PREFIX); - Persistent.setLobbyServer(LOBBY_SERVER); - - SQL.connect( - "jdbc:mysql://127.0.0.1:3306/developer", - Password.getUsername(), - Password.getPassword() - ); - return; - } + config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile); }catch(IOException e){ log("Could not save/load config.yml", e); ProxyServer.getInstance().stop(); diff --git a/src/de/steamwar/bungeecore/commands/BauCommand.java b/src/de/steamwar/bungeecore/commands/BauCommand.java index 18ab9beb..f50d456a 100644 --- a/src/de/steamwar/bungeecore/commands/BauCommand.java +++ b/src/de/steamwar/bungeecore/commands/BauCommand.java @@ -20,6 +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.sql.BauweltMember; import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.ProxyServer; @@ -226,32 +228,39 @@ public class BauCommand { } private static void delete(ProxiedPlayer p, String[] command){ - if(bau15(p, command, 2)){ - BungeeCore.send(p, BungeeCore.CHAT_PREFIX + "§aDeine Welt wird zurückgesetzt."); - ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { - for (Subserver subserver : Subserver.getServerList()) { - if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(p.getUniqueId())) { - subserver.stop(); - break; + SWInventory inventory = new SWInventory(p, 9, "§eDelete Confirm"); + inventory.addItem(1, new SWItem(1, "§cDecline", 1), click -> { + inventory.close(); + }); + inventory.addItem(7, new SWItem(7, "§aAccept", 10), click -> { + if(bau15(p, command, 2)){ + BungeeCore.send(p, BungeeCore.CHAT_PREFIX + "§aDeine Welt wird zurückgesetzt."); + ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { + for (Subserver subserver : Subserver.getServerList()) { + if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(p.getUniqueId())) { + subserver.stop(); + break; + } } - } - SteamwarUser user = SteamwarUser.get(p.getUniqueId()); - File directory = new File(BungeeCore.USERWORLDS15 + user.getId()); - del(directory); - }); - }else{ - BungeeCore.send(p, BungeeCore.CHAT_PREFIX + "§aDeine Welt wird zurückgesetzt."); - ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { - for (Subserver subserver : Subserver.getServerList()) { - if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(p.getUniqueId())) { - subserver.stop(); - break; + SteamwarUser user = SteamwarUser.get(p.getUniqueId()); + File directory = new File(BungeeCore.USERWORLDS15 + user.getId()); + del(directory); + }); + }else{ + BungeeCore.send(p, BungeeCore.CHAT_PREFIX + "§aDeine Welt wird zurückgesetzt."); + ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { + for (Subserver subserver : Subserver.getServerList()) { + if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(p.getUniqueId())) { + subserver.stop(); + break; + } } - } - File directory = new File(BungeeCore.WORLD_FOLDER + p.getUniqueId().toString()); - del(directory); - }); - } + File directory = new File(BungeeCore.WORLD_FOLDER + p.getUniqueId().toString()); + del(directory); + }); + } + }); + inventory.open(); } private static void testarena(ProxiedPlayer p, String[] command){ diff --git a/src/de/steamwar/bungeecore/commands/TestCommand.java b/src/de/steamwar/bungeecore/commands/TestCommand.java deleted file mode 100644 index 77d8419f..00000000 --- a/src/de/steamwar/bungeecore/commands/TestCommand.java +++ /dev/null @@ -1,33 +0,0 @@ -package de.steamwar.bungeecore.commands; - -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; - -public class TestCommand extends BasicCommand { - - public TestCommand() { - super("test", null); - } - - @Override - public void execute(CommandSender commandSender, String[] strings) { - if(!(commandSender instanceof ProxiedPlayer)) - return; - SWInventory inventory = new SWInventory((ProxiedPlayer) commandSender, 3*9, "Test Inventory"); - SWItem item = new SWItem("DIRT1", 13, "§e§lTest Item"); - item.setSkullOwner(commandSender.getName()); - for (ProxiedPlayer player: - ((ProxiedPlayer) commandSender).getServer().getInfo().getPlayers()) { - item.addLore("§6" + player.getDisplayName()); - } - item.setEnchanted(true); - item.setCallback(click -> { - inventory.close(); - commandSender.sendMessage("TEST!!! FJOnbds"); - }); - inventory.addItem(item); - inventory.open(); - } -} diff --git a/src/de/steamwar/bungeecore/comms/BungeePacket.java b/src/de/steamwar/bungeecore/comms/BungeePacket.java index 1233b302..8ccf0b7f 100644 --- a/src/de/steamwar/bungeecore/comms/BungeePacket.java +++ b/src/de/steamwar/bungeecore/comms/BungeePacket.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.bungeecore.comms; import com.google.common.io.ByteArrayDataOutput; diff --git a/src/de/steamwar/bungeecore/comms/SpigotHandler.java b/src/de/steamwar/bungeecore/comms/SpigotHandler.java index fafc9976..5c0f118c 100644 --- a/src/de/steamwar/bungeecore/comms/SpigotHandler.java +++ b/src/de/steamwar/bungeecore/comms/SpigotHandler.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.bungeecore.comms; import com.google.common.io.ByteArrayDataInput; diff --git a/src/de/steamwar/bungeecore/comms/SpigotReceiver.java b/src/de/steamwar/bungeecore/comms/SpigotReceiver.java index 2a5521ce..af2061e0 100644 --- a/src/de/steamwar/bungeecore/comms/SpigotReceiver.java +++ b/src/de/steamwar/bungeecore/comms/SpigotReceiver.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.bungeecore.comms; import com.google.common.io.ByteArrayDataInput; diff --git a/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java b/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java index 15eb08b8..2e7fb17c 100644 --- a/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.java +++ b/src/de/steamwar/bungeecore/comms/handlers/InventoryCallbackHandler.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.bungeecore.comms.handlers; import com.google.common.io.ByteArrayDataInput; diff --git a/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java b/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java index e84f6dae..fb8858b1 100644 --- a/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.java +++ b/src/de/steamwar/bungeecore/comms/packets/CloseInventoryPacket.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.bungeecore.comms.packets; import com.google.common.io.ByteArrayDataOutput; diff --git a/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java b/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java index 16417905..6c46ef86 100644 --- a/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java +++ b/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.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.bungeecore.comms.packets; import com.google.common.io.ByteArrayDataOutput; diff --git a/src/de/steamwar/bungeecore/comms/packets/PingPacket.java b/src/de/steamwar/bungeecore/comms/packets/PingPacket.java index ba435e67..dddb2727 100644 --- a/src/de/steamwar/bungeecore/comms/packets/PingPacket.java +++ b/src/de/steamwar/bungeecore/comms/packets/PingPacket.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.bungeecore.comms.packets; import com.google.common.io.ByteArrayDataOutput; diff --git a/src/de/steamwar/bungeecore/comms/packets/inventory/InvCallback.java b/src/de/steamwar/bungeecore/comms/packets/inventory/InvCallback.java index 71fd7672..666e456e 100644 --- a/src/de/steamwar/bungeecore/comms/packets/inventory/InvCallback.java +++ b/src/de/steamwar/bungeecore/comms/packets/inventory/InvCallback.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.bungeecore.comms.packets.inventory; public interface InvCallback { diff --git a/src/de/steamwar/bungeecore/comms/packets/inventory/SWInventory.java b/src/de/steamwar/bungeecore/comms/packets/inventory/SWInventory.java index 7b4c800a..8cb07287 100644 --- a/src/de/steamwar/bungeecore/comms/packets/inventory/SWInventory.java +++ b/src/de/steamwar/bungeecore/comms/packets/inventory/SWInventory.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.bungeecore.comms.packets.inventory; import de.steamwar.bungeecore.comms.handlers.InventoryCallbackHandler; diff --git a/src/de/steamwar/bungeecore/comms/packets/inventory/SWItem.java b/src/de/steamwar/bungeecore/comms/packets/inventory/SWItem.java index e2b58421..9a2a640c 100644 --- a/src/de/steamwar/bungeecore/comms/packets/inventory/SWItem.java +++ b/src/de/steamwar/bungeecore/comms/packets/inventory/SWItem.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.bungeecore.comms.packets.inventory; import com.google.gson.JsonArray; diff --git a/src/de/steamwar/bungeecore/comms/packets/inventory/SWListInv.java b/src/de/steamwar/bungeecore/comms/packets/inventory/SWListInv.java index b3e866be..5cd876a3 100644 --- a/src/de/steamwar/bungeecore/comms/packets/inventory/SWListInv.java +++ b/src/de/steamwar/bungeecore/comms/packets/inventory/SWListInv.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.bungeecore.comms.packets.inventory; import de.steamwar.bungeecore.sql.Schematic; diff --git a/src/de/steamwar/bungeecore/sql/SchematicMember.java b/src/de/steamwar/bungeecore/sql/SchematicMember.java index 7d3c9a5d..85b84855 100644 --- a/src/de/steamwar/bungeecore/sql/SchematicMember.java +++ b/src/de/steamwar/bungeecore/sql/SchematicMember.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.bungeecore.sql; import java.sql.ResultSet; From 7dd8d707a58e0d76e0ab1761986d96716731dd76 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 21 Sep 2020 23:23:39 +0200 Subject: [PATCH 08/11] 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"; From bd22287898844d4d89bd3628e4f3d8d17df3ee34 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 21 Sep 2020 23:34:26 +0200 Subject: [PATCH 09/11] Code Cleanup --- src/de/steamwar/bungeecore/sql/Schematic.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/sql/Schematic.java b/src/de/steamwar/bungeecore/sql/Schematic.java index 2ae7e513..37243da8 100644 --- a/src/de/steamwar/bungeecore/sql/Schematic.java +++ b/src/de/steamwar/bungeecore/sql/Schematic.java @@ -129,7 +129,7 @@ public class Schematic { ResultSet set = SQL.select("SELECT Item WHERE SchemID = ?", schemID); return set.getString("Item"); } catch (SQLException throwables) { - return "CAULDRON_ITEM"; + throw new SecurityException("Could not load Item"); } } From 8708b694dadbaaff3e5ebc67c6a748f1f1c27a54 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 22 Sep 2020 13:38:30 +0200 Subject: [PATCH 10/11] Code Cleanup --- src/de/steamwar/bungeecore/comms/SpigotReceiver.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/comms/SpigotReceiver.java b/src/de/steamwar/bungeecore/comms/SpigotReceiver.java index d747d154..9a8da48a 100644 --- a/src/de/steamwar/bungeecore/comms/SpigotReceiver.java +++ b/src/de/steamwar/bungeecore/comms/SpigotReceiver.java @@ -37,8 +37,10 @@ public class SpigotReceiver extends BasicListener { public void onPluginMessage(PluginMessageEvent event) { if(!event.getTag().equalsIgnoreCase("sw:bridge")) return; - if(!event.getSender().getAddress().getHostName().equals("localhost")) + if(!event.getSender().getAddress().getHostName().equals("localhost")){ + event.setCancelled(true); return; + } ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); Byte handler = in.readByte(); handlerMap.get(handler).handle(in); From 30290b33b614c5de38fdef944cd6bebcb42d6d07 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 24 Sep 2020 14:10:15 +0200 Subject: [PATCH 11/11] Code Cleanup --- .../bungeecore/commands/BauCommand.java | 48 ++++++++----------- .../bungeecore/comms/PacketIdManager.java | 19 ++++++++ .../comms/packets/InventoryPacket.java | 11 ++++- .../bungeecore/inventory/SWInventory.java | 25 ++++++++-- src/de/steamwar/bungeecore/sql/Schematic.java | 25 +++++----- 5 files changed, 81 insertions(+), 47 deletions(-) diff --git a/src/de/steamwar/bungeecore/commands/BauCommand.java b/src/de/steamwar/bungeecore/commands/BauCommand.java index 98aad176..043ddfb3 100644 --- a/src/de/steamwar/bungeecore/commands/BauCommand.java +++ b/src/de/steamwar/bungeecore/commands/BauCommand.java @@ -228,41 +228,33 @@ public class BauCommand { } private static void delete(ProxiedPlayer p, String[] command){ - SWInventory inventory = new SWInventory(p, 9, "§e/Bau delete Bestätigen"); - inventory.addItem(0, new SWItem(0, "§cAbbrechen", 1), click -> { - inventory.close(); - }); - inventory.addItem(8, new SWItem(8, "§aLöchen", 10), click -> { + SWInventory inventory = new SWInventory(p, 9, "§eWirklich Welt löschen?"); + inventory.addItem(8, new SWItem(8, "§cAbbrechen", 1), click -> inventory.close()); + inventory.addItem(1, new SWItem(1, "§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(), () -> { - for (Subserver subserver : Subserver.getServerList()) { - if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(p.getUniqueId())) { - subserver.stop(); - break; - } - } - SteamwarUser user = SteamwarUser.get(p.getUniqueId()); - File directory = new File(BungeeCore.USERWORLDS15 + user.getId()); - del(directory); - }); + SteamwarUser user = SteamwarUser.get(p.getUniqueId()); + deleteWorld(p, BungeeCore.USERWORLDS15 + user.getId()); }else{ - BungeeCore.send(p, BungeeCore.CHAT_PREFIX + "§aDeine Welt wird zurückgesetzt."); - ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { - for (Subserver subserver : Subserver.getServerList()) { - if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(p.getUniqueId())) { - subserver.stop(); - break; - } - } - File directory = new File(BungeeCore.WORLD_FOLDER + p.getUniqueId().toString()); - del(directory); - }); + deleteWorld(p, BungeeCore.WORLD_FOLDER + p.getUniqueId().toString()); } }); inventory.open(); } + private static void deleteWorld(ProxiedPlayer player, String world) { + BungeeCore.send(player, BungeeCore.CHAT_PREFIX + "§aDeine Welt wird zurückgesetzt."); + ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { + for (Subserver subserver : Subserver.getServerList()) { + if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(player.getUniqueId())) { + subserver.stop(); + break; + } + } + File directory = new File(world); + del(directory); + }); + } + private static void testarena(ProxiedPlayer p, String[] command){ FightCommand.createArena(p, "/bau testarena ", command, 2, false, (player, mode, map) -> ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { for (Subserver subserver : Subserver.getServerList()) { diff --git a/src/de/steamwar/bungeecore/comms/PacketIdManager.java b/src/de/steamwar/bungeecore/comms/PacketIdManager.java index 13f951a4..32d25e3b 100644 --- a/src/de/steamwar/bungeecore/comms/PacketIdManager.java +++ b/src/de/steamwar/bungeecore/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.bungeecore.comms; public class PacketIdManager { diff --git a/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java b/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java index db5282a6..4a37d1c8 100644 --- a/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java +++ b/src/de/steamwar/bungeecore/comms/packets/InventoryPacket.java @@ -23,19 +23,21 @@ import com.google.common.io.ByteArrayDataOutput; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import de.steamwar.bungeecore.comms.PacketIdManager; +import de.steamwar.bungeecore.inventory.SWInventory; 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; import java.util.HashMap; +import java.util.Map; public class InventoryPacket extends BungeePacket { final String title; final int player; final int size; - final HashMap items; + final Map items; public InventoryPacket(int size, String title, ProxiedPlayer player) { items = new HashMap<>(); @@ -44,6 +46,13 @@ public class InventoryPacket extends BungeePacket { this.player = SteamwarUser.get(player).getId(); } + public InventoryPacket(SWInventory inventory) { + items = inventory.getItems(); + this.title = inventory.getTitle(); + this.size = inventory.getSize(); + this.player = SteamwarUser.get(inventory.getPlayer()).getId(); + } + @Override public int getId() { return PacketIdManager.INVENTORY_PACKET; diff --git a/src/de/steamwar/bungeecore/inventory/SWInventory.java b/src/de/steamwar/bungeecore/inventory/SWInventory.java index c76b6a4e..6a308cff 100644 --- a/src/de/steamwar/bungeecore/inventory/SWInventory.java +++ b/src/de/steamwar/bungeecore/inventory/SWInventory.java @@ -96,6 +96,10 @@ public class SWInventory { return next; } + public Map getItems() { + return itemMap; + } + public void handleCallback(InvCallback.ClickType type, int pos) { itemMap.get(pos).getCallback().clicked(type); } @@ -107,14 +111,27 @@ public class SWInventory { } public void open() { - InventoryPacket inv = new InventoryPacket(size, title, player); - for (Map.Entry entry : itemMap.entrySet()) { - inv.setItem(entry.getKey(), entry.getValue()); - } + InventoryPacket inv = new InventoryPacket(this); inv.send(player); } public void close() { new CloseInventoryPacket(player).send(player); } + + public void setClose(InvCallback close) { + this.close = close; + } + + public ProxiedPlayer getPlayer() { + return player; + } + + public void setPlayer(ProxiedPlayer player) { + this.player = player; + } + + public String getTitle() { + return title; + } } diff --git a/src/de/steamwar/bungeecore/sql/Schematic.java b/src/de/steamwar/bungeecore/sql/Schematic.java index 37243da8..9d25a384 100644 --- a/src/de/steamwar/bungeecore/sql/Schematic.java +++ b/src/de/steamwar/bungeecore/sql/Schematic.java @@ -30,12 +30,14 @@ public class Schematic { private final String schemName; private final int schemOwner; private SchematicType schemType; + private String schemItem; private Schematic(ResultSet rs) throws SQLException { this.schemID = rs.getInt("SchemID"); this.schemName = rs.getString("SchemName"); this.schemOwner = rs.getInt("SchemOwner"); this.schemType = SchematicType.fromDB(rs.getString("SchemType")); + this.schemItem = rs.getString("Item"); } public static Schematic getSchemFromDB(String schemName, UUID schemOwner){ @@ -43,7 +45,7 @@ public class Schematic { } public static Schematic getSchemFromDB(String schemName, int schemOwner){ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType FROM Schematic WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner); + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner); try { if(schematic == null || !schematic.next()){ return null; @@ -55,7 +57,7 @@ public class Schematic { } public static Schematic getSchemFromDB(int schemID){ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType FROM Schematic WHERE SchemID = ?", schemID); + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemID = ?", schemID); try { if(!schematic.next()) throw new SecurityException("Failed loading schematic " + schemID); @@ -67,7 +69,7 @@ public class Schematic { public static List getAllSchemsOfType(SchematicType schemType){ try{ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType FROM Schematic WHERE SchemType = ?", schemType.toDB()); + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemType = ?", schemType.toDB()); List schematics = new ArrayList<>(); while(schematic.next()){ schematics.add(new Schematic(schematic)); @@ -80,7 +82,7 @@ public class Schematic { public static List getSchemsOfType(int userId, SchematicType schemType){ try{ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType FROM Schematic WHERE SchemType = ? AND SchemOwner = ?", schemType.toDB(), userId); + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemType = ? AND SchemOwner = ?", schemType.toDB(), userId); List schematics = new ArrayList<>(); while(schematic.next()){ schematics.add(new Schematic(schematic)); @@ -93,7 +95,7 @@ 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); + ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, Rank, SchemFormat, Item FROM Schematic WHERE SchemOwner = ? ORDER BY SchemName", schemOwner); List schematics = new ArrayList<>(); while(schematic.next()){ schematics.add(new Schematic(schematic)); @@ -120,17 +122,12 @@ public class Schematic { return schemOwner; } - public SchematicType getSchemType() { - return schemType; + public String getSchemItem() { + return schemItem; } - public String getSchemItem() { - try { - ResultSet set = SQL.select("SELECT Item WHERE SchemID = ?", schemID); - return set.getString("Item"); - } catch (SQLException throwables) { - throw new SecurityException("Could not load Item"); - } + public SchematicType getSchemType() { + return schemType; } public void setSchemType(SchematicType schemType) {