From 986b53fbf82bf6940d31a1c26aecd85179a304e9 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 14 Nov 2020 23:23:49 +0100 Subject: [PATCH] Reworked Ban and Mute --- src/de/steamwar/bungeecore/BungeeCore.java | 2 +- .../bungeecore/commands/BanCommand.java | 5 +- .../bungeecore/commands/MuteCommand.java | 3 +- .../bungeecore/commands/UnbanCommand.java | 3 +- .../bungeecore/commands/WhoisCommand.java | 10 +- .../bungeecore/listeners/BanListener.java | 9 ++ .../bungeecore/listeners/mods/Utils.java | 4 +- .../bungeecore/sql/BannedUserIPs.java | 4 + .../steamwar/bungeecore/sql/Punishment.java | 141 ++++++++++++++++++ .../steamwar/bungeecore/sql/SteamwarUser.java | 65 +++----- 10 files changed, 189 insertions(+), 57 deletions(-) create mode 100644 src/de/steamwar/bungeecore/sql/Punishment.java diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index a6a9cca..ce64862 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -54,7 +54,7 @@ public class BungeeCore extends Plugin { public static final String SERVER_TEAMCHAT_PREFIX = "§8STC §e"; public static final String TEAMCHAT_PREFIX = "§8TC §e"; - public static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + public static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("HH:mm dd.MM.yyyy"); public static String CHAT_PREFIX; public static String WORLD_FOLDER; diff --git a/src/de/steamwar/bungeecore/commands/BanCommand.java b/src/de/steamwar/bungeecore/commands/BanCommand.java index 9ac7ff8..c2d637d 100644 --- a/src/de/steamwar/bungeecore/commands/BanCommand.java +++ b/src/de/steamwar/bungeecore/commands/BanCommand.java @@ -22,6 +22,7 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.connection.ProxiedPlayer; import java.sql.Timestamp; import java.text.ParseException; @@ -56,12 +57,12 @@ public class BanCommand extends BasicCommand { } String msg = banReason.toString(); BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "Du hast " + target.getUserName() + " gebannt. Grund: §c" + msg); - target.ban(banTime, msg); + target.ban(banTime, msg, SteamwarUser.get(sender.getName()).getId(), args[1].equalsIgnoreCase("perma")); } public static Timestamp parseTime(CommandSender sender, String arg){ if(arg.equalsIgnoreCase("perma")) { - return Timestamp.from(Instant.ofEpochSecond(946674800)); + return Timestamp.from(Instant.ofEpochSecond(946684800)); }else{ SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy_HH:mm"); try{ diff --git a/src/de/steamwar/bungeecore/commands/MuteCommand.java b/src/de/steamwar/bungeecore/commands/MuteCommand.java index dc0d681..cf35b9d 100644 --- a/src/de/steamwar/bungeecore/commands/MuteCommand.java +++ b/src/de/steamwar/bungeecore/commands/MuteCommand.java @@ -24,6 +24,7 @@ import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.CommandSender; import java.sql.Timestamp; +import java.time.Instant; public class MuteCommand extends BasicCommand { @@ -52,6 +53,6 @@ public class MuteCommand extends BasicCommand { } String msg = muteReason.toString(); BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "Du hast " + target.getUserName() + " gemuted. Grund: §c" + msg); - target.mute(muteTime, msg); + target.mute(muteTime, msg, SteamwarUser.get(sender.getName()).getId(), args[1].equalsIgnoreCase("perma")); } } diff --git a/src/de/steamwar/bungeecore/commands/UnbanCommand.java b/src/de/steamwar/bungeecore/commands/UnbanCommand.java index 080a8d6..c21561a 100644 --- a/src/de/steamwar/bungeecore/commands/UnbanCommand.java +++ b/src/de/steamwar/bungeecore/commands/UnbanCommand.java @@ -22,6 +22,7 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.connection.ProxiedPlayer; import java.sql.Timestamp; import java.util.Date; @@ -44,6 +45,6 @@ public class UnbanCommand extends BasicCommand { return; BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "Du hast " + target.getUserName() + " entbannt."); - target.ban(Timestamp.from(new Date().toInstant()), ""); + target.ban(Timestamp.from(new Date().toInstant()), "Unban", SteamwarUser.get(sender.getName()).getId(), false); } } diff --git a/src/de/steamwar/bungeecore/commands/WhoisCommand.java b/src/de/steamwar/bungeecore/commands/WhoisCommand.java index 5bd83d6..cc66c1a 100644 --- a/src/de/steamwar/bungeecore/commands/WhoisCommand.java +++ b/src/de/steamwar/bungeecore/commands/WhoisCommand.java @@ -20,6 +20,7 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.BungeeCore; +import de.steamwar.bungeecore.sql.Punishment; import de.steamwar.bungeecore.sql.SteamwarUser; import de.steamwar.bungeecore.sql.Team; import net.md_5.bungee.api.CommandSender; @@ -70,11 +71,10 @@ public class WhoisCommand extends BasicCommand { Team team = Team.get(user.getTeam()); BungeeCore.send(player, "§7Team§8: §e" + team.getTeamName()); - if(user.isBanned()){ - player.sendMessage(user.banMessage()); - } - if(user.isMuted()){ - player.sendMessage(user.muteMessage()); + BungeeCore.send(player, "§7Bestrafungen: "); + for (Punishment punishment : Punishment.getAllPunishmentsByPlayer(user.getId())) { + BungeeCore.send(player, "§7" + SteamwarUser.get(punishment.getPunisher()).getUserName() + "§8» §f§l" + punishment.getType().name() + ": §e" + + punishment.getStartTime().toLocalDateTime().format(BungeeCore.DATE_FORMAT) + " - " + (punishment.isPerma()?"Perma":punishment.getEndTime().toLocalDateTime().format(BungeeCore.DATE_FORMAT)) + " §c" + punishment.getReason()); } } } diff --git a/src/de/steamwar/bungeecore/listeners/BanListener.java b/src/de/steamwar/bungeecore/listeners/BanListener.java index c9aadb7..bc56de3 100644 --- a/src/de/steamwar/bungeecore/listeners/BanListener.java +++ b/src/de/steamwar/bungeecore/listeners/BanListener.java @@ -47,6 +47,15 @@ public class BanListener extends BasicListener { List ips = BannedUserIPs.get(event.getConnection().getAddress().getAddress().getHostAddress()); if(!ips.isEmpty()){ + if(ips.stream().anyMatch(bannedUserIPs -> bannedUserIPs.getUserID() == user.getId())) { + ips.forEach(bannedUserIPs -> { + if(bannedUserIPs.getUserID() == user.getId()) + bannedUserIPs.remove(); + }); + ips.removeIf(bannedUserIPs -> bannedUserIPs.getUserID() == user.getId()); + if(ips.isEmpty()) + return; + } StringBuilder potentialBan = new StringBuilder(); potentialBan.append(BungeeCore.CHAT_PREFIX); potentialBan.append("§cMögliche Bannumgehung durch §r"); diff --git a/src/de/steamwar/bungeecore/listeners/mods/Utils.java b/src/de/steamwar/bungeecore/listeners/mods/Utils.java index 7f6d16f..1ca83e3 100644 --- a/src/de/steamwar/bungeecore/listeners/mods/Utils.java +++ b/src/de/steamwar/bungeecore/listeners/mods/Utils.java @@ -74,7 +74,7 @@ class Utils { if(max == ModType.YELLOW) player.disconnect(BungeeCore.stringToText("§7Deaktiviere den Mod §e" + mods.get(0).getModName() + "§7, um weiter auf §eSteam§8War §7spielen zu können.")); else{ - user.ban(Timestamp.from(Instant.now().plus(7, ChronoUnit.DAYS)), "Versuchte Benutzung des Mods " + mods.get(0).getModName()); + user.ban(Timestamp.from(Instant.now().plus(7, ChronoUnit.DAYS)), "Versuchte Benutzung des Mods " + mods.get(0).getModName(), 0, false); BungeeCore.log(Level.SEVERE, user.getUserName() + " " + user.getId() + " wurde automatisch wegen des Mods " + mods.get(0).getModName() + " gebannt."); } }else{ @@ -84,7 +84,7 @@ class Utils { if(max == ModType.YELLOW) player.disconnect(BungeeCore.stringToText("§7Deaktiviere die Mods\n§e" + sb.toString() + "§7um weiter auf §eSteam§8War §7spielen zu können.")); else{ - user.ban(Timestamp.from(Instant.now().plus(7, ChronoUnit.DAYS)), "Versuchte Benutzung der Mods\n" + sb.toString()); + user.ban(Timestamp.from(Instant.now().plus(7, ChronoUnit.DAYS)), "Versuchte Benutzung der Mods\n" + sb.toString(), 0, false); BungeeCore.log(Level.SEVERE, user.getUserName() + " " + user.getId() + " wurde automatisch wegen der Mods " + sb.toString() + " gebannt."); } } diff --git a/src/de/steamwar/bungeecore/sql/BannedUserIPs.java b/src/de/steamwar/bungeecore/sql/BannedUserIPs.java index 6b371cc..243e5f3 100644 --- a/src/de/steamwar/bungeecore/sql/BannedUserIPs.java +++ b/src/de/steamwar/bungeecore/sql/BannedUserIPs.java @@ -78,4 +78,8 @@ public class BannedUserIPs { public java.sql.Timestamp getTimestamp() { return timestamp; } + + public void remove() { + SQL.update("DELETE FROM BannedUserIPs WHERE UserID = ?", userID); + } } diff --git a/src/de/steamwar/bungeecore/sql/Punishment.java b/src/de/steamwar/bungeecore/sql/Punishment.java new file mode 100644 index 0000000..ec787a7 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/Punishment.java @@ -0,0 +1,141 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.steamwar.bungeecore.sql; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.*; +import java.util.stream.Collectors; + +public class Punishment { + + public static Punishment getPunishmentByPlayer(int user, PunishmentType type) { + ResultSet set = SQL.select("SELECT * FROM `Punishments` WHERE `PunishmentId` IN (SELECT MAX(`PunishmentId`) FROM Punishments WHERE UserId = ? AND Type = ?)", user, type.toDb()); + try { + if(!set.next()) + return null; + return new Punishment(set); + } catch (SQLException throwable) { + throwable.printStackTrace(); + throw new SecurityException("Could not Load Punishments", throwable); + } + } + + public static Map getPunishmentsByPlayer(int user) { + ResultSet set = SQL.select("SELECT * FROM `Punishments` WHERE `PunishmentId` IN (SELECT MAX(`PunishmentId`) FROM Punishments WHERE UserId = ? GROUP BY Type)", user); + try { + Map punishmentMap = new HashMap<>(); + while (set.next()) { + Punishment punishment = new Punishment(set); + punishmentMap.put(punishment.getType(), punishment); + } + return punishmentMap; + } catch (SQLException throwable) { + throwable.printStackTrace(); + throw new SecurityException("Could not Load Punishments", throwable); + } + } + + public static Set getAllPunishmentsByPlayer(int user) { + ResultSet set = SQL.select("SELECT * FROM Punishments WHERE UserId = ? ORDER BY `PunishmentId` DESC", user); + try { + Set punishments = new HashSet<>(); + while (set.next()) { + punishments.add(new Punishment(set)); + } + return punishments; + } catch (SQLException throwable) { + throwable.printStackTrace(); + throw new SecurityException("Could not Load all Punishments", throwable); + } + } + + public static Punishment createPunishment(int user, int executor, PunishmentType type, String reason, Timestamp endTime, Boolean perma) { + SQL.update("INSERT INTO Punishments (UserId, Punisher, Type, Reason, EndTime, Perma) VALUES (?, ?, ?, ?, ?, ?)", + user, executor, type.toDb(), reason, endTime, perma); + return getPunishmentByPlayer(user, type); + } + + private final Timestamp startTime; + private final Timestamp endTime; + private final PunishmentType type; + private final int user; + private final String reason; + private final int id; + private final int punisher; + private final boolean perma; + + private Punishment(ResultSet set) throws SQLException { + id = set.getInt("PunishmentId"); + user = set.getInt("UserId"); + reason = set.getString("Reason"); + type = PunishmentType.getTypeFromDB(set.getString("Type")); + startTime = set.getTimestamp("StartTime"); + endTime = set.getTimestamp("EndTime"); + punisher = set.getInt("Punisher"); + perma = set.getBoolean("Perma"); + } + + public Timestamp getStartTime() { + return startTime; + } + + public Timestamp getEndTime() { + return endTime; + } + + public PunishmentType getType() { + return type; + } + + public int getUser() { + return user; + } + + public String getReason() { + return reason; + } + + public int getId() { + return id; + } + + public int getPunisher() { + return punisher; + } + + public boolean isPerma() { + return perma; + } + + public enum PunishmentType { + Ban, + Mute; + + public String toDb() { + return name().toLowerCase(); + } + + public static PunishmentType getTypeFromDB(String str) { + return Arrays.stream(values()).filter(punishmentType -> punishmentType.name().equalsIgnoreCase(str)).collect(Collectors.toList()).get(0); + } + } +} diff --git a/src/de/steamwar/bungeecore/sql/SteamwarUser.java b/src/de/steamwar/bungeecore/sql/SteamwarUser.java index 61625ea..82ace6b 100644 --- a/src/de/steamwar/bungeecore/sql/SteamwarUser.java +++ b/src/de/steamwar/bungeecore/sql/SteamwarUser.java @@ -32,7 +32,6 @@ import java.net.UnknownHostException; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; -import java.time.Instant; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -43,16 +42,12 @@ public class SteamwarUser { private final UUID uuid; private String userName; private UserGroup userGroup; - private Timestamp banTime; - private String banReason; private int team; - private Timestamp muteTime; - private String muteReason; + private Map punishments; private static final Map usersByName = new HashMap<>(); private static final Map usersByUUID = new HashMap<>(); private static final Map usersById = new HashMap<>(); - private static final Timestamp PERMA_BAN = Timestamp.from(Instant.ofEpochSecond(946684800)); private static final InetAddress LIXFEL_DE; static { @@ -69,14 +64,11 @@ public class SteamwarUser { uuid = UUID.fromString(rs.getString("UUID")); userName = rs.getString("UserName"); userGroup = UserGroup.getUsergroup(rs.getString("UserGroup")); - banTime = rs.getTimestamp("BanTime"); - banReason = rs.getString("BanReason"); team = rs.getInt("Team"); - muteTime = rs.getTimestamp("MuteTime"); - muteReason = rs.getString("MuteReason"); usersById.put(id, this); usersByName.put(userName.toLowerCase(), this); usersByUUID.put(uuid, this); + punishments = Punishment.getPunishmentsByPlayer(id); } public static SteamwarUser getOrCreate(PendingConnection connection){ @@ -157,47 +149,32 @@ public class SteamwarUser { } public boolean isBanned() { - if (banTime == null) { - return false; - } else if (banTime.after(new Date()) || banTime.before(PERMA_BAN)) { - return true; - } else { - SQL.update("UPDATE UserData SET BanTime = NULL, BanReason = '' WHERE id = ?", id); - SQL.update("DELETE FROM BannedUserIPs WHERE UserID = ?", id); - banTime = null; - banReason = ""; - return false; - } + return punishments.containsKey(Punishment.PunishmentType.Ban) && + (punishments.get(Punishment.PunishmentType.Ban).getEndTime().after(new Date()) || punishments.get(Punishment.PunishmentType.Ban).isPerma()); } public boolean isMuted(){ - if(muteTime == null){ - return false; - }else if(muteTime.after(new Date()) || muteTime.before(PERMA_BAN)){ - return true; - }else{ - SQL.update("UPDATE UserData SET MuteTime = NULL, MuteReason = '' WHERE id = ?", id); - muteTime = null; - muteReason = ""; - return false; - } + return punishments.containsKey(Punishment.PunishmentType.Mute) && + (punishments.get(Punishment.PunishmentType.Mute).getEndTime().after(new Date()) || punishments.get(Punishment.PunishmentType.Mute).isPerma()); } public TextComponent banMessage(){ - if (banTime.before(PERMA_BAN)) { - return BungeeCore.stringToText(BungeeCore.CHAT_PREFIX + "§cDu bist permanent gebannt. §r§lGrund§r: §c" + banReason); + Punishment punishment = punishments.get(Punishment.PunishmentType.Ban); + if (punishment.isPerma()) { + return BungeeCore.stringToText(BungeeCore.CHAT_PREFIX + "§cDu bist permanent gebannt. §r§lGrund§r: §c" + punishment.getReason()); } else { return BungeeCore.stringToText(BungeeCore.CHAT_PREFIX + " Du bist bis zum " + - banTime.toLocalDateTime().format(BungeeCore.DATE_FORMAT) + " gebannt. §r§lGrund§r: §c" + banReason); + punishment.getEndTime().toLocalDateTime().format(BungeeCore.DATE_FORMAT) + " gebannt. §r§lGrund§r: §c" + punishment.getReason()); } } public TextComponent muteMessage(){ - if (muteTime.before(PERMA_BAN)) { - return BungeeCore.stringToText(BungeeCore.CHAT_PREFIX + "§cDu bist permanent gemuted. §r§lGrund§r: §c" + muteReason); + Punishment punishment = punishments.get(Punishment.PunishmentType.Mute); + if (punishment.isPerma()) { + return BungeeCore.stringToText(BungeeCore.CHAT_PREFIX + "§cDu bist permanent gemuted. §r§lGrund§r: §c" + punishment.getReason()); } else { return BungeeCore.stringToText(BungeeCore.CHAT_PREFIX + " Du bist bis zum " + - muteTime.toLocalDateTime().format(BungeeCore.DATE_FORMAT) + " gemuted. §r§lGrund§r: §c" + muteReason); + punishment.getEndTime().toLocalDateTime().format(BungeeCore.DATE_FORMAT) + " gemuted. §r§lGrund§r: §c" + punishment.getReason()); } } @@ -205,10 +182,9 @@ public class SteamwarUser { BannedUserIPs.banIP(this, ip); } - public void ban(Timestamp time, String banReason){ - SQL.update("UPDATE UserData SET BanTime = ?, BanReason = ? WHERE id = ?", time, banReason, id); - banTime = time; - this.banReason = banReason; + public void ban(Timestamp time, String banReason, int from, boolean perma){ + punishments.remove(Punishment.PunishmentType.Ban); + punishments.put(Punishment.PunishmentType.Ban, Punishment.createPunishment(id, from, Punishment.PunishmentType.Ban, banReason, time, perma)); ProxiedPlayer player = ProxyServer.getInstance().getPlayer(uuid); if(player != null){ @@ -218,10 +194,9 @@ public class SteamwarUser { updateBanIP(""); } - public void mute(Timestamp time, String muteReason){ - SQL.update("UPDATE UserData SET MuteTime = ?, MuteReason = ? WHERE id = ?", time, muteReason, id); - muteTime = time; - this.muteReason = muteReason; + public void mute(Timestamp time, String muteReason, int from, boolean perma){ + punishments.remove(Punishment.PunishmentType.Mute); + punishments.put(Punishment.PunishmentType.Mute, Punishment.createPunishment(id, from, Punishment.PunishmentType.Mute, muteReason, time, perma)); } private static SteamwarUser dbInit(ResultSet rs){