improve holo commands

This commit is contained in:
Pyrbu 2023-05-11 07:27:28 +01:00
parent 74af9522ae
commit 191eae3778
7 changed files with 20 additions and 53 deletions

@ -41,6 +41,7 @@ import lol.pyr.znpcsplus.util.ZLocation;
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.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.bstats.bukkit.Metrics; import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -63,6 +64,10 @@ public class ZNpcsPlus extends JavaPlugin {
public static TaskScheduler SCHEDULER; public static TaskScheduler SCHEDULER;
public static BungeeUtil BUNGEE_UTIL; public static BungeeUtil BUNGEE_UTIL;
public static BukkitAudiences ADVENTURE; public static BukkitAudiences ADVENTURE;
public static LegacyComponentSerializer LEGACY_AMPERSAND_SERIALIZER = LegacyComponentSerializer.builder()
.character('&')
.hexCharacter('#')
.hexColors().build();
private boolean enabled = false; private boolean enabled = false;
public static final String DEBUG_NPC_PREFIX = "debug_npc"; public static final String DEBUG_NPC_PREFIX = "debug_npc";
@ -205,11 +210,9 @@ public class ZNpcsPlus extends JavaPlugin {
manager.registerCommand("npc", new MultiCommand() manager.registerCommand("npc", new MultiCommand()
.addSubcommand("action", new ActionCommand()) .addSubcommand("action", new ActionCommand())
.addSubcommand("conversations", new ConversationsCommand())
.addSubcommand("create", new CreateCommand()) .addSubcommand("create", new CreateCommand())
.addSubcommand("delete", new DeleteCommand()) .addSubcommand("delete", new DeleteCommand())
.addSubcommand("move", new MoveCommand()) .addSubcommand("move", new MoveCommand())
.addSubcommand("path", new PathCommand())
.addSubcommand("properties", new PropertiesCommand()) .addSubcommand("properties", new PropertiesCommand())
.addSubcommand("teleport", new TeleportCommand()) .addSubcommand("teleport", new TeleportCommand())
.addSubcommand("list", new ListCommand()) .addSubcommand("list", new ListCommand())

@ -1,20 +0,0 @@
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 java.util.Collections;
import java.util.List;
public class ConversationsCommand implements CommandHandler {
@Override
public void run(CommandContext context) throws CommandExecutionException {
context.send("Not implemented yet.");
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
return Collections.emptyList();
}
}

@ -1,20 +0,0 @@
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 java.util.Collections;
import java.util.List;
public class PathCommand implements CommandHandler {
@Override
public void run(CommandContext context) throws CommandExecutionException {
context.send("Not implemented yet.");
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
return Collections.emptyList();
}
}

@ -3,12 +3,12 @@ package lol.pyr.znpcsplus.commands.hologram;
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.ZNpcsPlus;
import lol.pyr.znpcsplus.hologram.HologramImpl; import lol.pyr.znpcsplus.hologram.HologramImpl;
import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcEntryImpl;
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;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -18,7 +18,7 @@ public class HoloAddCommand implements CommandHandler {
public void run(CommandContext context) throws CommandExecutionException { public void run(CommandContext context) throws CommandExecutionException {
context.setUsage(context.getLabel() + " holo add <npc_id> <text>"); context.setUsage(context.getLabel() + " holo add <npc_id> <text>");
HologramImpl hologram = context.parse(NpcEntryImpl.class).getNpc().getHologram(); HologramImpl hologram = context.parse(NpcEntryImpl.class).getNpc().getHologram();
hologram.addLine(LegacyComponentSerializer.legacyAmpersand().deserialize(context.dumpAllArgs())); hologram.addLine(ZNpcsPlus.LEGACY_AMPERSAND_SERIALIZER.deserialize(context.dumpAllArgs()));
context.send(Component.text("NPC line added!", NamedTextColor.GREEN)); context.send(Component.text("NPC line added!", NamedTextColor.GREEN));
} }

@ -28,7 +28,7 @@ public class HoloDeleteCommand implements CommandHandler {
public List<String> suggest(CommandContext context) throws CommandExecutionException { public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds()); if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds());
if (context.argSize() == 2) return context.suggestStream(Stream.iterate(0, n -> n + 1) if (context.argSize() == 2) return context.suggestStream(Stream.iterate(0, n -> n + 1)
.limit(context.suggestionParse(1, NpcEntryImpl.class).getNpc().getHologram().getLines().size()) .limit(context.suggestionParse(0, NpcEntryImpl.class).getNpc().getHologram().getLines().size())
.map(String::valueOf)); .map(String::valueOf));
return Collections.emptyList(); return Collections.emptyList();
} }

@ -3,12 +3,12 @@ package lol.pyr.znpcsplus.commands.hologram;
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.ZNpcsPlus;
import lol.pyr.znpcsplus.hologram.HologramImpl; import lol.pyr.znpcsplus.hologram.HologramImpl;
import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcEntryImpl;
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;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -21,7 +21,7 @@ public class HoloInsertCommand implements CommandHandler {
HologramImpl hologram = context.parse(NpcEntryImpl.class).getNpc().getHologram(); HologramImpl hologram = context.parse(NpcEntryImpl.class).getNpc().getHologram();
int line = context.parse(Integer.class); int line = context.parse(Integer.class);
if (line < 0 || line >= hologram.getLines().size()) context.halt(Component.text("Invalid line number!", NamedTextColor.RED)); if (line < 0 || line >= hologram.getLines().size()) context.halt(Component.text("Invalid line number!", NamedTextColor.RED));
hologram.insertLine(line, LegacyComponentSerializer.legacyAmpersand().deserialize(context.dumpAllArgs())); hologram.insertLine(line, ZNpcsPlus.LEGACY_AMPERSAND_SERIALIZER.deserialize(context.dumpAllArgs()));
context.send(Component.text("NPC line inserted!", NamedTextColor.GREEN)); context.send(Component.text("NPC line inserted!", NamedTextColor.GREEN));
} }
@ -29,7 +29,7 @@ public class HoloInsertCommand implements CommandHandler {
public List<String> suggest(CommandContext context) throws CommandExecutionException { public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds()); if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds());
if (context.argSize() == 2) return context.suggestStream(Stream.iterate(0, n -> n + 1) if (context.argSize() == 2) return context.suggestStream(Stream.iterate(0, n -> n + 1)
.limit(context.suggestionParse(1, NpcEntryImpl.class).getNpc().getHologram().getLines().size()) .limit(context.suggestionParse(0, NpcEntryImpl.class).getNpc().getHologram().getLines().size())
.map(String::valueOf)); .map(String::valueOf));
return Collections.emptyList(); return Collections.emptyList();
} }

