Skip to content

Commit

Permalink
[#119] Obfuscation height limit (#167)
Browse files Browse the repository at this point in the history
* feat: add minY/maxY to world configs

* feat: add minY/maxY to proximity blocks

* chore: convert default configs to new format
  • Loading branch information
Ingrim4 authored Dec 31, 2021
1 parent 65d3ef3 commit 2994a31
Show file tree
Hide file tree
Showing 33 changed files with 2,594 additions and 2,369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface AdvancedConfig {

int maxMillisecondPerTick();
int maxMillisecondsPerTick();

int protocolLibThreads();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
public interface BlockFlags {

public static final int FLAG_OBFUSCATE = 1;
public static final int FLAG_TILE_ENTITY = 2;
public static final int FLAG_BLOCK_ENTITY = 2;
public static final int FLAG_PROXIMITY = 4;
public static final int FLAG_USE_BLOCK_BELOW = 8;

public static boolean isEmpty(int mask) {
return (mask & 0xFFF) == 0;
return (mask & 0xFF) == 0;
}

public static boolean isBitSet(int mask, int flag) {
Expand All @@ -19,8 +19,8 @@ public static boolean isObfuscateBitSet(int mask) {
return isBitSet(mask, FLAG_OBFUSCATE);
}

public static boolean isTileEntityBitSet(int mask) {
return isBitSet(mask, FLAG_TILE_ENTITY);
public static boolean isBlockEntityBitSet(int mask) {
return isBitSet(mask, FLAG_BLOCK_ENTITY);
}

public static boolean isProximityBitSet(int mask) {
Expand All @@ -31,7 +31,7 @@ public static boolean isUseBlockBelowBitSet(int mask) {
return isBitSet(mask, FLAG_USE_BLOCK_BELOW);
}

int flags(int blockId);
int flags(int blockState);

int flags(int blockId, int y);
int flags(int blockState, int y);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface Config {

CacheConfig cache();

WorldConfigBundle bundle(World world);

BlockFlags blockFlags(World world);

boolean needsObfuscation(World world);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ public interface WorldConfig {

boolean isEnabled();

int getMinY();

int getMaxY();

boolean matchesWorldName(String worldName);

int nextRandomBlockId();
boolean shouldObfuscate(int y);

int nextRandomBlockState();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.imprex.orebfuscator.config;

public interface WorldConfigBundle {

BlockFlags blockFlags();

ObfuscationConfig obfuscation();

ProximityConfig proximity();

int minSectionIndex();

int maxSectionIndex();

boolean shouldObfuscate(int y);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@

public class BlockPos {

private static final int bitsPerX = 26;
private static final int bitsPerZ = bitsPerX;
private static final int bitsPerY = 64 - bitsPerX - bitsPerZ;
// from net.minecraft.core.BlockPos
private static final int BITS_PER_X = 26;
private static final int BITS_PER_Z = BITS_PER_X;
private static final int BITS_PER_Y = 64 - BITS_PER_X - BITS_PER_Z;

private static final int offsetY = 0;
private static final int offsetZ = offsetY + bitsPerY;
private static final int offsetX = offsetZ + bitsPerZ;
private static final int OFFSET_Y = 0;
private static final int OFFSET_Z = OFFSET_Y + BITS_PER_Y;
private static final int OFFSET_X = OFFSET_Z + BITS_PER_Z;

private static final long maskX = (1L << bitsPerX) - 1L;
private static final long maskY = (1L << bitsPerY) - 1L;
private static final long maskZ = (1L << bitsPerZ) - 1L;
private static final long MASK_X = (1L << BITS_PER_X) - 1L;
private static final long MASK_Y = (1L << BITS_PER_Y) - 1L;
private static final long MASK_Z = (1L << BITS_PER_Z) - 1L;

// from net.minecraft.world.level.dimension.DimensionType
public static final int Y_SIZE = (1 << BITS_PER_Y) - 32;
public static final int MAX_Y = (Y_SIZE >> 1) - 1;
public static final int MIN_Y = MAX_Y - Y_SIZE + 1;

public final int x;
public final int y;
Expand All @@ -35,13 +41,13 @@ public ChunkPosition toChunkPosition(World world) {
}

public long toLong() {
return (this.x & maskX) << offsetX | (this.y & maskY) << offsetY | (this.z & maskZ) << offsetZ;
return (this.x & MASK_X) << OFFSET_X | (this.y & MASK_Y) << OFFSET_Y | (this.z & MASK_Z) << OFFSET_Z;
}

public static BlockPos fromLong(long value) {
int x = (int) (value << (64 - bitsPerX - offsetX) >> (64 - bitsPerX));
int y = (int) (value << (64 - bitsPerY - offsetY) >> (64 - bitsPerY));
int z = (int) (value << (64 - bitsPerZ - offsetZ) >> (64 - bitsPerZ));
int x = (int) (value << (64 - BITS_PER_X - OFFSET_X) >> (64 - BITS_PER_X));
int y = (int) (value << (64 - BITS_PER_Y - OFFSET_Y) >> (64 - BITS_PER_Y));
int z = (int) (value << (64 - BITS_PER_Z - OFFSET_Z) >> (64 - BITS_PER_Z));
return new BlockPos(x, y, z);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public final boolean isOccluding(int blockId) {
}

@Override
public final boolean isTileEntity(int blockId) {
public final boolean isBlockEntity(int blockId) {
return (this.blockFlags[blockId] & FLAG_TILE_ENTITY) != 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface NmsManager {

boolean isOccluding(int blockId);

boolean isTileEntity(int blockId);
boolean isBlockEntity(int blockId);

ReadOnlyChunk getReadOnlyChunk(World world, int chunkX, int chunkZ);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@

import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;

import org.bstats.bukkit.Metrics;
import org.bstats.charts.DrilldownPie;
import org.bstats.charts.SimplePie;
import org.bukkit.Bukkit;

import net.imprex.orebfuscator.config.OrebfuscatorConfig;
import net.imprex.orebfuscator.util.MathUtil;

public class MetricsSystem {

private static final NavigableMap<Integer, String> PLAYER_COUNT_GROUPS = new TreeMap<>();

static {
PLAYER_COUNT_GROUPS.put(8, "0-8");
PLAYER_COUNT_GROUPS.put(16, "9-16");
PLAYER_COUNT_GROUPS.put(32, "17-32");
PLAYER_COUNT_GROUPS.put(64, "33-64");
PLAYER_COUNT_GROUPS.put(128, "65-128");
PLAYER_COUNT_GROUPS.put(Integer.MAX_VALUE, ">129");
}

private final Metrics metrics;

public MetricsSystem(Orebfuscator orebfuscator) {
this.metrics = new Metrics(orebfuscator, 8942);
this.addMemoryChart();
this.addFastGazeChart(orebfuscator.getOrebfuscatorConfig());
this.addPlayerCountChart();
this.addConfigCharts(orebfuscator.getOrebfuscatorConfig());
this.addUsageCharts(orebfuscator.getOrebfuscatorConfig());
}

public void addMemoryChart() {
Expand All @@ -29,16 +45,47 @@ public void addMemoryChart() {
if (memory == Long.MAX_VALUE) {
result.put("unlimited", exact);
} else {
int gibiByte = (int) (memory / 1073741824L);
float gibiByte = Math.round(memory / 1073741824f * 100f) / 100f;
exact.put(gibiByte + "GiB", 1);
result.put(MathUtil.ceilToPowerOfTwo(gibiByte) + "GiB", exact);
result.put(MathUtil.ceilToPowerOfTwo((int) gibiByte) + "GiB", exact);
}

return result;
}));
}

public void addFastGazeChart(OrebfuscatorConfig config) {
public void addPlayerCountChart() {
this.metrics.addCustomChart(new SimplePie("player_count", () -> {
int playerCount = Bukkit.getOnlinePlayers().size();
return PLAYER_COUNT_GROUPS.ceilingEntry(playerCount).getValue();
}));
}

public void addConfigCharts(OrebfuscatorConfig config) {
this.metrics.addCustomChart(new SimplePie("max_mspt", () -> {
return Integer.toString(config.advanced().maxMillisecondsPerTick());
}));
this.metrics.addCustomChart(new SimplePie("update_radius", () -> {
return Integer.toString(config.general().updateRadius());
}));
}

public void addUsageCharts(OrebfuscatorConfig config) {
this.metrics.addCustomChart(new SimplePie("check_for_updates", () -> {
return Boolean.toString(config.general().checkForUpdates());
}));
this.metrics.addCustomChart(new SimplePie("ignore_spectator", () -> {
return Boolean.toString(config.general().ignoreSpectator());
}));
this.metrics.addCustomChart(new SimplePie("cache", () -> {
return Boolean.toString(config.cache().enabled());
}));
this.metrics.addCustomChart(new SimplePie("proximity", () -> {
return Boolean.toString(config.proximityEnabled());
}));
this.metrics.addCustomChart(new SimplePie("block_specific_config", () -> {
return Boolean.toString(config.usesBlockSpecificConfigs());
}));
this.metrics.addCustomChart(new SimplePie("fast_gaze", () -> {
return Boolean.toString(config.usesFastGaze());
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public static boolean isOccluding(int blockId) {
return instance.isOccluding(blockId);
}

public static boolean isTileEntity(int blockId) {
return instance.isTileEntity(blockId);
public static boolean isBlockEntity(int blockId) {
return instance.isBlockEntity(blockId);
}

public static ReadOnlyChunk getReadOnlyChunk(World world, int chunkX, int chunkZ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public int getBlock(int index) {
return this.palette.valueFor(this.data.get(index));
}

public boolean isEmpty() {
return ChunkCapabilities.hasBlockCount() && this.blockCount == 0;
}

public void write(ByteBuf buffer) {
if (ChunkCapabilities.hasBlockCount()) {
buffer.writeShort(this.blockCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.configuration.ConfigurationSection;

import net.imprex.orebfuscator.NmsInstance;
import net.imprex.orebfuscator.util.BlockPos;
import net.imprex.orebfuscator.util.OFCLogger;
import net.imprex.orebfuscator.util.WeightedRandom;

Expand All @@ -19,6 +20,8 @@ public abstract class AbstractWorldConfig implements WorldConfig {
private final String name;

protected boolean enabled = false;
protected int minY = BlockPos.MIN_Y;
protected int maxY = BlockPos.MAX_Y;

protected final List<WorldMatcher> worldMatchers = new ArrayList<>();

Expand All @@ -42,6 +45,18 @@ protected final void fail(String message) {
OFCLogger.warn(message);
}

protected void deserializeBase(ConfigurationSection section) {
this.enabled = section.getBoolean("enabled", true);
this.minY = Math.max(BlockPos.MIN_Y, section.getInt("minY", BlockPos.MIN_Y));
this.maxY = Math.min(BlockPos.MAX_Y, section.getInt("maxY", BlockPos.MAX_Y));
}

protected void serializeBase(ConfigurationSection section) {
section.set("enabled", this.enabled);
section.set("minY", this.minY);
section.set("maxY", this.maxY);
}

protected void deserializeWorlds(ConfigurationSection section, String path) {
section.getStringList(path).stream().map(WorldMatcher::parseMatcher).forEach(worldMatchers::add);

Expand Down Expand Up @@ -101,6 +116,16 @@ public boolean isEnabled() {
return enabled;
}

@Override
public int getMinY() {
return this.minY;
}

@Override
public int getMaxY() {
return this.maxY;
}

@Override
public boolean matchesWorldName(String worldName) {
for (WorldMatcher matcher : this.worldMatchers) {
Expand All @@ -112,7 +137,12 @@ public boolean matchesWorldName(String worldName) {
}

@Override
public int nextRandomBlockId() {
public boolean shouldObfuscate(int y) {
return y >= this.minY && y <= this.maxY;
}

@Override
public int nextRandomBlockState() {
return this.weightedBlockIds.next();
}
}
Loading

0 comments on commit 2994a31

Please sign in to comment.