fix mute & freeze not staying after relog

add configurable timers for mute and freeze
This commit is contained in:
Taah 2024-03-23 08:14:36 -07:00
parent 54015f668a
commit 85605774cf
11 changed files with 117 additions and 41 deletions

View File

@ -41,11 +41,12 @@ public class FreezeCMD extends PlexCommand
Punishment punishment = new Punishment(punishedPlayer.getUuid(), getUUID(sender)); Punishment punishment = new Punishment(punishedPlayer.getUuid(), getUUID(sender));
punishment.setCustomTime(false); punishment.setCustomTime(false);
ZonedDateTime date = ZonedDateTime.now(ZoneId.of(TimeUtils.TIMEZONE)); ZonedDateTime date = ZonedDateTime.now(ZoneId.of(TimeUtils.TIMEZONE));
punishment.setEndDate(date.plusMinutes(5)); punishment.setEndDate(date.plusSeconds(plugin.config.getInt("punishments.freeze-timer", 300)));
punishment.setType(PunishmentType.FREEZE); punishment.setType(PunishmentType.FREEZE);
punishment.setPunishedUsername(player.getName()); punishment.setPunishedUsername(player.getName());
punishment.setIp(player.getAddress().getAddress().getHostAddress().trim()); punishment.setIp(player.getAddress().getAddress().getHostAddress().trim());
punishment.setReason(""); punishment.setReason("");
punishment.setActive(true);
plugin.getPunishmentManager().punish(punishedPlayer, punishment); plugin.getPunishmentManager().punish(punishedPlayer, punishment);
PlexUtils.broadcast(messageComponent("frozePlayer", sender.getName(), player.getName())); PlexUtils.broadcast(messageComponent("frozePlayer", sender.getName(), player.getName()));

View File

@ -47,11 +47,12 @@ public class MuteCMD extends PlexCommand
Punishment punishment = new Punishment(punishedPlayer.getUuid(), getUUID(sender)); Punishment punishment = new Punishment(punishedPlayer.getUuid(), getUUID(sender));
punishment.setCustomTime(false); punishment.setCustomTime(false);
ZonedDateTime date = ZonedDateTime.now(ZoneId.of(TimeUtils.TIMEZONE)); ZonedDateTime date = ZonedDateTime.now(ZoneId.of(TimeUtils.TIMEZONE));
punishment.setEndDate(date.plusMinutes(5)); punishment.setEndDate(date.plusSeconds(plugin.config.getInt("punishments.mute-timer", 300)));
punishment.setType(PunishmentType.MUTE); punishment.setType(PunishmentType.MUTE);
punishment.setPunishedUsername(player.getName()); punishment.setPunishedUsername(player.getName());
punishment.setIp(player.getAddress().getAddress().getHostAddress().trim()); punishment.setIp(player.getAddress().getAddress().getHostAddress().trim());
punishment.setReason(""); punishment.setReason("");
punishment.setActive(true);
plugin.getPunishmentManager().punish(punishedPlayer, punishment); plugin.getPunishmentManager().punish(punishedPlayer, punishment);
PlexUtils.broadcast(messageComponent("mutedPlayer", sender.getName(), player.getName())); PlexUtils.broadcast(messageComponent("mutedPlayer", sender.getName(), player.getName()));

View File

@ -1,11 +1,14 @@
package dev.plex.command.impl; package dev.plex.command.impl;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import dev.plex.cache.DataUtils;
import dev.plex.command.PlexCommand; import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters; import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions; import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.CommandFailException; import dev.plex.command.exception.CommandFailException;
import dev.plex.command.exception.PlayerNotFoundException;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import dev.plex.punishment.PunishmentType;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -26,14 +29,22 @@ public class UnfreezeCMD extends PlexCommand
{ {
return usage(); return usage();
} }
Player player = getNonNullPlayer(args[0]); PlexPlayer punishedPlayer = DataUtils.getPlayer(args[0]);
PlexPlayer punishedPlayer = getOfflinePlexPlayer(player.getUniqueId()); if (punishedPlayer == null)
{
throw new PlayerNotFoundException();
}
if (!punishedPlayer.isFrozen()) if (!punishedPlayer.isFrozen())
{ {
throw new CommandFailException(PlexUtils.messageString("playerNotFrozen")); throw new CommandFailException(PlexUtils.messageString("playerNotFrozen"));
} }
punishedPlayer.setFrozen(false); punishedPlayer.setFrozen(false);
PlexUtils.broadcast(messageComponent("unfrozePlayer", sender.getName(), player.getName())); punishedPlayer.getPunishments().stream().filter(punishment -> punishment.getType() == PunishmentType.FREEZE && punishment.isActive()).forEach(punishment -> {
punishment.setActive(false);
plugin.getSqlPunishment().updatePunishment(punishment.getType(), false, punishment.getPunished());
});
PlexUtils.broadcast(messageComponent("unfrozePlayer", sender.getName(), punishedPlayer.getName()));
return null; return null;
} }

