add action click type
This commit is contained in:
parent
8abe011956
commit
b71207d07a
14 changed files with 99 additions and 29 deletions
|
@ -25,13 +25,11 @@ import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
|
||||||
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
|
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
|
||||||
import lol.pyr.znpcsplus.interaction.ActionRegistry;
|
import lol.pyr.znpcsplus.interaction.ActionRegistry;
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionPacketListener;
|
import lol.pyr.znpcsplus.interaction.InteractionPacketListener;
|
||||||
|
import lol.pyr.znpcsplus.interaction.InteractionType;
|
||||||
import lol.pyr.znpcsplus.metadata.*;
|
import lol.pyr.znpcsplus.metadata.*;
|
||||||
import lol.pyr.znpcsplus.npc.*;
|
import lol.pyr.znpcsplus.npc.*;
|
||||||
import lol.pyr.znpcsplus.packets.*;
|
import lol.pyr.znpcsplus.packets.*;
|
||||||
import lol.pyr.znpcsplus.parsers.EntityPropertyParser;
|
import lol.pyr.znpcsplus.parsers.*;
|
||||||
import lol.pyr.znpcsplus.parsers.NamedTextColorParser;
|
|
||||||
import lol.pyr.znpcsplus.parsers.NpcEntryParser;
|
|
||||||
import lol.pyr.znpcsplus.parsers.NpcTypeParser;
|
|
||||||
import lol.pyr.znpcsplus.scheduling.FoliaScheduler;
|
import lol.pyr.znpcsplus.scheduling.FoliaScheduler;
|
||||||
import lol.pyr.znpcsplus.scheduling.SpigotScheduler;
|
import lol.pyr.znpcsplus.scheduling.SpigotScheduler;
|
||||||
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
||||||
|
@ -221,6 +219,7 @@ public class ZNpcsPlus extends JavaPlugin {
|
||||||
manager.registerParser(Double.class, new DoubleParser(incorrectUsageMessage));
|
manager.registerParser(Double.class, new DoubleParser(incorrectUsageMessage));
|
||||||
manager.registerParser(Boolean.class, new BooleanParser(incorrectUsageMessage));
|
manager.registerParser(Boolean.class, new BooleanParser(incorrectUsageMessage));
|
||||||
manager.registerParser(NamedTextColor.class, new NamedTextColorParser(incorrectUsageMessage));
|
manager.registerParser(NamedTextColor.class, new NamedTextColorParser(incorrectUsageMessage));
|
||||||
|
manager.registerParser(InteractionType.class, new InteractionTypeParser(incorrectUsageMessage));
|
||||||
|
|
||||||
manager.registerCommand("npc", new MultiCommand()
|
manager.registerCommand("npc", new MultiCommand()
|
||||||
.addSubcommand("create", new CreateCommand(npcRegistry, typeRegistry))
|
.addSubcommand("create", new CreateCommand(npcRegistry, typeRegistry))
|
||||||
|
|
|
@ -22,6 +22,7 @@ public class ActionDeleteCommand implements CommandHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(CommandContext context) throws CommandExecutionException {
|
public void run(CommandContext context) throws CommandExecutionException {
|
||||||
|
context.setUsage(context.getLabel() + " action delete <id> <index>");
|
||||||
NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc();
|
NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc();
|
||||||
int index = context.parse(Integer.class);
|
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));
|
if (index >= npc.getActions().size() || index < 0) context.halt(Component.text("That npc doesn't have any action with the index " + index, NamedTextColor.RED));
|
||||||
|
|
|
@ -7,8 +7,10 @@ import java.util.UUID;
|
||||||
public abstract class InteractionAction {
|
public abstract class InteractionAction {
|
||||||
private final UUID id;
|
private final UUID id;
|
||||||
private final long delay;
|
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.id = UUID.randomUUID();
|
||||||
this.delay = delay;
|
this.delay = delay;
|
||||||
}
|
}
|
||||||
|
@ -21,5 +23,9 @@ public abstract class InteractionAction {
|
||||||
return delay;
|
return delay;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public InteractionType getInteractionType() {
|
||||||
|
return interactionType;
|
||||||
|
}
|
||||||
|
|
||||||
public abstract void run(Player player);
|
public abstract void run(Player player);
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,8 +34,21 @@ public class InteractionPacketListener implements PacketListener {
|
||||||
NpcImpl npc = entry.getNpc();
|
NpcImpl npc = entry.getNpc();
|
||||||
|
|
||||||
for (InteractionAction action : npc.getActions()) {
|
for (InteractionAction action : npc.getActions()) {
|
||||||
|
if (!isAllowed(action.getInteractionType(), packet.getAction())) continue;
|
||||||
if (action.getCooldown() > 0 && !user.actionCooldownCheck(action)) continue;
|
if (action.getCooldown() > 0 && !user.actionCooldownCheck(action)) continue;
|
||||||
action.run(player);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package lol.pyr.znpcsplus.interaction;
|
||||||
|
|
||||||
|
public enum InteractionType {
|
||||||
|
ANY_CLICK,
|
||||||
|
LEFT_CLICK,
|
||||||
|
RIGHT_CLICK
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package lol.pyr.znpcsplus.interaction.consolecommand;
|
package lol.pyr.znpcsplus.interaction.consolecommand;
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
||||||
|
import lol.pyr.znpcsplus.interaction.InteractionType;
|
||||||
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
||||||
import lol.pyr.znpcsplus.util.PapiUtil;
|
import lol.pyr.znpcsplus.util.PapiUtil;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
@ -10,8 +11,8 @@ public class ConsoleCommandAction extends InteractionAction {
|
||||||
private final TaskScheduler scheduler;
|
private final TaskScheduler scheduler;
|
||||||
private final String command;
|
private final String command;
|
||||||
|
|
||||||
public ConsoleCommandAction(TaskScheduler scheduler, String command, long delay) {
|
public ConsoleCommandAction(TaskScheduler scheduler, String command, InteractionType interactionType, long delay) {
|
||||||
super(delay);
|
super(delay, interactionType);
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
this.command = command;
|
this.command = command;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import lol.pyr.director.adventure.command.CommandContext;
|
||||||
import lol.pyr.director.common.command.CommandExecutionException;
|
import lol.pyr.director.common.command.CommandExecutionException;
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionActionType;
|
import lol.pyr.znpcsplus.interaction.InteractionActionType;
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionCommandHandler;
|
import lol.pyr.znpcsplus.interaction.InteractionCommandHandler;
|
||||||
|
import lol.pyr.znpcsplus.interaction.InteractionType;
|
||||||
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
||||||
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
||||||
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
||||||
|
@ -32,7 +33,8 @@ public class ConsoleCommandActionType implements InteractionActionType<ConsoleCo
|
||||||
@Override
|
@Override
|
||||||
public ConsoleCommandAction deserialize(String str) {
|
public ConsoleCommandAction deserialize(String str) {
|
||||||
String[] split = str.split(";");
|
String[] split = str.split(";");
|
||||||
return new ConsoleCommandAction(scheduler, 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 ConsoleCommandAction(scheduler, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), type, Long.parseLong(split[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -47,18 +49,20 @@ public class ConsoleCommandActionType implements InteractionActionType<ConsoleCo
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(CommandContext context) throws CommandExecutionException {
|
public void run(CommandContext context) throws CommandExecutionException {
|
||||||
context.setUsage(context.getUsage() + " consolecommand <id> <cooldown seconds> <command>");
|
context.setUsage(context.getUsage() + " consolecommand <id> <type> <cooldown seconds> <command>");
|
||||||
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
|
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
|
||||||
|
InteractionType type = context.parse(InteractionType.class);
|
||||||
long cooldown = (long) (context.parse(Double.class) * 1000D);
|
long cooldown = (long) (context.parse(Double.class) * 1000D);
|
||||||
String command = context.dumpAllArgs();
|
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));
|
context.send(Component.text("Added a console command action to the npc with the command " + command, NamedTextColor.GREEN));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
||||||
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
|
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();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package lol.pyr.znpcsplus.interaction.message;
|
package lol.pyr.znpcsplus.interaction.message;
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
||||||
|
import lol.pyr.znpcsplus.interaction.InteractionType;
|
||||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
@ -9,8 +10,8 @@ public class MessageAction extends InteractionAction {
|
||||||
private final BukkitAudiences adventure;
|
private final BukkitAudiences adventure;
|
||||||
private final Component message;
|
private final Component message;
|
||||||
|
|
||||||
public MessageAction(BukkitAudiences adventure, Component message, long delay) {
|
public MessageAction(BukkitAudiences adventure, Component message, InteractionType interactionType, long delay) {
|
||||||
super(delay);
|
super(delay, interactionType);
|
||||||
this.adventure = adventure;
|
this.adventure = adventure;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import lol.pyr.director.adventure.command.CommandContext;
|
||||||
import lol.pyr.director.common.command.CommandExecutionException;
|
import lol.pyr.director.common.command.CommandExecutionException;
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionActionType;
|
import lol.pyr.znpcsplus.interaction.InteractionActionType;
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionCommandHandler;
|
import lol.pyr.znpcsplus.interaction.InteractionCommandHandler;
|
||||||
|
import lol.pyr.znpcsplus.interaction.InteractionType;
|
||||||
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
||||||
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
||||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||||
|
@ -36,7 +37,8 @@ public class MessageActionType implements InteractionActionType<MessageAction>,
|
||||||
@Override
|
@Override
|
||||||
public MessageAction deserialize(String str) {
|
public MessageAction deserialize(String str) {
|
||||||
String[] split = str.split(";");
|
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
|
@Override
|
||||||
|
@ -51,18 +53,20 @@ public class MessageActionType implements InteractionActionType<MessageAction>,
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(CommandContext context) throws CommandExecutionException {
|
public void run(CommandContext context) throws CommandExecutionException {
|
||||||
context.setUsage(context.getUsage() + " consolecommand <id> <cooldown seconds> <message>");
|
context.setUsage(context.getUsage() + " consolecommand <id> <type> <cooldown seconds> <message>");
|
||||||
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
|
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
|
||||||
|
InteractionType type = context.parse(InteractionType.class);
|
||||||
long cooldown = (long) (context.parse(Double.class) * 1000D);
|
long cooldown = (long) (context.parse(Double.class) * 1000D);
|
||||||
Component message = textSerializer.deserialize(context.dumpAllArgs());
|
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));
|
context.send(Component.text("Added a message action to the npc with the message ", NamedTextColor.GREEN).append(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
||||||
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
|
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();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package lol.pyr.znpcsplus.interaction.playercommand;
|
package lol.pyr.znpcsplus.interaction.playercommand;
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
||||||
|
import lol.pyr.znpcsplus.interaction.InteractionType;
|
||||||
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
||||||
import lol.pyr.znpcsplus.util.PapiUtil;
|
import lol.pyr.znpcsplus.util.PapiUtil;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
@ -10,8 +11,8 @@ public class PlayerCommandAction extends InteractionAction {
|
||||||
private final TaskScheduler scheduler;
|
private final TaskScheduler scheduler;
|
||||||
private final String command;
|
private final String command;
|
||||||
|
|
||||||
public PlayerCommandAction(TaskScheduler scheduler, String command, long delay) {
|
public PlayerCommandAction(TaskScheduler scheduler, String command, InteractionType interactionType, long delay) {
|
||||||
super(delay);
|
super(delay, interactionType);
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
this.command = command;
|
this.command = command;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import lol.pyr.director.adventure.command.CommandContext;
|
||||||
import lol.pyr.director.common.command.CommandExecutionException;
|
import lol.pyr.director.common.command.CommandExecutionException;
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionActionType;
|
import lol.pyr.znpcsplus.interaction.InteractionActionType;
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionCommandHandler;
|
import lol.pyr.znpcsplus.interaction.InteractionCommandHandler;
|
||||||
|
import lol.pyr.znpcsplus.interaction.InteractionType;
|
||||||
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
||||||
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
||||||
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
||||||
|
@ -32,7 +33,8 @@ public class PlayerCommandActionType implements InteractionActionType<PlayerComm
|
||||||
@Override
|
@Override
|
||||||
public PlayerCommandAction deserialize(String str) {
|
public PlayerCommandAction deserialize(String str) {
|
||||||
String[] split = str.split(";");
|
String[] split = str.split(";");
|
||||||
return new PlayerCommandAction(scheduler, 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 PlayerCommandAction(scheduler, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), type, Long.parseLong(split[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -47,18 +49,20 @@ public class PlayerCommandActionType implements InteractionActionType<PlayerComm
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(CommandContext context) throws CommandExecutionException {
|
public void run(CommandContext context) throws CommandExecutionException {
|
||||||
context.setUsage(context.getUsage() + " playercommand <id> <cooldown seconds> <command>");
|
context.setUsage(context.getUsage() + " playercommand <id> <type> <cooldown seconds> <command>");
|
||||||
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
|
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
|
||||||
|
InteractionType type = context.parse(InteractionType.class);
|
||||||
long cooldown = (long) (context.parse(Double.class) * 1000D);
|
long cooldown = (long) (context.parse(Double.class) * 1000D);
|
||||||
String command = context.dumpAllArgs();
|
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));
|
context.send(Component.text("Added a player command action to the npc with the command " + command, NamedTextColor.GREEN));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
||||||
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
|
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();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package lol.pyr.znpcsplus.interaction.switchserver;
|
package lol.pyr.znpcsplus.interaction.switchserver;
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
||||||
|
import lol.pyr.znpcsplus.interaction.InteractionType;
|
||||||
import lol.pyr.znpcsplus.util.BungeeConnector;
|
import lol.pyr.znpcsplus.util.BungeeConnector;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
@ -8,8 +9,8 @@ public class SwitchServerAction extends InteractionAction {
|
||||||
private final BungeeConnector bungeeConnector;
|
private final BungeeConnector bungeeConnector;
|
||||||
private final String server;
|
private final String server;
|
||||||
|
|
||||||
public SwitchServerAction(BungeeConnector bungeeConnector, String server, long delay) {
|
public SwitchServerAction(BungeeConnector bungeeConnector, String server, InteractionType interactionType, long delay) {
|
||||||
super(delay);
|
super(delay, interactionType);
|
||||||
this.bungeeConnector = bungeeConnector;
|
this.bungeeConnector = bungeeConnector;
|
||||||
this.server = server;
|
this.server = server;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import lol.pyr.director.adventure.command.CommandContext;
|
||||||
import lol.pyr.director.common.command.CommandExecutionException;
|
import lol.pyr.director.common.command.CommandExecutionException;
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionActionType;
|
import lol.pyr.znpcsplus.interaction.InteractionActionType;
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionCommandHandler;
|
import lol.pyr.znpcsplus.interaction.InteractionCommandHandler;
|
||||||
|
import lol.pyr.znpcsplus.interaction.InteractionType;
|
||||||
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
||||||
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
||||||
import lol.pyr.znpcsplus.util.BungeeConnector;
|
import lol.pyr.znpcsplus.util.BungeeConnector;
|
||||||
|
@ -32,7 +33,8 @@ public class SwitchServerActionType implements InteractionActionType<SwitchServe
|
||||||
@Override
|
@Override
|
||||||
public SwitchServerAction deserialize(String str) {
|
public SwitchServerAction deserialize(String str) {
|
||||||
String[] split = str.split(";");
|
String[] split = str.split(";");
|
||||||
return new SwitchServerAction(bungeeConnector, 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 SwitchServerAction(bungeeConnector, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), type, Long.parseLong(split[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -47,18 +49,20 @@ public class SwitchServerActionType implements InteractionActionType<SwitchServe
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(CommandContext context) throws CommandExecutionException {
|
public void run(CommandContext context) throws CommandExecutionException {
|
||||||
context.setUsage(context.getUsage() + " switchserver <id> <cooldown seconds> <server>");
|
context.setUsage(context.getUsage() + " switchserver <id> <type> <cooldown seconds> <server>");
|
||||||
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
|
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
|
||||||
|
InteractionType type = context.parse(InteractionType.class);
|
||||||
long cooldown = (long) (context.parse(Double.class) * 1000D);
|
long cooldown = (long) (context.parse(Double.class) * 1000D);
|
||||||
String server = context.dumpAllArgs();
|
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));
|
context.send(Component.text("Added a switch server action to the npc with the server " + server, NamedTextColor.GREEN));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
||||||
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
|
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();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<InteractionType> {
|
||||||
|
public InteractionTypeParser(Message<CommandContext> message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionType parse(Deque<String> deque) throws CommandExecutionException {
|
||||||
|
try {
|
||||||
|
return InteractionType.valueOf(deque.pop().toUpperCase());
|
||||||
|
} catch (IllegalArgumentException ignored) {
|
||||||
|
throw new CommandExecutionException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue