Add a separation mechanic to /npc follow

This commit is contained in:
fullwall 2021-02-04 16:34:20 +08:00
parent 52a004fcaf
commit 65d02398fb

View File

@ -12,6 +12,9 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.ai.flocking.Flocker;
import net.citizensnpcs.api.ai.flocking.RadiusNPCFlock;
import net.citizensnpcs.api.ai.flocking.SeparationBehavior;
import net.citizensnpcs.api.persistence.Persist; import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName; import net.citizensnpcs.api.trait.TraitName;
@ -23,6 +26,7 @@ import net.citizensnpcs.api.trait.TraitName;
public class FollowTrait extends Trait { public class FollowTrait extends Trait {
@Persist("active") @Persist("active")
private boolean enabled = false; private boolean enabled = false;
private Flocker flock;
@Persist @Persist
private UUID followingUUID; private UUID followingUUID;
private Player player; private Player player;
@ -48,6 +52,11 @@ public class FollowTrait extends Trait {
return enabled; return enabled;
} }
@Override
public void onAttach() {
flock = new Flocker(npc, new RadiusNPCFlock(4, 20), new SeparationBehavior(1));
}
@EventHandler @EventHandler
private void onEntityDamage(EntityDamageByEntityEvent event) { private void onEntityDamage(EntityDamageByEntityEvent event) {
if (isActive() && protect && event.getEntity().equals(player)) { if (isActive() && protect && event.getEntity().equals(player)) {
@ -83,6 +92,8 @@ public class FollowTrait extends Trait {
} }
if (!npc.getNavigator().isNavigating()) { if (!npc.getNavigator().isNavigating()) {
npc.getNavigator().setTarget(player, false); npc.getNavigator().setTarget(player, false);
} else {
flock.run();
} }
} }