fix /notes
fix /rank
switch /admin to a datautils check instead of using ashcon api
This commit is contained in:
Taah 2022-04-10 15:02:50 -07:00
parent 81d3cc8dde
commit 8dfaaf7a8c
13 changed files with 88 additions and 39 deletions

View File

@ -29,6 +29,18 @@ public class DataUtils
} }
} }
public static boolean hasPlayedBefore(String username)
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return Plex.get().getMongoPlayerData().exists(username);
}
else
{
return Plex.get().getSqlPlayerData().exists(username);
}
}
/** /**
* Gets a player from cache or from the database * Gets a player from cache or from the database
* *

View File

@ -43,6 +43,14 @@ public class MongoPlayerData
return query.first() != null; return query.first() != null;
} }
public boolean exists(String username)
{
Query<PlexPlayer> query = datastore.find(PlexPlayer.class)
.filter(Filters.regex("name").caseInsensitive().pattern(username));
return query.first() != null;
}
/** /**
* Gets the player from cache or from mongo's database * Gets the player from cache or from mongo's database
* *

View File

@ -42,6 +42,22 @@ public class SQLPlayerData
return false; return false;
} }
public boolean exists(String username)
{
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement("SELECT * FROM `players` WHERE name=?");
statement.setString(1, username);
ResultSet set = statement.executeQuery();
return set.next();
}
catch (SQLException throwables)
{
throwables.printStackTrace();
}
return false;
}
/** /**
* Gets the player from cache or from the SQL database * Gets the player from cache or from the SQL database
* *

View File

@ -47,6 +47,7 @@ public class SQLNotes
catch (SQLException e) catch (SQLException e)
{ {
e.printStackTrace(); e.printStackTrace();
return notes;
} }
return notes; return notes;
}); });

View File

@ -50,6 +50,7 @@ public class SQLPunishment
catch (SQLException e) catch (SQLException e)
{ {
e.printStackTrace(); e.printStackTrace();
return punishments;
} }
return punishments; return punishments;
}); });

View File

@ -2,13 +2,14 @@ package dev.plex.command.blocking;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import lombok.Data; import lombok.Data;
import net.kyori.adventure.text.Component;
import java.util.List; import java.util.List;
@Data @Data
public class BlockedCommand public class BlockedCommand
{ {
private String message; private Component message;
private String requiredLevel; private String requiredLevel;
private String regex; private String regex;
private String command; private String command;

View File

@ -54,18 +54,18 @@ public class AdminCMD extends PlexCommand
throw new ConsoleOnlyException(); throw new ConsoleOnlyException();
} }
UUID targetUUID = PlexUtils.getFromName(args[1]); /*UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID != null) if (targetUUID != null)
{ {
PlexLog.debug("Admin Adding UUID: " + targetUUID); PlexLog.debug("Admin Adding UUID: " + targetUUID);
} }*/
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID)) if (!DataUtils.hasPlayedBefore(args[1]))
{ {
throw new PlayerNotFoundException(); throw new PlayerNotFoundException();
} }
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID); PlexPlayer plexPlayer = DataUtils.getPlayer(args[1]);
if (isAdmin(plexPlayer)) if (isAdmin(plexPlayer))
{ {
@ -87,13 +87,13 @@ public class AdminCMD extends PlexCommand
throw new ConsoleOnlyException(); throw new ConsoleOnlyException();
} }
UUID targetUUID = PlexUtils.getFromName(args[1]); // UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID)) if (!DataUtils.hasPlayedBefore(args[1]))
{ {
throw new PlayerNotFoundException(); throw new PlayerNotFoundException();
} }
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID); PlexPlayer plexPlayer = DataUtils.getPlayer(args[1]);
if (!isAdmin(plexPlayer)) if (!isAdmin(plexPlayer))
{ {
@ -116,9 +116,9 @@ public class AdminCMD extends PlexCommand
throw new ConsoleOnlyException(); throw new ConsoleOnlyException();
} }
UUID targetUUID = PlexUtils.getFromName(args[1]); // UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID)) if (!DataUtils.hasPlayedBefore(args[1]))
{ {
throw new PlayerNotFoundException(); throw new PlayerNotFoundException();
} }
@ -135,7 +135,7 @@ public class AdminCMD extends PlexCommand
return messageComponent("rankMustBeHigherThanAdmin"); return messageComponent("rankMustBeHigherThanAdmin");
} }
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID); PlexPlayer plexPlayer = DataUtils.getPlayer(args[1]);
if (!isAdmin(plexPlayer)) if (!isAdmin(plexPlayer))
{ {

View File

@ -94,6 +94,10 @@ public class NotesCMD extends PlexCommand
} }
case "remove": case "remove":
{ {
if (args.length < 3)
{
return usage();
}
int id; int id;
try try
{ {
@ -107,18 +111,20 @@ public class NotesCMD extends PlexCommand
{ {
plugin.getSqlNotes().getNotes(plexPlayer.getUuid()).whenComplete((notes, ex) -> plugin.getSqlNotes().getNotes(plexPlayer.getUuid()).whenComplete((notes, ex) ->
{ {
boolean deleted = false;
for (Note note : notes) for (Note note : notes)
{ {
if (note.getId() == id) if (note.getId() == id)
{ {
plugin.getSqlNotes().deleteNote(id, plexPlayer.getUuid()).whenComplete((notes1, ex1) -> plugin.getSqlNotes().deleteNote(id, plexPlayer.getUuid()).whenComplete((notes1, ex1) ->
send(sender, Component.text("Removed note with ID: " + id).color(NamedTextColor.GREEN))); send(sender, Component.text("Removed note with ID: " + id).color(NamedTextColor.GREEN)));
deleted = true;
} }
else }
if (!deleted)
{ {
send(sender, mmString("<red>A note with this ID could not be found")); send(sender, mmString("<red>A note with this ID could not be found"));
} }
}
plexPlayer.getNotes().removeIf(note -> note.getId() == id); plexPlayer.getNotes().removeIf(note -> note.getId() == id);
}); });
} }
@ -130,6 +136,7 @@ public class NotesCMD extends PlexCommand
} }
return mmString("<red>A note with this ID could not be found"); return mmString("<red>A note with this ID could not be found");
} }
return null;
} }
case "clear": case "clear":
{ {
@ -167,7 +174,7 @@ public class NotesCMD extends PlexCommand
for (Note note : notes) for (Note note : notes)
{ {
Component noteLine = Component.text(note.getId() + " - Written by: " + DataUtils.getPlayer(note.getWrittenBy()).getName() + " on " + DATE_FORMAT.format(note.getTimestamp())).color(NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, false); Component noteLine = Component.text(note.getId() + " - Written by: " + DataUtils.getPlayer(note.getWrittenBy()).getName() + " on " + DATE_FORMAT.format(note.getTimestamp())).color(NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, false);
noteLine = noteLine.append(Component.text(note.getNote())).color(NamedTextColor.GOLD).decoration(TextDecoration.ITALIC, true); noteLine = noteLine.append(Component.space()).append(Component.text(note.getNote())).color(NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, true);
noteList.set(noteList.get().append(Component.newline())); noteList.set(noteList.get().append(Component.newline()));
noteList.set(noteList.get().append(noteLine)); noteList.set(noteList.get().append(noteLine));
} }

View File

@ -33,14 +33,14 @@ public class RankCMD extends PlexCommand
if (!(playerSender == null)) if (!(playerSender == null))
{ {
Rank rank = getPlexPlayer(playerSender).getRankFromString(); Rank rank = getPlexPlayer(playerSender).getRankFromString();
return messageComponent("yourRank", rank.getReadable()); return messageComponent("yourRank", rank.isAtLeast(Rank.ADMIN) && !getPlexPlayer(playerSender).isAdminActive() ? (playerSender.isOp() ? Rank.OP.getReadable() : Rank.NONOP.getReadable()) : rank.getReadable());
} }
} }
else else
{ {
Player player = getNonNullPlayer(args[0]); Player player = getNonNullPlayer(args[0]);
Rank rank = getPlexPlayer(player).getRankFromString(); Rank rank = getPlexPlayer(player).getRankFromString();
return messageComponent("otherRank", player.getName(), rank.getReadable()); return messageComponent("otherRank", player.getName(), rank.isAtLeast(Rank.ADMIN) && !getPlexPlayer(player).isAdminActive() ? (player.isOp() ? Rank.OP.getReadable() : Rank.NONOP.getReadable()) : rank.getReadable());
} }
return null; return null;
} }

