Normalize block entity IDs and handle bundled packets

- Normalize hidden block entity identifiers across versions
- Add block break and tile entity handling for newer protocol versions
- Preserve packet filtering through bundle packets and fix chunk section copying
This commit is contained in:
2026-05-11 23:40:36 +02:00
parent 1ea8dea381
commit 5305aaf669
11 changed files with 291 additions and 22 deletions
@@ -25,16 +25,20 @@ import io.netty.buffer.Unpooled;
import net.minecraft.network.protocol.game.ClientboundLevelChunkPacketData;
import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket;
import net.minecraft.util.SimpleBitStorage;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.Set;
import java.util.function.IntUnaryOperator;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
public class ChunkHider21 implements ChunkHider {
private static final int DIRECT_BLOCK_BITS = Math.max(9, Integer.SIZE - Integer.numberOfLeadingZeros(Block.BLOCK_STATE_REGISTRY.size() - 1));
@Override
public Class<?> mapChunkPacket() {
return ClientboundLevelChunkWithLightPacket.class;
@@ -96,33 +100,70 @@ public class ChunkHider21 implements ChunkHider {
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) {
return !hiddenBlockEntities.contains(getName.invoke(getKey.invoke(nameField.get(null), entityType.get(tile))));
return !hiddenBlockEntities.contains(TechHider.normalizeBlockEntityId((String) getName.invoke(getKey.invoke(nameField.get(null), entityType.get(tile)))));
}
private void blocks(SectionHider section) {
section.copyBitsPerBlock();
int sourceBitsPerBlock = section.readBitsPerBlock();
boolean singleValued = section.getBitsPerBlock() == 0;
if(section.isSkipSection()) {
section.writeBitsPerBlock(sourceBitsPerBlock);
copyBlockStorage(section);
return;
}
boolean singleValued = sourceBitsPerBlock == 0;
if (singleValued) {
int value = ProtocolUtils.readVarInt(section.getIn());
ProtocolUtils.writeVarInt(section.getOut(), !section.isSkipSection() && section.getObfuscate().contains(value) ? section.getTarget() : value);
if(section.blockPrecise()) {
writeDirectBlocks(section, DIRECT_BLOCK_BITS, ignored -> value);
} else {
section.writeBitsPerBlock(sourceBitsPerBlock);
ProtocolUtils.writeVarInt(section.getOut(), section.getObfuscate().contains(value) ? section.getTarget() : value);
}
return;
} else if (section.getBitsPerBlock() < 9) {
} else if (sourceBitsPerBlock < 9) {
if(section.blockPrecise()) {
int[] palette = section.readPalette();
SimpleBitStorage values = new SimpleBitStorage(sourceBitsPerBlock, 4096, section.readNewDataArray(4096));
writeDirectBlocks(section, DIRECT_BLOCK_BITS, pos -> palette[values.get(pos)]);
return;
}
section.writeBitsPerBlock(sourceBitsPerBlock);
// Indirect (paletted) storage only present when bitsPerBlock < 9 in 1.21+
section.processPalette();
}
if (section.isSkipSection() || (!section.blockPrecise() && section.isPaletted())) {
if (!section.blockPrecise() && section.isPaletted()) {
section.skipNewDataArray(4096);
return;
}
SimpleBitStorage values = new SimpleBitStorage(section.getBitsPerBlock(), 4096, section.readNewDataArray(4096));
SimpleBitStorage values = new SimpleBitStorage(sourceBitsPerBlock, 4096, section.readNewDataArray(4096));
writeDirectBlocks(section, sourceBitsPerBlock, values::get);
}
private void copyBlockStorage(SectionHider section) {
if(section.getBitsPerBlock() == 0) {
section.copyVarInt();
} else {
if(section.getBitsPerBlock() < 9)
section.skipPalette();
section.skipNewDataArray(4096);
}
}
private void writeDirectBlocks(SectionHider section, int bitsPerBlock, IntUnaryOperator source) {
section.writeBitsPerBlock(bitsPerBlock);
SimpleBitStorage values = new SimpleBitStorage(bitsPerBlock, 4096);
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
int pos = (((y * 16) + z) * 16) + x;
int value = source.applyAsInt(pos);
TechHider.State test = section.test(x, y, z);
@@ -130,15 +171,17 @@ public class ChunkHider21 implements ChunkHider {
case SKIP:
break;
case CHECK:
if (!section.getObfuscate().contains(values.get(pos)))
if (!section.getObfuscate().contains(value))
break;
case HIDE:
values.set(pos, section.getTarget());
value = section.getTarget();
break;
case HIDE_AIR:
default:
values.set(pos, section.getAir());
value = section.getAir();
}
values.set(pos, value);
}
}
}
@@ -0,0 +1,104 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.techhider;
import de.steamwar.Reflection;
import net.minecraft.core.BlockPos;
import net.minecraft.core.SectionPos;
import net.minecraft.network.protocol.game.ClientboundBlockDestructionPacket;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.function.BiFunction;
public class ProtocolWrapper21 implements ProtocolWrapper {
private static final Reflection.Field<SectionPos> multiBlockChangeChunk = Reflection.getField(TechHider.multiBlockChangePacket, SectionPos.class, 0);
private static final Reflection.Field<short[]> multiBlockChangePos = Reflection.getField(TechHider.multiBlockChangePacket, short[].class, 0);
private static final Reflection.Field<BlockState[]> multiBlockChangeBlocks = Reflection.getField(TechHider.multiBlockChangePacket, BlockState[].class, 0);
@Override
public BiFunction<Player, Object, Object> multiBlockChangeGenerator(TechHider techHider) {
return (p, packet) -> {
TechHider.LocationEvaluator locationEvaluator = techHider.getLocationEvaluator();
SectionPos sectionPos = multiBlockChangeChunk.get(packet);
int chunkX = sectionPos.x();
int chunkY = sectionPos.y();
int chunkZ = sectionPos.z();
if(locationEvaluator.skipChunkSection(p, chunkX, chunkY, chunkZ))
return packet;
packet = TechHider.multiBlockChangeCloner.apply(packet);
final short[] oldPos = multiBlockChangePos.get(packet);
final BlockState[] oldBlocks = multiBlockChangeBlocks.get(packet);
ArrayList<Short> poss = new ArrayList<>(oldPos.length);
ArrayList<BlockState> blocks = new ArrayList<>(oldPos.length);
for(int i = 0; i < oldPos.length; i++) {
short pos = oldPos[i];
BlockState block = oldBlocks[i];
switch(locationEvaluator.check(p, 16*chunkX + SectionPos.sectionRelativeX(pos), 16*chunkY + SectionPos.sectionRelativeY(pos), 16*chunkZ + SectionPos.sectionRelativeZ(pos))) {
case SKIP:
poss.add(pos);
blocks.add(block);
break;
case CHECK:
poss.add(pos);
blocks.add(techHider.iBlockDataHidden(block) ? (BlockState) techHider.getObfuscationTarget() : block);
break;
default:
break;
}
}
if(blocks.isEmpty())
return null;
short[] newPos = new short[poss.size()];
for(int i = 0; i < newPos.length; i++)
newPos[i] = poss.get(i);
multiBlockChangePos.set(packet, newPos);
multiBlockChangeBlocks.set(packet, blocks.toArray(new BlockState[0]));
return packet;
};
}
private static final Reflection.Field<BlockEntityType> tileEntityType = Reflection.getField(TechHider.tileEntityDataPacket, BlockEntityType.class, 0);
@Override
public boolean unfilteredTileEntityDataAction(Object packet) {
BlockEntityType type = tileEntityType.get(packet);
return type != BlockEntityType.SIGN && type != BlockEntityType.HANGING_SIGN;
}
@Override
public String tileEntityDataType(Object packet) {
return BlockEntityType.getKey(tileEntityType.get(packet)).getPath();
}
private static final Reflection.Field<BlockPos> blockBreakPosition = Reflection.getField(ClientboundBlockDestructionPacket.class, BlockPos.class, 0);
@Override
public BiFunction<Player, Object, Object> blockBreakHiderGenerator(Class<?> blockBreakPacket, TechHider techHider) {
return (p, packet) -> techHider.getLocationEvaluator().checkBlockPos(p, blockBreakPosition.get(packet)) == TechHider.State.SKIP ? packet : null;
}
}