Reformat orebfuscator code / rework for easy reading

This commit is contained in:
md_5 2013-05-16 19:21:46 +10:00
parent 6d6ebe7c7c
commit b4dc7117c7

View File

@ -1,9 +1,8 @@
From 77b92872f2fc584029598f8d601251169a57d209 Mon Sep 17 00:00:00 2001
From 2617973d6c96afdf7aed70fbb3fc328f031431ae Mon Sep 17 00:00:00 2001
From: md_5 <md_5@live.com.au>
Date: Thu, 16 May 2013 18:51:05 +1000
Subject: [PATCH] Orebfuscator
Implement lightweight orebfuscator to Spigot
diff --git a/src/main/java/net/minecraft/server/EntityFallingBlock.java b/src/main/java/net/minecraft/server/EntityFallingBlock.java
index 17d837d..d38eb4d 100644
@ -174,10 +173,10 @@ index 67477f4..e5004b3 100644
}
diff --git a/src/main/java/org/spigotmc/OrebfuscatorManager.java b/src/main/java/org/spigotmc/OrebfuscatorManager.java
new file mode 100644
index 0000000..9de208b
index 0000000..b4d66f6
--- /dev/null
+++ b/src/main/java/org/spigotmc/OrebfuscatorManager.java
@@ -0,0 +1,179 @@
@@ -0,0 +1,211 @@
+package org.spigotmc;
+
+import gnu.trove.set.TByteSet;
@ -187,32 +186,38 @@ index 0000000..9de208b
+import net.minecraft.server.World;
+import org.bukkit.CustomTimingsHandler;
+
+public class OrebfuscatorManager {
+public class OrebfuscatorManager
+{
+
+ private static final CustomTimingsHandler update = new CustomTimingsHandler("xray - update");
+ private static final CustomTimingsHandler obfuscate = new CustomTimingsHandler("xray - obfuscate");
+ 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 static final boolean[] obfuscateBlocks = new boolean[Short.MAX_VALUE];
+ private static final boolean[] obfuscateBlocks = new boolean[ Short.MAX_VALUE ];
+ // Used to select a random replacement ore
+ private static byte[] replacementOres;
+
+ static {
+ static
+ {
+ // Set all listed blocks as true to be obfuscated
+ for (short id : MinecraftServer.getServer().server.orebfuscatorBlocks) {
+ for ( short id : MinecraftServer.getServer().server.orebfuscatorBlocks )
+ {
+ obfuscateBlocks[id] = true;
+ }
+
+ // For every block
+ TByteSet blocks = new TByteHashSet();
+ for (int i = 0; i < obfuscateBlocks.length; i++) {
+ for ( int i = 0; i < obfuscateBlocks.length; i++ )
+ {
+ // If we are obfuscating it
+ if (obfuscateBlocks[i]) {
+ if ( obfuscateBlocks[i] )
+ {
+ Block block = Block.byId[i];
+ // Check it exists and is not a tile entity
+ if (block != null && !block.t() /* isTileEntity */) {
+ if ( block != null && !block.t() /* isTileEntity */ )
+ {
+ // Add it to the set of replacement blocks
+ blocks.add((byte) i);
+ blocks.add( (byte) i );
+ }
+ }
+ }
@ -224,10 +229,12 @@ index 0000000..9de208b
+ * Starts the timings handler, then updates all blocks within the set radius
+ * of the given coordinate, revealing them if they are hidden ores.
+ */
+ public static void updateNearbyBlocks(World world, int x, int y, int z) {
+ if (world.getWorld().obfuscated) {
+ public static void updateNearbyBlocks(World world, int x, int y, int z)
+ {
+ if ( world.getWorld().obfuscated )
+ {
+ update.startTiming();
+ updateNearbyBlocks(world, x, y, z, 2); // 2 is the radius, we shouldn't change it as that would make it exponentially slower
+ updateNearbyBlocks( world, x, y, z, 2 ); // 2 is the radius, we shouldn't change it as that would make it exponentially slower
+ update.stopTiming();
+ }
+ }
@ -236,10 +243,12 @@ index 0000000..9de208b
+ * Starts the timings handler, and then removes all non exposed ores from
+ * the chunk buffer.
+ */
+ public static void obfuscateSync(int chunkX, int chunkY, int bitmask, byte[] buffer, World world) {
+ if (world.getWorld().obfuscated) {
+ public static void obfuscateSync(int chunkX, int chunkY, int bitmask, byte[] buffer, World world)
+ {
+ if ( world.getWorld().obfuscated )
+ {
+ obfuscate.startTiming();
+ obfuscate(chunkX, chunkY, bitmask, buffer, world);
+ obfuscate( chunkX, chunkY, bitmask, buffer, world );
+ obfuscate.stopTiming();
+ }
+ }
@ -247,9 +256,11 @@ index 0000000..9de208b
+ /**
+ * Removes all non exposed ores from the chunk buffer.
+ */
+ public static void obfuscate(int chunkX, int chunkY, int bitmask, byte[] buffer, World world) {
+ public static void obfuscate(int chunkX, int chunkY, int bitmask, byte[] buffer, World world)
+ {
+ // If the world is marked as obfuscated
+ if (world.getWorld().obfuscated) {
+ if ( world.getWorld().obfuscated )
+ {
+ // 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
@ -262,42 +273,53 @@ index 0000000..9de208b
+ int startZ = chunkY << 4;
+
+ // Chunks can have up to 16 sections
+ for (int i = 0; i < 16; i++) {
+ for ( int i = 0; i < 16; i++ )
+ {
+ // If the bitmask indicates this chunk is sent...
+ if ((bitmask & 1 << i) != 0) {
+ 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 ( int y = 0; y < 16; y++ )
+ {
+ for ( int z = 0; z < 16; z++ )
+ {
+ for ( int x = 0; x < 16; x++ )
+ {
+ // Grab the block ID in the buffer.
+ // TODO: extended IDs are not yet supported
+ int blockId = buffer[index] & 0xFF;
+ // Check if the block should be obfuscated
+ if (obfuscateBlocks[blockId]) {
+ if ( obfuscateBlocks[blockId] )
+ {
+ // TODO: Don't really understand this, but if radius is not 0 and the world isn't loaded, bail out
+ if (initialRadius != 0 && !isWorldLoaded(world, startX + x, (i << 4) + y, startZ + z, initialRadius)) {
+ if ( initialRadius != 0 && !isLoaded( world, startX + x, ( i << 4 ) + y, startZ + z, initialRadius ) )
+ {
+ continue;
+ }
+ // On the otherhand, if radius is 0, or the nearby blocks are all non air, we can obfuscate
+ if (initialRadius == 0 || !areAjacentBlocksTransparent(world, startX + x, (i << 4) + y, startZ + z, initialRadius)) {
+ switch (world.getServer().orebfuscatorEngineMode) {
+ if ( initialRadius == 0 || !hasTransparentBlockAdjacent( world, startX + x, ( i << 4 ) + y, startZ + z, initialRadius ) )
+ {
+ switch ( world.getServer().orebfuscatorEngineMode )
+ {
+ case 1:
+ // Replace with stone
+ buffer[index] = (byte) Block.STONE.id;
+ break;
+ case 2:
+ // Replace with random ore.
+ if (randomOre >= replacementOres.length) {
+ if ( randomOre >= replacementOres.length )
+ {
+ randomOre = 0;
+ }
+ buffer[index] = (byte) (int) replacementOres[randomOre++];
+ buffer[index] = replacementOres[randomOre++];
+ break;
+ }
+ }
+ }
+
+ // 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) {
+ if ( ++index >= buffer.length )
+ {
+ return;
+ }
+ }
@ -308,53 +330,62 @@ index 0000000..9de208b
+ }
+ }
+
+ private static void updateNearbyBlocks(World world, int x, int y, int z, int radius) {
+ private static void updateNearbyBlocks(World world, int x, int y, int z, int radius)
+ {
+ // If the block in question is loaded
+ if (world.isLoaded(x, y, z)) {
+ if ( world.isLoaded( x, y, z ) )
+ {
+ // Get block id
+ int id = world.getTypeId(x, y, z);
+ int id = world.getTypeId( x, y, z );
+
+ // See if it needs update
+ if (obfuscateBlocks[id]) {
+ if ( obfuscateBlocks[id] )
+ {
+ // Send the update
+ world.notify(x, y, z);
+ world.notify( x, y, z );
+ }
+
+ // Check other blocks for updates
+ if (radius != 0) {
+ updateNearbyBlocks(world, x + 1, y, z, radius - 1);
+ updateNearbyBlocks(world, x - 1, y, z, radius - 1);
+ updateNearbyBlocks(world, x, y + 1, z, radius - 1);
+ updateNearbyBlocks(world, x, y - 1, z, radius - 1);
+ updateNearbyBlocks(world, x, y, z + 1, radius - 1);
+ updateNearbyBlocks(world, x, y, z - 1, radius - 1);
+ if ( radius != 0 )
+ {
+ updateNearbyBlocks( world, x + 1, y, z, radius - 1 );
+ updateNearbyBlocks( world, x - 1, y, z, radius - 1 );
+ updateNearbyBlocks( world, x, y + 1, z, radius - 1 );
+ updateNearbyBlocks( world, x, y - 1, z, radius - 1 );
+ updateNearbyBlocks( world, x, y, z + 1, radius - 1 );
+ updateNearbyBlocks( world, x, y, z - 1, radius - 1 );
+ }
+ }
+ }
+
+ private static boolean isWorldLoaded(World world, int x, int y, int z, int radius) {
+ boolean toret = (y > 0 && y <= world.getHeight() && world.isLoaded(x, y, z));
+ if (toret) {
+ return toret || (radius > 0 && (isWorldLoaded(world, x, y + 1, z, radius - 1)
+ || isWorldLoaded(world, x, y - 1, z, radius - 1)
+ || isWorldLoaded(world, x + 1, y, z, radius - 1)
+ || isWorldLoaded(world, x - 1, y, z, radius - 1)
+ || isWorldLoaded(world, x, y, z + 1, radius - 1)
+ || isWorldLoaded(world, x, y, z - 1, radius - 1)));
+ private static boolean isLoaded(World world, int x, int y, int z, int radius)
+ {
+ if ( radius > 0 && y > 0 && y <= world.getHeight() && world.isLoaded( x, y, z ) )
+ {
+ return isLoaded( world, x + 1, y, z, radius - 1 )
+ || isLoaded( world, x - 1, y, z, radius - 1 )
+ || isLoaded( world, x, y + 1, z, radius - 1 )
+ || isLoaded( world, x, y - 1, z, radius - 1 )
+ || isLoaded( world, x, y, z + 1, radius - 1 )
+ || isLoaded( world, x, y, z - 1, radius - 1 );
+ }
+
+ return false;
+ }
+
+ private static boolean areAjacentBlocksTransparent(World world, int x, int y, int z, int radius) {
+ return y > 0 && y <= world.getHeight()
+ && !Block.l(world.getTypeId(x, y, z))
+ || (radius > 0 && (areAjacentBlocksTransparent(world, x, y + 1, z, radius - 1)
+ || areAjacentBlocksTransparent(world, x, y - 1, z, radius - 1)
+ || areAjacentBlocksTransparent(world, x + 1, y, z, radius - 1)
+ || areAjacentBlocksTransparent(world, x - 1, y, z, radius - 1)
+ || areAjacentBlocksTransparent(world, x, y, z + 1, radius - 1)
+ || areAjacentBlocksTransparent(world, x, y, z - 1, radius - 1)));
+ private static boolean hasTransparentBlockAdjacent(World world, int x, int y, int z, int radius)
+ {
+ if ( radius > 0 && y > 0 && y <= world.getHeight() && !Block.l( world.getTypeId( x, y, z ) ) /* isSolidBlock */ )
+ {
+ return hasTransparentBlockAdjacent( world, x + 1, y, z, radius - 1 )
+ || hasTransparentBlockAdjacent( world, x - 1, y, z, radius - 1 )
+ || hasTransparentBlockAdjacent( world, x, y + 1, z, radius - 1 )
+ || hasTransparentBlockAdjacent( world, x, y - 1, z, radius - 1 )
+ || hasTransparentBlockAdjacent( world, x, y, z + 1, radius - 1 )
+ || hasTransparentBlockAdjacent( world, x, y, z - 1, radius - 1 );
+ }
+
+ return false;
+ }
+}
diff --git a/src/main/resources/configurations/bukkit.yml b/src/main/resources/configurations/bukkit.yml