View File

@ -1,11 +1,14 @@
package dev.plex.command.impl; package dev.plex.command.impl;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import dev.plex.cache.DataUtils;
import dev.plex.command.PlexCommand; import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters; import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions; import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.CommandFailException; import dev.plex.command.exception.CommandFailException;
import dev.plex.command.exception.PlayerNotFoundException;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import dev.plex.punishment.PunishmentType;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -26,14 +29,22 @@ public class UnmuteCMD extends PlexCommand
{ {
return usage(); return usage();
} }
Player player = getNonNullPlayer(args[0]); PlexPlayer punishedPlayer = DataUtils.getPlayer(args[0]);
PlexPlayer punishedPlayer = getOfflinePlexPlayer(player.getUniqueId()); if (punishedPlayer == null)
{
throw new PlayerNotFoundException();
}
if (!punishedPlayer.isMuted()) if (!punishedPlayer.isMuted())
{ {
throw new CommandFailException(PlexUtils.messageString("playerNotMuted")); throw new CommandFailException(PlexUtils.messageString("playerNotMuted"));
} }
punishedPlayer.setMuted(false); punishedPlayer.setMuted(false);
PlexUtils.broadcast(messageComponent("unmutedPlayer", sender.getName(), player.getName())); punishedPlayer.getPunishments().stream().filter(punishment -> punishment.getType() == PunishmentType.MUTE && punishment.isActive()).forEach(punishment -> {
punishment.setActive(false);
plugin.getSqlPunishment().updatePunishment(punishment.getType(), false, punishment.getPunished());
});
PlexUtils.broadcast(messageComponent("unmutedPlayer", sender.getName(), punishedPlayer.getName()));
return null; return null;
} }

View File

@ -26,7 +26,7 @@ public class PunishedPlayerMenu extends PageableMenu<Punishment>
@Override @Override
protected ItemStack toItem(Punishment object) protected ItemStack toItem(Punishment object)
{ {
return new ItemBuilder(Material.PAPER).displayName("<!italic><red>" + object.getType().name()).lore("<!italic><red>By: <gold>" + (object.getPunisher() == null ? "CONSOLE" : Plex.get().getSqlPlayerData().getNameByUUID(object.getPunished())), "<!italic><red>Expire(d/s): <gold>" + TimeUtils.useTimezone(object.getEndDate()), "<!italic><red>Reason: <gold>" + object.getReason()).build(); return new ItemBuilder(Material.PAPER).displayName("<!italic><red>" + object.getType().name()).lore("<!italic><red>By: <gold>" + (object.getPunisher() == null ? "CONSOLE" : Plex.get().getSqlPlayerData().getNameByUUID(object.getPunisher())), "<!italic><red>Expire(d/s): <gold>" + TimeUtils.useTimezone(object.getEndDate()), "<!italic><red>Reason: <gold>" + object.getReason()).build();
} }
@Override @Override

View File

