This commit is contained in:
ayunami2000 2022-04-08 01:16:05 -04:00
parent 294f8db528
commit 583394d22e
9 changed files with 150 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import dev.plex.cache.player.PlayerCache;
import dev.plex.cache.player.SQLPlayerData;
import dev.plex.cache.sql.SQLNotes;
import dev.plex.cache.sql.SQLPunishment;
import dev.plex.cmdblocker.CommandBlockerManager;
import dev.plex.config.Config;
import dev.plex.handlers.CommandHandler;
import dev.plex.handlers.ListenerHandler;
@ -44,6 +45,7 @@ public class Plex extends JavaPlugin
public Config config;
public Config messages;
public Config indefBans;
public Config blockedCommands;
public File modulesFolder;
private StorageType storageType = StorageType.SQLITE;
@ -62,6 +64,7 @@ public class Plex extends JavaPlugin
private RankManager rankManager;
private ServiceManager serviceManager;
private PunishmentManager punishmentManager;
private CommandBlockerManager commandBlockerManager;
private AdminList adminList;
private UpdateChecker updateChecker;
@ -79,6 +82,7 @@ public class Plex extends JavaPlugin
config = new Config(this, "config.yml");
messages = new Config(this, "messages.yml");
indefBans = new Config(this, "indefbans.yml");
blockedCommands = new Config(this, "commands.yml");
build.load(this);
modulesFolder = new File(this.getDataFolder() + File.separator + "modules");
@ -99,6 +103,7 @@ public class Plex extends JavaPlugin
messages.load();
// Don't add default entries to indefinite ban file
indefBans.load(false);
blockedCommands.load();
sqlConnection = new SQLConnection();
mongoConnection = new MongoConnection();
@ -158,6 +163,9 @@ public class Plex extends JavaPlugin
punishmentManager.mergeIndefiniteBans();
PlexLog.log("Punishment System initialized");
commandBlockerManager = new CommandBlockerManager();
PlexLog.log("Command Blocker initialized");
generateWorlds();
serviceManager = new ServiceManager();

View File

@ -0,0 +1,17 @@
package dev.plex.cmdblocker;
import dev.plex.rank.enums.Rank;
import lombok.Getter;
@Getter
public class BaseCommand
{
public final Rank rank;
public final String message;
public BaseCommand(Rank r, String m)
{
rank = r;
message = m;
}
}

View File

@ -0,0 +1,46 @@
package dev.plex.cmdblocker;
import dev.plex.Plex;
import dev.plex.rank.enums.Rank;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class CommandBlockerManager
{
private List<BaseCommand> blockedCommands = new ArrayList<>();
public void syncCommands()
{
blockedCommands.clear();
List<String> raw = Plex.get().blockedCommands.getStringList("blockedCommands");
for (String cmd : raw)
{
List<String> pieces = new ArrayList<>();
int lastDelim = cmd.lastIndexOf(':');
String cmdWithoutMsg = cmd.substring(0, lastDelim);
String[] rawPieces = cmdWithoutMsg.split(":", 3);
pieces.add(rawPieces[0].toLowerCase()); // type
pieces.add(rawPieces[1].toLowerCase()); // rank
pieces.add(rawPieces[2]); // RegEx or match
pieces.add(cmd.substring(lastDelim + 1)); // Message (w/o : in it)
Rank rank = Plex.get().getRankManager().getRankFromString(pieces.get(1));
if (pieces.get(0).equals("r"))
{
blockedCommands.add(new RegexCommand(Pattern.compile(pieces.get(2)), rank, pieces.get(3)));
}
else if (pieces.get(0).equals("m"))
{
blockedCommands.add(new MatchCommand(pieces.get(2), rank, pieces.get(3)));
}
}
}
}

View File

@ -0,0 +1,16 @@
package dev.plex.cmdblocker;
import dev.plex.rank.enums.Rank;
import lombok.Getter;
@Getter
public class MatchCommand extends BaseCommand
{
public final String match;
public MatchCommand(String r1, Rank r2, String m1)
{
super(r2, m1);
match = r1;
}
}

View File

@ -0,0 +1,18 @@
package dev.plex.cmdblocker;
import dev.plex.rank.enums.Rank;
import lombok.Getter;
import java.util.regex.Pattern;
@Getter
public class RegexCommand extends BaseCommand
{
public final Pattern regex;
public RegexCommand(Pattern r1, Rank r2, String m1)
{
super(r2, m1);
regex = r1;
}
}

View File

@ -18,6 +18,7 @@ import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -51,13 +52,16 @@ public class PlexCMD extends PlexCommand
plugin.indefBans.load(false);
plugin.getPunishmentManager().mergeIndefiniteBans();
send(sender, "Reloaded indefinite bans");
plugin.blockedCommands.load();
plugin.getCommandBlockerManager().syncCommands();
send(sender, "Reloaded blocked commands file");
plugin.getRankManager().importDefaultRanks();
send(sender, "Imported ranks");
send(sender, "Plex successfully reloaded.");
plugin.setSystem(plugin.config.getString("system"));
plugin.getServiceManager().endServices();
plugin.getServiceManager().startServices();
PlexLog.debug("Restarted services");
send(sender, "Plex successfully reloaded.");
return null;
}
else if (args[0].equalsIgnoreCase("redis"))

View File

@ -0,0 +1,19 @@
package dev.plex.listener.impl;
import dev.plex.listener.PlexListener;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
public class CmdBlockerListener extends PlexListener
{
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
{
Player player = event.getPlayer();
String message = event.getMessage().substring(1);
// check if commands are blocked here
// DEFAULT message is named "commandBlocked"
}
}

View File

@ -0,0 +1,19 @@
#
# Command Blocker
#
# Format:
# - "<regex or match>:<rank>:command name no slash:Block message"
#
# Symbols to use:
# - r for RegEx
# - m for matching
# - The ranks are "e" for everyone, "a" for admin and "s" for senior admin
# - The command is just the command without slashes. Optional arguments are specified as well
# - Finally the block message. MUST NOT CONTAIN ":". Use _ to use the default command blocked message as specified in messages.yml, or you can optionally put your own in
#
# So these would be valid:
# - "m:e:mail sendall:You cannot send messages to everyone on the server"
# - "r:e:(.*:):Plugin specific commands are disabled"
blockedCommands:
- "m:e:mail sendall:You cannot send messages to everyone on the server"
- "r:e:(.*:):Plugin specific commands are disabled"

View File

@ -167,3 +167,4 @@ removedMobs: "<red>{0} - Removed {1} mobs"
autoWipeDisabled: "<gray>Item wiping is currently disabled in the config!"
allowDropsDisabled: "<gray>No longer allowing drops from players."
allowDropsEnabled: "<gray>Now allowing drops from players."
commandBlocked: "<gray>This command is blocked"