From b71207d07a028f242546c1c094417aa179b56d7d Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Wed, 24 May 2023 15:07:55 +0100 Subject: [PATCH] add action click type --- .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 7 +++--- .../commands/action/ActionDeleteCommand.java | 1 + .../interaction/InteractionAction.java | 8 ++++++- .../InteractionPacketListener.java | 13 ++++++++++ .../interaction/InteractionType.java | 7 ++++++ .../consolecommand/ConsoleCommandAction.java | 5 ++-- .../ConsoleCommandActionType.java | 12 ++++++---- .../interaction/message/MessageAction.java | 5 ++-- .../message/MessageActionType.java | 12 ++++++---- .../playercommand/PlayerCommandAction.java | 5 ++-- .../PlayerCommandActionType.java | 12 ++++++---- .../switchserver/SwitchServerAction.java | 5 ++-- .../switchserver/SwitchServerActionType.java | 12 ++++++---- .../parsers/InteractionTypeParser.java | 24 +++++++++++++++++++ 14 files changed, 99 insertions(+), 29 deletions(-) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionType.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/parsers/InteractionTypeParser.java diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index d0c32a0..c1a9754 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -25,13 +25,11 @@ import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl; import lol.pyr.znpcsplus.interaction.ActionRegistry; import lol.pyr.znpcsplus.interaction.InteractionPacketListener; +import lol.pyr.znpcsplus.interaction.InteractionType; import lol.pyr.znpcsplus.metadata.*; import lol.pyr.znpcsplus.npc.*; import lol.pyr.znpcsplus.packets.*; -import lol.pyr.znpcsplus.parsers.EntityPropertyParser; -import lol.pyr.znpcsplus.parsers.NamedTextColorParser; -import lol.pyr.znpcsplus.parsers.NpcEntryParser; -import lol.pyr.znpcsplus.parsers.NpcTypeParser; +import lol.pyr.znpcsplus.parsers.*; import lol.pyr.znpcsplus.scheduling.FoliaScheduler; import lol.pyr.znpcsplus.scheduling.SpigotScheduler; import lol.pyr.znpcsplus.scheduling.TaskScheduler; @@ -221,6 +219,7 @@ public class ZNpcsPlus extends JavaPlugin { manager.registerParser(Double.class, new DoubleParser(incorrectUsageMessage)); manager.registerParser(Boolean.class, new BooleanParser(incorrectUsageMessage)); manager.registerParser(NamedTextColor.class, new NamedTextColorParser(incorrectUsageMessage)); + manager.registerParser(InteractionType.class, new InteractionTypeParser(incorrectUsageMessage)); manager.registerCommand("npc", new MultiCommand() .addSubcommand("create", new CreateCommand(npcRegistry, typeRegistry)) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/action/ActionDeleteCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/action/ActionDeleteCommand.java index 56e9440..b282a35 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/action/ActionDeleteCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/action/ActionDeleteCommand.java @@ -22,6 +22,7 @@ public class ActionDeleteCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { + context.setUsage(context.getLabel() + " action delete "); NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); int index = context.parse(Integer.class); if (index >= npc.getActions().size() || index < 0) context.halt(Component.text("That npc doesn't have any action with the index " + index, NamedTextColor.RED)); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionAction.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionAction.java index 79b39bf..3d4867d 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionAction.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionAction.java @@ -7,8 +7,10 @@ import java.util.UUID; public abstract class InteractionAction { private final UUID id; private final long delay; + private final InteractionType interactionType; - protected InteractionAction(long delay) { + protected InteractionAction(long delay, InteractionType interactionType) { + this.interactionType = interactionType; this.id = UUID.randomUUID(); this.delay = delay; } @@ -21,5 +23,9 @@ public abstract class InteractionAction { return delay; } + public InteractionType getInteractionType() { + return interactionType; + } + public abstract void run(Player player); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionPacketListener.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionPacketListener.java index b504069..0aee012 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionPacketListener.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionPacketListener.java @@ -34,8 +34,21 @@ public class InteractionPacketListener implements PacketListener { NpcImpl npc = entry.getNpc(); for (InteractionAction action : npc.getActions()) { + if (!isAllowed(action.getInteractionType(), packet.getAction())) continue; if (action.getCooldown() > 0 && !user.actionCooldownCheck(action)) continue; action.run(player); } } + + private boolean isAllowed(InteractionType type, WrapperPlayClientInteractEntity.InteractAction action) { + switch (type) { + case ANY_CLICK: + return true; + case LEFT_CLICK: + return action == WrapperPlayClientInteractEntity.InteractAction.ATTACK; + case RIGHT_CLICK: + return action == WrapperPlayClientInteractEntity.InteractAction.INTERACT || action == WrapperPlayClientInteractEntity.InteractAction.INTERACT_AT; + } + return false; + } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionType.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionType.java new file mode 100644 index 0000000..801f2a1 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/InteractionType.java @@ -0,0 +1,7 @@ +package lol.pyr.znpcsplus.interaction; + +public enum InteractionType { + ANY_CLICK, + LEFT_CLICK, + RIGHT_CLICK +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/consolecommand/ConsoleCommandAction.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/consolecommand/ConsoleCommandAction.java index e71ad5d..a5a9090 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/consolecommand/ConsoleCommandAction.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/consolecommand/ConsoleCommandAction.java @@ -1,6 +1,7 @@ package lol.pyr.znpcsplus.interaction.consolecommand; import lol.pyr.znpcsplus.interaction.InteractionAction; +import lol.pyr.znpcsplus.interaction.InteractionType; import lol.pyr.znpcsplus.scheduling.TaskScheduler; import lol.pyr.znpcsplus.util.PapiUtil; import org.bukkit.Bukkit; @@ -10,8 +11,8 @@ public class ConsoleCommandAction extends InteractionAction { private final TaskScheduler scheduler; private final String command; - public ConsoleCommandAction(TaskScheduler scheduler, String command, long delay) { - super(delay); + public ConsoleCommandAction(TaskScheduler scheduler, String command, InteractionType interactionType, long delay) { + super(delay, interactionType); this.scheduler = scheduler; this.command = command; } 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 1891c2f..70ef3f9 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 @@ -4,6 +4,7 @@ import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.director.common.command.CommandExecutionException; import lol.pyr.znpcsplus.interaction.InteractionActionType; import lol.pyr.znpcsplus.interaction.InteractionCommandHandler; +import lol.pyr.znpcsplus.interaction.InteractionType; import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import lol.pyr.znpcsplus.scheduling.TaskScheduler; @@ -32,7 +33,8 @@ public class ConsoleCommandActionType implements InteractionActionType 2 ? InteractionType.valueOf(split[2]) : InteractionType.ANY_CLICK; + return new ConsoleCommandAction(scheduler, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), type, Long.parseLong(split[1])); } @Override @@ -47,18 +49,20 @@ public class ConsoleCommandActionType implements InteractionActionType "); + context.setUsage(context.getUsage() + " consolecommand "); NpcEntryImpl entry = context.parse(NpcEntryImpl.class); + InteractionType type = context.parse(InteractionType.class); long cooldown = (long) (context.parse(Double.class) * 1000D); String command = context.dumpAllArgs(); - entry.getNpc().addAction(new ConsoleCommandAction(scheduler, command, cooldown)); + entry.getNpc().addAction(new ConsoleCommandAction(scheduler, command, type, cooldown)); context.send(Component.text("Added a console command action to the npc with the command " + command, NamedTextColor.GREEN)); } @Override public List suggest(CommandContext context) throws CommandExecutionException { if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); - if (context.argSize() == 2) return context.suggestLiteral("1"); + if (context.argSize() == 2) return context.suggestEnum(InteractionType.values()); + if (context.argSize() == 3) return context.suggestLiteral("1"); return Collections.emptyList(); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/message/MessageAction.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/message/MessageAction.java index e62da50..3b9205d 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/message/MessageAction.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/message/MessageAction.java @@ -1,6 +1,7 @@ package lol.pyr.znpcsplus.interaction.message; import lol.pyr.znpcsplus.interaction.InteractionAction; +import lol.pyr.znpcsplus.interaction.InteractionType; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.Component; import org.bukkit.entity.Player; @@ -9,8 +10,8 @@ public class MessageAction extends InteractionAction { private final BukkitAudiences adventure; private final Component message; - public MessageAction(BukkitAudiences adventure, Component message, long delay) { - super(delay); + public MessageAction(BukkitAudiences adventure, Component message, InteractionType interactionType, long delay) { + super(delay, interactionType); this.adventure = adventure; this.message = message; } 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 fbd67b0..f968fac 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 @@ -4,6 +4,7 @@ import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.director.common.command.CommandExecutionException; import lol.pyr.znpcsplus.interaction.InteractionActionType; import lol.pyr.znpcsplus.interaction.InteractionCommandHandler; +import lol.pyr.znpcsplus.interaction.InteractionType; import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import net.kyori.adventure.platform.bukkit.BukkitAudiences; @@ -36,7 +37,8 @@ public class MessageActionType implements InteractionActionType, @Override public MessageAction deserialize(String str) { String[] split = str.split(";"); - return new MessageAction(adventure, MiniMessage.miniMessage().deserialize(new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8)), Long.parseLong(split[1])); + InteractionType type = split.length > 2 ? InteractionType.valueOf(split[2]) : InteractionType.ANY_CLICK; + return new MessageAction(adventure, MiniMessage.miniMessage().deserialize(new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8)), type, Long.parseLong(split[1])); } @Override @@ -51,18 +53,20 @@ public class MessageActionType implements InteractionActionType, @Override public void run(CommandContext context) throws CommandExecutionException { - context.setUsage(context.getUsage() + " consolecommand "); + context.setUsage(context.getUsage() + " consolecommand "); NpcEntryImpl entry = context.parse(NpcEntryImpl.class); + InteractionType type = context.parse(InteractionType.class); long cooldown = (long) (context.parse(Double.class) * 1000D); Component message = textSerializer.deserialize(context.dumpAllArgs()); - entry.getNpc().addAction(new MessageAction(adventure, message, cooldown)); + entry.getNpc().addAction(new MessageAction(adventure, message, type, cooldown)); context.send(Component.text("Added a message action to the npc with the message ", NamedTextColor.GREEN).append(message)); } @Override public List suggest(CommandContext context) throws CommandExecutionException { if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); - if (context.argSize() == 2) return context.suggestLiteral("1"); + if (context.argSize() == 2) return context.suggestEnum(InteractionType.values()); + if (context.argSize() == 3) return context.suggestLiteral("1"); return Collections.emptyList(); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playercommand/PlayerCommandAction.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playercommand/PlayerCommandAction.java index 26c5514..cf65df9 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playercommand/PlayerCommandAction.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/playercommand/PlayerCommandAction.java @@ -1,6 +1,7 @@ package lol.pyr.znpcsplus.interaction.playercommand; import lol.pyr.znpcsplus.interaction.InteractionAction; +import lol.pyr.znpcsplus.interaction.InteractionType; import lol.pyr.znpcsplus.scheduling.TaskScheduler; import lol.pyr.znpcsplus.util.PapiUtil; import org.bukkit.Bukkit; @@ -10,8 +11,8 @@ public class PlayerCommandAction extends InteractionAction { private final TaskScheduler scheduler; private final String command; - public PlayerCommandAction(TaskScheduler scheduler, String command, long delay) { - super(delay); + public PlayerCommandAction(TaskScheduler scheduler, String command, InteractionType interactionType, long delay) { + super(delay, interactionType); this.scheduler = scheduler; this.command = command; } 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 7160170..0e4a7c2 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 @@ -4,6 +4,7 @@ import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.director.common.command.CommandExecutionException; import lol.pyr.znpcsplus.interaction.InteractionActionType; import lol.pyr.znpcsplus.interaction.InteractionCommandHandler; +import lol.pyr.znpcsplus.interaction.InteractionType; import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import lol.pyr.znpcsplus.scheduling.TaskScheduler; @@ -32,7 +33,8 @@ public class PlayerCommandActionType implements InteractionActionType 2 ? InteractionType.valueOf(split[2]) : InteractionType.ANY_CLICK; + return new PlayerCommandAction(scheduler, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), type, Long.parseLong(split[1])); } @Override @@ -47,18 +49,20 @@ public class PlayerCommandActionType implements InteractionActionType "); + context.setUsage(context.getUsage() + " playercommand "); NpcEntryImpl entry = context.parse(NpcEntryImpl.class); + InteractionType type = context.parse(InteractionType.class); long cooldown = (long) (context.parse(Double.class) * 1000D); String command = context.dumpAllArgs(); - entry.getNpc().addAction(new PlayerCommandAction(scheduler, command, cooldown)); + entry.getNpc().addAction(new PlayerCommandAction(scheduler, command, type, cooldown)); context.send(Component.text("Added a player command action to the npc with the command " + command, NamedTextColor.GREEN)); } @Override public List suggest(CommandContext context) throws CommandExecutionException { if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); - if (context.argSize() == 2) return context.suggestLiteral("1"); + if (context.argSize() == 2) return context.suggestEnum(InteractionType.values()); + if (context.argSize() == 3) return context.suggestLiteral("1"); return Collections.emptyList(); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/switchserver/SwitchServerAction.java b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/switchserver/SwitchServerAction.java index c2fb557..4cac69b 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/interaction/switchserver/SwitchServerAction.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/interaction/switchserver/SwitchServerAction.java @@ -1,6 +1,7 @@ package lol.pyr.znpcsplus.interaction.switchserver; import lol.pyr.znpcsplus.interaction.InteractionAction; +import lol.pyr.znpcsplus.interaction.InteractionType; import lol.pyr.znpcsplus.util.BungeeConnector; import org.bukkit.entity.Player; @@ -8,8 +9,8 @@ public class SwitchServerAction extends InteractionAction { private final BungeeConnector bungeeConnector; private final String server; - public SwitchServerAction(BungeeConnector bungeeConnector, String server, long delay) { - super(delay); + public SwitchServerAction(BungeeConnector bungeeConnector, String server, InteractionType interactionType, long delay) { + super(delay, interactionType); this.bungeeConnector = bungeeConnector; this.server = server; } 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 6fa1d7c..69929cf 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 @@ -4,6 +4,7 @@ import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.director.common.command.CommandExecutionException; import lol.pyr.znpcsplus.interaction.InteractionActionType; import lol.pyr.znpcsplus.interaction.InteractionCommandHandler; +import lol.pyr.znpcsplus.interaction.InteractionType; import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import lol.pyr.znpcsplus.util.BungeeConnector; @@ -32,7 +33,8 @@ public class SwitchServerActionType implements InteractionActionType 2 ? InteractionType.valueOf(split[2]) : InteractionType.ANY_CLICK; + return new SwitchServerAction(bungeeConnector, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), type, Long.parseLong(split[1])); } @Override @@ -47,18 +49,20 @@ public class SwitchServerActionType implements InteractionActionType "); + context.setUsage(context.getUsage() + " switchserver "); NpcEntryImpl entry = context.parse(NpcEntryImpl.class); + InteractionType type = context.parse(InteractionType.class); long cooldown = (long) (context.parse(Double.class) * 1000D); String server = context.dumpAllArgs(); - entry.getNpc().addAction(new SwitchServerAction(bungeeConnector, server, cooldown)); + entry.getNpc().addAction(new SwitchServerAction(bungeeConnector, server, type, cooldown)); context.send(Component.text("Added a switch server action to the npc with the server " + server, NamedTextColor.GREEN)); } @Override public List suggest(CommandContext context) throws CommandExecutionException { if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); - if (context.argSize() == 2) return context.suggestLiteral("1"); + if (context.argSize() == 2) return context.suggestEnum(InteractionType.values()); + if (context.argSize() == 3) return context.suggestLiteral("1"); return Collections.emptyList(); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/parsers/InteractionTypeParser.java b/plugin/src/main/java/lol/pyr/znpcsplus/parsers/InteractionTypeParser.java new file mode 100644 index 0000000..a5dbcce --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/parsers/InteractionTypeParser.java @@ -0,0 +1,24 @@ +package lol.pyr.znpcsplus.parsers; + +import lol.pyr.director.adventure.command.CommandContext; +import lol.pyr.director.adventure.parse.ParserType; +import lol.pyr.director.common.command.CommandExecutionException; +import lol.pyr.director.common.message.Message; +import lol.pyr.znpcsplus.interaction.InteractionType; + +import java.util.Deque; + +public class InteractionTypeParser extends ParserType { + public InteractionTypeParser(Message message) { + super(message); + } + + @Override + public InteractionType parse(Deque deque) throws CommandExecutionException { + try { + return InteractionType.valueOf(deque.pop().toUpperCase()); + } catch (IllegalArgumentException ignored) { + throw new CommandExecutionException(); + } + } +}