Skip to content

Commit

Permalink
Fix commands register method
Browse files Browse the repository at this point in the history
  • Loading branch information
TonimatasDEV committed Dec 1, 2023
1 parent 27bcb26 commit e8d69c4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package net.tonimatasdev.perworldplugins.api;

import net.tonimatasdev.perworldplugins.util.PerWorldUtils;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.plugin.Plugin;

import java.util.ArrayList;
import java.util.List;

public abstract class PerWorldCommand extends Command {
public abstract class PerWorldCommand extends Command implements PluginIdentifiableCommand {
protected Plugin plugin;
protected List<String> disabledWorlds;

public PerWorldCommand(Command command, Plugin plugin) {
// Add all command information.
super(command.getName());
setAliases(command.getAliases());
setDescription(command.getDescription());
setLabel(command.getLabel());
setName(command.getName());
setPermission(command.getPermission());
setPermissionMessage(command.getPermissionMessage());
setUsage(command.getUsage());
super(command.getName(), command.getDescription(), command.getUsage(), command.getAliases());

// Add plugin and disabled worlds.
this.plugin = command instanceof PluginCommand ? ((PluginCommand) command).getPlugin() : plugin;
Expand Down Expand Up @@ -59,6 +54,21 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
return command.tabComplete(sender, alias, args);
}

@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args, Location location) throws IllegalArgumentException {
return command.tabComplete(sender, alias, args, location);
}

@Override
public boolean testPermission(CommandSender target) {
return command.testPermission(target);
}

@Override
public boolean testPermissionSilent(CommandSender target) {
return command.testPermissionSilent(target);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package net.tonimatasdev.perworldplugins.manager;

import net.tonimatasdev.perworldplugins.PerWorldPlugins;
import net.tonimatasdev.perworldplugins.api.PerWorldCommand;
import net.tonimatasdev.perworldplugins.command.PrimaryCommand;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.SimpleCommandMap;
Expand All @@ -11,34 +9,39 @@
import org.bukkit.plugin.SimplePluginManager;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

public class CommandManager implements Listener {
private static final List<String> defaultCommands = Arrays.asList("version", "timings", "reload", "plugins", "tps", "mspt", "paper", "spigot", "restart", "perworldplugins");
private static final Map<String, PerWorldCommand> perWorldCommands = new HashMap<>();
private static final List<PerWorldCommand> commands = new ArrayList<>();

public static void init() {
// Replace all Commands to PerWorldCommands.
perWorldCommands.keySet().forEach(key -> getCommands().replace(key, perWorldCommands.get(key)));

for (PerWorldCommand command : commands) {
// Replace command for the PerWorldCommand.
replace(command);
}

// Set the blocked worlds to the commands.
setWorldsToCommands();
perWorldCommands.clear();
commands.clear();
}

public static void addPluginCommands(Plugin plugin) {
// Get all keys.
for (String commandKey : getCommands().keySet()) {
// Get the command from the key.
Command command = getCommands().get(commandKey);
// Check if it is default command, PerWorldCommand or is a registered key.
if (defaultCommands.contains(command.getName()) || command instanceof PerWorldCommand || perWorldCommands.containsKey(commandKey)) continue;
// Get and add a PerWorldCommand to perWorldCommands map.
perWorldCommands.put(commandKey, PerWorldCommand.get(command, plugin));
List<String> keys = new ArrayList<>(getCommands().keySet());

for (String key : keys) {
// Get PerWorldCommand and add to perWorldCommands list.
Command command = getCommands().get(key);

// If a default command or PerWorldCommand, continue.
if (defaultCommands.contains(command.getName()) || command instanceof PerWorldCommand) continue;
// If the command are registered, continue.
if (commands.stream().anyMatch(perWorldCommand -> perWorldCommand.getName().equals(command.getName()) || perWorldCommand.getAliases().contains(command.getName()))) continue;

// Get and add PerWorldCommand.
commands.add(PerWorldCommand.get(command, plugin));
}
}

Expand All @@ -53,6 +56,21 @@ public static void setWorldsToCommands() {
}
}

public static void replace(PerWorldCommand command) {
// Replace command name.
getCommands().replace(command.getName(), command);
// Replace plugin + command name.
getCommands().replace(command.getPlugin().getName().toLowerCase(Locale.ENGLISH) + ":" + command.getName(), command);

// Replace all aliases.
for (String alias : command.getAliases()) {
// Replace alias.
getCommands().replace(alias, command);
// Replace plugin + alias.
getCommands().replace(command.getPlugin().getName().toLowerCase(Locale.ENGLISH) + ":" + alias, command);
}
}

@SuppressWarnings("unchecked")
public static Map<String, Command> getCommands() {
try {
Expand Down

0 comments on commit e8d69c4

Please sign in to comment.