From 80344df9b0c4a08aa612382ca86f97e5b412ed54 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 24 Apr 2022 13:28:05 +0200 Subject: [PATCH 01/14] :tada: Add Network Packets --- .../network/packets/NetworkHandler.java | 28 ++++++ .../network/packets/NetworkPacket.java | 43 +++++++++ .../packets/client/ClientNetworkHandler.java | 30 +++++++ .../network/packets/client/ClientPacket.java | 37 ++++++++ .../packets/client/ExecuteCommandPacket.java | 36 ++++++++ .../packets/client/ImALobbyPacket.java | 33 +++++++ .../client/InventoryCallbackPacket.java | 87 +++++++++++++++++++ .../packets/client/PrepareSchemPacket.java | 37 ++++++++ .../packets/client/TablistNamePacket.java | 36 ++++++++ .../network/packets/common/CommonPacket.java | 28 ++++++ .../packets/common/FightEndsPacket.java | 42 +++++++++ .../packets/common/FightInfoPacket.java | 50 +++++++++++ .../packets/server/BaumemberUpdatePacket.java | 33 +++++++ .../packets/server/CloseInventoryPacket.java | 35 ++++++++ .../packets/server/InventoryPacket.java | 40 +++++++++ .../network/packets/server/PingPacket.java | 32 +++++++ .../packets/server/ServerNetworkHandler.java | 30 +++++++ .../network/packets/server/ServerPacket.java | 37 ++++++++ .../packets/server/StartingServerPacket.java | 34 ++++++++ 19 files changed, 728 insertions(+) create mode 100644 src/de/steamwar/network/packets/NetworkHandler.java create mode 100644 src/de/steamwar/network/packets/NetworkPacket.java create mode 100644 src/de/steamwar/network/packets/client/ClientNetworkHandler.java create mode 100644 src/de/steamwar/network/packets/client/ClientPacket.java create mode 100644 src/de/steamwar/network/packets/client/ExecuteCommandPacket.java create mode 100644 src/de/steamwar/network/packets/client/ImALobbyPacket.java create mode 100644 src/de/steamwar/network/packets/client/InventoryCallbackPacket.java create mode 100644 src/de/steamwar/network/packets/client/PrepareSchemPacket.java create mode 100644 src/de/steamwar/network/packets/client/TablistNamePacket.java create mode 100644 src/de/steamwar/network/packets/common/CommonPacket.java create mode 100644 src/de/steamwar/network/packets/common/FightEndsPacket.java create mode 100644 src/de/steamwar/network/packets/common/FightInfoPacket.java create mode 100644 src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java create mode 100644 src/de/steamwar/network/packets/server/CloseInventoryPacket.java create mode 100644 src/de/steamwar/network/packets/server/InventoryPacket.java create mode 100644 src/de/steamwar/network/packets/server/PingPacket.java create mode 100644 src/de/steamwar/network/packets/server/ServerNetworkHandler.java create mode 100644 src/de/steamwar/network/packets/server/ServerPacket.java create mode 100644 src/de/steamwar/network/packets/server/StartingServerPacket.java diff --git a/src/de/steamwar/network/packets/NetworkHandler.java b/src/de/steamwar/network/packets/NetworkHandler.java new file mode 100644 index 0000000..cd068d0 --- /dev/null +++ b/src/de/steamwar/network/packets/NetworkHandler.java @@ -0,0 +1,28 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets; + +import de.steamwar.network.packets.common.FightInfoPacket; +import de.steamwar.network.packets.common.FightEndsPacket; + +public interface NetworkHandler { + void handle(FightEndsPacket packet); + void handle(FightInfoPacket packet); +} diff --git a/src/de/steamwar/network/packets/NetworkPacket.java b/src/de/steamwar/network/packets/NetworkPacket.java new file mode 100644 index 0000000..c146913 --- /dev/null +++ b/src/de/steamwar/network/packets/NetworkPacket.java @@ -0,0 +1,43 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets; + +import java.io.Serializable; + +public abstract class NetworkPacket implements Serializable { + + private final Sender sender; + + public NetworkPacket(Sender sender) { + this.sender = sender; + } + + public Sender getSender() { + return sender; + } + + public abstract void handle(NetworkHandler handler); + + public enum Sender { + SERVER, + CLIENT, + BOTH + } +} diff --git a/src/de/steamwar/network/packets/client/ClientNetworkHandler.java b/src/de/steamwar/network/packets/client/ClientNetworkHandler.java new file mode 100644 index 0000000..c8b7434 --- /dev/null +++ b/src/de/steamwar/network/packets/client/ClientNetworkHandler.java @@ -0,0 +1,30 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.client; + +import de.steamwar.network.packets.server.*; + +public interface ClientNetworkHandler { + void handle(BaumemberUpdatePacket packet); + void handle(CloseInventoryPacket packet); + void handle(InventoryPacket packet); + void handle(PingPacket packet); + void handle(StartingServerPacket packet); +} diff --git a/src/de/steamwar/network/packets/client/ClientPacket.java b/src/de/steamwar/network/packets/client/ClientPacket.java new file mode 100644 index 0000000..9829bf8 --- /dev/null +++ b/src/de/steamwar/network/packets/client/ClientPacket.java @@ -0,0 +1,37 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.client; + +import de.steamwar.network.packets.NetworkHandler; +import de.steamwar.network.packets.NetworkPacket; +import de.steamwar.network.packets.server.ServerNetworkHandler; + +public abstract class ClientPacket extends NetworkPacket { + public ClientPacket() { + super(Sender.CLIENT); + } + + @Override + public void handle(NetworkHandler handler) { + handle((ServerNetworkHandler) handler); + } + + public abstract void handle(ServerNetworkHandler handler); +} diff --git a/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java b/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java new file mode 100644 index 0000000..a2b6570 --- /dev/null +++ b/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java @@ -0,0 +1,36 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.client; + +import de.steamwar.network.packets.server.ServerNetworkHandler; +import lombok.Data; + +@Data +public class ExecuteCommandPacket extends ClientPacket { + + private static final long serialVersionUID = 6283457297487602016L; + private int playerId; + private String command; + + @Override + public void handle(ServerNetworkHandler handler) { + handler.handle(this); + } +} diff --git a/src/de/steamwar/network/packets/client/ImALobbyPacket.java b/src/de/steamwar/network/packets/client/ImALobbyPacket.java new file mode 100644 index 0000000..5656f63 --- /dev/null +++ b/src/de/steamwar/network/packets/client/ImALobbyPacket.java @@ -0,0 +1,33 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.client; + +import de.steamwar.network.packets.server.ServerNetworkHandler; +import lombok.Data; + +@Data +public class ImALobbyPacket extends ClientPacket { + private static final long serialVersionUID = 8110246509205246654L; + + @Override + public void handle(ServerNetworkHandler handler) { + handler.handle(this); + } +} diff --git a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java new file mode 100644 index 0000000..c461102 --- /dev/null +++ b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java @@ -0,0 +1,87 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.client; + +import de.steamwar.network.packets.server.ServerNetworkHandler; +import lombok.Data; + +@Data +public class InventoryCallbackPacket extends ClientPacket { + private static final long serialVersionUID = -261823209186008718L; + private int position; + private int owner; + private CallbackType type; + private ClickType clickType; + + @Override + public void handle(ServerNetworkHandler handler) { + handler.handle(this); + } + + public enum CallbackType { + CLICK, + CLOSE, + } + + public enum ClickType { + LEFT, + SHIFT_LEFT, + RIGHT, + SHIFT_RIGHT, + WINDOW_BORDER_LEFT, + WINDOW_BORDER_RIGHT, + MIDDLE, + NUMBER_KEY, + DOUBLE_CLICK, + DROP, + CONTROL_DROP, + CREATIVE, + UNKNOWN; + + 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; + } + + public static ClickType getByName(String name) { + for (ClickType type : values()) { + if (type.name().equalsIgnoreCase(name)) { + return type; + } + } + return UNKNOWN; + } + } +} diff --git a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java new file mode 100644 index 0000000..186b7b3 --- /dev/null +++ b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java @@ -0,0 +1,37 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.client; + +import de.steamwar.network.packets.server.ServerNetworkHandler; +import lombok.Data; + +@Data +public class PrepareSchemPacket extends ClientPacket { + + private static final long serialVersionUID = -4798561188105813349L; + private int player; + private int schem; + private int schemType; + + @Override + public void handle(ServerNetworkHandler handler) { + handler.handle(this); + } +} diff --git a/src/de/steamwar/network/packets/client/TablistNamePacket.java b/src/de/steamwar/network/packets/client/TablistNamePacket.java new file mode 100644 index 0000000..c8d2fed --- /dev/null +++ b/src/de/steamwar/network/packets/client/TablistNamePacket.java @@ -0,0 +1,36 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.client; + +import de.steamwar.network.packets.server.ServerNetworkHandler; +import lombok.Data; + +@Data +public class TablistNamePacket extends ClientPacket { + + private static final long serialVersionUID = -1709089784235478752L; + private int player; + private String tablistName; + + @Override + public void handle(ServerNetworkHandler handler) { + handler.handle(this); + } +} diff --git a/src/de/steamwar/network/packets/common/CommonPacket.java b/src/de/steamwar/network/packets/common/CommonPacket.java new file mode 100644 index 0000000..ad58090 --- /dev/null +++ b/src/de/steamwar/network/packets/common/CommonPacket.java @@ -0,0 +1,28 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.common; + +import de.steamwar.network.packets.NetworkPacket; + +public abstract class CommonPacket extends NetworkPacket { + public CommonPacket() { + super(Sender.BOTH); + } +} diff --git a/src/de/steamwar/network/packets/common/FightEndsPacket.java b/src/de/steamwar/network/packets/common/FightEndsPacket.java new file mode 100644 index 0000000..2f91618 --- /dev/null +++ b/src/de/steamwar/network/packets/common/FightEndsPacket.java @@ -0,0 +1,42 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.common; + +import de.steamwar.network.packets.NetworkHandler; +import lombok.Data; + +import java.util.List; + +@Data +public class FightEndsPacket extends CommonPacket { + + private static final long serialVersionUID = 1279352415549011332L; + private byte win; + private int blueSchem; + private int redSchem; + private List bluePlayers; + private List redPlayers; + private String gameMode; + + @Override + public void handle(NetworkHandler handler) { + handler.handle(this); + } +} diff --git a/src/de/steamwar/network/packets/common/FightInfoPacket.java b/src/de/steamwar/network/packets/common/FightInfoPacket.java new file mode 100644 index 0000000..d0f416b --- /dev/null +++ b/src/de/steamwar/network/packets/common/FightInfoPacket.java @@ -0,0 +1,50 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.common; + +import de.steamwar.network.packets.NetworkHandler; +import lombok.Data; + +import java.util.List; + +@Data +public class FightInfoPacket extends CommonPacket{ + + private static final long serialVersionUID = 7448644597856605853L; + private String serverName; // Name of the Server + private String gameMode; // GameMode aka Schematictype (if known, else "") + private String arena; // Name of the arena + private String blueName; // Name of the blue team, expected to begin with "§a" colorcode + private String redName; // Name of the red team, expected to begin with "§a" colorcode + private String fightState; // Fight state (technical term) (if known, else "") + private int countdown; // Countdown state in seconds (if known, else 0) + private int blueLeader; // SWUserID of the blue team leader (if known, else 0) + private int redLeader; // SWUserID of the red team leader (if known, else 0) + private int blueSchem; // Blue SchemID (if known, else 0) + private int redSchem; // Red SchemID (if known, else 0) + private List bluePlayers; // List of Blue SWUserIDs + private List redPlayers; // List of Red SWUserIDs + private List spectators; // List of Spectator SWUserIDs + + @Override + public void handle(NetworkHandler handler) { + handler.handle(this); + } +} diff --git a/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java b/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java new file mode 100644 index 0000000..ac610d5 --- /dev/null +++ b/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java @@ -0,0 +1,33 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.server; + +import de.steamwar.network.packets.client.ClientNetworkHandler; +import lombok.Data; + +@Data +public class BaumemberUpdatePacket extends ServerPacket{ + private static final long serialVersionUID = 6863118892424244051L; + + @Override + public void handle(ClientNetworkHandler handler) { + handler.handle(this); + } +} diff --git a/src/de/steamwar/network/packets/server/CloseInventoryPacket.java b/src/de/steamwar/network/packets/server/CloseInventoryPacket.java new file mode 100644 index 0000000..691df1a --- /dev/null +++ b/src/de/steamwar/network/packets/server/CloseInventoryPacket.java @@ -0,0 +1,35 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.server; + +import de.steamwar.network.packets.client.ClientNetworkHandler; +import lombok.Data; + +@Data +public class CloseInventoryPacket extends ServerPacket { + + private static final long serialVersionUID = -2191021190060504521L; + private int playerId; + + @Override + public void handle(ClientNetworkHandler handler) { + handler.handle(this); + } +} diff --git a/src/de/steamwar/network/packets/server/InventoryPacket.java b/src/de/steamwar/network/packets/server/InventoryPacket.java new file mode 100644 index 0000000..9ec6a1d --- /dev/null +++ b/src/de/steamwar/network/packets/server/InventoryPacket.java @@ -0,0 +1,40 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.server; + +import de.steamwar.network.packets.client.ClientNetworkHandler; +import lombok.Data; + +import java.util.Map; + +@Data +public class InventoryPacket extends ServerPacket { + + private static final long serialVersionUID = 8071052544654047316L; + private String title; + private int player; + private int size; + private Map items; + + @Override + public void handle(ClientNetworkHandler handler) { + handler.handle(this); + } +} diff --git a/src/de/steamwar/network/packets/server/PingPacket.java b/src/de/steamwar/network/packets/server/PingPacket.java new file mode 100644 index 0000000..d9a07ec --- /dev/null +++ b/src/de/steamwar/network/packets/server/PingPacket.java @@ -0,0 +1,32 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.server; + +import de.steamwar.network.packets.client.ClientNetworkHandler; + +public class PingPacket extends ServerPacket { + private static final long serialVersionUID = 714647343959550378L; + private int id; + + @Override + public void handle(ClientNetworkHandler handler) { + handler.handle(this); + } +} diff --git a/src/de/steamwar/network/packets/server/ServerNetworkHandler.java b/src/de/steamwar/network/packets/server/ServerNetworkHandler.java new file mode 100644 index 0000000..0a8cb7c --- /dev/null +++ b/src/de/steamwar/network/packets/server/ServerNetworkHandler.java @@ -0,0 +1,30 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.server; + +import de.steamwar.network.packets.client.*; + +public interface ServerNetworkHandler { + void handle(ExecuteCommandPacket packet); + void handle(ImALobbyPacket packet); + void handle(InventoryCallbackPacket packet); + void handle(PrepareSchemPacket packet); + void handle(TablistNamePacket packet); +} diff --git a/src/de/steamwar/network/packets/server/ServerPacket.java b/src/de/steamwar/network/packets/server/ServerPacket.java new file mode 100644 index 0000000..bfab1e7 --- /dev/null +++ b/src/de/steamwar/network/packets/server/ServerPacket.java @@ -0,0 +1,37 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.server; + +import de.steamwar.network.packets.NetworkHandler; +import de.steamwar.network.packets.NetworkPacket; +import de.steamwar.network.packets.client.ClientNetworkHandler; + +public abstract class ServerPacket extends NetworkPacket { + public ServerPacket() { + super(Sender.SERVER); + } + + @Override + public void handle(NetworkHandler handler) { + handle((ClientNetworkHandler) handler); + } + + public abstract void handle(ClientNetworkHandler handler); +} diff --git a/src/de/steamwar/network/packets/server/StartingServerPacket.java b/src/de/steamwar/network/packets/server/StartingServerPacket.java new file mode 100644 index 0000000..b819e37 --- /dev/null +++ b/src/de/steamwar/network/packets/server/StartingServerPacket.java @@ -0,0 +1,34 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets.server; + +import de.steamwar.network.packets.client.ClientNetworkHandler; +import lombok.Data; + +@Data +public class StartingServerPacket extends ServerPacket { + private static final long serialVersionUID = 2808607245898121801L; + private int user; + + @Override + public void handle(ClientNetworkHandler handler) { + handler.handle(this); + } +} -- 2.39.2 From 4c79af01820259984ea8587703db67f06a27c3ae Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 24 Apr 2022 20:55:46 +0200 Subject: [PATCH 02/14] :tada: Add Network Packets --- .../network/packets/NetworkHandler.java | 6 ++-- .../packets/client/ClientNetworkHandler.java | 13 +++++---- .../packets/common/FightInfoPacket.java | 28 +++++++++---------- .../packets/server/ServerNetworkHandler.java | 13 +++++---- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/de/steamwar/network/packets/NetworkHandler.java b/src/de/steamwar/network/packets/NetworkHandler.java index cd068d0..926d3f7 100644 --- a/src/de/steamwar/network/packets/NetworkHandler.java +++ b/src/de/steamwar/network/packets/NetworkHandler.java @@ -22,7 +22,7 @@ package de.steamwar.network.packets; import de.steamwar.network.packets.common.FightInfoPacket; import de.steamwar.network.packets.common.FightEndsPacket; -public interface NetworkHandler { - void handle(FightEndsPacket packet); - void handle(FightInfoPacket packet); +public abstract class NetworkHandler { + public void handle(FightEndsPacket packet) {}; + public void handle(FightInfoPacket packet) {}; } diff --git a/src/de/steamwar/network/packets/client/ClientNetworkHandler.java b/src/de/steamwar/network/packets/client/ClientNetworkHandler.java index c8b7434..6698fe1 100644 --- a/src/de/steamwar/network/packets/client/ClientNetworkHandler.java +++ b/src/de/steamwar/network/packets/client/ClientNetworkHandler.java @@ -19,12 +19,13 @@ package de.steamwar.network.packets.client; +import de.steamwar.network.packets.NetworkHandler; import de.steamwar.network.packets.server.*; -public interface ClientNetworkHandler { - void handle(BaumemberUpdatePacket packet); - void handle(CloseInventoryPacket packet); - void handle(InventoryPacket packet); - void handle(PingPacket packet); - void handle(StartingServerPacket packet); +public abstract class ClientNetworkHandler extends NetworkHandler { + public void handle(BaumemberUpdatePacket packet) {}; + public void handle(CloseInventoryPacket packet) {}; + public void handle(InventoryPacket packet) {} + public void handle(PingPacket packet) {} + public void handle(StartingServerPacket packet) {} } diff --git a/src/de/steamwar/network/packets/common/FightInfoPacket.java b/src/de/steamwar/network/packets/common/FightInfoPacket.java index d0f416b..1d9e1e5 100644 --- a/src/de/steamwar/network/packets/common/FightInfoPacket.java +++ b/src/de/steamwar/network/packets/common/FightInfoPacket.java @@ -28,20 +28,20 @@ import java.util.List; public class FightInfoPacket extends CommonPacket{ private static final long serialVersionUID = 7448644597856605853L; - private String serverName; // Name of the Server - private String gameMode; // GameMode aka Schematictype (if known, else "") - private String arena; // Name of the arena - private String blueName; // Name of the blue team, expected to begin with "§a" colorcode - private String redName; // Name of the red team, expected to begin with "§a" colorcode - private String fightState; // Fight state (technical term) (if known, else "") - private int countdown; // Countdown state in seconds (if known, else 0) - private int blueLeader; // SWUserID of the blue team leader (if known, else 0) - private int redLeader; // SWUserID of the red team leader (if known, else 0) - private int blueSchem; // Blue SchemID (if known, else 0) - private int redSchem; // Red SchemID (if known, else 0) - private List bluePlayers; // List of Blue SWUserIDs - private List redPlayers; // List of Red SWUserIDs - private List spectators; // List of Spectator SWUserIDs + private String serverName; + private String gameMode; + private String arena; + private String blueName; + private String redName; + private String fightState; + private int countdown; + private int blueLeader; + private int redLeader; + private int blueSchem; + private int redSchem; + private List bluePlayers; + private List redPlayers; + private List spectators; @Override public void handle(NetworkHandler handler) { diff --git a/src/de/steamwar/network/packets/server/ServerNetworkHandler.java b/src/de/steamwar/network/packets/server/ServerNetworkHandler.java index 0a8cb7c..c418380 100644 --- a/src/de/steamwar/network/packets/server/ServerNetworkHandler.java +++ b/src/de/steamwar/network/packets/server/ServerNetworkHandler.java @@ -19,12 +19,13 @@ package de.steamwar.network.packets.server; +import de.steamwar.network.packets.NetworkHandler; import de.steamwar.network.packets.client.*; -public interface ServerNetworkHandler { - void handle(ExecuteCommandPacket packet); - void handle(ImALobbyPacket packet); - void handle(InventoryCallbackPacket packet); - void handle(PrepareSchemPacket packet); - void handle(TablistNamePacket packet); +public abstract class ServerNetworkHandler extends NetworkHandler { + public void handle(ExecuteCommandPacket packet) {}; + public void handle(ImALobbyPacket packet) {} + public void handle(InventoryCallbackPacket packet) {} + public void handle(PrepareSchemPacket packet) {} + public void handle(TablistNamePacket packet) {} } -- 2.39.2 From 186592f0ecc877f2718923339983bf319e3652ef Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 24 Apr 2022 21:07:42 +0200 Subject: [PATCH 03/14] :tada: Add Network Packets --- .../packets/client/TablistNamePacket.java | 36 ------------------- .../network/packets/server/PingPacket.java | 2 ++ .../packets/server/ServerNetworkHandler.java | 1 - 3 files changed, 2 insertions(+), 37 deletions(-) delete mode 100644 src/de/steamwar/network/packets/client/TablistNamePacket.java diff --git a/src/de/steamwar/network/packets/client/TablistNamePacket.java b/src/de/steamwar/network/packets/client/TablistNamePacket.java deleted file mode 100644 index c8d2fed..0000000 --- a/src/de/steamwar/network/packets/client/TablistNamePacket.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2022 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.network.packets.client; - -import de.steamwar.network.packets.server.ServerNetworkHandler; -import lombok.Data; - -@Data -public class TablistNamePacket extends ClientPacket { - - private static final long serialVersionUID = -1709089784235478752L; - private int player; - private String tablistName; - - @Override - public void handle(ServerNetworkHandler handler) { - handler.handle(this); - } -} diff --git a/src/de/steamwar/network/packets/server/PingPacket.java b/src/de/steamwar/network/packets/server/PingPacket.java index d9a07ec..f40dbb1 100644 --- a/src/de/steamwar/network/packets/server/PingPacket.java +++ b/src/de/steamwar/network/packets/server/PingPacket.java @@ -20,7 +20,9 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.client.ClientNetworkHandler; +import lombok.Data; +@Data public class PingPacket extends ServerPacket { private static final long serialVersionUID = 714647343959550378L; private int id; diff --git a/src/de/steamwar/network/packets/server/ServerNetworkHandler.java b/src/de/steamwar/network/packets/server/ServerNetworkHandler.java index c418380..28c55f5 100644 --- a/src/de/steamwar/network/packets/server/ServerNetworkHandler.java +++ b/src/de/steamwar/network/packets/server/ServerNetworkHandler.java @@ -27,5 +27,4 @@ public abstract class ServerNetworkHandler extends NetworkHandler { public void handle(ImALobbyPacket packet) {} public void handle(InventoryCallbackPacket packet) {} public void handle(PrepareSchemPacket packet) {} - public void handle(TablistNamePacket packet) {} } -- 2.39.2 From 8d677e200d652abc8d8c8d9ac90fbb63d7d931e9 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 24 Apr 2022 21:20:45 +0200 Subject: [PATCH 04/14] :tada: Add Network Packets --- src/de/steamwar/network/packets/client/PrepareSchemPacket.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java index 186b7b3..8d195c5 100644 --- a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java +++ b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java @@ -28,7 +28,7 @@ public class PrepareSchemPacket extends ClientPacket { private static final long serialVersionUID = -4798561188105813349L; private int player; private int schem; - private int schemType; + private String schemType; @Override public void handle(ServerNetworkHandler handler) { -- 2.39.2 From ed24abe67f0f4f48356c90d8444fd53279703ef2 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 24 Apr 2022 21:44:51 +0200 Subject: [PATCH 05/14] :tada: Add Network Packets --- .../steamwar/network/packets/client/ExecuteCommandPacket.java | 2 ++ src/de/steamwar/network/packets/client/ImALobbyPacket.java | 2 ++ .../network/packets/client/InventoryCallbackPacket.java | 2 ++ src/de/steamwar/network/packets/client/PrepareSchemPacket.java | 2 ++ src/de/steamwar/network/packets/common/FightEndsPacket.java | 2 ++ src/de/steamwar/network/packets/common/FightInfoPacket.java | 2 ++ .../steamwar/network/packets/server/BaumemberUpdatePacket.java | 2 ++ .../steamwar/network/packets/server/CloseInventoryPacket.java | 2 ++ src/de/steamwar/network/packets/server/InventoryPacket.java | 2 ++ src/de/steamwar/network/packets/server/PingPacket.java | 2 ++ .../steamwar/network/packets/server/StartingServerPacket.java | 2 ++ 11 files changed, 22 insertions(+) diff --git a/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java b/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java index a2b6570..aec2fcc 100644 --- a/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java +++ b/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java @@ -20,9 +20,11 @@ package de.steamwar.network.packets.client; import de.steamwar.network.packets.server.ServerNetworkHandler; +import lombok.AllArgsConstructor; import lombok.Data; @Data +@AllArgsConstructor public class ExecuteCommandPacket extends ClientPacket { private static final long serialVersionUID = 6283457297487602016L; diff --git a/src/de/steamwar/network/packets/client/ImALobbyPacket.java b/src/de/steamwar/network/packets/client/ImALobbyPacket.java index 5656f63..8968c37 100644 --- a/src/de/steamwar/network/packets/client/ImALobbyPacket.java +++ b/src/de/steamwar/network/packets/client/ImALobbyPacket.java @@ -20,9 +20,11 @@ package de.steamwar.network.packets.client; import de.steamwar.network.packets.server.ServerNetworkHandler; +import lombok.AllArgsConstructor; import lombok.Data; @Data +@AllArgsConstructor public class ImALobbyPacket extends ClientPacket { private static final long serialVersionUID = 8110246509205246654L; diff --git a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java index c461102..df605a4 100644 --- a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java +++ b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java @@ -20,9 +20,11 @@ package de.steamwar.network.packets.client; import de.steamwar.network.packets.server.ServerNetworkHandler; +import lombok.AllArgsConstructor; import lombok.Data; @Data +@AllArgsConstructor public class InventoryCallbackPacket extends ClientPacket { private static final long serialVersionUID = -261823209186008718L; private int position; diff --git a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java index 8d195c5..6b5f6e3 100644 --- a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java +++ b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java @@ -20,9 +20,11 @@ package de.steamwar.network.packets.client; import de.steamwar.network.packets.server.ServerNetworkHandler; +import lombok.AllArgsConstructor; import lombok.Data; @Data +@AllArgsConstructor public class PrepareSchemPacket extends ClientPacket { private static final long serialVersionUID = -4798561188105813349L; diff --git a/src/de/steamwar/network/packets/common/FightEndsPacket.java b/src/de/steamwar/network/packets/common/FightEndsPacket.java index 2f91618..dfea7ba 100644 --- a/src/de/steamwar/network/packets/common/FightEndsPacket.java +++ b/src/de/steamwar/network/packets/common/FightEndsPacket.java @@ -20,11 +20,13 @@ package de.steamwar.network.packets.common; import de.steamwar.network.packets.NetworkHandler; +import lombok.AllArgsConstructor; import lombok.Data; import java.util.List; @Data +@AllArgsConstructor public class FightEndsPacket extends CommonPacket { private static final long serialVersionUID = 1279352415549011332L; diff --git a/src/de/steamwar/network/packets/common/FightInfoPacket.java b/src/de/steamwar/network/packets/common/FightInfoPacket.java index 1d9e1e5..e273e2b 100644 --- a/src/de/steamwar/network/packets/common/FightInfoPacket.java +++ b/src/de/steamwar/network/packets/common/FightInfoPacket.java @@ -20,11 +20,13 @@ package de.steamwar.network.packets.common; import de.steamwar.network.packets.NetworkHandler; +import lombok.AllArgsConstructor; import lombok.Data; import java.util.List; @Data +@AllArgsConstructor public class FightInfoPacket extends CommonPacket{ private static final long serialVersionUID = 7448644597856605853L; diff --git a/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java b/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java index ac610d5..386a0fc 100644 --- a/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java +++ b/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java @@ -20,9 +20,11 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.client.ClientNetworkHandler; +import lombok.AllArgsConstructor; import lombok.Data; @Data +@AllArgsConstructor public class BaumemberUpdatePacket extends ServerPacket{ private static final long serialVersionUID = 6863118892424244051L; diff --git a/src/de/steamwar/network/packets/server/CloseInventoryPacket.java b/src/de/steamwar/network/packets/server/CloseInventoryPacket.java index 691df1a..660e3ba 100644 --- a/src/de/steamwar/network/packets/server/CloseInventoryPacket.java +++ b/src/de/steamwar/network/packets/server/CloseInventoryPacket.java @@ -20,9 +20,11 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.client.ClientNetworkHandler; +import lombok.AllArgsConstructor; import lombok.Data; @Data +@AllArgsConstructor public class CloseInventoryPacket extends ServerPacket { private static final long serialVersionUID = -2191021190060504521L; diff --git a/src/de/steamwar/network/packets/server/InventoryPacket.java b/src/de/steamwar/network/packets/server/InventoryPacket.java index 9ec6a1d..da59488 100644 --- a/src/de/steamwar/network/packets/server/InventoryPacket.java +++ b/src/de/steamwar/network/packets/server/InventoryPacket.java @@ -20,11 +20,13 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.client.ClientNetworkHandler; +import lombok.AllArgsConstructor; import lombok.Data; import java.util.Map; @Data +@AllArgsConstructor public class InventoryPacket extends ServerPacket { private static final long serialVersionUID = 8071052544654047316L; diff --git a/src/de/steamwar/network/packets/server/PingPacket.java b/src/de/steamwar/network/packets/server/PingPacket.java index f40dbb1..30342c3 100644 --- a/src/de/steamwar/network/packets/server/PingPacket.java +++ b/src/de/steamwar/network/packets/server/PingPacket.java @@ -20,9 +20,11 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.client.ClientNetworkHandler; +import lombok.AllArgsConstructor; import lombok.Data; @Data +@AllArgsConstructor public class PingPacket extends ServerPacket { private static final long serialVersionUID = 714647343959550378L; private int id; diff --git a/src/de/steamwar/network/packets/server/StartingServerPacket.java b/src/de/steamwar/network/packets/server/StartingServerPacket.java index b819e37..30d32fb 100644 --- a/src/de/steamwar/network/packets/server/StartingServerPacket.java +++ b/src/de/steamwar/network/packets/server/StartingServerPacket.java @@ -20,9 +20,11 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.client.ClientNetworkHandler; +import lombok.AllArgsConstructor; import lombok.Data; @Data +@AllArgsConstructor public class StartingServerPacket extends ServerPacket { private static final long serialVersionUID = 2808607245898121801L; private int user; -- 2.39.2 From f2cf9b40a43c502f7e67325802222c2444626966 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 24 Apr 2022 22:32:19 +0200 Subject: [PATCH 06/14] :art: Refactor --- .../packets/client/ExecuteCommandPacket.java | 5 +++-- .../network/packets/client/ImALobbyPacket.java | 2 -- .../packets/client/InventoryCallbackPacket.java | 4 ++++ .../packets/client/PrepareSchemPacket.java | 6 ++++-- .../network/packets/common/FightEndsPacket.java | 9 +++++++-- .../network/packets/common/FightInfoPacket.java | 17 +++++++++++++++-- .../packets/server/BaumemberUpdatePacket.java | 2 -- .../packets/server/CloseInventoryPacket.java | 4 ++-- .../network/packets/server/InventoryPacket.java | 7 +++++-- .../network/packets/server/PingPacket.java | 4 ++-- .../packets/server/StartingServerPacket.java | 4 ++-- 11 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java b/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java index aec2fcc..62c3acb 100644 --- a/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java +++ b/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java @@ -20,15 +20,16 @@ package de.steamwar.network.packets.client; import de.steamwar.network.packets.server.ServerNetworkHandler; -import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NonNull; @Data -@AllArgsConstructor public class ExecuteCommandPacket extends ClientPacket { private static final long serialVersionUID = 6283457297487602016L; + @NonNull private int playerId; + @NonNull private String command; @Override diff --git a/src/de/steamwar/network/packets/client/ImALobbyPacket.java b/src/de/steamwar/network/packets/client/ImALobbyPacket.java index 8968c37..5656f63 100644 --- a/src/de/steamwar/network/packets/client/ImALobbyPacket.java +++ b/src/de/steamwar/network/packets/client/ImALobbyPacket.java @@ -20,11 +20,9 @@ package de.steamwar.network.packets.client; import de.steamwar.network.packets.server.ServerNetworkHandler; -import lombok.AllArgsConstructor; import lombok.Data; @Data -@AllArgsConstructor public class ImALobbyPacket extends ClientPacket { private static final long serialVersionUID = 8110246509205246654L; diff --git a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java index df605a4..c28c163 100644 --- a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java +++ b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java @@ -22,13 +22,17 @@ package de.steamwar.network.packets.client; import de.steamwar.network.packets.server.ServerNetworkHandler; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NonNull; @Data @AllArgsConstructor public class InventoryCallbackPacket extends ClientPacket { private static final long serialVersionUID = -261823209186008718L; + @NonNull private int position; + @NonNull private int owner; + @NonNull private CallbackType type; private ClickType clickType; diff --git a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java index 6b5f6e3..55d2b39 100644 --- a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java +++ b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java @@ -20,16 +20,18 @@ package de.steamwar.network.packets.client; import de.steamwar.network.packets.server.ServerNetworkHandler; -import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NonNull; @Data -@AllArgsConstructor public class PrepareSchemPacket extends ClientPacket { private static final long serialVersionUID = -4798561188105813349L; + @NonNull private int player; + @NonNull private int schem; + @NonNull private String schemType; @Override diff --git a/src/de/steamwar/network/packets/common/FightEndsPacket.java b/src/de/steamwar/network/packets/common/FightEndsPacket.java index dfea7ba..f046538 100644 --- a/src/de/steamwar/network/packets/common/FightEndsPacket.java +++ b/src/de/steamwar/network/packets/common/FightEndsPacket.java @@ -20,21 +20,26 @@ package de.steamwar.network.packets.common; import de.steamwar.network.packets.NetworkHandler; -import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NonNull; import java.util.List; @Data -@AllArgsConstructor public class FightEndsPacket extends CommonPacket { private static final long serialVersionUID = 1279352415549011332L; + @NonNull private byte win; + @NonNull private int blueSchem; + @NonNull private int redSchem; + @NonNull private List bluePlayers; + @NonNull private List redPlayers; + @NonNull private String gameMode; @Override diff --git a/src/de/steamwar/network/packets/common/FightInfoPacket.java b/src/de/steamwar/network/packets/common/FightInfoPacket.java index e273e2b..5295d25 100644 --- a/src/de/steamwar/network/packets/common/FightInfoPacket.java +++ b/src/de/steamwar/network/packets/common/FightInfoPacket.java @@ -20,29 +20,42 @@ package de.steamwar.network.packets.common; import de.steamwar.network.packets.NetworkHandler; -import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NonNull; import java.util.List; @Data -@AllArgsConstructor public class FightInfoPacket extends CommonPacket{ private static final long serialVersionUID = 7448644597856605853L; + @NonNull private String serverName; + @NonNull private String gameMode; + @NonNull private String arena; + @NonNull private String blueName; + @NonNull private String redName; + @NonNull private String fightState; + @NonNull private int countdown; + @NonNull private int blueLeader; + @NonNull private int redLeader; + @NonNull private int blueSchem; + @NonNull private int redSchem; + @NonNull private List bluePlayers; + @NonNull private List redPlayers; + @NonNull private List spectators; @Override diff --git a/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java b/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java index 386a0fc..ac610d5 100644 --- a/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java +++ b/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java @@ -20,11 +20,9 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.AllArgsConstructor; import lombok.Data; @Data -@AllArgsConstructor public class BaumemberUpdatePacket extends ServerPacket{ private static final long serialVersionUID = 6863118892424244051L; diff --git a/src/de/steamwar/network/packets/server/CloseInventoryPacket.java b/src/de/steamwar/network/packets/server/CloseInventoryPacket.java index 660e3ba..7ea7e6a 100644 --- a/src/de/steamwar/network/packets/server/CloseInventoryPacket.java +++ b/src/de/steamwar/network/packets/server/CloseInventoryPacket.java @@ -20,14 +20,14 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NonNull; @Data -@AllArgsConstructor public class CloseInventoryPacket extends ServerPacket { private static final long serialVersionUID = -2191021190060504521L; + @NonNull private int playerId; @Override diff --git a/src/de/steamwar/network/packets/server/InventoryPacket.java b/src/de/steamwar/network/packets/server/InventoryPacket.java index da59488..1a507d1 100644 --- a/src/de/steamwar/network/packets/server/InventoryPacket.java +++ b/src/de/steamwar/network/packets/server/InventoryPacket.java @@ -20,19 +20,22 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NonNull; import java.util.Map; @Data -@AllArgsConstructor public class InventoryPacket extends ServerPacket { private static final long serialVersionUID = 8071052544654047316L; + @NonNull private String title; + @NonNull private int player; + @NonNull private int size; + @NonNull private Map items; @Override diff --git a/src/de/steamwar/network/packets/server/PingPacket.java b/src/de/steamwar/network/packets/server/PingPacket.java index 30342c3..f2ca61e 100644 --- a/src/de/steamwar/network/packets/server/PingPacket.java +++ b/src/de/steamwar/network/packets/server/PingPacket.java @@ -20,13 +20,13 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NonNull; @Data -@AllArgsConstructor public class PingPacket extends ServerPacket { private static final long serialVersionUID = 714647343959550378L; + @NonNull private int id; @Override diff --git a/src/de/steamwar/network/packets/server/StartingServerPacket.java b/src/de/steamwar/network/packets/server/StartingServerPacket.java index 30d32fb..bc0cb66 100644 --- a/src/de/steamwar/network/packets/server/StartingServerPacket.java +++ b/src/de/steamwar/network/packets/server/StartingServerPacket.java @@ -20,13 +20,13 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NonNull; @Data -@AllArgsConstructor public class StartingServerPacket extends ServerPacket { private static final long serialVersionUID = 2808607245898121801L; + @NonNull private int user; @Override -- 2.39.2 From 0883af8b795ae2d36c622ae0a62e2f9a262bc218 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 24 Apr 2022 22:39:41 +0200 Subject: [PATCH 07/14] :art: Refactor --- src/de/steamwar/network/packets/NetworkHandler.java | 4 ++-- .../network/packets/client/ClientNetworkHandler.java | 4 ++-- .../network/packets/client/InventoryCallbackPacket.java | 5 +++-- .../network/packets/server/ServerNetworkHandler.java | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/de/steamwar/network/packets/NetworkHandler.java b/src/de/steamwar/network/packets/NetworkHandler.java index 926d3f7..ca19fe0 100644 --- a/src/de/steamwar/network/packets/NetworkHandler.java +++ b/src/de/steamwar/network/packets/NetworkHandler.java @@ -23,6 +23,6 @@ import de.steamwar.network.packets.common.FightInfoPacket; import de.steamwar.network.packets.common.FightEndsPacket; public abstract class NetworkHandler { - public void handle(FightEndsPacket packet) {}; - public void handle(FightInfoPacket packet) {}; + public void handle(FightEndsPacket packet) {} + public void handle(FightInfoPacket packet) {} } diff --git a/src/de/steamwar/network/packets/client/ClientNetworkHandler.java b/src/de/steamwar/network/packets/client/ClientNetworkHandler.java index 6698fe1..becd500 100644 --- a/src/de/steamwar/network/packets/client/ClientNetworkHandler.java +++ b/src/de/steamwar/network/packets/client/ClientNetworkHandler.java @@ -23,8 +23,8 @@ import de.steamwar.network.packets.NetworkHandler; import de.steamwar.network.packets.server.*; public abstract class ClientNetworkHandler extends NetworkHandler { - public void handle(BaumemberUpdatePacket packet) {}; - public void handle(CloseInventoryPacket packet) {}; + public void handle(BaumemberUpdatePacket packet) {} + public void handle(CloseInventoryPacket packet) {} public void handle(InventoryPacket packet) {} public void handle(PingPacket packet) {} public void handle(StartingServerPacket packet) {} diff --git a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java index c28c163..76d90dc 100644 --- a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java +++ b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java @@ -29,12 +29,13 @@ import lombok.NonNull; public class InventoryCallbackPacket extends ClientPacket { private static final long serialVersionUID = -261823209186008718L; @NonNull - private int position; + private int position = -1; @NonNull private int owner; @NonNull private CallbackType type; - private ClickType clickType; + @NonNull + private ClickType clickType = ClickType.UNKNOWN; @Override public void handle(ServerNetworkHandler handler) { diff --git a/src/de/steamwar/network/packets/server/ServerNetworkHandler.java b/src/de/steamwar/network/packets/server/ServerNetworkHandler.java index 28c55f5..7947b84 100644 --- a/src/de/steamwar/network/packets/server/ServerNetworkHandler.java +++ b/src/de/steamwar/network/packets/server/ServerNetworkHandler.java @@ -23,7 +23,7 @@ import de.steamwar.network.packets.NetworkHandler; import de.steamwar.network.packets.client.*; public abstract class ServerNetworkHandler extends NetworkHandler { - public void handle(ExecuteCommandPacket packet) {}; + public void handle(ExecuteCommandPacket packet) {} public void handle(ImALobbyPacket packet) {} public void handle(InventoryCallbackPacket packet) {} public void handle(PrepareSchemPacket packet) {} -- 2.39.2 From dcd06af5597027ea840f9d97e1c9d09e14686b1d Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 25 Apr 2022 14:16:03 +0200 Subject: [PATCH 08/14] :art: Refactor --- .../network/packets/client/InventoryCallbackPacket.java | 4 ++++ src/de/steamwar/network/packets/common/FightEndsPacket.java | 2 ++ src/de/steamwar/network/packets/common/FightInfoPacket.java | 2 ++ .../steamwar/network/packets/server/StartingServerPacket.java | 2 +- 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java index 76d90dc..1c11c66 100644 --- a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java +++ b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java @@ -21,20 +21,24 @@ package de.steamwar.network.packets.client; import de.steamwar.network.packets.server.ServerNetworkHandler; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NonNull; +@Builder @Data @AllArgsConstructor public class InventoryCallbackPacket extends ClientPacket { private static final long serialVersionUID = -261823209186008718L; @NonNull + @Builder.Default private int position = -1; @NonNull private int owner; @NonNull private CallbackType type; @NonNull + @Builder.Default private ClickType clickType = ClickType.UNKNOWN; @Override diff --git a/src/de/steamwar/network/packets/common/FightEndsPacket.java b/src/de/steamwar/network/packets/common/FightEndsPacket.java index f046538..b89476e 100644 --- a/src/de/steamwar/network/packets/common/FightEndsPacket.java +++ b/src/de/steamwar/network/packets/common/FightEndsPacket.java @@ -20,11 +20,13 @@ package de.steamwar.network.packets.common; import de.steamwar.network.packets.NetworkHandler; +import lombok.Builder; import lombok.Data; import lombok.NonNull; import java.util.List; +@Builder @Data public class FightEndsPacket extends CommonPacket { diff --git a/src/de/steamwar/network/packets/common/FightInfoPacket.java b/src/de/steamwar/network/packets/common/FightInfoPacket.java index 5295d25..ff2a8f7 100644 --- a/src/de/steamwar/network/packets/common/FightInfoPacket.java +++ b/src/de/steamwar/network/packets/common/FightInfoPacket.java @@ -20,11 +20,13 @@ package de.steamwar.network.packets.common; import de.steamwar.network.packets.NetworkHandler; +import lombok.Builder; import lombok.Data; import lombok.NonNull; import java.util.List; +@Builder @Data public class FightInfoPacket extends CommonPacket{ diff --git a/src/de/steamwar/network/packets/server/StartingServerPacket.java b/src/de/steamwar/network/packets/server/StartingServerPacket.java index bc0cb66..3dadceb 100644 --- a/src/de/steamwar/network/packets/server/StartingServerPacket.java +++ b/src/de/steamwar/network/packets/server/StartingServerPacket.java @@ -26,7 +26,7 @@ import lombok.NonNull; @Data public class StartingServerPacket extends ServerPacket { private static final long serialVersionUID = 2808607245898121801L; - @NonNull + private int user; @Override -- 2.39.2 From ddc7353ad1e6a68674ee61b8601f5630484a152a Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 1 May 2022 21:50:26 +0200 Subject: [PATCH 09/14] :art: Refactor --- .../network/packets/NetworkHandler.java | 28 ------- .../network/packets/NetworkPacket.java | 37 ++++++--- .../network/packets/PacketHandler.java | 77 +++++++++++++++++ .../packets/client/ClientNetworkHandler.java | 31 ------- .../network/packets/client/ClientPacket.java | 37 --------- .../packets/client/ExecuteCommandPacket.java | 20 ++--- .../packets/client/ImALobbyPacket.java | 13 +-- .../client/InventoryCallbackPacket.java | 21 ++--- .../packets/client/PrepareSchemPacket.java | 21 ++--- .../network/packets/common/CommonPacket.java | 28 ------- .../packets/common/FightEndsPacket.java | 23 ++--- .../packets/common/FightInfoPacket.java | 31 ++----- .../packets/server/BaumemberUpdatePacket.java | 10 +-- .../packets/server/CloseInventoryPacket.java | 19 ++--- .../packets/server/InventoryPacket.java | 22 ++--- .../network/packets/server/PingPacket.java | 19 ++--- .../packets/server/ServerNetworkHandler.java | 30 ------- .../packets/server/StartingServerPacket.java | 19 ++--- testsrc/de/steamwar/RandomGenerator.java | 83 +++++++++++++++++++ testsrc/de/steamwar/network/AllPackets.java | 80 ++++++++++++++++++ .../de/steamwar/network/PacketTest.java | 27 +++--- 21 files changed, 351 insertions(+), 325 deletions(-) delete mode 100644 src/de/steamwar/network/packets/NetworkHandler.java create mode 100644 src/de/steamwar/network/packets/PacketHandler.java delete mode 100644 src/de/steamwar/network/packets/client/ClientNetworkHandler.java delete mode 100644 src/de/steamwar/network/packets/client/ClientPacket.java delete mode 100644 src/de/steamwar/network/packets/common/CommonPacket.java delete mode 100644 src/de/steamwar/network/packets/server/ServerNetworkHandler.java create mode 100644 testsrc/de/steamwar/RandomGenerator.java create mode 100644 testsrc/de/steamwar/network/AllPackets.java rename src/de/steamwar/network/packets/server/ServerPacket.java => testsrc/de/steamwar/network/PacketTest.java (57%) diff --git a/src/de/steamwar/network/packets/NetworkHandler.java b/src/de/steamwar/network/packets/NetworkHandler.java deleted file mode 100644 index ca19fe0..0000000 --- a/src/de/steamwar/network/packets/NetworkHandler.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2022 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.network.packets; - -import de.steamwar.network.packets.common.FightInfoPacket; -import de.steamwar.network.packets.common.FightEndsPacket; - -public abstract class NetworkHandler { - public void handle(FightEndsPacket packet) {} - public void handle(FightInfoPacket packet) {} -} diff --git a/src/de/steamwar/network/packets/NetworkPacket.java b/src/de/steamwar/network/packets/NetworkPacket.java index c146913..32857c6 100644 --- a/src/de/steamwar/network/packets/NetworkPacket.java +++ b/src/de/steamwar/network/packets/NetworkPacket.java @@ -19,25 +19,36 @@ package de.steamwar.network.packets; -import java.io.Serializable; +import lombok.EqualsAndHashCode; +import java.io.*; + +@EqualsAndHashCode public abstract class NetworkPacket implements Serializable { - private final Sender sender; - - public NetworkPacket(Sender sender) { - this.sender = sender; + public byte[] serialize() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(this); + oos.flush(); + } catch (Exception e) { + throw new RuntimeException(e); + } + return baos.toByteArray(); } - public Sender getSender() { - return sender; + public static void handle(byte[] data) { + PacketHandler.handlePacket(deserialize(data)); } - public abstract void handle(NetworkHandler handler); - - public enum Sender { - SERVER, - CLIENT, - BOTH + public static NetworkPacket deserialize(byte[] data) { + ByteArrayInputStream bais = new ByteArrayInputStream(data); + try { + ObjectInputStream ois = new ObjectInputStream(bais); + return (NetworkPacket) ois.readObject(); + } catch (Exception e) { + throw new RuntimeException(e); + } } } diff --git a/src/de/steamwar/network/packets/PacketHandler.java b/src/de/steamwar/network/packets/PacketHandler.java new file mode 100644 index 0000000..2f01ab0 --- /dev/null +++ b/src/de/steamwar/network/packets/PacketHandler.java @@ -0,0 +1,77 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network.packets; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public abstract class PacketHandler { + + private static final List PACKET_HANDLERS = new ArrayList<>(); + + private final Map, Method> HANDLER_MAP = new HashMap<>(); + + public static void handlePacket(NetworkPacket packet) { + for (PacketHandler handler : PACKET_HANDLERS) { + handler.handle(packet); + } + } + + public PacketHandler() { + Method[] methods = getClass().getMethods(); + for (Method method : methods) { + if(method.getParameterCount() != 1 || !NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) { + continue; + } + if (method.isAnnotationPresent(Handler.class)) { + Class packetClass = (Class) method.getParameterTypes()[0]; + HANDLER_MAP.put(packetClass, method); + } + } + } + + public void register() { + PACKET_HANDLERS.add(this); + } + + public void unregister() { + PACKET_HANDLERS.remove(this); + } + + public void handle(NetworkPacket packet) { + Method method = HANDLER_MAP.get(packet.getClass()); + if (method == null) { + return; + } + try { + method.invoke(this, packet); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Retention(RetentionPolicy.RUNTIME) + protected @interface Handler {} +} diff --git a/src/de/steamwar/network/packets/client/ClientNetworkHandler.java b/src/de/steamwar/network/packets/client/ClientNetworkHandler.java deleted file mode 100644 index becd500..0000000 --- a/src/de/steamwar/network/packets/client/ClientNetworkHandler.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2022 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.network.packets.client; - -import de.steamwar.network.packets.NetworkHandler; -import de.steamwar.network.packets.server.*; - -public abstract class ClientNetworkHandler extends NetworkHandler { - public void handle(BaumemberUpdatePacket packet) {} - public void handle(CloseInventoryPacket packet) {} - public void handle(InventoryPacket packet) {} - public void handle(PingPacket packet) {} - public void handle(StartingServerPacket packet) {} -} diff --git a/src/de/steamwar/network/packets/client/ClientPacket.java b/src/de/steamwar/network/packets/client/ClientPacket.java deleted file mode 100644 index 9829bf8..0000000 --- a/src/de/steamwar/network/packets/client/ClientPacket.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2022 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.network.packets.client; - -import de.steamwar.network.packets.NetworkHandler; -import de.steamwar.network.packets.NetworkPacket; -import de.steamwar.network.packets.server.ServerNetworkHandler; - -public abstract class ClientPacket extends NetworkPacket { - public ClientPacket() { - super(Sender.CLIENT); - } - - @Override - public void handle(NetworkHandler handler) { - handle((ServerNetworkHandler) handler); - } - - public abstract void handle(ServerNetworkHandler handler); -} diff --git a/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java b/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java index 62c3acb..0c56fc9 100644 --- a/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java +++ b/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java @@ -19,21 +19,17 @@ package de.steamwar.network.packets.client; -import de.steamwar.network.packets.server.ServerNetworkHandler; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; -@Data -public class ExecuteCommandPacket extends ClientPacket { +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class ExecuteCommandPacket extends NetworkPacket { private static final long serialVersionUID = 6283457297487602016L; - @NonNull private int playerId; - @NonNull private String command; - - @Override - public void handle(ServerNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/client/ImALobbyPacket.java b/src/de/steamwar/network/packets/client/ImALobbyPacket.java index 5656f63..dcd07dd 100644 --- a/src/de/steamwar/network/packets/client/ImALobbyPacket.java +++ b/src/de/steamwar/network/packets/client/ImALobbyPacket.java @@ -19,15 +19,10 @@ package de.steamwar.network.packets.client; -import de.steamwar.network.packets.server.ServerNetworkHandler; -import lombok.Data; +import de.steamwar.network.packets.NetworkPacket; +import lombok.EqualsAndHashCode; -@Data -public class ImALobbyPacket extends ClientPacket { +@EqualsAndHashCode(callSuper = true) +public class ImALobbyPacket extends NetworkPacket { private static final long serialVersionUID = 8110246509205246654L; - - @Override - public void handle(ServerNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java index 1c11c66..2e1188b 100644 --- a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java +++ b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java @@ -19,33 +19,22 @@ package de.steamwar.network.packets.client; -import de.steamwar.network.packets.server.ServerNetworkHandler; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.*; @Builder -@Data +@NoArgsConstructor @AllArgsConstructor -public class InventoryCallbackPacket extends ClientPacket { +@EqualsAndHashCode(callSuper = true) +public class InventoryCallbackPacket extends NetworkPacket { private static final long serialVersionUID = -261823209186008718L; - @NonNull @Builder.Default private int position = -1; - @NonNull private int owner; - @NonNull private CallbackType type; - @NonNull @Builder.Default private ClickType clickType = ClickType.UNKNOWN; - @Override - public void handle(ServerNetworkHandler handler) { - handler.handle(this); - } - public enum CallbackType { CLICK, CLOSE, diff --git a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java index 55d2b39..5f9ae81 100644 --- a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java +++ b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java @@ -19,23 +19,18 @@ package de.steamwar.network.packets.client; -import de.steamwar.network.packets.server.ServerNetworkHandler; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; -@Data -public class PrepareSchemPacket extends ClientPacket { +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class PrepareSchemPacket extends NetworkPacket { private static final long serialVersionUID = -4798561188105813349L; - @NonNull private int player; - @NonNull private int schem; - @NonNull private String schemType; - - @Override - public void handle(ServerNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/common/CommonPacket.java b/src/de/steamwar/network/packets/common/CommonPacket.java deleted file mode 100644 index ad58090..0000000 --- a/src/de/steamwar/network/packets/common/CommonPacket.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2022 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.network.packets.common; - -import de.steamwar.network.packets.NetworkPacket; - -public abstract class CommonPacket extends NetworkPacket { - public CommonPacket() { - super(Sender.BOTH); - } -} diff --git a/src/de/steamwar/network/packets/common/FightEndsPacket.java b/src/de/steamwar/network/packets/common/FightEndsPacket.java index b89476e..93c992e 100644 --- a/src/de/steamwar/network/packets/common/FightEndsPacket.java +++ b/src/de/steamwar/network/packets/common/FightEndsPacket.java @@ -19,33 +19,22 @@ package de.steamwar.network.packets.common; -import de.steamwar.network.packets.NetworkHandler; -import lombok.Builder; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.*; import java.util.List; @Builder -@Data -public class FightEndsPacket extends CommonPacket { +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class FightEndsPacket extends NetworkPacket { private static final long serialVersionUID = 1279352415549011332L; - @NonNull private byte win; - @NonNull private int blueSchem; - @NonNull private int redSchem; - @NonNull private List bluePlayers; - @NonNull private List redPlayers; - @NonNull private String gameMode; - - @Override - public void handle(NetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/common/FightInfoPacket.java b/src/de/steamwar/network/packets/common/FightInfoPacket.java index ff2a8f7..b0dfb3f 100644 --- a/src/de/steamwar/network/packets/common/FightInfoPacket.java +++ b/src/de/steamwar/network/packets/common/FightInfoPacket.java @@ -19,49 +19,30 @@ package de.steamwar.network.packets.common; -import de.steamwar.network.packets.NetworkHandler; -import lombok.Builder; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.*; import java.util.List; @Builder -@Data -public class FightInfoPacket extends CommonPacket{ +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class FightInfoPacket extends NetworkPacket { private static final long serialVersionUID = 7448644597856605853L; - @NonNull private String serverName; - @NonNull private String gameMode; - @NonNull private String arena; - @NonNull private String blueName; - @NonNull private String redName; - @NonNull private String fightState; - @NonNull private int countdown; - @NonNull private int blueLeader; - @NonNull private int redLeader; - @NonNull private int blueSchem; - @NonNull private int redSchem; - @NonNull private List bluePlayers; - @NonNull private List redPlayers; - @NonNull private List spectators; - - @Override - public void handle(NetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java b/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java index ac610d5..b72dfe9 100644 --- a/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java +++ b/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java @@ -19,15 +19,13 @@ package de.steamwar.network.packets.server; -import de.steamwar.network.packets.client.ClientNetworkHandler; +import de.steamwar.network.packets.NetworkPacket; import lombok.Data; +import lombok.EqualsAndHashCode; @Data -public class BaumemberUpdatePacket extends ServerPacket{ +@EqualsAndHashCode(callSuper = true) +public class BaumemberUpdatePacket extends NetworkPacket { private static final long serialVersionUID = 6863118892424244051L; - @Override - public void handle(ClientNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/server/CloseInventoryPacket.java b/src/de/steamwar/network/packets/server/CloseInventoryPacket.java index 7ea7e6a..1212970 100644 --- a/src/de/steamwar/network/packets/server/CloseInventoryPacket.java +++ b/src/de/steamwar/network/packets/server/CloseInventoryPacket.java @@ -19,19 +19,16 @@ package de.steamwar.network.packets.server; -import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; -@Data -public class CloseInventoryPacket extends ServerPacket { +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class CloseInventoryPacket extends NetworkPacket { private static final long serialVersionUID = -2191021190060504521L; - @NonNull private int playerId; - - @Override - public void handle(ClientNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/server/InventoryPacket.java b/src/de/steamwar/network/packets/server/InventoryPacket.java index 1a507d1..298d518 100644 --- a/src/de/steamwar/network/packets/server/InventoryPacket.java +++ b/src/de/steamwar/network/packets/server/InventoryPacket.java @@ -19,27 +19,21 @@ package de.steamwar.network.packets.server; -import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; import java.util.Map; -@Data -public class InventoryPacket extends ServerPacket { +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class InventoryPacket extends NetworkPacket { private static final long serialVersionUID = 8071052544654047316L; - @NonNull private String title; - @NonNull private int player; - @NonNull private int size; - @NonNull private Map items; - - @Override - public void handle(ClientNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/server/PingPacket.java b/src/de/steamwar/network/packets/server/PingPacket.java index f2ca61e..0c7f0bf 100644 --- a/src/de/steamwar/network/packets/server/PingPacket.java +++ b/src/de/steamwar/network/packets/server/PingPacket.java @@ -19,18 +19,15 @@ package de.steamwar.network.packets.server; -import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; -@Data -public class PingPacket extends ServerPacket { +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class PingPacket extends NetworkPacket { private static final long serialVersionUID = 714647343959550378L; - @NonNull private int id; - - @Override - public void handle(ClientNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/server/ServerNetworkHandler.java b/src/de/steamwar/network/packets/server/ServerNetworkHandler.java deleted file mode 100644 index 7947b84..0000000 --- a/src/de/steamwar/network/packets/server/ServerNetworkHandler.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2022 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.network.packets.server; - -import de.steamwar.network.packets.NetworkHandler; -import de.steamwar.network.packets.client.*; - -public abstract class ServerNetworkHandler extends NetworkHandler { - public void handle(ExecuteCommandPacket packet) {} - public void handle(ImALobbyPacket packet) {} - public void handle(InventoryCallbackPacket packet) {} - public void handle(PrepareSchemPacket packet) {} -} diff --git a/src/de/steamwar/network/packets/server/StartingServerPacket.java b/src/de/steamwar/network/packets/server/StartingServerPacket.java index 3dadceb..2476e22 100644 --- a/src/de/steamwar/network/packets/server/StartingServerPacket.java +++ b/src/de/steamwar/network/packets/server/StartingServerPacket.java @@ -19,18 +19,15 @@ package de.steamwar.network.packets.server; -import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; -@Data -public class StartingServerPacket extends ServerPacket { +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class StartingServerPacket extends NetworkPacket { private static final long serialVersionUID = 2808607245898121801L; - private int user; - - @Override - public void handle(ClientNetworkHandler handler) { - handler.handle(this); - } } diff --git a/testsrc/de/steamwar/RandomGenerator.java b/testsrc/de/steamwar/RandomGenerator.java new file mode 100644 index 0000000..b9ea1a2 --- /dev/null +++ b/testsrc/de/steamwar/RandomGenerator.java @@ -0,0 +1,83 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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; + +import java.util.*; +import java.util.function.Function; + +public class RandomGenerator { + + private static final Random random = new Random(); + private static final Map, Function, ?>> generators = new HashMap<>(); + + static { + generators.put(Integer.class, (clazz) -> random.nextInt()); + generators.put(int.class, (clazz) -> random.nextInt()); + generators.put(Double.class, (clazz) -> random.nextDouble()); + generators.put(double.class, (clazz) -> random.nextInt()); + generators.put(Boolean.class, (clazz) -> random.nextBoolean()); + generators.put(boolean.class, (clazz) -> random.nextBoolean()); + generators.put(Byte.class, (clazz) -> (byte) random.nextInt(127)); + generators.put(byte.class, (clazz) -> (byte) random.nextInt(127)); + + generators.put(List.class, aClass -> { + int length = random.nextInt(10); + List list = new ArrayList<>(); + for (int i = 0; i < length; i++) { + list.add(generators.get(Integer.class).apply(Integer.class)); + } + + return list; + }); + + generators.put(String.class, aClass -> { + int length = random.nextInt(10); + StringBuilder builder = new StringBuilder(); + + for (int i = 0; i < length; i++) { + builder.append(random.nextInt()); + } + + return builder.toString(); + }); + + generators.put(Map.class, aClass -> { + int length = random.nextInt(10); + Map map = new HashMap<>(); + for (int i = 0; i < length; i++) { + map.put(generators.get(Integer.class).apply(Integer.class), generators.get(String.class).apply(String.class)); + } + + return map; + }); + } + + public static T generateRandom(Class clazz) { + if(clazz.isEnum()) { + return generateRandomEnum(clazz); + } else { + return (T) generators.get(clazz).apply(clazz); + } + } + + public static T generateRandomEnum(Class clazz) { + return clazz.getEnumConstants()[random.nextInt(clazz.getEnumConstants().length)]; + } +} diff --git a/testsrc/de/steamwar/network/AllPackets.java b/testsrc/de/steamwar/network/AllPackets.java new file mode 100644 index 0000000..4d9fd86 --- /dev/null +++ b/testsrc/de/steamwar/network/AllPackets.java @@ -0,0 +1,80 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network; + +import de.steamwar.network.packets.NetworkPacket; +import de.steamwar.network.packets.client.ExecuteCommandPacket; +import de.steamwar.network.packets.client.ImALobbyPacket; +import de.steamwar.network.packets.client.InventoryCallbackPacket; +import de.steamwar.network.packets.client.PrepareSchemPacket; +import de.steamwar.network.packets.common.FightEndsPacket; +import de.steamwar.network.packets.common.FightInfoPacket; +import de.steamwar.network.packets.server.*; +import lombok.SneakyThrows; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Parameter; +import java.util.ArrayList; +import java.util.List; + +import static de.steamwar.RandomGenerator.*; + +public class AllPackets { + + public static final Class[] packets = new Class[]{ + ExecuteCommandPacket.class, + ImALobbyPacket.class, + InventoryCallbackPacket.class, + PrepareSchemPacket.class, + FightEndsPacket.class, + FightInfoPacket.class, + BaumemberUpdatePacket.class, + CloseInventoryPacket.class, + InventoryPacket.class, + PingPacket.class, + StartingServerPacket.class + }; + + @SneakyThrows + public static List getAllPackets() { + List packets = new ArrayList<>(); + + for (Class packet : AllPackets.packets) { + Constructor longConstructor = (Constructor) packet.getConstructors()[0]; + for (Constructor constructor : packet.getConstructors()) { + if (constructor.getParameterCount() > longConstructor.getParameterCount()) { + longConstructor = (Constructor) constructor; + } + } + + Object[] args = new Object[longConstructor.getParameterCount()]; + Parameter[] parameters = longConstructor.getParameters(); + for (int i = 0; i < parameters.length; i++) { + Parameter parameter = parameters[i]; + args[i] = generateRandom(parameter.getType()); + } + + NetworkPacket packetInstance = longConstructor.newInstance(args); + packets.add(packetInstance); + } + return packets; + } +} diff --git a/src/de/steamwar/network/packets/server/ServerPacket.java b/testsrc/de/steamwar/network/PacketTest.java similarity index 57% rename from src/de/steamwar/network/packets/server/ServerPacket.java rename to testsrc/de/steamwar/network/PacketTest.java index bfab1e7..5a65da6 100644 --- a/src/de/steamwar/network/packets/server/ServerPacket.java +++ b/testsrc/de/steamwar/network/PacketTest.java @@ -17,21 +17,22 @@ * along with this program. If not, see . */ -package de.steamwar.network.packets.server; +package de.steamwar.network; -import de.steamwar.network.packets.NetworkHandler; import de.steamwar.network.packets.NetworkPacket; -import de.steamwar.network.packets.client.ClientNetworkHandler; +import org.junit.Test; -public abstract class ServerPacket extends NetworkPacket { - public ServerPacket() { - super(Sender.SERVER); +import static org.junit.Assert.*; + +public class PacketTest { + + @Test + public void testPacketSerialization() { + for (NetworkPacket allPacket : AllPackets.getAllPackets()) { + byte[] data = allPacket.serialize(); + NetworkPacket deserializedPacket = NetworkPacket.deserialize(data); + assertEquals("Packet: " + allPacket.getClass().getName() + " is not correctly deserialized", allPacket, deserializedPacket); + System.out.println("Packet: " + allPacket.getClass().getName() + " is correctly deserialized"); + } } - - @Override - public void handle(NetworkHandler handler) { - handle((ClientNetworkHandler) handler); - } - - public abstract void handle(ClientNetworkHandler handler); } -- 2.39.2 From d51b8db0bfc3b58a4eed98f756c34960ffe0d3e0 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 1 May 2022 22:13:37 +0200 Subject: [PATCH 10/14] :art: Refactor --- .../network/packets/PacketHandler.java | 11 ++-- .../steamwar/network/PacketHandlerTest.java | 52 +++++++++++++++++++ testsrc/de/steamwar/network/PacketTest.java | 6 +-- testsrc/de/steamwar/network/TestPacket.java | 30 +++++++++++ .../steamwar/network/TestPacketHandler.java | 30 +++++++++++ 5 files changed, 120 insertions(+), 9 deletions(-) create mode 100644 testsrc/de/steamwar/network/PacketHandlerTest.java create mode 100644 testsrc/de/steamwar/network/TestPacket.java create mode 100644 testsrc/de/steamwar/network/TestPacketHandler.java diff --git a/src/de/steamwar/network/packets/PacketHandler.java b/src/de/steamwar/network/packets/PacketHandler.java index 2f01ab0..f23afe5 100644 --- a/src/de/steamwar/network/packets/PacketHandler.java +++ b/src/de/steamwar/network/packets/PacketHandler.java @@ -19,6 +19,8 @@ package de.steamwar.network.packets; +import lombok.SneakyThrows; + import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @@ -39,7 +41,7 @@ public abstract class PacketHandler { } } - public PacketHandler() { + protected PacketHandler() { Method[] methods = getClass().getMethods(); for (Method method : methods) { if(method.getParameterCount() != 1 || !NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) { @@ -60,16 +62,13 @@ public abstract class PacketHandler { PACKET_HANDLERS.remove(this); } + @SneakyThrows public void handle(NetworkPacket packet) { Method method = HANDLER_MAP.get(packet.getClass()); if (method == null) { return; } - try { - method.invoke(this, packet); - } catch (Exception e) { - e.printStackTrace(); - } + method.invoke(this, packet); } @Retention(RetentionPolicy.RUNTIME) diff --git a/testsrc/de/steamwar/network/PacketHandlerTest.java b/testsrc/de/steamwar/network/PacketHandlerTest.java new file mode 100644 index 0000000..ac2299c --- /dev/null +++ b/testsrc/de/steamwar/network/PacketHandlerTest.java @@ -0,0 +1,52 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network; + +import de.steamwar.network.packets.PacketHandler; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.InvocationTargetException; + +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +public class PacketHandlerTest { + + TestPacketHandler packetHandler; + + @Before + public void setUp() { + packetHandler = new TestPacketHandler(); + packetHandler.register(); + } + + @Test + public void testPacketHandler() { + TestPacket packet = new TestPacket(); + try { + PacketHandler.handlePacket(packet); + } catch (Exception e) { + assertThat(e, instanceOf(InvocationTargetException.class)); + assertThat(e.getCause(), instanceOf(SecurityException.class)); + assertThat(e.getCause().getMessage(), equalTo(packet.getClass().getName())); + } + } +} diff --git a/testsrc/de/steamwar/network/PacketTest.java b/testsrc/de/steamwar/network/PacketTest.java index 5a65da6..316ea6f 100644 --- a/testsrc/de/steamwar/network/PacketTest.java +++ b/testsrc/de/steamwar/network/PacketTest.java @@ -22,7 +22,8 @@ package de.steamwar.network; import de.steamwar.network.packets.NetworkPacket; import org.junit.Test; -import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; +import static org.hamcrest.MatcherAssert.*; public class PacketTest { @@ -31,8 +32,7 @@ public class PacketTest { for (NetworkPacket allPacket : AllPackets.getAllPackets()) { byte[] data = allPacket.serialize(); NetworkPacket deserializedPacket = NetworkPacket.deserialize(data); - assertEquals("Packet: " + allPacket.getClass().getName() + " is not correctly deserialized", allPacket, deserializedPacket); - System.out.println("Packet: " + allPacket.getClass().getName() + " is correctly deserialized"); + assertThat("Packet: " + allPacket.getClass().getName() + " is not correctly deserialized", allPacket, equalTo(deserializedPacket)); } } } diff --git a/testsrc/de/steamwar/network/TestPacket.java b/testsrc/de/steamwar/network/TestPacket.java new file mode 100644 index 0000000..7f95f03 --- /dev/null +++ b/testsrc/de/steamwar/network/TestPacket.java @@ -0,0 +1,30 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network; + +import de.steamwar.network.packets.NetworkPacket; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +@EqualsAndHashCode(callSuper = true) +@NoArgsConstructor +public class TestPacket extends NetworkPacket { + private static final long serialVersionUID = -9079104421295590626L; +} diff --git a/testsrc/de/steamwar/network/TestPacketHandler.java b/testsrc/de/steamwar/network/TestPacketHandler.java new file mode 100644 index 0000000..2d525be --- /dev/null +++ b/testsrc/de/steamwar/network/TestPacketHandler.java @@ -0,0 +1,30 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.network; + +import de.steamwar.network.packets.PacketHandler; + +public class TestPacketHandler extends PacketHandler { + + @Handler + public void handle(TestPacket packet) { + throw new SecurityException(packet.getClass().getName()); + } +} -- 2.39.2 From d5ed6abcad7eae8548ce8f7b26f95a01b15f82cc Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 3 May 2022 15:43:16 +0200 Subject: [PATCH 11/14] :art: Update @Getter --- src/de/steamwar/network/packets/client/ImALobbyPacket.java | 1 + .../network/packets/client/InventoryCallbackPacket.java | 1 + src/de/steamwar/network/packets/client/PrepareSchemPacket.java | 2 ++ src/de/steamwar/network/packets/common/FightEndsPacket.java | 1 + src/de/steamwar/network/packets/common/FightInfoPacket.java | 1 + .../steamwar/network/packets/server/BaumemberUpdatePacket.java | 2 -- .../steamwar/network/packets/server/CloseInventoryPacket.java | 2 ++ src/de/steamwar/network/packets/server/InventoryPacket.java | 2 ++ src/de/steamwar/network/packets/server/PingPacket.java | 2 ++ .../steamwar/network/packets/server/StartingServerPacket.java | 2 ++ testsrc/de/steamwar/network/AllPackets.java | 2 +- 11 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/de/steamwar/network/packets/client/ImALobbyPacket.java b/src/de/steamwar/network/packets/client/ImALobbyPacket.java index dcd07dd..d8e1ba4 100644 --- a/src/de/steamwar/network/packets/client/ImALobbyPacket.java +++ b/src/de/steamwar/network/packets/client/ImALobbyPacket.java @@ -21,6 +21,7 @@ package de.steamwar.network.packets.client; import de.steamwar.network.packets.NetworkPacket; import lombok.EqualsAndHashCode; +import lombok.Getter; @EqualsAndHashCode(callSuper = true) public class ImALobbyPacket extends NetworkPacket { diff --git a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java index 2e1188b..cfe9fbc 100644 --- a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java +++ b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java @@ -26,6 +26,7 @@ import lombok.*; @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(callSuper = true) +@Getter public class InventoryCallbackPacket extends NetworkPacket { private static final long serialVersionUID = -261823209186008718L; @Builder.Default diff --git a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java index 5f9ae81..8f607fa 100644 --- a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java +++ b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java @@ -22,11 +22,13 @@ package de.steamwar.network.packets.client; import de.steamwar.network.packets.NetworkPacket; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper = true) +@Getter public class PrepareSchemPacket extends NetworkPacket { private static final long serialVersionUID = -4798561188105813349L; diff --git a/src/de/steamwar/network/packets/common/FightEndsPacket.java b/src/de/steamwar/network/packets/common/FightEndsPacket.java index 93c992e..a970d7b 100644 --- a/src/de/steamwar/network/packets/common/FightEndsPacket.java +++ b/src/de/steamwar/network/packets/common/FightEndsPacket.java @@ -28,6 +28,7 @@ import java.util.List; @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper = true) +@Getter public class FightEndsPacket extends NetworkPacket { private static final long serialVersionUID = 1279352415549011332L; diff --git a/src/de/steamwar/network/packets/common/FightInfoPacket.java b/src/de/steamwar/network/packets/common/FightInfoPacket.java index b0dfb3f..5a6c258 100644 --- a/src/de/steamwar/network/packets/common/FightInfoPacket.java +++ b/src/de/steamwar/network/packets/common/FightInfoPacket.java @@ -28,6 +28,7 @@ import java.util.List; @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper = true) +@Getter public class FightInfoPacket extends NetworkPacket { private static final long serialVersionUID = 7448644597856605853L; diff --git a/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java b/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java index b72dfe9..5351473 100644 --- a/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java +++ b/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java @@ -20,10 +20,8 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.NetworkPacket; -import lombok.Data; import lombok.EqualsAndHashCode; -@Data @EqualsAndHashCode(callSuper = true) public class BaumemberUpdatePacket extends NetworkPacket { private static final long serialVersionUID = 6863118892424244051L; diff --git a/src/de/steamwar/network/packets/server/CloseInventoryPacket.java b/src/de/steamwar/network/packets/server/CloseInventoryPacket.java index 1212970..e30d927 100644 --- a/src/de/steamwar/network/packets/server/CloseInventoryPacket.java +++ b/src/de/steamwar/network/packets/server/CloseInventoryPacket.java @@ -22,11 +22,13 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.NetworkPacket; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper = true) +@Getter public class CloseInventoryPacket extends NetworkPacket { private static final long serialVersionUID = -2191021190060504521L; diff --git a/src/de/steamwar/network/packets/server/InventoryPacket.java b/src/de/steamwar/network/packets/server/InventoryPacket.java index 298d518..8e7f7b3 100644 --- a/src/de/steamwar/network/packets/server/InventoryPacket.java +++ b/src/de/steamwar/network/packets/server/InventoryPacket.java @@ -22,6 +22,7 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.NetworkPacket; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; import java.util.Map; @@ -29,6 +30,7 @@ import java.util.Map; @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper = true) +@Getter public class InventoryPacket extends NetworkPacket { private static final long serialVersionUID = 8071052544654047316L; diff --git a/src/de/steamwar/network/packets/server/PingPacket.java b/src/de/steamwar/network/packets/server/PingPacket.java index 0c7f0bf..88ac32a 100644 --- a/src/de/steamwar/network/packets/server/PingPacket.java +++ b/src/de/steamwar/network/packets/server/PingPacket.java @@ -22,11 +22,13 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.NetworkPacket; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(callSuper = true) +@Getter public class PingPacket extends NetworkPacket { private static final long serialVersionUID = 714647343959550378L; private int id; diff --git a/src/de/steamwar/network/packets/server/StartingServerPacket.java b/src/de/steamwar/network/packets/server/StartingServerPacket.java index 2476e22..f536d8f 100644 --- a/src/de/steamwar/network/packets/server/StartingServerPacket.java +++ b/src/de/steamwar/network/packets/server/StartingServerPacket.java @@ -22,11 +22,13 @@ package de.steamwar.network.packets.server; import de.steamwar.network.packets.NetworkPacket; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(callSuper = true) +@Getter public class StartingServerPacket extends NetworkPacket { private static final long serialVersionUID = 2808607245898121801L; private int user; diff --git a/testsrc/de/steamwar/network/AllPackets.java b/testsrc/de/steamwar/network/AllPackets.java index 4d9fd86..fe4d10a 100644 --- a/testsrc/de/steamwar/network/AllPackets.java +++ b/testsrc/de/steamwar/network/AllPackets.java @@ -30,7 +30,6 @@ import de.steamwar.network.packets.server.*; import lombok.SneakyThrows; import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Parameter; import java.util.ArrayList; import java.util.List; @@ -69,6 +68,7 @@ public class AllPackets { Parameter[] parameters = longConstructor.getParameters(); for (int i = 0; i < parameters.length; i++) { Parameter parameter = parameters[i]; + parameter.getName(); args[i] = generateRandom(parameter.getType()); } -- 2.39.2 From 0d3ad7783be2fe4de5b2e52682f6badca4bc8b6e Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 3 May 2022 15:51:30 +0200 Subject: [PATCH 12/14] :art: Update @Getter --- .../steamwar/network/packets/client/ExecuteCommandPacket.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java b/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java index 0c56fc9..9f7d000 100644 --- a/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java +++ b/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java @@ -22,11 +22,13 @@ package de.steamwar.network.packets.client; import de.steamwar.network.packets.NetworkPacket; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper = true) +@Getter public class ExecuteCommandPacket extends NetworkPacket { private static final long serialVersionUID = 6283457297487602016L; -- 2.39.2 From 75c03f79c5a0719a65a3dd34cdd705dfb2d56a01 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 3 May 2022 15:55:14 +0200 Subject: [PATCH 13/14] :art: Update @Getter --- src/de/steamwar/network/packets/common/FightInfoPacket.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/network/packets/common/FightInfoPacket.java b/src/de/steamwar/network/packets/common/FightInfoPacket.java index 5a6c258..bbca8b0 100644 --- a/src/de/steamwar/network/packets/common/FightInfoPacket.java +++ b/src/de/steamwar/network/packets/common/FightInfoPacket.java @@ -32,7 +32,7 @@ import java.util.List; public class FightInfoPacket extends NetworkPacket { private static final long serialVersionUID = 7448644597856605853L; - private String serverName; + private @With String serverName; private String gameMode; private String arena; private String blueName; -- 2.39.2 From fed8a5f830a5b15c78c3a887debc26350d85012b Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 9 May 2022 17:24:08 +0200 Subject: [PATCH 14/14] :art: Update Exceptions --- .../network/packets/NetworkPacket.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/de/steamwar/network/packets/NetworkPacket.java b/src/de/steamwar/network/packets/NetworkPacket.java index 32857c6..b1d6bf3 100644 --- a/src/de/steamwar/network/packets/NetworkPacket.java +++ b/src/de/steamwar/network/packets/NetworkPacket.java @@ -20,21 +20,19 @@ package de.steamwar.network.packets; import lombok.EqualsAndHashCode; +import lombok.SneakyThrows; import java.io.*; @EqualsAndHashCode public abstract class NetworkPacket implements Serializable { + @SneakyThrows public byte[] serialize() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(this); - oos.flush(); - } catch (Exception e) { - throw new RuntimeException(e); - } + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(this); + oos.flush(); return baos.toByteArray(); } @@ -42,13 +40,10 @@ public abstract class NetworkPacket implements Serializable { PacketHandler.handlePacket(deserialize(data)); } + @SneakyThrows public static NetworkPacket deserialize(byte[] data) { ByteArrayInputStream bais = new ByteArrayInputStream(data); - try { - ObjectInputStream ois = new ObjectInputStream(bais); - return (NetworkPacket) ois.readObject(); - } catch (Exception e) { - throw new RuntimeException(e); - } + ObjectInputStream ois = new ObjectInputStream(bais); + return (NetworkPacket) ois.readObject(); } } -- 2.39.2