mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-06-20 11:34:55 +02:00
Support java 12
This commit is contained in:
parent
d691416db1
commit
65cbb8850f
|
@ -88,12 +88,49 @@ public class NMS {
|
|||
return f;
|
||||
}
|
||||
|
||||
public static Field getFinalField(Class<?> clazz, String field) {
|
||||
return getFinalField(clazz, field, true);
|
||||
public static MethodHandle getFinalSetter(Class<?> clazz, String field) {
|
||||
return getFinalSetter(clazz, field, true);
|
||||
}
|
||||
|
||||
public static Field getFinalField(Class<?> clazz, String field, boolean log) {
|
||||
Field f = getField(clazz, field, log);
|
||||
public static MethodHandle getFinalSetter(Class<?> clazz, String field, boolean log) {
|
||||
Field f;
|
||||
if (MODIFIERS_FIELD == null) {
|
||||
if (UNSAFE == null) {
|
||||
try {
|
||||
UNSAFE = NMS.getField(Class.forName("sun.misc.Unsafe"), "theUnsafe").get(null);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
if (log) {
|
||||
Messaging.logTr(Messages.ERROR_GETTING_FIELD, field, e.getLocalizedMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
UNSAFE_STATIC_FIELD_OFFSET = NMS
|
||||
.getMethodHandle(UNSAFE.getClass(), "staticFieldOffset", true, Field.class).bindTo(UNSAFE);
|
||||
UNSAFE_FIELD_OFFSET = NMS.getMethodHandle(UNSAFE.getClass(), "objectFieldOffset", true, Field.class)
|
||||
.bindTo(UNSAFE);
|
||||
UNSAFE_PUT_OBJECT = NMS
|
||||
.getMethodHandle(UNSAFE.getClass(), "putObject", true, Object.class, long.class, Object.class)
|
||||
.bindTo(UNSAFE);
|
||||
}
|
||||
f = NMS.getField(clazz, field, log);
|
||||
if (f == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
boolean isStatic = Modifier.isStatic(f.getModifiers());
|
||||
long offset = (long) (isStatic ? UNSAFE_STATIC_FIELD_OFFSET.invoke(f) : UNSAFE_FIELD_OFFSET.invoke(f));
|
||||
return isStatic ? MethodHandles.insertArguments(UNSAFE_PUT_OBJECT, 0, clazz, offset)
|
||||
: MethodHandles.insertArguments(UNSAFE_PUT_OBJECT, 1, offset);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
if (log) {
|
||||
Messaging.logTr(Messages.ERROR_GETTING_FIELD, field, t.getLocalizedMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
f = getField(clazz, field, log);
|
||||
if (f == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -105,18 +142,6 @@ public class NMS {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
public static MethodHandle getFinalSetter(Class<?> clazz, String field) {
|
||||
return getFinalSetter(clazz, field, true);
|
||||
}
|
||||
|
||||
public static MethodHandle getFinalSetter(Class<?> clazz, String field, boolean log) {
|
||||
Field f = getFinalField(clazz, field, log);
|
||||
if (f == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return LOOKUP.unreflectSetter(f);
|
||||
} catch (Exception e) {
|
||||
|
@ -409,5 +434,9 @@ public class NMS {
|
|||
|
||||
private static NMSBridge BRIDGE;
|
||||
private static MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
|
||||
private static Field MODIFIERS_FIELD = NMS.getField(Field.class, "modifiers");
|
||||
private static Field MODIFIERS_FIELD = NMS.getField(Field.class, "modifiers", false);
|
||||
private static Object UNSAFE;
|
||||
private static MethodHandle UNSAFE_FIELD_OFFSET;
|
||||
private static MethodHandle UNSAFE_PUT_OBJECT;
|
||||
private static MethodHandle UNSAFE_STATIC_FIELD_OFFSET;
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package net.citizensnpcs.nms.v1_11_R1.util;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
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;
|
||||
|
@ -989,13 +989,10 @@ public class NMSImpl implements NMSBridge {
|
|||
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) {
|
||||
|
||||
MethodHandle setter = NMS.getFinalSetter(EntityTypes.class, "b");
|
||||
setter.invoke(ENTITY_REGISTRY.getWrapped());
|
||||
} catch (Throwable e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1530,12 +1527,11 @@ public class NMSImpl implements NMSBridge {
|
|||
static {
|
||||
try {
|
||||
Field field = NMS.getField(EntityTypes.class, "b");
|
||||
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) {
|
||||
MethodHandle setter = NMS.getFinalSetter(EntityTypes.class, "b");
|
||||
setter.invoke(ENTITY_REGISTRY);
|
||||
} catch (Throwable e) {
|
||||
Messaging.logTr(Messages.ERROR_GETTING_ID_MAPPING, e.getMessage());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
package net.citizensnpcs.nms.v1_12_R1.entity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.net.Socket;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -73,13 +72,13 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
|
|||
private PlayerControllerJump controllerJump;
|
||||
private PlayerControllerLook controllerLook;
|
||||
private PlayerControllerMove controllerMove;
|
||||
private boolean isTracked = false;
|
||||
private int jumpTicks = 0;
|
||||
private PlayerNavigation navigation;
|
||||
private final CitizensNPC npc;
|
||||
private final Location packetLocationCache = new Location(null, 0, 0, 0);
|
||||
private final SkinPacketTracker skinTracker;
|
||||
private int updateCounter = 0;
|
||||
private boolean isTracked = false;
|
||||
|
||||
public EntityHumanNPC(MinecraftServer minecraftServer, WorldServer world, GameProfile gameProfile,
|
||||
PlayerInteractManager playerInteractManager, NPC npc) {
|
||||
|
@ -95,8 +94,11 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
|
|||
}
|
||||
}
|
||||
|
||||
public void setTracked() {
|
||||
isTracked = true;
|
||||
@Override
|
||||
protected void a(double d0, boolean flag, IBlockData block, BlockPosition blockposition) {
|
||||
if (npc == null || !npc.isFlyable()) {
|
||||
super.a(d0, flag, block, blockposition);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -107,13 +109,6 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
|
|||
return super.a(entityplayer);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void a(double d0, boolean flag, IBlockData block, BlockPosition blockposition) {
|
||||
if (npc == null || !npc.isFlyable()) {
|
||||
super.a(d0, flag, block, blockposition);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(float f, float f1, float f2) {
|
||||
if (npc == null || !npc.isFlyable()) {
|
||||
|
@ -336,11 +331,9 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
|
|||
|
||||
EmptyAdvancementDataPlayer.clear(this.getAdvancementData());
|
||||
try {
|
||||
ADVANCEMENT_DATA_PLAYER.set(this,
|
||||
ADVANCEMENT_DATA_PLAYER.invoke(this,
|
||||
new EmptyAdvancementDataPlayer(minecraftServer, CitizensAPI.getDataFolder().getParentFile(), this));
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -446,6 +439,10 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
|
|||
controllerLook.a(target.getX(), target.getY(), target.getZ(), 10, 40);
|
||||
}
|
||||
|
||||
public void setTracked() {
|
||||
isTracked = true;
|
||||
}
|
||||
|
||||
public void updateAI() {
|
||||
controllerMove.a();
|
||||
controllerLook.a();
|
||||
|
@ -551,14 +548,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
|
|||
}
|
||||
}
|
||||
|
||||
private static Field ADVANCEMENT_DATA_PLAYER = NMS.getField(EntityPlayer.class, "bY");
|
||||
private static MethodHandle ADVANCEMENT_DATA_PLAYER = NMS.getFinalSetter(EntityPlayer.class, "bY");
|
||||
private static final float EPSILON = 0.005F;
|
||||
private static final Location LOADED_LOCATION = new Location(null, 0, 0, 0);
|
||||
static {
|
||||
Field modifiersField = NMS.getField(Field.class, "modifiers");
|
||||
try {
|
||||
modifiersField.setInt(ADVANCEMENT_DATA_PLAYER, ADVANCEMENT_DATA_PLAYER.getModifiers() & ~Modifier.FINAL);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package net.citizensnpcs.nms.v1_12_R1.util;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
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;
|
||||
|
@ -890,10 +890,8 @@ public class NMSImpl implements NMSBridge {
|
|||
@Override
|
||||
public void setDummyAdvancement(Player entity) {
|
||||
try {
|
||||
ADVANCEMENT_PLAYER_FIELD.set(getHandle(entity), DummyPlayerAdvancementData.INSTANCE);
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
ADVANCEMENT_PLAYER_FIELD.invoke(getHandle(entity), DummyPlayerAdvancementData.INSTANCE);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -1003,12 +1001,10 @@ public class NMSImpl implements NMSBridge {
|
|||
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) {
|
||||
MethodHandle setter = NMS.getFinalSetter(EntityTypes.class, "b");
|
||||
setter.invoke(ENTITY_REGISTRY.getWrapped());
|
||||
} catch (Throwable e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1518,7 +1514,7 @@ public class NMSImpl implements NMSBridge {
|
|||
navigation.d();
|
||||
};
|
||||
|
||||
private static Field ADVANCEMENT_PLAYER_FIELD = NMS.getFinalField(EntityPlayer.class, "bY");
|
||||
private static MethodHandle ADVANCEMENT_PLAYER_FIELD = NMS.getFinalSetter(EntityPlayer.class, "bY");
|
||||
private static final Set<EntityType> BAD_CONTROLLER_LOOK = EnumSet.of(EntityType.POLAR_BEAR, EntityType.SILVERFISH,
|
||||
EntityType.SHULKER, EntityType.ENDERMITE, EntityType.ENDER_DRAGON, EntityType.BAT, EntityType.SLIME,
|
||||
EntityType.MAGMA_CUBE, EntityType.HORSE, EntityType.GHAST);
|
||||
|
@ -1543,11 +1539,12 @@ public class NMSImpl implements NMSBridge {
|
|||
|
||||
static {
|
||||
try {
|
||||
Field field = NMS.getFinalField(EntityTypes.class, "b");
|
||||
Field field = NMS.getField(EntityTypes.class, "b");
|
||||
ENTITY_REGISTRY = new CustomEntityRegistry(
|
||||
(RegistryMaterials<MinecraftKey, Class<? extends Entity>>) field.get(null));
|
||||
field.set(null, ENTITY_REGISTRY);
|
||||
} catch (Exception e) {
|
||||
MethodHandle setter = NMS.getFinalSetter(EntityTypes.class, "b");
|
||||
setter.invoke(ENTITY_REGISTRY);
|
||||
} catch (Throwable e) {
|
||||
Messaging.logTr(Messages.ERROR_GETTING_ID_MAPPING, e.getMessage());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
package net.citizensnpcs.nms.v1_13_R2.entity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.net.Socket;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -296,11 +295,9 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
|
|||
|
||||
EmptyAdvancementDataPlayer.clear(this.getAdvancementData());
|
||||
try {
|
||||
ADVANCEMENT_DATA_PLAYER.set(this,
|
||||
ADVANCEMENT_DATA_PLAYER.invoke(this,
|
||||
new EmptyAdvancementDataPlayer(minecraftServer, CitizensAPI.getDataFolder().getParentFile(), this));
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -551,14 +548,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder, Skinnable
|
|||
}
|
||||
}
|
||||
|
||||
private static Field ADVANCEMENT_DATA_PLAYER = NMS.getField(EntityPlayer.class, "cf");
|
||||
private static MethodHandle ADVANCEMENT_DATA_PLAYER = NMS.getFinalSetter(EntityPlayer.class, "cf");
|
||||
private static final float EPSILON = 0.005F;
|
||||
private static final Location LOADED_LOCATION = new Location(null, 0, 0, 0);
|
||||
static {
|
||||
Field modifiersField = NMS.getField(Field.class, "modifiers");
|
||||
try {
|
||||
modifiersField.setInt(ADVANCEMENT_DATA_PLAYER, ADVANCEMENT_DATA_PLAYER.getModifiers() & ~Modifier.FINAL);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,10 +34,10 @@ public class NMSBoundingBox {
|
|||
return new BoundingBox(minX, minY, minZ, maxX, maxY, maxZ);
|
||||
}
|
||||
|
||||
private static final Field a = NMS.getFinalField(AxisAlignedBB.class, "a", false);
|
||||
private static final Field b = NMS.getFinalField(AxisAlignedBB.class, "b", false);
|
||||
private static final Field c = NMS.getFinalField(AxisAlignedBB.class, "c", false);
|
||||
private static final Field d = NMS.getFinalField(AxisAlignedBB.class, "d", false);
|
||||
private static final Field e = NMS.getFinalField(AxisAlignedBB.class, "e", false);
|
||||
private static final Field f = NMS.getFinalField(AxisAlignedBB.class, "f", false);
|
||||
private static final Field a = NMS.getField(AxisAlignedBB.class, "a", false);
|
||||
private static final Field b = NMS.getField(AxisAlignedBB.class, "b", false);
|
||||
private static final Field c = NMS.getField(AxisAlignedBB.class, "c", false);
|
||||
private static final Field d = NMS.getField(AxisAlignedBB.class, "d", false);
|
||||
private static final Field e = NMS.getField(AxisAlignedBB.class, "e", false);
|
||||
private static final Field f = NMS.getField(AxisAlignedBB.class, "f", false);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package net.citizensnpcs.nms.v1_13_R2.util;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.SocketAddress;
|
||||
|
@ -916,10 +917,8 @@ public class NMSImpl implements NMSBridge {
|
|||
@Override
|
||||
public void setDummyAdvancement(Player entity) {
|
||||
try {
|
||||
ADVANCEMENT_PLAYER_FIELD.set(getHandle(entity), DummyPlayerAdvancementData.INSTANCE);
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
ADVANCEMENT_PLAYER_FIELD.invoke(getHandle(entity), DummyPlayerAdvancementData.INSTANCE);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -1029,13 +1028,13 @@ public class NMSImpl implements NMSBridge {
|
|||
public void shutdown() {
|
||||
if (ENTITY_REGISTRY == null)
|
||||
return;
|
||||
Field field = NMS.getFinalField(EntityTypes.class, "REGISTRY", false);
|
||||
MethodHandle field = NMS.getFinalSetter(EntityTypes.class, "REGISTRY", false);
|
||||
if (field == null) {
|
||||
field = NMS.getFinalField(IRegistry.class, "ENTITY_TYPE");
|
||||
field = NMS.getFinalSetter(IRegistry.class, "ENTITY_TYPE", false);
|
||||
}
|
||||
try {
|
||||
field.set(null, ENTITY_REGISTRY.getWrapped());
|
||||
} catch (Exception e) {
|
||||
field.invoke(ENTITY_REGISTRY.getWrapped());
|
||||
} catch (Throwable e) {
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1623,7 +1622,7 @@ public class NMSImpl implements NMSBridge {
|
|||
navigation.d();
|
||||
}
|
||||
|
||||
private static Field ADVANCEMENT_PLAYER_FIELD = NMS.getFinalField(EntityPlayer.class, "cf");
|
||||
private static MethodHandle ADVANCEMENT_PLAYER_FIELD = NMS.getFinalSetter(EntityPlayer.class, "cf");
|
||||
private static final Set<EntityType> BAD_CONTROLLER_LOOK = EnumSet.of(EntityType.POLAR_BEAR, EntityType.SILVERFISH,
|
||||
EntityType.SHULKER, EntityType.ENDERMITE, EntityType.ENDER_DRAGON, EntityType.BAT, EntityType.SLIME,
|
||||
EntityType.MAGMA_CUBE, EntityType.HORSE, EntityType.GHAST);
|
||||
|
@ -1658,13 +1657,17 @@ public class NMSImpl implements NMSBridge {
|
|||
}
|
||||
|
||||
try {
|
||||
Field field = NMS.getFinalField(EntityTypes.class, "REGISTRY", false);
|
||||
MethodHandle setter = NMS.getFinalSetter(EntityTypes.class, "REGISTRY", false);
|
||||
if (setter == null) {
|
||||
setter = NMS.getFinalSetter(IRegistry.class, "ENTITY_TYPE", false);
|
||||
}
|
||||
Field field = NMS.getField(EntityTypes.class, "REGISTRY", false);
|
||||
if (field == null) {
|
||||
field = NMS.getFinalField(IRegistry.class, "ENTITY_TYPE");
|
||||
field = NMS.getField(IRegistry.class, "ENTITY_TYPE");
|
||||
}
|
||||
ENTITY_REGISTRY = new CustomEntityRegistry((RegistryMaterials<EntityTypes<?>>) field.get(null));
|
||||
field.set(null, ENTITY_REGISTRY);
|
||||
} catch (Exception e) {
|
||||
setter.invoke(ENTITY_REGISTRY);
|
||||
} catch (Throwable e) {
|
||||
Messaging.logTr(Messages.ERROR_GETTING_ID_MAPPING, e.getMessage());
|
||||
}
|
||||
|
||||
|
|
|
@ -1510,7 +1510,7 @@ public class NMSImpl implements NMSBridge {
|
|||
if (RABBIT_FIELD == null)
|
||||
return null;
|
||||
try {
|
||||
return (DataWatcherObject<Integer>) RABBIT_FIELD.invoke(null);
|
||||
return (DataWatcherObject<Integer>) RABBIT_FIELD.invoke();
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -1691,10 +1691,10 @@ public class NMSImpl implements NMSBridge {
|
|||
private static Field SKULL_PROFILE_FIELD;
|
||||
static {
|
||||
try {
|
||||
Field field = NMS.getFinalField(IRegistry.class, "ENTITY_TYPE");
|
||||
ENTITY_REGISTRY = new CustomEntityRegistry((RegistryBlocks<EntityTypes<?>>) field.get(null));
|
||||
field.set(null, ENTITY_REGISTRY);
|
||||
} catch (Exception e) {
|
||||
ENTITY_REGISTRY = new CustomEntityRegistry(
|
||||
(RegistryBlocks<EntityTypes<?>>) NMS.getGetter(IRegistry.class, "ENTITY_TYPE").invoke());
|
||||
NMS.getFinalSetter(IRegistry.class, "ENTITY_TYPE").invoke(ENTITY_REGISTRY);
|
||||
} catch (Throwable e) {
|
||||
Messaging.logTr(Messages.ERROR_GETTING_ID_MAPPING, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user