Fix a whole bunch of bugs

- Rewrite how bans are handled
- Add /unfreeze command
- Rename LoginListener to BanListener
- Persistent commandspy
This commit is contained in:
Telesphoreo 2022-02-06 23:53:57 -06:00
parent fddf57d7f5
commit 2631867e27
27 changed files with 84 additions and 120 deletions

View File

@ -4,9 +4,7 @@ import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
import dev.morphia.annotations.IndexOptions;
import dev.morphia.annotations.Indexed;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;
import lombok.AccessLevel;
import lombok.Getter;

View File

@ -6,7 +6,6 @@ import dev.morphia.query.experimental.filters.Filters;
import dev.morphia.query.experimental.updates.UpdateOperators;
import dev.plex.Plex;
import dev.plex.storage.StorageType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -14,7 +13,6 @@ import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.List;
import java.util.UUID;

View File

@ -75,7 +75,8 @@ public class MongoPlayerData
UpdateOperators.set("rank", player.getRank().toLowerCase()),
UpdateOperators.set("ips", player.getIps()),
UpdateOperators.set("coins", player.getCoins()),
UpdateOperators.set("vanished", player.isVanished()));
UpdateOperators.set("vanished", player.isVanished()),
UpdateOperators.set("commandspy", player.isCommandSpy()));
updateOps.execute();
}
@ -90,5 +91,4 @@ public class MongoPlayerData
{
datastore.save(plexPlayer);
}
}

View File

@ -17,8 +17,8 @@ import java.util.UUID;
public class SQLPlayerData
{
private final String SELECT = "SELECT * FROM `players` WHERE uuid=?";
private final String UPDATE = "UPDATE `players` SET name=?, login_msg=?, prefix=?, rank=?, ips=?, coins=?, vanished=? WHERE uuid=?";
private final String INSERT = "INSERT INTO `players` (`uuid`, `name`, `login_msg`, `prefix`, `rank`, `ips`, `coins`, `vanished`) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
private final String UPDATE = "UPDATE `players` SET name=?, login_msg=?, prefix=?, rank=?, ips=?, coins=?, vanished=?, commandspy=? WHERE uuid=?";
private final String INSERT = "INSERT INTO `players` (`uuid`, `name`, `login_msg`, `prefix`, `rank`, `ips`, `coins`, `vanished`, `commandspy`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
/**
* Checks if a player exists in the SQL database
@ -68,6 +68,7 @@ public class SQLPlayerData
String rankName = set.getString("rank").toUpperCase();
long coins = set.getLong("coins");
boolean vanished = set.getBoolean("vanished");
boolean commandspy = set.getBoolean("commandspy");
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>()
{
}.getType());
@ -78,6 +79,7 @@ public class SQLPlayerData
plexPlayer.setIps(ips);
plexPlayer.setCoins(coins);
plexPlayer.setVanished(vanished);
plexPlayer.setCommandSpy(commandspy);
}
return plexPlayer;
}
@ -105,7 +107,8 @@ public class SQLPlayerData
statement.setString(5, new Gson().toJson(player.getIps()));
statement.setLong(6, player.getCoins());
statement.setBoolean(7, player.isVanished());
statement.setString(8, player.getUuid());
statement.setBoolean(8, player.isCommandSpy());
statement.setString(9, player.getUuid());
statement.executeUpdate();
}
catch (SQLException throwables)
@ -132,6 +135,7 @@ public class SQLPlayerData
statement.setString(6, new Gson().toJson(player.getIps()));
statement.setLong(7, player.getCoins());
statement.setBoolean(8, player.isVanished());
statement.setBoolean(9, player.isCommandSpy());
statement.execute();
}
catch (SQLException throwables)
@ -139,5 +143,4 @@ public class SQLPlayerData
throwables.printStackTrace();
}
}
}

View File

@ -5,27 +5,33 @@ import dev.plex.cache.DataUtils;
import dev.plex.cache.PlayerCache;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.*;
import dev.plex.command.exception.CommandFailException;
import dev.plex.command.exception.ConsoleMustDefinePlayerException;
import dev.plex.command.exception.ConsoleOnlyException;
import dev.plex.command.exception.PlayerNotBannedException;
import dev.plex.command.exception.PlayerNotFoundException;
import dev.plex.command.source.RequiredCommandSource;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import java.util.Arrays;
import java.util.UUID;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.UUID;
/**
* Superclass for all commands
*/

View File

@ -15,14 +15,11 @@ import dev.plex.punishment.PunishmentType;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

View File

@ -1,5 +1,6 @@
package dev.plex.command.impl;
import dev.plex.cache.DataUtils;
import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
@ -20,11 +21,15 @@ public class CommandSpyCMD extends PlexCommand
@Override
protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args)
{
PlexPlayer plexPlayer = getPlexPlayer(playerSender);
if (playerSender != null)
{
PlexPlayer plexPlayer = DataUtils.getPlayer(playerSender.getUniqueId());
plexPlayer.setCommandSpy(!plexPlayer.isCommandSpy());
Component component = Component.text("CommandSpy has been").color(NamedTextColor.GRAY)
DataUtils.update(plexPlayer);
return Component.text("CommandSpy has been").color(NamedTextColor.GRAY)
.append(Component.space())
.append(Component.text(plexPlayer.isCommandSpy() ? "enabled." : "disabled.").color(NamedTextColor.GRAY));
return component;
}
return null;
}
}

