Designing abstraction layers for commands.

This commit is contained in:
Aust1n46 2022-03-03 18:24:52 -06:00
parent 54a4504713
commit 6f830190a6
5 changed files with 34 additions and 24 deletions

View File

@ -269,6 +269,7 @@ public class CommandController implements TabExecutor {
}
}, 0);
registerCommand("channel", channel);
}

View File

@ -30,7 +30,7 @@ public class Channel extends PlayerCommand {
}
@Override
public void execute(final Player player, final String commandLabel, final String[] args) {
protected void executeCommand(final Player player, final String commandLabel, final String[] args) {
final VentureChatPlayer mcp = playerApiService.getOnlineMineverseChatPlayer(player);
if (args.length > 0) {
if (!configService.isChannel(args[0])) {

View File

@ -5,20 +5,21 @@ import org.bukkit.entity.Player;
import venture.Aust1n46.chat.localization.LocalizedMessage;
public abstract class PlayerCommand extends UniversalCommand {
public abstract class PlayerCommand extends PluginCommand {
protected PlayerCommand(final String name) {
super(name);
}
@Override
public void executeVoid(final CommandSender sender, final String commandLabel, final String[] args) {
public final boolean execute(final CommandSender sender, final String commandLabel, final String[] args) {
if (sender instanceof Player) {
final Player player = (Player) sender;
execute(player, commandLabel, args);
executeCommand(player, commandLabel, args);
} else {
plugin.getServer().getConsoleSender().sendMessage(LocalizedMessage.COMMAND_MUST_BE_RUN_BY_PLAYER.toString());
}
return true;
}
public abstract void execute(final Player player, final String commandLabel, final String[] args);
protected abstract void executeCommand(final Player player, final String commandLabel, final String[] args);
}

View File

@ -0,0 +1,23 @@
package venture.Aust1n46.chat.model;
import org.bukkit.command.Command;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.plugin.Plugin;
import com.google.inject.Inject;
import venture.Aust1n46.chat.initiators.application.VentureChat;
public abstract class PluginCommand extends Command implements PluginIdentifiableCommand {
@Inject
protected VentureChat plugin;
protected PluginCommand(final String name) {
super(name);
}
@Override
public final Plugin getPlugin() {
return plugin;
}
}

View File

@ -1,32 +1,17 @@
package venture.Aust1n46.chat.model;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.plugin.Plugin;
import com.google.inject.Inject;
import venture.Aust1n46.chat.initiators.application.VentureChat;
public abstract class UniversalCommand extends Command implements PluginIdentifiableCommand {
@Inject
protected VentureChat plugin;
public abstract class UniversalCommand extends PluginCommand {
protected UniversalCommand(final String name) {
super(name);
}
@Override
public boolean execute(final CommandSender sender, final String commandLabel, final String[] args) {
executeVoid(sender, commandLabel, args);
public final boolean execute(final CommandSender sender, final String commandLabel, final String[] args) {
executeCommand(sender, commandLabel, args);
return true;
}
public abstract void executeVoid(final CommandSender sender, final String commandLabel, final String[] args);
@Override
public Plugin getPlugin() {
return plugin;
}
protected abstract void executeCommand(final CommandSender sender, final String commandLabel, final String[] args);
}