Modify EntityTypes hooks to be less invasive

This commit is contained in:
fullwall 2016-11-22 01:00:52 +08:00
parent bf7f92cd8f
commit 765c42cb45
8 changed files with 97 additions and 5 deletions

View File

@ -31,7 +31,7 @@
</repository>
<repository>
<id>vault-repo</id>
<url>http://nexus.theyeticave.net/content/repositories/pub_releases</url>
<url>http://nexus.hc.to/content/repositories/pub_releases</url>
</repository>
</repositories>

View File

@ -247,6 +247,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
saves.saveToDiskImmediate();
despawnNPCs();
npcRegistry = null;
NMS.shutdown();
}
CitizensAPI.shutdown();

View File

@ -249,6 +249,10 @@ public class NMS {
return BRIDGE.shouldJump(entity);
}
public static void shutdown() {
BRIDGE.shutdown();
}
public static boolean tick(Entity next) {
return BRIDGE.tick(next);
}

View File

@ -126,6 +126,8 @@ public interface NMSBridge {
public boolean shouldJump(Entity entity);
public void shutdown();
public boolean tick(Entity next);
public void trySwim(Entity entity);

View File

@ -829,6 +829,10 @@ public class NMSImpl implements NMSBridge {
return false;
}
@Override
public void shutdown() {
}
@Override
public boolean tick(org.bukkit.entity.Entity next) {
Entity entity = NMSImpl.getHandle(next);
@ -1326,6 +1330,7 @@ public class NMSImpl implements NMSBridge {
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");

View File

@ -47,6 +47,8 @@ import net.minecraft.server.v1_11_R1.DamageSource;
import net.minecraft.server.v1_11_R1.Entity;
import net.minecraft.server.v1_11_R1.EntityHuman;
import net.minecraft.server.v1_11_R1.EntityPlayer;
import net.minecraft.server.v1_11_R1.EntityTypes;
import net.minecraft.server.v1_11_R1.EntityVillager;
import net.minecraft.server.v1_11_R1.EnumGamemode;
import net.minecraft.server.v1_11_R1.EnumItemSlot;
import net.minecraft.server.v1_11_R1.EnumProtocolDirection;
@ -109,6 +111,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
super.A_();
if (npc == null)
return;
System.out.println(EntityTypes.getName(EntityVillager.class));
if (updateCounter + 1 > Setting.PACKET_UPDATE_DELAY.asInt()) {
updateEffects = true;
}

View File

@ -0,0 +1,58 @@
package net.citizensnpcs.nms.v1_11_R1.util;
import java.util.Map;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Maps;
import net.minecraft.server.v1_11_R1.Entity;
import net.minecraft.server.v1_11_R1.MinecraftKey;
import net.minecraft.server.v1_11_R1.RegistryMaterials;
public class CustomEntityRegistry extends RegistryMaterials<MinecraftKey, Class<? extends Entity>> {
private final BiMap<MinecraftKey, Class<? extends Entity>> entities = HashBiMap.create();
private final BiMap<Class<? extends Entity>, MinecraftKey> entityClasses = this.entities.inverse();
private final Map<Class<? extends Entity>, Integer> entityIds = Maps.newHashMap();
private final RegistryMaterials<MinecraftKey, Class<? extends Entity>> wrapped;
public CustomEntityRegistry(RegistryMaterials<MinecraftKey, Class<? extends Entity>> original) {
this.wrapped = original;
}
@Override
public int a(Class<? extends Entity> key) {
if (this.entityIds.containsKey(key)) {
return this.entityIds.get(key);
}
return this.wrapped.a(key);
}
@Override
public MinecraftKey b(Class<? extends Entity> value) {
if (entityClasses.containsKey(value)) {
return entityClasses.get(value);
}
return wrapped.b(value);
}
@Override
public Class<? extends Entity> get(MinecraftKey key) {
if (entities.containsKey(key)) {
return entities.get(key);
}
return wrapped.get(key);
}
public RegistryMaterials<MinecraftKey, Class<? extends Entity>> getWrapped() {
return wrapped;
}
public void put(int entityId, MinecraftKey key, Class<? extends Entity> entityClass) {
this.entities.put(key, entityClass);
this.entityIds.put(entityClass, entityId);
}
}

View File

@ -2,6 +2,7 @@ package net.citizensnpcs.nms.v1_11_R1.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.SocketAddress;
import java.net.URL;
import java.util.ArrayList;
@ -652,7 +653,7 @@ public class NMSImpl implements NMSBridge {
@Override
public void registerEntityClass(Class<?> clazz) {
if (ENTITY_REGISTRY == null || ENTITY_REGISTRY.b((Class<? extends Entity>) clazz) != null)
if (ENTITY_REGISTRY == null)
return;
Class<?> search = clazz;
@ -661,7 +662,7 @@ public class NMSImpl implements NMSBridge {
if (key == null)
continue;
int code = ENTITY_REGISTRY.a((Class<? extends Entity>) search);
ENTITY_REGISTRY.a(code, key, (Class<? extends Entity>) clazz);
ENTITY_REGISTRY.put(code, key, (Class<? extends Entity>) clazz);
return;
}
throw new IllegalArgumentException("unable to find valid entity superclass for class " + clazz.toString());
@ -861,6 +862,20 @@ public class NMSImpl implements NMSBridge {
return false;
}
@Override
public void shutdown() {
if (ENTITY_REGISTRY == null)
return;
Field field = NMS.getField(EntityTypes.class, "b");
Field modifiersField = NMS.getField(Field.class, "modifiers");
try {
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, ENTITY_REGISTRY.getWrapped());
} catch (Exception e) {
}
}
@Override
public boolean tick(org.bukkit.entity.Entity next) {
Entity entity = NMSImpl.getHandle(next);
@ -1345,7 +1360,7 @@ public class NMSImpl implements NMSBridge {
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 RegistryMaterials<MinecraftKey, Class<? extends Entity>> ENTITY_REGISTRY;
private static CustomEntityRegistry ENTITY_REGISTRY;
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;
@ -1362,7 +1377,11 @@ public class NMSImpl implements NMSBridge {
static {
try {
Field field = NMS.getField(EntityTypes.class, "b");
ENTITY_REGISTRY = (RegistryMaterials<MinecraftKey, Class<? extends Entity>>) field.get(null);
Field modifiersField = NMS.getField(Field.class, "modifiers");
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
ENTITY_REGISTRY = new CustomEntityRegistry(
(RegistryMaterials<MinecraftKey, Class<? extends Entity>>) field.get(null));
field.set(null, ENTITY_REGISTRY);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_ID_MAPPING, e.getMessage());
}