make npc near use the same format as npc list (resolves #114)

This commit is contained in:
Pyrbu 2023-12-18 17:11:47 +01:00
parent e078c3cb5e
commit f9c5bd564d
2 changed files with 28 additions and 12 deletions

@ -7,7 +7,6 @@ import lol.pyr.znpcsplus.npc.NpcImpl;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
import lol.pyr.znpcsplus.util.NpcLocation;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
@ -20,20 +19,20 @@ public class ListCommand implements CommandHandler {
@Override
public void run(CommandContext context) throws CommandExecutionException {
TextComponent.Builder component = Component.text("Npc List:\n").color(NamedTextColor.GOLD).toBuilder();
Component component = Component.text("Npc List:", NamedTextColor.GOLD).appendNewline();
for (String id : npcRegistry.getModifiableIds()) {
NpcImpl npc = npcRegistry.getById(id).getNpc();
NpcLocation location = npc.getLocation();
component.append(Component.text("ID: " + id, npc.isEnabled() ? NamedTextColor.GREEN : NamedTextColor.RED))
component = component.append(Component.text("ID: " + id, npc.isEnabled() ? NamedTextColor.GREEN : NamedTextColor.RED))
.append(Component.text(" | ", NamedTextColor.GRAY))
.append(Component.text("Type: ", NamedTextColor.GREEN))
.append(Component.text(npc.getType().getName(), NamedTextColor.GREEN))
.append(Component.text(" | ", NamedTextColor.GRAY))
.append(Component.text("Location: " + npc.getWorldName() + " X:" + location.getBlockX() + " Y:" + location.getBlockY() + " Z:" + location.getBlockZ(), NamedTextColor.GREEN))
.append(Component.text(" | ", NamedTextColor.GRAY))
.append(Component.text("[TELEPORT]", NamedTextColor.DARK_GREEN).clickEvent(ClickEvent.runCommand("/znpcsplus:npc teleport " + id)))
.append(Component.text("\n", NamedTextColor.GRAY));
.append(Component.text("[TELEPORT]", NamedTextColor.DARK_GREEN).clickEvent(ClickEvent.runCommand("/znpcs teleport " + id)))
.appendNewline();
}
context.send(component.build());
context.send(component);
}
}

@ -4,11 +4,15 @@ 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 lol.pyr.znpcsplus.util.NpcLocation;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.stream.Collectors;
public class NearCommand implements CommandHandler {
@ -25,14 +29,27 @@ public class NearCommand implements CommandHandler {
int raw = context.parse(Integer.class);
double radius = Math.pow(raw, 2);
String npcs = npcRegistry.getAllModifiable().stream()
List<NpcEntryImpl> entries = npcRegistry.getAllModifiable().stream()
.filter(entry -> entry.getNpc().getWorld().equals(player.getWorld()))
.filter(entry -> entry.getNpc().getBukkitLocation().distanceSquared(player.getLocation()) < radius)
.map(NpcEntryImpl::getId)
.collect(Collectors.joining(", "));
.collect(Collectors.toList());
if (npcs.length() == 0) context.halt(Component.text("There are no npcs within " + raw + " blocks around you.", NamedTextColor.RED));
context.send(Component.text("All NPCs that are within " + raw + " blocks from you:", NamedTextColor.GREEN).appendNewline()
.append(Component.text(npcs, NamedTextColor.GREEN)));
if (entries.isEmpty()) context.halt(Component.text("There are no npcs within " + raw + " blocks around you.", NamedTextColor.RED));
Component component = Component.text("All NPCs that are within " + raw + " blocks from you:", NamedTextColor.GOLD).appendNewline();
for (NpcEntryImpl entry : entries) {
NpcImpl npc = entry.getNpc();
NpcLocation location = npc.getLocation();
component = component.append(Component.text("ID: " + entry.getId(), npc.isEnabled() ? NamedTextColor.GREEN : NamedTextColor.RED))
.append(Component.text(" | ", NamedTextColor.GRAY))
.append(Component.text("Type: ", NamedTextColor.GREEN))
.append(Component.text(npc.getType().getName(), NamedTextColor.GREEN))
.append(Component.text(" | ", NamedTextColor.GRAY))
.append(Component.text("Location: " + npc.getWorldName() + " X:" + location.getBlockX() + " Y:" + location.getBlockY() + " Z:" + location.getBlockZ(), NamedTextColor.GREEN))
.append(Component.text(" | ", NamedTextColor.GRAY))
.append(Component.text("[TELEPORT]", NamedTextColor.DARK_GREEN).clickEvent(ClickEvent.runCommand("/znpcs teleport " + entry.getId())))
.appendNewline();
}
context.send(component);
}
}