Skip to content

Commit

Permalink
More cleanup, fix mistranslated field breaking head yaw for players
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jul 3, 2013
1 parent 04c4fdd commit d4ada89
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 52 deletions.
23 changes: 3 additions & 20 deletions src/main/java/net/citizensnpcs/trait/Controllable.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.citizensnpcs.trait;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Map;

import net.citizensnpcs.Settings.Setting;
Expand Down Expand Up @@ -227,16 +226,9 @@ public void run(Player rider) {
}

public class GroundController implements MovementController {
private int jumpTicks;
private int jumpTicks = 0;
private double speed = 0.07D;

private void jump() {
boolean allowed = getHandle().onGround;
if (!allowed)
return;
getHandle().motY = JUMP_VELOCITY;
}

@Override
public void leftClick(PlayerInteractEvent event) {
}
Expand Down Expand Up @@ -280,18 +272,10 @@ private void updateSpeed(EntityLiving handle, float speedMod) {
newSpeed = 0.35D;
}

boolean shouldJump = false;
try {
if (JUMP_FIELD != null)
shouldJump = JUMP_FIELD.getBoolean(handle.passenger);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
boolean shouldJump = NMS.shouldJump(handle.passenger);
if (shouldJump) {
if (handle.onGround && jumpTicks == 0) {
jump();
getHandle().motY = JUMP_VELOCITY;
jumpTicks = 10;
}
} else {
Expand Down Expand Up @@ -323,7 +307,6 @@ public static interface MovementController {

private static final Map<EntityType, Class<? extends MovementController>> controllerTypes = Maps
.newEnumMap(EntityType.class);
private static final Field JUMP_FIELD = NMS.getField(EntityLiving.class, "bd");

static {
controllerTypes.put(EntityType.BAT, AirController.class);
Expand Down
56 changes: 25 additions & 31 deletions src/main/java/net/citizensnpcs/util/NMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static void clearGoals(PathfinderGoalSelector... goalSelectors) {
List<?> list = (List<?>) NMS.GOAL_FIELD.get(selector);
list.clear();
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_CLEARING_GOALS, e.getMessage());
Messaging.logTr(Messages.ERROR_CLEARING_GOALS, e.getLocalizedMessage());
}
}
}
Expand All @@ -130,7 +130,7 @@ public static Field getField(Class<?> clazz, String field) {
f = clazz.getDeclaredField(field);
f.setAccessible(true);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, field, e.getMessage());
Messaging.logTr(Messages.ERROR_GETTING_FIELD, field, e.getLocalizedMessage());
}
return f;
}
Expand All @@ -140,7 +140,7 @@ public static EntityLiving getHandle(LivingEntity entity) {
}

public static float getHeadYaw(EntityLiving handle) {
return handle.aA;
return handle.aP;
}

public static float getSpeedFor(NPC npc) {
Expand Down Expand Up @@ -300,6 +300,19 @@ public static void setShouldJump(LivingEntity entity) {
}
}

public static boolean shouldJump(net.minecraft.server.v1_6_R1.Entity entity) {
if (JUMP_FIELD == null || !(entity instanceof EntityLiving))
return false;
try {
return JUMP_FIELD.getBoolean(entity);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}

public static org.bukkit.entity.Entity spawnCustomEntity(org.bukkit.World world, Location at,
Class<? extends Entity> clazz, EntityType type) {
World handle = ((CraftWorld) world).getHandle();
Expand Down Expand Up @@ -381,44 +394,25 @@ public static void updatePathfindingRange(NPC npc, float pathfindingRange) {
}

private static final float DEFAULT_SPEED = 1F;

private static Map<Class<?>, Integer> ENTITY_CLASS_TO_INT;
private static final Map<Class<?>, Constructor<?>> ENTITY_CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
private static Map<Integer, Class<?>> ENTITY_INT_TO_CLASS;
private static Field GOAL_FIELD;
private static Field LAND_SPEED_MODIFIER_FIELD;
private static Field GOAL_FIELD = getField(PathfinderGoalSelector.class, "a");
private static final Field JUMP_FIELD = getField(EntityLiving.class, "bd");
private static Field LAND_SPEED_MODIFIER_FIELD = getField(EntityLiving.class, "bs");
private static final Map<EntityType, Float> MOVEMENT_SPEEDS = Maps.newEnumMap(EntityType.class);
private static Field NAVIGATION_WORLD_FIELD;
private static Field NAVIGATION_WORLD_FIELD = getField(Navigation.class, "b");
private static final Location PACKET_CACHE_LOCATION = new Location(null, 0, 0, 0);
private static Field PATHFINDING_RANGE;
private static Field PATHFINDING_RANGE = getField(Navigation.class, "d");
private static final Random RANDOM = Util.getFastRandom();
private static Field SPEED_FIELD;

private static Field THREAD_STOPPER;
private static Field SPEED_FIELD = getField(EntityLiving.class, "bI");
private static Field THREAD_STOPPER = getField(NetworkManager.class, "n");
// true field above false and three synchronised lists

static {
// TODO: speed fields are all wrong - need to use attributes

// true field above false and three synchronised lists
THREAD_STOPPER = getField(NetworkManager.class, "n");

// constants taken from source code
MOVEMENT_SPEEDS.put(EntityType.CHICKEN, 1F);
MOVEMENT_SPEEDS.put(EntityType.COW, 1F);
MOVEMENT_SPEEDS.put(EntityType.CREEPER, 1F);
MOVEMENT_SPEEDS.put(EntityType.IRON_GOLEM, 1F);
MOVEMENT_SPEEDS.put(EntityType.MUSHROOM_COW, 1F);
MOVEMENT_SPEEDS.put(EntityType.OCELOT, 1F);
MOVEMENT_SPEEDS.put(EntityType.SHEEP, 1F);
MOVEMENT_SPEEDS.put(EntityType.SNOWMAN, 1F);
MOVEMENT_SPEEDS.put(EntityType.PIG, 1F);
MOVEMENT_SPEEDS.put(EntityType.PLAYER, 1F);
MOVEMENT_SPEEDS.put(EntityType.VILLAGER, 1F);
LAND_SPEED_MODIFIER_FIELD = getField(EntityLiving.class, "bs");
SPEED_FIELD = getField(EntityLiving.class, "bI");
NAVIGATION_WORLD_FIELD = getField(Navigation.class, "b");
PATHFINDING_RANGE = getField(Navigation.class, "d");
GOAL_FIELD = getField(PathfinderGoalSelector.class, "a");

try {
Field field = getField(EntityTypes.class, "d");
ENTITY_INT_TO_CLASS = (Map<Integer, Class<?>>) field.get(null);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ citizens.limits.over-npc-limt=Over the NPC limit of {0}.
citizens.load-task-error=NPC load task couldn''t be scheduled, disabling...
citizens.nms-errors.clearing-goals=Could not clear goals: {0}.
citizens.nms-errors.error-setting-persistent=Could not set NPC as persistent: {0}. NPC entity may despawn.
citizens.nms-errors.getting-field=Could not fetch field {0}: {1}.
citizens.nms-errors.getting-field=Could not fetch NMS field {0}: {1}.
citizens.nms-errors.getting-id-mapping=Could not fetch entity id mapping fields: {0}.
citizens.nms-errors.spawning-custom-entity=Could not spawn custom entity: {0}.
citizens.nms-errors.stopping-network-threads=Could not stop network threads: {0}.
Expand Down

0 comments on commit d4ada89

Please sign in to comment.