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