Citizens2/main/src/main/java/net/citizensnpcs/trait/CurrentLocation.java

61 lines
1.5 KiB
Java
Raw Normal View History

package net.citizensnpcs.trait;
import org.bukkit.Location;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
2020-04-30 13:11:56 +02:00
import net.citizensnpcs.api.util.DataKey;
2020-03-21 11:30:52 +01:00
import net.citizensnpcs.util.NMS;
2019-05-16 13:58:29 +02:00
/**
* Persists the current {@link Location} of the {@link NPC}. Will cache last known location if despawned.
*/
@TraitName("location")
public class CurrentLocation extends Trait {
2020-03-21 11:30:52 +01:00
@Persist
2020-04-30 17:11:45 +02:00
private float bodyYaw = Float.NaN;
@Persist(value = "", required = true)
private Location location = new Location(null, 0, 0, 0);
public CurrentLocation() {
super("location");
}
2020-04-30 13:11:56 +02:00
public float getBodyYaw() {
return bodyYaw;
2020-03-21 11:30:52 +01:00
}
public Location getLocation() {
2020-06-28 12:05:04 +02:00
return location.getWorld() == null ? null : location.clone();
}
2020-04-30 13:11:56 +02:00
@Override
public void load(DataKey key) {
key.removeKey("headYaw");
}
2020-04-30 17:11:45 +02:00
@Override
public void onSpawn() {
if (!Float.isNaN(bodyYaw)) {
NMS.setBodyYaw(npc.getEntity(), bodyYaw);
}
}
@Override
public void run() {
if (!npc.isSpawned())
return;
location = npc.getEntity().getLocation(location);
2020-04-30 13:11:56 +02:00
bodyYaw = NMS.getYaw(npc.getEntity());
}
public void setLocation(Location loc) {
location = loc.clone();
}
@Override
public String toString() {
return "CurrentLocation{" + location + "}";
}
}