-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add module to register commands using plugin.yml instead of Com…
…mandMap.
- Loading branch information
Showing
5 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
60 changes: 60 additions & 0 deletions
60
bukkit/plugin/src/main/java/team.unnamed.commandflow.bukkit/BukkitCommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
bukkit/plugin/src/main/java/team.unnamed.commandflow.bukkit/BukkitPluginCommandManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...it/plugin/src/main/java/team.unnamed.commandflow.bukkit/DefaultBukkitCommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters