From 1b10f583394438c49a5e9d4216ad7cf68da715dc Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 7 Jun 2022 20:27:09 +0200 Subject: [PATCH] :tada: Rework Server-Bungee Networking --- CommonCore | 2 +- SpigotCore_Main/build.gradle | 2 + .../src/de/steamwar/comms/BungeeReceiver.java | 70 ------- .../de/steamwar/comms/PacketIdManager.java | 42 ----- .../comms/handlers/BungeeHandler.java | 27 --- .../comms/handlers/InventoryHandler.java | 62 ------- .../handlers/LocaleInvalidationHandler.java | 31 ---- .../comms/packets/ExecuteCommandPacket.java | 47 ----- .../comms/packets/FightEndsPacket.java | 76 -------- .../comms/packets/FightInfoPacket.java | 174 ------------------ .../comms/packets/ImALobbyPacket.java | 36 ---- .../packets/InventoryCallbackPacket.java | 90 --------- .../comms/packets/PrepareSchemPacket.java | 51 ----- .../steamwar/comms/packets/SpigotPacket.java | 39 ---- .../comms/packets/TablistNamePacket.java | 26 --- .../src/de/steamwar/core/Core.java | 4 +- .../steamwar/network/CoreNetworkHandler.java | 52 ++++++ .../de/steamwar/network/NetworkReceiver.java | 17 ++ .../de/steamwar/network/NetworkSender.java | 19 ++ .../network/handlers/InventoryHandler.java | 37 ++++ 20 files changed, 130 insertions(+), 774 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/handlers/BungeeHandler.java delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/handlers/LocaleInvalidationHandler.java delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/packets/ExecuteCommandPacket.java delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/packets/FightEndsPacket.java delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/packets/FightInfoPacket.java delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/packets/ImALobbyPacket.java delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java delete mode 100644 SpigotCore_Main/src/de/steamwar/comms/packets/TablistNamePacket.java create mode 100644 SpigotCore_Main/src/de/steamwar/network/CoreNetworkHandler.java create mode 100644 SpigotCore_Main/src/de/steamwar/network/NetworkReceiver.java create mode 100644 SpigotCore_Main/src/de/steamwar/network/NetworkSender.java create mode 100644 SpigotCore_Main/src/de/steamwar/network/handlers/InventoryHandler.java diff --git a/CommonCore b/CommonCore index 919c4d5..492894c 160000 --- a/CommonCore +++ b/CommonCore @@ -1 +1 @@ -Subproject commit 919c4d525ea39756b24bd961bb72d8d58bdd5bd4 +Subproject commit 492894ca8d41ee0bde4dcb9d520db5f7478c50c7 diff --git a/SpigotCore_Main/build.gradle b/SpigotCore_Main/build.gradle index d421a14..fe306ca 100644 --- a/SpigotCore_Main/build.gradle +++ b/SpigotCore_Main/build.gradle @@ -62,6 +62,8 @@ dependencies { testImplementation 'junit:junit:4.13.2' testImplementation 'org.hamcrest:hamcrest:2.2' + + implementation project(':CommonCore') } processResources { diff --git a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java deleted file mode 100644 index e7a0b8c..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.comms; - -import com.google.common.io.ByteArrayDataInput; -import com.google.common.io.ByteStreams; -import de.steamwar.comms.handlers.BungeeHandler; -import de.steamwar.comms.handlers.InventoryHandler; -import de.steamwar.comms.handlers.LocaleInvalidationHandler; -import de.steamwar.core.BountifulWrapper; -import de.steamwar.sql.BauweltMember; -import de.steamwar.sql.SteamwarUser; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.plugin.messaging.PluginMessageListener; - -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -public class BungeeReceiver implements PluginMessageListener { - - private static Map handlerMap = new HashMap<>(); - - public static void registerHandler(Byte code, BungeeHandler handler) { - handlerMap.put(code, handler); - } - - static { - registerHandler(PacketIdManager.PING_PACKET, byteArrayDataInput -> { - UUID uuid = SteamwarUser.get(byteArrayDataInput.readInt()).getUUID(); - if(Bukkit.getPlayer(uuid).isOnline()) { - Player player = Bukkit.getPlayer(uuid); - BountifulWrapper.impl.playPling(player); - } - - }); - registerHandler(PacketIdManager.BAUMEMBER_UPDATE, byteArrayDataInput -> BauweltMember.clear()); - registerHandler(PacketIdManager.INVENTORY_PACKET, new InventoryHandler()); - registerHandler(PacketIdManager.INVENTORY_CLOSE_PACKET, byteArrayDataInput -> { - Player player = Bukkit.getPlayer(SteamwarUser.get(byteArrayDataInput.readInt()).getUUID()); - player.closeInventory(); - }); - registerHandler(PacketIdManager.LOCALE_INVALIDATION, new LocaleInvalidationHandler()); - } - - @Override - public void onPluginMessageReceived(String s, Player player, byte[] bytes) { - ByteArrayDataInput in = ByteStreams.newDataInput(bytes); - Byte handler = in.readByte(); - handlerMap.get(handler).handle(in); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java deleted file mode 100644 index 7020c68..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.comms; - -public class PacketIdManager { - private PacketIdManager(){} - - //0x0(X) Standalone Packets - public static final byte PING_PACKET = 0x01; - public static final byte TABLIST_NAME = 0x02; - public static final byte PREPARE_SCHEM = 0x03; - public static final byte BAUMEMBER_UPDATE = 0x04; - public static final byte EXECUTE_COMMAND = 0x05; - public static final byte LOCALE_INVALIDATION = 0x06; - - //0x1(X) Bungee Inventory - public static final byte INVENTORY_PACKET = 0x10; - public static final byte INVENTORY_CALLBACK_PACKET = 0x11; - public static final byte INVENTORY_CLOSE_PACKET = 0x12; - //0x2(X) Server Information System - public static final byte I_AM_A_LOBBY = 0x20; - public static final byte FIGHT_INFO = 0x21; - public static final byte FIGHT_ENDS = 0x22; - public static final byte STARTING_SERVER = 0x23; -} diff --git a/SpigotCore_Main/src/de/steamwar/comms/handlers/BungeeHandler.java b/SpigotCore_Main/src/de/steamwar/comms/handlers/BungeeHandler.java deleted file mode 100644 index 8907fee..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/handlers/BungeeHandler.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.comms.handlers; - -import com.google.common.io.ByteArrayDataInput; - -public interface BungeeHandler { - - void handle(ByteArrayDataInput byteArrayDataInput); -} \ No newline at end of file diff --git a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java b/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java deleted file mode 100644 index 98f545f..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/handlers/InventoryHandler.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.comms.handlers; - -import com.google.common.io.ByteArrayDataInput; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import de.steamwar.comms.packets.InventoryCallbackPacket; -import de.steamwar.inventory.SWInventory; -import de.steamwar.inventory.SWItem; -import de.steamwar.sql.SteamwarUser; -import org.bukkit.Bukkit; -import org.bukkit.event.inventory.InventoryType; - -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -public class InventoryHandler implements BungeeHandler { - - @Override - public void handle(ByteArrayDataInput byteArrayDataInput) { - JsonObject object = new JsonParser().parse(byteArrayDataInput.readUTF()).getAsJsonObject(); - UUID player = SteamwarUser.get(object.get("id").getAsInt()).getUUID(); - String title = object.get("title").getAsString(); - int size = object.get("size").getAsInt(); - int length = object.get("itemcount").getAsInt(); - Map items = new HashMap<>(); - JsonArray array = object.get("items").getAsJsonArray(); - for (int i = 0; i < length; i++) { - JsonObject itemJson = array.get(i).getAsJsonObject(); - SWItem item = SWItem.getItemFromJson(itemJson); - item.setCallback(click -> new InventoryCallbackPacket(itemJson, click, player).send(Bukkit.getPlayer(player))); - items.put(itemJson.get("position").getAsInt(), item); - } - - SWInventory inventory = new SWInventory(Bukkit.getPlayer(player), size, title, items); - inventory.addCloseCallback(click -> { - if(Bukkit.getPlayer(player).getOpenInventory().getType() != InventoryType.CHEST) - new InventoryCallbackPacket(player).send(Bukkit.getPlayer(player)); - }); - inventory.open(); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/comms/handlers/LocaleInvalidationHandler.java b/SpigotCore_Main/src/de/steamwar/comms/handlers/LocaleInvalidationHandler.java deleted file mode 100644 index 3ebaaa1..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/handlers/LocaleInvalidationHandler.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2020 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.comms.handlers; - -import com.google.common.io.ByteArrayDataInput; -import de.steamwar.sql.SteamwarUser; - -public class LocaleInvalidationHandler implements BungeeHandler { - - @Override - public void handle(ByteArrayDataInput byteArrayDataInput) { - SteamwarUser.invalidate(byteArrayDataInput.readInt()); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/ExecuteCommandPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/ExecuteCommandPacket.java deleted file mode 100644 index bede2b2..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/ExecuteCommandPacket.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2021 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.comms.packets; - -import com.google.common.io.ByteArrayDataOutput; -import de.steamwar.comms.PacketIdManager; -import de.steamwar.sql.SteamwarUser; -import org.bukkit.entity.Player; - -public class ExecuteCommandPacket extends SpigotPacket { - - private final SteamwarUser user; - private final String command; - - public ExecuteCommandPacket(Player player, String command) { - this.user = SteamwarUser.get(player.getUniqueId()); - this.command = command; - } - - @Override - public int getName() { - return PacketIdManager.EXECUTE_COMMAND; - } - - @Override - public void writeVars(ByteArrayDataOutput out) { - out.writeInt(user.getId()); - out.writeUTF(command); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/FightEndsPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/FightEndsPacket.java deleted file mode 100644 index 5bd976f..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/FightEndsPacket.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2020 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.comms.packets; - -import com.google.common.io.ByteArrayDataInput; -import com.google.common.io.ByteArrayDataOutput; -import de.steamwar.comms.PacketIdManager; -import lombok.AllArgsConstructor; -import lombok.Getter; - -import java.util.List; - -@AllArgsConstructor -@Getter -public class FightEndsPacket extends SpigotPacket { - - private byte win; - private int blueSchem; - private int redSchem; - private List bluePlayers; - private List redPlayers; - private String gameMode; - - public FightEndsPacket(ByteArrayDataInput byteArrayDataInput) { - win = byteArrayDataInput.readByte(); - blueSchem = byteArrayDataInput.readInt(); - redSchem = byteArrayDataInput.readInt(); - int blueSize = byteArrayDataInput.readInt(); - for (int i = 0; i < blueSize; i++) { - bluePlayers.add(byteArrayDataInput.readInt()); - } - int redSize = byteArrayDataInput.readInt(); - for (int i = 0; i < redSize; i++) { - redPlayers.add(byteArrayDataInput.readInt()); - } - gameMode = byteArrayDataInput.readUTF(); - } - - @Override - public int getName() { - return PacketIdManager.FIGHT_ENDS; - } - - @Override - public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - byteArrayDataOutput.writeByte(win); - byteArrayDataOutput.writeInt(blueSchem); - byteArrayDataOutput.writeInt(redSchem); - byteArrayDataOutput.writeInt(bluePlayers.size()); - for (int i : bluePlayers) { - byteArrayDataOutput.writeInt(i); - } - byteArrayDataOutput.writeInt(redPlayers.size()); - for (int i : redPlayers) { - byteArrayDataOutput.writeInt(i); - } - byteArrayDataOutput.writeUTF(gameMode); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/FightInfoPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/FightInfoPacket.java deleted file mode 100644 index 54b9498..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/FightInfoPacket.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.comms.packets; - -import com.google.common.io.ByteArrayDataInput; -import com.google.common.io.ByteArrayDataOutput; -import de.steamwar.comms.PacketIdManager; - -import java.util.ArrayList; -import java.util.List; - -public class FightInfoPacket extends SpigotPacket { - - private final String serverName; // Name of the Server, might be changed by Bungee - private final String gameMode; // GameMode aka Schematictype (if known, else "") - private final String arena; // Name of the arena - private final String blueName; // Name of the blue team, expected to begin with "§a" colorcode - private final String redName; // Name of the red team, expected to begin with "§a" colorcode - private final String fightState; // Fight state (technical term) (if known, else "") - private final int countdown; // Countdown state in seconds (if known, else 0) - private final int blueLeader; // SWUserID of the blue team leader (if known, else 0) - private final int redLeader; // SWUserID of the red team leader (if known, else 0) - private final int blueSchem; // Blue SchemID (if known, else 0) - private final int redSchem; // Red SchemID (if known, else 0) - private final List bluePlayers; // List of Blue SWUserIDs - private final List redPlayers; // List of Red SWUserIDs - private final List spectators; // List of Spectator SWUserIDs - - public FightInfoPacket(String serverName, String gameMode, String arena, String blueName, String redName, String fightState, int countdown, int blueLeader, int redLeader, int blueSchem, int redSchem, List bluePlayers, List redPlayers, List spectators) { - this.serverName = serverName; - this.gameMode = gameMode; - this.arena = arena; - this.blueName = blueName; - this.redName = redName; - this.fightState = fightState; - this.countdown = countdown; - this.blueLeader = blueLeader; - this.redLeader = redLeader; - this.blueSchem = blueSchem; - this.redSchem = redSchem; - this.bluePlayers = bluePlayers; - this.redPlayers = redPlayers; - this.spectators = spectators; - } - - public FightInfoPacket(ByteArrayDataInput in) { - this.serverName = in.readUTF(); - this.gameMode = in.readUTF(); - this.arena = in.readUTF(); - this.blueName = in.readUTF(); - this.redName = in.readUTF(); - this.fightState = in.readUTF(); - this.countdown = in.readInt(); - this.blueLeader = in.readInt(); - this.redLeader = in.readInt(); - this.blueSchem = in.readInt(); - this.redSchem = in.readInt(); - this.bluePlayers = readPlayerList(in); - this.redPlayers = readPlayerList(in); - this.spectators = readPlayerList(in); - } - - @Override - public int getName() { - return PacketIdManager.FIGHT_INFO; - } - - @Override - public void writeVars(ByteArrayDataOutput out) { - out.writeUTF(serverName); - out.writeUTF(gameMode); - out.writeUTF(arena); - out.writeUTF(blueName); - out.writeUTF(redName); - out.writeUTF(fightState); - out.writeInt(countdown); - out.writeInt(blueLeader); - out.writeInt(redLeader); - out.writeInt(blueSchem); - out.writeInt(redSchem); - writePlayerList(out, bluePlayers); - writePlayerList(out, redPlayers); - writePlayerList(out, spectators); - } - - public String getServerName() { - return serverName; - } - - public String getGameMode() { - return gameMode; - } - - public String getArena() { - return arena; - } - - public String getBlueName() { - return blueName; - } - - public String getRedName() { - return redName; - } - - public String getFightState() { - return fightState; - } - - public int getCountdown() { - return countdown; - } - - public int getBlueLeader() { - return blueLeader; - } - - public int getRedLeader() { - return redLeader; - } - - public int getBlueSchem() { - return blueSchem; - } - - public int getRedSchem() { - return redSchem; - } - - public List getBluePlayers() { - return bluePlayers; - } - - public List getRedPlayers() { - return redPlayers; - } - - public List getSpectators() { - return spectators; - } - - private static List readPlayerList(ByteArrayDataInput in) { - int length = in.readInt(); - List players = new ArrayList<>(length); - for(int i = 0; i < length; i++) { - players.add(in.readInt()); - } - return players; - } - - private void writePlayerList(ByteArrayDataOutput out, List players) { - out.writeInt(players.size()); - for(Integer player : players) { - out.writeInt(player); - } - } -} diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/ImALobbyPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/ImALobbyPacket.java deleted file mode 100644 index dbe7c0a..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/ImALobbyPacket.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.comms.packets; - -import com.google.common.io.ByteArrayDataOutput; -import de.steamwar.comms.PacketIdManager; - -public class ImALobbyPacket extends SpigotPacket { - - @Override - public int getName() { - return PacketIdManager.I_AM_A_LOBBY; - } - - @Override - public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - //no content - } -} diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java deleted file mode 100644 index 6ba1c42..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/InventoryCallbackPacket.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.comms.packets; - -import com.google.common.io.ByteArrayDataOutput; -import com.google.gson.JsonObject; -import de.steamwar.comms.PacketIdManager; -import de.steamwar.sql.SteamwarUser; -import org.bukkit.event.inventory.ClickType; - -import java.util.UUID; - -public class InventoryCallbackPacket extends SpigotPacket { - - int position = -1; - ClickType clickType; - CallbackType callbackType; - int owner; - - @Override - public int getName() { - return PacketIdManager.INVENTORY_CALLBACK_PACKET; - } - - @Override - public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - byteArrayDataOutput.writeInt(owner); - byteArrayDataOutput.writeUTF(callbackType.name()); - if(position != -1){ - byteArrayDataOutput.writeInt(position); - byteArrayDataOutput.writeUTF(clickType.name()); - } - } - - public InventoryCallbackPacket() {} - - public InventoryCallbackPacket(JsonObject itemJson, ClickType click, UUID player) { - setPosition(itemJson.get("position").getAsInt()); - setClick(click); - setCallback(InventoryCallbackPacket.CallbackType.CLICK); - setOwner(SteamwarUser.get(player).getId()); - } - - public InventoryCallbackPacket(UUID player) { - setCallback(CallbackType.CLOSE); - setOwner(SteamwarUser.get(player).getId()); - } - - public InventoryCallbackPacket setPosition(int position) { - this.position = position; - return this; - } - - public InventoryCallbackPacket setClick(ClickType type){ - this.clickType = type; - return this; - } - - public InventoryCallbackPacket setCallback(CallbackType callback) { - callbackType = callback; - return this; - } - - public InventoryCallbackPacket setOwner(int id) { - this.owner = id; - return this; - } - - public enum CallbackType { - CLICK, - CLOSE, - } -} diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java deleted file mode 100644 index aba2380..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/PrepareSchemPacket.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.comms.packets; - -import com.google.common.io.ByteArrayDataOutput; -import de.steamwar.comms.PacketIdManager; -import de.steamwar.sql.SchematicNode; -import de.steamwar.sql.SchematicType; -import de.steamwar.sql.SteamwarUser; - -public class PrepareSchemPacket extends SpigotPacket{ - - private final SteamwarUser user; - private final SchematicNode schematic; - private final SchematicType schematicType; - - public PrepareSchemPacket(SteamwarUser user, SchematicNode schematic, SchematicType schematicType) { - this.user = user; - this.schematic = schematic; - this.schematicType = schematicType; - } - - @Override - public int getName() { - return PacketIdManager.PREPARE_SCHEM; - } - - @Override - public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - byteArrayDataOutput.writeInt(user.getId()); - byteArrayDataOutput.writeInt(schematic.getId()); - byteArrayDataOutput.writeUTF(schematicType.toDB()); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java deleted file mode 100644 index 25b5ecb..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/SpigotPacket.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.comms.packets; - -import com.google.common.io.ByteArrayDataOutput; -import com.google.common.io.ByteStreams; -import de.steamwar.core.Core; -import org.bukkit.entity.Player; - -public abstract class SpigotPacket { - - public void send(Player player) { - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeByte(getName()); - writeVars(out); - player.sendPluginMessage(Core.getInstance(), "sw:bridge", out.toByteArray()); - } - - public abstract int getName(); - - public abstract void writeVars(ByteArrayDataOutput byteArrayDataOutput); -} diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/TablistNamePacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/TablistNamePacket.java deleted file mode 100644 index 7f5454e..0000000 --- a/SpigotCore_Main/src/de/steamwar/comms/packets/TablistNamePacket.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.steamwar.comms.packets; - -import com.google.common.io.ByteArrayDataOutput; -import de.steamwar.comms.PacketIdManager; - -public class TablistNamePacket extends SpigotPacket { - - final int swUserId; - final String tablistName; - - public TablistNamePacket(int swUserId, String tablistName){ - this.swUserId = swUserId; - this.tablistName = tablistName; - } - - @Override - public int getName() { - return PacketIdManager.TABLIST_NAME; - } - - @Override - public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { - byteArrayDataOutput.writeInt(swUserId); - byteArrayDataOutput.writeUTF(tablistName); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index 111d344..4bb0a70 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -23,12 +23,12 @@ import com.comphenix.tinyprotocol.TinyProtocol; import de.steamwar.command.SWCommandUtils; import de.steamwar.command.SWTypeMapperCreator; import de.steamwar.command.TypeMapper; -import de.steamwar.comms.BungeeReceiver; import de.steamwar.core.authlib.AuthlibInjector; import de.steamwar.core.events.ChattingEvent; import de.steamwar.core.events.PlayerJoinedEvent; import de.steamwar.core.events.WorldLoadEvent; import de.steamwar.message.Message; +import de.steamwar.network.NetworkReceiver; import de.steamwar.sql.Statement; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -118,7 +118,7 @@ public class Core extends JavaPlugin{ Bukkit.getPluginManager().registerEvents(new PlayerJoinedEvent(), this); Bukkit.getPluginManager().registerEvents(new ChattingEvent(), this); Bukkit.getPluginManager().registerEvents(new WorldLoadEvent(), this); - getServer().getMessenger().registerIncomingPluginChannel(this, "sw:bridge", new BungeeReceiver()); + getServer().getMessenger().registerIncomingPluginChannel(this, "sw:bridge", new NetworkReceiver()); getServer().getMessenger().registerOutgoingPluginChannel(this, "sw:bridge"); AuthlibInjector.inject(); TinyProtocol.init(); diff --git a/SpigotCore_Main/src/de/steamwar/network/CoreNetworkHandler.java b/SpigotCore_Main/src/de/steamwar/network/CoreNetworkHandler.java new file mode 100644 index 0000000..ee55a64 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/network/CoreNetworkHandler.java @@ -0,0 +1,52 @@ +package de.steamwar.network; + +import de.steamwar.core.BountifulWrapper; +import de.steamwar.network.handlers.InventoryHandler; +import de.steamwar.network.packets.PacketHandler; +import de.steamwar.network.packets.server.*; +import de.steamwar.sql.BauweltMember; +import de.steamwar.sql.SteamwarUser; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.util.UUID; + +public class CoreNetworkHandler extends PacketHandler { + + public CoreNetworkHandler() { + super(); + new InventoryHandler().register(); + } + + @Handler + public void handleBaumemberUpdatePacket(BaumemberUpdatePacket packet) { + BauweltMember.clear(); + } + + @Handler + public void handleCloseInventoryPacket(CloseInventoryPacket packet) { + Player player = Bukkit.getPlayer(SteamwarUser.get(packet.getPlayerId()).getUUID()); + if (player != null) { + player.closeInventory(); + } + } + + @Handler + public void handleInventoryPacket(InventoryPacket packet) { + InventoryHandler.handleInventoryPacket(packet); + } + + @Handler + public void handlePingPacket(PingPacket packet) { + UUID uuid = SteamwarUser.get(packet.getId()).getUUID(); + if(Bukkit.getPlayer(uuid) != null) { + Player player = Bukkit.getPlayer(uuid); + BountifulWrapper.impl.playPling(player); + } + } + + @Handler + public void handleLocaleChange(LocaleInvalidationPacket packet) { + SteamwarUser.invalidate(packet.getPlayerId()); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/network/NetworkReceiver.java b/SpigotCore_Main/src/de/steamwar/network/NetworkReceiver.java new file mode 100644 index 0000000..396d33f --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/network/NetworkReceiver.java @@ -0,0 +1,17 @@ +package de.steamwar.network; + +import de.steamwar.network.packets.NetworkPacket; +import org.bukkit.entity.Player; +import org.bukkit.plugin.messaging.PluginMessageListener; + +public class NetworkReceiver implements PluginMessageListener { + + public NetworkReceiver() { + new CoreNetworkHandler().register(); + } + + @Override + public void onPluginMessageReceived(String channel, Player player, byte[] message) { + NetworkPacket.handle(message); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/network/NetworkSender.java b/SpigotCore_Main/src/de/steamwar/network/NetworkSender.java new file mode 100644 index 0000000..c9d3060 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/network/NetworkSender.java @@ -0,0 +1,19 @@ +package de.steamwar.network; + +import de.steamwar.core.Core; +import de.steamwar.network.packets.NetworkPacket; +import lombok.SneakyThrows; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +public class NetworkSender { + + public static void send(NetworkPacket packet) { + Bukkit.getOnlinePlayers().stream().findAny().ifPresent(player -> send(packet, player)); + } + + @SneakyThrows + public static void send(NetworkPacket packet, Player player) { + player.sendPluginMessage(Core.getInstance(), "sw:bridge", packet.serialize()); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/network/handlers/InventoryHandler.java b/SpigotCore_Main/src/de/steamwar/network/handlers/InventoryHandler.java new file mode 100644 index 0000000..d1c1612 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/network/handlers/InventoryHandler.java @@ -0,0 +1,37 @@ +package de.steamwar.network.handlers; + +import com.google.gson.JsonParser; +import de.steamwar.inventory.SWInventory; +import de.steamwar.inventory.SWItem; +import de.steamwar.network.NetworkSender; +import de.steamwar.network.packets.PacketHandler; +import de.steamwar.network.packets.client.InventoryCallbackPacket; +import de.steamwar.network.packets.server.InventoryPacket; +import de.steamwar.sql.SteamwarUser; +import org.bukkit.Bukkit; +import org.bukkit.event.inventory.InventoryType; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class InventoryHandler extends PacketHandler { + + @Handler + public static void handleInventoryPacket(InventoryPacket packet) { + Map items = new HashMap<>(); + packet.getItems().forEach((i, item) -> { + SWItem it = SWItem.getItemFromJson(JsonParser.parseString(item).getAsJsonObject()); + it.setCallback(click -> NetworkSender.send(InventoryCallbackPacket.builder().owner(packet.getPlayer()).position(i).type(InventoryCallbackPacket.CallbackType.CLICK).clickType(InventoryCallbackPacket.ClickType.getByName(click.name())).build())); + items.put(i, it); + }); + UUID player = SteamwarUser.get(packet.getPlayer()).getUUID(); + + SWInventory inventory = new SWInventory(Bukkit.getPlayer(player), packet.getSize(), packet.getTitle(), items); + inventory.addCloseCallback(click -> { + if(Bukkit.getPlayer(player).getOpenInventory().getType() != InventoryType.CHEST) + NetworkSender.send(InventoryCallbackPacket.builder().owner(packet.getPlayer()).type(InventoryCallbackPacket.CallbackType.CLOSE).build()); + }); + inventory.open(); + } +}