add near command and some refactoring

This commit is contained in:
Pyrbu 2023-05-10 16:55:37 +01:00
parent 37b5c190c9
commit 4197c58008
4 changed files with 39 additions and 9 deletions

@ -204,12 +204,14 @@ public class ZNpcsPlus extends JavaPlugin {
.addSubcommand("properties", new PropertiesCommand()) .addSubcommand("properties", new PropertiesCommand())
.addSubcommand("teleport", new TeleportCommand()) .addSubcommand("teleport", new TeleportCommand())
.addSubcommand("list", new ListCommand()) .addSubcommand("list", new ListCommand())
.addSubcommand("near", new NearCommand())
.addSubcommand("holo", new MultiCommand() .addSubcommand("holo", new MultiCommand()
.addSubcommand("add", new HoloAddCommand()) .addSubcommand("add", new HoloAddCommand())
.addSubcommand("delete", new HoloDeleteCommand()) .addSubcommand("delete", new HoloDeleteCommand())
.addSubcommand("info", new HoloInfoCommand()) .addSubcommand("info", new HoloInfoCommand())
.addSubcommand("insert", new HoloInsertCommand()) .addSubcommand("insert", new HoloInsertCommand())
.addSubcommand("set", new HoloSetCommand())) .addSubcommand("set", new HoloSetCommand())
)
); );
} }
} }

@ -11,9 +11,6 @@ import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Location; import org.bukkit.Location;
import java.util.Collections;
import java.util.List;
public class ListCommand implements CommandHandler { public class ListCommand implements CommandHandler {
@Override @Override
public void run(CommandContext context) throws CommandExecutionException { public void run(CommandContext context) throws CommandExecutionException {
@ -36,9 +33,4 @@ public class ListCommand implements CommandHandler {
} }
context.send(component.build()); context.send(component.build());
} }
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
return Collections.emptyList();
}
} }

@ -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)));
}
}

@ -32,6 +32,12 @@ public class NpcRegistryImpl implements NpcRegistry {
return Collections.unmodifiableCollection(npcMap.values()); return Collections.unmodifiableCollection(npcMap.values());
} }
public Collection<NpcEntryImpl> allModifiable() {
return Collections.unmodifiableCollection(npcMap.values().stream()
.filter(NpcEntryImpl::isAllowCommandModification)
.collect(Collectors.toList()));
}
public NpcEntryImpl getByEntityId(int id) { public NpcEntryImpl getByEntityId(int id) {
return all().stream().filter(entry -> entry.getNpc().getEntity().getEntityId() == id).findFirst().orElse(null); return all().stream().filter(entry -> entry.getNpc().getEntity().getEntityId() == id).findFirst().orElse(null);
} }