From 14f4a7574c3676eb5d7d0553fe40fd36a7437f05 Mon Sep 17 00:00:00 2001 From: youhavetrouble Date: Fri, 10 Jul 2020 23:09:45 +0200 Subject: [PATCH] Initial commit --- .gitignore | 113 ++++++++++++++++++ pom.xml | 75 ++++++++++++ .../commandwhitelist/CommandWhitelist.java | 36 ++++++ .../commandwhitelist/config/ConfigCache.java | 35 ++++++ .../listeners/PlayerCommandPreProcess.java | 39 ++++++ .../listeners/PlayerCommandSend.java | 33 +++++ src/main/resources/config.yml | 20 ++++ src/main/resources/plugin.yml | 11 ++ 8 files changed, 362 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/eu/endermite/commandwhitelist/CommandWhitelist.java create mode 100644 src/main/java/eu/endermite/commandwhitelist/config/ConfigCache.java create mode 100644 src/main/java/eu/endermite/commandwhitelist/listeners/PlayerCommandPreProcess.java create mode 100644 src/main/java/eu/endermite/commandwhitelist/listeners/PlayerCommandSend.java create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4788b4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,113 @@ +# User-specific stuff +.idea/ + +*.iml +*.ipr +*.iws + +# IntelliJ +out/ + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +target/ + +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next + +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.flattened-pom.xml + +# Common working directory +run/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..c9fc1d7 --- /dev/null +++ b/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + + eu.endermite + CommandWhitelist + 1.0-SNAPSHOT + jar + + CommandWhitelist + + Control what commands players can use + + 1.8 + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.2 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + + + + spigotmc-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + + + + org.spigotmc + spigot-api + 1.16.1-R0.1-SNAPSHOT + provided + + + diff --git a/src/main/java/eu/endermite/commandwhitelist/CommandWhitelist.java b/src/main/java/eu/endermite/commandwhitelist/CommandWhitelist.java new file mode 100644 index 0000000..c1ba4fc --- /dev/null +++ b/src/main/java/eu/endermite/commandwhitelist/CommandWhitelist.java @@ -0,0 +1,36 @@ +package eu.endermite.commandwhitelist; + +import eu.endermite.commandwhitelist.config.ConfigCache; +import eu.endermite.commandwhitelist.listeners.PlayerCommandPreProcess; +import eu.endermite.commandwhitelist.listeners.PlayerCommandSend; +import org.bukkit.plugin.java.JavaPlugin; + +public final class CommandWhitelist extends JavaPlugin { + + private static CommandWhitelist commandWhitelist; + private static ConfigCache configCache; + + @Override + public void onEnable() { + + commandWhitelist = this; + reloadPluginConfig(); + + getServer().getPluginManager().registerEvents(new PlayerCommandPreProcess(), this); + getServer().getPluginManager().registerEvents(new PlayerCommandSend(), this); + + } + + @Override + public void onDisable() { + // Plugin shutdown logic + } + + public void reloadPluginConfig() { + saveDefaultConfig(); + configCache = new ConfigCache(this.getConfig()); + } + + public static CommandWhitelist getPlugin() {return commandWhitelist;} + public static ConfigCache getConfigCache() {return configCache;} +} diff --git a/src/main/java/eu/endermite/commandwhitelist/config/ConfigCache.java b/src/main/java/eu/endermite/commandwhitelist/config/ConfigCache.java new file mode 100644 index 0000000..1e5d849 --- /dev/null +++ b/src/main/java/eu/endermite/commandwhitelist/config/ConfigCache.java @@ -0,0 +1,35 @@ +package eu.endermite.commandwhitelist.config; + +import eu.endermite.commandwhitelist.CommandWhitelist; +import org.bukkit.configuration.file.FileConfiguration; + +import java.util.HashMap; +import java.util.List; +import java.util.Set; + +public class ConfigCache { + + private final HashMap> permList = new HashMap<>(); + private final String prefix; + private final String commandDenied; + + public ConfigCache(FileConfiguration yamlConfiguration) { + Set perms = yamlConfiguration.getConfigurationSection("commands").getKeys(false); + for (String s : perms) { + this.permList.put(s, CommandWhitelist.getPlugin().getConfig().getStringList("commands."+s)); + } + + this.prefix = CommandWhitelist.getPlugin().getConfig().getString("messages.prefix"); + this.commandDenied = CommandWhitelist.getPlugin().getConfig().getString("messages.command-denied"); + } + + public HashMap> getPermList() { + return permList; + } + + public List getPerm(String s) { + return permList.get(s); + } + public String getPrefix() {return prefix;} + public String getCommandDenied() {return commandDenied;} +} diff --git a/src/main/java/eu/endermite/commandwhitelist/listeners/PlayerCommandPreProcess.java b/src/main/java/eu/endermite/commandwhitelist/listeners/PlayerCommandPreProcess.java new file mode 100644 index 0000000..c617c9f --- /dev/null +++ b/src/main/java/eu/endermite/commandwhitelist/listeners/PlayerCommandPreProcess.java @@ -0,0 +1,39 @@ +package eu.endermite.commandwhitelist.listeners; + +import eu.endermite.commandwhitelist.CommandWhitelist; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; + +import java.util.List; +import java.util.Map; + +public class PlayerCommandPreProcess implements Listener { + + @EventHandler(priority = EventPriority.LOWEST) + public void PlayerCommandSendEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent event) { + Player player = event.getPlayer(); + + if (player.hasPermission("commandwhitelist.bypass:")) { + return; + } + + String command = event.getMessage(); + + for (Map.Entry> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) { + if (player.hasPermission("commandwhitelist.commands." + s.getKey())) { + for (String comm : s.getValue()) { + if (command.startsWith("/" + comm)) { + return; + } + } + } + } + event.setCancelled(true); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + " " + CommandWhitelist.getConfigCache().getCommandDenied())); + + } + +} diff --git a/src/main/java/eu/endermite/commandwhitelist/listeners/PlayerCommandSend.java b/src/main/java/eu/endermite/commandwhitelist/listeners/PlayerCommandSend.java new file mode 100644 index 0000000..e819ca1 --- /dev/null +++ b/src/main/java/eu/endermite/commandwhitelist/listeners/PlayerCommandSend.java @@ -0,0 +1,33 @@ +package eu.endermite.commandwhitelist.listeners; + +import eu.endermite.commandwhitelist.CommandWhitelist; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; + +import java.util.*; + +public class PlayerCommandSend implements Listener { + + @EventHandler(priority = EventPriority.HIGH) + public void PlayerCommandSendEvent(org.bukkit.event.player.PlayerCommandSendEvent event) { + Player player = event.getPlayer(); + + if (player.hasPermission("commandwhitelist.bypass:")) { + return; + } + + List commandList = new ArrayList<>(); + + for (Map.Entry> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) { + if (player.hasPermission("commandwhitelist.commands."+s.getKey())) { + commandList.addAll(s.getValue()); + } + } + + event.getCommands().removeIf((cmd) -> !commandList.contains(cmd)); + + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..f721634 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,20 @@ +messages: + prefix: "CommandWhitelist >" + command-denied: "No such command." + +commands: + default: + - ? + - help + - spawn + - bal + - balance + - baltop + - pay + - r + - msg + - tpa + - tpahere + - tpaccept + - tpdeny + - warp \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..2e2df30 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,11 @@ +name: CommandWhitelist +version: ${project.version} +main: eu.endermite.commandwhitelist.CommandWhitelist +api-version: 1.16 +authors: [YouHaveTrouble] +description: Control what commands players can use +permissions: + commandwhitelist.bypass: + default: OP + commandwhitelist.commands.default: + default: true \ No newline at end of file