@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.punishment.Punishment; import dev.plex.punishment.Punishment;
import dev.plex.punishment.PunishmentType;
import dev.plex.punishment.extra.Note; import dev.plex.punishment.extra.Note;
import dev.plex.storage.annotation.MapObjectList; import dev.plex.storage.annotation.MapObjectList;
import dev.plex.storage.annotation.PrimaryKey; import dev.plex.storage.annotation.PrimaryKey;
@ -79,6 +80,7 @@ public class PlexPlayer
if (loadPunishments) if (loadPunishments)
{ {
this.loadPunishments(); this.loadPunishments();
this.checkMutesAndFreeze();
// this.permissions.addAll(Plex.get().getSqlPermissions().getPermissions(this.uuid)); // this.permissions.addAll(Plex.get().getSqlPermissions().getPermissions(this.uuid));
} }
} }
@ -93,6 +95,12 @@ public class PlexPlayer
return PlainTextComponentSerializer.plainText().serialize(getPlayer().displayName()); return PlainTextComponentSerializer.plainText().serialize(getPlayer().displayName());
} }
public void checkMutesAndFreeze() {
final ZonedDateTime now = ZonedDateTime.now();
this.muted = this.punishments.stream().filter(punishment -> punishment.getType() == PunishmentType.MUTE).anyMatch(punishment -> punishment.isActive() && now.isBefore(punishment.getEndDate()));
this.frozen = this.punishments.stream().filter(punishment -> punishment.getType() == PunishmentType.FREEZE).anyMatch(punishment -> punishment.isActive() && now.isBefore(punishment.getEndDate()));
}
public void loadPunishments() public void loadPunishments()
{ {
this.setPunishments(Plex.get().getSqlPunishment().getPunishments(this.getUuid())); this.setPunishments(Plex.get().getSqlPunishment().getPunishments(this.getUuid()));

View File

@ -147,7 +147,6 @@ public class PunishmentManager implements PlexBase
public Punishment getBanByIP(String ip) public Punishment getBanByIP(String ip)
{ {
final Gson gson = new GsonBuilder().registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeAdapter()).setPrettyPrinting().create();
return plugin.getSqlPunishment().getPunishments(ip).stream().filter(punishment -> punishment.getType() == PunishmentType.TEMPBAN || punishment.getType() == PunishmentType.BAN).filter(Punishment::isActive).filter(punishment -> punishment.getIp().equals(ip)).findFirst().orElse(null); return plugin.getSqlPunishment().getPunishments(ip).stream().filter(punishment -> punishment.getType() == PunishmentType.TEMPBAN || punishment.getType() == PunishmentType.BAN).filter(Punishment::isActive).filter(punishment -> punishment.getIp().equals(ip)).findFirst().orElse(null);
} }
@ -180,6 +179,11 @@ public class PunishmentManager implements PlexBase
return Plex.get().getSqlPunishment().removeBan(uuid); return Plex.get().getSqlPunishment().removeBan(uuid);
} }
public void updateOutdatedPunishments(PlexPlayer player)
{
}
private void doPunishment(PlexPlayer player, Punishment punishment) private void doPunishment(PlexPlayer player, Punishment punishment)
{ {
if (punishment.getType() == PunishmentType.FREEZE) if (punishment.getType() == PunishmentType.FREEZE)
@ -193,13 +197,18 @@ public class PunishmentManager implements PlexBase
@Override @Override
public void run() public void run()
{ {
if (!player.isFrozen()) PlexPlayer afterPlayer = DataUtils.getPlayer(player.getUuid());
if (!afterPlayer.isFrozen())
{ {
this.cancel(); this.cancel();
return; return;
} }
player.setFrozen(false); afterPlayer.setFrozen(false);
Bukkit.broadcast(PlexUtils.messageComponent("unfrozePlayer", "Plex", Bukkit.getOfflinePlayer(player.getUuid()).getName())); punishment.setActive(false);
plugin.getSqlPunishment().updatePunishment(punishment.getType(), false, punishment.getPunished());
DataUtils.update(afterPlayer);
Bukkit.broadcast(PlexUtils.messageComponent("unfrozePlayer", "Plex", Bukkit.getOfflinePlayer(afterPlayer.getUuid()).getName()));
} }
}.runTaskLater(Plex.get(), 20 * seconds); }.runTaskLater(Plex.get(), 20 * seconds);
} }
@ -214,13 +223,17 @@ public class PunishmentManager implements PlexBase
@Override @Override
public void run() public void run()
{ {
if (!player.isMuted()) PlexPlayer afterPlayer = DataUtils.getPlayer(player.getUuid());
if (!afterPlayer.isMuted())
{ {
this.cancel(); this.cancel();
return; return;
} }
player.setMuted(false); afterPlayer.setMuted(false);
Bukkit.broadcast(PlexUtils.messageComponent("unmutedPlayer", "Plex", Bukkit.getOfflinePlayer(player.getUuid()).getName())); punishment.setActive(false);
plugin.getSqlPunishment().updatePunishment(punishment.getType(), false, punishment.getPunished());
Bukkit.broadcast(PlexUtils.messageComponent("unmutedPlayer", "Plex", Bukkit.getOfflinePlayer(afterPlayer.getUuid()).getName()));
} }
}.runTaskLater(Plex.get(), 20 * seconds); }.runTaskLater(Plex.get(), 20 * seconds);
} }

