config shenanigans

This commit is contained in:
YouHaveTrouble
2021-07-03 22:21:20 +02:00
parent 20eb731b93
commit 88d51b29aa
13 changed files with 190 additions and 149 deletions
@@ -61,8 +61,10 @@ public class CommandWhitelistBukkit extends JavaPlugin {
private void reloadPluginConfig() {
File configFile = new File("plugins/CommandWhitelist/config.yml");
configCache = new ConfigCache(configFile, true);
if (configCache == null)
configCache = new ConfigCache(configFile, true, getSLF4JLogger());
else
configCache.reloadConfig();
}
public void reloadPluginConfig(CommandSender sender) {
@@ -91,8 +93,9 @@ public class CommandWhitelistBukkit extends JavaPlugin {
* @param player Bukkit Player
* @return commands available to the player
*/
public static HashSet<String> getCommands(org.bukkit.entity.Player player, HashMap<String, CWGroup> groups) {
public static HashSet<String> getCommands(org.bukkit.entity.Player player) {
HashSet<String> commandList = new HashSet<>();
HashMap<String, CWGroup> groups = configCache.getGroupList();
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
if (s.getKey().equalsIgnoreCase("default"))
commandList.addAll(s.getValue().getCommands());
@@ -106,13 +109,13 @@ public class CommandWhitelistBukkit extends JavaPlugin {
* @param player Bukkit Player
* @return subcommands unavailable for the player
*/
public static HashSet<String> getSuggestions(org.bukkit.entity.Player player, HashMap<String, CWGroup> groups) {
public static HashSet<String> getSuggestions(org.bukkit.entity.Player player) {
HashSet<String> suggestionList = new HashSet<>();
HashMap<String, CWGroup> groups = configCache.getGroupList();
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
if (s.getKey().equalsIgnoreCase("default"))
suggestionList.addAll(s.getValue().getSubCommands());
if (player.hasPermission("commandwhitelist.group." + s.getKey()))
continue;
if (player.hasPermission("commandwhitelist.group." + s.getKey())) continue;
suggestionList.addAll(s.getValue().getSubCommands());
}
return suggestionList;
@@ -28,16 +28,12 @@ public class PacketCommandPreProcessListener {
public void onPacketReceiving(PacketEvent event) {
PacketContainer packet = event.getPacket();
String string = packet.getStrings().read(0);
if (!string.startsWith("/"))
return;
if (!string.startsWith("/")) return;
Player player = event.getPlayer();
if (player.hasPermission("commandwhitelist.bypass"))
return;
ConfigCache configCache = CommandWhitelistBukkit.getConfigCache();
if (player.hasPermission("commandwhitelist.bypass")) return;
String label = CommandUtil.getCommandLabel(string.toLowerCase());
HashSet<String> commands = CommandWhitelistBukkit.getCommands(player, configCache.getGroupList());
HashSet<String> commands = CommandWhitelistBukkit.getCommands(player);
if (!commands.contains(label)) {
event.setCancelled(true);
ConfigCache config = CommandWhitelistBukkit.getConfigCache();
@@ -45,7 +41,7 @@ public class PacketCommandPreProcessListener {
return;
}
HashSet<String> bannedSubCommands = CommandWhitelistBukkit.getSuggestions(player, configCache.getGroupList());
HashSet<String> bannedSubCommands = CommandWhitelistBukkit.getSuggestions(player);
for (String bannedSubCommand : bannedSubCommands) {
if (string.toLowerCase().substring(1).startsWith(bannedSubCommand)) {
event.setCancelled(true);
@@ -15,14 +15,12 @@ public class PlayerCommandPreProcessListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
public void PlayerCommandSendEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent event) {
Player player = event.getPlayer();
if (player.hasPermission("commandwhitelist.bypass"))
return;
if (player.hasPermission("commandwhitelist.bypass")) return;
String label = CommandUtil.getCommandLabel(event.getMessage().toLowerCase());
ConfigCache configCache = CommandWhitelistBukkit.getConfigCache();
BukkitAudiences audiences = CommandWhitelistBukkit.getAudiences();
HashSet<String> commands = CommandWhitelistBukkit.getCommands(player, configCache.getGroupList());
HashSet<String> commands = CommandWhitelistBukkit.getCommands(player);
if (!commands.contains(label)) {
event.setCancelled(true);
ConfigCache config = CommandWhitelistBukkit.getConfigCache();
@@ -30,7 +28,7 @@ public class PlayerCommandPreProcessListener implements Listener {
return;
}
HashSet<String> bannedSubCommands = CommandWhitelistBukkit.getSuggestions(player, configCache.getGroupList());
HashSet<String> bannedSubCommands = CommandWhitelistBukkit.getSuggestions(player);
for (String bannedSubCommand : bannedSubCommands) {
if (event.getMessage().toLowerCase().substring(1).startsWith(bannedSubCommand)) {
event.setCancelled(true);
@@ -8,12 +8,11 @@ import org.bukkit.event.Listener;
import java.util.*;
public class PlayerCommandSendListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
@EventHandler(priority = EventPriority.NORMAL)
public void PlayerCommandSendEvent(org.bukkit.event.player.PlayerCommandSendEvent event) {
Player player = event.getPlayer();
if (player.hasPermission("commandwhitelist.bypass"))
return;
HashSet<String> commandList = CommandWhitelistBukkit.getCommands(player, CommandWhitelistBukkit.getConfigCache().getGroupList());
if (player.hasPermission("commandwhitelist.bypass")) return;
HashSet<String> commandList = CommandWhitelistBukkit.getCommands(player);
event.getCommands().removeIf((cmd) -> !commandList.contains(cmd));
}
}
@@ -8,16 +8,15 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
public class TabCompleteBlockerListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGHEST)
public void onCommandTabComplete(org.bukkit.event.server.TabCompleteEvent event) {
if (!(event.getSender() instanceof Player))
return;
if (!(event.getSender() instanceof Player)) return;
Player player = (Player) event.getSender();
event.setCompletions(
CommandUtil.filterSuggestions(
event.getBuffer(),
event.getCompletions(),
CommandWhitelistBukkit.getSuggestions(player, CommandWhitelistBukkit.getConfigCache().getGroupList())
CommandWhitelistBukkit.getSuggestions(player)
)
);
}