add npc toggle command

This commit is contained in:
Pyrbu 2023-06-19 15:29:34 +02:00
parent 78fbb13348
commit 60b5ac9683
7 changed files with 53 additions and 1 deletions

@ -233,6 +233,7 @@ public class ZNpcsPlus extends JavaPlugin {
manager.registerCommand("npc", new MultiCommand(loadHelpMessage("root")) manager.registerCommand("npc", new MultiCommand(loadHelpMessage("root"))
.addSubcommand("create", new CreateCommand(npcRegistry, typeRegistry)) .addSubcommand("create", new CreateCommand(npcRegistry, typeRegistry))
.addSubcommand("toggle", new ToggleCommand(npcRegistry))
.addSubcommand("skin", new SkinCommand(skinCache, npcRegistry, typeRegistry, propertyRegistry)) .addSubcommand("skin", new SkinCommand(skinCache, npcRegistry, typeRegistry, propertyRegistry))
.addSubcommand("delete", new DeleteCommand(npcRegistry, adventure)) .addSubcommand("delete", new DeleteCommand(npcRegistry, adventure))
.addSubcommand("move", new MoveCommand(npcRegistry)) .addSubcommand("move", new MoveCommand(npcRegistry))

@ -24,7 +24,7 @@ public class ListCommand implements CommandHandler {
for (String id : npcRegistry.getModifiableIds()) { for (String id : npcRegistry.getModifiableIds()) {
NpcImpl npc = npcRegistry.getById(id).getNpc(); NpcImpl npc = npcRegistry.getById(id).getNpc();
NpcLocation location = npc.getLocation(); NpcLocation location = npc.getLocation();
component.append(Component.text("ID: " + id, NamedTextColor.GREEN)) component.append(Component.text("ID: " + id, npc.isEnabled() ? NamedTextColor.GREEN : NamedTextColor.RED))
.append(Component.text(" | ", NamedTextColor.GRAY)) .append(Component.text(" | ", NamedTextColor.GRAY))
.append(Component.text("Type: ", NamedTextColor.GREEN)) .append(Component.text("Type: ", NamedTextColor.GREEN))
.append(Component.text(npc.getType().getName(), NamedTextColor.GREEN)) .append(Component.text(npc.getType().getName(), NamedTextColor.GREEN))

@ -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.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;
public class ToggleCommand implements CommandHandler {
private final NpcRegistryImpl npcRegistry;
public ToggleCommand(NpcRegistryImpl npcRegistry) {
this.npcRegistry = npcRegistry;
}
@Override
public void run(CommandContext context) throws CommandExecutionException {
context.setUsage(context.getLabel() + " toggle <id>");
NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc();
boolean enabled = !npc.isEnabled();
npc.setEnabled(enabled);
context.send(Component.text("NPC has been " + (enabled ? "enabled" : "disabled"), NamedTextColor.GREEN));
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds());
return Collections.emptyList();
}
}

@ -24,6 +24,7 @@ public class NpcImpl extends Viewable implements Npc {
private PacketEntity entity; private PacketEntity entity;
private NpcLocation location; private NpcLocation location;
private NpcTypeImpl type; private NpcTypeImpl type;
private boolean enabled = true;
private final HologramImpl hologram; private final HologramImpl hologram;
private final UUID uuid; private final UUID uuid;
@ -78,6 +79,15 @@ public class NpcImpl extends Viewable implements Npc {
return hologram; return hologram;
} }
public void setEnabled(boolean enabled) {
this.enabled = enabled;
if (!enabled) delete();
}
public boolean isEnabled() {
return enabled;
}
public UUID getUuid() { public UUID getUuid() {
return uuid; return uuid;
} }

@ -52,6 +52,8 @@ public class YamlStorage implements NpcStorage {
NpcImpl npc = new NpcImpl(uuid, configManager, packetFactory, textSerializer, config.getString("world"), NpcImpl npc = new NpcImpl(uuid, configManager, packetFactory, textSerializer, config.getString("world"),
typeRegistry.getByName(config.getString("type")), deserializeLocation(config.getConfigurationSection("location"))); typeRegistry.getByName(config.getString("type")), deserializeLocation(config.getConfigurationSection("location")));
if (config.isBoolean("enabled")) npc.setEnabled(config.getBoolean("enabled"));
ConfigurationSection properties = config.getConfigurationSection("properties"); ConfigurationSection properties = config.getConfigurationSection("properties");
if (properties != null) { if (properties != null) {
for (String key : properties.getKeys(false)) { for (String key : properties.getKeys(false)) {
@ -84,6 +86,7 @@ public class YamlStorage implements NpcStorage {
config.set("allow-commands", entry.isAllowCommandModification()); config.set("allow-commands", entry.isAllowCommandModification());
NpcImpl npc = entry.getNpc(); NpcImpl npc = entry.getNpc();
config.set("enabled", npc.isEnabled());
config.set("uuid", npc.getUuid().toString()); config.set("uuid", npc.getUuid().toString());
config.set("world", npc.getWorldName()); config.set("world", npc.getWorldName());
config.set("location", serializeLocation(npc.getLocation())); config.set("location", serializeLocation(npc.getLocation()));

@ -31,6 +31,7 @@ public class NpcProcessorTask extends BukkitRunnable {
EntityPropertyImpl<Boolean> lookProperty = propertyRegistry.getByName("look", Boolean.class); EntityPropertyImpl<Boolean> lookProperty = propertyRegistry.getByName("look", Boolean.class);
for (NpcEntryImpl entry : npcRegistry.getProcessable()) { for (NpcEntryImpl entry : npcRegistry.getProcessable()) {
NpcImpl npc = entry.getNpc(); NpcImpl npc = entry.getNpc();
if (!npc.isEnabled()) continue;
double closestDist = Double.MAX_VALUE; double closestDist = Double.MAX_VALUE;
Player closest = null; Player closest = null;

@ -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 toggle <id>
<gold>* <yellow>/npc list <gold>* <yellow>/npc list
<gold>* <yellow>/npc type <id> <type> <gold>* <yellow>/npc type <id> <type>