Paper/CraftBukkit-Patches/0018-Add-oreobfuscator-for-Spigot.patch

359 lines
17 KiB
Diff
Raw Normal View History

2013-02-26 01:43:00 +01:00
From f7efe872d9e6c29ebdc4f8c9857a8724eee1300d Mon Sep 17 00:00:00 2001
From: lishid <lishid@gmail.com>
2013-02-16 00:13:00 +01:00
Date: Sat, 16 Feb 2013 10:05:25 +1100
Subject: [PATCH] Add oreobfuscator for Spigot.
---
.../net/minecraft/server/EntityFallingBlock.java | 1 +
2013-02-16 00:13:00 +01:00
src/main/java/net/minecraft/server/Explosion.java | 1 +
.../net/minecraft/server/Packet51MapChunk.java | 1 +
2013-02-26 01:43:00 +01:00
.../net/minecraft/server/Packet56MapChunkBulk.java | 21 ++-
2013-02-16 00:13:00 +01:00
.../minecraft/server/PlayerInteractManager.java | 5 +
.../java/org/bukkit/craftbukkit/CraftServer.java | 6 +
.../java/org/bukkit/craftbukkit/CraftWorld.java | 4 +
2013-02-26 01:43:00 +01:00
.../bukkit/craftbukkit/OrebfuscatorManager.java | 146 +++++++++++++++++++++
2013-02-16 00:13:00 +01:00
src/main/java/org/bukkit/craftbukkit/Spigot.java | 8 ++
src/main/resources/configurations/bukkit.yml | 6 +
2013-02-26 01:43:00 +01:00
10 files changed, 198 insertions(+), 1 deletion(-)
create mode 100644 src/main/java/org/bukkit/craftbukkit/OrebfuscatorManager.java
diff --git a/src/main/java/net/minecraft/server/EntityFallingBlock.java b/src/main/java/net/minecraft/server/EntityFallingBlock.java
index ea440dc..e86b10f 100644
--- a/src/main/java/net/minecraft/server/EntityFallingBlock.java
+++ b/src/main/java/net/minecraft/server/EntityFallingBlock.java
@@ -103,6 +103,7 @@ public class EntityFallingBlock extends Entity {
// CraftBukkit end
this.world.setTypeId(i, j, k, 0);
+ org.bukkit.craftbukkit.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 ba2f88f..8d5b1d8 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.bukkit.craftbukkit.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
2013-02-16 00:13:00 +01:00
index b51d90c..230dd62 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;
2013-02-16 00:13:00 +01:00
+ org.bukkit.craftbukkit.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 867ebd3..9d5cee7 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,9 @@ 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
+ // Moved to compress()
if (buildBuffer.length < j + chunkmap.a.length) {
byte[] abyte = new byte[j + chunkmap.a.length];
@@ -53,7 +57,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 +86,21 @@ public class Packet56MapChunkBulk extends Packet {
return;
}
+ // Spigot (Orebfuscator) start - Obfuscate chunks
+ int finalBufferSize = 0;
+ for (int i = 0; i < a.length; i++) {
+ org.bukkit.craftbukkit.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 5faee12..55f9ffa 100644
--- a/src/main/java/net/minecraft/server/PlayerInteractManager.java
+++ b/src/main/java/net/minecraft/server/PlayerInteractManager.java
@@ -291,6 +291,11 @@ public class PlayerInteractManager {
}
return false;
}
+ // Spigot (Orebfuscator) start
+ else {
+ org.bukkit.craftbukkit.OrebfuscatorManager.updateNearbyBlocks(world, i, j, k);
+ }
+ // Spigot (Orebfuscator) end
}
if (false) { // Never trigger
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 257497e..1a3cc03 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
2013-02-16 00:13:00 +01:00
@@ -163,6 +163,12 @@ public final class CraftServer implements Server {
private WarningState warningState = WarningState.DEFAULT;
private final BooleanWrapper online = new BooleanWrapper();
+ // Orebfuscator use
+ public boolean orebfuscatorEnabled = false;
2013-02-16 00:13:00 +01:00
+ public int orebfuscatorEngineMode = 1;
+ public int orebfuscatorUpdateRadius = 2;
+ public List<String> orebfuscatorDisabledWorlds;
+
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 94e07fe..21bd64a 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -119,6 +119,8 @@ public class CraftWorld implements World {
viewDistance = Bukkit.getServer().getViewDistance();
viewDistance = configuration.getInt("world-settings." + name + ".view-distance", viewDistance);
+ obfuscated = !world.getServer().orebfuscatorDisabledWorlds.contains(name);
+
server.getLogger().info("-------------- Spigot ----------------");
server.getLogger().info("-------- World Settings For [" + name + "] --------");
server.getLogger().info("Growth Per Chunk: " + growthPerTick);
@@ -135,6 +137,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
}
@@ -146,6 +149,7 @@ public class CraftWorld implements World {
public int mobSpawnRange = 4;
public int aggregateTicks = 4;
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/OrebfuscatorManager.java b/src/main/java/org/bukkit/craftbukkit/OrebfuscatorManager.java
new file mode 100644
2013-02-26 01:43:00 +01:00
index 0000000..fe14f6d
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/OrebfuscatorManager.java
2013-02-26 01:43:00 +01:00
@@ -0,0 +1,146 @@
+package org.bukkit.craftbukkit;
+
2013-02-16 00:13:00 +01:00
+import java.util.ArrayList;
+import java.util.List;
+import net.minecraft.server.Block;
+import net.minecraft.server.World;
2013-02-16 00:13:00 +01:00
+import org.bukkit.event.CustomTimingsHandler;
+
+public class OrebfuscatorManager {
+
+ // Used to keep track of which blocks to obfuscate
2013-02-16 00:13:00 +01:00
+ 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");
+ private static int ITERATOR = 0;
+
+ // Default blocks
+ static {
+ obfuscateBlocks[Block.STONE.id] = true;
+ obfuscateBlocks[Block.GOLD_ORE.id] = true;
+ obfuscateBlocks[Block.IRON_ORE.id] = true;
+ obfuscateBlocks[Block.COAL_ORE.id] = true;
+ obfuscateBlocks[Block.LAPIS_ORE.id] = true;
+ obfuscateBlocks[Block.CHEST.id] = true;
+ obfuscateBlocks[Block.DIAMOND_ORE.id] = true;
+ obfuscateBlocks[Block.REDSTONE_ORE.id] = true;
+ obfuscateBlocks[Block.GLOWING_REDSTONE_ORE.id] = true;
+ obfuscateBlocks[Block.EMERALD_ORE.id] = true;
+ obfuscateBlocks[Block.ENDER_CHEST.id] = true;
2013-02-16 00:13:00 +01:00
+
+ List<Byte> blocks = new ArrayList<Byte>();
+ for (int i = 0; i < obfuscateBlocks.length; i++) {
+ if (obfuscateBlocks[i]) {
2013-02-16 00:24:25 +01:00
+ if (i != Block.STONE.id && i != Block.CHEST.id && i != Block.ENDER_CHEST.id) {
+ blocks.add((byte) i);
+ }
2013-02-16 00:13:00 +01:00
+ }
+ }
+ ores = blocks.toArray(new Byte[blocks.size()]);
+ }
+
+ public static void updateNearbyBlocks(World world, int x, int y, int z) {
2013-02-16 00:13:00 +01:00
+ update.startTiming();
+ updateNearbyBlocks(world, x, y, z, world.getServer().orebfuscatorUpdateRadius);
2013-02-16 00:13:00 +01:00
+ 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.getServer().orebfuscatorEnabled && world.getWorld().obfuscated) {
+ int initialRadius = 1;
+ int index = 0;
+ int startX = chunkX << 4;
+ int startZ = chunkY << 4;
+ 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]) {
2013-02-26 01:43:00 +01:00
+ 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)) {
2013-02-16 00:13:00 +01:00
+ 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;
+ }
+ }
+ }
+ }
2013-01-24 09:28:22 +01:00
+ if (++index >= buffer.length) {
2013-01-23 23:13:53 +01:00
+ return;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ private static void updateNearbyBlocks(World world, int x, int y, int z, int radius) {
+ if (world.getServer().orebfuscatorEnabled && 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);
+ }
+ }
+ }
2013-02-26 01:43:00 +01:00
+
+ 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.i(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/java/org/bukkit/craftbukkit/Spigot.java b/src/main/java/org/bukkit/craftbukkit/Spigot.java
index 4a4f949..3bfeb49 100644
--- a/src/main/java/org/bukkit/craftbukkit/Spigot.java
+++ b/src/main/java/org/bukkit/craftbukkit/Spigot.java
@@ -13,6 +13,14 @@ public class Spigot {
server.commandComplete = configuration.getBoolean("settings.command-complete", true);
server.spamGuardExclusions = configuration.getStringList("settings.spam-exclusions");
+ server.orebfuscatorEnabled = configuration.getBoolean("orebfuscator.enable", false);
2013-02-16 00:13:00 +01:00
+ server.orebfuscatorEngineMode = configuration.getInt("orebfuscator.engine-mode", 1);
+ server.orebfuscatorUpdateRadius = configuration.getInt("orebfuscator.update-radius", 2);
+ server.orebfuscatorDisabledWorlds = configuration.getStringList("orebfuscator.disabled-worlds");
2013-02-16 00:13:00 +01:00
+ if (server.orebfuscatorEngineMode != 1 && server.orebfuscatorEngineMode != 2) {
+ server.orebfuscatorEngineMode = 1;
+ }
+
if (server.chunkGCPeriod == 0) {
2013-02-22 09:11:59 +01:00
server.getLogger().severe("[Spigot] You should not disable chunk-gc, unexpected behaviour may occur!");
}
diff --git a/src/main/resources/configurations/bukkit.yml b/src/main/resources/configurations/bukkit.yml
index 0c92b66..78e9a66 100644
--- a/src/main/resources/configurations/bukkit.yml
+++ b/src/main/resources/configurations/bukkit.yml
@@ -81,3 +81,9 @@ database:
driver: org.sqlite.JDBC
password: walrus
url: jdbc:sqlite:{DIR}{NAME}.db
+orebfuscator:
+ enable: false
2013-02-16 00:13:00 +01:00
+ engine-mode: 1
+ update-radius: 2
+ disabled-worlds:
+ - world_the_end
--
2013-02-22 09:11:59 +01:00
1.8.1-rc2