View File

@ -10,7 +10,6 @@ import dev.plex.punishment.Punishment;
import dev.plex.punishment.PunishmentType;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
@ -20,7 +19,7 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@CommandParameters(name = "freeze", description = "Freeze a player on the server", usage = "/<command> <player>")
@CommandParameters(name = "freeze", description = "Freeze a player on the server", usage = "/<command> <player>", aliases = "fr")
@CommandPermissions(level = Rank.ADMIN, permission = "plex.freeze")
public class FreezeCMD extends PlexCommand
{

View File

@ -10,17 +10,12 @@ import dev.plex.util.AshconInfo;
import dev.plex.util.MojangUtils;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -30,7 +25,7 @@ import org.jetbrains.annotations.Nullable;
@CommandPermissions(level = Rank.OP, permission = "plex.namehistory")
public class NameHistoryCMD extends PlexCommand
{
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' HH:mm:ss");
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm:ss a");
@Override
protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args)
@ -44,7 +39,7 @@ public class NameHistoryCMD extends PlexCommand
AshconInfo info = MojangUtils.getInfo(username);
if (info == null)
{
return Component.text("Couldn't find this user! Please check if your spelling was correct and this player exists").color(NamedTextColor.RED);
return tl("nameHistoryDoesntExist");
}
PlexLog.debug("NameHistory UUID: " + info.getUuid());
PlexLog.debug("NameHistory Size: " + info.getUsernameHistories().length);

View File

@ -4,7 +4,6 @@ import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.source.RequiredCommandSource;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;

View File

@ -9,14 +9,13 @@ import dev.plex.command.exception.CommandFailException;
import dev.plex.player.PunishedPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import java.util.List;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@CommandPermissions(level = Rank.ADMIN, permission = "plex.unfreeze")
@CommandParameters(name = "unfreeze", description = "Unfreeze a player", usage = "/<command> <player>")
public class UnfreezeCMD extends PlexCommand

View File

@ -37,6 +37,7 @@ public class CommandHandler extends PlexBase
commands.add(new SpectatorCMD());
commands.add(new SurvivalCMD());
commands.add(new UnbanCMD());
commands.add(new UnfreezeCMD());
commands.add(new WorldCMD());
PlexLog.log(String.format("Registered %s commands!", commands.size()));
}

View File

@ -3,10 +3,10 @@ package dev.plex.handlers;
import com.google.common.collect.Lists;
import dev.plex.listener.PlexListener;
import dev.plex.listener.impl.AdminListener;
import dev.plex.listener.impl.BanListener;
import dev.plex.listener.impl.ChatListener;
import dev.plex.listener.impl.CommandListener;
import dev.plex.listener.impl.FreezeListener;
import dev.plex.listener.impl.LoginListener;
import dev.plex.listener.impl.PlayerListener;
import dev.plex.listener.impl.ServerListener;
import dev.plex.listener.impl.WorldListener;
@ -25,7 +25,7 @@ public class ListenerHandler
listeners.add(new WorldListener());
listeners.add(new FreezeListener());
listeners.add(new AdminListener());
listeners.add(new LoginListener());
listeners.add(new BanListener());
PlexLog.log(String.format("Registered %s listeners!", listeners.size()));
}
}

View File

@ -0,0 +1,33 @@
package dev.plex.listener.impl;
import dev.plex.banning.Ban;
import dev.plex.cache.DataUtils;
import dev.plex.listener.PlexListener;
import dev.plex.player.PlexPlayer;
import dev.plex.util.PlexUtils;
import java.time.format.DateTimeFormatter;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
public class BanListener extends PlexListener
{
private final String banUrl = plugin.config.getString("banning.ban_url");
private final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm:ss a");
@EventHandler
public void onPreLogin(AsyncPlayerPreLoginEvent event)
{
if (plugin.getBanManager().isBanned(event.getUniqueId()))
{
for (Ban ban : plugin.getBanManager().getActiveBans())
{
PlexPlayer player = DataUtils.getPlayer(ban.getBanner());
String banMessage = PlexUtils.tl("banMessage", banUrl, ban.getReason(),
DATE_FORMAT.format(ban.getEndDate()), ban.getBanner() == null ? "CONSOLE" : player.getName());
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED,
LegacyComponentSerializer.legacyAmpersand().deserialize(banMessage));
}
}
}
}

