bungee command blocker (no tab complete block yet)

This commit is contained in:
youhavetrouble
2020-07-31 15:32:43 +02:00
parent 86faeb8aeb
commit 6d2eb27ef3
6 changed files with 191 additions and 1 deletions
@@ -0,0 +1,68 @@
package eu.endermite.commandwhitelist;
import com.google.common.io.ByteStreams;
import eu.endermite.commandwhitelist.config.BungeeConfigCache;
import eu.endermite.commandwhitelist.listeners.BungeeChatEventListener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public final class CommandWhitelistBungee extends Plugin {
private static eu.endermite.commandwhitelist.CommandWhitelistBungee plugin;
private static BungeeConfigCache configCache;
@Override
public void onEnable() {
plugin = this;
loadConfig();
this.getProxy().getPluginManager().registerListener(this, new BungeeChatEventListener());
}
public static eu.endermite.commandwhitelist.CommandWhitelistBungee getPlugin() {
return plugin;
}
public static BungeeConfigCache getConfigCache() {
return configCache;
}
public void loadConfig() {
try {
if (!getDataFolder().exists()) {
getDataFolder().mkdir();
}
File file = new File(getDataFolder(), "config.yml");
if (!file.exists()) {
file.createNewFile();
try (InputStream in = getResourceAsStream("bungeeconfig.yml");
OutputStream out = new FileOutputStream(file)) {
ByteStreams.copy(in, out);
}
}
final Configuration config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, new File(getDataFolder(), "config.yml"));
configCache = new BungeeConfigCache(config);
} catch (Exception e) {
e.printStackTrace();
}
}
public void loadConfigAsync() {
getProxy().getScheduler().runAsync(this, this::loadConfig);
}
}
@@ -0,0 +1,38 @@
package eu.endermite.commandwhitelist.config;
import net.md_5.bungee.config.Configuration;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
public class BungeeConfigCache {
private HashMap<String, List<String>> permList = new HashMap<>();
private String prefix, commandDenied, noPermission, noSubCommand, configReloaded;
public BungeeConfigCache(Configuration config) {
prefix = config.getString("messages.prefix");
commandDenied = config.getString("messages.command-denied");
noPermission = config.getString("messages.no-permission");
noSubCommand = config.getString("messages.no-such-subcommand");
configReloaded = config.getString("messages.config-reloaded");
Collection<String> perms = config.getSection("commands").getKeys();
for (String s : perms) {
this.permList.put(s, config.getStringList("commands."+s));
}
}
public HashMap<String, List<String>> getPermList() {
return permList;
}
public String getPrefix() {return prefix;}
public String getCommandDenied() {return commandDenied;}
public String getNoPermission() {return noPermission;}
public String getNoSubCommand() {return noSubCommand;}
public String getConfigReloaded() {return configReloaded;}
}
@@ -0,0 +1,54 @@
package eu.endermite.commandwhitelist.listeners;
import eu.endermite.commandwhitelist.CommandWhitelistBungee;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import java.util.List;
import java.util.Map;
public class BungeeChatEventListener implements Listener {
@EventHandler
public void onChatEvent(net.md_5.bungee.api.event.ChatEvent event) {
if (!event.isCancelled() && event.getSender() instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) event.getSender();
if (event.isProxyCommand()) {
if (player.hasPermission("commandwhitelist.bypass")) {
return;
}
String command = event.getMessage().toLowerCase();
boolean found = false;
for (Map.Entry<String, List<String>> s : CommandWhitelistBungee.getConfigCache().getPermList().entrySet()) {
if (player.hasPermission("commandwhitelist.commands." + s.getKey())) {
for (String comm : s.getValue()) {
comm = comm.toLowerCase();
if (command.equalsIgnoreCase("/"+comm)) {
found = true;
break;
}
else if (command.startsWith("/" + comm + " ")) {
found = true;
break;
}
}
}
}
if (!found) {
event.setCancelled(true);
player.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelistBungee.getConfigCache().getPrefix() + CommandWhitelistBungee.getConfigCache().getCommandDenied()));
}
}
}
}
}
+4
View File
@@ -0,0 +1,4 @@
name: CommandWhitelistBungee
version: ${project.version}
main: eu.endermite.commandwhitelist.CommandWhitelistBungee
author: YouHaveTrouble
+20
View File
@@ -0,0 +1,20 @@
# This is bungeecord version of the config.
messages:
prefix: "CommandWhitelist > "
command-denied: "No such command."
no-permission: "&cYou don't have permission to do this."
no-such-subcommand: "&cNo subcommand by that name."
config-reloaded: "&eConfiguration reloaded."
commands:
# Permissions that control what commands players can use
# you can add unlimited amount of whitelists
# commandwhitelist.commands.default
# this will be automatically given to players by default
default:
- glist
- server
# commandwhitelist.commands.example
example:
- example