Add CommandListener to enforce command whitelist and block restricted commands
This commit is contained in:
parent
a137ef4cc6
commit
5ce26782f2
|
|
@ -4,11 +4,7 @@ import com.alttd.hunger_games.commands.BaseCommand;
|
|||
import com.alttd.hunger_games.config.Config;
|
||||
import com.alttd.hunger_games.config.LootItems;
|
||||
import com.alttd.hunger_games.config.Messages;
|
||||
import com.alttd.hunger_games.event_listeners.ChestListener;
|
||||
import com.alttd.hunger_games.event_listeners.ItemSpawnListener;
|
||||
import com.alttd.hunger_games.event_listeners.PlayerDamageListener;
|
||||
import com.alttd.hunger_games.event_listeners.PlayerDisconnectListener;
|
||||
import com.alttd.hunger_games.event_listeners.PlayerJoinListener;
|
||||
import com.alttd.hunger_games.event_listeners.*;
|
||||
import com.alttd.hunger_games.services.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
|
|
@ -64,6 +60,7 @@ public final class Main extends JavaPlugin {
|
|||
pluginManager.registerEvents(new PlayerDamageListener(roundService, playerService, statService), this);
|
||||
pluginManager.registerEvents(new ChestListener(roundService, lootService), this);
|
||||
pluginManager.registerEvents(new ItemSpawnListener(roundService), this);
|
||||
pluginManager.registerEvents(new CommandListener(), this);
|
||||
}
|
||||
|
||||
public void reloadConfigs() {
|
||||
|
|
|
|||
|
|
@ -8,10 +8,7 @@ import org.bukkit.configuration.ConfigurationSection;
|
|||
|
||||
import java.io.File;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
public class Config extends AbstractConfig {
|
||||
|
|
@ -134,4 +131,17 @@ public class Config extends AbstractConfig {
|
|||
STUCK_WARMUP = Duration.ofSeconds(config.getInt(prefix, "stuck-warmup-seconds", (int) STUCK_WARMUP.toSeconds()));
|
||||
}
|
||||
}
|
||||
|
||||
public static class COMMAND_WHITELIST {
|
||||
private static final String prefix = "command-whitelist";
|
||||
|
||||
public static final List<String> PREFIXES = new ArrayList<>(List.of("msg", "ac", "gac", "tell", "r", "server", "lobby", "hub", "bayou", "creative", "hg", "lp"));
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static void load() {
|
||||
List<String> stringList = config.getStringList(prefix, "prefix-list", PREFIXES);
|
||||
PREFIXES.clear();
|
||||
PREFIXES.addAll(stringList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,6 +57,7 @@ public class Messages extends AbstractConfig {
|
|||
public static String PLAYER_ONLY = "<red>This command can only be executed as a player</red>";
|
||||
public static String PLAYER_NOT_FOUND = "<red>Unable to find online player <player></red>";
|
||||
public static String RELOAD_SUCCESS = "<green>Config and messages have been reloaded.</green>";
|
||||
public static String COMMAND_BLOCKED = "<red>You cannot use that command here!</red>";
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static void load() {
|
||||
|
|
@ -64,6 +65,7 @@ public class Messages extends AbstractConfig {
|
|||
PLAYER_ONLY = config.getString(prefix, "player-only", PLAYER_ONLY);
|
||||
PLAYER_NOT_FOUND = config.getString(prefix, "player-only", PLAYER_NOT_FOUND);
|
||||
RELOAD_SUCCESS = config.getString(prefix, "reload-success", RELOAD_SUCCESS);
|
||||
COMMAND_BLOCKED = config.getString(prefix, "command-blocked", COMMAND_BLOCKED);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
package com.alttd.hunger_games.event_listeners;
|
||||
|
||||
import com.alttd.hunger_games.config.Config;
|
||||
import com.alttd.hunger_games.config.Messages;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class CommandListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (player.hasPermission("hg.bypass")) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove leading slash
|
||||
String message = event.getMessage().substring(1).toLowerCase();
|
||||
String command = message.split(" ")[0];
|
||||
|
||||
boolean whitelisted = Config.COMMAND_WHITELIST.PREFIXES.stream()
|
||||
.anyMatch(prefix -> command.startsWith(prefix.toLowerCase()));
|
||||
|
||||
if (!whitelisted) {
|
||||
event.setCancelled(true);
|
||||
player.sendRichMessage(Messages.GENERIC.COMMAND_BLOCKED);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user