Support FollowTrait on 1.8.8

This commit is contained in:
fullwall 2023-12-02 14:55:37 +08:00
parent a1e4255771
commit 49036e1967
2 changed files with 21 additions and 1 deletions

View File

@ -17,6 +17,7 @@ import net.citizensnpcs.api.ai.flocking.SeparationBehavior;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.util.Util;
/**
* Persists a {@link Player} to follow while spawned. Optionally allows protecting of the player as well.
@ -98,7 +99,7 @@ public class FollowTrait extends Trait {
return;
entity = Bukkit.getPlayer(followingUUID);
if (entity == null) {
entity = Bukkit.getEntity(followingUUID);
entity = Util.getEntity(followingUUID);
}
if (entity == null)
return;

View File

@ -17,6 +17,7 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
@ -218,6 +219,23 @@ public class Util {
return DUMMY_SCOREBOARD;
}
public static Entity getEntity(UUID uuid) {
if (SUPPORTS_BUKKIT_GETENTITY) {
try {
return Bukkit.getEntity(uuid);
} catch (Throwable t) {
SUPPORTS_BUKKIT_GETENTITY = false;
}
}
for (World world : Bukkit.getWorlds()) {
for (Entity entity : world.getEntities()) {
if (entity.getUniqueId().equals(uuid))
return entity;
}
}
return null;
}
public static Location getEyeLocation(Entity entity) {
return entity instanceof LivingEntity ? ((LivingEntity) entity).getEyeLocation() : entity.getLocation();
}
@ -579,6 +597,7 @@ public class Util {
}
private static final Scoreboard DUMMY_SCOREBOARD = Bukkit.getScoreboardManager().getNewScoreboard();
private static boolean SUPPORTS_BUKKIT_GETENTITY = true;
private static final DecimalFormat TWO_DIGIT_DECIMAL = new DecimalFormat();
static {