View File

@ -8,7 +8,6 @@ import io.papermc.paper.chat.ChatRenderer;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.entity.Player;

View File

@ -1,11 +1,9 @@
package dev.plex.listener.impl;
import dev.plex.cache.DataUtils;
import dev.plex.cache.PlayerCache;
import dev.plex.listener.PlexListener;
import dev.plex.player.PunishedPlayer;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
public class FreezeListener extends PlexListener

View File

@ -1,44 +0,0 @@
package dev.plex.listener.impl;
import dev.plex.cache.DataUtils;
import dev.plex.listener.PlexListener;
import dev.plex.player.PlexPlayer;
import dev.plex.player.PunishedPlayer;
import dev.plex.punishment.Punishment;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
public class LoginListener extends PlexListener
{
private final String banUrl = plugin.config.getString("banning.ban_url");
@EventHandler
public void onPreLogin(AsyncPlayerPreLoginEvent event)
{
if (plugin.getBanManager().isBanned(event.getUniqueId()))
{
PunishedPlayer punishedPlayer = new PunishedPlayer(event.getUniqueId());
Punishment punishment = punishedPlayer.getPunishments().get(punishedPlayer.getPunishments().size() - 1);
String banMessage;
if (punishment.getPunisher() == null)
{
banMessage = PlexUtils.tl("banMessage", banUrl, punishment.getReason(),
punishment.getEndDate(), "CONSOLE");
}
else
{
PlexPlayer player = DataUtils.getPlayer(punishment.getPunisher());
banMessage = PlexUtils.tl("banMessage", banUrl, punishment.getReason(),
punishment.getEndDate(), player.getName());
}
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED,
LegacyComponentSerializer.legacyAmpersand().deserialize(banMessage));
}
}
}

View File

@ -8,7 +8,6 @@ import dev.plex.cache.SQLPlayerData;
import dev.plex.listener.PlexListener;
import dev.plex.player.PlexPlayer;
import dev.plex.player.PunishedPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexLog;
import java.util.Collections;
import java.util.UUID;

View File

@ -4,12 +4,11 @@ import com.google.common.collect.Lists;
import com.google.gson.GsonBuilder;
import dev.plex.util.adapter.LocalDateTimeDeserializer;
import dev.plex.util.adapter.LocalDateTimeSerializer;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter

View File

@ -14,14 +14,10 @@ import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;

View File

@ -3,9 +3,7 @@ package dev.plex.services.impl;
import dev.plex.Plex;
import dev.plex.banning.Ban;
import dev.plex.services.AbstractService;
import java.time.LocalDateTime;
import java.util.Date;
import org.bukkit.Bukkit;
public class BanService extends AbstractService

View File

@ -50,7 +50,7 @@ public class SQLConnection extends PlexBase
"`ips` VARCHAR(2000), " +
"`coins` BIGINT, " +
"`vanished` BOOLEAN, " +
"`commandSpy` BOOLEAN, " +
"`commandspy` BOOLEAN, " +
"PRIMARY KEY (`uuid`));").execute();
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `bans` (" +
"`banID` VARCHAR(46), " +

View File

@ -1,14 +1,11 @@
package dev.plex.util;
import com.google.common.collect.Lists;
import com.google.gson.annotations.SerializedName;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.time.LocalDateTime;
import java.util.List;
@Getter
@Setter
public class AshconInfo

View File

@ -1,29 +1,19 @@
package dev.plex.util;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Maps;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.*;
import java.util.stream.Collectors;
import com.google.common.collect.Multimap;
import com.google.gson.*;
import dev.plex.util.adapter.LocalDateTimeDeserializer;
import dev.plex.util.adapter.LocalDateTimeSerializer;
import java.util.Arrays;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
public class MojangUtils

View File

@ -1,12 +1,13 @@
package dev.plex.util.adapter;
import com.google.gson.*;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeDeserializer implements JsonDeserializer<LocalDateTime>
{

View File

@ -4,13 +4,10 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeSerializer implements JsonSerializer<LocalDateTime>
{

View File

@ -58,6 +58,7 @@ nameHistoryTitle: "Name History of <v>"
nameHistoryBody: " - <v> (<v>)"
# 1: the username that failed
nameHistoryFail: "<e>Something went wrong while trying to retrieve name history of <v>! Try again later!"
nameHistoryDoesntExist: "<e>Couldn't find this user! Please check if your spelling was correct and this player exists"
gameModeSetTo: "Your gamemode has been set to <v>."
setOtherPlayerGameModeTo: "You set <v>'s gamemode to <v>."
playerSetOtherGameMode: "<v> set your gamemode to <v>."