Simplify mount trait

This commit is contained in:
fullwall 2023-06-11 16:00:33 +08:00
parent 9facad1b2a
commit ddc938a38c
1 changed files with 19 additions and 24 deletions

View File

@ -17,27 +17,26 @@ import net.citizensnpcs.util.NMS;
*/ */
@TraitName("mounttrait") @TraitName("mounttrait")
public class MountTrait extends Trait { public class MountTrait extends Trait {
private UUID mountedOn; private UUID currentMount;
private boolean triggered = false;
@Persist("mountedon") @Persist("mountedon")
private String uuid; private UUID uuid;
public MountTrait() { public MountTrait() {
super("mounttrait"); super("mounttrait");
} }
public void checkMounted(Entity mounted) { public void checkMounted() {
if (mountedOn == null || (mounted != null && mounted.getUniqueId().equals(mountedOn))) if (uuid == null || uuid.equals(currentMount))
return; return;
NPC other = CitizensAPI.getNPCRegistry().getByUniqueId(mountedOn); NPC other = CitizensAPI.getNPCRegistry().getByUniqueId(uuid);
if (other != null && other.isSpawned()) { if (other != null && other.isSpawned()) {
NMS.mount(other.getEntity(), npc.getEntity()); NMS.mount(other.getEntity(), npc.getEntity());
triggered = true; currentMount = uuid;
} }
} }
public UUID getMountedOn() { public UUID getMountedOn() {
return mountedOn; return currentMount;
} }
@Override @Override
@ -47,44 +46,40 @@ public class MountTrait extends Trait {
} }
} }
@Override
public void onRemove() {
onDespawn();
}
@Override @Override
public void onSpawn() { public void onSpawn() {
checkMounted(null); checkMounted();
} }
@Override @Override
public void run() { public void run() {
if (!npc.isSpawned()) if (!npc.isSpawned())
return; return;
if (!triggered && uuid != null) {
try {
mountedOn = UUID.fromString(uuid);
} catch (IllegalArgumentException e) {
mountedOn = null;
}
checkMounted(null);
}
Entity vehicle = NMS.getVehicle(npc.getEntity()); Entity vehicle = NMS.getVehicle(npc.getEntity());
if (vehicle == null && !triggered) { if (vehicle == null && currentMount != null) {
mountedOn = null; currentMount = null;
} else if (vehicle instanceof NPCHolder) { } else if (vehicle instanceof NPCHolder) {
setMountedOn(((NPCHolder) vehicle).getNPC().getUniqueId()); setMountedOn(((NPCHolder) vehicle).getNPC().getUniqueId());
} }
checkMounted(vehicle); checkMounted();
} }
public void setMountedOn(UUID uuid) { public void setMountedOn(UUID uuid) {
this.mountedOn = uuid; this.uuid = uuid;
this.uuid = uuid.toString();
} }
public void unmount() { public void unmount() {
if (mountedOn == null) if (currentMount == null)
return; return;
if (NMS.getVehicle(npc.getEntity()) != null) { if (NMS.getVehicle(npc.getEntity()) != null) {
npc.getEntity().leaveVehicle(); npc.getEntity().leaveVehicle();
} }
uuid = null; uuid = null;
mountedOn = null; currentMount = null;
} }
} }