From ce7c0ac39044dfe8ad145cb765c21ef9a855bb44 Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 16 May 2013 18:09:28 +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..ecc52b9 100644 --- a/src/main/java/net/minecraft/server/EntityFallingBlock.java +++ b/src/main/java/net/minecraft/server/EntityFallingBlock.java @@ -94,6 +94,7 @@ public class EntityFallingBlock extends Entity { } this.world.setAir(i, j, k); + org.spigotmc.OrebfuscatorManager.updateNearbyBlocks(world, i, j, k); // Spigot (Orebfuscator) } if (this.onGround) { diff --git a/src/main/java/net/minecraft/server/Explosion.java b/src/main/java/net/minecraft/server/Explosion.java index ef220c1..63f43b8 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); + org.spigotmc.OrebfuscatorManager.updateNearbyBlocks(world, i, j, k); // Spigot (Orebfuscator) 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 d11c0ea..6967b74 100644 --- a/src/main/java/net/minecraft/server/Packet51MapChunk.java +++ b/src/main/java/net/minecraft/server/Packet51MapChunk.java @@ -46,6 +46,7 @@ public class Packet51MapChunk extends Packet { this.d = chunkmap.c; this.c = chunkmap.b; + org.spigotmc.OrebfuscatorManager.obfuscateSync(chunk.x, chunk.z, i, chunkmap.a, chunk.world); // Spigot (Orebfuscator) 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 index 129dc4f..b7ffab5 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 + private World world; // Spigot (Orebfuscator) Keep track of world public Packet56MapChunkBulk() {} @@ -46,6 +47,8 @@ public class Packet56MapChunkBulk extends Packet { Chunk chunk = (Chunk) list.get(k); ChunkMap chunkmap = Packet51MapChunk.a(chunk, true, '\uffff'); + world = chunk.world; // Spigot (Orebfuscator) + /* Spigot (Orebfuscator) - Don't use the build buffer yet. Copy to it more efficiently once the chunk is obfuscated if (buildBuffer.length < j + chunkmap.a.length) { byte[] abyte = new byte[j + chunkmap.a.length]; @@ -53,7 +56,7 @@ public class Packet56MapChunkBulk extends Packet { buildBuffer = abyte; } - System.arraycopy(chunkmap.a, 0, buildBuffer, j, chunkmap.a.length); + System.arraycopy(chunkmap.a, 0, buildBuffer, j, chunkmap.a.length); */ j += chunkmap.a.length; this.c[k] = chunk.x; this.d[k] = chunk.z; @@ -82,6 +85,21 @@ public class Packet56MapChunkBulk extends Packet { return; } + // Spigot (Orebfuscator) start - Obfuscate chunks + int finalBufferSize = 0; + for (int i = 0; i < a.length; i++) { + org.spigotmc.OrebfuscatorManager.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; + } + // Spigot (Orebfuscator) end Deflater deflater = localDeflater.get(); deflater.reset(); deflater.setInput(this.buildBuffer); diff --git a/src/main/java/net/minecraft/server/PlayerInteractManager.java b/src/main/java/net/minecraft/server/PlayerInteractManager.java index 1243d1d..e45bcd2 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; } } + org.spigotmc.OrebfuscatorManager.updateNearbyBlocks(world, i, j, k); // Spigot } } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 12c5f81..010e9c3 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -163,6 +163,12 @@ public final class CraftServer implements Server { private final BooleanWrapper online = new BooleanWrapper(); public CraftScoreboardManager scoreboardManager; + // Orebfuscator use + public boolean orebfuscatorEnabled = false; + public int orebfuscatorEngineMode = 1; + public List orebfuscatorDisabledWorlds; + public List orebfuscatorBlocks; + private final class BooleanWrapper { private boolean value = true; } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index 44d1842..7d05a77 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -123,6 +123,8 @@ public class CraftWorld implements World { viewDistance = Bukkit.getServer().getViewDistance(); viewDistance = configuration.getInt("world-settings." + name + ".view-distance", viewDistance); + obfuscated = world.getServer().orebfuscatorEnabled && !world.getServer().orebfuscatorDisabledWorlds.contains(name); + if (!info) return; server.getLogger().info("-------------- Spigot ----------------"); server.getLogger().info("-------- World Settings For [" + name + "] --------"); @@ -138,6 +140,7 @@ public class CraftWorld implements World { server.getLogger().info("Tree Growth Modifier: " + treeGrowthModifier); server.getLogger().info("Mushroom Growth Modifier: " + mushroomGrowthModifier); server.getLogger().info("View distance: " + viewDistance); + server.getLogger().info("Oreobfuscator: " + obfuscated); server.getLogger().info("-------------------------------------------------"); // Spigot end } @@ -149,6 +152,7 @@ public class CraftWorld implements World { public double itemMergeRadius = 3.5; public double expMergeRadius = 3.5; public int viewDistance; + public boolean obfuscated = false; //Crop growth rates: public int wheatGrowthModifier = 100; public int cactusGrowthModifier = 100; diff --git a/src/main/java/org/bukkit/craftbukkit/Spigot.java b/src/main/java/org/bukkit/craftbukkit/Spigot.java index 67477f4..e5004b3 100644 --- a/src/main/java/org/bukkit/craftbukkit/Spigot.java +++ b/src/main/java/org/bukkit/craftbukkit/Spigot.java @@ -19,6 +19,14 @@ public class Spigot { server.spamGuardExclusions = configuration.getStringList("settings.spam-exclusions"); filterIps = configuration.getBoolean("settings.filter-unsafe-ips", false); + server.orebfuscatorEnabled = configuration.getBoolean("orebfuscator.enable", false); + server.orebfuscatorEngineMode = configuration.getInt("orebfuscator.engine-mode", 1); + server.orebfuscatorDisabledWorlds = configuration.getStringList("orebfuscator.disabled-worlds"); + server.orebfuscatorBlocks = configuration.getShortList("orebfuscator.blocks"); + if (server.orebfuscatorEngineMode != 1 && server.orebfuscatorEngineMode != 2) { + server.orebfuscatorEngineMode = 1; + } + if (server.chunkGCPeriod == 0) { server.getLogger().severe("[Spigot] You should not disable chunk-gc, unexpected behaviour may occur!"); } diff --git a/src/main/java/org/spigotmc/OrebfuscatorManager.java b/src/main/java/org/spigotmc/OrebfuscatorManager.java new file mode 100644 index 0000000..b1075f1 --- /dev/null +++ b/src/main/java/org/spigotmc/OrebfuscatorManager.java @@ -0,0 +1,143 @@ +package org.spigotmc; + +import java.util.ArrayList; +import java.util.List; +import net.minecraft.server.Block; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.World; +import org.bukkit.CustomTimingsHandler; + +public class OrebfuscatorManager { + + // Used to keep track of which blocks to obfuscate + private static final boolean[] obfuscateBlocks = new boolean[Short.MAX_VALUE]; + private static byte[] ores; + private static final CustomTimingsHandler obfuscate = new CustomTimingsHandler("xray - obfuscate"); + private static final CustomTimingsHandler update = new CustomTimingsHandler("xray - update"); + + // Default blocks + static { + for (short id : MinecraftServer.getServer().server.orebfuscatorBlocks) { + obfuscateBlocks[id] = true; + } + + List blocks = new ArrayList(); + for (int i = 0; i < obfuscateBlocks.length; i++) { + if (obfuscateBlocks[i]) { + Block block = Block.byId[i]; + if (i != Block.STONE.id && block != null && !block.t() /* isTileEntity */) { + blocks.add((byte) i); + } + } + } + ores = new byte[blocks.size()]; + for (int i = 0; i < ores.length; i++) { + ores[i] = blocks.get(i); + } + } + + public static void updateNearbyBlocks(World world, int x, int y, int z) { + update.startTiming(); + updateNearbyBlocks(world, x, y, z, 2); + update.stopTiming(); + } + + public static void obfuscateSync(int chunkX, int chunkY, int bitmask, byte[] buffer, World world) { + obfuscate.startTiming(); + obfuscate(chunkX, chunkY, bitmask, buffer, world); + obfuscate.stopTiming(); + } + + public static void obfuscate(int chunkX, int chunkY, int bitmask, byte[] buffer, World world) { + if (world.getWorld().obfuscated) { + int initialRadius = 1; + int index = 0; + int startX = chunkX << 4; + int startZ = chunkY << 4; + int iterator = 0; + for (int i = 0; i < 16; i++) { + // If the bitmask indicates this chunk is sent... + if ((bitmask & 1 << i) != 0) { + for (int y = 0; y < 16; y++) { + for (int z = 0; z < 16; z++) { + for (int x = 0; x < 16; x++) { + byte data = buffer[index]; + // Check if the block should be obfuscated for the default engine modes + if (obfuscateBlocks[data & 0xFF]) { + if (initialRadius != 0 && !isWorldLoaded(world, startX + x, (i << 4) + y, startZ + z, initialRadius)) { + continue; + } + if (initialRadius == 0 || !areAjacentBlocksTransparent(world, startX + x, (i << 4) + y, startZ + z, initialRadius)) { + if (world.getServer().orebfuscatorEngineMode == 2) { + // Replace with random ore. + if (iterator >= ores.length) { + iterator = 0; + } + buffer[index] = (byte) (int) ores[iterator++]; + } else { + if (world.getServer().orebfuscatorEngineMode == 1) { + // Replace with stone + buffer[index] = (byte) Block.STONE.id; + } + } + } + } + if (++index >= buffer.length) { + return; + } + } + } + } + } + } + } + } + + private static void updateNearbyBlocks(World world, int x, int y, int z, int radius) { + if (world.getWorld().obfuscated && world.isLoaded(x, y, z)) { + // Get block id + int id = world.getTypeId(x, y, z); + + // See if it needs update + if (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); + 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))); + } + + 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))); + } +} diff --git a/src/main/resources/configurations/bukkit.yml b/src/main/resources/configurations/bukkit.yml index 10ef7f2..e08325d 100644 --- a/src/main/resources/configurations/bukkit.yml +++ b/src/main/resources/configurations/bukkit.yml @@ -82,3 +82,9 @@ database: driver: org.sqlite.JDBC password: walrus url: jdbc:sqlite:{DIR}{NAME}.db +orebfuscator: + enable: false + engine-mode: 1 + disabled-worlds: + - world_the_end + blocks: [1, 5, 14, 15, 16, 21, 48, 49, 54, 56, 73, 74, 82, 129, 130] -- 1.8.2.1