mirror of
https://github.com/YouHaveTrouble/CommandWhitelist.git
synced 2026-05-12 06:26:57 +00:00
config shenanigans
This commit is contained in:
+9
-6
@@ -61,8 +61,10 @@ public class CommandWhitelistBukkit extends JavaPlugin {
|
|||||||
|
|
||||||
private void reloadPluginConfig() {
|
private void reloadPluginConfig() {
|
||||||
File configFile = new File("plugins/CommandWhitelist/config.yml");
|
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) {
|
public void reloadPluginConfig(CommandSender sender) {
|
||||||
@@ -91,8 +93,9 @@ public class CommandWhitelistBukkit extends JavaPlugin {
|
|||||||
* @param player Bukkit Player
|
* @param player Bukkit Player
|
||||||
* @return commands available to the 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<>();
|
HashSet<String> commandList = new HashSet<>();
|
||||||
|
HashMap<String, CWGroup> groups = configCache.getGroupList();
|
||||||
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
|
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
|
||||||
if (s.getKey().equalsIgnoreCase("default"))
|
if (s.getKey().equalsIgnoreCase("default"))
|
||||||
commandList.addAll(s.getValue().getCommands());
|
commandList.addAll(s.getValue().getCommands());
|
||||||
@@ -106,13 +109,13 @@ public class CommandWhitelistBukkit extends JavaPlugin {
|
|||||||
* @param player Bukkit Player
|
* @param player Bukkit Player
|
||||||
* @return subcommands unavailable for the 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<>();
|
HashSet<String> suggestionList = new HashSet<>();
|
||||||
|
HashMap<String, CWGroup> groups = configCache.getGroupList();
|
||||||
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
|
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
|
||||||
if (s.getKey().equalsIgnoreCase("default"))
|
if (s.getKey().equalsIgnoreCase("default"))
|
||||||
suggestionList.addAll(s.getValue().getSubCommands());
|
suggestionList.addAll(s.getValue().getSubCommands());
|
||||||
if (player.hasPermission("commandwhitelist.group." + s.getKey()))
|
if (player.hasPermission("commandwhitelist.group." + s.getKey())) continue;
|
||||||
continue;
|
|
||||||
suggestionList.addAll(s.getValue().getSubCommands());
|
suggestionList.addAll(s.getValue().getSubCommands());
|
||||||
}
|
}
|
||||||
return suggestionList;
|
return suggestionList;
|
||||||
|
|||||||
+4
-8
@@ -28,16 +28,12 @@ public class PacketCommandPreProcessListener {
|
|||||||
public void onPacketReceiving(PacketEvent event) {
|
public void onPacketReceiving(PacketEvent event) {
|
||||||
PacketContainer packet = event.getPacket();
|
PacketContainer packet = event.getPacket();
|
||||||
String string = packet.getStrings().read(0);
|
String string = packet.getStrings().read(0);
|
||||||
if (!string.startsWith("/"))
|
if (!string.startsWith("/")) return;
|
||||||
return;
|
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
if (player.hasPermission("commandwhitelist.bypass"))
|
if (player.hasPermission("commandwhitelist.bypass")) return;
|
||||||
return;
|
|
||||||
|
|
||||||
ConfigCache configCache = CommandWhitelistBukkit.getConfigCache();
|
|
||||||
|
|
||||||
String label = CommandUtil.getCommandLabel(string.toLowerCase());
|
String label = CommandUtil.getCommandLabel(string.toLowerCase());
|
||||||
HashSet<String> commands = CommandWhitelistBukkit.getCommands(player, configCache.getGroupList());
|
HashSet<String> commands = CommandWhitelistBukkit.getCommands(player);
|
||||||
if (!commands.contains(label)) {
|
if (!commands.contains(label)) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
ConfigCache config = CommandWhitelistBukkit.getConfigCache();
|
ConfigCache config = CommandWhitelistBukkit.getConfigCache();
|
||||||
@@ -45,7 +41,7 @@ public class PacketCommandPreProcessListener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
HashSet<String> bannedSubCommands = CommandWhitelistBukkit.getSuggestions(player, configCache.getGroupList());
|
HashSet<String> bannedSubCommands = CommandWhitelistBukkit.getSuggestions(player);
|
||||||
for (String bannedSubCommand : bannedSubCommands) {
|
for (String bannedSubCommand : bannedSubCommands) {
|
||||||
if (string.toLowerCase().substring(1).startsWith(bannedSubCommand)) {
|
if (string.toLowerCase().substring(1).startsWith(bannedSubCommand)) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
|
|||||||
+3
-5
@@ -15,14 +15,12 @@ public class PlayerCommandPreProcessListener implements Listener {
|
|||||||
@EventHandler(priority = EventPriority.HIGHEST)
|
@EventHandler(priority = EventPriority.HIGHEST)
|
||||||
public void PlayerCommandSendEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent event) {
|
public void PlayerCommandSendEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent event) {
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
if (player.hasPermission("commandwhitelist.bypass"))
|
if (player.hasPermission("commandwhitelist.bypass")) return;
|
||||||
return;
|
|
||||||
String label = CommandUtil.getCommandLabel(event.getMessage().toLowerCase());
|
String label = CommandUtil.getCommandLabel(event.getMessage().toLowerCase());
|
||||||
|
|
||||||
ConfigCache configCache = CommandWhitelistBukkit.getConfigCache();
|
|
||||||
BukkitAudiences audiences = CommandWhitelistBukkit.getAudiences();
|
BukkitAudiences audiences = CommandWhitelistBukkit.getAudiences();
|
||||||
|
|
||||||
HashSet<String> commands = CommandWhitelistBukkit.getCommands(player, configCache.getGroupList());
|
HashSet<String> commands = CommandWhitelistBukkit.getCommands(player);
|
||||||
if (!commands.contains(label)) {
|
if (!commands.contains(label)) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
ConfigCache config = CommandWhitelistBukkit.getConfigCache();
|
ConfigCache config = CommandWhitelistBukkit.getConfigCache();
|
||||||
@@ -30,7 +28,7 @@ public class PlayerCommandPreProcessListener implements Listener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
HashSet<String> bannedSubCommands = CommandWhitelistBukkit.getSuggestions(player, configCache.getGroupList());
|
HashSet<String> bannedSubCommands = CommandWhitelistBukkit.getSuggestions(player);
|
||||||
for (String bannedSubCommand : bannedSubCommands) {
|
for (String bannedSubCommand : bannedSubCommands) {
|
||||||
if (event.getMessage().toLowerCase().substring(1).startsWith(bannedSubCommand)) {
|
if (event.getMessage().toLowerCase().substring(1).startsWith(bannedSubCommand)) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
|
|||||||
+3
-4
@@ -8,12 +8,11 @@ import org.bukkit.event.Listener;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class PlayerCommandSendListener implements Listener {
|
public class PlayerCommandSendListener implements Listener {
|
||||||
@EventHandler(priority = EventPriority.HIGHEST)
|
@EventHandler(priority = EventPriority.NORMAL)
|
||||||
public void PlayerCommandSendEvent(org.bukkit.event.player.PlayerCommandSendEvent event) {
|
public void PlayerCommandSendEvent(org.bukkit.event.player.PlayerCommandSendEvent event) {
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
if (player.hasPermission("commandwhitelist.bypass"))
|
if (player.hasPermission("commandwhitelist.bypass")) return;
|
||||||
return;
|
HashSet<String> commandList = CommandWhitelistBukkit.getCommands(player);
|
||||||
HashSet<String> commandList = CommandWhitelistBukkit.getCommands(player, CommandWhitelistBukkit.getConfigCache().getGroupList());
|
|
||||||
event.getCommands().removeIf((cmd) -> !commandList.contains(cmd));
|
event.getCommands().removeIf((cmd) -> !commandList.contains(cmd));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-4
@@ -8,16 +8,15 @@ import org.bukkit.event.EventPriority;
|
|||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
|
||||||
public class TabCompleteBlockerListener implements 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) {
|
public void onCommandTabComplete(org.bukkit.event.server.TabCompleteEvent event) {
|
||||||
if (!(event.getSender() instanceof Player))
|
if (!(event.getSender() instanceof Player)) return;
|
||||||
return;
|
|
||||||
Player player = (Player) event.getSender();
|
Player player = (Player) event.getSender();
|
||||||
event.setCompletions(
|
event.setCompletions(
|
||||||
CommandUtil.filterSuggestions(
|
CommandUtil.filterSuggestions(
|
||||||
event.getBuffer(),
|
event.getBuffer(),
|
||||||
event.getCompletions(),
|
event.getCompletions(),
|
||||||
CommandWhitelistBukkit.getSuggestions(player, CommandWhitelistBukkit.getConfigCache().getGroupList())
|
CommandWhitelistBukkit.getSuggestions(player)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,15 +47,42 @@
|
|||||||
</resources>
|
</resources>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>jitpack.io</id>
|
||||||
|
<url>https://jitpack.io</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>velocitypowered-repo</id>
|
||||||
|
<url>https://repo.velocitypowered.com/releases/</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.yaml</groupId>
|
<groupId>com.github.Thatsmusic99</groupId>
|
||||||
<artifactId>snakeyaml</artifactId>
|
<artifactId>ConfigurationMaster</artifactId>
|
||||||
<version>1.28</version>
|
<version>v2.0.0-ALPHA-2</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.velocitypowered</groupId>
|
||||||
|
<artifactId>velocity-api</artifactId>
|
||||||
|
<version>1.1.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.destroystokyo.paper</groupId>
|
||||||
|
<artifactId>paper-api</artifactId>
|
||||||
|
<version>1.16.5-R0.1-SNAPSHOT</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.github.waterfallmc</groupId>
|
||||||
|
<artifactId>waterfall-api</artifactId>
|
||||||
|
<version>1.16-R0.5-SNAPSHOT</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
+9
-6
@@ -8,18 +8,22 @@ public class CommandUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Filters blocked command suggestions from provided collection of strings
|
* Filters blocked command suggestions from provided collection of strings
|
||||||
|
*
|
||||||
* @param buffer Command buffer
|
* @param buffer Command buffer
|
||||||
* @param suggestions Full suggestions list
|
* @param suggestions Full suggestions list
|
||||||
* @param blockedSubCommands Subcommands to filter out
|
* @param blockedSubCommands Subcommands to filter out
|
||||||
* @return Filtered list of suggestions
|
* @return Filtered list of suggestions
|
||||||
*/
|
*/
|
||||||
public static List<String> filterSuggestions(String buffer, Collection<String> suggestions, Collection<String> blockedSubCommands) {
|
public static List<String> filterSuggestions(String buffer, Collection<String> suggestions, Collection<String> blockedSubCommands) {
|
||||||
String cmd = buffer.replace(getLastArgument(buffer), "");
|
|
||||||
for (String s : blockedSubCommands) {
|
for (String s : blockedSubCommands) {
|
||||||
String slast = getLastArgument(s.toLowerCase());
|
String slast = getLastArgument(s);
|
||||||
String scommand = s.replace(slast, "");
|
String scommand = s.replace(slast, "");
|
||||||
cmd = cmd.replace(getLastArgument(cmd), "");
|
String[] cmdSplit = buffer.split(" ");
|
||||||
if (cmd.substring(1).startsWith("/" + scommand)) {
|
StringBuilder cmdBuilder = new StringBuilder();
|
||||||
|
for (int i = 0; i <= cmdSplit.length - 1; i++)
|
||||||
|
cmdBuilder.append(cmdSplit[i]).append(" ");
|
||||||
|
String cmd = cmdBuilder.toString();
|
||||||
|
if (cmd.startsWith("/" + scommand)) {
|
||||||
while (suggestions.contains(slast))
|
while (suggestions.contains(slast))
|
||||||
suggestions.remove(slast);
|
suggestions.remove(slast);
|
||||||
}
|
}
|
||||||
@@ -33,8 +37,7 @@ public class CommandUtil {
|
|||||||
*/
|
*/
|
||||||
public static String getLastArgument(String cmd) {
|
public static String getLastArgument(String cmd) {
|
||||||
String[] parts = cmd.split(" ");
|
String[] parts = cmd.split(" ");
|
||||||
if (parts.length <= 1)
|
if (parts.length <= 1) return "";
|
||||||
return "";
|
|
||||||
String last = "";
|
String last = "";
|
||||||
for (String part : parts) {
|
for (String part : parts) {
|
||||||
last = part;
|
last = part;
|
||||||
|
|||||||
+92
-80
@@ -1,36 +1,62 @@
|
|||||||
package eu.endermite.commandwhitelist.common;
|
package eu.endermite.commandwhitelist.common;
|
||||||
|
|
||||||
import org.yaml.snakeyaml.DumperOptions;
|
import io.github.thatsmusic99.configurationmaster.api.ConfigFile;
|
||||||
import org.yaml.snakeyaml.Yaml;
|
import io.github.thatsmusic99.configurationmaster.api.ConfigSection;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class ConfigCache {
|
public class ConfigCache {
|
||||||
|
|
||||||
private final File configFile;
|
private final File configFile;
|
||||||
|
private ConfigFile config;
|
||||||
|
private final Object logger;
|
||||||
private final boolean canDoProtocolLib;
|
private final boolean canDoProtocolLib;
|
||||||
private final HashMap<String, CWGroup> groupList = new LinkedHashMap<>();
|
private final HashMap<String, CWGroup> groupList = new LinkedHashMap<>();
|
||||||
public String prefix, command_denied, no_permission, no_such_subcommand, config_reloaded, added_to_whitelist,
|
public String prefix, command_denied, no_permission, no_such_subcommand, config_reloaded, added_to_whitelist,
|
||||||
removed_from_whitelist, group_doesnt_exist, subcommand_denied;
|
removed_from_whitelist, group_doesnt_exist, subcommand_denied;
|
||||||
public boolean useProtocolLib = false;
|
public boolean useProtocolLib = false;
|
||||||
|
|
||||||
public ConfigCache(File configFile, boolean canDoProtocolLib) {
|
public ConfigCache(File configFile, boolean canDoProtocolLib, Object logger){
|
||||||
this.configFile = configFile;
|
this.configFile = configFile;
|
||||||
this.canDoProtocolLib = canDoProtocolLib;
|
this.canDoProtocolLib = canDoProtocolLib;
|
||||||
if (!reloadConfig())
|
this.logger = logger;
|
||||||
|
|
||||||
reloadConfig();
|
reloadConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveDefaultConfig(Map<String, Object> data, File configFile, boolean canDoProtocolLib) {
|
public boolean reloadConfig() {
|
||||||
|
|
||||||
data.put("messages", processMessages());
|
boolean firstLoad = createFiles();
|
||||||
|
config = ConfigFile.loadConfig(configFile);
|
||||||
|
|
||||||
if (canDoProtocolLib) {
|
config.addDefault("messages.prefix", "CommandWhitelist > ");
|
||||||
data.put("use_protocollib_to_detect_commands", useProtocolLib);
|
config.addDefault("messages.command_denied", "No such command.");
|
||||||
|
config.addDefault("messages.subcommand_denied", "You cannot use this subcommand");
|
||||||
|
config.addDefault("messages.no_permission", "<red>You don't have permission to do this.");
|
||||||
|
config.addDefault("messages.no_such_subcommand", "<red>No subcommand by that name.");
|
||||||
|
config.addDefault("messages.config_reloaded", "<yellow>Configuration reloaded.");
|
||||||
|
config.addDefault("messages.added_to_whitelist", "<yellow>Whitelisted command <gold>%s <yellow>for permission <gold>%s");
|
||||||
|
config.addDefault("messages.removed_from_whitelist", "<yellow>Removed command <gold>%s <yellow>from permission <gold>%s");
|
||||||
|
config.addDefault("messages.group_doesnt_exist", "<red>Group doesn't exist or error occured");
|
||||||
|
|
||||||
|
config.addComment("messages", "Messages use MiniMessage formatting (https://docs.adventure.kyori.net/minimessage.html#format)");
|
||||||
|
|
||||||
|
if (canDoProtocolLib)
|
||||||
|
config.addDefault("use_protocollib", false, "Do not enable if you don't have issues with aliased commands.\nThis requires server restart to take effect.");
|
||||||
|
|
||||||
|
if (firstLoad) {
|
||||||
|
List<String> exampleCommands = new ArrayList<>();
|
||||||
|
exampleCommands.add("example");
|
||||||
|
List<String> exampleSubCommands = new ArrayList<>();
|
||||||
|
exampleSubCommands.add("example of");
|
||||||
|
|
||||||
|
config.addDefault("groups.example.commands", exampleCommands, "This is the WHITELIST of commands that players will be able to see/use in the group \"example\"");
|
||||||
|
config.addDefault("groups.example.subcommands", exampleSubCommands, "This is the BLACKLIST of subcommands that players will NOT be able to see/use in the group \"example\"");
|
||||||
|
config.addComment("groups.example", "All groups except from default require commandwhitelist.group.<group_name> permission\ncommandwhitelist.group.example in this case\n If you wish to leave the list empty, put \"commands: []\" or \"subcommands: []\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> defaultCommands = new ArrayList<>();
|
List<String> defaultCommands = new ArrayList<>();
|
||||||
List<String> defaultSubcommands = new ArrayList<>();
|
|
||||||
defaultCommands.add("help");
|
defaultCommands.add("help");
|
||||||
defaultCommands.add("spawn");
|
defaultCommands.add("spawn");
|
||||||
defaultCommands.add("bal");
|
defaultCommands.add("bal");
|
||||||
@@ -44,73 +70,69 @@ public class ConfigCache {
|
|||||||
defaultCommands.add("tpaccept");
|
defaultCommands.add("tpaccept");
|
||||||
defaultCommands.add("tpdeny");
|
defaultCommands.add("tpdeny");
|
||||||
defaultCommands.add("warp");
|
defaultCommands.add("warp");
|
||||||
|
List<String> defaultSubcommands = new ArrayList<>();
|
||||||
defaultSubcommands.add("help about");
|
defaultSubcommands.add("help about");
|
||||||
|
|
||||||
HashMap<String, Object> groups = new LinkedHashMap<>();
|
config.addDefault("groups.default", new CWGroup("default", defaultCommands, defaultSubcommands).serialize());
|
||||||
|
|
||||||
for (CWGroup group : groupList.values()) {
|
prefix = config.getString("messages.prefix");
|
||||||
groups.put(group.getId(), group.serialize());
|
command_denied = config.getString("messages.command_denied");
|
||||||
|
subcommand_denied = config.getString("messages.subcommand_denied");
|
||||||
|
no_permission = config.getString("messages.no_permission");
|
||||||
|
no_such_subcommand = config.getString("messages.no_such_subcommand");
|
||||||
|
config_reloaded = config.getString("messages.config_reloaded");
|
||||||
|
added_to_whitelist = config.getString("messages.added_to_whitelist");
|
||||||
|
removed_from_whitelist = config.getString("messages.removed_from_whitelist");
|
||||||
|
group_doesnt_exist = config.getString("messages.group_doesnt_exist");
|
||||||
|
|
||||||
|
ConfigSection groupSection = config.getConfigSection("groups");
|
||||||
|
for (String key : groupSection.getKeys(false)) {
|
||||||
|
groupList.put("key", loadCWGroup(key, groupSection));
|
||||||
}
|
}
|
||||||
|
|
||||||
groups.putIfAbsent("default", new CWGroup("default", defaultCommands, defaultSubcommands).serialize());
|
return saveConfig();
|
||||||
//TODO find a way to update groups
|
}
|
||||||
data.putIfAbsent("groups", groups);
|
|
||||||
|
|
||||||
DumperOptions dumperOptions = new DumperOptions();
|
private boolean saveConfig() {
|
||||||
dumperOptions.setPrettyFlow(true);
|
|
||||||
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
||||||
dumperOptions.setAllowUnicode(true);
|
|
||||||
Yaml yaml = new Yaml(dumperOptions);
|
|
||||||
try {
|
try {
|
||||||
FileWriter writer = new FileWriter(configFile.getPath());
|
config.save();
|
||||||
yaml.dump(data, writer);
|
return true;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean reloadConfig() {
|
|
||||||
HashMap<String, Object> config = new LinkedHashMap<>();
|
|
||||||
Yaml yaml = new Yaml();
|
|
||||||
try {
|
|
||||||
FileInputStream fileInputStream = new FileInputStream(configFile);
|
|
||||||
config = yaml.load(fileInputStream);
|
|
||||||
} catch (FileNotFoundException ignored) {
|
|
||||||
saveDefaultConfig(config, configFile, canDoProtocolLib);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
HashMap<String, String> messages = (HashMap<String, String>) config.get("messages");
|
|
||||||
this.prefix = messages.get("prefix");
|
|
||||||
this.command_denied = messages.get("command_denied");
|
|
||||||
this.no_such_subcommand = messages.get("no_such_subcommand");
|
|
||||||
this.no_permission = messages.get("no_permission");
|
|
||||||
this.config_reloaded = messages.get("config_reloaded");
|
|
||||||
this.added_to_whitelist = messages.get("added_to_whitelist");
|
|
||||||
this.removed_from_whitelist = messages.get("removed_from_whitelist");
|
|
||||||
this.group_doesnt_exist = messages.get("group_doesnt_exist");
|
|
||||||
this.subcommand_denied = messages.get("subcommand_denied");
|
|
||||||
|
|
||||||
if (canDoProtocolLib)
|
|
||||||
this.useProtocolLib = (boolean) config.get("use_protocollib_to_detect_commands");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
HashMap<String, HashMap<String, Object>> groups = (HashMap<String, HashMap<String, Object>>) config.get("groups");
|
|
||||||
for (Map.Entry<String, HashMap<String, Object>> entry : groups.entrySet()) {
|
|
||||||
groupList.put(entry.getKey(), loadCWGroup(entry.getKey(), entry.getValue()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
saveDefaultConfig(config, configFile, canDoProtocolLib);
|
private boolean createFiles() {
|
||||||
|
boolean creatingFiles = false;
|
||||||
return true;
|
try {
|
||||||
|
File parent = new File(configFile.getParent());
|
||||||
|
if (!parent.exists())
|
||||||
|
parent.mkdir();
|
||||||
|
if (!configFile.exists()) {
|
||||||
|
configFile.createNewFile();
|
||||||
|
creatingFiles = true;
|
||||||
|
}
|
||||||
|
return creatingFiles;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public CWGroup loadCWGroup(String id, HashMap<String, Object> map) {
|
public CWGroup loadCWGroup(String id, ConfigSection section) {
|
||||||
List<String> subCommands = new ArrayList<>((Collection<? extends String>) map.get("subcommands"));
|
HashSet<String> commands = new HashSet<>();
|
||||||
List<String> commands = new ArrayList<>((Collection<? extends String>) map.get("commands"));
|
for (String cmd : section.getStringList(id+".commands")) {
|
||||||
|
if (cmd.contains(" ")) {
|
||||||
|
String[] cmdSplit = cmd.split(" ");
|
||||||
|
warn("CommandWhitelist - \""+cmd+"\" is not a command. Loading it as \""+cmdSplit[0]+"\".");
|
||||||
|
cmd = cmdSplit[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commands.contains(cmd)) continue;
|
||||||
|
commands.add(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> subCommands = section.getStringList(id+".subcommands");
|
||||||
return new CWGroup(id, commands, subCommands);
|
return new CWGroup(id, commands, subCommands);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,25 +140,15 @@ public class ConfigCache {
|
|||||||
return groupList;
|
return groupList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashMap<String, String> processMessages() {
|
private void warn(String log) {
|
||||||
HashMap<String, String> messages = new LinkedHashMap<>();
|
if (logger instanceof org.slf4j.Logger) {
|
||||||
messages.put("prefix", stringOrDefault(prefix, "CommandWhitelist > "));
|
((org.slf4j.Logger) logger).warn(log);
|
||||||
messages.put("command_denied", stringOrDefault(command_denied, "No such command."));
|
return;
|
||||||
messages.put("subcommand_denied", stringOrDefault(subcommand_denied, "You cannot use this subcommand"));
|
}
|
||||||
messages.put("no_permission", stringOrDefault(no_permission, "<red>You don't have permission to do this."));
|
if (logger instanceof java.util.logging.Logger) {
|
||||||
messages.put("no_such_subcommand", stringOrDefault(no_such_subcommand, "<red>No subcommand by that name."));
|
((java.util.logging.Logger) logger).warning(log);
|
||||||
messages.put("config_reloaded", stringOrDefault(config_reloaded, "<yellow>Configuration reloaded."));
|
return;
|
||||||
messages.put("added_to_whitelist", stringOrDefault(added_to_whitelist, "<yellow>Whitelisted command <gold>%s <yellow>for permission <gold>%s"));
|
|
||||||
messages.put("removed_from_whitelist", stringOrDefault(removed_from_whitelist, "<yellow>Removed command <gold>%s <yellow>from permission <gold>%s"));
|
|
||||||
messages.put("group_doesnt_exist", stringOrDefault(group_doesnt_exist, "<red>Group doesn't exist or error occured"));
|
|
||||||
return messages;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String stringOrDefault(String value, String def) {
|
|
||||||
if (value != null)
|
|
||||||
return value;
|
|
||||||
else
|
|
||||||
return def;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-10
@@ -15,6 +15,7 @@ import eu.endermite.commandwhitelist.velocity.command.VelocityMainCommand;
|
|||||||
import net.kyori.adventure.identity.Identity;
|
import net.kyori.adventure.identity.Identity;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@@ -28,16 +29,23 @@ public class CommandWhitelistVelocity {
|
|||||||
private static ProxyServer server;
|
private static ProxyServer server;
|
||||||
private static ConfigCache configCache;
|
private static ConfigCache configCache;
|
||||||
private static Path folder;
|
private static Path folder;
|
||||||
|
private static Logger logger;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public CommandWhitelistVelocity(ProxyServer server, Logger logger, @DataDirectory final Path folder) {
|
public CommandWhitelistVelocity(ProxyServer server, Logger logger, @DataDirectory final Path folder) {
|
||||||
CommandWhitelistVelocity.server = server;
|
CommandWhitelistVelocity.server = server;
|
||||||
CommandWhitelistVelocity.folder = folder;
|
CommandWhitelistVelocity.folder = folder;
|
||||||
CommandWhitelistVelocity.plugin = this;
|
CommandWhitelistVelocity.plugin = this;
|
||||||
|
CommandWhitelistVelocity.logger = logger;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void reloadConfig() {
|
private static void reloadConfig() {
|
||||||
configCache = new ConfigCache(new File(String.valueOf(folder), "config.yml"), false);
|
if (configCache == null)
|
||||||
|
configCache = new ConfigCache(new File(String.valueOf(folder), "config.yml"), false, logger);
|
||||||
|
else
|
||||||
|
configCache.reloadConfig();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void reloadConfig(CommandSource source) {
|
public static void reloadConfig(CommandSource source) {
|
||||||
@@ -56,9 +64,9 @@ public class CommandWhitelistVelocity {
|
|||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void onUserCommandSendEvent(PlayerAvailableCommandsEvent event) {
|
public void onUserCommandSendEvent(PlayerAvailableCommandsEvent event) {
|
||||||
if (event.getPlayer().hasPermission("commandwhitelist.bypass"))
|
Player player = event.getPlayer();
|
||||||
return;
|
if (player.hasPermission("commandwhitelist.bypass")) return;
|
||||||
HashSet<String> allowedCommands = CommandWhitelistVelocity.getCommands(event.getPlayer(), configCache.getGroupList());
|
HashSet<String> allowedCommands = CommandWhitelistVelocity.getCommands(player);
|
||||||
event.getRootNode().getChildren().removeIf((commandNode) ->
|
event.getRootNode().getChildren().removeIf((commandNode) ->
|
||||||
server.getCommandManager().hasCommand(commandNode.getName())
|
server.getCommandManager().hasCommand(commandNode.getName())
|
||||||
&& !allowedCommands.contains(commandNode.getName())
|
&& !allowedCommands.contains(commandNode.getName())
|
||||||
@@ -67,14 +75,12 @@ public class CommandWhitelistVelocity {
|
|||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void onUserCommandExecuteEvent(com.velocitypowered.api.event.command.CommandExecuteEvent event) {
|
public void onUserCommandExecuteEvent(com.velocitypowered.api.event.command.CommandExecuteEvent event) {
|
||||||
if (!(event.getCommandSource() instanceof Player))
|
if (!(event.getCommandSource() instanceof Player)) return;
|
||||||
return;
|
|
||||||
Player player = (Player) event.getCommandSource();
|
Player player = (Player) event.getCommandSource();
|
||||||
|
|
||||||
if (player.hasPermission("commandwhitelist.bypass"))
|
if (player.hasPermission("commandwhitelist.bypass")) return;
|
||||||
return;
|
|
||||||
|
|
||||||
HashSet<String> allowedCommands = CommandWhitelistVelocity.getCommands(player, configCache.getGroupList());
|
HashSet<String> allowedCommands = CommandWhitelistVelocity.getCommands(player);
|
||||||
String command = event.getCommand().split(" ")[0];
|
String command = event.getCommand().split(" ")[0];
|
||||||
if (server.getCommandManager().hasCommand(command)
|
if (server.getCommandManager().hasCommand(command)
|
||||||
&& !allowedCommands.contains(command))
|
&& !allowedCommands.contains(command))
|
||||||
@@ -89,7 +95,8 @@ public class CommandWhitelistVelocity {
|
|||||||
* @param player Velocity Player
|
* @param player Velocity Player
|
||||||
* @return commands available to the player
|
* @return commands available to the player
|
||||||
*/
|
*/
|
||||||
public static HashSet<String> getCommands(Player player, HashMap<String, CWGroup> groups) {
|
public static HashSet<String> getCommands(Player player) {
|
||||||
|
HashMap<String, CWGroup> groups = configCache.getGroupList();
|
||||||
HashSet<String> commandList = new HashSet<>();
|
HashSet<String> commandList = new HashSet<>();
|
||||||
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
|
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
|
||||||
if (s.getKey().equalsIgnoreCase("default"))
|
if (s.getKey().equalsIgnoreCase("default"))
|
||||||
|
|||||||
+8
-11
@@ -55,15 +55,10 @@ public final class CommandWhitelistWaterfall extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void loadConfig() {
|
public void loadConfig() {
|
||||||
try {
|
if (configCache == null)
|
||||||
if (!getDataFolder().exists()) {
|
configCache = new ConfigCache(new File(getDataFolder(), "config.yml"), false, getLogger());
|
||||||
getDataFolder().mkdir();
|
else
|
||||||
}
|
configCache.reloadConfig();
|
||||||
|
|
||||||
configCache = new ConfigCache(new File(getDataFolder(), "config.yml"), false);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadConfigAsync(CommandSender sender) {
|
public void loadConfigAsync(CommandSender sender) {
|
||||||
@@ -77,8 +72,9 @@ public final class CommandWhitelistWaterfall extends Plugin {
|
|||||||
* @param player Bungee Player
|
* @param player Bungee Player
|
||||||
* @return commands available to the player
|
* @return commands available to the player
|
||||||
*/
|
*/
|
||||||
public static HashSet<String> getCommands(ProxiedPlayer player, HashMap<String, CWGroup> groups) {
|
public static HashSet<String> getCommands(ProxiedPlayer player) {
|
||||||
HashSet<String> commandList = new HashSet<>();
|
HashSet<String> commandList = new HashSet<>();
|
||||||
|
HashMap<String, CWGroup> groups = configCache.getGroupList();
|
||||||
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
|
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
|
||||||
if (s.getKey().equalsIgnoreCase("default"))
|
if (s.getKey().equalsIgnoreCase("default"))
|
||||||
commandList.addAll(s.getValue().getCommands());
|
commandList.addAll(s.getValue().getCommands());
|
||||||
@@ -92,7 +88,8 @@ public final class CommandWhitelistWaterfall extends Plugin {
|
|||||||
* @param player Bungee Player
|
* @param player Bungee Player
|
||||||
* @return subcommands unavailable for the player
|
* @return subcommands unavailable for the player
|
||||||
*/
|
*/
|
||||||
public static HashSet<String> getSuggestions(ProxiedPlayer player, HashMap<String, CWGroup> groups) {
|
public static HashSet<String> getSuggestions(ProxiedPlayer player) {
|
||||||
|
HashMap<String, CWGroup> groups = configCache.getGroupList();
|
||||||
HashSet<String> suggestionList = new HashSet<>();
|
HashSet<String> suggestionList = new HashSet<>();
|
||||||
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
|
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
|
||||||
if (s.getKey().equalsIgnoreCase("default"))
|
if (s.getKey().equalsIgnoreCase("default"))
|
||||||
|
|||||||
+2
-2
@@ -29,14 +29,14 @@ public class BungeeChatEventListener implements Listener {
|
|||||||
BungeeAudiences audiences = CommandWhitelistWaterfall.getAudiences();
|
BungeeAudiences audiences = CommandWhitelistWaterfall.getAudiences();
|
||||||
|
|
||||||
String label = CommandUtil.getCommandLabel(command);
|
String label = CommandUtil.getCommandLabel(command);
|
||||||
HashSet<String> commands = CommandWhitelistWaterfall.getCommands(player, configCache.getGroupList());
|
HashSet<String> commands = CommandWhitelistWaterfall.getCommands(player);
|
||||||
if (!commands.contains(label)) {
|
if (!commands.contains(label)) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
CommandWhitelistWaterfall.getAudiences().player(player).sendMessage(MiniMessage.markdown().parse(configCache.prefix + configCache.command_denied));
|
CommandWhitelistWaterfall.getAudiences().player(player).sendMessage(MiniMessage.markdown().parse(configCache.prefix + configCache.command_denied));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
HashSet<String> bannedSubCommands = CommandWhitelistWaterfall.getSuggestions(player, configCache.getGroupList());
|
HashSet<String> bannedSubCommands = CommandWhitelistWaterfall.getSuggestions(player);
|
||||||
for (String bannedSubCommand : bannedSubCommands) {
|
for (String bannedSubCommand : bannedSubCommands) {
|
||||||
if (command.toLowerCase().substring(1).startsWith(bannedSubCommand)) {
|
if (command.toLowerCase().substring(1).startsWith(bannedSubCommand)) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
|
|||||||
+1
-1
@@ -17,7 +17,7 @@ public class WaterfallDefineCommandsListener implements Listener {
|
|||||||
if (player.hasPermission("commandwhitelist.bypass"))
|
if (player.hasPermission("commandwhitelist.bypass"))
|
||||||
return;
|
return;
|
||||||
HashMap<String, Command> commandHashMap = new HashMap<>();
|
HashMap<String, Command> commandHashMap = new HashMap<>();
|
||||||
CommandWhitelistWaterfall.getCommands(player, CommandWhitelistWaterfall.getConfigCache().getGroupList()).forEach(cmdName ->
|
CommandWhitelistWaterfall.getCommands(player).forEach(cmdName ->
|
||||||
CommandWhitelistWaterfall.getPlugin().getProxy().getPluginManager().getCommands()
|
CommandWhitelistWaterfall.getPlugin().getProxy().getPluginManager().getCommands()
|
||||||
.stream()
|
.stream()
|
||||||
.filter(commandEntry -> cmdName.equalsIgnoreCase(commandEntry.getValue().getName()))
|
.filter(commandEntry -> cmdName.equalsIgnoreCase(commandEntry.getValue().getName()))
|
||||||
|
|||||||
@@ -74,7 +74,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.kyori</groupId>
|
<groupId>net.kyori</groupId>
|
||||||
<artifactId>adventure-api</artifactId>
|
<artifactId>adventure-api</artifactId>
|
||||||
<version>4.7.0</version>
|
<version>4.8.0</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
Reference in New Issue
Block a user