Added Action Edit command

This commit is contained in:
D3v1s0m 2023-05-28 11:02:00 +05:30
parent 84af1753e6
commit 8d073aa2d9
No known key found for this signature in database
GPG Key ID: 3B6EC35367B8D82E
10 changed files with 170 additions and 68 deletions

@ -13,9 +13,11 @@ import lol.pyr.director.adventure.parse.primitive.DoubleParser;
import lol.pyr.director.adventure.parse.primitive.IntegerParser;
import lol.pyr.director.common.message.Message;
import lol.pyr.znpcsplus.api.NpcApiProvider;
import lol.pyr.znpcsplus.api.interaction.InteractionType;
import lol.pyr.znpcsplus.commands.*;
import lol.pyr.znpcsplus.commands.action.ActionAddCommand;
import lol.pyr.znpcsplus.commands.action.ActionDeleteCommand;
import lol.pyr.znpcsplus.commands.action.ActionEditCommand;
import lol.pyr.znpcsplus.commands.action.ActionListCommand;
import lol.pyr.znpcsplus.commands.hologram.*;
import lol.pyr.znpcsplus.commands.storage.LoadAllCommand;
@ -25,7 +27,6 @@ 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.api.interaction.InteractionType;
import lol.pyr.znpcsplus.metadata.*;
import lol.pyr.znpcsplus.npc.*;
import lol.pyr.znpcsplus.packets.*;
@ -117,7 +118,7 @@ public class ZNpcsPlus extends JavaPlugin {
log(ChatColor.WHITE + " * Registerring components...");
typeRegistry.registerDefault(packetEvents, propertyRegistry);
actionRegistry.registerTypes(npcRegistry, scheduler, adventure, bungeeConnector, textSerializer);
actionRegistry.registerTypes(scheduler, adventure, bungeeConnector, textSerializer);
packetEvents.getEventManager().registerListener(new InteractionPacketListener(userManager, npcRegistry), PacketListenerPriority.MONITOR);
new Metrics(this, PLUGIN_ID);
pluginManager.registerEvents(new UserListener(userManager), this);
@ -243,8 +244,9 @@ public class ZNpcsPlus extends JavaPlugin {
.addSubcommand("set", new HoloSetCommand(npcRegistry, textSerializer))
.addSubcommand("offset", new HoloOffsetCommand(npcRegistry)))
.addSubcommand("action", new MultiCommand()
.addSubcommand("add", new ActionAddCommand(actionRegistry))
.addSubcommand("add", new ActionAddCommand(npcRegistry, actionRegistry))
.addSubcommand("delete", new ActionDeleteCommand(npcRegistry))
.addSubcommand("edit", new ActionEditCommand(npcRegistry, actionRegistry))
.addSubcommand("list", new ActionListCommand()))
);
}

