Add PAPI nearest npc id placeholder

This commit is contained in:
fullwall 2023-03-14 20:40:02 +08:00
parent 2d8ea825a2
commit 6d03d3ce62

View File

@ -1,9 +1,14 @@
package net.citizensnpcs;
import java.util.Optional;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.NPCSelector;
public class CitizensPlaceholders extends PlaceholderExpansion {
@ -33,12 +38,22 @@ public class CitizensPlaceholders extends PlaceholderExpansion {
if (player == null || !player.isOnline())
return null;
if (params.equalsIgnoreCase("citizens_selected_npc_name")) {
return selector.getSelected((CommandSender) player).getFullName();
}
NPC selected = selector.getSelected((CommandSender) player);
switch (params) {
case "citizens_selected_npc_name":
return selected == null ? "" : selected.getFullName();
case "citizens_selected_npc_id":
return selected == null ? "" : Integer.toString(selected.getId());
case "citizens_selected_npc_uuid":
return selected == null ? "" : selected.getUniqueId().toString();
case "citizens_nearest_npc_id":
Location location = player.getPlayer().getLocation();
if (params.equalsIgnoreCase("citizens_selected_npc_id")) {
return Integer.toString(selector.getSelected((CommandSender) player).getId());
Optional<NPC> closestNPC = player.getPlayer().getNearbyEntities(12.5, 25, 12.5).stream()
.map(CitizensAPI.getNPCRegistry()::getNPC).filter(e -> e != null && e.getEntity() != player)
.min((a, b) -> Double.compare(a.getEntity().getLocation().distanceSquared(location),
b.getEntity().getLocation().distanceSquared(location)));
return closestNPC.isPresent() ? Integer.toString(closestNPC.get().getId()) : "";
}
return null;