Skip to content

Commit

Permalink
Fix mob NPC not using pitch
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Nov 22, 2016
1 parent 2f7e4ea commit b695afd
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.common.collect.Maps;

import gnu.trove.map.hash.TIntObjectHashMap;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCCreateEvent;
Expand All @@ -25,6 +26,7 @@
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.util.NMS;

public class CitizensNPCRegistry implements NPCRegistry {
Expand Down Expand Up @@ -53,6 +55,9 @@ public NPC createNPC(EntityType type, UUID uuid, int id, String name) {
if (type == EntityType.ARMOR_STAND && !npc.hasTrait(ArmorStandTrait.class)) {
npc.addTrait(ArmorStandTrait.class);
}
if (Setting.DEFAULT_LOOK_CLOSE.asBoolean()) {
npc.addTrait(LookClose.class);
}
return npc;
}

Expand Down
6 changes: 5 additions & 1 deletion main/src/main/java/net/citizensnpcs/util/NMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,14 @@ public static void loadPlugins() {
BRIDGE.loadPlugins();
}

public static void look(org.bukkit.entity.Entity entity, float yaw, float pitch) {
public static void look(Entity entity, float yaw, float pitch) {
BRIDGE.look(entity, yaw, pitch);
}

public static void look(org.bukkit.entity.Entity entity, Location to, boolean headOnly) {
BRIDGE.look(entity, to, headOnly);
}

public static void look(org.bukkit.entity.Entity bhandle, org.bukkit.entity.Entity btarget) {
BRIDGE.look(bhandle, btarget);
}
Expand Down
2 changes: 2 additions & 0 deletions main/src/main/java/net/citizensnpcs/util/NMSBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public interface NMSBridge {

public void look(Entity entity, float yaw, float pitch);

public void look(Entity entity, Location to, boolean headOnly);

public void mount(Entity entity, Entity passenger);

public void openHorseScreen(Horse horse, Player equipper);
Expand Down
21 changes: 1 addition & 20 deletions main/src/main/java/net/citizensnpcs/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,7 @@ public static void faceLocation(Entity entity, Location to) {
public static void faceLocation(Entity entity, Location to, boolean headOnly) {
if (to == null || entity.getWorld() != to.getWorld())
return;
Location fromLocation = entity.getLocation(FROM_LOCATION);
double xDiff, yDiff, zDiff;
xDiff = to.getX() - fromLocation.getX();
yDiff = to.getY() - fromLocation.getY();
zDiff = to.getZ() - fromLocation.getZ();

double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);

double yaw = Math.toDegrees(Math.acos(xDiff / distanceXZ));
double pitch = Math.toDegrees(Math.acos(yDiff / distanceY)) - 90;
if (zDiff < 0.0)
yaw += Math.abs(180 - yaw) * 2;

if (headOnly) {
NMS.setHeadYaw(entity, (float) yaw - 90);
} else {
NMS.look(entity, (float) yaw - 90, (float) pitch);
}
NMS.look(entity, to, headOnly);
}

public static Location getEyeLocation(Entity entity) {
Expand Down Expand Up @@ -171,5 +153,4 @@ public static String prettyEnum(Enum<?> e) {
}

private static final Location AT_LOCATION = new Location(null, 0, 0, 0);
private static final Location FROM_LOCATION = new Location(null, 0, 0, 0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ public void setTargetLook(Entity target, float yawOffset, float renderOffset) {
controllerLook.a(target, yawOffset, renderOffset);
}

public void setTargetLook(Location target) {
controllerLook.a(target.getX(), target.getY(), target.getZ(), target.getYaw(), target.getPitch());
}

public void updateAI() {
controllerMove.c();
controllerLook.a();
Expand Down Expand Up @@ -469,6 +473,5 @@ public void setSkinName(String skinName, boolean forceUpdate) {
}

private static final float EPSILON = 0.005F;

private static final Location LOADED_LOCATION = new Location(null, 0, 0, 0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,38 @@ public void look(org.bukkit.entity.Entity entity, float yaw, float pitch) {
handle.pitch = pitch;
}

@Override
public void look(org.bukkit.entity.Entity entity, Location to, boolean headOnly) {
Entity handle = NMSImpl.getHandle(entity);
if (headOnly || (!(handle instanceof EntityInsentient) && !(handle instanceof EntityHumanNPC))) {
Location fromLocation = entity.getLocation(FROM_LOCATION);
double xDiff, yDiff, zDiff;
xDiff = to.getX() - fromLocation.getX();
yDiff = to.getY() - fromLocation.getY();
zDiff = to.getZ() - fromLocation.getZ();

double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);

double yaw = Math.toDegrees(Math.acos(xDiff / distanceXZ));
double pitch = Math.toDegrees(Math.acos(yDiff / distanceY)) - 90;
if (zDiff < 0.0)
yaw += Math.abs(180 - yaw) * 2;

if (headOnly) {
setHeadYaw(entity, (float) yaw - 90);
} else {
look(entity, (float) yaw - 90, (float) pitch);
}
}
if (handle instanceof EntityInsentient) {
((EntityInsentient) handle).getControllerLook().a(to.getX(), to.getY(), to.getZ(), to.getYaw(),
to.getPitch());
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).setTargetLook(to);
}
}

@Override
public void look(org.bukkit.entity.Entity from, org.bukkit.entity.Entity to) {
Entity handle = NMSImpl.getHandle(from), target = NMSImpl.getHandle(to);
Expand Down Expand Up @@ -1315,11 +1347,13 @@ public static void updateNavigation(NavigationAbstract navigation) {
}

private static final Field CRAFT_BOSSBAR_HANDLE_FIELD = NMS.getField(CraftBossBar.class, "handle");

private static final float DEFAULT_SPEED = 1F;
private static final Field ENDERDRAGON_BATTLE_BAR_FIELD = NMS.getField(EnderDragonBattle.class, "c");
private static final Field ENDERDRAGON_BATTLE_FIELD = NMS.getField(EntityEnderDragon.class, "bK");
private static Map<Class<?>, Integer> ENTITY_CLASS_TO_INT;
private static Map<Class<?>, String> ENTITY_CLASS_TO_NAME;
private static final Location FROM_LOCATION = new Location(null, 0, 0, 0);
public static Field GOAL_FIELD = NMS.getField(PathfinderGoalSelector.class, "b");
private static final Field JUMP_FIELD = NMS.getField(EntityLiving.class, "be");
private static Method MAKE_REQUEST;
Expand All @@ -1330,11 +1364,8 @@ public static void updateNavigation(NavigationAbstract navigation) {
private static final Field RABBIT_FIELD = NMS.getField(EntityRabbit.class, "bx");
private static final Random RANDOM = Util.getFastRandom();
private static Field SKULL_PROFILE_FIELD;

private static Field TRACKED_ENTITY_SET = NMS.getField(EntityTracker.class, "c");

private static final Field WITHER_BOSS_BAR_FIELD = NMS.getField(EntityWither.class, "bG");

static {
try {
Field field = NMS.getField(EntityTypes.class, "f");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ public void setTargetLook(Entity target, float yawOffset, float renderOffset) {
controllerLook.a(target, yawOffset, renderOffset);
}

public void setTargetLook(Location target) {
controllerLook.a(target.getX(), target.getY(), target.getZ(), target.getYaw(), target.getPitch());
}

public void updateAI() {
controllerMove.c();
controllerLook.a();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,38 @@ public void look(org.bukkit.entity.Entity entity, float yaw, float pitch) {
handle.pitch = pitch;
}

@Override
public void look(org.bukkit.entity.Entity entity, Location to, boolean headOnly) {
Entity handle = NMSImpl.getHandle(entity);
if (headOnly || (!(handle instanceof EntityInsentient) && !(handle instanceof EntityHumanNPC))) {
Location fromLocation = entity.getLocation(FROM_LOCATION);
double xDiff, yDiff, zDiff;
xDiff = to.getX() - fromLocation.getX();
yDiff = to.getY() - fromLocation.getY();
zDiff = to.getZ() - fromLocation.getZ();

double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);

double yaw = Math.toDegrees(Math.acos(xDiff / distanceXZ));
double pitch = Math.toDegrees(Math.acos(yDiff / distanceY)) - 90;
if (zDiff < 0.0)
yaw += Math.abs(180 - yaw) * 2;

if (headOnly) {
setHeadYaw(entity, (float) yaw - 90);
} else {
look(entity, (float) yaw - 90, (float) pitch);
}
}
if (handle instanceof EntityInsentient) {
((EntityInsentient) handle).getControllerLook().a(to.getX(), to.getY(), to.getZ(), to.getYaw(),
to.getPitch());
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).setTargetLook(to);
}
}

@Override
public void look(org.bukkit.entity.Entity from, org.bukkit.entity.Entity to) {
Entity handle = NMSImpl.getHandle(from), target = NMSImpl.getHandle(to);
Expand Down Expand Up @@ -1357,10 +1389,12 @@ public static void updateNavigation(NavigationAbstract navigation) {
}

private static final Field CRAFT_BOSSBAR_HANDLE_FIELD = NMS.getField(CraftBossBar.class, "handle");

private static final float DEFAULT_SPEED = 1F;
private static final Field ENDERDRAGON_BATTLE_BAR_FIELD = NMS.getField(EnderDragonBattle.class, "c");
private static final Field ENDERDRAGON_BATTLE_FIELD = NMS.getField(EntityEnderDragon.class, "bJ");
private static CustomEntityRegistry ENTITY_REGISTRY;
private static final Location FROM_LOCATION = new Location(null, 0, 0, 0);
public static Field GOAL_FIELD = NMS.getField(PathfinderGoalSelector.class, "b");
private static final Field JUMP_FIELD = NMS.getField(EntityLiving.class, "bd");
private static Method MAKE_REQUEST;
Expand All @@ -1372,6 +1406,7 @@ public static void updateNavigation(NavigationAbstract navigation) {
private static final Random RANDOM = Util.getFastRandom();
private static Field SKULL_PROFILE_FIELD;
private static Field TRACKED_ENTITY_SET = NMS.getField(EntityTracker.class, "c");

private static final Field WITHER_BOSS_BAR_FIELD = NMS.getField(EntityWither.class, "bF");

static {
Expand Down

0 comments on commit b695afd

Please sign in to comment.