generalize action types more

This commit is contained in:
Pyrbu 2023-05-29 21:10:51 +01:00
parent 962664b5b5
commit cd2cdd6869
6 changed files with 44 additions and 70 deletions

@ -5,8 +5,6 @@ import lol.pyr.director.adventure.command.CommandHandler;
import lol.pyr.director.common.command.CommandExecutionException; import lol.pyr.director.common.command.CommandExecutionException;
import lol.pyr.znpcsplus.interaction.ActionRegistry; import lol.pyr.znpcsplus.interaction.ActionRegistry;
import lol.pyr.znpcsplus.interaction.InteractionCommandHandler; 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 lol.pyr.znpcsplus.npc.NpcRegistryImpl;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
@ -27,11 +25,11 @@ public class ActionAddCommand implements CommandHandler {
@Override @Override
public void run(CommandContext context) throws CommandExecutionException { public void run(CommandContext context) throws CommandExecutionException {
List<InteractionCommandHandler> commands = actionRegistry.getCommands(); List<InteractionCommandHandler> commands = actionRegistry.getCommands();
context.setUsage(context.getLabel() + " action add <id> <action type> ..."); context.setUsage(context.getLabel() + " action add <action type> ...");
NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc();
String sub = context.popString(); String sub = context.popString();
for (InteractionCommandHandler command : commands) if (command.getSubcommandName().equalsIgnoreCase(sub)) { for (InteractionCommandHandler command : commands) if (command.getSubcommandName().equalsIgnoreCase(sub)) {
command.parse(context, npc); context.setUsage(context.getLabel() + " action add ");
command.run(context);
return; return;
} }
context.send(Component.text("Invalid action type, available action types:\n" + context.send(Component.text("Invalid action type, available action types:\n" +

@ -3,10 +3,22 @@ package lol.pyr.znpcsplus.interaction;
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 lol.pyr.znpcsplus.npc.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcImpl;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
public interface InteractionCommandHandler extends CommandHandler { public interface InteractionCommandHandler extends CommandHandler {
String getSubcommandName(); 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));
}
} }

@ -6,10 +6,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionType;
import lol.pyr.znpcsplus.interaction.InteractionAction; import lol.pyr.znpcsplus.interaction.InteractionAction;
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.npc.NpcImpl;
import lol.pyr.znpcsplus.scheduling.TaskScheduler; 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.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
@ -46,22 +43,16 @@ public class ConsoleCommandActionType implements InteractionActionType<ConsoleCo
} }
@Override @Override
public InteractionAction parse(CommandContext context, NpcImpl npc) throws CommandExecutionException { public void appendUsage(CommandContext context) {
context.setUsage(context.getUsage() + getSubcommandName() + " <type> <cooldown seconds> <command>"); context.setUsage(context.getUsage() + " " + getSubcommandName() + " <id> <click type> <cooldown seconds> <server>");
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;
} }
@Override @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 @Override

@ -6,10 +6,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionType;
import lol.pyr.znpcsplus.interaction.InteractionAction; import lol.pyr.znpcsplus.interaction.InteractionAction;
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.npc.NpcImpl;
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.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -49,22 +46,16 @@ public class MessageActionType implements InteractionActionType<MessageAction>,
} }
@Override @Override
public InteractionAction parse(CommandContext context, NpcImpl npc) throws CommandExecutionException { public void appendUsage(CommandContext context) {
context.setUsage(context.getUsage() + " <type> <cooldown seconds> <message>"); context.setUsage(context.getUsage() + " " + getSubcommandName() + " <id> <click type> <cooldown seconds> <message>");
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;
} }
@Override @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 @Override

@ -6,10 +6,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionType;
import lol.pyr.znpcsplus.interaction.InteractionAction; import lol.pyr.znpcsplus.interaction.InteractionAction;
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.npc.NpcImpl;
import lol.pyr.znpcsplus.scheduling.TaskScheduler; 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.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
@ -46,22 +43,16 @@ public class PlayerCommandActionType implements InteractionActionType<PlayerComm
} }
@Override @Override
public InteractionAction parse(CommandContext context, NpcImpl npc) throws CommandExecutionException { public void appendUsage(CommandContext context) {
context.setUsage(context.getUsage() + getSubcommandName() + " <type> <cooldown seconds> <command>"); context.setUsage(context.getUsage() + " " + getSubcommandName() + " <id> <click type> <cooldown seconds> <command>");
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;
} }
@Override @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 @Override

@ -6,10 +6,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionType;
import lol.pyr.znpcsplus.interaction.InteractionAction; import lol.pyr.znpcsplus.interaction.InteractionAction;
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.npc.NpcImpl;
import lol.pyr.znpcsplus.util.BungeeConnector; 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.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
@ -46,22 +43,16 @@ public class SwitchServerActionType implements InteractionActionType<SwitchServe
} }
@Override @Override
public InteractionAction parse(CommandContext context, NpcImpl npc) throws CommandExecutionException { public void appendUsage(CommandContext context) {
context.setUsage(context.getUsage() +getSubcommandName() + " <type> <cooldown seconds> <server>"); context.setUsage(context.getUsage() + " " + getSubcommandName() + " <id> <click type> <cooldown seconds> <server>");
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;
} }
@Override @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 @Override