Skip to content

Commit

Permalink
[#325] Add support for 1.20.2 (#326)
Browse files Browse the repository at this point in the history
* fix: chunk unload listener for 1.20.2

* fix: allow non occluding blocks for block below if possible
  • Loading branch information
Ingrim4 authored Oct 6, 2023
1 parent ddc9bc9 commit e5b7c4b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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() {
Expand All @@ -38,6 +40,10 @@ public static int minorVersion() {
return MINOR_VERSION;
}

public static int revisionNumber() {
return REVISION_NUMBER;
}

private MinecraftVersion() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -27,6 +30,10 @@ public final class ChunkCapabilities {
private ChunkCapabilities() {
}

public static boolean hasChunkPosFieldUnloadPacket() {
return hasChunkPosFieldUnloadPacket;
}

public static boolean hasClientboundLevelChunkPacketData() {
return hasClientboundLevelChunkPacketData;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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()));

Expand All @@ -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) {
Expand All @@ -70,9 +86,9 @@ public void onPacketSending(PacketEvent event) {

this.preChunkProcessing(event);

AdvancedConfig advancedConfig = this.config.advanced();

CompletableFuture<ObfuscationResult> future = this.obfuscationSystem.obfuscate(struct);

AdvancedConfig advancedConfig = this.config.advanced();
if (advancedConfig.hasObfuscationTimeout()) {
future = future.orTimeout(advancedConfig.obfuscationTimeout(), TimeUnit.MILLISECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,8 +56,14 @@ public void onPacketSending(PacketEvent event) {

OrebfuscatorPlayer orebfuscatorPlayer = this.playerMap.get(player);
if (orebfuscatorPlayer != null) {
StructureModifier<Integer> 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<Integer> ints = packet.getIntegers();
orebfuscatorPlayer.removeChunk(ints.read(0), ints.read(1));
}
}
}
}

0 comments on commit e5b7c4b

Please sign in to comment.