From 8b4bf455ad97f5e8945d8ffc23270728c028bae8 Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 16 May 2013 18:51:05 +1000 Subject: [PATCH] Orebfuscator diff --git a/src/main/java/net/minecraft/server/EntityFallingBlock.java b/src/main/java/net/minecraft/server/EntityFallingBlock.java index 22dc42d..fd78677 100644 --- a/src/main/java/net/minecraft/server/EntityFallingBlock.java +++ b/src/main/java/net/minecraft/server/EntityFallingBlock.java @@ -60,6 +60,7 @@ public class EntityFallingBlock extends Entity { blockposition = new BlockPosition(this); if (this.world.getType(blockposition).getBlock() == block && !CraftEventFactory.callEntityChangeBlockEvent(this, blockposition.getX(), blockposition.getY(), blockposition.getZ(), Blocks.AIR, 0).isCancelled()) { this.world.setAir(blockposition); + world.spigotConfig.antiXrayInstance.updateNearbyBlocks(world, blockposition); // Spigot } else if (!this.world.isStatic) { this.die(); return; @@ -84,6 +85,7 @@ public class EntityFallingBlock extends Entity { return; } this.world.setTypeAndData(blockposition, this.block, 3); + world.spigotConfig.antiXrayInstance.updateNearbyBlocks(world, blockposition); // Spigot // CraftBukkit end if (block instanceof BlockFalling) { ((BlockFalling) block).a_(this.world, blockposition); diff --git a/src/main/java/net/minecraft/server/Explosion.java b/src/main/java/net/minecraft/server/Explosion.java index 5ce4985..735a7d4 100644 --- a/src/main/java/net/minecraft/server/Explosion.java +++ b/src/main/java/net/minecraft/server/Explosion.java @@ -195,6 +195,7 @@ public class Explosion { blockposition = (BlockPosition) iterator.next(); Block block = this.world.getType(blockposition).getBlock(); + world.spigotConfig.antiXrayInstance.updateNearbyBlocks(world, blockposition); // Spigot if (flag) { double d0 = (double) ((float) blockposition.getX() + this.world.random.nextFloat()); double d1 = (double) ((float) blockposition.getY() + this.world.random.nextFloat()); diff --git a/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java b/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java index e24d3ae..e106a1f 100644 --- a/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java +++ b/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java @@ -18,6 +18,7 @@ public class PacketPlayOutMapChunk implements Packet { this.b = chunk.locZ; this.d = flag; this.c = a(chunk, flag, !chunk.getWorld().worldProvider.o(), i); + chunk.world.spigotConfig.antiXrayInstance.obfuscateSync(chunk.locX, chunk.locZ, c.b, c.a, chunk.world); } public void a(PacketDataSerializer packetdataserializer) { diff --git a/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java b/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java index 8af5e1f..9df0d53 100644 --- a/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java +++ b/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java @@ -8,6 +8,7 @@ public class PacketPlayOutMapChunkBulk implements Packet { private int[] b; private ChunkMap[] c; private boolean d; + private World world; // Spigot public PacketPlayOutMapChunkBulk() {} @@ -27,7 +28,8 @@ public class PacketPlayOutMapChunkBulk implements Packet { this.b[j] = chunk.locZ; this.c[j] = chunkmap; } - + + world = ((Chunk) list.get(0)).getWorld(); // Spigot } public void a(PacketDataSerializer packetdataserializer) { @@ -67,6 +69,7 @@ public class PacketPlayOutMapChunkBulk implements Packet { } for (i = 0; i < this.a.length; ++i) { + world.spigotConfig.antiXrayInstance.obfuscate(this.a[i], this.b[i], this.c[i].b, this.c[i].a, world); // Spigot packetdataserializer.writeBytes(this.c[i].a); } diff --git a/src/main/java/net/minecraft/server/PlayerInteractManager.java b/src/main/java/net/minecraft/server/PlayerInteractManager.java index b2431d9..7b469e1 100644 --- a/src/main/java/net/minecraft/server/PlayerInteractManager.java +++ b/src/main/java/net/minecraft/server/PlayerInteractManager.java @@ -197,6 +197,7 @@ public class PlayerInteractManager { } } + world.spigotConfig.antiXrayInstance.updateNearbyBlocks(world, blockposition); // Spigot } public void a(BlockPosition blockposition) { diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java index dad3d19..f6b1dac 100644 --- a/src/main/java/net/minecraft/server/World.java +++ b/src/main/java/net/minecraft/server/World.java @@ -460,6 +460,7 @@ public abstract class World implements IBlockAccess { this.d(blockposition.up(), block); this.d(blockposition.north(), block); this.d(blockposition.south(), block); + spigotConfig.antiXrayInstance.updateNearbyBlocks(this, blockposition); // Spigot } public void a(BlockPosition blockposition, Block block, EnumDirection enumdirection) { @@ -673,9 +674,16 @@ public abstract class World implements IBlockAccess { return this.worldProvider.p()[this.getLightLevel(blockposition)]; } - public IBlockData getType(BlockPosition blockposition) { + // Spigot start + public IBlockData getType(BlockPosition blockposition) + { + return getType( blockposition, true ); + } + + public IBlockData getType(BlockPosition blockposition, boolean useCaptured) { // CraftBukkit start - tree generation - if (captureTreeGeneration) { + if (captureTreeGeneration && useCaptured) { + // Spigot end Iterator it = capturedBlockStates.iterator(); while (it.hasNext()) { BlockState previous = it.next(); diff --git a/src/main/java/org/spigotmc/AntiXray.java b/src/main/java/org/spigotmc/AntiXray.java new file mode 100644 index 0000000..6f28cd8 --- /dev/null +++ b/src/main/java/org/spigotmc/AntiXray.java @@ -0,0 +1,234 @@ +package org.spigotmc; + +import gnu.trove.set.TByteSet; +import gnu.trove.set.hash.TByteHashSet; +import net.minecraft.server.Block; +import net.minecraft.server.BlockPosition; +import net.minecraft.server.Blocks; +import net.minecraft.server.World; +import org.bukkit.craftbukkit.util.CraftMagicNumbers; + +public class AntiXray +{ + + private static final CustomTimingsHandler update = new CustomTimingsHandler( "xray - update" ); + private static final CustomTimingsHandler obfuscate = new CustomTimingsHandler( "xray - obfuscate" ); + /*========================================================================*/ + // Used to keep track of which blocks to obfuscate + private final boolean[] obfuscateBlocks = new boolean[ Short.MAX_VALUE ]; + // Used to select a random replacement ore + private final byte[] replacementOres; + + public AntiXray(SpigotWorldConfig config) + { + // Set all listed blocks as true to be obfuscated + for ( int id : ( config.engineMode == 1 ) ? config.hiddenBlocks : config.replaceBlocks ) + { + obfuscateBlocks[id] = true; + } + + // For every block + TByteSet blocks = new TByteHashSet(); + for ( Integer i : config.hiddenBlocks ) + { + Block block = Block.getById( i ); + // Check it exists and is not a tile entity + if ( block != null && !block.isTileEntity() ) + { + // Add it to the set of replacement blocks + blocks.add( (byte) (int) i ); + } + } + // Bake it to a flat array of replacements + replacementOres = blocks.toArray(); + } + + /** + * Starts the timings handler, then updates all blocks within the set radius + * of the given coordinate, revealing them if they are hidden ores. + */ + public void updateNearbyBlocks(World world, BlockPosition position) + { + if ( world.spigotConfig.antiXray ) + { + update.startTiming(); + updateNearbyBlocks( world, position, 2, false ); // 2 is the radius, we shouldn't change it as that would make it exponentially slower + update.stopTiming(); + } + } + + /** + * Starts the timings handler, and then removes all non exposed ores from + * the chunk buffer. + */ + public void obfuscateSync(int chunkX, int chunkY, int bitmask, byte[] buffer, World world) + { + if ( world.spigotConfig.antiXray ) + { + obfuscate.startTiming(); + obfuscate( chunkX, chunkY, bitmask, buffer, world ); + obfuscate.stopTiming(); + } + } + + /** + * Removes all non exposed ores from the chunk buffer. + */ + public void obfuscate(int chunkX, int chunkY, int bitmask, byte[] buffer, World world) + { + // If the world is marked as obfuscated + if ( world.spigotConfig.antiXray ) + { + // Initial radius to search around for air + int initialRadius = 1; + // Which block in the buffer we are looking at, anywhere from 0 to 16^4 + int index = 0; + // The iterator marking which random ore we should use next + int randomOre = 0; + + // Chunk corner X and Z blocks + int startX = chunkX << 4; + int startZ = chunkY << 4; + + byte replaceWithTypeId; + switch ( world.getWorld().getEnvironment() ) + { + case NETHER: + replaceWithTypeId = (byte) CraftMagicNumbers.getId(Blocks.NETHERRACK); + break; + case THE_END: + replaceWithTypeId = (byte) CraftMagicNumbers.getId(Blocks.END_STONE); + break; + default: + replaceWithTypeId = (byte) CraftMagicNumbers.getId(Blocks.STONE); + break; + } + + // Chunks can have up to 16 sections + for ( int i = 0; i < 16; i++ ) + { + // If the bitmask indicates this chunk is sent... + if ( ( bitmask & 1 << i ) != 0 ) + { + // Work through all blocks in the chunk, y,z,x + for ( int y = 0; y < 16; y++ ) + { + for ( int z = 0; z < 16; z++ ) + { + for ( int x = 0; x < 16; x++ ) + { + // For some reason we can get too far ahead of ourselves (concurrent modification on bulk chunks?) so if we do, just abort and move on + if ( index >= buffer.length ) + { + index++; + continue; + } + // Grab the block ID in the buffer. + // TODO: extended IDs are not yet supported + int blockId = (buffer[index << 1] & 0xFF) + | ((buffer[(index << 1) + 1] & 0xFF) << 8); + blockId >>>= 4; + // Check if the block should be obfuscated + if ( obfuscateBlocks[blockId] ) + { + // The world isn't loaded, bail out + if ( !isLoaded( world, new BlockPosition( startX + x, ( i << 4 ) + y, startZ + z ), initialRadius ) ) + { + index++; + continue; + } + // On the otherhand, if radius is 0, or the nearby blocks are all non air, we can obfuscate + if ( !hasTransparentBlockAdjacent( world, new BlockPosition( startX + x, ( i << 4 ) + y, startZ + z ), initialRadius ) ) + { + int newId = blockId; + switch ( world.spigotConfig.engineMode ) + { + case 1: + // Replace with replacement material + newId = replaceWithTypeId & 0xFF; + break; + case 2: + // Replace with random ore. + if ( randomOre >= replacementOres.length ) + { + randomOre = 0; + } + newId = replacementOres[randomOre++] & 0xFF; + break; + } + newId <<= 4; + buffer[index << 1] = (byte) (newId & 0xFF); + buffer[(index << 1) + 1] = (byte) ((newId >> 8) & 0xFF); + } + } + + index++; + } + } + } + } + } + } + } + + private void updateNearbyBlocks(World world, BlockPosition position, int radius, boolean updateSelf) + { + // If the block in question is loaded + if ( world.isLoaded( position ) ) + { + // Get block id + Block block = world.getType(position).getBlock(); + + // See if it needs update + if ( updateSelf && obfuscateBlocks[Block.getId( block )] ) + { + // Send the update + world.notify( position ); + } + + // Check other blocks for updates + if ( radius > 0 ) + { + updateNearbyBlocks( world, position.east(), radius - 1, true ); + updateNearbyBlocks( world, position.west(), radius - 1, true ); + updateNearbyBlocks( world, position.up(), radius - 1, true ); + updateNearbyBlocks( world, position.down(), radius - 1, true ); + updateNearbyBlocks( world, position.south(), radius - 1, true ); + updateNearbyBlocks( world, position.north(), radius - 1, true ); + } + } + } + + private static boolean isLoaded(World world, BlockPosition position, int radius) + { + return world.isLoaded( position ) + && ( radius == 0 || + ( isLoaded( world, position.east(), radius - 1 ) + && isLoaded( world, position.west(), radius - 1 ) + && isLoaded( world, position.up(), radius - 1 ) + && isLoaded( world, position.down(), radius - 1 ) + && isLoaded( world, position.south(), radius - 1 ) + && isLoaded( world, position.north(), radius - 1 ) ) ); + } + + private static boolean hasTransparentBlockAdjacent(World world, BlockPosition position, int radius) + { + return !isSolidBlock(world.getType(position, false).getBlock()) /* isSolidBlock */ + || ( radius > 0 + && ( hasTransparentBlockAdjacent( world, position.east(), radius - 1 ) + || hasTransparentBlockAdjacent( world, position.west(), radius - 1 ) + || hasTransparentBlockAdjacent( world, position.up(), radius - 1 ) + || hasTransparentBlockAdjacent( world, position.down(), radius - 1 ) + || hasTransparentBlockAdjacent( world, position.south(), radius - 1 ) + || hasTransparentBlockAdjacent( world, position.north(), radius - 1 ) ) ); + } + + private static boolean isSolidBlock(Block block) { + // Mob spawners are treated as solid blocks as far as the + // game is concerned for lighting and other tasks but for + // rendering they can be seen through therefor we special + // case them so that the antixray doesn't show the fake + // blocks around them. + return block.isOccluding() && block != Blocks.MOB_SPAWNER; + } +} diff --git a/src/main/java/org/spigotmc/SpigotWorldConfig.java b/src/main/java/org/spigotmc/SpigotWorldConfig.java index 1814b4f..45b2aae 100644 --- a/src/main/java/org/spigotmc/SpigotWorldConfig.java +++ b/src/main/java/org/spigotmc/SpigotWorldConfig.java @@ -1,5 +1,6 @@ package org.spigotmc; +import java.util.Arrays; import java.util.List; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; @@ -212,4 +213,36 @@ public class SpigotWorldConfig arrowDespawnRate = getInt( "arrow-despawn-rate", 1200 ); log( "Arrow Despawn Rate: " + arrowDespawnRate ); } + + public boolean antiXray; + public int engineMode; + public List hiddenBlocks; + public List replaceBlocks; + public AntiXray antiXrayInstance; + private void antiXray() + { + antiXray = getBoolean( "anti-xray.enabled", true ); + log( "Anti X-Ray: " + antiXray ); + + engineMode = getInt( "anti-xray.engine-mode", 1 ); + log( "\tEngine Mode: " + engineMode ); + + if ( SpigotConfig.version < 5 ) + { + set( "anti-xray.blocks", null ); + } + hiddenBlocks = getList( "anti-xray.hide-blocks", Arrays.asList( new Integer[] + { + 14, 15, 16, 21, 48, 49, 54, 56, 73, 74, 82, 129, 130 + } ) ); + log( "\tHidden Blocks: " + hiddenBlocks ); + + replaceBlocks = getList( "anti-xray.replace-blocks", Arrays.asList( new Integer[] + { + 1, 5 + } ) ); + log( "\tReplace Blocks: " + replaceBlocks ); + + antiXrayInstance = new AntiXray( this ); + } } -- 2.1.0