Paper/CraftBukkit-Patches/0018-Orebfuscator.patch

363 lines
15 KiB
Diff
Raw Normal View History

From 3c7f43e649d3e15939a321426a27eb5dbabddfea Mon Sep 17 00:00:00 2001
From: md_5 <md_5@live.com.au>
2013-05-16 10:51:25 +02:00
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
2013-07-02 05:03:56 +02:00
index daa376c..79c94dc 100644
--- a/src/main/java/net/minecraft/server/EntityFallingBlock.java
+++ b/src/main/java/net/minecraft/server/EntityFallingBlock.java
2013-07-02 05:03:56 +02:00
@@ -86,6 +86,7 @@ public class EntityFallingBlock extends Entity {
}
this.world.setAir(i, j, k);
2013-06-20 10:52:23 +02:00
+ world.spigotConfig.antiXrayInstance.updateNearbyBlocks(world, i, j, k); // Spigot
}
if (this.onGround) {
diff --git a/src/main/java/net/minecraft/server/Explosion.java b/src/main/java/net/minecraft/server/Explosion.java
2013-07-02 05:03:56 +02:00
index 758d447..1d1c88c 100644
--- a/src/main/java/net/minecraft/server/Explosion.java
+++ b/src/main/java/net/minecraft/server/Explosion.java
@@ -240,6 +240,7 @@ public class Explosion {
j = chunkposition.y;
k = chunkposition.z;
l = this.world.getTypeId(i, j, k);
2013-06-20 10:52:23 +02:00
+ world.spigotConfig.antiXrayInstance.updateNearbyBlocks(world, i, j, k); // Spigot
if (flag) {
double d0 = (double) ((float) i + this.world.random.nextFloat());
double d1 = (double) ((float) j + this.world.random.nextFloat());
diff --git a/src/main/java/net/minecraft/server/Packet51MapChunk.java b/src/main/java/net/minecraft/server/Packet51MapChunk.java
index fa10397..91d7feb 100644
--- a/src/main/java/net/minecraft/server/Packet51MapChunk.java
+++ b/src/main/java/net/minecraft/server/Packet51MapChunk.java
@@ -33,6 +33,7 @@ public class Packet51MapChunk extends Packet {
this.d = chunkmap.c;
this.c = chunkmap.b;
2013-06-20 10:52:23 +02:00
+ chunk.world.spigotConfig.antiXrayInstance.obfuscateSync(chunk.x, chunk.z, i, chunkmap.a, chunk.world); // Spigot
2013-05-16 09:56:35 +02:00
try {
this.inflatedBuffer = chunkmap.a;
diff --git a/src/main/java/net/minecraft/server/Packet56MapChunkBulk.java b/src/main/java/net/minecraft/server/Packet56MapChunkBulk.java
2013-07-02 05:03:56 +02:00
index 04c44fc..a6e3be2 100644
--- a/src/main/java/net/minecraft/server/Packet56MapChunkBulk.java
+++ b/src/main/java/net/minecraft/server/Packet56MapChunkBulk.java
@@ -28,6 +28,7 @@ public class Packet56MapChunkBulk extends Packet {
}
};
// CraftBukkit end
2013-05-16 10:16:30 +02:00
+ private World world; // Spigot
public Packet56MapChunkBulk() {}
2013-05-16 10:51:25 +02:00
@@ -46,6 +47,9 @@ public class Packet56MapChunkBulk extends Packet {
Chunk chunk = (Chunk) list.get(k);
ChunkMap chunkmap = Packet51MapChunk.a(chunk, true, '\uffff');
2013-05-16 10:16:30 +02:00
+ // Spigot start
+ world = chunk.world;
+ /*
if (buildBuffer.length < j + chunkmap.a.length) {
byte[] abyte = new byte[j + chunkmap.a.length];
2013-05-16 10:51:25 +02:00
@@ -54,6 +58,8 @@ public class Packet56MapChunkBulk extends Packet {
}
2013-05-16 10:16:30 +02:00
System.arraycopy(chunkmap.a, 0, buildBuffer, j, chunkmap.a.length);
2013-05-16 10:51:25 +02:00
+ */
+ // Spigot end
j += chunkmap.a.length;
2013-05-16 10:51:25 +02:00
this.c[k] = chunk.x;
this.d[k] = chunk.z;
2013-05-16 10:16:30 +02:00
@@ -81,6 +87,22 @@ public class Packet56MapChunkBulk extends Packet {
if (this.buffer != null) {
return;
}
2013-05-16 10:16:30 +02:00
+ // Spigot start
+ int finalBufferSize = 0;
2013-05-16 10:16:30 +02:00
+ // Obfuscate all sections
+ for (int i = 0; i < a.length; i++) {
2013-06-20 10:52:23 +02:00
+ world.spigotConfig.antiXrayInstance.obfuscate(c[i], d[i], a[i], inflatedBuffers[i], world);
+ finalBufferSize += inflatedBuffers[i].length;
+ }
+
+ // Now it's time to efficiently copy the chunk to the build buffer
+ buildBuffer = new byte[finalBufferSize];
+ int bufferLocation = 0;
+ for (int i = 0; i < a.length; i++) {
+ System.arraycopy(inflatedBuffers[i], 0, buildBuffer, bufferLocation, inflatedBuffers[i].length);
+ bufferLocation += inflatedBuffers[i].length;
+ }
2013-05-16 10:16:30 +02:00
+ // Spigot end
Deflater deflater = localDeflater.get();
deflater.reset();
diff --git a/src/main/java/net/minecraft/server/PlayerInteractManager.java b/src/main/java/net/minecraft/server/PlayerInteractManager.java
index a7afa55..1a60229 100644
--- a/src/main/java/net/minecraft/server/PlayerInteractManager.java
+++ b/src/main/java/net/minecraft/server/PlayerInteractManager.java
@@ -184,6 +184,7 @@ public class PlayerInteractManager {
this.o = j1;
}
}
2013-06-20 10:52:23 +02:00
+ world.spigotConfig.antiXrayInstance.updateNearbyBlocks(world, i, j, k); // Spigot
}
}
2013-06-20 10:52:23 +02:00
diff --git a/src/main/java/org/spigotmc/AntiXray.java b/src/main/java/org/spigotmc/AntiXray.java
new file mode 100644
index 0000000..c165304
--- /dev/null
2013-06-20 10:52:23 +02:00
+++ b/src/main/java/org/spigotmc/AntiXray.java
@@ -0,0 +1,204 @@
+package org.spigotmc;
+
+import gnu.trove.set.TByteSet;
+import gnu.trove.set.hash.TByteHashSet;
+import net.minecraft.server.Block;
+import net.minecraft.server.World;
+
2013-06-20 10:52:23 +02:00
+public class AntiXray
+{
+
+ private static final CustomTimingsHandler update = new CustomTimingsHandler( "xray - update" );
+ private static final CustomTimingsHandler obfuscate = new CustomTimingsHandler( "xray - obfuscate" );
2013-05-16 10:51:25 +02:00
+ /*========================================================================*/
+ // Used to keep track of which blocks to obfuscate
2013-06-20 10:52:23 +02:00
+ private final boolean[] obfuscateBlocks = new boolean[ Short.MAX_VALUE ];
+ // Used to select a random replacement ore
+ private byte[] replacementOres;
+
2013-06-20 10:52:23 +02:00
+ public AntiXray(SpigotWorldConfig config)
+ {
2013-05-16 10:51:25 +02:00
+ // Set all listed blocks as true to be obfuscated
2013-06-22 05:59:56 +02:00
+ for ( int id : config.blocks )
+ {
+ obfuscateBlocks[id] = true;
+ }
+
+ // For every block
+ TByteSet blocks = new TByteHashSet();
+ for ( int i = 0; i < obfuscateBlocks.length; i++ )
2013-07-28 07:58:10 +02:00
+ {
+ // If we are obfuscating it
+ if ( obfuscateBlocks[i] )
+ {
+ Block block = Block.byId[i];
+ // Check it exists and is not a tile entity
+ if ( block != null && !block.t() /* isTileEntity */ )
+ {
+ // Add it to the set of replacement blocks
+ blocks.add( (byte) i );
+ }
+ }
2013-07-28 07:58:10 +02:00
+ }
+ // Bake it to a flat array of replacements
+ replacementOres = blocks.toArray();
+ }
+
2013-05-16 10:51:25 +02:00
+ /**
+ * Starts the timings handler, then updates all blocks within the set radius
+ * of the given coordinate, revealing them if they are hidden ores.
+ */
2013-06-20 10:52:23 +02:00
+ public void updateNearbyBlocks(World world, int x, int y, int z)
+ {
2013-06-20 10:52:23 +02:00
+ if ( world.spigotConfig.antiXray )
+ {
2013-05-16 10:51:25 +02:00
+ update.startTiming();
+ updateNearbyBlocks( world, x, y, z, 2, false ); // 2 is the radius, we shouldn't change it as that would make it exponentially slower
2013-05-16 10:51:25 +02:00
+ update.stopTiming();
+ }
2013-02-16 00:13:00 +01:00
+ }
+
2013-05-16 10:51:25 +02:00
+ /**
+ * Starts the timings handler, and then removes all non exposed ores from
+ * the chunk buffer.
+ */
2013-06-20 10:52:23 +02:00
+ public void obfuscateSync(int chunkX, int chunkY, int bitmask, byte[] buffer, World world)
+ {
2013-06-20 10:52:23 +02:00
+ if ( world.spigotConfig.antiXray )
+ {
2013-05-16 10:51:25 +02:00
+ obfuscate.startTiming();
+ obfuscate( chunkX, chunkY, bitmask, buffer, world );
2013-05-16 10:51:25 +02:00
+ obfuscate.stopTiming();
+ }
+ }
+
2013-05-16 10:51:25 +02:00
+ /**
+ * Removes all non exposed ores from the chunk buffer.
+ */
2013-06-20 10:52:23 +02:00
+ public void obfuscate(int chunkX, int chunkY, int bitmask, byte[] buffer, World world)
+ {
2013-05-16 10:51:25 +02:00
+ // If the world is marked as obfuscated
2013-06-20 10:52:23 +02:00
+ if ( world.spigotConfig.antiXray )
+ {
+ // Initial radius to search around for air
+ int initialRadius = 1;
2013-05-16 10:51:25 +02:00
+ // Which block in the buffer we are looking at, anywhere from 0 to 16^4
+ int index = 0;
2013-05-16 10:51:25 +02:00
+ // 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;
2013-05-16 10:51:25 +02:00
+
+ // 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 )
+ {
2013-05-16 10:51:25 +02:00
+ // 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 )
+ {
+ continue;
+ }
2013-05-16 10:51:25 +02:00
+ // 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] )
+ {
+ // TODO: Don't really understand this, but if radius is not 0 and the world isn't loaded, bail out
+ 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 || !hasTransparentBlockAdjacent( world, startX + x, ( i << 4 ) + y, startZ + z, initialRadius ) )
+ {
2013-06-20 10:52:23 +02:00
+ switch ( world.spigotConfig.engineMode )
+ {
2013-05-16 10:51:25 +02:00
+ case 1:
2013-02-16 00:13:00 +01:00
+ // Replace with stone
+ buffer[index] = (byte) Block.STONE.id;
2013-05-16 10:51:25 +02:00
+ break;
+ case 2:
+ // Replace with random ore.
+ if ( randomOre >= replacementOres.length )
+ {
2013-07-28 07:58:10 +02:00
+ randomOre = 0;
2013-05-16 10:51:25 +02:00
+ }
+ buffer[index] = replacementOres[randomOre++];
2013-05-16 10:51:25 +02:00
+ break;
2013-02-16 00:13:00 +01:00
+ }
+ }
+ }
2013-05-16 10:51:25 +02:00
+
+ index++;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ private void updateNearbyBlocks(World world, int x, int y, int z, int radius, boolean updateSelf)
+ {
2013-05-16 10:51:25 +02:00
+ // If the block in question is loaded
+ if ( world.isLoaded( x, y, z ) )
+ {
+ // Get block id
+ int id = world.getTypeId( x, y, z );
+
+ // See if it needs update
+ if ( updateSelf && obfuscateBlocks[id] )
+ {
+ // Send the update
+ world.notify( x, y, z );
+ }
+
+ // Check other blocks for updates
+ if ( radius > 0 )
+ {
+ updateNearbyBlocks( world, x + 1, y, z, radius - 1, true );
+ updateNearbyBlocks( world, x - 1, y, z, radius - 1, true );
+ updateNearbyBlocks( world, x, y + 1, z, radius - 1, true );
+ updateNearbyBlocks( world, x, y - 1, z, radius - 1, true );
+ updateNearbyBlocks( world, x, y, z + 1, radius - 1, true );
+ updateNearbyBlocks( world, x, y, z - 1, radius - 1, true );
+ }
+ }
+ }
+
+ private static boolean isLoaded(World world, int x, int y, int z, int radius)
+ {
+ return world.isLoaded( x, y, z )
+ || ( radius > 0
+ && ( 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 ) ) );
+ }
+
+ private static boolean hasTransparentBlockAdjacent(World world, int x, int y, int z, int radius)
+ {
+ return !Block.l( world.getTypeId( x, y, z ) ) /* isSolidBlock */
+ || ( radius > 0
+ && ( 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 ) ) );
+ }
+}
2013-06-20 10:52:23 +02:00
diff --git a/src/main/java/org/spigotmc/SpigotWorldConfig.java b/src/main/java/org/spigotmc/SpigotWorldConfig.java
index 3e66d79..bab9d8f 100644
2013-06-20 10:52:23 +02:00
--- a/src/main/java/org/spigotmc/SpigotWorldConfig.java
+++ b/src/main/java/org/spigotmc/SpigotWorldConfig.java
@@ -1,5 +1,6 @@
2013-06-20 10:52:23 +02:00
package org.spigotmc;
+import java.util.Arrays;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -128,4 +129,29 @@ public class SpigotWorldConfig
2013-06-20 10:52:23 +02:00
viewDistance = getInt( "view-distance", Bukkit.getViewDistance() );
log( "View Distance: " + viewDistance );
}
+
+ public boolean antiXray = true;
+ public int engineMode = 1;
+ public List<Integer> blocks = Arrays.asList( new Integer[]
+ {
+ 1, 5, 14, 15, 16, 21, 48, 49, 54, 56, 73, 74, 82, 129, 130
+ } );
2013-06-20 10:52:23 +02:00
+ public AntiXray antiXrayInstance;
+ private void antiXray()
+ {
+ antiXray = getBoolean( "anti-xray.enabled", antiXray );
2013-06-20 10:52:23 +02:00
+ log( "Anti X-Ray: " + antiXray );
+
+ engineMode = getInt( "anti-xray.engine-mode", engineMode );
2013-06-20 10:52:23 +02:00
+ log( "\tEngine Mode: " + engineMode );
+
+ if ( SpigotConfig.version < 3 )
+ {
+ set( "anti-xray.blocks", blocks );
+ }
+ blocks = getList( "anti-xray.blocks", blocks );
2013-06-20 10:52:23 +02:00
+ log( "\tBlocks: " + blocks );
+
2013-06-20 10:52:23 +02:00
+ antiXrayInstance = new AntiXray( this );
+ }
}
--
1.8.1.2