1 Commits
sw-ui ... 1.21

Author SHA1 Message Date
5a46f10f8b First fix REntity for 1.21
All checks were successful
SteamWarCI Build successful
2025-04-16 10:10:32 +02:00
3 changed files with 40 additions and 3 deletions

View File

@ -33,7 +33,7 @@ public class BountifulWrapper21 extends BountifulWrapper9 {
return (packet, x, y, z, pitch, yaw) -> {
PositionMoveRotation pos = field.get(packet);
field.set(packet, new PositionMoveRotation(new Vec3D(x, y, z), pos.b(), yaw, pitch));
field.set(packet, new PositionMoveRotation(new Vec3D(x, y, z), pos == null ? new Vec3D(0, 0, 0) : pos.b(), yaw, pitch));
};
} catch (IllegalArgumentException e) {
return super.getPositionSetter(packetClass, fieldOffset);

View File

@ -177,9 +177,30 @@ public final class Reflection {
}
public void set(Object target, Object value) {
// This now works for Fields in records!
try {
f.set(target, value);
} catch (IllegalAccessException e) {
long offset = Unsafe.getUnsafe().objectFieldOffset(f);
Class<?> type = f.getType();
if (type == int.class) {
Unsafe.getUnsafe().putInt(target, offset, (Integer) value);
} else if (type == float.class) {
Unsafe.getUnsafe().putFloat(target, offset, (Float) value);
} else if (type == double.class) {
Unsafe.getUnsafe().putDouble(target, offset, (Double) value);
} else if (type == boolean.class) {
Unsafe.getUnsafe().putBoolean(target, offset, (Boolean) value);
} else if (type == byte.class) {
Unsafe.getUnsafe().putByte(target, offset, (Byte) value);
} else if (type == char.class) {
Unsafe.getUnsafe().putChar(target, offset, (Character) value);
} else if (type == short.class) {
Unsafe.getUnsafe().putShort(target, offset, (Short) value);
} else if (type == long.class) {
Unsafe.getUnsafe().putLong(target, offset, (Long) value);
} else {
Unsafe.getUnsafe().putReference(target, offset, value);
}
} catch (Exception e) {
throw new IllegalArgumentException("Cannot write field", e);
}
}

View File

@ -396,10 +396,26 @@ public class REntity {
public static final Class<?> teleportPacket = Reflection.getClass("net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket");
public static final Reflection.Field<Integer> teleportEntity = Reflection.getField(teleportPacket, int.class, 0);
public static final BountifulWrapper.PositionSetter teleportPosition = BountifulWrapper.impl.getPositionSetter(teleportPacket, Core.getVersion() == 8 ? 1 : 0);
public static final Class<?> relative;
public static final Reflection.Field<Set> relatives;
static {
if (Core.getVersion() >= 21) {
relative = Reflection.getClass("net.minecraft.world.entity.Relative");
relatives = Reflection.getField(teleportPacket, Set.class, 0);
} else {
relative = null;
relatives = null;
}
}
private Object getTeleportPacket(){
Object packet = Reflection.newInstance(teleportPacket);
teleportEntity.set(packet, entityId);
teleportPosition.set(packet, x, y, z, pitch, yaw);
if (Core.getVersion() >= 21) {
// https://mappings.dev/1.21.3/net/minecraft/world/entity/Relative.html
Object[] constants = relative.getEnumConstants();
relatives.set(packet, new HashSet<>(Arrays.asList(constants[0], constants[1], constants[2], constants[3], constants[4], constants[5])));
}
return packet;
}