add action command
This commit is contained in:
parent
c8c649e800
commit
8b47b78f59
18 changed files with 396 additions and 132 deletions
|
@ -0,0 +1,43 @@
|
||||||
|
package lol.pyr.znpcsplus.commands.action;
|
||||||
|
|
||||||
|
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.interaction.ActionRegistry;
|
||||||
|
import lol.pyr.znpcsplus.interaction.InteractionCommandHandler;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class ActionAddCommand implements CommandHandler {
|
||||||
|
private final ActionRegistry actionRegistry;
|
||||||
|
|
||||||
|
public ActionAddCommand(ActionRegistry actionRegistry) {
|
||||||
|
this.actionRegistry = actionRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(CommandContext context) throws CommandExecutionException {
|
||||||
|
List<InteractionCommandHandler> commands = actionRegistry.getCommands();
|
||||||
|
context.setUsage(context.getLabel() + " action add <action type>");
|
||||||
|
String sub = context.popString();
|
||||||
|
for (InteractionCommandHandler command : commands) if (command.getSubcommandName().equalsIgnoreCase(sub)) {
|
||||||
|
command.run(context);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
context.send(Component.text("Invalid action type, available action types:\n" +
|
||||||
|
commands.stream().map(InteractionCommandHandler::getSubcommandName).collect(Collectors.joining(", ")), NamedTextColor.RED));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
||||||
|
List<InteractionCommandHandler> commands = actionRegistry.getCommands();
|
||||||
|
if (context.argSize() == 1) return context.suggestStream(commands.stream().map(InteractionCommandHandler::getSubcommandName));
|
||||||
|
String sub = context.popString();
|
||||||
|
for (InteractionCommandHandler command : commands) if (command.getSubcommandName().equalsIgnoreCase(sub)) return command.suggest(context);
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package lol.pyr.znpcsplus.commands.action;
|
||||||
|
|
||||||
|
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 lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class ActionDeleteCommand implements CommandHandler {
|
||||||
|
private final NpcRegistryImpl npcRegistry;
|
||||||
|
|
||||||
|
public ActionDeleteCommand(NpcRegistryImpl npcRegistry) {
|
||||||
|
this.npcRegistry = npcRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(CommandContext context) throws CommandExecutionException {
|
||||||
|
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));
|
||||||
|
npc.removeAction(index);
|
||||||
|
context.send(Component.text("Removed action with index " + index, NamedTextColor.GREEN));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
||||||
|
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.modifiableIds());
|
||||||
|
if (context.argSize() == 2) return context.suggestStream(Stream.iterate(0, n -> n + 1)
|
||||||
|
.limit(context.suggestionParse(0, NpcEntryImpl.class).getNpc().getActions().size())
|
||||||
|
.map(String::valueOf));
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,20 +1,19 @@
|
||||||
package lol.pyr.znpcsplus.commands;
|
package lol.pyr.znpcsplus.commands.action;
|
||||||
|
|
||||||
import lol.pyr.director.adventure.command.CommandContext;
|
import lol.pyr.director.adventure.command.CommandContext;
|
||||||
import lol.pyr.director.adventure.command.CommandHandler;
|
import lol.pyr.director.adventure.command.CommandHandler;
|
||||||
import lol.pyr.director.common.command.CommandExecutionException;
|
import lol.pyr.director.common.command.CommandExecutionException;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ActionCommand implements CommandHandler {
|
public class ActionListCommand implements CommandHandler {
|
||||||
@Override
|
@Override
|
||||||
public void run(CommandContext context) throws CommandExecutionException {
|
public void run(CommandContext commandContext) throws CommandExecutionException {
|
||||||
context.send("Not implemented yet.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
||||||
return Collections.emptyList();
|
return CommandHandler.super.suggest(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,31 +1,48 @@
|
||||||
package lol.pyr.znpcsplus.interaction;
|
package lol.pyr.znpcsplus.interaction;
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.serialization.ConsoleCommandActionSerializer;
|
import lol.pyr.znpcsplus.interaction.consolecommand.ConsoleCommandActionType;
|
||||||
import lol.pyr.znpcsplus.interaction.serialization.MessageActionSerializer;
|
import lol.pyr.znpcsplus.interaction.message.MessageActionType;
|
||||||
import lol.pyr.znpcsplus.interaction.serialization.PlayerCommandActionSerializer;
|
import lol.pyr.znpcsplus.interaction.playercommand.PlayerCommandActionType;
|
||||||
import lol.pyr.znpcsplus.interaction.serialization.SwitchServerActionSerializer;
|
import lol.pyr.znpcsplus.interaction.switchserver.SwitchServerActionType;
|
||||||
import lol.pyr.znpcsplus.interaction.types.*;
|
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
||||||
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
||||||
import lol.pyr.znpcsplus.util.BungeeUtil;
|
import lol.pyr.znpcsplus.util.BungeeUtil;
|
||||||
import lol.pyr.znpcsplus.util.StringSerializer;
|
import lol.pyr.znpcsplus.util.StringSerializer;
|
||||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||||
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class ActionRegistry {
|
public class ActionRegistry {
|
||||||
private final Map<Class<?>, StringSerializer<?>> serializerMap = new HashMap<>();
|
private final Map<Class<?>, InteractionActionType<?>> serializerMap = new HashMap<>();
|
||||||
|
|
||||||
public ActionRegistry(TaskScheduler taskScheduler, BukkitAudiences adventure, BungeeUtil bungeeUtil) {
|
public ActionRegistry() {
|
||||||
register(ConsoleCommandAction.class, new ConsoleCommandActionSerializer(taskScheduler));
|
|
||||||
register(PlayerCommandAction.class, new PlayerCommandActionSerializer(taskScheduler));
|
|
||||||
register(SwitchServerAction.class, new SwitchServerActionSerializer(bungeeUtil));
|
|
||||||
register(MessageAction.class, new MessageActionSerializer(adventure));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends InteractionAction> void register(Class<T> clazz, StringSerializer<T> serializer) {
|
public void registerTypes(NpcRegistryImpl npcRegistry, TaskScheduler taskScheduler, BukkitAudiences adventure, BungeeUtil bungeeUtil, LegacyComponentSerializer textSerializer) {
|
||||||
serializerMap.put(clazz, serializer);
|
register(new ConsoleCommandActionType(taskScheduler, npcRegistry));
|
||||||
|
register(new PlayerCommandActionType(taskScheduler, npcRegistry));
|
||||||
|
register(new SwitchServerActionType(bungeeUtil, npcRegistry));
|
||||||
|
register(new MessageActionType(adventure, textSerializer, npcRegistry));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void register(InteractionActionType<?> type) {
|
||||||
|
serializerMap.put(type.getActionClass(), type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unregister(Class<? extends InteractionAction> clazz) {
|
||||||
|
serializerMap.remove(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<InteractionCommandHandler> getCommands() {
|
||||||
|
return serializerMap.values().stream()
|
||||||
|
.filter(type -> type instanceof InteractionCommandHandler)
|
||||||
|
.map(type -> (InteractionCommandHandler) type)
|
||||||
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package lol.pyr.znpcsplus.interaction;
|
||||||
|
|
||||||
|
public interface InteractionActionType<T> {
|
||||||
|
String serialize(T obj);
|
||||||
|
T deserialize(String str);
|
||||||
|
Class<T> getActionClass();
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package lol.pyr.znpcsplus.interaction;
|
||||||
|
|
||||||
|
import lol.pyr.director.adventure.command.CommandHandler;
|
||||||
|
|
||||||
|
public interface InteractionCommandHandler extends CommandHandler {
|
||||||
|
String getSubcommandName();
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package lol.pyr.znpcsplus.interaction.types;
|
package lol.pyr.znpcsplus.interaction.consolecommand;
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
||||||
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
|
@ -0,0 +1,64 @@
|
||||||
|
package lol.pyr.znpcsplus.interaction.consolecommand;
|
||||||
|
|
||||||
|
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.npc.NpcEntryImpl;
|
||||||
|
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
||||||
|
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;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ConsoleCommandActionType implements InteractionActionType<ConsoleCommandAction>, InteractionCommandHandler {
|
||||||
|
private final TaskScheduler scheduler;
|
||||||
|
private final NpcRegistryImpl npcRegistry;
|
||||||
|
|
||||||
|
public ConsoleCommandActionType(TaskScheduler scheduler, NpcRegistryImpl npcRegistry) {
|
||||||
|
this.scheduler = scheduler;
|
||||||
|
this.npcRegistry = npcRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String serialize(ConsoleCommandAction obj) {
|
||||||
|
return Base64.getEncoder().encodeToString(obj.getCommand().getBytes(StandardCharsets.UTF_8)) + ";" + obj.getCooldown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ConsoleCommandAction deserialize(String str) {
|
||||||
|
String[] split = str.split(";");
|
||||||
|
return new ConsoleCommandAction(scheduler, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), Long.parseLong(split[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<ConsoleCommandAction> getActionClass() {
|
||||||
|
return ConsoleCommandAction.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSubcommandName() {
|
||||||
|
return "consolecommand";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(CommandContext context) throws CommandExecutionException {
|
||||||
|
context.setUsage(context.getUsage() + " consolecommand <id> <cooldown seconds> <command>");
|
||||||
|
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
|
||||||
|
long cooldown = (long) (context.parse(Double.class) * 1000D);
|
||||||
|
String command = context.dumpAllArgs();
|
||||||
|
entry.getNpc().addAction(new ConsoleCommandAction(scheduler, command, cooldown));
|
||||||
|
context.send(Component.text("Added a console command action to the npc with the command " + command, NamedTextColor.GREEN));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
||||||
|
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.modifiableIds());
|
||||||
|
if (context.argSize() == 2) return context.suggestLiteral("1");
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package lol.pyr.znpcsplus.interaction.types;
|
package lol.pyr.znpcsplus.interaction.message;
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
||||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
|
@ -0,0 +1,68 @@
|
||||||
|
package lol.pyr.znpcsplus.interaction.message;
|
||||||
|
|
||||||
|
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.npc.NpcEntryImpl;
|
||||||
|
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
||||||
|
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.minimessage.MiniMessage;
|
||||||
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class MessageActionType implements InteractionActionType<MessageAction>, InteractionCommandHandler {
|
||||||
|
private final BukkitAudiences adventure;
|
||||||
|
private final LegacyComponentSerializer textSerializer;
|
||||||
|
private final NpcRegistryImpl npcRegistry;
|
||||||
|
|
||||||
|
public MessageActionType(BukkitAudiences adventure, LegacyComponentSerializer textSerializer, NpcRegistryImpl npcRegistry) {
|
||||||
|
this.adventure = adventure;
|
||||||
|
this.textSerializer = textSerializer;
|
||||||
|
this.npcRegistry = npcRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String serialize(MessageAction obj) {
|
||||||
|
return Base64.getEncoder().encodeToString(MiniMessage.miniMessage().serialize(obj.getMessage()).getBytes(StandardCharsets.UTF_8)) + ";" + obj.getCooldown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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]));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<MessageAction> getActionClass() {
|
||||||
|
return MessageAction.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSubcommandName() {
|
||||||
|
return "message";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(CommandContext context) throws CommandExecutionException {
|
||||||
|
context.setUsage(context.getUsage() + " consolecommand <id> <cooldown seconds> <message>");
|
||||||
|
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
|
||||||
|
long cooldown = (long) (context.parse(Double.class) * 1000D);
|
||||||
|
Component message = textSerializer.deserialize(context.dumpAllArgs());
|
||||||
|
entry.getNpc().addAction(new MessageAction(adventure, message, cooldown));
|
||||||
|
context.send(Component.text("Added a message action to the npc with the message ", NamedTextColor.GREEN).append(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
||||||
|
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.modifiableIds());
|
||||||
|
if (context.argSize() == 2) return context.suggestLiteral("1");
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package lol.pyr.znpcsplus.interaction.types;
|
package lol.pyr.znpcsplus.interaction.playercommand;
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
||||||
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
|
@ -0,0 +1,64 @@
|
||||||
|
package lol.pyr.znpcsplus.interaction.playercommand;
|
||||||
|
|
||||||
|
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.npc.NpcEntryImpl;
|
||||||
|
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
||||||
|
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;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PlayerCommandActionType implements InteractionActionType<PlayerCommandAction>, InteractionCommandHandler {
|
||||||
|
private final TaskScheduler scheduler;
|
||||||
|
private final NpcRegistryImpl npcRegistry;
|
||||||
|
|
||||||
|
public PlayerCommandActionType(TaskScheduler scheduler, NpcRegistryImpl npcRegistry) {
|
||||||
|
this.scheduler = scheduler;
|
||||||
|
this.npcRegistry = npcRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String serialize(PlayerCommandAction obj) {
|
||||||
|
return Base64.getEncoder().encodeToString(obj.getCommand().getBytes(StandardCharsets.UTF_8)) + ";" + obj.getCooldown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PlayerCommandAction deserialize(String str) {
|
||||||
|
String[] split = str.split(";");
|
||||||
|
return new PlayerCommandAction(scheduler, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), Long.parseLong(split[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<PlayerCommandAction> getActionClass() {
|
||||||
|
return PlayerCommandAction.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSubcommandName() {
|
||||||
|
return "playercommand";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(CommandContext context) throws CommandExecutionException {
|
||||||
|
context.setUsage(context.getUsage() + " playercommand <id> <cooldown seconds> <command>");
|
||||||
|
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
|
||||||
|
long cooldown = (long) (context.parse(Double.class) * 1000D);
|
||||||
|
String command = context.dumpAllArgs();
|
||||||
|
entry.getNpc().addAction(new PlayerCommandAction(scheduler, command, cooldown));
|
||||||
|
context.send(Component.text("Added a player command action to the npc with the command " + command, NamedTextColor.GREEN));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
||||||
|
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.modifiableIds());
|
||||||
|
if (context.argSize() == 2) return context.suggestLiteral("1");
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,27 +0,0 @@
|
||||||
package lol.pyr.znpcsplus.interaction.serialization;
|
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.types.ConsoleCommandAction;
|
|
||||||
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
|
||||||
import lol.pyr.znpcsplus.util.StringSerializer;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Base64;
|
|
||||||
|
|
||||||
public class ConsoleCommandActionSerializer implements StringSerializer<ConsoleCommandAction> {
|
|
||||||
private final TaskScheduler scheduler;
|
|
||||||
|
|
||||||
public ConsoleCommandActionSerializer(TaskScheduler scheduler) {
|
|
||||||
this.scheduler = scheduler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String serialize(ConsoleCommandAction obj) {
|
|
||||||
return Base64.getEncoder().encodeToString(obj.getCommand().getBytes(StandardCharsets.UTF_8)) + ";" + obj.getCooldown();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ConsoleCommandAction deserialize(String str) {
|
|
||||||
String[] split = str.split(";");
|
|
||||||
return new ConsoleCommandAction(scheduler, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), Long.parseLong(split[1]));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,28 +0,0 @@
|
||||||
package lol.pyr.znpcsplus.interaction.serialization;
|
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.types.MessageAction;
|
|
||||||
import lol.pyr.znpcsplus.util.StringSerializer;
|
|
||||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Base64;
|
|
||||||
|
|
||||||
public class MessageActionSerializer implements StringSerializer<MessageAction> {
|
|
||||||
private final BukkitAudiences adventure;
|
|
||||||
|
|
||||||
public MessageActionSerializer(BukkitAudiences adventure) {
|
|
||||||
this.adventure = adventure;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String serialize(MessageAction obj) {
|
|
||||||
return Base64.getEncoder().encodeToString(MiniMessage.miniMessage().serialize(obj.getMessage()).getBytes(StandardCharsets.UTF_8)) + ";" + obj.getCooldown();
|
|
||||||
}
|
|
||||||
|
|
||||||
@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]));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
package lol.pyr.znpcsplus.interaction.serialization;
|
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.types.PlayerCommandAction;
|
|
||||||
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
|
||||||
import lol.pyr.znpcsplus.util.StringSerializer;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Base64;
|
|
||||||
|
|
||||||
public class PlayerCommandActionSerializer implements StringSerializer<PlayerCommandAction> {
|
|
||||||
private final TaskScheduler scheduler;
|
|
||||||
|
|
||||||
public PlayerCommandActionSerializer(TaskScheduler scheduler) {
|
|
||||||
this.scheduler = scheduler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String serialize(PlayerCommandAction obj) {
|
|
||||||
return Base64.getEncoder().encodeToString(obj.getCommand().getBytes(StandardCharsets.UTF_8)) + ";" + obj.getCooldown();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PlayerCommandAction deserialize(String str) {
|
|
||||||
String[] split = str.split(";");
|
|
||||||
return new PlayerCommandAction(scheduler, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), Long.parseLong(split[1]));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
package lol.pyr.znpcsplus.interaction.serialization;
|
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.types.SwitchServerAction;
|
|
||||||
import lol.pyr.znpcsplus.util.BungeeUtil;
|
|
||||||
import lol.pyr.znpcsplus.util.StringSerializer;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Base64;
|
|
||||||
|
|
||||||
public class SwitchServerActionSerializer implements StringSerializer<SwitchServerAction> {
|
|
||||||
private final BungeeUtil bungeeUtil;
|
|
||||||
|
|
||||||
public SwitchServerActionSerializer(BungeeUtil bungeeUtil) {
|
|
||||||
this.bungeeUtil = bungeeUtil;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String serialize(SwitchServerAction obj) {
|
|
||||||
return Base64.getEncoder().encodeToString(obj.getServer().getBytes(StandardCharsets.UTF_8)) + ";" + obj.getCooldown();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SwitchServerAction deserialize(String str) {
|
|
||||||
String[] split = str.split(";");
|
|
||||||
return new SwitchServerAction(bungeeUtil, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), Long.parseLong(split[1]));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +1,4 @@
|
||||||
package lol.pyr.znpcsplus.interaction.types;
|
package lol.pyr.znpcsplus.interaction.switchserver;
|
||||||
|
|
||||||
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
import lol.pyr.znpcsplus.interaction.InteractionAction;
|
||||||
import lol.pyr.znpcsplus.util.BungeeUtil;
|
import lol.pyr.znpcsplus.util.BungeeUtil;
|
|
@ -0,0 +1,64 @@
|
||||||
|
package lol.pyr.znpcsplus.interaction.switchserver;
|
||||||
|
|
||||||
|
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.npc.NpcEntryImpl;
|
||||||
|
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
|
||||||
|
import lol.pyr.znpcsplus.util.BungeeUtil;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SwitchServerActionType implements InteractionActionType<SwitchServerAction>, InteractionCommandHandler {
|
||||||
|
private final BungeeUtil bungeeUtil;
|
||||||
|
private final NpcRegistryImpl npcRegistry;
|
||||||
|
|
||||||
|
public SwitchServerActionType(BungeeUtil bungeeUtil, NpcRegistryImpl npcRegistry) {
|
||||||
|
this.bungeeUtil = bungeeUtil;
|
||||||
|
this.npcRegistry = npcRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String serialize(SwitchServerAction obj) {
|
||||||
|
return Base64.getEncoder().encodeToString(obj.getServer().getBytes(StandardCharsets.UTF_8)) + ";" + obj.getCooldown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SwitchServerAction deserialize(String str) {
|
||||||
|
String[] split = str.split(";");
|
||||||
|
return new SwitchServerAction(bungeeUtil, new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), Long.parseLong(split[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<SwitchServerAction> getActionClass() {
|
||||||
|
return SwitchServerAction.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSubcommandName() {
|
||||||
|
return "switchserver";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(CommandContext context) throws CommandExecutionException {
|
||||||
|
context.setUsage(context.getUsage() + " switchserver <id> <cooldown seconds> <server>");
|
||||||
|
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
|
||||||
|
long cooldown = (long) (context.parse(Double.class) * 1000D);
|
||||||
|
String server = context.dumpAllArgs();
|
||||||
|
entry.getNpc().addAction(new SwitchServerAction(bungeeUtil, server, cooldown));
|
||||||
|
context.send(Component.text("Added a switch server action to the npc with the server " + server, NamedTextColor.GREEN));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> suggest(CommandContext context) throws CommandExecutionException {
|
||||||
|
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.modifiableIds());
|
||||||
|
if (context.argSize() == 2) return context.suggestLiteral("1");
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue