From cd2cdd68696e277d9d0af6519f9ef4fac5cba79a Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Mon, 29 May 2023 21:10:51 +0100 Subject: [PATCH] generalize action types more --- .../commands/action/ActionAddCommand.java | 8 +++---- .../InteractionCommandHandler.java | 14 ++++++++++- .../ConsoleCommandActionType.java | 23 ++++++------------- .../message/MessageActionType.java | 23 ++++++------------- .../PlayerCommandActionType.java | 23 ++++++------------- .../switchserver/SwitchServerActionType.java | 23 ++++++------------- 6 files changed, 44 insertions(+), 70 deletions(-) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/action/ActionAddCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/action/ActionAddCommand.java index 060d316..3109c1b 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/action/ActionAddCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/action/ActionAddCommand.java @@ -5,8 +5,6 @@ import lol.pyr.director.adventure.command.CommandHandler; import lol.pyr.director.common.command.CommandExecutionException; import lol.pyr.znpcsplus.interaction.ActionRegistry; import lol.pyr.znpcsplus.interaction.InteractionCommandHandler; -import lol.pyr.znpcsplus.npc.NpcEntryImpl; -import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -27,11 +25,11 @@ public class ActionAddCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { List commands = actionRegistry.getCommands(); - context.setUsage(context.getLabel() + " action add ..."); - NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); + context.setUsage(context.getLabel() + " action add ..."); String sub = context.popString(); for (InteractionCommandHandler command : commands) if (command.getSubcommandName().equalsIgnoreCase(sub)) { - command.parse(context, npc); + context.setUsage(context.getLabel() + " action add "); + command.run(context); return; } context.send(Component.text("Invalid action type, available action types:\n" + diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionCommandHandler.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionCommandHandler.java index 2b07e2a..e6367dc 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionCommandHandler.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionCommandHandler.java @@ -3,10 +3,22 @@ package lol.pyr.znpcsplus.interaction; import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.director.adventure.command.CommandHandler; import lol.pyr.director.common.command.CommandExecutionException; +import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcImpl; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; public interface InteractionCommandHandler extends CommandHandler { String getSubcommandName(); - InteractionAction parse(CommandContext context, NpcImpl npc) throws CommandExecutionException; + InteractionAction parse(CommandContext context) throws CommandExecutionException; + void appendUsage(CommandContext context); + + @Override + default void run(CommandContext context) throws CommandExecutionException { + appendUsage(context); + NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); + npc.addAction(parse(context)); + context.send(Component.text("Added action to npc", NamedTextColor.GREEN)); + } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/consolecommand/ConsoleCommandActionType.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/consolecommand/ConsoleCommandActionType.java index f1af2c0..558c454 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/consolecommand/ConsoleCommandActionType.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/consolecommand/ConsoleCommandActionType.java @@ -6,10 +6,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionType; import lol.pyr.znpcsplus.interaction.InteractionAction; import lol.pyr.znpcsplus.interaction.InteractionActionType; import lol.pyr.znpcsplus.interaction.InteractionCommandHandler; -import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.scheduling.TaskScheduler; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; import java.nio.charset.StandardCharsets; import java.util.Base64; @@ -46,22 +43,16 @@ public class ConsoleCommandActionType implements InteractionActionType "); - InteractionType type = context.parse(InteractionType.class); - long cooldown = (long) (context.parse(Double.class) * 1000D); - String command = context.dumpAllArgs(); - ConsoleCommandAction action = new ConsoleCommandAction(scheduler, command, type, cooldown); - if (npc != null) { - npc.addAction(action); - context.send(Component.text("Added a console command action to the npc with the command " + action.getCommand(), NamedTextColor.GREEN)); - } - return action; + public void appendUsage(CommandContext context) { + context.setUsage(context.getUsage() + " " + getSubcommandName() + " "); } @Override - public void run(CommandContext context) throws CommandExecutionException { - + public InteractionAction parse(CommandContext context) throws CommandExecutionException { + InteractionType type = context.parse(InteractionType.class); + long cooldown = (long) (context.parse(Double.class) * 1000D); + String command = context.dumpAllArgs(); + return new ConsoleCommandAction(scheduler, command, type, cooldown); } @Override diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/message/MessageActionType.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/message/MessageActionType.java index d646589..434966b 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/message/MessageActionType.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/message/MessageActionType.java @@ -6,10 +6,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionType; import lol.pyr.znpcsplus.interaction.InteractionAction; import lol.pyr.znpcsplus.interaction.InteractionActionType; import lol.pyr.znpcsplus.interaction.InteractionCommandHandler; -import lol.pyr.znpcsplus.npc.NpcImpl; import net.kyori.adventure.platform.bukkit.BukkitAudiences; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import java.nio.charset.StandardCharsets; @@ -49,22 +46,16 @@ public class MessageActionType implements InteractionActionType, } @Override - public InteractionAction parse(CommandContext context, NpcImpl npc) throws CommandExecutionException { - context.setUsage(context.getUsage() + " "); - InteractionType type = context.parse(InteractionType.class); - long cooldown = (long) (context.parse(Double.class) * 1000D); - String message = context.dumpAllArgs(); - MessageAction action = new MessageAction(adventure, message, type, textSerializer, cooldown); - if (npc != null) { - npc.addAction(action); - context.send(Component.text("Added a message action to the npc with the message ", NamedTextColor.GREEN).append(Component.text(message))); - } - return action; + public void appendUsage(CommandContext context) { + context.setUsage(context.getUsage() + " " + getSubcommandName() + " "); } @Override - public void run(CommandContext context) throws CommandExecutionException { - + public InteractionAction parse(CommandContext context) throws CommandExecutionException { + InteractionType type = context.parse(InteractionType.class); + long cooldown = (long) (context.parse(Double.class) * 1000D); + String message = context.dumpAllArgs(); + return new MessageAction(adventure, message, type, textSerializer, cooldown); } @Override diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playercommand/PlayerCommandActionType.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playercommand/PlayerCommandActionType.java index f7b133e..eb7bd21 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playercommand/PlayerCommandActionType.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playercommand/PlayerCommandActionType.java @@ -6,10 +6,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionType; import lol.pyr.znpcsplus.interaction.InteractionAction; import lol.pyr.znpcsplus.interaction.InteractionActionType; import lol.pyr.znpcsplus.interaction.InteractionCommandHandler; -import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.scheduling.TaskScheduler; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; import java.nio.charset.StandardCharsets; import java.util.Base64; @@ -46,22 +43,16 @@ public class PlayerCommandActionType implements InteractionActionType "); - InteractionType type = context.parse(InteractionType.class); - long cooldown = (long) (context.parse(Double.class) * 1000D); - String command = context.dumpAllArgs(); - PlayerCommandAction action = new PlayerCommandAction(scheduler, command, type, cooldown); - if (npc != null) { - npc.addAction(action); - context.send(Component.text("Added a player command action to the npc with the command " + action.getCommand(), NamedTextColor.GREEN)); - } - return action; + public void appendUsage(CommandContext context) { + context.setUsage(context.getUsage() + " " + getSubcommandName() + " "); } @Override - public void run(CommandContext context) throws CommandExecutionException { - + public InteractionAction parse(CommandContext context) throws CommandExecutionException { + InteractionType type = context.parse(InteractionType.class); + long cooldown = (long) (context.parse(Double.class) * 1000D); + String command = context.dumpAllArgs(); + return new PlayerCommandAction(scheduler, command, type, cooldown); } @Override diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/switchserver/SwitchServerActionType.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/switchserver/SwitchServerActionType.java index e92573c..01d95dd 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/switchserver/SwitchServerActionType.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/switchserver/SwitchServerActionType.java @@ -6,10 +6,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionType; import lol.pyr.znpcsplus.interaction.InteractionAction; import lol.pyr.znpcsplus.interaction.InteractionActionType; import lol.pyr.znpcsplus.interaction.InteractionCommandHandler; -import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.util.BungeeConnector; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; import java.nio.charset.StandardCharsets; import java.util.Base64; @@ -46,22 +43,16 @@ public class SwitchServerActionType implements InteractionActionType "); - InteractionType type = context.parse(InteractionType.class); - long cooldown = (long) (context.parse(Double.class) * 1000D); - String server = context.dumpAllArgs(); - SwitchServerAction action = new SwitchServerAction(bungeeConnector, server, type, cooldown); - if (npc != null) { - npc.addAction(action); - context.send(Component.text("Added a switch server action to the npc with the server " + action.getServer(), NamedTextColor.GREEN)); - } - return action; + public void appendUsage(CommandContext context) { + context.setUsage(context.getUsage() + " " + getSubcommandName() + " "); } @Override - public void run(CommandContext context) throws CommandExecutionException { - + public InteractionAction parse(CommandContext context) throws CommandExecutionException { + InteractionType type = context.parse(InteractionType.class); + long cooldown = (long) (context.parse(Double.class) * 1000D); + String server = context.dumpAllArgs(); + return new SwitchServerAction(bungeeConnector, server, type, cooldown); } @Override