Paper/CraftBukkit-Patches/0053-Orebfuscator.patch

415 lines
18 KiB
Diff
Raw Normal View History

2015-03-16 11:19:21 +01:00
From 0e8505239a1fb379830bfa7b713b18d91e2b7236 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
diff --git a/src/main/java/net/minecraft/server/EntityFallingBlock.java b/src/main/java/net/minecraft/server/EntityFallingBlock.java
2015-02-28 12:36:22 +01:00
index 9e0d5ad..ce91553 100644
--- a/src/main/java/net/minecraft/server/EntityFallingBlock.java
+++ b/src/main/java/net/minecraft/server/EntityFallingBlock.java
2014-11-28 02:17:45 +01:00
@@ -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
2015-02-28 12:36:22 +01:00
} else if (!this.world.isClientSide) {
2014-11-28 02:17:45 +01:00
this.die();
return;
2015-02-28 12:36:22 +01:00
@@ -85,6 +86,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
2015-02-28 12:36:22 +01:00
index 71c78bd..4564877 100644
--- a/src/main/java/net/minecraft/server/Explosion.java
+++ b/src/main/java/net/minecraft/server/Explosion.java
2015-02-28 12:36:22 +01:00
@@ -194,6 +194,7 @@ public class Explosion {
2014-11-28 02:17:45 +01:00
blockposition = (BlockPosition) iterator.next();
Block block = this.world.getType(blockposition).getBlock();
+ world.spigotConfig.antiXrayInstance.updateNearbyBlocks(world, blockposition); // Spigot
if (flag) {
2014-11-28 02:17:45 +01:00
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
2015-02-28 12:36:22 +01:00
index 6e368f5..58c0275 100644
--- a/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java
+++ b/src/main/java/net/minecraft/server/PacketPlayOutMapChunk.java
2015-02-28 12:36:22 +01:00
@@ -19,6 +19,7 @@ public class PacketPlayOutMapChunk implements Packet<PacketListenerPlayOut> {
2014-11-28 02:17:45 +01:00
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);
}
2015-02-28 12:36:22 +01:00
public void a(PacketDataSerializer packetdataserializer) throws IOException {
diff --git a/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java b/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java
2015-02-28 12:36:22 +01:00
index 63b90f7..10c0e34 100644
--- a/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java
+++ b/src/main/java/net/minecraft/server/PacketPlayOutMapChunkBulk.java
2015-02-28 12:36:22 +01:00
@@ -9,6 +9,7 @@ public class PacketPlayOutMapChunkBulk implements Packet<PacketListenerPlayOut>
2014-11-28 02:17:45 +01:00
private int[] b;
2015-02-28 12:36:22 +01:00
private PacketPlayOutMapChunk.ChunkMap[] c;
2014-11-28 02:17:45 +01:00
private boolean d;
+ private World world; // Spigot
public PacketPlayOutMapChunkBulk() {}
2015-02-28 12:36:22 +01:00
@@ -28,7 +29,8 @@ public class PacketPlayOutMapChunkBulk implements Packet<PacketListenerPlayOut>
2014-11-28 02:17:45 +01:00
this.b[j] = chunk.locZ;
2015-02-28 12:36:22 +01:00
this.c[j] = packetplayoutmapchunk_chunkmap;
2014-11-28 02:17:45 +01:00
}
-
+
+ world = ((Chunk) list.get(0)).getWorld(); // Spigot
}
2015-02-28 12:36:22 +01:00
public void a(PacketDataSerializer packetdataserializer) throws IOException {
@@ -68,6 +70,7 @@ public class PacketPlayOutMapChunkBulk implements Packet<PacketListenerPlayOut>
2014-11-28 02:17:45 +01:00
}
2014-11-28 02:17:45 +01:00
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
2015-02-28 12:36:22 +01:00
index 0301fba..7a84a95 100644
--- a/src/main/java/net/minecraft/server/PlayerInteractManager.java
+++ b/src/main/java/net/minecraft/server/PlayerInteractManager.java
2014-11-28 02:17:45 +01:00
@@ -197,6 +197,7 @@ public class PlayerInteractManager {
}
2014-11-28 02:17:45 +01:00
}
2014-11-28 02:17:45 +01:00
+ world.spigotConfig.antiXrayInstance.updateNearbyBlocks(world, blockposition); // Spigot
}
2014-11-28 02:17:45 +01:00
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
2015-03-16 11:19:21 +01:00
index 3218138..b168454 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
2015-03-16 11:19:21 +01:00
@@ -497,6 +497,7 @@ public abstract class World implements IBlockAccess {
2014-11-28 02:17:45 +01:00
this.d(blockposition.up(), block);
this.d(blockposition.north(), block);
this.d(blockposition.south(), block);
+ spigotConfig.antiXrayInstance.updateNearbyBlocks(this, blockposition); // Spigot
}
2014-11-28 02:17:45 +01:00
public void a(BlockPosition blockposition, Block block, EnumDirection enumdirection) {
2015-03-16 11:19:21 +01:00
@@ -722,9 +723,16 @@ public abstract class World implements IBlockAccess {
2014-11-28 02:17:45 +01:00
return this.worldProvider.p()[this.getLightLevel(blockposition)];
}
- public IBlockData getType(BlockPosition blockposition) {
+ // Spigot start
2014-11-28 02:17:45 +01:00
+ public IBlockData getType(BlockPosition blockposition)
+ {
2014-11-28 02:17:45 +01:00
+ return getType( blockposition, true );
+ }
2014-11-28 02:17:45 +01:00
+
+ public IBlockData getType(BlockPosition blockposition, boolean useCaptured) {
// CraftBukkit start - tree generation
- if (captureTreeGeneration) {
+ if (captureTreeGeneration && useCaptured) {
+ // Spigot end
Iterator<BlockState> 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
2014-11-28 02:17:45 +01:00
index 0000000..6f28cd8
--- /dev/null
+++ b/src/main/java/org/spigotmc/AntiXray.java
2014-11-28 02:17:45 +01:00
@@ -0,0 +1,234 @@
+package org.spigotmc;
+
2014-11-28 02:17:45 +01:00
+import gnu.trove.set.TByteSet;
+import gnu.trove.set.hash.TByteHashSet;
+import net.minecraft.server.Block;
2014-11-28 02:17:45 +01:00
+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.
+ */
2014-11-28 02:17:45 +01:00
+ public void updateNearbyBlocks(World world, BlockPosition position)
+ {
+ if ( world.spigotConfig.antiXray )
+ {
+ update.startTiming();
2014-11-28 02:17:45 +01:00
+ 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:
2014-11-28 02:17:45 +01:00
+ 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
2014-11-28 02:17:45 +01:00
+ 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
2014-11-28 02:17:45 +01:00
+ 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
2014-11-28 02:17:45 +01:00
+ if ( !hasTransparentBlockAdjacent( world, new BlockPosition( startX + x, ( i << 4 ) + y, startZ + z ), initialRadius ) )
+ {
2014-11-28 02:17:45 +01:00
+ int newId = blockId;
+ switch ( world.spigotConfig.engineMode )
+ {
+ case 1:
+ // Replace with replacement material
2014-11-28 02:17:45 +01:00
+ newId = replaceWithTypeId & 0xFF;
+ break;
+ case 2:
+ // Replace with random ore.
+ if ( randomOre >= replacementOres.length )
+ {
+ randomOre = 0;
+ }
2014-11-28 02:17:45 +01:00
+ newId = replacementOres[randomOre++] & 0xFF;
+ break;
+ }
2014-11-28 02:17:45 +01:00
+ newId <<= 4;
+ buffer[index << 1] = (byte) (newId & 0xFF);
+ buffer[(index << 1) + 1] = (byte) ((newId >> 8) & 0xFF);
+ }
+ }
+
+ index++;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
2014-11-28 02:17:45 +01:00
+ private void updateNearbyBlocks(World world, BlockPosition position, int radius, boolean updateSelf)
+ {
+ // If the block in question is loaded
2014-11-28 02:17:45 +01:00
+ if ( world.isLoaded( position ) )
+ {
+ // Get block id
2014-11-28 02:17:45 +01:00
+ Block block = world.getType(position).getBlock();
+
+ // See if it needs update
+ if ( updateSelf && obfuscateBlocks[Block.getId( block )] )
+ {
+ // Send the update
2014-11-28 02:17:45 +01:00
+ world.notify( position );
+ }
+
+ // Check other blocks for updates
+ if ( radius > 0 )
+ {
2014-11-28 02:17:45 +01:00
+ 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 );
+ }
+ }
+ }
+
2014-11-28 02:17:45 +01:00
+ private static boolean isLoaded(World world, BlockPosition position, int radius)
+ {
2014-11-28 02:17:45 +01:00
+ return world.isLoaded( position )
+ && ( radius == 0 ||
2014-11-28 02:17:45 +01:00
+ ( 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 ) ) );
+ }
+
2014-11-28 02:17:45 +01:00
+ private static boolean hasTransparentBlockAdjacent(World world, BlockPosition position, int radius)
+ {
2014-11-28 02:17:45 +01:00
+ return !isSolidBlock(world.getType(position, false).getBlock()) /* isSolidBlock */
+ || ( radius > 0
2014-11-28 02:17:45 +01:00
+ && ( 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.
2014-11-28 02:17:45 +01:00
+ 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 af0c4b2..c390381 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<Integer> hiddenBlocks;
+ public List<Integer> 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 );
+ }
}
--
2014-11-28 02:17:45 +01:00
2.1.0