From 9576900832d0246b91a188c9f1fe37afd50a016f Mon Sep 17 00:00:00 2001 From: Aleksey-Terzi Date: Thu, 21 Apr 2016 17:48:25 +0300 Subject: [PATCH 1/5] Some refactoring --- pom.xml | 2 +- .../com/lishid/orebfuscator/Orebfuscator.java | 47 ++----------- .../orebfuscator/OrebfuscatorConfig.java | 25 ++----- .../cache/ObfuscatedDataCache.java | 69 +++++++++++-------- .../commands/OrebfuscatorCommandExecutor.java | 10 ++- .../orebfuscator/hook/ProtocolLibHook.java | 1 - .../orebfuscator/internal/ChunkCache.java | 4 +- .../listeners/OrebfuscatorBlockListener.java | 13 ++-- 8 files changed, 73 insertions(+), 98 deletions(-) diff --git a/pom.xml b/pom.xml index 6e78221d..73abe327 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.lishid orebfuscator - 4.0.5-SNAPSHOT + 4.0.6-SNAPSHOT jar Orebfuscator4 diff --git a/src/main/java/com/lishid/orebfuscator/Orebfuscator.java b/src/main/java/com/lishid/orebfuscator/Orebfuscator.java index aa4ccef2..be3335fb 100644 --- a/src/main/java/com/lishid/orebfuscator/Orebfuscator.java +++ b/src/main/java/com/lishid/orebfuscator/Orebfuscator.java @@ -16,9 +16,6 @@ package com.lishid.orebfuscator; -import java.io.File; -import java.io.IOException; -import java.util.Objects; import java.util.logging.Logger; import org.bukkit.ChatColor; @@ -34,7 +31,6 @@ import com.lishid.orebfuscator.listeners.OrebfuscatorBlockListener; import com.lishid.orebfuscator.listeners.OrebfuscatorEntityListener; import com.lishid.orebfuscator.listeners.OrebfuscatorPlayerListener; -import com.lishid.orebfuscator.utils.FileHelper; /** * Orebfuscator Anti X-RAY @@ -45,8 +41,6 @@ public class Orebfuscator extends JavaPlugin { public static final Logger logger = Logger.getLogger("Minecraft.OFC"); public static Orebfuscator instance; - public static boolean usePL = false; - public static boolean useSpigot = false; @Override public void onEnable() { @@ -56,50 +50,23 @@ public void onEnable() { instance = this; // Load configurations OrebfuscatorConfig.load(); - - //Make sure cache is cleared if config was changed since last start - checkCacheAndConfigSynchronized(); + + if (pm.getPlugin("ProtocolLib") == null) { + Orebfuscator.log("ProtocolLib is not found! Plugin cannot be enabled."); + return; + } // Orebfuscator events pm.registerEvents(new OrebfuscatorPlayerListener(), this); pm.registerEvents(new OrebfuscatorEntityListener(), this); pm.registerEvents(new OrebfuscatorBlockListener(), this); - if (pm.getPlugin("ProtocolLib") != null) { - Orebfuscator.log("ProtocolLib found! Hooking into ProtocolLib."); - (new ProtocolLibHook()).register(this); - usePL = true; - } - } - - private void checkCacheAndConfigSynchronized() { - String configContent = getConfig().saveToString(); - - File cacheFolder = OrebfuscatorConfig.getCacheFolder(); - File cacheConfigFile = new File(cacheFolder, "cache_config.yml"); - - try { - String cacheConfigContent = FileHelper.readFile(cacheConfigFile); - - if(Objects.equals(configContent, cacheConfigContent)) return; - - Orebfuscator.log("Clear cache."); - - if(cacheFolder.exists()) { - FileHelper.delete(cacheFolder); - } - - cacheFolder.mkdirs(); - - getConfig().save(cacheConfigFile); - } catch (IOException e) { - e.printStackTrace(); - } + (new ProtocolLibHook()).register(this); } @Override public void onDisable() { - ObfuscatedDataCache.clearCache(); + ObfuscatedDataCache.closeCacheFiles(); BlockHitManager.clearAll(); getServer().getScheduler().cancelTasks(this); } diff --git a/src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java b/src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java index 1391b6a8..5c917784 100644 --- a/src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java +++ b/src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java @@ -378,24 +378,6 @@ public static void load() { // Version check int version = getInt("ConfigVersion", CONFIG_VERSION); if (version < CONFIG_VERSION) { - // Orebfuscator.log("Configuration out of date. Recreating new configuration file."); - // File configFile = new File(Orebfuscator.instance.getDataFolder(), "config.yml"); - // File destination = new File(Orebfuscator.instance.getDataFolder(), "config_old.yml"); - // if (destination.exists()) - // { - // try - // { - // destination.delete(); - // } - // catch (Exception e) - // { - // Orebfuscator.log(e); - // } - // } - // configFile.renameTo(destination); - // reload(); - - ObfuscatedDataCache.ClearCache(); setData("ConfigVersion", CONFIG_VERSION); } @@ -460,6 +442,13 @@ public static void load() { save(); createPaletteBlocks(); + + //Make sure cache is cleared if config was changed since last start + try { + ObfuscatedDataCache.checkCacheAndConfigSynchronized(); + } catch (IOException e) { + e.printStackTrace(); + } } private static void createPaletteBlocks() { diff --git a/src/main/java/com/lishid/orebfuscator/cache/ObfuscatedDataCache.java b/src/main/java/com/lishid/orebfuscator/cache/ObfuscatedDataCache.java index 0820d705..3792a5f6 100644 --- a/src/main/java/com/lishid/orebfuscator/cache/ObfuscatedDataCache.java +++ b/src/main/java/com/lishid/orebfuscator/cache/ObfuscatedDataCache.java @@ -16,15 +16,19 @@ package com.lishid.orebfuscator.cache; -import com.lishid.orebfuscator.Orebfuscator; -import com.lishid.orebfuscator.OrebfuscatorConfig; -import com.lishid.orebfuscator.internal.ChunkCache; - import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; +import java.io.IOException; +import java.util.Objects; + +import com.lishid.orebfuscator.Orebfuscator; +import com.lishid.orebfuscator.OrebfuscatorConfig; +import com.lishid.orebfuscator.internal.ChunkCache; +import com.lishid.orebfuscator.utils.FileHelper; public class ObfuscatedDataCache { + private static final String cacheFileName = "cache_config.yml"; private static ChunkCache internalCache; private static ChunkCache getInternalCache() { @@ -34,8 +38,37 @@ private static ChunkCache getInternalCache() { return internalCache; } - public static void clearCache() { - getInternalCache().clearCache(); + public static void closeCacheFiles() { + getInternalCache().closeCacheFiles(); + } + + public static void checkCacheAndConfigSynchronized() throws IOException { + String configContent = Orebfuscator.instance.getConfig().saveToString(); + + File cacheFolder = OrebfuscatorConfig.getCacheFolder(); + File cacheConfigFile = new File(cacheFolder, cacheFileName); + String cacheConfigContent = FileHelper.readFile(cacheConfigFile); + + if(Objects.equals(configContent, cacheConfigContent)) return; + + clearCache(); + } + + public static void clearCache() throws IOException { + closeCacheFiles(); + + File cacheFolder = OrebfuscatorConfig.getCacheFolder(); + File cacheConfigFile = new File(cacheFolder, cacheFileName); + + if(cacheFolder.exists()) { + FileHelper.delete(cacheFolder); + } + + Orebfuscator.log("Cache cleared."); + + cacheFolder.mkdirs(); + + Orebfuscator.instance.getConfig().save(cacheConfigFile); } public static DataInputStream getInputStream(File folder, int x, int z) { @@ -45,28 +78,4 @@ public static DataInputStream getInputStream(File folder, int x, int z) { public static DataOutputStream getOutputStream(File folder, int x, int z) { return getInternalCache().getOutputStream(folder, x, z); } - - public static void ClearCache() { - getInternalCache().clearCache(); - try { - DeleteDir(OrebfuscatorConfig.getCacheFolder()); - } catch (Exception e) { - Orebfuscator.log(e); - } - } - - private static void DeleteDir(File dir) { - try { - if (!dir.exists()) - return; - - if (dir.isDirectory()) - for (File f : dir.listFiles()) - DeleteDir(f); - - dir.delete(); - } catch (Exception e) { - Orebfuscator.log(e); - } - } } \ No newline at end of file diff --git a/src/main/java/com/lishid/orebfuscator/commands/OrebfuscatorCommandExecutor.java b/src/main/java/com/lishid/orebfuscator/commands/OrebfuscatorCommandExecutor.java index e0b4f897..a0a76639 100644 --- a/src/main/java/com/lishid/orebfuscator/commands/OrebfuscatorCommandExecutor.java +++ b/src/main/java/com/lishid/orebfuscator/commands/OrebfuscatorCommandExecutor.java @@ -16,6 +16,8 @@ package com.lishid.orebfuscator.commands; +import java.io.IOException; + import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -179,8 +181,12 @@ else if (args[0].equalsIgnoreCase("status")) { } else if (args[0].equalsIgnoreCase("clearcache")) { - ObfuscatedDataCache.ClearCache(); - Orebfuscator.message(sender, "Cache cleared."); + try { + ObfuscatedDataCache.clearCache(); + Orebfuscator.message(sender, "Cache cleared."); + } catch (IOException e) { + e.printStackTrace(); + } } else if (args[0].equalsIgnoreCase("debug")) { diff --git a/src/main/java/com/lishid/orebfuscator/hook/ProtocolLibHook.java b/src/main/java/com/lishid/orebfuscator/hook/ProtocolLibHook.java index 86c5a18f..590fefde 100644 --- a/src/main/java/com/lishid/orebfuscator/hook/ProtocolLibHook.java +++ b/src/main/java/com/lishid/orebfuscator/hook/ProtocolLibHook.java @@ -28,7 +28,6 @@ import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.reflect.StructureModifier; -import com.lishid.orebfuscator.Orebfuscator; import com.lishid.orebfuscator.chunkmap.ChunkData; import com.lishid.orebfuscator.hithack.BlockHitManager; import com.lishid.orebfuscator.obfuscation.Calculations; diff --git a/src/main/java/com/lishid/orebfuscator/internal/ChunkCache.java b/src/main/java/com/lishid/orebfuscator/internal/ChunkCache.java index b535dce1..01840f24 100644 --- a/src/main/java/com/lishid/orebfuscator/internal/ChunkCache.java +++ b/src/main/java/com/lishid/orebfuscator/internal/ChunkCache.java @@ -44,7 +44,7 @@ private synchronized RegionFile getRegionFile(File folder, int x, int z) { } if (cachedRegionFiles.size() >= OrebfuscatorConfig.MaxLoadedCacheFiles) { - clearCache(); + closeCacheFiles(); } regionFile = new RegionFile(file); @@ -73,7 +73,7 @@ public DataOutputStream getOutputStream(File folder, int x, int z) { return regionFile.b(x & 0x1F, z & 0x1F); } - public synchronized void clearCache() { + public synchronized void closeCacheFiles() { for (RegionFile regionFile : cachedRegionFiles.values()) { try { if (regionFile != null) diff --git a/src/main/java/com/lishid/orebfuscator/listeners/OrebfuscatorBlockListener.java b/src/main/java/com/lishid/orebfuscator/listeners/OrebfuscatorBlockListener.java index bed0c608..4b6ad1e7 100644 --- a/src/main/java/com/lishid/orebfuscator/listeners/OrebfuscatorBlockListener.java +++ b/src/main/java/com/lishid/orebfuscator/listeners/OrebfuscatorBlockListener.java @@ -16,15 +16,20 @@ package com.lishid.orebfuscator.listeners; -import com.lishid.orebfuscator.OrebfuscatorConfig; -import com.lishid.orebfuscator.hithack.BlockHitManager; -import com.lishid.orebfuscator.obfuscation.BlockUpdate; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.block.*; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockDamageEvent; +import org.bukkit.event.block.BlockPhysicsEvent; +import org.bukkit.event.block.BlockPistonExtendEvent; +import org.bukkit.event.block.BlockPistonRetractEvent; + +import com.lishid.orebfuscator.OrebfuscatorConfig; +import com.lishid.orebfuscator.hithack.BlockHitManager; +import com.lishid.orebfuscator.obfuscation.BlockUpdate; public class OrebfuscatorBlockListener implements Listener { From 42b5e9e2b5c67bba14310f57b0e23f93248cb629 Mon Sep 17 00:00:00 2001 From: Aleksey-Terzi Date: Sat, 23 Apr 2016 13:05:36 +0300 Subject: [PATCH 2/5] Mark lava as transparent block, small fixes. --- .../com/lishid/orebfuscator/Orebfuscator.java | 9 +++- .../commands/OrebfuscatorCommandExecutor.java | 7 ++- .../obfuscation/Calculations.java | 54 ++++++++++--------- .../resources/transparent_blocks.txt | 10 ++-- 4 files changed, 47 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/lishid/orebfuscator/Orebfuscator.java b/src/main/java/com/lishid/orebfuscator/Orebfuscator.java index be3335fb..7f08ffcf 100644 --- a/src/main/java/com/lishid/orebfuscator/Orebfuscator.java +++ b/src/main/java/com/lishid/orebfuscator/Orebfuscator.java @@ -41,6 +41,11 @@ public class Orebfuscator extends JavaPlugin { public static final Logger logger = Logger.getLogger("Minecraft.OFC"); public static Orebfuscator instance; + + private boolean isProtocolLibFound; + public boolean getIsProtocolLibFound() { + return this.isProtocolLibFound; + } @Override public void onEnable() { @@ -50,8 +55,10 @@ public void onEnable() { instance = this; // Load configurations OrebfuscatorConfig.load(); + + this.isProtocolLibFound = pm.getPlugin("ProtocolLib") != null; - if (pm.getPlugin("ProtocolLib") == null) { + if (!this.isProtocolLibFound) { Orebfuscator.log("ProtocolLib is not found! Plugin cannot be enabled."); return; } diff --git a/src/main/java/com/lishid/orebfuscator/commands/OrebfuscatorCommandExecutor.java b/src/main/java/com/lishid/orebfuscator/commands/OrebfuscatorCommandExecutor.java index a0a76639..f1ed8a23 100644 --- a/src/main/java/com/lishid/orebfuscator/commands/OrebfuscatorCommandExecutor.java +++ b/src/main/java/com/lishid/orebfuscator/commands/OrebfuscatorCommandExecutor.java @@ -167,11 +167,16 @@ else if (args[0].equalsIgnoreCase("reload")) { } else if (args[0].equalsIgnoreCase("status")) { - Orebfuscator.message(sender, "Orebfuscator " + Orebfuscator.instance.getDescription().getVersion() + " is: " + (OrebfuscatorConfig.Enabled ? "Enabled" : "Disabled")); + String status = Orebfuscator.instance.getIsProtocolLibFound() + ? (OrebfuscatorConfig.Enabled ? "Enabled" : "Disabled") + : "ProtocolLib is not found! Plugin cannot be enabled."; + + Orebfuscator.message(sender, "Orebfuscator " + Orebfuscator.instance.getDescription().getVersion() + " is: " + status); Orebfuscator.message(sender, "EngineMode: " + OrebfuscatorConfig.EngineMode); Orebfuscator.message(sender, "Caching: " + (OrebfuscatorConfig.UseCache ? "Enabled" : "Disabled")); Orebfuscator.message(sender, "ProximityHider: " + (OrebfuscatorConfig.UseProximityHider ? "Enabled" : "Disabled")); + Orebfuscator.message(sender, "DarknessHideBlocks: " + (OrebfuscatorConfig.DarknessHideBlocks ? "Enabled": "Disabled")); Orebfuscator.message(sender, "Initial Obfuscation Radius: " + OrebfuscatorConfig.InitialRadius); Orebfuscator.message(sender, "Update Radius: " + OrebfuscatorConfig.UpdateRadius); diff --git a/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java b/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java index 7aae6964..492e74be 100644 --- a/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java +++ b/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java @@ -175,7 +175,7 @@ private static byte[] Obfuscate(ChunkData chunkData, Player player, ArrayList= world.getMaxHeight() || y < 0) return true; - ChunkData chunkData = manager.getChunkData(); - int blockData = manager.get(x, y, z); - int id; - - if (blockData < 0) { - if (CalculationsUtil.isChunkLoaded(world, chunkData.chunkX, chunkData.chunkZ)) { - id = world.getBlockTypeIdAt(x, y, z); - } else { - id = 1; - chunkData.useCache = false; - } - } else { - id = ChunkMapManager.getBlockIdFromData(blockData); - } - - if (id != currentBlockID && OrebfuscatorConfig.isBlockTransparent(id)) { - return true; + if(checkCurrentBlock) { + ChunkData chunkData = manager.getChunkData(); + int blockData = manager.get(x, y, z); + int id; + + if (blockData < 0) { + if (CalculationsUtil.isChunkLoaded(world, chunkData.chunkX, chunkData.chunkZ)) { + id = world.getBlockTypeIdAt(x, y, z); + } else { + id = 1; + chunkData.useCache = false; + } + } else { + id = ChunkMapManager.getBlockIdFromData(blockData); + } + + if (OrebfuscatorConfig.isBlockTransparent(id)) { + return true; + } } if (countdown == 0) return false; - if (areAjacentBlocksTransparent(manager, world, currentBlockID, x, y + 1, z, countdown - 1)) + if (areAjacentBlocksTransparent(manager, world, true, x, y + 1, z, countdown - 1)) return true; - if (areAjacentBlocksTransparent(manager, world, currentBlockID, x, y - 1, z, countdown - 1)) + if (areAjacentBlocksTransparent(manager, world, true, x, y - 1, z, countdown - 1)) return true; - if (areAjacentBlocksTransparent(manager, world, currentBlockID, x + 1, y, z, countdown - 1)) + if (areAjacentBlocksTransparent(manager, world, true, x + 1, y, z, countdown - 1)) return true; - if (areAjacentBlocksTransparent(manager, world, currentBlockID, x - 1, y, z, countdown - 1)) + if (areAjacentBlocksTransparent(manager, world, true, x - 1, y, z, countdown - 1)) return true; - if (areAjacentBlocksTransparent(manager, world, currentBlockID, x, y, z + 1, countdown - 1)) + if (areAjacentBlocksTransparent(manager, world, true, x, y, z + 1, countdown - 1)) return true; - if (areAjacentBlocksTransparent(manager, world, currentBlockID, x, y, z - 1, countdown - 1)) + if (areAjacentBlocksTransparent(manager, world, true, x, y, z - 1, countdown - 1)) return true; return false; diff --git a/src/main/resources/resources/transparent_blocks.txt b/src/main/resources/resources/transparent_blocks.txt index b2d14fbc..617ac88a 100644 --- a/src/main/resources/resources/transparent_blocks.txt +++ b/src/main/resources/resources/transparent_blocks.txt @@ -9,7 +9,7 @@ 8:true //minecraft:flowing_water 9:true //minecraft:water 10:true //minecraft:flowing_lava -11:false //minecraft:lava +11:true //minecraft:lava 12:false //minecraft:sand 13:false //minecraft:gravel 14:false //minecraft:gold_ore @@ -118,7 +118,7 @@ 117:true //minecraft:brewing_stand 118:true //minecraft:cauldron 119:true //minecraft:end_portal -120:false //minecraft:end_portal_frame +120:true //minecraft:end_portal_frame 121:false //minecraft:end_stone 122:true //minecraft:dragon_egg 123:false //minecraft:redstone_lamp @@ -164,7 +164,7 @@ 163:true //minecraft:acacia_stairs 164:true //minecraft:dark_oak_stairs 165:true //minecraft:slime -166:false //minecraft:barrier +166:true //minecraft:barrier 167:true //minecraft:iron_trapdoor 168:false //minecraft:prismarine 169:false //minecraft:sea_lantern @@ -206,8 +206,8 @@ 205:true //minecraft:purpur_slab 206:false //minecraft:end_bricks 207:true //minecraft:beetroots -208:false //minecraft:grass_path -209:false //minecraft:end_gateway +208:true //minecraft:grass_path +209:true //minecraft:end_gateway 210:false //minecraft:repeating_command_block 211:false //minecraft:chain_command_block 212:true //minecraft:frosted_ice From 7f6ad9791a8a774feb25f1ae72c279cd423eebd2 Mon Sep 17 00:00:00 2001 From: Aleksey-Terzi Date: Sat, 23 Apr 2016 19:57:57 +0300 Subject: [PATCH 3/5] Some improvements for ProximityHider --- .../orebfuscator/OrebfuscatorConfig.java | 5 +- .../chunkmap/ChunkMapManager.java | 4 + .../internal/MinecraftInternals.java | 14 +- .../obfuscation/Calculations.java | 42 +++--- .../obfuscation/ProximityHider.java | 128 +++++++++--------- .../obfuscation/ProximityHiderBlock.java | 42 ++++++ .../obfuscation/ProximityHiderPlayer.java | 13 +- 7 files changed, 155 insertions(+), 93 deletions(-) create mode 100644 src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderBlock.java diff --git a/src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java b/src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java index 5c917784..8eddae4f 100644 --- a/src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java +++ b/src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java @@ -29,7 +29,6 @@ import org.bukkit.Bukkit; import org.bukkit.World; -import org.bukkit.block.Block; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; @@ -37,7 +36,7 @@ public class OrebfuscatorConfig { // Constant/persistent data - private static final int CONFIG_VERSION = 10; + private static final int CONFIG_VERSION = 11; private static Random random = new Random(); private static int AvailableProcessors = Runtime.getRuntime().availableProcessors(); @@ -186,7 +185,7 @@ public static boolean skipProximityHiderCheck(int y) { return UseYLocationProximity && y < ProximityHiderEnd; } - public static boolean proximityHiderDeobfuscate(int playerY, Block block) { + public static boolean proximityHiderDeobfuscate() { return UseYLocationProximity; } diff --git a/src/main/java/com/lishid/orebfuscator/chunkmap/ChunkMapManager.java b/src/main/java/com/lishid/orebfuscator/chunkmap/ChunkMapManager.java index 81b5cbfd..2275d9ea 100644 --- a/src/main/java/com/lishid/orebfuscator/chunkmap/ChunkMapManager.java +++ b/src/main/java/com/lishid/orebfuscator/chunkmap/ChunkMapManager.java @@ -90,6 +90,10 @@ public static int getBlockIdFromData(int blockData) { return blockData >>> 4; } + public static int getBlockMetaFromData(int blockData) { + return blockData & 0xf; + } + public static int blockStateToData(BlockState blockState) { return (blockState.id << 4) | blockState.meta; } diff --git a/src/main/java/com/lishid/orebfuscator/internal/MinecraftInternals.java b/src/main/java/com/lishid/orebfuscator/internal/MinecraftInternals.java index 421b83b0..c8403e59 100644 --- a/src/main/java/com/lishid/orebfuscator/internal/MinecraftInternals.java +++ b/src/main/java/com/lishid/orebfuscator/internal/MinecraftInternals.java @@ -21,6 +21,7 @@ import net.minecraft.server.v1_9_R1.Packet; import net.minecraft.server.v1_9_R1.TileEntity; +import org.bukkit.Material; import org.bukkit.craftbukkit.v1_9_R1.CraftChunk; import org.bukkit.craftbukkit.v1_9_R1.CraftWorld; import org.bukkit.craftbukkit.v1_9_R1.block.CraftBlock; @@ -30,9 +31,9 @@ //Volatile public class MinecraftInternals { - public static void updateBlockTileEntity(org.bukkit.block.Block block, Player player) { - CraftWorld world = (CraftWorld) block.getWorld(); - TileEntity tileEntity = world.getTileEntityAt(block.getX(), block.getY(), block.getZ()); + public static void updateBlockTileEntity(int blockX, int blockY, int blockZ, Player player) { + CraftWorld world = (CraftWorld) player.getWorld(); + TileEntity tileEntity = world.getTileEntityAt(blockX, blockY, blockZ); if (tileEntity == null) { return; } @@ -49,4 +50,11 @@ public static void notifyBlockChange(org.bukkit.World world, CraftBlock block) { ((CraftWorld) world).getHandle().notify(blockPosition, blockData, blockData, 0); } + + public static boolean isSign(int blockId) { + return blockId == Material.CHORUS_FLOWER.getId() + || blockId == Material.CLAY_BALL.getId() + || blockId == Material.PRISMARINE_SHARD.getId() + ; + } } diff --git a/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java b/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java index 492e74be..c2fb71d0 100644 --- a/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java +++ b/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java @@ -28,8 +28,6 @@ import org.bukkit.World; import org.bukkit.World.Environment; -import org.bukkit.block.Block; -import org.bukkit.block.Sign; import org.bukkit.entity.Player; import com.lishid.orebfuscator.OrebfuscatorConfig; @@ -37,6 +35,7 @@ import com.lishid.orebfuscator.chunkmap.BlockState; import com.lishid.orebfuscator.chunkmap.ChunkData; import com.lishid.orebfuscator.chunkmap.ChunkMapManager; +import com.lishid.orebfuscator.internal.MinecraftInternals; public class Calculations { @@ -64,11 +63,11 @@ public static Set getSignsList(Player player, int chunkX, int ch return map.get(address); } - public static void putSignsList(Player player, int chunkX, int chunkZ, List proximityBlocks) { + public static void putSignsList(Player player, int chunkX, int chunkZ, List proximityBlocks) { Set signs = new HashSet(); - for (Block b : proximityBlocks) { - if (b.getState() instanceof Sign) { - signs.add(new MinecraftBlock(b)); + for (ProximityHiderBlock b : proximityBlocks) { + if (MinecraftInternals.isSign(b.getId())) { + signs.add(new MinecraftBlock(b.x, b.y, b.z)); } } putSignsList(player, chunkX, chunkZ, signs); @@ -91,7 +90,7 @@ public static byte[] ObfuscateOrUseCache(ChunkData chunkData, Player player) thr } // Blocks kept track for ProximityHider - ArrayList proximityBlocks = new ArrayList(); + ArrayList proximityBlocks = new ArrayList(); byte[] output = Obfuscate(chunkData, player, proximityBlocks); @@ -99,14 +98,16 @@ public static byte[] ObfuscateOrUseCache(ChunkData chunkData, Player player) thr // If cache is still allowed if(chunkData.useCache) { // Save cache - int[] proximityList = new int[proximityBlocks.size() * 3]; + int[] proximityList = new int[proximityBlocks.size() * 4]; + int index = 0; for (int i = 0; i < proximityBlocks.size(); i++) { - Block b = proximityBlocks.get(i); + ProximityHiderBlock b = proximityBlocks.get(i); if (b != null) { - proximityList[i * 3] = b.getX(); - proximityList[i * 3 + 1] = b.getY(); - proximityList[i * 3 + 2] = b.getZ(); + proximityList[index++] = b.blockData; + proximityList[index++] = b.x; + proximityList[index++] = b.y; + proximityList[index++] = b.z; } } @@ -119,7 +120,7 @@ public static byte[] ObfuscateOrUseCache(ChunkData chunkData, Player player) thr return output; } - private static byte[] Obfuscate(ChunkData chunkData, Player player, ArrayList proximityBlocks) throws IOException { + private static byte[] Obfuscate(ChunkData chunkData, Player player, ArrayList proximityBlocks) throws IOException { Environment environment = player.getWorld().getEnvironment(); int initialRadius = OrebfuscatorConfig.InitialRadius; @@ -193,7 +194,7 @@ private static byte[] Obfuscate(ChunkData chunkData, Player player, ArrayList proximityBlocks = new ArrayList(); + ArrayList proximityBlocks = new ArrayList(); // Decrypt chest list if (proximityList != null) { - for (int i = 0; i < proximityList.length; i += 3) { - Block b = CalculationsUtil.getBlockAt(player.getWorld(), proximityList[i], proximityList[i + 1], proximityList[i + 2]); + int index = 0; + + while (index < proximityList.length) { + int blockData = proximityList[index++]; + int x = proximityList[index++]; + int y = proximityList[index++]; + int z = proximityList[index++]; + ProximityHiderBlock b = new ProximityHiderBlock(blockData, x, y, z); + proximityBlocks.add(b); } } diff --git a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHider.java b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHider.java index 8b70cbc3..9f8f12c4 100644 --- a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHider.java +++ b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHider.java @@ -21,12 +21,11 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import java.util.UUID; import java.util.WeakHashMap; import java.util.concurrent.atomic.AtomicBoolean; import org.bukkit.Location; -import org.bukkit.block.Block; +import org.bukkit.World; import org.bukkit.entity.Player; import com.lishid.orebfuscator.Orebfuscator; @@ -39,7 +38,7 @@ public class ProximityHider extends Thread implements Runnable { private static ProximityHider thread = new ProximityHider(); - private Map> proximityHiderTrackerLocal = new WeakHashMap>(); + private Map proximityHiderTrackerLocal = new WeakHashMap(); private long lastExecute = System.currentTimeMillis(); private AtomicBoolean kill = new AtomicBoolean(false); private static boolean running = false; @@ -107,59 +106,65 @@ public void run() { continue; } } - - Set blocks = proximityHiderTrackerLocal.get(p); - Set removedBlocks = new HashSet(); - if (blocks == null) { - blocks = new HashSet(); - proximityHiderTrackerLocal.put(p, blocks); + + ProximityHiderPlayer localPlayerInfo = proximityHiderTrackerLocal.get(p); + + if(localPlayerInfo == null) { + proximityHiderTrackerLocal.put(p, localPlayerInfo = new ProximityHiderPlayer(p.getWorld())); } int y = (int) Math.floor(p.getLocation().getY()); - boolean skip = OrebfuscatorConfig.skipProximityHiderCheck(y); - synchronized (proximityHiderTracker) { - Set synchronizedBlocks = proximityHiderTracker.get(p).blocks; - - if (synchronizedBlocks != null) { - blocks.addAll(synchronizedBlocks); - synchronizedBlocks.clear(); + ProximityHiderPlayer playerInfo = proximityHiderTracker.get(p); + + if (playerInfo != null) { + if(!localPlayerInfo.world.equals(playerInfo.world)) { + localPlayerInfo.world = playerInfo.world; + localPlayerInfo.blocks.clear(); + } + + localPlayerInfo.blocks.addAll(playerInfo.blocks); + playerInfo.blocks.clear(); } } - if (!skip) { - for (Block b : blocks) { - if (b == null || b.getWorld() == null || p.getWorld() == null) { - removedBlocks.add(b); - continue; - } - - if (!p.getWorld().equals(b.getWorld())) { - removedBlocks.add(b); - continue; - } - - if (OrebfuscatorConfig.proximityHiderDeobfuscate(y, b) || p.getLocation().distanceSquared(b.getLocation()) < distanceSquared) { - removedBlocks.add(b); - - if (CalculationsUtil.isChunkLoaded(b.getWorld(), b.getChunk().getX(), b.getChunk().getZ())) { - p.sendBlockChange(b.getLocation(), b.getTypeId(), b.getData()); - final Block block = b; - final Player player = p; - Orebfuscator.instance.runTask(new Runnable() { - @Override - public void run() { - MinecraftInternals.updateBlockTileEntity(block, player); - } - }); - } + if (OrebfuscatorConfig.skipProximityHiderCheck(y)) continue; + + if(localPlayerInfo.world == null || p.getWorld() == null || !p.getWorld().equals(localPlayerInfo.world)) { + localPlayerInfo.blocks.clear(); + continue; + } + + Set removedBlocks = new HashSet(); + + for (ProximityHiderBlock b : localPlayerInfo.blocks) { + if (b == null) { + removedBlocks.add(b); + continue; + } + + Location blockLocation = new Location(localPlayerInfo.world, b.x, b.y, b.z); + + if (OrebfuscatorConfig.proximityHiderDeobfuscate() || p.getLocation().distanceSquared(blockLocation) < distanceSquared) { + removedBlocks.add(b); + + if (CalculationsUtil.isChunkLoaded(localPlayerInfo.world, b.x >> 4, b.z >> 4)) { + p.sendBlockChange(blockLocation, b.getId(), (byte)b.getMeta()); + final ProximityHiderBlock block = b; + final Player player = p; + Orebfuscator.instance.runTask(new Runnable() { + @Override + public void run() { + MinecraftInternals.updateBlockTileEntity(block.x, block.y, block.z, player); + } + }); } } + } - for (Block b : removedBlocks) { - blocks.remove(b); - } + for (ProximityHiderBlock b : removedBlocks) { + localPlayerInfo.blocks.remove(b); } } } catch (Exception e) { @@ -182,27 +187,23 @@ public static void restart() { } } - public static void addProximityBlocks(Player player, ArrayList blocks) { + public static void addProximityBlocks(Player player, ArrayList blocks) { if (!OrebfuscatorConfig.UseProximityHider) return; restart(); synchronized (proximityHiderTracker) { - ProximityHiderPlayer playerInfo; - UUID worldUID = player.getWorld().getUID(); + ProximityHiderPlayer playerInfo = proximityHiderTracker.get(player); + World world = player.getWorld(); - if (!proximityHiderTracker.containsKey(player)) { - proximityHiderTracker.put(player, playerInfo = new ProximityHiderPlayer(worldUID)); - } else { - playerInfo = proximityHiderTracker.get(player); - - if(!playerInfo.worldUID.equals(worldUID)) { - playerInfo.worldUID = worldUID; - playerInfo.blocks.clear(); - } + if (playerInfo == null) { + proximityHiderTracker.put(player, playerInfo = new ProximityHiderPlayer(world)); + } else if(!playerInfo.world.equals(world)) { + playerInfo.world = world; + playerInfo.blocks.clear(); } - for (Block b : blocks) { + for (ProximityHiderBlock b : blocks) { playerInfo.blocks.add(b); } } @@ -216,12 +217,13 @@ public static void clearPlayer(Player player) { public static void clearBlocksForOldWorld(Player player) { synchronized (ProximityHider.proximityHiderTracker) { - if(ProximityHider.proximityHiderTracker.containsKey(player)) { - ProximityHiderPlayer playerInfo = ProximityHider.proximityHiderTracker.get(player); - UUID worldUID = player.getWorld().getUID(); + ProximityHiderPlayer playerInfo = ProximityHider.proximityHiderTracker.get(player); + + if(playerInfo != null) { + World world = player.getWorld(); - if(!playerInfo.worldUID.equals(worldUID)) { - playerInfo.worldUID = worldUID; + if(!playerInfo.world.equals(world)) { + playerInfo.world = world; playerInfo.blocks.clear(); } } diff --git a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderBlock.java b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderBlock.java new file mode 100644 index 00000000..be4314ce --- /dev/null +++ b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderBlock.java @@ -0,0 +1,42 @@ +package com.lishid.orebfuscator.obfuscation; + +import com.lishid.orebfuscator.chunkmap.ChunkMapManager; + +public class ProximityHiderBlock { + int blockData; + int x, y, z; + + public ProximityHiderBlock(int blockData, int x, int y, int z) { + this.blockData = blockData; + this.x = x; + this.y = y; + this.z = z; + } + + public int getId() { + return ChunkMapManager.getBlockIdFromData(blockData); + } + + public int getMeta() { + return ChunkMapManager.getBlockMetaFromData(blockData); + } + + @Override + public boolean equals(Object other) { + if (other == null || !(other instanceof ProximityHiderBlock)) { + return false; + } + ProximityHiderBlock object = (ProximityHiderBlock) other; + return this.blockData == object.blockData && this.x == object.x && this.y == object.y && this.z == object.z; + } + + @Override + public int hashCode() { + return x ^ y ^ z; + } + + @Override + public String toString() { + return x + " " + y + " " + z; + } +} diff --git a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderPlayer.java b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderPlayer.java index e50a5d11..1e27fc68 100644 --- a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderPlayer.java +++ b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderPlayer.java @@ -7,16 +7,15 @@ import java.util.HashSet; import java.util.Set; -import java.util.UUID; -import org.bukkit.block.Block; +import org.bukkit.World; public class ProximityHiderPlayer { - public UUID worldUID; - public Set blocks; + public World world; + public Set blocks; - public ProximityHiderPlayer(UUID worldUID) { - this.worldUID = worldUID; - this.blocks = new HashSet(); + public ProximityHiderPlayer(World world) { + this.world = world; + this.blocks = new HashSet(); } } From bf91c531d2a6ea6cd7a05f757e632f27e1fcf550 Mon Sep 17 00:00:00 2001 From: Aleksey-Terzi Date: Sun, 24 Apr 2016 14:38:23 +0300 Subject: [PATCH 4/5] ProximityHider improved: it now check blocks only from nearby chunks --- .../obfuscation/Calculations.java | 4 +- .../obfuscation/ProximityHider.java | 115 +++++++++++------- .../obfuscation/ProximityHiderPlayer.java | 46 ++++++- 3 files changed, 112 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java b/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java index c2fb71d0..d38ed450 100644 --- a/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java +++ b/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java @@ -271,7 +271,7 @@ private static byte[] Obfuscate(ChunkData chunkData, Player player, ArrayList> 4; + + if((OrebfuscatorConfig.ProximityHiderDistance & 0xf) != 0) { + checkRadius++; + } + HashMap checkPlayers = new HashMap(); synchronized (playersToCheck) { @@ -119,52 +123,69 @@ public void run() { ProximityHiderPlayer playerInfo = proximityHiderTracker.get(p); if (playerInfo != null) { - if(!localPlayerInfo.world.equals(playerInfo.world)) { - localPlayerInfo.world = playerInfo.world; - localPlayerInfo.blocks.clear(); + if(!localPlayerInfo.getWorld().equals(playerInfo.getWorld())) { + localPlayerInfo.setWorld(playerInfo.getWorld()); + localPlayerInfo.clearChunks(); } - localPlayerInfo.blocks.addAll(playerInfo.blocks); - playerInfo.blocks.clear(); + localPlayerInfo.copyChunks(playerInfo); + playerInfo.clearChunks(); } } if (OrebfuscatorConfig.skipProximityHiderCheck(y)) continue; - if(localPlayerInfo.world == null || p.getWorld() == null || !p.getWorld().equals(localPlayerInfo.world)) { - localPlayerInfo.blocks.clear(); + if(localPlayerInfo.getWorld() == null || p.getWorld() == null || !p.getWorld().equals(localPlayerInfo.getWorld())) { + localPlayerInfo.clearChunks(); continue; } - Set removedBlocks = new HashSet(); + ArrayList removedBlocks = new ArrayList(); + Location playerLocation = p.getLocation(); + int minChunkX = (playerLocation.getBlockX() >> 4) - checkRadius; + int maxChunkX = minChunkX + (checkRadius << 1); + int minChunkZ = (playerLocation.getBlockZ() >> 4) - checkRadius; + int maxChunkZ = minChunkZ + (checkRadius << 1); - for (ProximityHiderBlock b : localPlayerInfo.blocks) { - if (b == null) { - removedBlocks.add(b); - continue; - } - - Location blockLocation = new Location(localPlayerInfo.world, b.x, b.y, b.z); - - if (OrebfuscatorConfig.proximityHiderDeobfuscate() || p.getLocation().distanceSquared(blockLocation) < distanceSquared) { - removedBlocks.add(b); - - if (CalculationsUtil.isChunkLoaded(localPlayerInfo.world, b.x >> 4, b.z >> 4)) { - p.sendBlockChange(blockLocation, b.getId(), (byte)b.getMeta()); - final ProximityHiderBlock block = b; - final Player player = p; - Orebfuscator.instance.runTask(new Runnable() { - @Override - public void run() { - MinecraftInternals.updateBlockTileEntity(block.x, block.y, block.z, player); - } - }); - } - } - } - - for (ProximityHiderBlock b : removedBlocks) { - localPlayerInfo.blocks.remove(b); + for(int chunkZ = minChunkZ; chunkZ <= maxChunkZ; chunkZ++) { + for(int chunkX = minChunkX; chunkX <= maxChunkX; chunkX++) { + ArrayList blocks = localPlayerInfo.getBlocks(chunkX, chunkZ); + + if(blocks == null) continue; + + removedBlocks.clear(); + + for (ProximityHiderBlock b : blocks) { + if (b == null) { + removedBlocks.add(b); + continue; + } + + Location blockLocation = new Location(localPlayerInfo.getWorld(), b.x, b.y, b.z); + + if (OrebfuscatorConfig.proximityHiderDeobfuscate() || playerLocation.distanceSquared(blockLocation) < distanceSquared) { + removedBlocks.add(b); + + if (CalculationsUtil.isChunkLoaded(localPlayerInfo.getWorld(), b.x >> 4, b.z >> 4)) { + p.sendBlockChange(blockLocation, b.getId(), (byte)b.getMeta()); + final ProximityHiderBlock block = b; + final Player player = p; + Orebfuscator.instance.runTask(new Runnable() { + @Override + public void run() { + MinecraftInternals.updateBlockTileEntity(block.x, block.y, block.z, player); + } + }); + } + } + } + + if(blocks.size() == removedBlocks.size()) { + localPlayerInfo.removeChunk(chunkX, chunkZ); + } else { + blocks.removeAll(removedBlocks); + } + } } } } catch (Exception e) { @@ -187,7 +208,7 @@ public static void restart() { } } - public static void addProximityBlocks(Player player, ArrayList blocks) { + public static void addProximityBlocks(Player player, int chunkX, int chunkZ, ArrayList blocks) { if (!OrebfuscatorConfig.UseProximityHider) return; restart(); @@ -198,13 +219,15 @@ public static void addProximityBlocks(Player player, ArrayList 0) { + playerInfo.putBlocks(chunkX, chunkZ, blocks); + } else { + playerInfo.removeChunk(chunkX, chunkZ); } } } @@ -222,9 +245,9 @@ public static void clearBlocksForOldWorld(Player player) { if(playerInfo != null) { World world = player.getWorld(); - if(!playerInfo.world.equals(world)) { - playerInfo.world = world; - playerInfo.blocks.clear(); + if(!playerInfo.getWorld().equals(world)) { + playerInfo.setWorld(world); + playerInfo.clearChunks(); } } } diff --git a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderPlayer.java b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderPlayer.java index 1e27fc68..2e523780 100644 --- a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderPlayer.java +++ b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderPlayer.java @@ -5,17 +5,53 @@ package com.lishid.orebfuscator.obfuscation; -import java.util.HashSet; -import java.util.Set; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import org.bukkit.World; public class ProximityHiderPlayer { - public World world; - public Set blocks; + private World world; + private Map> chunks; public ProximityHiderPlayer(World world) { this.world = world; - this.blocks = new HashSet(); + this.chunks = new HashMap>(); + } + + public World getWorld() { + return this.world; + } + + public void setWorld(World world) { + this.world = world; + } + + public void clearChunks() { + this.chunks.clear(); + } + + public void putBlocks(int chunkX, int chunkZ, ArrayList blocks) { + long key = getKey(chunkX, chunkZ); + this.chunks.put(key, blocks); + } + + public ArrayList getBlocks(int chunkX, int chunkZ) { + long key = getKey(chunkX, chunkZ); + return this.chunks.get(key); + } + + public void copyChunks(ProximityHiderPlayer playerInfo) { + this.chunks.putAll(playerInfo.chunks); + } + + public void removeChunk(int chunkX, int chunkZ) { + long key = getKey(chunkX, chunkZ); + this.chunks.remove(key); + } + + private static long getKey(int chunkX, int chunkZ) { + return ((chunkZ & 0xffffffffL) << 32) | (chunkX & 0xffffffffL); } } From 11c4ff3589c938ea98448463db36b1cfd65e2538 Mon Sep 17 00:00:00 2001 From: Aleksey-Terzi Date: Sun, 24 Apr 2016 19:21:29 +0300 Subject: [PATCH 5/5] Bug fix in ProximityHider --- pom.xml | 2 +- .../orebfuscator/OrebfuscatorConfig.java | 2 +- .../internal/MinecraftInternals.java | 13 ++---- .../obfuscation/Calculations.java | 33 +++++++-------- .../obfuscation/ProximityHider.java | 21 +++++----- .../obfuscation/ProximityHiderBlock.java | 42 ------------------- .../obfuscation/ProximityHiderPlayer.java | 9 ++-- 7 files changed, 37 insertions(+), 85 deletions(-) delete mode 100644 src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderBlock.java diff --git a/pom.xml b/pom.xml index 73abe327..21ac04d8 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.lishid orebfuscator - 4.0.6-SNAPSHOT + 4.0.7-SNAPSHOT jar Orebfuscator4 diff --git a/src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java b/src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java index 8eddae4f..2d41951e 100644 --- a/src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java +++ b/src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java @@ -36,7 +36,7 @@ public class OrebfuscatorConfig { // Constant/persistent data - private static final int CONFIG_VERSION = 11; + private static final int CONFIG_VERSION = 12; private static Random random = new Random(); private static int AvailableProcessors = Runtime.getRuntime().availableProcessors(); diff --git a/src/main/java/com/lishid/orebfuscator/internal/MinecraftInternals.java b/src/main/java/com/lishid/orebfuscator/internal/MinecraftInternals.java index c8403e59..077ecf5a 100644 --- a/src/main/java/com/lishid/orebfuscator/internal/MinecraftInternals.java +++ b/src/main/java/com/lishid/orebfuscator/internal/MinecraftInternals.java @@ -21,7 +21,7 @@ import net.minecraft.server.v1_9_R1.Packet; import net.minecraft.server.v1_9_R1.TileEntity; -import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.craftbukkit.v1_9_R1.CraftChunk; import org.bukkit.craftbukkit.v1_9_R1.CraftWorld; import org.bukkit.craftbukkit.v1_9_R1.block.CraftBlock; @@ -31,9 +31,9 @@ //Volatile public class MinecraftInternals { - public static void updateBlockTileEntity(int blockX, int blockY, int blockZ, Player player) { + public static void updateBlockTileEntity(Block block, Player player) { CraftWorld world = (CraftWorld) player.getWorld(); - TileEntity tileEntity = world.getTileEntityAt(blockX, blockY, blockZ); + TileEntity tileEntity = world.getTileEntityAt(block.getX(), block.getY(), block.getZ()); if (tileEntity == null) { return; } @@ -50,11 +50,4 @@ public static void notifyBlockChange(org.bukkit.World world, CraftBlock block) { ((CraftWorld) world).getHandle().notify(blockPosition, blockData, blockData, 0); } - - public static boolean isSign(int blockId) { - return blockId == Material.CHORUS_FLOWER.getId() - || blockId == Material.CLAY_BALL.getId() - || blockId == Material.PRISMARINE_SHARD.getId() - ; - } } diff --git a/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java b/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java index d38ed450..0a3aa023 100644 --- a/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java +++ b/src/main/java/com/lishid/orebfuscator/obfuscation/Calculations.java @@ -28,6 +28,8 @@ import org.bukkit.World; import org.bukkit.World.Environment; +import org.bukkit.block.Block; +import org.bukkit.block.Sign; import org.bukkit.entity.Player; import com.lishid.orebfuscator.OrebfuscatorConfig; @@ -35,7 +37,6 @@ import com.lishid.orebfuscator.chunkmap.BlockState; import com.lishid.orebfuscator.chunkmap.ChunkData; import com.lishid.orebfuscator.chunkmap.ChunkMapManager; -import com.lishid.orebfuscator.internal.MinecraftInternals; public class Calculations { @@ -63,11 +64,11 @@ public static Set getSignsList(Player player, int chunkX, int ch return map.get(address); } - public static void putSignsList(Player player, int chunkX, int chunkZ, List proximityBlocks) { + public static void putSignsList(Player player, int chunkX, int chunkZ, List proximityBlocks) { Set signs = new HashSet(); - for (ProximityHiderBlock b : proximityBlocks) { - if (MinecraftInternals.isSign(b.getId())) { - signs.add(new MinecraftBlock(b.x, b.y, b.z)); + for (Block b : proximityBlocks) { + if (b.getState() instanceof Sign) { + signs.add(new MinecraftBlock(b)); } } putSignsList(player, chunkX, chunkZ, signs); @@ -90,7 +91,7 @@ public static byte[] ObfuscateOrUseCache(ChunkData chunkData, Player player) thr } // Blocks kept track for ProximityHider - ArrayList proximityBlocks = new ArrayList(); + ArrayList proximityBlocks = new ArrayList(); byte[] output = Obfuscate(chunkData, player, proximityBlocks); @@ -98,16 +99,15 @@ public static byte[] ObfuscateOrUseCache(ChunkData chunkData, Player player) thr // If cache is still allowed if(chunkData.useCache) { // Save cache - int[] proximityList = new int[proximityBlocks.size() * 4]; + int[] proximityList = new int[proximityBlocks.size() * 3]; int index = 0; for (int i = 0; i < proximityBlocks.size(); i++) { - ProximityHiderBlock b = proximityBlocks.get(i); + Block b = proximityBlocks.get(i); if (b != null) { - proximityList[index++] = b.blockData; - proximityList[index++] = b.x; - proximityList[index++] = b.y; - proximityList[index++] = b.z; + proximityList[index++] = b.getX(); + proximityList[index++] = b.getY(); + proximityList[index++] = b.getZ(); } } @@ -120,7 +120,7 @@ public static byte[] ObfuscateOrUseCache(ChunkData chunkData, Player player) thr return output; } - private static byte[] Obfuscate(ChunkData chunkData, Player player, ArrayList proximityBlocks) throws IOException { + private static byte[] Obfuscate(ChunkData chunkData, Player player, ArrayList proximityBlocks) throws IOException { Environment environment = player.getWorld().getEnvironment(); int initialRadius = OrebfuscatorConfig.InitialRadius; @@ -194,7 +194,7 @@ private static byte[] Obfuscate(ChunkData chunkData, Player player, ArrayList proximityBlocks = new ArrayList(); + ArrayList proximityBlocks = new ArrayList(); // Decrypt chest list if (proximityList != null) { int index = 0; while (index < proximityList.length) { - int blockData = proximityList[index++]; int x = proximityList[index++]; int y = proximityList[index++]; int z = proximityList[index++]; - ProximityHiderBlock b = new ProximityHiderBlock(blockData, x, y, z); + Block b = CalculationsUtil.getBlockAt(player.getWorld(), x, y, z); proximityBlocks.add(b); } diff --git a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHider.java b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHider.java index f05483b3..fb70f6b9 100644 --- a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHider.java +++ b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHider.java @@ -24,6 +24,7 @@ import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.entity.Player; import com.lishid.orebfuscator.Orebfuscator; @@ -140,7 +141,7 @@ public void run() { continue; } - ArrayList removedBlocks = new ArrayList(); + ArrayList removedBlocks = new ArrayList(); Location playerLocation = p.getLocation(); int minChunkX = (playerLocation.getBlockX() >> 4) - checkRadius; int maxChunkX = minChunkX + (checkRadius << 1); @@ -149,31 +150,31 @@ public void run() { for(int chunkZ = minChunkZ; chunkZ <= maxChunkZ; chunkZ++) { for(int chunkX = minChunkX; chunkX <= maxChunkX; chunkX++) { - ArrayList blocks = localPlayerInfo.getBlocks(chunkX, chunkZ); + ArrayList blocks = localPlayerInfo.getBlocks(chunkX, chunkZ); if(blocks == null) continue; removedBlocks.clear(); - for (ProximityHiderBlock b : blocks) { + for (Block b : blocks) { if (b == null) { removedBlocks.add(b); continue; } - Location blockLocation = new Location(localPlayerInfo.getWorld(), b.x, b.y, b.z); - + Location blockLocation = b.getLocation(); + if (OrebfuscatorConfig.proximityHiderDeobfuscate() || playerLocation.distanceSquared(blockLocation) < distanceSquared) { removedBlocks.add(b); - if (CalculationsUtil.isChunkLoaded(localPlayerInfo.getWorld(), b.x >> 4, b.z >> 4)) { - p.sendBlockChange(blockLocation, b.getId(), (byte)b.getMeta()); - final ProximityHiderBlock block = b; + if (CalculationsUtil.isChunkLoaded(localPlayerInfo.getWorld(), b.getChunk().getX(), b.getChunk().getZ())) { + p.sendBlockChange(blockLocation, b.getTypeId(), (byte)b.getData()); + final Block block = b; final Player player = p; Orebfuscator.instance.runTask(new Runnable() { @Override public void run() { - MinecraftInternals.updateBlockTileEntity(block.x, block.y, block.z, player); + MinecraftInternals.updateBlockTileEntity(block, player); } }); } @@ -208,7 +209,7 @@ public static void restart() { } } - public static void addProximityBlocks(Player player, int chunkX, int chunkZ, ArrayList blocks) { + public static void addProximityBlocks(Player player, int chunkX, int chunkZ, ArrayList blocks) { if (!OrebfuscatorConfig.UseProximityHider) return; restart(); diff --git a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderBlock.java b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderBlock.java deleted file mode 100644 index be4314ce..00000000 --- a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderBlock.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.lishid.orebfuscator.obfuscation; - -import com.lishid.orebfuscator.chunkmap.ChunkMapManager; - -public class ProximityHiderBlock { - int blockData; - int x, y, z; - - public ProximityHiderBlock(int blockData, int x, int y, int z) { - this.blockData = blockData; - this.x = x; - this.y = y; - this.z = z; - } - - public int getId() { - return ChunkMapManager.getBlockIdFromData(blockData); - } - - public int getMeta() { - return ChunkMapManager.getBlockMetaFromData(blockData); - } - - @Override - public boolean equals(Object other) { - if (other == null || !(other instanceof ProximityHiderBlock)) { - return false; - } - ProximityHiderBlock object = (ProximityHiderBlock) other; - return this.blockData == object.blockData && this.x == object.x && this.y == object.y && this.z == object.z; - } - - @Override - public int hashCode() { - return x ^ y ^ z; - } - - @Override - public String toString() { - return x + " " + y + " " + z; - } -} diff --git a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderPlayer.java b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderPlayer.java index 2e523780..aa89d21f 100644 --- a/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderPlayer.java +++ b/src/main/java/com/lishid/orebfuscator/obfuscation/ProximityHiderPlayer.java @@ -10,14 +10,15 @@ import java.util.Map; import org.bukkit.World; +import org.bukkit.block.Block; public class ProximityHiderPlayer { private World world; - private Map> chunks; + private Map> chunks; public ProximityHiderPlayer(World world) { this.world = world; - this.chunks = new HashMap>(); + this.chunks = new HashMap>(); } public World getWorld() { @@ -32,12 +33,12 @@ public void clearChunks() { this.chunks.clear(); } - public void putBlocks(int chunkX, int chunkZ, ArrayList blocks) { + public void putBlocks(int chunkX, int chunkZ, ArrayList blocks) { long key = getKey(chunkX, chunkZ); this.chunks.put(key, blocks); } - public ArrayList getBlocks(int chunkX, int chunkZ) { + public ArrayList getBlocks(int chunkX, int chunkZ) { long key = getKey(chunkX, chunkZ); return this.chunks.get(key); }