add change id command (resolves #98)

This commit is contained in:
Pyrbu 2023-10-21 07:36:39 +02:00
parent 600810ab97
commit 11b81ba5fc
4 changed files with 48 additions and 0 deletions

@ -296,6 +296,7 @@ public class ZNpcsPlus {
.addSubcommand("setlocation", new SetLocationCommand(npcRegistry)) .addSubcommand("setlocation", new SetLocationCommand(npcRegistry))
.addSubcommand("lookatme", new LookAtMeCommand(npcRegistry)) .addSubcommand("lookatme", new LookAtMeCommand(npcRegistry))
.addSubcommand("setrotation", new SetRotationCommand(npcRegistry)) .addSubcommand("setrotation", new SetRotationCommand(npcRegistry))
.addSubcommand("changeid", new ChangeIdCommand(npcRegistry))
.addSubcommand("property", new MultiCommand(bootstrap.loadHelpMessage("property")) .addSubcommand("property", new MultiCommand(bootstrap.loadHelpMessage("property"))
.addSubcommand("set", new PropertySetCommand(npcRegistry)) .addSubcommand("set", new PropertySetCommand(npcRegistry))
.addSubcommand("remove", new PropertyRemoveCommand(npcRegistry))) .addSubcommand("remove", new PropertyRemoveCommand(npcRegistry)))

@ -0,0 +1,36 @@
package lol.pyr.znpcsplus.commands;
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.NpcRegistryImpl;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.util.Collections;
import java.util.List;
public class ChangeIdCommand implements CommandHandler {
private final NpcRegistryImpl npcRegistry;
public ChangeIdCommand(NpcRegistryImpl npcRegistry) {
this.npcRegistry = npcRegistry;
}
@Override
public void run(CommandContext context) throws CommandExecutionException {
context.setUsage(context.getLabel() + " changeid <old> <new>");
NpcEntryImpl old = context.parse(NpcEntryImpl.class);
String newId = context.popString();
if (npcRegistry.getById(newId) != null) context.halt(Component.text("There is already an npc with the new id you have provided", NamedTextColor.RED));
npcRegistry.switchIds(old.getId(), newId);
context.send(Component.text("Npc's id changed to " + newId.toLowerCase(), NamedTextColor.GREEN));
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
return Collections.emptyList();
}
}

@ -161,6 +161,16 @@ public class NpcRegistryImpl implements NpcRegistry {
storage.deleteNpc(entry); storage.deleteNpc(entry);
} }
public void switchIds(String oldId, String newId) {
NpcEntryImpl entry = getById(oldId);
delete(oldId);
NpcEntryImpl newEntry = new NpcEntryImpl(newId, entry.getNpc());
newEntry.setSave(entry.isSave());
newEntry.setProcessed(entry.isProcessed());
newEntry.setAllowCommandModification(entry.isAllowCommandModification());
register(newEntry);
}
public void unload() { public void unload() {
npcList.forEach(npcEntry -> npcEntry.getNpc().delete()); npcList.forEach(npcEntry -> npcEntry.getNpc().delete());
} }

@ -4,6 +4,7 @@
<gold>* <yellow>/npc create <id> <type> <gold>* <yellow>/npc create <id> <type>
<gold>* <yellow>/npc delete <id> <gold>* <yellow>/npc delete <id>
<gold>* <yellow>/npc changeid <old> <new>
<gold>* <yellow>/npc toggle <id> <gold>* <yellow>/npc toggle <id>
<gold>* <yellow>/npc list <gold>* <yellow>/npc list
<gold>* <yellow>/npc type <id> <type> <gold>* <yellow>/npc type <id> <type>