Remove more reflection

This commit is contained in:
2026-05-16 13:38:10 +02:00
parent e7454f6ce8
commit 9e4c9ce04a
17 changed files with 42 additions and 144 deletions
@@ -19,7 +19,6 @@
package de.steamwar.fightsystem.ai; package de.steamwar.fightsystem.ai;
import de.steamwar.Reflection;
import de.steamwar.fightsystem.ArenaMode; import de.steamwar.fightsystem.ArenaMode;
import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.fight.FightTeam; import de.steamwar.fightsystem.fight.FightTeam;
@@ -30,28 +29,30 @@ import org.bukkit.Material;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.function.BooleanSupplier; import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@AllArgsConstructor @AllArgsConstructor
public class AIManager { public class AIManager {
private static final List<AIManager> AIs = Arrays.asList( private static final List<AIManager> AIs = Arrays.asList(
new AIManager(DummyAI.class, Material.STONE, () -> ArenaMode.Test.contains(Config.mode)) new AIManager("DummyAI", DummyAI::new, Material.STONE, () -> ArenaMode.Test.contains(Config.mode))
); );
public static List<AIManager> availableAIs() { public static List<AIManager> availableAIs() {
return AIs.stream().filter(manager -> manager.available.getAsBoolean()).collect(Collectors.toList()); return AIs.stream().filter(manager -> manager.available.getAsBoolean()).collect(Collectors.toList());
} }
private final Class<? extends AI> aiClass; private final String name;
private final Function<FightTeam, AI> constructor;
@Getter @Getter
private final Material icon; private final Material icon;
private final BooleanSupplier available; private final BooleanSupplier available;
public String name() { public String name() {
return aiClass.getSimpleName(); return name;
} }
public void join(FightTeam team) { public void join(FightTeam team) {
Reflection.getConstructor(aiClass, FightTeam.class).invoke(team); constructor.apply(team);
} }
} }
@@ -83,9 +83,8 @@ public class Recording implements Listener {
} }
public static final Class<?> primedTnt = PrimedTnt.class; public static final Class<?> primedTnt = PrimedTnt.class;
private static final Reflection.Method getBukkitEntity = Reflection.getTypedMethod(net.minecraft.world.entity.Entity.class, "getBukkitEntity", null);
public static void iterateOverEntities(Predicate<Object> filter, Consumer<Entity> consumer) { public static void iterateOverEntities(Predicate<Object> filter, Consumer<Entity> consumer) {
CraftbukkitWrapper.impl.entityIterator().filter(filter).map(entity -> (Entity) getBukkitEntity.invoke(entity)).forEach(consumer); CraftbukkitWrapper.impl.entityIterator().filter(filter).map(net.minecraft.world.entity.Entity::getBukkitEntity).forEach(consumer);
} }
public Recording() { public Recording() {
@@ -21,12 +21,10 @@ package de.steamwar.fightsystem.utils;
import com.comphenix.tinyprotocol.TinyProtocol; import com.comphenix.tinyprotocol.TinyProtocol;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import de.steamwar.Reflection;
import de.steamwar.core.ProtocolWrapper; import de.steamwar.core.ProtocolWrapper;
import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.FightSystem;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Material; import org.bukkit.Material;
@@ -34,19 +32,11 @@ import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.block.CraftBlockState; import org.bukkit.craftbukkit.block.CraftBlockState;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.util.CraftMagicNumbers; import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class BlockIdWrapper { public class BlockIdWrapper {
public static final Class<?> worldServer = ServerLevel.class;
public static final Reflection.Method getWorldHandle = Reflection.getTypedMethod(CraftWorld.class, "getHandle", worldServer);
public static final Class<?> craftPlayer = CraftPlayer.class;
public static final Class<?> entityPlayer = ServerPlayer.class;
public static final Reflection.Method getPlayer = Reflection.getTypedMethod(craftPlayer, "getHandle", entityPlayer);
public static final BlockIdWrapper impl = new BlockIdWrapper(); public static final BlockIdWrapper impl = new BlockIdWrapper();
public Material idToMaterial(int blockState) { public Material idToMaterial(int blockState) {
@@ -19,14 +19,10 @@
package de.steamwar.fightsystem.utils; package de.steamwar.fightsystem.utils;
import de.steamwar.Reflection;
import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.Config;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.chunk.LevelChunk; import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.LevelChunkSection; import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.entity.LevelEntityGetter;
import org.bukkit.Chunk;
import org.bukkit.GameRule; import org.bukkit.GameRule;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.CraftWorld;
@@ -41,26 +37,16 @@ import java.util.stream.StreamSupport;
public class CraftbukkitWrapper { public class CraftbukkitWrapper {
public static final CraftbukkitWrapper impl = new CraftbukkitWrapper(); public static final CraftbukkitWrapper impl = new CraftbukkitWrapper();
private static final Reflection.Method getWorld = Reflection.getMethod(CraftWorld.class, "getHandle");
private static final Reflection.Method getChunk = Reflection.getTypedMethod(ServerLevel.class, null, LevelChunk.class, int.class, int.class);
private static final Reflection.Method getChunkSections = Reflection.getTypedMethod(LevelChunk.class, null, LevelChunkSection[].class);
private LevelChunkSection[] getChunkSections(World world, int x, int z) {
return (LevelChunkSection[]) getChunkSections.invoke(getChunk.invoke(getWorld.invoke(world), x, z));
}
private static final Reflection.Method getEntity = Reflection.getTypedMethod(CraftEntity.class, "getHandle", net.minecraft.world.entity.Entity.class);
protected net.minecraft.world.entity.Entity getEntity(Entity e) { protected net.minecraft.world.entity.Entity getEntity(Entity e) {
return (net.minecraft.world.entity.Entity) getEntity.invoke(e); return ((CraftEntity) e).getHandle();
} }
public float headRotation(Entity e) { public float headRotation(Entity e) {
return getEntity(e).getYHeadRot(); return getEntity(e).getYHeadRot();
} }
private static final Reflection.Method getWorldEntities = Reflection.getTypedMethod(ServerLevel.class, null, LevelEntityGetter.class); public Stream<net.minecraft.world.entity.Entity> entityIterator() {
private static final Reflection.Method getIterable = Reflection.getTypedMethod(LevelEntityGetter.class, null, Iterable.class); return StreamSupport.stream(((CraftWorld) Config.world).getHandle().getEntities().getAll().spliterator(), false);
public Stream<?> entityIterator() {
return StreamSupport.stream(((Iterable<?>) getIterable.invoke(getWorldEntities.invoke(getWorld.invoke(Config.world)))).spliterator(), false);
} }
public void setupGamerule() { public void setupGamerule() {
@@ -19,7 +19,6 @@
package de.steamwar.fightsystem.utils; package de.steamwar.fightsystem.utils;
import de.steamwar.Reflection;
import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.Config;
import it.unimi.dsi.fastutil.shorts.Short2ObjectArrayMap; import it.unimi.dsi.fastutil.shorts.Short2ObjectArrayMap;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@@ -34,12 +33,11 @@ import java.util.List;
public class HullHiderWrapper { public class HullHiderWrapper {
public static final HullHiderWrapper impl = new HullHiderWrapper(); public static final HullHiderWrapper impl = new HullHiderWrapper();
private static final Reflection.Method getState = Reflection.getTypedMethod(CraftBlockData.class, "getState", BlockState.class);
public Object generateBlockChangePacket(List<Hull.IntVector> changes) { public Object generateBlockChangePacket(List<Hull.IntVector> changes) {
Object[] blockdata = new Object[changes.size()]; Object[] blockdata = new Object[changes.size()];
for(int i = 0; i < blockdata.length; i++) { for(int i = 0; i < blockdata.length; i++) {
Hull.IntVector change = changes.get(i); Hull.IntVector change = changes.get(i);
blockdata[i] = getState.invoke(Config.world.getBlockData(change.getX(), change.getY(), change.getZ())); blockdata[i] = ((CraftBlockData) Config.world.getBlockData(change.getX(), change.getY(), change.getZ())).getState();
} }
return generateBlockChangePacket(changes, blockdata); return generateBlockChangePacket(changes, blockdata);
@@ -206,18 +206,10 @@ public final class Reflection {
return getField(target, name, fieldType, 0); return getField(target, name, fieldType, 0);
} }
public static <T> Field<T> getField(String className, String name, Class<T> fieldType) {
return getField(getClass(className), name, fieldType, 0);
}
public static <T> Field<T> getField(Class<?> target, Class<T> fieldType, int index) { public static <T> Field<T> getField(Class<?> target, Class<T> fieldType, int index) {
return getField(target, null, fieldType, index); return getField(target, null, fieldType, index);
} }
public static <T> Field<T> getField(String className, Class<T> fieldType, int index) {
return getField(getClass(className), fieldType, index);
}
public static <T> Field<T> getField(Class<?> target, Class<T> fieldType, int index, Class<?>... parameters) { public static <T> Field<T> getField(Class<?> target, Class<T> fieldType, int index, Class<?>... parameters) {
return getField(target, null, fieldType, index, parameters); return getField(target, null, fieldType, index, parameters);
} }
@@ -268,14 +260,6 @@ public final class Reflection {
} }
} }
public static Method getMethod(String className, String methodName, Class<?>... params) {
return getTypedMethod(getClass(className), methodName, null, params);
}
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
return getTypedMethod(clazz, methodName, null, params);
}
public static Method getTypedMethod(Class<?> clazz, String methodName, Class<?> returnType, Class<?>... params) { public static Method getTypedMethod(Class<?> clazz, String methodName, Class<?> returnType, Class<?>... params) {
for (final java.lang.reflect.Method method : clazz.getDeclaredMethods()) { for (final java.lang.reflect.Method method : clazz.getDeclaredMethods()) {
if ((methodName == null || method.getName().equals(methodName)) if ((methodName == null || method.getName().equals(methodName))
@@ -306,10 +290,6 @@ public final class Reflection {
} }
} }
public static Constructor getConstructor(String className, Class<?>... params) {
return getConstructor(getClass(className), params);
}
public static Constructor getConstructor(Class<?> clazz, Class<?>... params) { public static Constructor getConstructor(Class<?> clazz, Class<?>... params) {
for (final java.lang.reflect.Constructor<?> constructor : clazz.getDeclaredConstructors()) { for (final java.lang.reflect.Constructor<?> constructor : clazz.getDeclaredConstructors()) {
if (Arrays.equals(constructor.getParameterTypes(), params)) { if (Arrays.equals(constructor.getParameterTypes(), params)) {
@@ -323,12 +303,8 @@ public final class Reflection {
public static Object newInstance(Class<?> clazz) { public static Object newInstance(Class<?> clazz) {
try { try {
if (Core.getVersion() > 15) { return Unsafe.getUnsafe().allocateInstance(clazz);
return Unsafe.getUnsafe().allocateInstance(clazz); } catch (InstantiationException e) {
} else {
return clazz.newInstance();
}
} catch (InstantiationException | IllegalAccessException e) {
throw new SecurityException("Could not create object", e); throw new SecurityException("Could not create object", e);
} }
} }
@@ -47,18 +47,14 @@ public class BountifulWrapper {
player.spigot().sendMessage(type, msg); player.spigot().sendMessage(type, msg);
} }
private static final Class<?> dataWatcherObject = EntityDataAccessor.class;
private static final Class<?> dataWatcherRegistry = EntityDataSerializers.class; private static final Class<?> dataWatcherRegistry = EntityDataSerializers.class;
private static final Class<?> dataWatcherSerializer = EntityDataSerializer.class; private static final Class<?> dataWatcherSerializer = EntityDataSerializer.class;
private static final Reflection.Constructor dataWatcherObjectConstructor = Reflection.getConstructor(dataWatcherObject, int.class, dataWatcherSerializer);
public Object getDataWatcherObject(int index, Class<?> type) { public Object getDataWatcherObject(int index, Class<?> type) {
return dataWatcherObjectConstructor.invoke(index, Reflection.getField(dataWatcherRegistry, dataWatcherSerializer, 0, type).get(null)); return new EntityDataAccessor<>(index, (EntityDataSerializer<Object>) Reflection.getField(dataWatcherRegistry, dataWatcherSerializer, 0, type).get(null));
} }
private static final Class<?> item = SynchedEntityData.DataItem.class;
private static final Reflection.Constructor itemConstructor = Reflection.getConstructor(item, dataWatcherObject, Object.class);
public Object getDataWatcherItem(Object dwo, Object value) { public Object getDataWatcherItem(Object dwo, Object value) {
return itemConstructor.invoke(dwo, value); return new SynchedEntityData.DataItem<>((EntityDataAccessor<Object>) dwo, value);
} }
public BountifulWrapper.PositionSetter getPositionSetter(Class<?> packetClass, int fieldOffset) { public BountifulWrapper.PositionSetter getPositionSetter(Class<?> packetClass, int fieldOffset) {
@@ -93,7 +93,6 @@ class CheckpointUtilsJ9 {
private static final Reflection.Field<List> channelFutures = Reflection.getField(ServerConnectionListener.class, List.class, 0, ChannelFuture.class); private static final Reflection.Field<List> channelFutures = Reflection.getField(ServerConnectionListener.class, List.class, 0, ChannelFuture.class);
private static final Reflection.Method bind = Reflection.getMethod(ServerConnectionListener.class, null, InetAddress.class, int.class);
private static void freezeInternal(Path path) throws Exception { private static void freezeInternal(Path path) throws Exception {
Bukkit.getPluginManager().callEvent(new CRIUSleepEvent()); Bukkit.getPluginManager().callEvent(new CRIUSleepEvent());
@@ -137,7 +136,7 @@ class CheckpointUtilsJ9 {
} }
// Reopen socket // Reopen socket
bind.invoke(serverConnection, InetAddress.getLoopbackAddress(), port); serverConnection.startTcpServerListener(InetAddress.getLoopbackAddress(), port);
if(Core.getVersion() > 12) { if(Core.getVersion() > 12) {
for(Object future : channels) { for(Object future : channels) {
((ChannelFuture) future).channel().config().setAutoRead(true); ((ChannelFuture) future).channel().config().setAutoRead(true);
@@ -21,15 +21,15 @@ package de.steamwar.core;
import com.destroystokyo.paper.profile.PlayerProfile; import com.destroystokyo.paper.profile.PlayerProfile;
import de.steamwar.Reflection; import de.steamwar.Reflection;
import net.minecraft.core.DefaultedRegistry;
import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundSetObjectivePacket; import net.minecraft.network.protocol.game.ClientboundSetObjectivePacket;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.ProgressListener;
import net.minecraft.world.entity.Pose; import net.minecraft.world.entity.Pose;
import org.bukkit.*; import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.util.CraftNamespacedKey; import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
@@ -337,33 +337,18 @@ public class FlatteningWrapper {
return displayName != null ? Optional.of(ChatWrapper.impl.stringToChatComponent(displayName)) : Optional.empty(); return displayName != null ? Optional.of(ChatWrapper.impl.stringToChatComponent(displayName)) : Optional.empty();
} }
private static final Class<?> registryBlocks = DefaultedRegistry.class; private static final Reflection.Field<net.minecraft.world.entity.EntityType> spawnType = Reflection.getField(ProtocolWrapper.spawnPacket, net.minecraft.world.entity.EntityType.class, 0);
private static final Class<?> entityTypes = net.minecraft.world.entity.EntityType.class;
private static final Object entityTypesRegistry = Reflection.getField(BuiltInRegistries.class, registryBlocks, 0, entityTypes).get(null);
private static final Reflection.Method get = Reflection.getMethod(registryBlocks, null, ResourceLocation.class);
private static final Reflection.Field<?> spawnType = Reflection.getField(ProtocolWrapper.spawnPacket, entityTypes, 0);
private static final Reflection.Field<?> spawnLivingType = spawnType;
private static final Reflection.Method toMinecraft = Reflection.getMethod(CraftNamespacedKey.class, "toMinecraft", NamespacedKey.class);
private static final Map<EntityType, Object> types = new HashMap<>();
static {
types.put(EntityType.ARMOR_STAND, 1);
}
public void setSpawnPacketType(Object packet, EntityType type) { public void setSpawnPacketType(Object packet, EntityType type) {
if(type.isAlive()) { ResourceLocation key = CraftNamespacedKey.toMinecraft(type.getKey());
spawnLivingType.set(packet, Core.getVersion() > 18 ? get.invoke(entityTypesRegistry, toMinecraft.invoke(null, type.getKey())) : types.get(type)); spawnType.set(packet, BuiltInRegistries.ENTITY_TYPE.get(key));
} else {
spawnType.set(packet, get.invoke(entityTypesRegistry, toMinecraft.invoke(null, type.getKey())));
}
} }
public int getViewDistance(Player player) { public int getViewDistance(Player player) {
return player.getClientViewDistance(); return player.getClientViewDistance();
} }
private static final Reflection.Method getHandle = Reflection.getMethod(CraftWorld.class, "getHandle");
private static final Reflection.Method save = Reflection.getMethod(ServerLevel.class, null, ProgressListener.class, boolean.class, boolean.class);
public void syncSave(World world) { public void syncSave(World world) {
save.invoke(getHandle.invoke(world), null, true, false); ((CraftWorld) world).getHandle().save(null, true, false);
} }
public enum EntityPose { public enum EntityPose {
@@ -19,7 +19,6 @@
package de.steamwar.core; package de.steamwar.core;
import de.steamwar.Reflection;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@@ -80,11 +79,8 @@ public class TPSWatcher {
} }
} }
private static final Class<?> minecraftServer = MinecraftServer.class;
private static final Reflection.Method getServer = Reflection.getTypedMethod(minecraftServer, "getServer", minecraftServer);
private static final Reflection.Field<double[]> recentTps = Reflection.getField(minecraftServer, "recentTps", double[].class);
private static double[] getSpigotTPS() { private static double[] getSpigotTPS() {
return recentTps.get(getServer.invoke(null)); return MinecraftServer.getServer().recentTps;
} }
private static double round(double d) { private static double round(double d) {
@@ -19,9 +19,7 @@
package de.steamwar.entity; package de.steamwar.entity;
import de.steamwar.Reflection;
import de.steamwar.core.BountifulWrapper; import de.steamwar.core.BountifulWrapper;
import de.steamwar.core.Core;
import lombok.Getter; import lombok.Getter;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import org.bukkit.Location; import org.bukkit.Location;
@@ -60,12 +58,10 @@ public class RBlockDisplay extends RDisplay {
sendPacket(updatePacketSink, this::getBlock); sendPacket(updatePacketSink, this::getBlock);
} }
private static final Class<?> iBlockDataClass = BlockState.class; private static final Object blockWatcher = BountifulWrapper.impl.getDataWatcherObject(23, BlockState.class);
private static final Reflection.Method getState = Reflection.getTypedMethod(CraftBlockData.class, "getState", iBlockDataClass);
private static final Object blockWatcher = BountifulWrapper.impl.getDataWatcherObject(Core.getVersion() >= 21 ? 23 : 22, iBlockDataClass);
private void getBlock(boolean ignoreDefault, BiConsumer<Object, Object> packetSink) { private void getBlock(boolean ignoreDefault, BiConsumer<Object, Object> packetSink) {
if (ignoreDefault || !block.getAsString(true).equals(DEFAULT_BLOCK.getAsString(true))) { if (ignoreDefault || !block.getAsString(true).equals(DEFAULT_BLOCK.getAsString(true))) {
packetSink.accept(blockWatcher, getState.invoke(block)); packetSink.accept(blockWatcher, ((CraftBlockData) block).getState());
} }
} }
} }
@@ -449,13 +449,11 @@ public class REntity {
private static final Reflection.Field<Integer> equipmentEntity = Reflection.getField(ProtocolWrapper.equipmentPacket, int.class, 0); private static final Reflection.Field<Integer> equipmentEntity = Reflection.getField(ProtocolWrapper.equipmentPacket, int.class, 0);
private static final Reflection.Field<List> equipmentSlots = Reflection.getField(ProtocolWrapper.equipmentPacket, List.class, 0); private static final Reflection.Field<List> equipmentSlots = Reflection.getField(ProtocolWrapper.equipmentPacket, List.class, 0);
private static final Class<?> craftItemStack = CraftItemStack.class;
protected static final Reflection.Method asNMSCopy = Reflection.getTypedMethod(REntity.craftItemStack, "asNMSCopy", ProtocolWrapper.itemStack, ItemStack.class);
protected Object getEquipmentPacket(Object slot, ItemStack stack){ protected Object getEquipmentPacket(Object slot, ItemStack stack){
Object packet = Reflection.newInstance(ProtocolWrapper.equipmentPacket); Object packet = Reflection.newInstance(ProtocolWrapper.equipmentPacket);
equipmentEntity.set(packet, entityId); equipmentEntity.set(packet, entityId);
equipmentSlots.set(packet, new ArrayList<>()); equipmentSlots.set(packet, new ArrayList<>());
ProtocolWrapper.impl.setEquipmentPacketStack(packet, slot, asNMSCopy.invoke(null, stack)); ProtocolWrapper.impl.setEquipmentPacketStack(packet, slot, CraftItemStack.asNMSCopy(stack));
return packet; return packet;
} }
@@ -19,8 +19,8 @@
package de.steamwar.entity; package de.steamwar.entity;
import de.steamwar.Reflection;
import com.comphenix.tinyprotocol.TinyProtocol; import com.comphenix.tinyprotocol.TinyProtocol;
import de.steamwar.Reflection;
import de.steamwar.core.Core; import de.steamwar.core.Core;
import de.steamwar.core.FlatteningWrapper; import de.steamwar.core.FlatteningWrapper;
import net.minecraft.network.protocol.game.ServerboundInteractPacket; import net.minecraft.network.protocol.game.ServerboundInteractPacket;
@@ -54,13 +54,9 @@ public class REntityServer implements Listener {
private static final Reflection.Field<?> useEntityAction = Reflection.getField(useEntity, useEntityEnumAction, 0); private static final Reflection.Field<?> useEntityAction = Reflection.getField(useEntity, useEntityEnumAction, 0);
private static final Function<Object, Integer> getEntityAction; private static final Function<Object, Integer> getEntityAction;
static { static {
if(Core.getVersion() > 15) { Class<?> useEntityEnumActionType = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundInteractPacket$ActionType");
Class<?> useEntityEnumActionType = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundInteractPacket$ActionType"); Reflection.Method useEntityGetAction = Reflection.getTypedMethod(useEntityEnumAction, null, useEntityEnumActionType);
Reflection.Method useEntityGetAction = Reflection.getTypedMethod(useEntityEnumAction, null, useEntityEnumActionType); getEntityAction = value -> ((Enum<?>) useEntityGetAction.invoke(value)).ordinal();
getEntityAction = value -> ((Enum<?>) useEntityGetAction.invoke(value)).ordinal();
} else {
getEntityAction = value -> ((Enum<?>) value).ordinal();
}
} }
private final ConcurrentHashMap<Integer, REntity> entityMap = new ConcurrentHashMap<>(); private final ConcurrentHashMap<Integer, REntity> entityMap = new ConcurrentHashMap<>();
@@ -20,11 +20,11 @@
package de.steamwar.entity; package de.steamwar.entity;
import de.steamwar.core.BountifulWrapper; import de.steamwar.core.BountifulWrapper;
import de.steamwar.core.Core;
import de.steamwar.core.ProtocolWrapper; import de.steamwar.core.ProtocolWrapper;
import lombok.Getter; import lombok.Getter;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.ItemDisplay; import org.bukkit.entity.ItemDisplay;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@@ -61,14 +61,14 @@ public class RItemDisplay extends RDisplay {
sendPacket(updatePacketSink, this::getItemStack); sendPacket(updatePacketSink, this::getItemStack);
} }
private static final Object itemStackWatcher = BountifulWrapper.impl.getDataWatcherObject(Core.getVersion() >= 21 ? 23 : 22, ProtocolWrapper.itemStack); private static final Object itemStackWatcher = BountifulWrapper.impl.getDataWatcherObject(23, ProtocolWrapper.itemStack);
private void getItemStack(boolean ignoreDefault, BiConsumer<Object, Object> packetSink) { private void getItemStack(boolean ignoreDefault, BiConsumer<Object, Object> packetSink) {
if (ignoreDefault || !itemStack.equals(DEFAULT_ITEM_STACK)) { if (ignoreDefault || !itemStack.equals(DEFAULT_ITEM_STACK)) {
packetSink.accept(itemStackWatcher, asNMSCopy.invoke(null, itemStack)); packetSink.accept(itemStackWatcher, CraftItemStack.asNMSCopy(itemStack));
} }
} }
private static final Object itemDisplayTransformWatcher = BountifulWrapper.impl.getDataWatcherObject(Core.getVersion() >= 21 ? 24 : 23, Byte.class); private static final Object itemDisplayTransformWatcher = BountifulWrapper.impl.getDataWatcherObject(24, Byte.class);
public void setItemDisplayTransform(ItemDisplay.ItemDisplayTransform itemDisplayTransform) { public void setItemDisplayTransform(ItemDisplay.ItemDisplayTransform itemDisplayTransform) {
this.itemDisplayTransform = itemDisplayTransform; this.itemDisplayTransform = itemDisplayTransform;
sendPacket(updatePacketSink, this::getItemDisplayTransform); sendPacket(updatePacketSink, this::getItemDisplayTransform);
@@ -27,6 +27,7 @@ import net.minecraft.world.level.material.FlowingFluid;
import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids; import net.minecraft.world.level.material.Fluids;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@@ -67,9 +68,8 @@ public class BlockIds {
return ids; return ids;
} }
private static final Reflection.Method getBlock = Reflection.getTypedMethod(TechHider.craftMagicNumbers, "getBlock", TechHider.block, Material.class);
private Object getBlock(Material material) { private Object getBlock(Material material) {
return getBlock.invoke(null, material); return CraftMagicNumbers.getBlock(material);
} }
public int getCombinedId(Object blockData) { public int getCombinedId(Object blockData) {
@@ -23,11 +23,9 @@ import de.steamwar.Reflection;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import lombok.Getter; import lombok.Getter;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.protocol.game.ClientboundLevelChunkPacketData; import net.minecraft.network.protocol.game.ClientboundLevelChunkPacketData;
import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket; import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.SimpleBitStorage; import net.minecraft.util.SimpleBitStorage;
import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.entity.BlockEntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -94,14 +92,8 @@ public class ChunkHider {
public static final Class<?> tileEntity = Reflection.getClass("net.minecraft.network.protocol.game.ClientboundLevelChunkPacketData$BlockEntityInfo"); public static final Class<?> tileEntity = Reflection.getClass("net.minecraft.network.protocol.game.ClientboundLevelChunkPacketData$BlockEntityInfo");
protected static final Reflection.Field<BlockEntityType> entityType = Reflection.getField(tileEntity, BlockEntityType.class, 0); protected static final Reflection.Field<BlockEntityType> entityType = Reflection.getField(tileEntity, BlockEntityType.class, 0);
private static final Class<?> builtInRegestries = BuiltInRegistries.class;
private static final Class<?> registry = Registry.class;
private static final Reflection.Field<?> nameField = Reflection.getField(builtInRegestries, "BLOCK_ENTITY_TYPE", registry);
private static final Class<?> resourceLocation = ResourceLocation.class;
private static final Reflection.Method getKey = Reflection.getTypedMethod(registry, "getKey", resourceLocation, Object.class);
private static final Reflection.Method getName = Reflection.getTypedMethod(resourceLocation, "getPath", String.class);
protected boolean tileEntityVisible(Set<String> hiddenBlockEntities, Object tile) { protected boolean tileEntityVisible(Set<String> hiddenBlockEntities, Object tile) {
return !hiddenBlockEntities.contains(getName.invoke(getKey.invoke(nameField.get(null), entityType.get(tile)))); return !hiddenBlockEntities.contains(BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(entityType.get(tile)).getPath());
} }
private void blocks(SectionHider section) { private void blocks(SectionHider section) {
@@ -25,13 +25,7 @@ import de.steamwar.core.Core;
import lombok.Getter; import lombok.Getter;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Vec3i; import net.minecraft.core.Vec3i;
import net.minecraft.network.protocol.game.ClientboundBlockDestructionPacket; import net.minecraft.network.protocol.game.*;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.network.protocol.game.ClientboundBlockEventPacket;
import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundSectionBlocksUpdatePacket;
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
import net.minecraft.network.protocol.game.ServerboundUseItemOnPacket;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import org.bukkit.Material; import org.bukkit.Material;
@@ -56,16 +50,12 @@ public class TechHider {
public static final Class<?> iBlockData = BlockState.class; public static final Class<?> iBlockData = BlockState.class;
public static final Class<?> block = Block.class; public static final Class<?> block = Block.class;
private static final Reflection.Method getBlockDataByBlock = Reflection.getTypedMethod(block, null, iBlockData);
public static final Class<?> craftMagicNumbers = CraftMagicNumbers.class;
private static final Reflection.Method getBlockByMaterial = Reflection.getTypedMethod(craftMagicNumbers, "getBlock", block, Material.class);
public boolean iBlockDataHidden(Object iBlockData) { public boolean iBlockDataHidden(Object iBlockData) {
return obfuscateIds.contains(BlockIds.impl.getCombinedId(iBlockData)); return obfuscateIds.contains(BlockIds.impl.getCombinedId(iBlockData));
} }
public static final Object AIR = getBlockDataByBlock.invoke(getBlockByMaterial.invoke(null, Material.AIR)); public static final Object AIR = CraftMagicNumbers.getBlock(Material.AIR).defaultBlockState();
public static final int AIR_ID = BlockIds.impl.materialToId(Material.AIR); public static final int AIR_ID = BlockIds.impl.materialToId(Material.AIR);
private final Map<Class<?>, BiFunction<Player, Object, Object>> techhiders = new HashMap<>(); private final Map<Class<?>, BiFunction<Player, Object, Object>> techhiders = new HashMap<>();
@@ -84,7 +74,7 @@ public class TechHider {
this.locationEvaluator = locationEvaluator; this.locationEvaluator = locationEvaluator;
this.obfuscateIds = obfuscate.stream().flatMap(m -> BlockIds.impl.materialToAllIds(m).stream()).collect(Collectors.toSet()); this.obfuscateIds = obfuscate.stream().flatMap(m -> BlockIds.impl.materialToAllIds(m).stream()).collect(Collectors.toSet());
this.hiddenBlockEntities = hiddenBlockEntities; this.hiddenBlockEntities = hiddenBlockEntities;
this.obfuscationTarget = getBlockDataByBlock.invoke(getBlockByMaterial.invoke(null, obfuscationTarget)); this.obfuscationTarget = CraftMagicNumbers.getBlock(obfuscationTarget).defaultBlockState();
this.obfuscationTargetId = BlockIds.impl.materialToId(obfuscationTarget); this.obfuscationTargetId = BlockIds.impl.materialToId(obfuscationTarget);
techhiders.put(blockActionPacket, this::blockActionHider); techhiders.put(blockActionPacket, this::blockActionHider);