View File

@ -25,7 +25,7 @@ public class SQLPunishment
private static final String SELECT_BY = "SELECT * FROM `punishments` WHERE punisher=?"; private static final String SELECT_BY = "SELECT * FROM `punishments` WHERE punisher=?";
private static final String INSERT = "INSERT INTO `punishments` (`punished`, `punisher`, `punishedUsername`, `ip`, `type`, `reason`, `customTime`, `active`, `endDate`) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"; private static final String INSERT = "INSERT INTO `punishments` (`punished`, `punisher`, `punishedUsername`, `ip`, `type`, `reason`, `customTime`, `active`, `endDate`) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
private static final String UPDATE_BAN = "UPDATE `punishments` SET active=? WHERE active=? AND punished=? AND type=?"; private static final String UPDATE_PUNISHMENT = "UPDATE `punishments` SET active=? WHERE punished=? AND type=?";
public CompletableFuture<List<Punishment>> getPunishments() public CompletableFuture<List<Punishment>> getPunishments()
{ {
@ -145,17 +145,16 @@ public class SQLPunishment
{ {
try (Connection con = Plex.get().getSqlConnection().getCon()) try (Connection con = Plex.get().getSqlConnection().getCon())
{ {
PreparedStatement statement = con.prepareStatement(UPDATE_BAN); PreparedStatement statement = con.prepareStatement(UPDATE_PUNISHMENT);
statement.setBoolean(1, false); statement.setBoolean(1, false);
statement.setBoolean(2, true); statement.setString(2, uuid.toString());
statement.setString(3, uuid.toString()); statement.setString(3, PunishmentType.BAN.name());
statement.setString(4, PunishmentType.BAN.name()); statement.executeUpdate();
PreparedStatement statement1 = con.prepareStatement(UPDATE_BAN); PreparedStatement statement1 = con.prepareStatement(UPDATE_PUNISHMENT);
statement1.setBoolean(1, false); statement1.setBoolean(1, false);
statement1.setBoolean(2, true); statement1.setString(2, uuid.toString());
statement1.setString(3, uuid.toString()); statement1.setString(3, PunishmentType.TEMPBAN.name());
statement1.setString(4, PunishmentType.TEMPBAN.name());
statement1.executeUpdate(); statement1.executeUpdate();
} }
catch (SQLException e) catch (SQLException e)
@ -164,24 +163,41 @@ public class SQLPunishment
} }
} }
public CompletableFuture<Void> updatePunishment(PunishmentType type, boolean active, UUID punished)
{
return CompletableFuture.runAsync(() ->
{
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(UPDATE_PUNISHMENT);
statement.setBoolean(1, active);
statement.setString(2, punished.toString());
statement.setString(3, type.name());
statement.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
});
}
public CompletableFuture<Void> removeBan(UUID uuid) public CompletableFuture<Void> removeBan(UUID uuid)
{ {
return CompletableFuture.runAsync(() -> return CompletableFuture.runAsync(() ->
{ {
try (Connection con = Plex.get().getSqlConnection().getCon()) try (Connection con = Plex.get().getSqlConnection().getCon())
{ {
PreparedStatement statement = con.prepareStatement(UPDATE_BAN); PreparedStatement statement = con.prepareStatement(UPDATE_PUNISHMENT);
statement.setBoolean(1, false); statement.setBoolean(1, false);
statement.setBoolean(2, true); statement.setString(2, uuid.toString());
statement.setString(3, uuid.toString()); statement.setString(3, PunishmentType.BAN.name());
statement.setString(4, PunishmentType.BAN.name());
statement.executeUpdate(); statement.executeUpdate();
PreparedStatement statement1 = con.prepareStatement(UPDATE_BAN); PreparedStatement statement1 = con.prepareStatement(UPDATE_PUNISHMENT);
statement1.setBoolean(1, false); statement1.setBoolean(1, false);
statement1.setBoolean(2, true); statement1.setString(2, uuid.toString());
statement1.setString(3, uuid.toString()); statement1.setString(3, PunishmentType.TEMPBAN.name());
statement1.setString(4, PunishmentType.TEMPBAN.name());
statement1.executeUpdate(); statement1.executeUpdate();
} }
catch (SQLException e) catch (SQLException e)
@ -190,4 +206,5 @@ public class SQLPunishment
} }
}); });
} }
} }

