diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 5b932b3..7313fba 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -19,6 +19,7 @@ package de.steamwar.command; +import de.steamwar.sql.SteamwarUser; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.command.Command; @@ -73,6 +74,7 @@ public class SWCommandUtils { if (s.equals("a") || s.equals("adventure") || s.equals("2")) return GameMode.ADVENTURE; return null; }, s -> Arrays.asList("s", "survival", "0", "c", "creative", "1", "sp", "spectator", "3", "a", "adventure", "2"))); + MAPPER_FUNCTIONS.put(SteamwarUser.class.getTypeName(), createMapper(SteamwarUser::get, s -> Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()))); } private static void addMapper(Class clazz, Class alternativeClazz, TypeMapper mapper) { diff --git a/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java b/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java index c0c160c..dce1882 100644 --- a/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java +++ b/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java @@ -29,7 +29,8 @@ public interface TypeMapper { } // For backwards compatibility, can be removed later on - @Deprecated(since = "Use the other map Function without calling super!") + // SINCE="Use the other map Function without calling super!" + @Deprecated default T map(String[] previousArguments, String s) { throw new SecurityException(); } diff --git a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java index b326674..f7b71e4 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java +++ b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java @@ -20,14 +20,18 @@ package de.steamwar.comms; public class PacketIdManager { + private PacketIdManager(){} //0x0(X) Standalone Packets - public final static byte PING_PACKET = 0x01; - public final static byte TABLIST_NAME = 0x02; + public static final byte PING_PACKET = 0x01; + public static final byte TABLIST_NAME = 0x02; public static final byte PREPARE_SCHEM = 0x03; - public final static byte BAUMEMBER_UPDATE = 0x04; + public static final byte BAUMEMBER_UPDATE = 0x04; //0x1(X) Bungee Inventory - public final static byte INVENTORY_PACKET = 0x10; - public final static byte INVENTORY_CALLBACK_PACKET = 0x11; - public final static byte INVENTORY_CLOSE_PACKET = 0x12; + public static final byte INVENTORY_PACKET = 0x10; + public static final byte INVENTORY_CALLBACK_PACKET = 0x11; + public static final byte INVENTORY_CLOSE_PACKET = 0x12; + //0x2(X) Server Information System + public static final byte I_AM_A_LOBBY = 0x20; + public static final byte FIGHT_INFO = 0x21; } diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/FightInfoPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/FightInfoPacket.java new file mode 100644 index 0000000..54b9498 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/FightInfoPacket.java @@ -0,0 +1,174 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package de.steamwar.comms.packets; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteArrayDataOutput; +import de.steamwar.comms.PacketIdManager; + +import java.util.ArrayList; +import java.util.List; + +public class FightInfoPacket extends SpigotPacket { + + private final String serverName; // Name of the Server, might be changed by Bungee + private final String gameMode; // GameMode aka Schematictype (if known, else "") + private final String arena; // Name of the arena + private final String blueName; // Name of the blue team, expected to begin with "§a" colorcode + private final String redName; // Name of the red team, expected to begin with "§a" colorcode + private final String fightState; // Fight state (technical term) (if known, else "") + private final int countdown; // Countdown state in seconds (if known, else 0) + private final int blueLeader; // SWUserID of the blue team leader (if known, else 0) + private final int redLeader; // SWUserID of the red team leader (if known, else 0) + private final int blueSchem; // Blue SchemID (if known, else 0) + private final int redSchem; // Red SchemID (if known, else 0) + private final List bluePlayers; // List of Blue SWUserIDs + private final List redPlayers; // List of Red SWUserIDs + private final List spectators; // List of Spectator SWUserIDs + + public FightInfoPacket(String serverName, String gameMode, String arena, String blueName, String redName, String fightState, int countdown, int blueLeader, int redLeader, int blueSchem, int redSchem, List bluePlayers, List redPlayers, List spectators) { + this.serverName = serverName; + this.gameMode = gameMode; + this.arena = arena; + this.blueName = blueName; + this.redName = redName; + this.fightState = fightState; + this.countdown = countdown; + this.blueLeader = blueLeader; + this.redLeader = redLeader; + this.blueSchem = blueSchem; + this.redSchem = redSchem; + this.bluePlayers = bluePlayers; + this.redPlayers = redPlayers; + this.spectators = spectators; + } + + public FightInfoPacket(ByteArrayDataInput in) { + this.serverName = in.readUTF(); + this.gameMode = in.readUTF(); + this.arena = in.readUTF(); + this.blueName = in.readUTF(); + this.redName = in.readUTF(); + this.fightState = in.readUTF(); + this.countdown = in.readInt(); + this.blueLeader = in.readInt(); + this.redLeader = in.readInt(); + this.blueSchem = in.readInt(); + this.redSchem = in.readInt(); + this.bluePlayers = readPlayerList(in); + this.redPlayers = readPlayerList(in); + this.spectators = readPlayerList(in); + } + + @Override + public int getName() { + return PacketIdManager.FIGHT_INFO; + } + + @Override + public void writeVars(ByteArrayDataOutput out) { + out.writeUTF(serverName); + out.writeUTF(gameMode); + out.writeUTF(arena); + out.writeUTF(blueName); + out.writeUTF(redName); + out.writeUTF(fightState); + out.writeInt(countdown); + out.writeInt(blueLeader); + out.writeInt(redLeader); + out.writeInt(blueSchem); + out.writeInt(redSchem); + writePlayerList(out, bluePlayers); + writePlayerList(out, redPlayers); + writePlayerList(out, spectators); + } + + public String getServerName() { + return serverName; + } + + public String getGameMode() { + return gameMode; + } + + public String getArena() { + return arena; + } + + public String getBlueName() { + return blueName; + } + + public String getRedName() { + return redName; + } + + public String getFightState() { + return fightState; + } + + public int getCountdown() { + return countdown; + } + + public int getBlueLeader() { + return blueLeader; + } + + public int getRedLeader() { + return redLeader; + } + + public int getBlueSchem() { + return blueSchem; + } + + public int getRedSchem() { + return redSchem; + } + + public List getBluePlayers() { + return bluePlayers; + } + + public List getRedPlayers() { + return redPlayers; + } + + public List getSpectators() { + return spectators; + } + + private static List readPlayerList(ByteArrayDataInput in) { + int length = in.readInt(); + List players = new ArrayList<>(length); + for(int i = 0; i < length; i++) { + players.add(in.readInt()); + } + return players; + } + + private void writePlayerList(ByteArrayDataOutput out, List players) { + out.writeInt(players.size()); + for(Integer player : players) { + out.writeInt(player); + } + } +} diff --git a/SpigotCore_Main/src/de/steamwar/comms/packets/ImALobbyPacket.java b/SpigotCore_Main/src/de/steamwar/comms/packets/ImALobbyPacket.java new file mode 100644 index 0000000..dbe7c0a --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/comms/packets/ImALobbyPacket.java @@ -0,0 +1,36 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package de.steamwar.comms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import de.steamwar.comms.PacketIdManager; + +public class ImALobbyPacket extends SpigotPacket { + + @Override + public int getName() { + return PacketIdManager.I_AM_A_LOBBY; + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + //no content + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java index 8d5bb86..5b5d4f5 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java @@ -57,6 +57,11 @@ public class EventFight { SQL.update("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?", winner, fightID); } + public void setFight(int fight){ + //Fight.FightID, not EventFight.FightID + SQL.update("UPDATE EventFight SET Fight = ? WHERE FightID = ?", fight, fightID); + } + public int getTeamBlue() { return teamBlue; } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Fight.java b/SpigotCore_Main/src/de/steamwar/sql/Fight.java index c0448dc..7604b12 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Fight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Fight.java @@ -19,6 +19,8 @@ package de.steamwar.sql; +import java.io.InputStream; +import java.sql.Blob; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; @@ -27,8 +29,12 @@ public class Fight { private Fight(){} public static int create(String gamemode, String arena, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){ - SQL.update("INSERT INTO Fight (GameMode, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", - gamemode, arena, starttime, duration, blueleader, redleader,blueschem, redschem, win, wincondition); + return create(gamemode, arena, null, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition); + } + + public static int create(String gamemode, String server, String arena, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){ + SQL.update("INSERT INTO Fight (GameMode, Server, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + gamemode, server, arena, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition); ResultSet rs = SQL.select("SELECT LAST_INSERT_ID() AS FightID"); try{ if(!rs.next()) @@ -39,4 +45,27 @@ public class Fight { throw new SecurityException(e); } } + + public static InputStream getReplay(int fightID) { + ResultSet rs = SQL.select("SELECT Replay FROM Fight WHERE FightID = ?", fightID); + try { + rs.next(); + Blob replay = rs.getBlob("Replay"); + if(replay == null) + throw new SecurityException("Replay null"); + return replay.getBinaryStream(); + } catch (SQLException e) { + throw new SecurityException(e); + } + } + + public static void setReplay(int fightID, byte[] data) { + Blob blob = SQL.blob(); + try { + blob.setBytes(1, data); + } catch (SQLException e) { + throw new SecurityException(e); + } + SQL.update("UPDATE Fight SET Replay = ? WHERE FightID = ?", blob, fightID); + } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index dbdee0f..417fb1e 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -182,21 +182,28 @@ public class Schematic { return true; } - public Clipboard load() throws IOException, NoClipboardException { + public InputStream schemData() throws IOException { ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = ?", schemID); try { rs.next(); Blob schemData = rs.getBlob("SchemData"); if(schemData == null) throw new IOException("SchemData is null"); - InputStream is = schemData.getBinaryStream(); - return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8), - new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14)); + return schemData.getBinaryStream(); } catch (SQLException e) { throw new IOException(e); } } + public static Clipboard clipboardFromStream(InputStream is, boolean schemFormat) { + return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8), + new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14)); + } + + public Clipboard load() throws IOException, NoClipboardException { + return clipboardFromStream(schemData(), schemFormat); + } + public void loadToPlayer(Player player) throws IOException, NoClipboardException { ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = ?", schemID); try { diff --git a/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java b/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java index b7a87c3..7c87302 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java +++ b/SpigotCore_Main/src/de/steamwar/sql/UserConfig.java @@ -64,5 +64,4 @@ public class UserConfig { public static void removePlayerConfig(int id, String config) { SQL.update("DELETE FROM UserConfig WHERE User = ? AND Config = ?", id, config); } - }