From e5b7c4b9674a54e00703b338cb49cb0cbcc01920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurice=20Eisenbl=C3=A4tter?= Date: Fri, 6 Oct 2023 23:10:18 +0200 Subject: [PATCH] [#325] Add support for 1.20.2 (#326) * fix: chunk unload listener for 1.20.2 * fix: allow non occluding blocks for block below if possible --- .../orebfuscator/util/MinecraftVersion.java | 6 +++++ .../orebfuscator/chunk/ChunkCapabilities.java | 9 +++++++- .../obfuscation/ObfuscationListener.java | 22 ++++++++++++++++--- .../obfuscation/ObfuscationProcessor.java | 12 +++++----- .../proximity/ProximityPacketListener.java | 13 +++++++++-- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/MinecraftVersion.java b/orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/MinecraftVersion.java index c900f831..17a1e5ab 100644 --- a/orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/MinecraftVersion.java +++ b/orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/MinecraftVersion.java @@ -12,6 +12,7 @@ public final class MinecraftVersion { private static final String NMS_VERSION; private static final int MAJOR_VERSION; private static final int MINOR_VERSION; + private static final int REVISION_NUMBER; static { String craftBukkitPackage = Bukkit.getServer().getClass().getPackage().getName(); @@ -24,6 +25,7 @@ public final class MinecraftVersion { NMS_VERSION = matcher.group(1); MAJOR_VERSION = Integer.parseInt(matcher.group(2)); MINOR_VERSION = Integer.parseInt(matcher.group(3)); + REVISION_NUMBER = Integer.parseInt(matcher.group(4)); } public static String nmsVersion() { @@ -38,6 +40,10 @@ public static int minorVersion() { return MINOR_VERSION; } + public static int revisionNumber() { + return REVISION_NUMBER; + } + private MinecraftVersion() { } } diff --git a/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/chunk/ChunkCapabilities.java b/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/chunk/ChunkCapabilities.java index 51b1fd15..6bc20a9d 100644 --- a/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/chunk/ChunkCapabilities.java +++ b/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/chunk/ChunkCapabilities.java @@ -4,7 +4,8 @@ public final class ChunkCapabilities { - // hasClientboundLevelChunkPacketData >= 18; + // hasChunkPosFieldUnloadPacket >= 1.20.2 + // hasClientboundLevelChunkPacketData >= 1.18; // hasBiomePalettedContainer >= 1.18 // hasSingleValuePalette >= 1.18 // hasHeightBitMask <= 1.17 @@ -14,6 +15,8 @@ public final class ChunkCapabilities { // hasDirectPaletteZeroLength < 1.13 // hasLight < 1.14 + private static final boolean hasChunkPosFieldUnloadPacket = MinecraftVersion.minorVersion() > 20 || + (MinecraftVersion.minorVersion() == 20 && MinecraftVersion.revisionNumber() >= 2); private static final boolean hasClientboundLevelChunkPacketData = MinecraftVersion.minorVersion() >= 18; private static final boolean hasBiomePalettedContainer = MinecraftVersion.minorVersion() >= 18; private static final boolean hasSingleValuePalette = MinecraftVersion.minorVersion() >= 18; @@ -27,6 +30,10 @@ public final class ChunkCapabilities { private ChunkCapabilities() { } + public static boolean hasChunkPosFieldUnloadPacket() { + return hasChunkPosFieldUnloadPacket; + } + public static boolean hasClientboundLevelChunkPacketData() { return hasClientboundLevelChunkPacketData; } diff --git a/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/obfuscation/ObfuscationListener.java b/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/obfuscation/ObfuscationListener.java index 4a88b3f3..7598d00e 100644 --- a/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/obfuscation/ObfuscationListener.java +++ b/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/obfuscation/ObfuscationListener.java @@ -1,6 +1,7 @@ package net.imprex.orebfuscator.obfuscation; import java.util.List; +import java.util.Objects; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @@ -10,6 +11,7 @@ import org.bukkit.entity.Player; import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.PacketTypeEnum; import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketEvent; @@ -29,15 +31,24 @@ public abstract class ObfuscationListener extends PacketAdapter { PacketType.Play.Server.MAP_CHUNK, PacketType.Play.Server.UNLOAD_CHUNK, PacketType.Play.Server.LIGHT_UPDATE, - PacketType.Play.Server.TILE_ENTITY_DATA + PacketType.Play.Server.TILE_ENTITY_DATA, + tryGetPacketType(PacketType.Play.Client.getInstance(), "CHUNK_BATCH_RECEIVED") ); + private static PacketType tryGetPacketType(PacketTypeEnum packetTypeEnum, String name) { + return packetTypeEnum.values().stream() + .filter(packetType -> packetType.name().equals(name)) + .findAny() + .orElse(null); + } + private final OrebfuscatorConfig config; private final OrebfuscatorPlayerMap playerMap; private final ObfuscationSystem obfuscationSystem; public ObfuscationListener(Orebfuscator orebfuscator) { super(orebfuscator, PACKET_TYPES.stream() + .filter(Objects::nonNull) .filter(PacketType::isSupported) .collect(Collectors.toList())); @@ -52,6 +63,11 @@ public ObfuscationListener(Orebfuscator orebfuscator) { public abstract void unregister(); + @Override + public void onPacketReceiving(PacketEvent event) { + event.getPacket().getFloat().write(0, 10f); + } + @Override public void onPacketSending(PacketEvent event) { if (event.getPacket().getType() != PacketType.Play.Server.MAP_CHUNK) { @@ -70,9 +86,9 @@ public void onPacketSending(PacketEvent event) { this.preChunkProcessing(event); - AdvancedConfig advancedConfig = this.config.advanced(); - CompletableFuture future = this.obfuscationSystem.obfuscate(struct); + + AdvancedConfig advancedConfig = this.config.advanced(); if (advancedConfig.hasObfuscationTimeout()) { future = future.orTimeout(advancedConfig.obfuscationTimeout(), TimeUnit.MILLISECONDS); } diff --git a/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/obfuscation/ObfuscationProcessor.java b/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/obfuscation/ObfuscationProcessor.java index be69f043..4aa41520 100644 --- a/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/obfuscation/ObfuscationProcessor.java +++ b/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/obfuscation/ObfuscationProcessor.java @@ -14,6 +14,7 @@ import net.imprex.orebfuscator.config.ObfuscationConfig; import net.imprex.orebfuscator.config.OrebfuscatorConfig; import net.imprex.orebfuscator.config.ProximityConfig; +import net.imprex.orebfuscator.config.ProximityHeightCondition; import net.imprex.orebfuscator.config.WorldConfigBundle; import net.imprex.orebfuscator.util.BlockPos; import net.imprex.orebfuscator.util.HeightAccessor; @@ -68,11 +69,11 @@ public void process(ObfuscationTask task) { int x = baseX + (index & 15); int z = baseZ + (index >> 4 & 15); + boolean isObfuscateBitSet = BlockFlags.isObfuscateBitSet(obfuscateBits); boolean obfuscated = false; // should current block be obfuscated - if (BlockFlags.isObfuscateBitSet(obfuscateBits) && shouldObfuscate(task, chunk, x, y, z) - && obfuscationConfig.shouldObfuscate(y)) { + if (isObfuscateBitSet && obfuscationConfig.shouldObfuscate(y) && shouldObfuscate(task, chunk, x, y, z)) { blockState = bundle.nextRandomObfuscationBlock(y); obfuscated = true; } @@ -81,7 +82,8 @@ public void process(ObfuscationTask task) { if (!obfuscated && BlockFlags.isProximityBitSet(obfuscateBits) && proximityConfig.shouldObfuscate(y)) { proximityBlocks.add(new BlockPos(x, y, z)); if (BlockFlags.isUseBlockBelowBitSet(obfuscateBits)) { - blockState = getBlockStateBelow(bundle, chunk, x, y, z); + boolean allowNonOcclude = !isObfuscateBitSet || !ProximityHeightCondition.isPresent(obfuscateBits); + blockState = getBlockStateBelow(bundle, chunk, x, y, z, allowNonOcclude); } else { blockState = bundle.nextRandomProximityBlock(y); } @@ -106,12 +108,12 @@ public void process(ObfuscationTask task) { // returns first block below given position that wouldn't be obfuscated in any // way at given position - private int getBlockStateBelow(WorldConfigBundle bundle, Chunk chunk, int x, int y, int z) { + private int getBlockStateBelow(WorldConfigBundle bundle, Chunk chunk, int x, int y, int z, boolean allowNonOcclude) { BlockFlags blockFlags = bundle.blockFlags(); for (int targetY = y - 1; targetY > chunk.getHeightAccessor().getMinBuildHeight(); targetY--) { int blockData = chunk.getBlockState(x, targetY, z); - if (blockData != -1 && OrebfuscatorNms.isOccluding(blockData)) { + if (blockData != -1 && (allowNonOcclude || OrebfuscatorNms.isOccluding(blockData))) { int mask = blockFlags.flags(blockData, y); if (BlockFlags.isEmpty(mask) || BlockFlags.isAllowForUseBlockBelowBitSet(mask)) { return blockData; diff --git a/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/proximity/ProximityPacketListener.java b/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/proximity/ProximityPacketListener.java index f9d7010c..d19e18b4 100644 --- a/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/proximity/ProximityPacketListener.java +++ b/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/proximity/ProximityPacketListener.java @@ -7,10 +7,13 @@ import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.reflect.StructureModifier; +import com.comphenix.protocol.wrappers.ChunkCoordIntPair; import net.imprex.orebfuscator.Orebfuscator; +import net.imprex.orebfuscator.chunk.ChunkCapabilities; import net.imprex.orebfuscator.config.OrebfuscatorConfig; import net.imprex.orebfuscator.config.ProximityConfig; import net.imprex.orebfuscator.player.OrebfuscatorPlayer; @@ -53,8 +56,14 @@ public void onPacketSending(PacketEvent event) { OrebfuscatorPlayer orebfuscatorPlayer = this.playerMap.get(player); if (orebfuscatorPlayer != null) { - StructureModifier ints = event.getPacket().getIntegers(); - orebfuscatorPlayer.removeChunk(ints.read(0), ints.read(1)); + PacketContainer packet = event.getPacket(); + if (ChunkCapabilities.hasChunkPosFieldUnloadPacket()) { + ChunkCoordIntPair chunkPos = packet.getChunkCoordIntPairs().read(0); + orebfuscatorPlayer.removeChunk(chunkPos.getChunkX(), chunkPos.getChunkZ()); + } else { + StructureModifier ints = packet.getIntegers(); + orebfuscatorPlayer.removeChunk(ints.read(0), ints.read(1)); + } } } }