diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 392ca64..ffa632f 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -204,12 +204,14 @@ public class ZNpcsPlus extends JavaPlugin { .addSubcommand("properties", new PropertiesCommand()) .addSubcommand("teleport", new TeleportCommand()) .addSubcommand("list", new ListCommand()) + .addSubcommand("near", new NearCommand()) .addSubcommand("holo", new MultiCommand() .addSubcommand("add", new HoloAddCommand()) .addSubcommand("delete", new HoloDeleteCommand()) .addSubcommand("info", new HoloInfoCommand()) .addSubcommand("insert", new HoloInsertCommand()) - .addSubcommand("set", new HoloSetCommand())) + .addSubcommand("set", new HoloSetCommand()) + ) ); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/ListCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ListCommand.java index 9bf0cd1..825c202 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/ListCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/ListCommand.java @@ -11,9 +11,6 @@ import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Location; -import java.util.Collections; -import java.util.List; - public class ListCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { @@ -36,9 +33,4 @@ public class ListCommand implements CommandHandler { } context.send(component.build()); } - - @Override - public List suggest(CommandContext context) throws CommandExecutionException { - return Collections.emptyList(); - } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/NearCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/NearCommand.java new file mode 100644 index 0000000..cb44ff7 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/NearCommand.java @@ -0,0 +1,30 @@ +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 org.bukkit.entity.Player; + +import java.util.stream.Collectors; + +public class NearCommand implements CommandHandler { + @Override + public void run(CommandContext context) throws CommandExecutionException { + Player player = context.ensureSenderIsPlayer(); + double radius = Math.pow(context.parse(Integer.class), 2); + + String npcs = NpcRegistryImpl.get().allModifiable().stream() + .filter(entry -> entry.getNpc().getLocation().toBukkitLocation(entry.getNpc().getWorld()).distanceSquared(player.getLocation()) < radius) + .map(NpcEntryImpl::getId) + .collect(Collectors.joining(", ")); + + if (npcs.length() == 0) context.halt(Component.text("There are no npcs within " + ((int) radius) + " blocks around you.", NamedTextColor.RED)); + context.send(Component.text("All NPCs that are within " + radius + " blocks from you:", NamedTextColor.GREEN).appendNewline() + .append(Component.text(npcs, NamedTextColor.GREEN))); + + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java index d7eeb32..a7e1cd6 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcRegistryImpl.java @@ -32,6 +32,12 @@ public class NpcRegistryImpl implements NpcRegistry { return Collections.unmodifiableCollection(npcMap.values()); } + public Collection allModifiable() { + return Collections.unmodifiableCollection(npcMap.values().stream() + .filter(NpcEntryImpl::isAllowCommandModification) + .collect(Collectors.toList())); + } + public NpcEntryImpl getByEntityId(int id) { return all().stream().filter(entry -> entry.getNpc().getEntity().getEntityId() == id).findFirst().orElse(null); }