From 6607b736ee0278de6bd233715ed8913ff42adc66 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 15 Nov 2020 11:02:23 +0100 Subject: [PATCH] Fixing... --- .../bungeecore/commands/BanCommand.java | 3 +- .../bungeecore/commands/MuteCommand.java | 3 +- .../bungeecore/commands/WhoisCommand.java | 4 +- .../steamwar/bungeecore/sql/Punishment.java | 48 ++++++------------- .../steamwar/bungeecore/sql/SteamwarUser.java | 16 +++++-- 5 files changed, 30 insertions(+), 44 deletions(-) diff --git a/src/de/steamwar/bungeecore/commands/BanCommand.java b/src/de/steamwar/bungeecore/commands/BanCommand.java index eef825b..ff4516b 100644 --- a/src/de/steamwar/bungeecore/commands/BanCommand.java +++ b/src/de/steamwar/bungeecore/commands/BanCommand.java @@ -55,9 +55,8 @@ public class BanCommand extends BasicCommand { banReason.append(args[i]).append(" "); } String msg = banReason.toString(); - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "Du hast " + target.getUserName() + " gebannt. Grund: §c" + msg); - ServerTeamchatCommand.sendToTeam(BungeeCore.CHAT_PREFIX + "§c" + target.getUserName() + " wurde von " + sender.getName() + " " + (args[1].equalsIgnoreCase("perma")?"Permanent":"bis zum " + banTime.toLocalDateTime().format(BungeeCore.DATE_FORMAT)) + " gebannt. §f§lGrund: §f" + banReason); target.ban(banTime, msg, SteamwarUser.get(sender.getName()).getId(), args[1].equalsIgnoreCase("perma")); + ServerTeamchatCommand.sendToTeam(BungeeCore.CHAT_PREFIX + "§c" + target.getUserName() + " wurde von " + sender.getName() + " " + (args[1].equalsIgnoreCase("perma")?"Permanent":"bis zum " + banTime.toLocalDateTime().format(BungeeCore.DATE_FORMAT)) + " gebannt. §f§lGrund: §f" + banReason); } public static Timestamp parseTime(CommandSender sender, String arg){ diff --git a/src/de/steamwar/bungeecore/commands/MuteCommand.java b/src/de/steamwar/bungeecore/commands/MuteCommand.java index 7e9b1ea..edd429a 100644 --- a/src/de/steamwar/bungeecore/commands/MuteCommand.java +++ b/src/de/steamwar/bungeecore/commands/MuteCommand.java @@ -51,8 +51,7 @@ public class MuteCommand extends BasicCommand { muteReason.append(args[i]).append(" "); } String msg = muteReason.toString(); - ServerTeamchatCommand.sendToTeam(BungeeCore.CHAT_PREFIX + "§c" + target.getUserName() + " wurde von " + sender.getName() + " " + (args[1].equalsIgnoreCase("perma")?"Permanent":"bis zum " + muteTime.toLocalDateTime().format(BungeeCore.DATE_FORMAT)) + " gemuted. §f§lGrund: §f" + muteReason); - BungeeCore.send(sender, BungeeCore.CHAT_PREFIX + "Du hast " + target.getUserName() + " gemuted. Grund: §c" + msg); target.mute(muteTime, msg, SteamwarUser.get(sender.getName()).getId(), args[1].equalsIgnoreCase("perma")); + ServerTeamchatCommand.sendToTeam(BungeeCore.CHAT_PREFIX + "§c" + target.getUserName() + " wurde von " + sender.getName() + " " + (args[1].equalsIgnoreCase("perma")?"Permanent":"bis zum " + muteTime.toLocalDateTime().format(BungeeCore.DATE_FORMAT)) + " gemuted. §f§lGrund: §f" + muteReason); } } diff --git a/src/de/steamwar/bungeecore/commands/WhoisCommand.java b/src/de/steamwar/bungeecore/commands/WhoisCommand.java index cc66c1a..d2e2f4b 100644 --- a/src/de/steamwar/bungeecore/commands/WhoisCommand.java +++ b/src/de/steamwar/bungeecore/commands/WhoisCommand.java @@ -71,8 +71,8 @@ public class WhoisCommand extends BasicCommand { Team team = Team.get(user.getTeam()); BungeeCore.send(player, "§7Team§8: §e" + team.getTeamName()); - BungeeCore.send(player, "§7Bestrafungen: "); - for (Punishment punishment : Punishment.getAllPunishmentsByPlayer(user.getId())) { + BungeeCore.send(player, "§7Strafen: "); + for (Punishment punishment : Punishment.getAllPunishmentsOfPlayer(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/sql/Punishment.java b/src/de/steamwar/bungeecore/sql/Punishment.java index ec787a7..87d50dd 100644 --- a/src/de/steamwar/bungeecore/sql/Punishment.java +++ b/src/de/steamwar/bungeecore/sql/Punishment.java @@ -23,24 +23,22 @@ 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()); + public static Punishment getPunishmentOfPlayer(int user, PunishmentType type) { + ResultSet set = SQL.select("SELECT * FROM `Punishments` WHERE EndTime < NOW() AND UserId = ? AND Type = ? ORDER BY PunishmentId DESC LIMIT 1", user, type.name()); try { if(!set.next()) return null; return new Punishment(set); - } catch (SQLException throwable) { - throwable.printStackTrace(); - throw new SecurityException("Could not Load Punishments", throwable); + } catch (SQLException e) { + throw new SecurityException("Could not Load Punishments", e); } } - 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); + public static Map getPunishmentsOfPlayer(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()) { @@ -48,30 +46,28 @@ public class Punishment { punishmentMap.put(punishment.getType(), punishment); } return punishmentMap; - } catch (SQLException throwable) { - throwable.printStackTrace(); - throw new SecurityException("Could not Load Punishments", throwable); + } catch (SQLException e) { + throw new SecurityException("Could not Load Punishments", e); } } - public static Set getAllPunishmentsByPlayer(int user) { + public static List getAllPunishmentsOfPlayer(int user) { ResultSet set = SQL.select("SELECT * FROM Punishments WHERE UserId = ? ORDER BY `PunishmentId` DESC", user); try { - Set punishments = new HashSet<>(); + List punishments = new ArrayList<>(); while (set.next()) { punishments.add(new Punishment(set)); } return punishments; - } catch (SQLException throwable) { - throwable.printStackTrace(); - throw new SecurityException("Could not Load all Punishments", throwable); + } catch (SQLException e) { + throw new SecurityException("Could not Load all Punishments", e); } } 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); + user, executor, type.name(), reason, endTime, perma); + return getPunishmentOfPlayer(user, type); } private final Timestamp startTime; @@ -79,15 +75,13 @@ public class Punishment { 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")); + type = PunishmentType.valueOf(set.getString("Type")); startTime = set.getTimestamp("StartTime"); endTime = set.getTimestamp("EndTime"); punisher = set.getInt("Punisher"); @@ -114,10 +108,6 @@ public class Punishment { return reason; } - public int getId() { - return id; - } - public int getPunisher() { return punisher; } @@ -129,13 +119,5 @@ public class Punishment { 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 82ace6b..d092b9e 100644 --- a/src/de/steamwar/bungeecore/sql/SteamwarUser.java +++ b/src/de/steamwar/bungeecore/sql/SteamwarUser.java @@ -68,7 +68,7 @@ public class SteamwarUser { usersById.put(id, this); usersByName.put(userName.toLowerCase(), this); usersByUUID.put(uuid, this); - punishments = Punishment.getPunishmentsByPlayer(id); + punishments = Punishment.getPunishmentsOfPlayer(id); } public static SteamwarUser getOrCreate(PendingConnection connection){ @@ -149,13 +149,19 @@ public class SteamwarUser { } public boolean isBanned() { - return punishments.containsKey(Punishment.PunishmentType.Ban) && - (punishments.get(Punishment.PunishmentType.Ban).getEndTime().after(new Date()) || punishments.get(Punishment.PunishmentType.Ban).isPerma()); + if(!punishments.containsKey(Punishment.PunishmentType.Ban)) + return false; + return isCurrent(punishments.get(Punishment.PunishmentType.Ban)); } public boolean isMuted(){ - return punishments.containsKey(Punishment.PunishmentType.Mute) && - (punishments.get(Punishment.PunishmentType.Mute).getEndTime().after(new Date()) || punishments.get(Punishment.PunishmentType.Mute).isPerma()); + if(!punishments.containsKey(Punishment.PunishmentType.Mute)) + return false; + return isCurrent(punishments.get(Punishment.PunishmentType.Mute)); + } + + private boolean isCurrent(Punishment punishment) { + return punishment.isPerma() || punishment.getEndTime().after(new Date()); } public TextComponent banMessage(){