@ -3,12 +3,12 @@ package lol.pyr.znpcsplus.commands.hologram;
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.ZNpcsPlus;
import lol.pyr.znpcsplus.hologram.HologramImpl; import lol.pyr.znpcsplus.hologram.HologramImpl;
import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcEntryImpl;
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;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -22,16 +22,20 @@ public class HoloSetCommand implements CommandHandler {
int line = context.parse(Integer.class); int line = context.parse(Integer.class);
if (line < 0 || line >= hologram.getLines().size()) context.halt(Component.text("Invalid line number!", NamedTextColor.RED)); if (line < 0 || line >= hologram.getLines().size()) context.halt(Component.text("Invalid line number!", NamedTextColor.RED));
hologram.removeLine(line); hologram.removeLine(line);
hologram.insertLine(line, LegacyComponentSerializer.legacyAmpersand().deserialize(context.dumpAllArgs())); hologram.insertLine(line, ZNpcsPlus.LEGACY_AMPERSAND_SERIALIZER.deserialize(context.dumpAllArgs()));
context.send(Component.text("NPC line set!", NamedTextColor.GREEN)); context.send(Component.text("NPC line set!", NamedTextColor.GREEN));
} }
@Override @Override
public List<String> suggest(CommandContext context) throws CommandExecutionException { public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds()); if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds());
if (context.argSize() >= 2) {
HologramImpl hologram = context.suggestionParse(0, NpcEntryImpl.class).getNpc().getHologram();
if (context.argSize() == 2) return context.suggestStream(Stream.iterate(0, n -> n + 1) if (context.argSize() == 2) return context.suggestStream(Stream.iterate(0, n -> n + 1)
.limit(context.suggestionParse(1, NpcEntryImpl.class).getNpc().getHologram().getLines().size()) .limit(hologram.getLines().size()).map(String::valueOf));
.map(String::valueOf)); if (context.argSize() == 3) return context.suggestLiteral(ZNpcsPlus.LEGACY_AMPERSAND_SERIALIZER.serialize(
hologram.getLine(context.suggestionParse(1, Integer.class))));
}
return Collections.emptyList(); return Collections.emptyList();
} }
} }