2022-06-14 15:38:15 +02:00
|
|
|
package net.citizensnpcs.trait;
|
|
|
|
|
2022-10-04 17:42:23 +02:00
|
|
|
import org.bukkit.Location;
|
2022-06-14 15:38:15 +02:00
|
|
|
import org.bukkit.entity.EntityType;
|
2023-06-11 10:51:28 +02:00
|
|
|
import org.bukkit.entity.Sittable;
|
2022-11-15 14:30:23 +01:00
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
2022-06-14 15:38:15 +02:00
|
|
|
|
|
|
|
import net.citizensnpcs.api.CitizensAPI;
|
|
|
|
import net.citizensnpcs.api.npc.MemoryNPCDataStore;
|
|
|
|
import net.citizensnpcs.api.npc.NPC;
|
|
|
|
import net.citizensnpcs.api.npc.NPCRegistry;
|
|
|
|
import net.citizensnpcs.api.persistence.Persist;
|
|
|
|
import net.citizensnpcs.api.trait.Trait;
|
|
|
|
import net.citizensnpcs.api.trait.TraitName;
|
|
|
|
import net.citizensnpcs.util.NMS;
|
|
|
|
|
|
|
|
@TraitName("sittrait")
|
|
|
|
public class SitTrait extends Trait {
|
2023-01-01 13:44:17 +01:00
|
|
|
private NPC chair;
|
2022-06-14 15:38:15 +02:00
|
|
|
@Persist
|
2022-10-04 17:42:23 +02:00
|
|
|
private Location sittingAt;
|
2022-06-14 15:38:15 +02:00
|
|
|
|
|
|
|
public SitTrait() {
|
|
|
|
super("sittrait");
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSitting() {
|
2022-10-04 17:42:23 +02:00
|
|
|
return sittingAt != null;
|
2022-06-14 15:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDespawn() {
|
2023-06-11 10:51:28 +02:00
|
|
|
if (SUPPORT_SITTABLE && npc.getEntity() instanceof Sittable) {
|
|
|
|
((Sittable) npc.getEntity()).setSitting(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-01 13:44:17 +01:00
|
|
|
if (chair != null) {
|
|
|
|
if (chair.getEntity() != null) {
|
|
|
|
chair.getEntity().eject();
|
|
|
|
}
|
|
|
|
chair.destroy();
|
|
|
|
chair = null;
|
2022-06-14 15:38:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 12:13:17 +01:00
|
|
|
@Override
|
|
|
|
public void onRemove() {
|
|
|
|
onDespawn();
|
|
|
|
}
|
|
|
|
|
2022-06-14 15:38:15 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2023-01-01 13:44:17 +01:00
|
|
|
if (!npc.isSpawned() || !isSitting())
|
2022-06-14 15:38:15 +02:00
|
|
|
return;
|
2022-10-04 16:18:05 +02:00
|
|
|
|
2023-06-11 10:51:28 +02:00
|
|
|
if (SUPPORT_SITTABLE && npc.getEntity() instanceof Sittable) {
|
|
|
|
((Sittable) npc.getEntity()).setSitting(true);
|
|
|
|
if (npc.getEntity().getLocation().distance(sittingAt) > 0.05) {
|
|
|
|
npc.teleport(sittingAt, TeleportCause.PLUGIN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-01 13:44:17 +01:00
|
|
|
if (chair == null) {
|
|
|
|
NPCRegistry registry = CitizensAPI.getNamedNPCRegistry("SitRegistry");
|
2022-06-14 15:38:15 +02:00
|
|
|
if (registry == null) {
|
2023-01-01 13:44:17 +01:00
|
|
|
registry = CitizensAPI.createNamedNPCRegistry("SitRegistry", new MemoryNPCDataStore());
|
|
|
|
}
|
|
|
|
chair = registry.createNPC(EntityType.ARMOR_STAND, "");
|
|
|
|
chair.getOrAddTrait(ArmorStandTrait.class).setAsHelperEntity(npc);
|
2023-02-23 16:52:27 +01:00
|
|
|
if (!chair.spawn(sittingAt.clone())) {
|
2023-01-01 13:44:17 +01:00
|
|
|
chair = null;
|
|
|
|
return;
|
2022-06-14 15:38:15 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-04 16:18:05 +02:00
|
|
|
|
2023-01-01 13:44:17 +01:00
|
|
|
if (chair.isSpawned() && !NMS.getPassengers(chair.getEntity()).contains(npc.getEntity())) {
|
|
|
|
NMS.mount(chair.getEntity(), npc.getEntity());
|
2022-06-14 15:38:15 +02:00
|
|
|
}
|
2022-11-15 14:30:23 +01:00
|
|
|
|
2023-01-01 13:44:17 +01:00
|
|
|
if (chair.getStoredLocation() != null && chair.getStoredLocation().distance(sittingAt) > 0.05) {
|
2023-02-23 16:52:27 +01:00
|
|
|
chair.teleport(sittingAt.clone(), TeleportCause.PLUGIN);
|
2022-11-23 12:03:06 +01:00
|
|
|
}
|
2022-06-14 15:38:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 17:42:23 +02:00
|
|
|
public void setSitting(Location at) {
|
|
|
|
this.sittingAt = at != null ? at.clone() : null;
|
|
|
|
if (!isSitting()) {
|
2022-06-14 15:39:20 +02:00
|
|
|
onDespawn();
|
|
|
|
}
|
2022-06-14 15:38:15 +02:00
|
|
|
}
|
2023-06-11 10:51:28 +02:00
|
|
|
|
|
|
|
private static boolean SUPPORT_SITTABLE = true;
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
Class.forName("org.bukkit.entity.Sittable");
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
SUPPORT_SITTABLE = false;
|
|
|
|
}
|
|
|
|
}
|
2022-06-14 15:38:15 +02:00
|
|
|
}
|