Skip to content

Commit

Permalink
Renamed Command Class
Browse files Browse the repository at this point in the history
  • Loading branch information
CptGummiball committed Sep 12, 2024
1 parent f2dc0f6 commit 488a148
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/main/java/org/cptgummiball/whitelister/Whitelister.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public void onEnable() {
startWebServer();

// Register commands and tab completer
WhitelistCommand whitelistCommand = new WhitelistCommand(this);
this.getCommand("whitelister").setExecutor(whitelistCommand);
this.getCommand("whitelister").setTabCompleter(whitelistCommand); // Register tab completer
WhitelisterCommand whitelisterCommand = new WhitelisterCommand(this);
this.getCommand("whitelister").setExecutor(whitelisterCommand);
this.getCommand("whitelister").setTabCompleter(whitelisterCommand); // Register tab completer

// Register event listener for player join notifications
getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
Expand Down Expand Up @@ -62,4 +62,5 @@ public WhitelistManager getWhitelistManager() {
public FileConfiguration getConfig(String string) {
return getConfig();
}
}
}

80 changes: 80 additions & 0 deletions src/main/java/org/cptgummiball/whitelister/WhitelisterCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.cptgummiball.whitelister;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class WhitelisterCommand implements CommandExecutor, TabCompleter {
private WhitelistManager whitelistManager;
private final Whitelister plugin;

public WhitelisterCommand(Whitelister plugin) {
this.plugin = plugin;
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
Player player = (Player) sender;
if (args.length == 0 || args[0].equalsIgnoreCase("list")) {

listPendingApplications(player);
} else if (args[0].equalsIgnoreCase("accept") && args.length == 2) {
acceptApplication(player, args[1]);
}
return true;
}
return true;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
if (!(sender instanceof Player)) {
return null;
}

List<String> completions = new ArrayList<>();
if (args.length == 1) {
completions.add("list");
completions.add("accept");
} else if (args.length == 2 && "accept".equalsIgnoreCase(args[0])) {
// Provide tab completion for usernames
completions.addAll(whitelistManager.getPendingApplications().keySet());
}

return completions;
}

private void listPendingApplications(Player player) {
String noApp = plugin.getConfig().getString("messages.no_applications", null);
String appList = plugin.getConfig().getString("messages.application_list", null);
Map<String, UUID> applications = whitelistManager.getPendingApplications();
if (applications.isEmpty()) {
player.sendMessage(noApp);
} else {
player.sendMessage(appList);
for (String username : applications.keySet()) {
player.sendMessage("- " + username);
}
}
}

private void acceptApplication(Player player, String username) {
String appAccept = plugin.getConfig().getString("messages.application_accepted", null);
String noAppFound = plugin.getConfig().getString("messages.no_application_found", null);
Map<String, UUID> applications = whitelistManager.getPendingApplications();
if (applications.containsKey(username)) {
whitelistManager.acceptApplication(username);
player.sendMessage(appAccept.replace("{username}", username));
} else {
player.sendMessage(noAppFound.replace("{username}", username));
}
}
}

0 comments on commit 488a148

Please sign in to comment.