@ -5,6 +5,9 @@ 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;
@ -13,19 +16,22 @@ import java.util.List;
import java.util.stream.Collectors;
public class ActionAddCommand implements CommandHandler {
private final NpcRegistryImpl npcRegistry;
private final ActionRegistry actionRegistry;
public ActionAddCommand(ActionRegistry actionRegistry) {
public ActionAddCommand(NpcRegistryImpl npcRegistry, ActionRegistry actionRegistry) {
this.npcRegistry = npcRegistry;
this.actionRegistry = actionRegistry;
}
@Override
public void run(CommandContext context) throws CommandExecutionException {
List<InteractionCommandHandler> commands = actionRegistry.getCommands();
context.setUsage(context.getLabel() + " action add <action type>");
context.setUsage(context.getLabel() + " action add <id> <action type> ...");
NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc();
String sub = context.popString();
for (InteractionCommandHandler command : commands) if (command.getSubcommandName().equalsIgnoreCase(sub)) {
command.run(context);
command.parse(context, npc);
return;
}
context.send(Component.text("Invalid action type, available action types:\n" +
@ -34,8 +40,10 @@ public class ActionAddCommand implements CommandHandler {
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
List<InteractionCommandHandler> commands = actionRegistry.getCommands();
if (context.argSize() == 1) return context.suggestStream(commands.stream().map(InteractionCommandHandler::getSubcommandName));
if (context.argSize() == 2) return context.suggestStream(commands.stream().map(InteractionCommandHandler::getSubcommandName));
context.popString();
String sub = context.popString();
for (InteractionCommandHandler command : commands) if (command.getSubcommandName().equalsIgnoreCase(sub)) return command.suggest(context);
return Collections.emptyList();

@ -0,0 +1,64 @@
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.InteractionAction;
import lol.pyr.znpcsplus.interaction.InteractionCommandHandler;
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
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.Collectors;
import java.util.stream.Stream;
public class ActionEditCommand implements CommandHandler {
private final NpcRegistryImpl npcRegistry;
private final ActionRegistry actionRegistry;
private InteractionCommandHandler commandHandler = null;
public ActionEditCommand(NpcRegistryImpl npcRegistry, ActionRegistry actionRegistry) {
this.npcRegistry = npcRegistry;
this.actionRegistry = actionRegistry;
}
@Override
public void run(CommandContext context) throws CommandExecutionException {
context.setUsage(context.getLabel() + " action edit <id> <action id> <action type> ...");
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
int index = context.parse(Integer.class);
if (index >= entry.getNpc().getActions().size() || index < 0) context.halt(Component.text("That npc doesn't have any action with the index " + index, NamedTextColor.RED));
List<InteractionCommandHandler> commands = actionRegistry.getCommands();
String sub = context.popString();
for (InteractionCommandHandler command : commands) if (command.getSubcommandName().equalsIgnoreCase(sub)) {
this.commandHandler = command;
}
if (this.commandHandler == null) {
context.send(Component.text("Invalid action type, available action types:\n" +
commands.stream().map(InteractionCommandHandler::getSubcommandName).collect(Collectors.joining(", ")), NamedTextColor.RED));
}
InteractionAction newAction = this.commandHandler.parse(context, null);
entry.getNpc().editAction(index, newAction);
context.send(Component.text("Edited action with index " + index + " of Npc " + entry.getId(), NamedTextColor.GREEN));
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
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));
List<InteractionCommandHandler> commands = actionRegistry.getCommands();
if (context.argSize() == 3) return context.suggestStream(commands.stream().map(InteractionCommandHandler::getSubcommandName));
context.popString();
context.popString();
String sub = context.popString();
for (InteractionCommandHandler command : commands) if (command.getSubcommandName().equalsIgnoreCase(sub)) return command.suggest(context);
return Collections.emptyList();
}
}

@ -4,7 +4,6 @@ import lol.pyr.znpcsplus.interaction.consolecommand.ConsoleCommandActionType;
import lol.pyr.znpcsplus.interaction.message.MessageActionType;
import lol.pyr.znpcsplus.interaction.playercommand.PlayerCommandActionType;
import lol.pyr.znpcsplus.interaction.switchserver.SwitchServerActionType;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import lol.pyr.znpcsplus.util.BungeeConnector;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
@ -22,11 +21,11 @@ public class ActionRegistry {
public ActionRegistry() {
}
public void registerTypes(NpcRegistryImpl npcRegistry, TaskScheduler taskScheduler, BukkitAudiences adventure, BungeeConnector bungeeConnector, LegacyComponentSerializer textSerializer) {
register(new ConsoleCommandActionType(taskScheduler, npcRegistry));
register(new PlayerCommandActionType(taskScheduler, npcRegistry));
register(new SwitchServerActionType(bungeeConnector, npcRegistry));
register(new MessageActionType(adventure, textSerializer, npcRegistry));
public void registerTypes(TaskScheduler taskScheduler, BukkitAudiences adventure, BungeeConnector bungeeConnector, LegacyComponentSerializer textSerializer) {
register(new ConsoleCommandActionType(taskScheduler));
register(new PlayerCommandActionType(taskScheduler));
register(new SwitchServerActionType(bungeeConnector));
register(new MessageActionType(adventure, textSerializer));
}
public void register(InteractionActionType<?> type) {

@ -1,7 +1,12 @@
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.NpcImpl;
public interface InteractionCommandHandler extends CommandHandler {
String getSubcommandName();
InteractionAction parse(CommandContext context, NpcImpl npc) throws CommandExecutionException;
}

@ -2,11 +2,11 @@ package lol.pyr.znpcsplus.interaction.consolecommand;
import lol.pyr.director.adventure.command.CommandContext;
import lol.pyr.director.common.command.CommandExecutionException;
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.api.interaction.InteractionType;
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
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;
@ -18,11 +18,9 @@ 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) {
public ConsoleCommandActionType(TaskScheduler scheduler) {
this.scheduler = scheduler;
this.npcRegistry = npcRegistry;
}
@Override
@ -48,21 +46,28 @@ public class ConsoleCommandActionType implements InteractionActionType<ConsoleCo
}
@Override
public void run(CommandContext context) throws CommandExecutionException {
context.setUsage(context.getUsage() + " consolecommand <id> <type> <cooldown seconds> <command>");
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
public InteractionAction parse(CommandContext context, NpcImpl npc) throws CommandExecutionException {
context.setUsage(context.getUsage() + getSubcommandName() + " <type> <cooldown seconds> <command>");
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, type, cooldown));
context.send(Component.text("Added a console command action to the npc with the command " + command, NamedTextColor.GREEN));
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
public void run(CommandContext context) throws CommandExecutionException {
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
if (context.argSize() == 2) return context.suggestEnum(InteractionType.values());
if (context.argSize() == 3) return context.suggestLiteral("1");
if (context.argSize() == 1) return context.suggestEnum(InteractionType.values());
if (context.argSize() == 2) return context.suggestLiteral("1");
return Collections.emptyList();
}
}

@ -3,10 +3,10 @@ package lol.pyr.znpcsplus.interaction.message;
import lol.pyr.director.adventure.command.CommandContext;
import lol.pyr.director.common.command.CommandExecutionException;
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.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
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;
@ -20,12 +20,10 @@ 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) {
public MessageActionType(BukkitAudiences adventure, LegacyComponentSerializer textSerializer) {
this.adventure = adventure;
this.textSerializer = textSerializer;
this.npcRegistry = npcRegistry;
}
@Override
@ -51,21 +49,28 @@ public class MessageActionType implements InteractionActionType<MessageAction>,
}
@Override
public void run(CommandContext context) throws CommandExecutionException {
context.setUsage(context.getUsage() + " message <id> <type> <cooldown seconds> <message>");
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
public InteractionAction parse(CommandContext context, NpcImpl npc) throws CommandExecutionException {
context.setUsage(context.getUsage() + " <type> <cooldown seconds> <message>");
InteractionType type = context.parse(InteractionType.class);
long cooldown = (long) (context.parse(Double.class) * 1000D);
String message = context.dumpAllArgs();
entry.getNpc().addAction(new MessageAction(adventure, message, type, textSerializer, cooldown));
context.send(Component.text("Added a message action to the npc with the message ", NamedTextColor.GREEN).append(Component.text(message)));
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
public void run(CommandContext context) throws CommandExecutionException {
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
if (context.argSize() == 2) return context.suggestEnum(InteractionType.values());
if (context.argSize() == 3) return context.suggestLiteral("1");
if (context.argSize() == 1) return context.suggestEnum(InteractionType.values());
if (context.argSize() == 2) return context.suggestLiteral("1");
return Collections.emptyList();
}
}

@ -2,11 +2,11 @@ package lol.pyr.znpcsplus.interaction.playercommand;
import lol.pyr.director.adventure.command.CommandContext;
import lol.pyr.director.common.command.CommandExecutionException;
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.api.interaction.InteractionType;
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
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;
@ -18,11 +18,9 @@ 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) {
public PlayerCommandActionType(TaskScheduler scheduler) {
this.scheduler = scheduler;
this.npcRegistry = npcRegistry;
}
@Override
@ -48,21 +46,28 @@ public class PlayerCommandActionType implements InteractionActionType<PlayerComm
}
@Override
public void run(CommandContext context) throws CommandExecutionException {
context.setUsage(context.getUsage() + " playercommand <id> <type> <cooldown seconds> <command>");
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
public InteractionAction parse(CommandContext context, NpcImpl npc) throws CommandExecutionException {
context.setUsage(context.getUsage() + getSubcommandName() + " <type> <cooldown seconds> <command>");
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, type, cooldown));
context.send(Component.text("Added a player command action to the npc with the command " + command, NamedTextColor.GREEN));
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
public void run(CommandContext context) throws CommandExecutionException {
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
if (context.argSize() == 2) return context.suggestEnum(InteractionType.values());
if (context.argSize() == 3) return context.suggestLiteral("1");
if (context.argSize() == 1) return context.suggestEnum(InteractionType.values());
if (context.argSize() == 2) return context.suggestLiteral("1");
return Collections.emptyList();
}
}

@ -2,11 +2,11 @@ package lol.pyr.znpcsplus.interaction.switchserver;
import lol.pyr.director.adventure.command.CommandContext;
import lol.pyr.director.common.command.CommandExecutionException;
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.api.interaction.InteractionType;
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
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;
@ -18,11 +18,9 @@ import java.util.List;
public class SwitchServerActionType implements InteractionActionType<SwitchServerAction>, InteractionCommandHandler {
private final BungeeConnector bungeeConnector;
private final NpcRegistryImpl npcRegistry;
public SwitchServerActionType(BungeeConnector bungeeConnector, NpcRegistryImpl npcRegistry) {
public SwitchServerActionType(BungeeConnector bungeeConnector) {
this.bungeeConnector = bungeeConnector;
this.npcRegistry = npcRegistry;
}
@Override
@ -48,21 +46,28 @@ public class SwitchServerActionType implements InteractionActionType<SwitchServe
}
@Override
public void run(CommandContext context) throws CommandExecutionException {
context.setUsage(context.getUsage() + " switchserver <id> <type> <cooldown seconds> <server>");
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
public InteractionAction parse(CommandContext context, NpcImpl npc) throws CommandExecutionException {
context.setUsage(context.getUsage() +getSubcommandName() + " <type> <cooldown seconds> <server>");
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, type, cooldown));
context.send(Component.text("Added a switch server action to the npc with the server " + server, NamedTextColor.GREEN));
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
public void run(CommandContext context) throws CommandExecutionException {
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
if (context.argSize() == 2) return context.suggestEnum(InteractionType.values());
if (context.argSize() == 3) return context.suggestLiteral("1");
if (context.argSize() == 1) return context.suggestEnum(InteractionType.values());
if (context.argSize() == 2) return context.suggestLiteral("1");
return Collections.emptyList();
}
}

@ -146,4 +146,8 @@ public class NpcImpl extends Viewable implements Npc {
public void addAction(InteractionAction action) {
actions.add(action);
}
public void editAction(int index, InteractionAction action) {
actions.set(index, action);
}
}