Fix for PaperSpigot and issue #188

master
Aleksey-Terzi 2018-10-24 18:00:08 +03:00
parent 972286af6c
commit b5fd5c57e7
5 changed files with 176 additions and 152 deletions

View File

@ -4,7 +4,7 @@
<groupId>com.lishid</groupId> <groupId>com.lishid</groupId>
<artifactId>orebfuscator</artifactId> <artifactId>orebfuscator</artifactId>
<version>4.4.1-SNAPSHOT</version> <version>4.4.2-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Orebfuscator4</name> <name>Orebfuscator4</name>

View File

@ -7,16 +7,25 @@ package com.lishid.orebfuscator.chunkmap;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Stack;
import com.lishid.orebfuscator.NmsInstance; import com.lishid.orebfuscator.NmsInstance;
public class ChunkMapManager { public class ChunkMapManager implements AutoCloseable {
private static final ThreadLocal<ChunkMapBuffer> _buffer = new ThreadLocal<ChunkMapBuffer>() { private static final Object _lock = new Object();
@Override private static final Stack<ChunkMapBuffer> _bufferStack = new Stack<>();
protected ChunkMapBuffer initialValue() {
return new ChunkMapBuffer(); private static ChunkMapBuffer popBuffer() {
synchronized(_lock) {
return _bufferStack.isEmpty() ? new ChunkMapBuffer() : _bufferStack.pop();
}
}
private static void pushBuffer(ChunkMapBuffer buffer) {
synchronized(_lock) {
_bufferStack.push(buffer);
}
} }
};
private ChunkMapBuffer buffer; private ChunkMapBuffer buffer;
private ChunkData chunkData; private ChunkData chunkData;
@ -42,41 +51,49 @@ public class ChunkMapManager {
return this.chunkData; return this.chunkData;
} }
public ChunkMapManager(ChunkData chunkData) { private ChunkMapManager() {
this.buffer = _buffer.get();
this.chunkData = chunkData;
} }
public void init() throws IOException { public static ChunkMapManager create(ChunkData chunkData) throws IOException {
this.reader = new ChunkReader(this.chunkData.data); ChunkMapManager manager = new ChunkMapManager();
this.sectionCount = 0; manager.chunkData = chunkData;
this.sectionIndex = -1; manager.buffer = popBuffer();
this.minX = this.chunkData.chunkX << 4; manager.reader = new ChunkReader(chunkData.data);
this.maxX = this.minX + 15; manager.sectionCount = 0;
this.minZ = this.chunkData.chunkZ << 4; manager.sectionIndex = -1;
this.maxZ = this.minZ + 15; manager.minX = chunkData.chunkX << 4;
manager.maxX = manager.minX + 15;
manager.minZ = chunkData.chunkZ << 4;
manager.maxZ = manager.minZ + 15;
this.buffer.lightArrayLength = 2048; manager.buffer.lightArrayLength = 2048;
if(this.chunkData.isOverworld) { if(chunkData.isOverworld) {
this.buffer.lightArrayLength <<= 1; manager.buffer.lightArrayLength <<= 1;
} }
this.buffer.writer.init(); manager.buffer.writer.init();
int mask = this.chunkData.primaryBitMask; int mask = chunkData.primaryBitMask;
while(mask != 0) { while(mask != 0) {
if((mask & 0x1) != 0) { if((mask & 0x1) != 0) {
this.sectionCount++; manager.sectionCount++;
} }
mask >>>= 1; mask >>>= 1;
} }
this.buffer.clearLayers(); manager.buffer.clearLayers();
moveToNextLayer(); manager.moveToNextLayer();
return manager;
}
public void close() throws Exception {
pushBuffer(this.buffer);
} }
public boolean inputHasNonAirBlock() { public boolean inputHasNonAirBlock() {

View File

@ -59,7 +59,7 @@ public class ChunkReader {
private void readLong() throws IOException { private void readLong() throws IOException {
if(this.byteIndex + 7 >= this.data.length) { if(this.byteIndex + 7 >= this.data.length) {
throw new IOException("No data to read."); throw new IOException("No data to read. byteIndex = " + this.byteIndex);
} }
this.buffer = ((this.data[this.byteIndex] & 0xffL) << 56) this.buffer = ((this.data[this.byteIndex] & 0xffL) << 56)
@ -83,7 +83,7 @@ public class ChunkReader {
value |= (b & 0x7F) << (size++ * 7); value |= (b & 0x7F) << (size++ * 7);
if(size > 5) { if(size > 5) {
throw new IOException("Invalid VarInt."); throw new IOException("Invalid VarInt. byteIndex = " + this.byteIndex + ", value = " + value + ", size = " + size);
} }
} }
@ -92,7 +92,7 @@ public class ChunkReader {
public int readByte() throws IOException { public int readByte() throws IOException {
if(this.byteIndex >= this.data.length) { if(this.byteIndex >= this.data.length) {
throw new IOException("No data to read."); throw new IOException("No data to read. byteIndex = " + this.byteIndex);
} }
return this.data[this.byteIndex++] & 0xff; return this.data[this.byteIndex++] & 0xff;

View File

@ -18,6 +18,7 @@ package com.lishid.orebfuscator.hook;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -50,6 +51,8 @@ public class ProtocolLibHook {
this.manager.addPacketListener(new PacketAdapter(plugin, PacketType.Play.Server.MAP_CHUNK) { this.manager.addPacketListener(new PacketAdapter(plugin, PacketType.Play.Server.MAP_CHUNK) {
@Override @Override
public void onPacketSending(PacketEvent event) { public void onPacketSending(PacketEvent event) {
ChunkData chunkData = null;
try { try {
Player player = event.getPlayer(); Player player = event.getPlayer();
@ -72,7 +75,7 @@ public class ProtocolLibHook {
List nmsTags = list.read(0); List nmsTags = list.read(0);
ChunkData chunkData = new ChunkData(); chunkData = new ChunkData();
chunkData.chunkX = ints.read(0); chunkData.chunkX = ints.read(0);
chunkData.chunkZ = ints.read(1); chunkData.chunkZ = ints.read(1);
chunkData.groundUpContinuous = bools.read(0); chunkData.groundUpContinuous = bools.read(0);
@ -83,7 +86,7 @@ public class ProtocolLibHook {
Calculations.Result result = Calculations.obfuscateOrUseCache(chunkData, player, worldConfig); Calculations.Result result = Calculations.obfuscateOrUseCache(chunkData, player, worldConfig);
if(result.output != null) { if(result != null && result.output != null) {
byteArray.write(0, result.output); byteArray.write(0, result.output);
if(nmsTags != null) { if(nmsTags != null) {
@ -92,6 +95,10 @@ public class ProtocolLibHook {
} }
} }
} catch (Exception e) { } catch (Exception e) {
if(chunkData != null) {
Orebfuscator.logger.log(Level.SEVERE, "ChunkX = " + chunkData.chunkX + ", chunkZ = " + chunkData.chunkZ);
}
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -156,7 +163,7 @@ public class ProtocolLibHook {
FileOutputStream fos; FileOutputStream fos;
try { try {
fos = new FileOutputStream("D:\\Temp\\chunk.dat"); fos = new FileOutputStream("D:\\Temp\\chunk_X" + chunkData.chunkX + "_Z" + chunkData.chunkZ + ".dat");
fos.write(chunkData.chunkX & 0xff); fos.write(chunkData.chunkX & 0xff);
fos.write((chunkData.chunkX >> 8) & 0xff); fos.write((chunkData.chunkX >> 8) & 0xff);
fos.write(chunkData.chunkZ & 0xff); fos.write(chunkData.chunkZ & 0xff);

View File

@ -48,7 +48,7 @@ public class Calculations {
private static Random random = new Random(); private static Random random = new Random();
public static Result obfuscateOrUseCache(ChunkData chunkData, Player player, WorldConfig worldConfig) throws IOException { public static Result obfuscateOrUseCache(ChunkData chunkData, Player player, WorldConfig worldConfig) throws Exception {
if(chunkData.primaryBitMask == 0) return null; if(chunkData.primaryBitMask == 0) return null;
byte[] output; byte[] output;
@ -137,7 +137,7 @@ public class Calculations {
Player player, Player player,
ArrayList<BlockCoord> proximityBlocks, ArrayList<BlockCoord> proximityBlocks,
ArrayList<BlockCoord> removedEntities ArrayList<BlockCoord> removedEntities
) throws IOException ) throws Exception
{ {
ProximityHiderConfig proximityHider = worldConfig.getProximityHiderConfig(); ProximityHiderConfig proximityHider = worldConfig.getProximityHiderConfig();
int initialRadius = Orebfuscator.config.getInitialRadius(); int initialRadius = Orebfuscator.config.getInitialRadius();
@ -149,7 +149,6 @@ public class Calculations {
int engineMode = Orebfuscator.config.getEngineMode(); int engineMode = Orebfuscator.config.getEngineMode();
int maxChance = worldConfig.getAirGeneratorMaxChance(); int maxChance = worldConfig.getAirGeneratorMaxChance();
int incrementMax = maxChance;
int randomBlocksLength = worldConfig.getRandomBlocks().length; int randomBlocksLength = worldConfig.getRandomBlocks().length;
boolean randomAlternate = false; boolean randomAlternate = false;
@ -157,15 +156,15 @@ public class Calculations {
int startX = chunkData.chunkX << 4; int startX = chunkData.chunkX << 4;
int startZ = chunkData.chunkZ << 4; int startZ = chunkData.chunkZ << 4;
ChunkMapManager manager = new ChunkMapManager(chunkData); byte[] output;
manager.init();
try (ChunkMapManager manager = ChunkMapManager.create(chunkData)) {
for (int i = 0; i < manager.getSectionCount(); i++) { for (int i = 0; i < manager.getSectionCount(); i++) {
worldConfig.shuffleRandomBlocks(); worldConfig.shuffleRandomBlocks();
for (int offsetY = 0; offsetY < 16; offsetY++) { for (int offsetY = 0; offsetY < 16; offsetY++) {
for (int offsetZ = 0; offsetZ < 16; offsetZ++) { for (int offsetZ = 0; offsetZ < 16; offsetZ++) {
incrementMax = (maxChance + random(maxChance)) / 2; int incrementMax = (maxChance + random(maxChance)) / 2;
for (int offsetX = 0; offsetX < 16; offsetX++) { for (int offsetX = 0; offsetX < 16; offsetX++) {
int blockData = manager.readNextBlock(); int blockData = manager.readNextBlock();
@ -279,7 +278,8 @@ public class Calculations {
manager.finalizeOutput(); manager.finalizeOutput();
byte[] output = manager.createOutput(); output = manager.createOutput();
}
ProximityHider.addProximityBlocks(player, chunkData.chunkX, chunkData.chunkZ, proximityBlocks); ProximityHider.addProximityBlocks(player, chunkData.chunkX, chunkData.chunkZ, proximityBlocks);