Skip to content

Commit

Permalink
feat: Add module to register commands using plugin.yml instead of Com…
Browse files Browse the repository at this point in the history
…mandMap.
  • Loading branch information
FixedDev committed Jan 2, 2024
1 parent a50d639 commit ba24753
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
23 changes: 23 additions & 0 deletions bukkit/plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>team.unnamed</groupId>
<artifactId>commandflow-bukkit</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>commandflow-bukkit-plugin</artifactId>

<dependencies>
<dependency>
<groupId>team.unnamed</groupId>
<artifactId>commandflow-bukkit-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package team.unnamed.commandflow.bukkit;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import team.unnamed.commandflow.CommandManager;
import team.unnamed.commandflow.Namespace;
import team.unnamed.commandflow.exception.CommandException;

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

public interface BukkitCommandExecutor extends CommandExecutor, TabCompleter {
@Override
default boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
List<String> argumentLine = new ArrayList<>();

argumentLine.add(label);
argumentLine.addAll(Arrays.asList(args));

Namespace namespace = Namespace.create();

namespace.setObject(CommandSender.class, BukkitCommonConstants.SENDER_NAMESPACE, sender);
namespace.setObject(String.class, "label", label);

try {
return commandManager().execute(namespace, argumentLine);
} catch (CommandException e) {
Throwable exceptionToSend = e;

Throwable cause = e.getCause();

if (cause instanceof org.bukkit.command.CommandException) {
throw (org.bukkit.command.CommandException) cause;
}

if (cause != null) {
exceptionToSend = cause;
}

throw new org.bukkit.command.CommandException("An unexpected exception occurred while executing the command " + label, exceptionToSend);
}
}

@Override
default List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
List<String> argumentLine = new ArrayList<>(Arrays.asList(args));
argumentLine.add(0, alias);

Namespace namespace = Namespace.create();
namespace.setObject(CommandSender.class, BukkitCommonConstants.SENDER_NAMESPACE, sender);

return commandManager().getSuggestions(namespace, argumentLine);
}

CommandManager commandManager();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package team.unnamed.commandflow.bukkit;

import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import team.unnamed.commandflow.CommandManager;
import team.unnamed.commandflow.SimpleCommandManager;
import team.unnamed.commandflow.command.Command;

public class BukkitPluginCommandManager extends BukkitCommandManager {

protected final String fallbackPrefix;

protected BukkitCommandExecutor commandExecutor;

public BukkitPluginCommandManager(CommandManager delegate, @NotNull BukkitCommandExecutor commandExecutor, String fallbackPrefix) {
super(delegate);
this.fallbackPrefix = fallbackPrefix;
this.commandExecutor = commandExecutor;
}

public BukkitPluginCommandManager(String fallbackPrefix) {
//noinspection DataFlowIssue
this(new SimpleCommandManager(), null, fallbackPrefix);

commandExecutor = new DefaultBukkitCommandExecutor(this);
setAuthorizer(new BukkitAuthorizer());
getTranslator().setProvider(new BukkitDefaultTranslationProvider());
getTranslator().setConverterFunction(LegacyComponentSerializer.legacyAmpersand()::deserialize);
}

@Override
protected void _register(Command command) {
PluginCommand pluginCommand = Bukkit.getPluginCommand(command.getName());

if (pluginCommand != null) {
pluginCommand.setExecutor(commandExecutor);
pluginCommand.setTabCompleter(commandExecutor);
}

for (String alias : command.getAliases()) {
registerCommand(fallbackPrefix + ":" + alias, command);
}

registerCommand(fallbackPrefix + ":" + command.getName(), command);
}

@Override
protected void _unregister(Command command) {
PluginCommand pluginCommand = Bukkit.getPluginCommand(command.getName());

if (pluginCommand != null) {
pluginCommand.setExecutor(null);
pluginCommand.setTabCompleter(null);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package team.unnamed.commandflow.bukkit;

import team.unnamed.commandflow.CommandManager;

public class DefaultBukkitCommandExecutor implements BukkitCommandExecutor {

private final CommandManager commandManager;

public DefaultBukkitCommandExecutor(CommandManager commandManager) {
this.commandManager = commandManager;
}

@Override
public CommandManager commandManager() {
return commandManager;
}
}
1 change: 1 addition & 0 deletions bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<module>commandmap</module>
<module>common</module>
<module>paper</module>
<module>plugin</module>
</modules>

<repositories>
Expand Down

0 comments on commit ba24753

Please sign in to comment.