Skip to content

Commit

Permalink
Whitelist function implemented
Browse files Browse the repository at this point in the history
Additional changes:
- Command changed to /whitelist or /wl
  (to replace vanilla one)
- Code refactorization and change of permission system
- First ALPHA Snapshot release: 0.1-SNAPSHOT
  • Loading branch information
Polda18 committed Dec 11, 2021
1 parent d59abcb commit f382c0b
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 74 deletions.
13 changes: 9 additions & 4 deletions src/main/java/me/polda18/betterwhitelist/BetterWhitelist.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.polda18.betterwhitelist.commands.WhitelistCommand;
import me.polda18.betterwhitelist.config.Language;
import me.polda18.betterwhitelist.config.Whitelist;
import me.polda18.betterwhitelist.events.EventsListener;
import me.polda18.betterwhitelist.utils.InvalidEntryException;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
Expand Down Expand Up @@ -97,15 +98,19 @@ public void onEnable() {
this.getLogger().log(Level.INFO, "");

// Register new command
this.getCommand("betterwhitelist").setExecutor(new WhitelistCommand(this));
this.getCommand("betterwhitelist").setTabCompleter(new Autocomplete(this));
this.getCommand("whitelist").setExecutor(new WhitelistCommand(this));
this.getCommand("whitelist").setTabCompleter(new Autocomplete(this));

// Get events listener
this.getServer().getPluginManager().registerEvents(new EventsListener(this), this);

// Get default configurations from resources if non-existent
this.getLogger().log(Level.INFO, "Loading config...");
this.saveDefaultConfig();
// Get language files from resources if non-existent
this.saveDefaultLanguage("cs.yml");
this.saveDefaultLanguage("en.yml");
for(String language : Language.DEFAULT_LANGUAGE_FILES) {
this.saveDefaultLanguage(language);
}
// New languages be put before this line
this.getLogger().log(Level.INFO, "Config loaded");

Expand Down
77 changes: 46 additions & 31 deletions src/main/java/me/polda18/betterwhitelist/commands/Autocomplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,74 @@ public Autocomplete(BetterWhitelist plugin) {
ArrayList<String> list = new ArrayList<>();
boolean isConsole = sender instanceof ConsoleCommandSender;

if(!command.getName().equalsIgnoreCase("betterwhitelist")
|| (!isConsole && !sender.hasPermission("betterwhitelist.execute"))) {
if(!command.getName().equalsIgnoreCase("whitelist")) {
return null;
// Make sure this autocomplete only returns when the command invoked belongs to this plugin
// and the command sender has appropriate permissions to run the command.
// Since this plugin is one-purpose, only one command belongs in here.
}

switch(args.length) {
case 1: // First argument
list.add("on");
list.add("off");
list.add("add");
list.add("remove");
list.add("reload");
list.add("list");
list.add("lang");
list.add("import");
if(isConsole || sender.hasPermission("betterwhitelist.admin"))
list.add("on"); // Add to list only if command sender has admin permission
if(isConsole || sender.hasPermission("betterwhitelist.admin"))
list.add("off"); // Add to list only if command sender has admin permission
if(isConsole || sender.hasPermission("betterwhitelist.add"))
list.add("add"); // Add to list only if command sender has add permission
if(isConsole || sender.hasPermission("betterwhitelist.remove"))
list.add("remove"); // Add to list only if command sender has remove permission
if(isConsole || sender.hasPermission("betterwhitelist.admin"))
list.add("reload"); // Add to list only if command sender has admin permission
if(isConsole || sender.hasPermission("betterwhitelist.list"))
list.add("list"); // Add to list only if command sender has list permission
if(isConsole || sender.hasPermission("betterwhitelist.admin"))
list.add("lang"); // Add to list only if command sender has admin permission
if(isConsole || sender.hasPermission("betterwhitelist.admin"))
list.add("import"); // Add to list only if command sender has admin permission
if(isConsole || sender.hasPermission("betterwhitelist.admin"))
list.add("status"); // Add to list only if command sender has admin permission

if(list.isEmpty()) {
return null; // If the list is empty (sender has no permission), no list is given
}
break;
case 2: // Second argument (if available)
switch(args[0].toLowerCase()) {
case "add": // User wants to add a player to whitelist
list.add("ExamplePlayer"); // Give an example player :)
if(isConsole || sender.hasPermission("betterwhitelist.add")) {
list.add("<ExamplePlayer>"); // Give an example player :)
} else {
return null;
}
break;
case "remove": // User wants to remove player from whitelist
for (String player :
plugin.getWhitelist().getConfig()
.getConfigurationSection("").getKeys(false)) {
list.add(player); // Get all whitelisted players
if(isConsole || sender.hasPermission("betterwhitelist.remove")) {
for (String player :
plugin.getWhitelist().getConfig().getKeys(false)) {
list.add(player); // Get all whitelisted players
}
} else {
return null;
}
break;
case "lang": // User wants to view or change set language
// Give a list of available languages
for(String lang_code: plugin.listAvailableLanguages().keySet()) {
list.add(lang_code);
if(isConsole || sender.hasPermission("betterwhitelist.admin")) {
for(String lang_code: plugin.listAvailableLanguages().keySet()) {
list.add(lang_code);
}
} else {
return null;
}
break;
case "import": // User wants to import vanilla whitelist
// No further informations can be provided here, fall back to default
case "on": // User wants to enable whitelist
// No further informations can be provided here, fall back to default
case "off": // User wants to disable whitelist
// No further informations can be provided here, fall back to default
case "reload": // User wants to reload whitelist from a config
// No further informations can be provided here, fall back to default
case "list": // User wants to list players in whitelist
// No further informations can be provided here, fall back to default
default: // User entered invalid argument
// Invalid argument provided, default to no list given
default: // User entered invalid subcommand or subcommand has no arguments
// Invalid subcommand provided or subcommand has no arguments, default to no list given
return null;
}
break;
default:
// No further informations provided, return empty list
// No further informations provided, return no list
return null;
}

return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.checkerframework.checker.units.qual.C;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -49,27 +50,36 @@ private void getUsage(CommandSender sender) {
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
boolean isConsole = sender instanceof ConsoleCommandSender;

if(!command.getName().equalsIgnoreCase("betterwhitelist")
&& !label.equalsIgnoreCase("betterwhitelist")
&& !label.equalsIgnoreCase("bw")) {
if(!command.getName().equalsIgnoreCase("whitelist")
&& !label.equalsIgnoreCase("whitelist")
&& !label.equalsIgnoreCase("wl")) {
// Executed command isn't for this plugin
return true;
}

if(!isConsole && !sender.hasPermission("betterwhitelist.execute")) {
// Command sender is not a console and has no permission to execute this command
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.error.permission")));
return true;
}

if(args.length < 1) {
// Check any requested permission
if(!isConsole && !sender.hasPermission("betterwhitelist.admin")
&& !sender.hasPermission("betterwhitelist.add")
&& !sender.hasPermission("betterwhitelist.remove")
&& !sender.hasPermission("betterwhitelist.list")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.error.permission")));
return true; // Command sender has no requested permission to execute
}

// Just a command alone is sent - display usage
this.getUsage(sender);
} else {
// Resolve subcommands and their arguments
switch(args[0].toLowerCase()) {
case "on":
// Check admin permission
if(!isConsole && !sender.hasPermission("betterwhitelist.admin")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.error.permission")));
}

// Turn whitelist on
if(plugin.whitelistIsEnabled()) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
Expand All @@ -88,6 +98,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
break;
case "off":
// Check admin permission
if(!isConsole && !sender.hasPermission("betterwhitelist.admin")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.error.permission")));
break; // Command sender has no admin permission
}

// Turn whitelist off
if(plugin.whitelistIsEnabled()) {
// Set the enabled flag in plugin structure to false
Expand All @@ -105,7 +122,29 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
plugin.getLanguage().getConfig().getString("messages.error.whitelist.already-disabled")));
}
break;
case "status":
// Check status of the whitelist
if(!(sender instanceof ConsoleCommandSender)
&& !sender.hasPermission("betterwhitelist.admin")) {
// Command sender has no admin permission to execute it
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.error.permission")));
} else {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.status.msg")
.replace("(status)", (plugin.whitelistIsEnabled())
? plugin.getLanguage().getConfig().getString("messages.status.enabled")
: plugin.getLanguage().getConfig().getString("messages.status.disabled"))));
}
break;
case "add":
// Check add permission
if(!isConsole && !sender.hasPermission("betterwhitelist.add")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.error.permission")));
break; // Command sender has no add permission to execute
}

// Add player to whitelist
if(args.length < 2) {
// Player not specified
Expand Down Expand Up @@ -175,6 +214,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
break;
case "remove":
// Check remove permission
if(!isConsole && !sender.hasPermission("betterwhitelist.remove")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.error.permission")));
break; // Command sender has no remove permission to execute
}

// Remove player from whitelist
if(args.length < 2) {
// Player not specified
Expand Down Expand Up @@ -210,13 +256,29 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
break;
case "reload":
// Check admin permission
if(!isConsole && !sender.hasPermission("betterwhitelist.admin")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.error.permission")));
break; // Command sender has no admin permission to execute
}

// Reload whitelist
plugin.reloadConfig();

sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.reload")));

break;
case "list":
// Check list permission
if(!isConsole && !sender.hasPermission("betterwhitelist.list")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.error.permission")));
break; // Command sender has no list permission to execute
}

// List all players in whitelist
FileConfiguration config = plugin.getWhitelist().getConfig();
HashMap<String, HashMap<String, String>> hm = new HashMap<>();

Expand Down Expand Up @@ -254,6 +316,14 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
plugin.getLanguage().getConfig().getString("messages.list.footer")));
break;
case "lang":
// Check admin permission
if(!isConsole && !sender.hasPermission("betterwhitelist.admin")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.error.permission")));
break; // Command sender has no admin permission to execute
}

// Check or change language
if(args.length < 2) {
// Language not specified, send current language
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
Expand Down Expand Up @@ -281,6 +351,14 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
break;
case "import":
// Check admin permission
if(!isConsole && !sender.hasPermission("betterwhitelist.admin")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.error.permission")));
break; // Command sender has no admin permission to execute
}

// Import data from vanilla whitelist
Set<OfflinePlayer> vanilla_whitelist = Bukkit.getWhitelistedPlayers();

for (OfflinePlayer player : vanilla_whitelist) {
Expand All @@ -295,6 +373,16 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
plugin.getLanguage().getConfig().getString("messages.import")));
break;
default:
// Check any of requested permission
if(!isConsole && !sender.hasPermission("betterwhitelist.admin")
&& !sender.hasPermission("betterwhitelist.add")
&& !sender.hasPermission("betterwhitelist.remove")
&& !sender.hasPermission("betterwhitelist.list")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.getLanguage().getConfig().getString("messages.error.permission")));
break; // Command sender has no requested permission to execute
}

// Subcommand not recognized, display usage
this.getUsage(sender);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Language {
public static final String[] DEFAULT_LANGUAGE_FILES = {
"cs.yml",
"en.yml"
// Add new languages before this line
};

public Language(String code, FileConfiguration config, File file) {
Expand Down
Loading

0 comments on commit f382c0b

Please sign in to comment.