Skip to content

Commit

Permalink
Rework the new system (I think it is the best way)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonimatasDEV committed Sep 10, 2024
1 parent 5244e2e commit b364c5e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pluginVersion=1.5.7
pluginVersion=1.5.8

minecraftVersion=1.20.1
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,34 @@ public void onPluginEnable(PluginEnableEvent event) {

@EventHandler(priority = EventPriority.MONITOR)
public void onCommandPreProcess(PlayerCommandPreprocessEvent event) {
Map<String, Command> commands = CommandManager.getCommands();
String commandStringWithVar = event.getMessage().split(" ")[0];
String commandString = commandStringWithVar.replaceFirst("/", "");

Command command = commands.get(commandString);
Command command = CommandManager.getCommands().get(commandString);

if (command == null) return;

List<String> possibleCommands = new ArrayList<>();

for (String plugin : CommandManager.pluginMap.keySet()) {
Map<String, Command> commandMap = CommandManager.pluginMap.get(plugin);

if (PerWorldUtils.getDisabledWorlds(plugin).contains(event.getPlayer().getWorld().getName())) {
continue;
}

boolean isHere = false;
for (Command cmd : commandMap.values()) {
if (cmd.getName().equalsIgnoreCase(commandString) || cmd.getAliases().contains(commandString)) {
isHere = true;
break;
}
}

if (!isHere) continue;
String possibleCommand = CommandManager.getCommand(command, commandString, CommandManager.pluginMap.get(plugin));

String commandStr;

if (commandString.contains(":")) {
commandStr = commandString;
} else {
commandStr = plugin.toLowerCase(Locale.ENGLISH) + ":" + commandString;
}

if (CommandManager.getCommands().get(commandStr) == null) {
possibleCommands.add(commandString);
} else {
possibleCommands.add(commandStr);
if (possibleCommand != null) {
possibleCommands.add(possibleCommand);
}
}

String possibleDefaultCommand = CommandManager.getCommand(command, commandString, CommandManager.defaultCommands);

for (Command defaultCommand : CommandManager.defaultCommands) {
if (defaultCommand.getName().equals(commandString) || defaultCommand.getAliases().contains(commandString)) {
possibleCommands.add(defaultCommand.getName());
}
if (possibleDefaultCommand != null) {
possibleCommands.add(possibleDefaultCommand);
}

if (possibleCommands.isEmpty() && commands.containsKey(commandString)) {
if (possibleCommands.isEmpty() && CommandManager.getCommands().containsKey(commandString)) {
event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&',
Objects.requireNonNull(PerWorldPlugins.getInstance().getConfig().getString("disabledCommandMessage")))
.replaceAll("\\{world}", event.getPlayer().getWorld().getName())
Expand All @@ -88,6 +67,5 @@ public void onCommandPreProcess(PlayerCommandPreprocessEvent event) {

event.setMessage(event.getMessage().replaceFirst(commandStringWithVar, "/" + possibleCommands.get(0)));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,27 @@
import java.util.Map;

public class CommandManager implements Listener {
public static Map<String, Map<String, Command>> pluginMap = new HashMap<>();
public static Map<String, List<Command>> pluginMap = new HashMap<>();
public static final List<Command> defaultCommands = new ArrayList<>();

public static void addPluginCommands(String plugin) {
getCommands().values().stream().filter(command -> {
if (defaultCommands.contains(command)) return false;
if (command.getClass().getName().contains("VanillaCommandWrapper")) return false;

for (Map<String, Command> commandMap : pluginMap.values()) {
if (commandMap.containsValue(command)) return false;
for (List<Command> commandMap : pluginMap.values()) {
if (commandMap.contains(command)) {
return false;
}
}

return true;
}).forEach(command -> {
Map<String, Command> commandMap = pluginMap.get(plugin);
List<Command> commandMap = pluginMap.get(plugin);

if (commandMap == null) commandMap = new HashMap<>();
if (commandMap == null) commandMap = new ArrayList<>();

commandMap.put(command.getName(), command);
commandMap.add(command);
pluginMap.put(plugin, commandMap);
});
}
Expand Down Expand Up @@ -74,4 +76,25 @@ public static CommandMap getCommandMap() {
throw new RuntimeException("Error on get commandMap field. Report it on PerWorldPlugins github.");
}
}

public static String getCommand(Command command, String commandString, List<Command> commands) {
for (Command cmd : commands) {
if (cmd == command) {
return commandString;
} else {
List<String> commandNames = new ArrayList<>(cmd.getAliases());
commandNames.add(cmd.getName());

if (commandNames.contains(commandString)) {
for (String commandName : CommandManager.getCommands().keySet()) {
Command equalCommand = CommandManager.getCommands().get(commandName);
if (equalCommand != cmd) continue;
return commandName;
}
}
}
}

return null;
}
}

0 comments on commit b364c5e

Please sign in to comment.