View File

@ -2,6 +2,7 @@ package dev.plex.util.sql;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import dev.plex.punishment.PunishmentType; import dev.plex.punishment.PunishmentType;
import dev.plex.storage.annotation.*; import dev.plex.storage.annotation.*;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
@ -13,10 +14,7 @@ import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.Arrays; import java.util.*;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -25,7 +23,7 @@ import java.util.stream.Collectors;
*/ */
public class SQLUtil public class SQLUtil
{ {
public static final List<Table> TABLES = Lists.newArrayList(); public static final Map<String, Table> TABLES = Maps.newHashMap();
public static List<String> createTable(List<String> result, Class<?> clazz) public static List<String> createTable(List<String> result, Class<?> clazz)
{ {
@ -83,7 +81,7 @@ public class SQLUtil
mainResult.append(");"); mainResult.append(");");
result.add(mainResult.toString()); result.add(mainResult.toString());
TABLES.add(table); TABLES.put(table.name(), table);
if (primaryKey == null && !collectionFields.isEmpty()) if (primaryKey == null && !collectionFields.isEmpty())
{ {
@ -113,11 +111,22 @@ public class SQLUtil
writeFieldToSQL(listTable, sql, Mapper.getByClass(finalPrimaryKey.getType()), finalPrimaryKey); writeFieldToSQL(listTable, sql, Mapper.getByClass(finalPrimaryKey.getType()), finalPrimaryKey);
sql.append(");"); sql.append(");");
result.add(sql.toString()); result.add(sql.toString());
table.mappedTables().add(listTable); table.mappedTables().put(field, listTable);
}); });
return result; return result;
} }
public static void update(String tableName, Object object)
{
final Table table = TABLES.get(tableName);
if (table == null)
{
PlexLog.error("Table {0} was not found", tableName);
return;
}
}
private static void writeFieldToSQL(Table table, StringBuilder sb, Mapper mapped, Field field) private static void writeFieldToSQL(Table table, StringBuilder sb, Mapper mapped, Field field)
{ {

View File

@ -5,6 +5,7 @@ import com.google.common.collect.Maps;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -19,5 +20,5 @@ public class Table
{ {
private final String name; private final String name;
private final Map<String, SQLUtil.Mapper> columns = Maps.newHashMap(); private final Map<String, SQLUtil.Mapper> columns = Maps.newHashMap();
private final List<Table> mappedTables = Lists.newArrayList(); private final Map<Field, Table> mappedTables = Maps.newHashMap();
} }

View File

@ -14,6 +14,10 @@ server:
banning: banning:
ban_url: "https://forum.plex.us.org" ban_url: "https://forum.plex.us.org"
punishments:
mute-timer: 300
freeze-timer: 300
chat: chat:
# Should the server use Plex's chat system? It is recommended to keep this on if you are using ranks. # Should the server use Plex's chat system? It is recommended to keep this on if you are using ranks.
# If you are using permissions, you should turn this off and use Vault to handle prefixes with a different chat plugin # If you are using permissions, you should turn this off and use Vault to handle prefixes with a different chat plugin