From 84f0ccee8175a12c4f9fa86866b085925e43d6af Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 22 Feb 2022 20:00:22 +0100 Subject: [PATCH] Fix 1.8/older versions Signed-off-by: Lixfel --- .../de/steamwar/fightsystem/fight/Fight.java | 3 +-- .../steamwar/fightsystem/record/REntity.java | 20 +++++++++---------- .../fightsystem/utils/ProtocolAPI.java | 12 ++--------- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/fight/Fight.java b/FightSystem_Core/src/de/steamwar/fightsystem/fight/Fight.java index 80e3cfa..431cbc6 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/fight/Fight.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/fight/Fight.java @@ -27,7 +27,6 @@ import de.steamwar.fightsystem.ArenaMode; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.record.GlobalRecorder; -import de.steamwar.fightsystem.utils.ProtocolAPI; import de.steamwar.fightsystem.utils.ProtocolWrapper; import org.bukkit.Bukkit; import org.bukkit.GameMode; @@ -150,7 +149,7 @@ public class Fight { } public static Object playerInfoPacket(Object action, GameProfile profile, Object mode) { - Object packet = ProtocolAPI.construct(playerInfoPacket); + Object packet = Reflection.newInstance(playerInfoPacket); playerInfoAction.set(packet, action); playerInfoData.set(packet, Collections.singletonList(ProtocolWrapper.impl.playerInfoDataConstructor(packet, profile, mode))); return packet; diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/record/REntity.java b/FightSystem_Core/src/de/steamwar/fightsystem/record/REntity.java index 3da799d..ceed2f1 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/record/REntity.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/record/REntity.java @@ -177,7 +177,7 @@ public class REntity { private static final Reflection.FieldAccessor animationEntity = Reflection.getField(animationPacket, int.class, 0); private static final Reflection.FieldAccessor animationAnimation = Reflection.getField(animationPacket, int.class, 1); public void animation(byte animation) { - Object packet = ProtocolAPI.construct(animationPacket); + Object packet = Reflection.newInstance(animationPacket); animationEntity.set(packet, entityId); animationAnimation.set(packet, (int) animation); ProtocolAPI.broadcastPacket(packet); @@ -189,7 +189,7 @@ public class REntity { private static final Reflection.FieldAccessor velocityY = Reflection.getField(velocityPacket, int.class, 2); private static final Reflection.FieldAccessor velocityZ = Reflection.getField(velocityPacket, int.class, 3); public void setVelocity(double dX, double dY, double dZ) { - Object packet = ProtocolAPI.construct(velocityPacket); + Object packet = Reflection.newInstance(velocityPacket); velocityEntity.set(packet, entityId); velocityX.set(packet, calcVelocity(dX)); velocityY.set(packet, calcVelocity(dY)); @@ -201,7 +201,7 @@ public class REntity { private static final Reflection.FieldAccessor statusEntity = Reflection.getField(statusPacket, int.class, 0); private static final Reflection.FieldAccessor statusStatus = Reflection.getField(statusPacket, byte.class, 0); public void damage() { - Object packet = ProtocolAPI.construct(statusPacket); + Object packet = Reflection.newInstance(statusPacket); statusEntity.set(packet, entityId); statusStatus.set(packet, (byte) 2); ProtocolAPI.broadcastPacket(packet); @@ -259,7 +259,7 @@ public class REntity { team.removeEntry(name); } - Object packet = ProtocolAPI.construct(destroyPacket); + Object packet = Reflection.newInstance(destroyPacket); destroyEntities.set(packet, Core.getVersion() > 15 ? new IntArrayList(new int[]{entityId}) : new int[]{entityId}); ProtocolAPI.broadcastPacket(packet); } @@ -272,7 +272,7 @@ public class REntity { private static final Reflection.FieldAccessor metadataEntity = Reflection.getField(metadataPacket, int.class, 0); private static final Reflection.FieldAccessor metadataMetadata = Reflection.getField(metadataPacket, List.class, 0); private Object getDataWatcherPacket(Object dataWatcherObject, Object value) { - Object packet = ProtocolAPI.construct(metadataPacket); + Object packet = Reflection.newInstance(metadataPacket); metadataEntity.set(packet, entityId); metadataMetadata.set(packet, Collections.singletonList(BountifulWrapper.impl.getDataWatcherItem(dataWatcherObject, value))); return packet; @@ -283,7 +283,7 @@ public class REntity { private static final Reflection.FieldAccessor teleportYaw = Reflection.getField(teleportPacket, byte.class, 0); private static final Reflection.FieldAccessor teleportPitch = Reflection.getField(teleportPacket, byte.class, 1); private Object getTeleportPacket(){ - Object packet = ProtocolAPI.construct(teleportPacket); + Object packet = Reflection.newInstance(teleportPacket); teleportEntity.set(packet, entityId); BountifulWrapper.impl.setTeleportPacketPosition(packet, locX, locY, locZ); teleportYaw.set(packet, yaw); @@ -295,7 +295,7 @@ public class REntity { private static final Reflection.FieldAccessor headRotationEntity = Reflection.getField(headRotationPacket, int.class, 0); private static final Reflection.FieldAccessor headRotationYaw = Reflection.getField(headRotationPacket, byte.class, 0); private Object getHeadRotationPacket(){ - Object packet = ProtocolAPI.construct(headRotationPacket); + Object packet = Reflection.newInstance(headRotationPacket); headRotationEntity.set(packet, entityId); headRotationYaw.set(packet, headYaw); return packet; @@ -304,7 +304,7 @@ public class REntity { public static final Class spawnPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutSpawnEntity"); private static final Reflection.FieldAccessor spawnEntity = Reflection.getField(spawnPacket, int.class, 0); private Object getSpawnEntityPacket(){ - Object packet = ProtocolAPI.construct(spawnPacket); + Object packet = Reflection.newInstance(spawnPacket); spawnEntity.set(packet, entityId); BountifulWrapper.impl.setSpawnPacketUUID(packet, uuid); ProtocolWrapper.impl.setSpawnPacketType(packet, entityType); @@ -318,7 +318,7 @@ public class REntity { private static final Class craftItemStack = Reflection.getClass("{obc}.inventory.CraftItemStack"); private static final Reflection.MethodInvoker asNMSCopy = Reflection.getTypedMethod(REntity.craftItemStack, "asNMSCopy", REntity.itemStack, ItemStack.class); private Object getEquipmentPacket(String slot, ItemStack stack){ - Object packet = ProtocolAPI.construct(equipmentPacket); + Object packet = Reflection.newInstance(equipmentPacket); equipmentEntity.set(packet, entityId); ProtocolWrapper.impl.setEquipmentPacketStack(packet, slot, asNMSCopy.invoke(null, stack)); return packet; @@ -332,7 +332,7 @@ public class REntity { private static final Reflection.FieldAccessor namedSpawnEntity = Reflection.getField(namedSpawnPacket, int.class, 0); private static final Reflection.FieldAccessor namedSpawnUUID = Reflection.getField(namedSpawnPacket, UUID.class, 0); private Object getNamedSpawnPacket(){ - Object packet = ProtocolAPI.construct(namedSpawnPacket); + Object packet = Reflection.newInstance(namedSpawnPacket); namedSpawnEntity.set(packet, entityId); namedSpawnUUID.set(packet, uuid); FlatteningWrapper.impl.setNamedSpawnPacketDataWatcher(packet); diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/ProtocolAPI.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/ProtocolAPI.java index 31a6788..a66f932 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/ProtocolAPI.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/ProtocolAPI.java @@ -19,8 +19,8 @@ package de.steamwar.fightsystem.utils; +import com.comphenix.tinyprotocol.Reflection; import com.comphenix.tinyprotocol.TinyProtocol; -import jdk.internal.misc.Unsafe; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -88,19 +88,11 @@ public class ProtocolAPI { }; } - public static T construct(Class clazz) { - try { - return (T) Unsafe.getUnsafe().allocateInstance(clazz); - } catch (InstantiationException e) { - throw new IllegalStateException(e); - } - } - public static UnaryOperator shallowCloneGenerator(Class clazz) { BiConsumer filler = shallowFill(clazz); return source -> { - Object clone = construct(clazz); + Object clone = Reflection.newInstance(clazz); filler.accept(source, clone); return clone; };