View File

@ -88,7 +88,7 @@ public class TagCMD extends PlexCommand
PlexPlayer plexTarget = DataUtils.getPlayer(target.getUniqueId()); PlexPlayer plexTarget = DataUtils.getPlayer(target.getUniqueId());
plexTarget.setPrefix(""); plexTarget.setPrefix("");
DataUtils.update(plexTarget); DataUtils.update(plexTarget);
messageComponent("otherPrefixCleared"); return messageComponent("otherPrefixCleared", target.getName());
} }
return usage(); return usage();
} }

View File

@ -45,22 +45,16 @@ public class CommandListener extends PlexListener
{ {
Player player = event.getPlayer(); Player player = event.getPlayer();
PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId()); PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId());
String commandName = StringUtils.normalizeSpace(event.getMessage()).split(" ")[0].replace("/", ""); String commandName = StringUtils.normalizeSpace(event.getMessage()).split(" ")[0].replaceFirst("/", "");
String arguments = StringUtils.normalizeSpace(StringUtils.normalizeSpace(event.getMessage()).replace(event.getMessage().split(" ")[0], "")); String arguments = StringUtils.normalizeSpace(StringUtils.normalizeSpace(event.getMessage()).replace(event.getMessage().split(" ")[0], ""));
PlexLog.debug("Checking Command: {0} with args {1}", commandName, arguments); PlexLog.debug("Checking Command: {0} with args {1}", commandName, arguments);
AtomicReference<BlockedCommand> cmdRef = new AtomicReference<>(); AtomicReference<BlockedCommand> cmdRef = new AtomicReference<>();
PlexLog.debug("Blocked Commands List: " + CommandBlockerService.getBLOCKED_COMMANDS().size()); PlexLog.debug("Blocked Commands List: " + CommandBlockerService.getBLOCKED_COMMANDS().size());
CommandBlockerService.getBLOCKED_COMMANDS().stream().filter(blockedCommand -> blockedCommand.getCommand() != null).forEach(blockedCommand -> CommandBlockerService.getBLOCKED_COMMANDS().stream().filter(blockedCommand -> blockedCommand.getCommand() != null).forEach(blockedCommand ->
{ {
/*if (event.getMessage().replace("/", "").toLowerCase(Locale.ROOT).startsWith(blockedCommand.getCommand().toLowerCase(Locale.ROOT)))
{
PlexLog.debug("Used blocked command exactly matched");
cmdRef.set(blockedCommand);
return;
}*/
boolean matches = true; boolean matches = true;
String[] args = blockedCommand.getCommand().split(" "); String[] args = blockedCommand.getCommand().split(" ");
String[] cmdArgs = event.getMessage().replace("/", "").split(" "); String[] cmdArgs = event.getMessage().replaceFirst("/", "").split(" ");
for (int i = 0; i < args.length; i++) for (int i = 0; i < args.length; i++)
{ {
if (i+1 > cmdArgs.length) if (i+1 > cmdArgs.length)
@ -99,10 +93,10 @@ public class CommandListener extends PlexListener
if (blockedCommand.getRegex() != null) if (blockedCommand.getRegex() != null)
{ {
Pattern pattern = Pattern.compile(blockedCommand.getRegex(), Pattern.CASE_INSENSITIVE); Pattern pattern = Pattern.compile(blockedCommand.getRegex(), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(event.getMessage().replace("/", "")); Matcher matcher = pattern.matcher(event.getMessage().replaceFirst("/", ""));
if (matcher.find()) if (matcher.find())
{ {
PlexLog.debug("Found blocked regexed command"); PlexLog.debug("Player attempted to use a blocked regex");
cmdRef.set(blockedCommand); cmdRef.set(blockedCommand);
} }
} }
@ -115,15 +109,23 @@ public class CommandListener extends PlexListener
{ {
case "e" -> { case "e" -> {
event.setCancelled(true); event.setCancelled(true);
event.getPlayer().sendMessage(PlexUtils.messageComponent("commandBlocked")); event.getPlayer().sendMessage(cmd.getMessage());
} }
case "a" -> { case "a" -> {
if (plexPlayer.isAdminActive() && plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN))
{
return;
}
event.setCancelled(true); event.setCancelled(true);
event.getPlayer().sendMessage(PlexUtils.messageComponent("commandBlocked")); event.getPlayer().sendMessage(cmd.getMessage());
} }
case "s" -> { case "s" -> {
if (plexPlayer.isAdminActive() && plexPlayer.getRankFromString().isAtLeast(Rank.SENIOR_ADMIN))
{
return;
}
event.setCancelled(true); event.setCancelled(true);
event.getPlayer().sendMessage(PlexUtils.messageComponent("commandBlocked")); event.getPlayer().sendMessage(cmd.getMessage());
} }
} }
} }

View File

@ -159,10 +159,14 @@ public class PunishmentManager extends PlexBase
} }
else else
{ {
PlexLog.debug("Checking active bans mysql");
CompletableFuture<List<Punishment>> future = new CompletableFuture<>(); CompletableFuture<List<Punishment>> future = new CompletableFuture<>();
Plex.get().getSqlPunishment().getPunishments().whenComplete((punishments, throwable) -> Plex.get().getSqlPunishment().getPunishments().whenComplete((punishments, throwable) ->
{ {
future.complete(punishments.stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN).toList()); PlexLog.debug("Received Punishments");
List<Punishment> punishmentList = punishments.stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN).toList();
PlexLog.debug("Completing with {0} punishments", punishmentList.size());
future.complete(punishmentList);
}); });
return future; return future;
} }

