ZNPCsPlus/plugin/src/main/java/lol/pyr/znpcsplus/commands/NearCommand.java

37 lines
1.5 KiB
Java
Raw Normal View History

2023-05-10 15:55:37 +00:00
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 {
private final NpcRegistryImpl npcRegistry;
public NearCommand(NpcRegistryImpl npcRegistry) {
this.npcRegistry = npcRegistry;
}
2023-05-10 15:55:37 +00:00
@Override
public void run(CommandContext context) throws CommandExecutionException {
Player player = context.ensureSenderIsPlayer();
2023-05-11 04:40:33 +00:00
int raw = context.parse(Integer.class);
double radius = Math.pow(raw, 2);
2023-05-10 15:55:37 +00:00
String npcs = npcRegistry.allModifiable().stream()
2023-05-11 04:40:33 +00:00
.filter(entry -> entry.getNpc().getBukkitLocation().distanceSquared(player.getLocation()) < radius)
2023-05-10 15:55:37 +00:00
.map(NpcEntryImpl::getId)
.collect(Collectors.joining(", "));
2023-05-11 04:40:33 +00:00
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()
2023-05-10 15:55:37 +00:00
.append(Component.text(npcs, NamedTextColor.GREEN)));
}
}