Skip to content

Commit

Permalink
Check instanceof CraftEntity in a few more places
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 27, 2014
1 parent c203eb1 commit 88600ed
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/main/java/net/citizensnpcs/util/NMS.java
Expand Up @@ -46,8 +46,6 @@
import org.bukkit.craftbukkit.v1_7_R2.CraftSound;
import org.bukkit.craftbukkit.v1_7_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_7_R2.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_7_R2.entity.CraftLivingEntity;
import org.bukkit.craftbukkit.v1_7_R2.entity.CraftPlayer;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.LivingEntity;
Expand Down Expand Up @@ -203,10 +201,12 @@ public static Field getField(Class<?> clazz, String field) {
}

public static EntityLiving getHandle(LivingEntity entity) {
return ((CraftLivingEntity) entity).getHandle();
return (EntityLiving) getHandle((org.bukkit.entity.Entity) entity);
}

public static net.minecraft.server.v1_7_R2.Entity getHandle(org.bukkit.entity.Entity entity) {
public static Entity getHandle(org.bukkit.entity.Entity entity) {
if (!(entity instanceof CraftEntity))
return null;
return ((CraftEntity) entity).getHandle();
}

Expand All @@ -231,12 +231,14 @@ public static String getSound(String flag) throws CommandException {
}

public static float getSpeedFor(NPC npc) {
if (!npc.isSpawned() || !(npc instanceof LivingEntity))
if (!npc.isSpawned() || !(npc.getEntity() instanceof LivingEntity))
return DEFAULT_SPEED;
EntityLiving handle = NMS.getHandle((LivingEntity) npc.getEntity());
if (handle == null)
return DEFAULT_SPEED;
// this is correct, but too slow. TODO: investigate
return (float) ((EntityLiving) NMS.getHandle(npc.getEntity())).getAttributeInstance(GenericAttributes.d)
.getValue();
// return DEFAULT_SPEED;
return DEFAULT_SPEED;
// return (float)
// handle.getAttributeInstance(GenericAttributes.d).getValue();
}

public static float getStepHeight(LivingEntity entity) {
Expand All @@ -260,6 +262,8 @@ public static void initNetworkManager(NetworkManager network) {

public static boolean inWater(org.bukkit.entity.Entity entity) {
Entity mcEntity = getHandle(entity);
if (mcEntity == null)
return false;
return mcEntity.L() || mcEntity.O();
}

Expand All @@ -281,6 +285,8 @@ public static void look(Entity handle, Entity target) {

public static void look(org.bukkit.entity.Entity entity, float yaw, float pitch) {
Entity handle = getHandle(entity);
if (handle == null)
return;
handle.yaw = yaw;
setHeadYaw(handle, yaw);
handle.pitch = pitch;
Expand All @@ -307,13 +313,19 @@ public static float modifiedSpeed(float baseSpeed, NPC npc) {
}

public static void mount(org.bukkit.entity.Entity entity, org.bukkit.entity.Entity passenger) {
if (NMS.getHandle(passenger) == null)
return;
NMS.getHandle(passenger).mount(NMS.getHandle(entity));
}

public static void openHorseScreen(Horse horse, Player equipper) {
EntityLiving handle = NMS.getHandle(horse);
EntityLiving equipperHandle = NMS.getHandle(equipper);
if (handle == null || equipperHandle == null)
return;
boolean wasTamed = horse.isTamed();
horse.setTamed(true);
((EntityHorse) getHandle(horse)).a((EntityHuman) NMS.getHandle(equipper));
((EntityHorse) handle).a((EntityHuman) equipperHandle);
horse.setTamed(wasTamed);
}

Expand All @@ -337,14 +349,14 @@ public static void registerEntityClass(Class<?> clazz) {
}

public static void removeFromServerPlayerList(Player player) {
EntityPlayer handle = ((CraftPlayer) player).getHandle();
EntityPlayer handle = (EntityPlayer) NMS.getHandle(player);
((CraftServer) Bukkit.getServer()).getHandle().players.remove(handle);
}

public static void sendPacket(Player player, Packet packet) {
if (packet == null)
return;
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
((EntityPlayer) NMS.getHandle(player)).playerConnection.sendPacket(packet);
}

public static void sendPacketNearby(Player from, Location location, Packet packet) {
Expand Down Expand Up @@ -387,7 +399,9 @@ public static void sendToOnline(Packet... packets) {
}

public static void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
Entity handle = ((CraftEntity) entity).getHandle();
Entity handle = NMS.getHandle(entity);
if (handle == null)
return;
if (handle instanceof EntityInsentient) {
((EntityInsentient) handle).getControllerMove().a(x, y, z, speed);
} else if (handle instanceof EntityHumanNPC) {
Expand All @@ -414,6 +428,8 @@ public static void setHeadYaw(Entity en, float yaw) {

public static void setShouldJump(org.bukkit.entity.Entity entity) {
Entity handle = getHandle(entity);
if (handle == null)
return;
if (handle instanceof EntityInsentient) {
ControllerJump controller = ((EntityInsentient) handle).getControllerJump();
controller.a();
Expand Down Expand Up @@ -498,8 +514,8 @@ public static void updateNavigation(Navigation navigation) {
public static void updateNavigationWorld(org.bukkit.entity.Entity entity, org.bukkit.World world) {
if (NAVIGATION_WORLD_FIELD == null)
return;
Entity en = ((CraftEntity) entity).getHandle();
if (!(en instanceof EntityInsentient))
Entity en = NMS.getHandle(entity);
if (en == null || !(en instanceof EntityInsentient))
return;
EntityInsentient handle = (EntityInsentient) en;
World worldHandle = ((CraftWorld) world).getHandle();
Expand All @@ -513,7 +529,7 @@ public static void updateNavigationWorld(org.bukkit.entity.Entity entity, org.bu
public static void updatePathfindingRange(NPC npc, float pathfindingRange) {
if (!npc.isSpawned() || !npc.getEntity().getType().isAlive())
return;
EntityLiving en = ((CraftLivingEntity) npc.getEntity()).getHandle();
EntityLiving en = NMS.getHandle((LivingEntity) npc.getEntity());
if (!(en instanceof EntityInsentient)) {
if (en instanceof EntityHumanNPC) {
((EntityHumanNPC) en).updatePathfindingRange(pathfindingRange);
Expand Down

0 comments on commit 88600ed

Please sign in to comment.