View File

@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import dev.plex.command.blocking.BlockedCommand; import dev.plex.command.blocking.BlockedCommand;
import dev.plex.services.AbstractService; import dev.plex.services.AbstractService;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import lombok.Getter; import lombok.Getter;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -38,7 +39,7 @@ public class CommandBlockerService extends AbstractService
{ {
command.setRequiredLevel(args[1]); command.setRequiredLevel(args[1]);
command.setRegex(args[2]); command.setRegex(args[2]);
command.setMessage(s.substring(lastDelim + 1)); command.setMessage(s.substring(lastDelim + 1).equalsIgnoreCase("_") ? PlexUtils.messageComponent("commandBlocked") : PlexUtils.mmDeserialize(s.substring(lastDelim + 1)));
/*PlexLog.debug("=Found regex blocked="); /*PlexLog.debug("=Found regex blocked=");
PlexLog.debug(" Regex: " + command.getRegex()); PlexLog.debug(" Regex: " + command.getRegex());
PlexLog.debug(" Message: " + command.getMessage()); PlexLog.debug(" Message: " + command.getMessage());
@ -47,7 +48,7 @@ public class CommandBlockerService extends AbstractService
{ {
command.setRequiredLevel(args[1]); command.setRequiredLevel(args[1]);
command.setCommand(args[2]); command.setCommand(args[2]);
command.setMessage(s.substring(lastDelim + 1)); command.setMessage(s.substring(lastDelim + 1).equalsIgnoreCase("_") ? PlexUtils.messageComponent("commandBlocked") : PlexUtils.mmDeserialize(s.substring(lastDelim + 1)));
Command cmd = plugin.getServer().getCommandMap().getCommand(command.getCommand().split(" ")[0]); Command cmd = plugin.getServer().getCommandMap().getCommand(command.getCommand().split(" ")[0]);
if (cmd == null) if (cmd == null)
{ {
@ -63,10 +64,6 @@ public class CommandBlockerService extends AbstractService
PlexLog.debug(" Aliases: " + Arrays.toString(command.getCommandAliases().toArray(new String[0]))); PlexLog.debug(" Aliases: " + Arrays.toString(command.getCommandAliases().toArray(new String[0])));
PlexLog.debug("====================");*/ PlexLog.debug("====================");*/
} }
if (command.getMessage().equalsIgnoreCase("_"))
{
command.setMessage("This command is blocked.");
}
BLOCKED_COMMANDS.add(command); BLOCKED_COMMANDS.add(command);
}); });
PlexLog.log("Command Blocker has loaded {0} entries!", BLOCKED_COMMANDS.size()); PlexLog.log("Command Blocker has loaded {0} entries!", BLOCKED_COMMANDS.size());