randomized command denied message

This commit is contained in:
YouHaveTrouble
2020-11-02 01:02:26 +01:00
parent 680839c2c5
commit 992c6fbf07
7 changed files with 54 additions and 17 deletions
@@ -0,0 +1,26 @@
package eu.endermite.commandwhitelist.api;
import java.util.List;
import java.util.Random;
public class RandomStuff {
/**
*
* @param list List of strings to pick a random one from
* @param single String that will be used as fallback
* @return Randomized message
*/
public static String getMessage(List<String> list, String single) {
if (list == null || list.size() == 0) {
return single;
}
Random random = new Random();
int r = random.nextInt(list.size());
return list.get(r);
}
}
@@ -8,13 +8,15 @@ import java.util.List;
public class BungeeConfigCache {
private HashMap<String, List<String>> permList = new HashMap<>();
private String prefix, commandDenied, noPermission, noSubCommand, configReloaded;
private final HashMap<String, List<String>> permList = new HashMap<>();
private final String prefix, commandDenied, noPermission, noSubCommand, configReloaded;
private List<String> commandDeniedList;
public BungeeConfigCache(Configuration config) {
prefix = config.getString("messages.prefix");
commandDenied = config.getString("messages.command-denied");
commandDenied = config.getString("messages.command-denied", null);
commandDeniedList = config.getStringList("messages.command-denied");
noPermission = config.getString("messages.no-permission");
noSubCommand = config.getString("messages.no-such-subcommand");
configReloaded = config.getString("messages.config-reloaded");
@@ -28,9 +30,11 @@ public class BungeeConfigCache {
public HashMap<String, List<String>> getPermList() {
return permList;
}
public String getPrefix() {return prefix;}
public String getCommandDenied() {return commandDenied;}
public List<String> getCommandDeniedList() {
return commandDeniedList;
}
public String getNoPermission() {return noPermission;}
public String getNoSubCommand() {return noSubCommand;}
public String getConfigReloaded() {return configReloaded;}
@@ -1,10 +1,14 @@
package eu.endermite.commandwhitelist.bungee.listeners;
import eu.endermite.commandwhitelist.api.RandomStuff;
import eu.endermite.commandwhitelist.bungee.CommandWhitelistBungee;
import net.md_5.bungee.api.ChatColor;
import eu.endermite.commandwhitelist.bungee.config.BungeeConfigCache;
import eu.endermite.commandwhitelist.spigot.CommandWhitelist;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import org.bukkit.ChatColor;
import java.util.List;
import java.util.Map;
@@ -47,7 +51,8 @@ public class BungeeChatEventListener implements Listener {
}
if (!found) {
event.setCancelled(true);
player.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelistBungee.getConfigCache().getPrefix() + CommandWhitelistBungee.getConfigCache().getCommandDenied()));
BungeeConfigCache config = CommandWhitelistBungee.getConfigCache();
player.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + RandomStuff.getMessage(config.getCommandDeniedList(), config.getCommandDenied())));
}
}
}
@@ -49,7 +49,7 @@ public class CommandWhitelist extends JavaPlugin {
public void reloadPluginConfig() {
saveDefaultConfig();
reloadConfig();
configCache = new ConfigCache();
configCache = new ConfigCache(getConfig());
}
public void reloadPluginConfig(CommandSender sender) {
@@ -1,6 +1,5 @@
package eu.endermite.commandwhitelist.spigot.config;
import eu.endermite.commandwhitelist.spigot.CommandWhitelist;
import org.bukkit.configuration.Configuration;
import java.util.HashMap;
@@ -11,13 +10,13 @@ public class ConfigCache {
private HashMap<String, List<String>> permList = new HashMap<>();
private String prefix, commandDenied, noPermission, noSubCommand, configReloaded;
private List<String> commandDeniedList;
public ConfigCache() {
Configuration config = CommandWhitelist.getPlugin().getConfig();
public ConfigCache(Configuration config) {
prefix = config.getString("messages.prefix");
commandDenied = config.getString("messages.command-denied");
commandDenied = config.getString("messages.command-denied", null);
commandDeniedList = config.getStringList("messages.command-denied");
noPermission = config.getString("messages.no-permission");
noSubCommand = config.getString("messages.no-such-subcommand");
configReloaded = config.getString("messages.config-reloaded");
@@ -32,11 +31,11 @@ public class ConfigCache {
return permList;
}
public List<String> getPerm(String s) {
return permList.get(s);
}
public String getPrefix() {return prefix;}
public String getCommandDenied() {return commandDenied;}
public List<String> getCommandDeniedList() {
return commandDeniedList;
}
public String getNoPermission() {return noPermission;}
public String getNoSubCommand() {return noSubCommand;}
public String getConfigReloaded() {return configReloaded;}
@@ -1,6 +1,8 @@
package eu.endermite.commandwhitelist.spigot.listeners;
import eu.endermite.commandwhitelist.api.RandomStuff;
import eu.endermite.commandwhitelist.spigot.CommandWhitelist;
import eu.endermite.commandwhitelist.spigot.config.ConfigCache;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -35,7 +37,8 @@ public class PlayerCommandPreProcessListener implements Listener {
}
}
event.setCancelled(true);
player.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + CommandWhitelist.getConfigCache().getCommandDenied()));
ConfigCache config = CommandWhitelist.getConfigCache();
player.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + RandomStuff.getMessage(config.getCommandDeniedList(), config.getCommandDenied())));
}
}