Skip to content

Commit

Permalink
Make lookAtFunction use packets for yaw, tweak API further
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Dec 6, 2022
1 parent 6533cdc commit d34fd48
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 123 deletions.
4 changes: 2 additions & 2 deletions main/src/main/java/net/citizensnpcs/ProtocolLibListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.RotationTrait.LocalRotationSession;
import net.citizensnpcs.trait.RotationTrait.PacketRotationSession;

public class ProtocolLibListener {
private final Class<?> flagsClass;
Expand Down Expand Up @@ -46,7 +46,7 @@ public void onPacketSending(PacketEvent event) {
if (trait == null)
return;

LocalRotationSession session = trait.getLocalSession(event.getPlayer());
PacketRotationSession session = trait.getPacketSession(event.getPlayer());
if (session == null || !session.isActive())
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,7 @@ public void rotate(CommandContext args, CommandSender sender, NPC npc, @Flag("bo
yaw = NMS.getHeadYaw(npc.getEntity());
}
}
npc.getOrAddTrait(RotationTrait.class).rotateToHave(yaw, pitch);
npc.getOrAddTrait(RotationTrait.class).getPhysicalSession().rotateToHave(yaw, pitch);
return;
}
if (yaw != null) {
Expand Down
21 changes: 12 additions & 9 deletions main/src/main/java/net/citizensnpcs/npc/ai/CitizensNavigator.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import net.citizensnpcs.api.astar.pathfinder.SwimmingExaminer;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.RotationTrait.PacketRotationSession;
import net.citizensnpcs.trait.RotationTrait.RotationParams;
import net.citizensnpcs.util.ChunkCoord;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;

public class CitizensNavigator implements Navigator, Runnable {
private Location activeTicket;
Expand All @@ -58,6 +60,7 @@ public class CitizensNavigator implements Navigator, Runnable {
private NavigatorParameters localParams = defaultParams;
private final NPC npc;
private boolean paused;
private PacketRotationSession session;
private int stationaryTicks;

public CitizensNavigator(NPC npc) {
Expand Down Expand Up @@ -174,15 +177,11 @@ public void run() {
localParams.run();
}
if (localParams.lookAtFunction() != null) {
Util.faceLocation(npc.getEntity(), localParams.lookAtFunction().apply(this), true, true);
Entity entity = npc.getEntity().getPassenger();
npcLoc = npc.getEntity().getLocation();
while (entity != null) {
Location loc = entity.getLocation(STATIONARY_LOCATION);
loc.setYaw(npcLoc.getYaw());
entity.teleport(loc);
entity = entity.getPassenger();
if (session == null) {
RotationTrait trait = npc.getOrAddTrait(RotationTrait.class);
session = trait.createPacketSession(new RotationParams().filter((p) -> true).persist(true));
}
session.getSession().rotateToFace(localParams.lookAtFunction().apply(this));
}
if (localParams.destinationTeleportMargin() > 0
&& npcLoc.distance(targetLoc) <= localParams.destinationTeleportMargin()) {
Expand Down Expand Up @@ -374,6 +373,10 @@ public void run() {
private void stopNavigating(CancelReason reason) {
if (!isNavigating())
return;
if (session != null) {
session.end();
session = null;
}
Iterator<NavigatorCallback> itr = localParams.callbacks().iterator();
List<NavigatorCallback> callbacks = new ArrayList<NavigatorCallback>();
while (itr.hasNext()) {
Expand Down
106 changes: 61 additions & 45 deletions main/src/main/java/net/citizensnpcs/trait/RotationTrait.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,32 @@ public class RotationTrait extends Trait {
@Persist(reify = true)
private final RotationParams globalParameters = new RotationParams();
private final RotationSession globalSession = new RotationSession(globalParameters);
private final List<LocalRotationSession> localSessions = Lists.newArrayList();
private final Map<UUID, LocalRotationSession> localSessionsByUUID = Maps.newHashMap();
private final List<PacketRotationSession> packetSessions = Lists.newArrayList();
private final Map<UUID, PacketRotationSession> packetSessionsByUUID = Maps.newHashMap();

public RotationTrait() {
super("rotationtrait");
}

public void clearLocalSessions() {
localSessions.clear();
public void clearPacketSessions() {
packetSessions.clear();
packetSessionsByUUID.clear();
}

/**
* @return The created session
*/
public LocalRotationSession createLocalSession(RotationParams params) {
public PacketRotationSession createPacketSession(RotationParams params) {
if (params.filter == null && params.uuidFilter == null)
throw new IllegalStateException();
RotationSession session = new RotationSession(params);
LocalRotationSession lrs = new LocalRotationSession(session);
PacketRotationSession lrs = new PacketRotationSession(session);
if (params.uuidFilter != null) {
for (UUID uuid : params.uuidFilter) {
localSessionsByUUID.put(uuid, lrs);
packetSessionsByUUID.put(uuid, lrs);
}
} else {
localSessions.add(lrs);
packetSessions.add(lrs);
}
return lrs;
}
Expand All @@ -71,18 +72,22 @@ public RotationParams getGlobalParameters() {
return globalParameters;
}

public LocalRotationSession getLocalSession(Player player) {
LocalRotationSession lrs = localSessionsByUUID.get(player.getUniqueId());
public PacketRotationSession getPacketSession(Player player) {
PacketRotationSession lrs = packetSessionsByUUID.get(player.getUniqueId());
if (lrs != null)
return lrs;
for (LocalRotationSession session : localSessions) {
for (PacketRotationSession session : packetSessions) {
if (session.accepts(player)) {
return session;
}
}
return null;
}

public RotationSession getPhysicalSession() {
return globalSession;
}

private double getX() {
return npc.getStoredLocation().getX();
}
Expand All @@ -95,44 +100,15 @@ private double getZ() {
return npc.getStoredLocation().getZ();
}

/**
* Rotates to face target entity
*
* @param target
* The target entity to face
*/
public void rotateToFace(Entity target) {
Location loc = target.getLocation();
loc.setY(loc.getY() + NMS.getHeight(target));
rotateToFace(loc);
}

/**
* Rotates to face target location
*
* @param target
* The target location to face
*/
public void rotateToFace(Location target) {
globalSession.setTarget(target);
}

public void rotateToHave(float yaw, float pitch) {
double pitchCos = Math.cos(Math.toRadians(pitch));
Vector vector = new Vector(Math.sin(Math.toRadians(yaw)) * -pitchCos, -Math.sin(Math.toRadians(pitch)),
Math.cos(Math.toRadians(yaw)) * pitchCos).normalize();
rotateToFace(npc.getStoredLocation().clone().add(vector));
}

@Override
public void run() {
if (!npc.isSpawned())
return;

Set<LocalRotationSession> run = Sets.newHashSet();
for (Iterator<LocalRotationSession> itr = Iterables.concat(localSessions, localSessionsByUUID.values())
Set<PacketRotationSession> run = Sets.newHashSet();
for (Iterator<PacketRotationSession> itr = Iterables.concat(packetSessions, packetSessionsByUUID.values())
.iterator(); itr.hasNext();) {
LocalRotationSession session = itr.next();
PacketRotationSession session = itr.next();
if (run.contains(session))
continue;
run.add(session);
Expand Down Expand Up @@ -166,12 +142,12 @@ public void apply() {
}
}

public static class LocalRotationSession {
public static class PacketRotationSession {
private boolean ended;
private final RotationSession session;
private RotationTriple triple;

public LocalRotationSession(RotationSession session) {
public PacketRotationSession(RotationSession session) {
this.session = session;
}

Expand All @@ -195,6 +171,11 @@ public float getPitch() {
return triple.pitch;
}

// TODO: inheritance rather than composition?
public RotationSession getSession() {
return session;
}

public boolean isActive() {
return !ended && session.isActive();
}
Expand Down Expand Up @@ -416,6 +397,41 @@ public boolean isActive() {
return params.persist || t >= 0;
}

/**
* Rotates to face target entity
*
* @param target
* The target entity to face
*/
public void rotateToFace(Entity target) {
Location loc = target.getLocation();
loc.setY(loc.getY() + NMS.getHeight(target));
rotateToFace(loc);
}

/**
* Rotates to face target location
*
* @param target
* The target location to face
*/
public void rotateToFace(Location target) {
setTarget(target);
}

/**
* Rotate to have the given yaw and pitch
*
* @param yaw
* @param pitch
*/
public void rotateToHave(float yaw, float pitch) {
double pitchCos = Math.cos(Math.toRadians(pitch));
Vector vector = new Vector(Math.sin(Math.toRadians(yaw)) * -pitchCos, -Math.sin(Math.toRadians(pitch)),
Math.cos(Math.toRadians(yaw)) * pitchCos).normalize();
rotateToFace(npc.getStoredLocation().clone().add(vector));
}

private void run(RotationTriple rot) {
if (!isActive())
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ public void look(org.bukkit.entity.Entity entity, Location to, boolean headOnly,
((EntityInsentient) handle).aQ += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).getPhysicalSession().rotateToFace(to);
}
}

Expand All @@ -810,7 +810,7 @@ public void look(org.bukkit.entity.Entity from, org.bukkit.entity.Entity to) {
((EntityLiving) handle).aQ += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).getPhysicalSession().rotateToFace(to);
}
}

Expand Down Expand Up @@ -1744,15 +1744,15 @@ public static void sendPacketNearby(Player from, Location location, Packet<?> pa
public static void sendPacketsNearby(Player from, Location location, Collection<Packet<?>> packets, double radius) {
radius *= radius;
final org.bukkit.World world = location.getWorld();
for (Player ply : Bukkit.getServer().getOnlinePlayers()) {
if (ply == null || world != ply.getWorld() || (from != null && !ply.canSee(from))) {
for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(location, radius)) {
if (world != player.getWorld() || (from != null && !player.canSee(from))) {
continue;
}
if (location.distanceSquared(ply.getLocation(PACKET_CACHE_LOCATION)) > radius) {
if (location.distanceSquared(player.getLocation(PACKET_CACHE_LOCATION)) > radius) {
continue;
}
for (Packet<?> packet : packets) {
NMSImpl.sendPacket(ply, packet);
NMSImpl.sendPacket(player, packet);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ public void look(org.bukkit.entity.Entity entity, Location to, boolean headOnly,
((EntityLiving) handle).aP += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).getPhysicalSession().rotateToFace(to);
}
}

Expand All @@ -866,7 +866,7 @@ public void look(org.bukkit.entity.Entity from, org.bukkit.entity.Entity to) {
((EntityLiving) handle).aP += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).getPhysicalSession().rotateToFace(to);
}
}

Expand Down Expand Up @@ -1808,15 +1808,15 @@ public static void sendPacketNearby(Player from, Location location, Packet<?> pa
public static void sendPacketsNearby(Player from, Location location, Collection<Packet<?>> packets, double radius) {
radius *= radius;
final org.bukkit.World world = location.getWorld();
for (Player ply : Bukkit.getServer().getOnlinePlayers()) {
if (ply == null || world != ply.getWorld() || (from != null && !ply.canSee(from))) {
for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(location, radius)) {
if (world != player.getWorld() || (from != null && !player.canSee(from))) {
continue;
}
if (location.distanceSquared(ply.getLocation(PACKET_CACHE_LOCATION)) > radius) {
if (location.distanceSquared(player.getLocation(PACKET_CACHE_LOCATION)) > radius) {
continue;
}
for (Packet<?> packet : packets) {
NMSImpl.sendPacket(ply, packet);
NMSImpl.sendPacket(player, packet);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ public void look(org.bukkit.entity.Entity entity, Location to, boolean headOnly,
((EntityLiving) handle).aP += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).getPhysicalSession().rotateToFace(to);
}
}

Expand All @@ -874,7 +874,7 @@ public void look(org.bukkit.entity.Entity from, org.bukkit.entity.Entity to) {
((EntityLiving) handle).aP += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).getPhysicalSession().rotateToFace(to);
}
}

Expand Down Expand Up @@ -1818,15 +1818,15 @@ public static void sendPacketNearby(Player from, Location location, Packet<?> pa
public static void sendPacketsNearby(Player from, Location location, Collection<Packet<?>> packets, double radius) {
radius *= radius;
final org.bukkit.World world = location.getWorld();
for (Player ply : Bukkit.getServer().getOnlinePlayers()) {
if (ply == null || world != ply.getWorld() || (from != null && !ply.canSee(from))) {
for (Player player : CitizensAPI.getLocationLookup().getNearbyPlayers(location, radius)) {
if (world != player.getWorld() || (from != null && !player.canSee(from))) {
continue;
}
if (location.distanceSquared(ply.getLocation(PACKET_CACHE_LOCATION)) > radius) {
if (location.distanceSquared(player.getLocation(PACKET_CACHE_LOCATION)) > radius) {
continue;
}
for (Packet<?> packet : packets) {
NMSImpl.sendPacket(ply, packet);
NMSImpl.sendPacket(player, packet);
}
}
}
Expand Down
Loading

0 comments on commit d34fd48

Please sign in to comment.