Skip to content

Commit

Permalink
Merge pull request #78 from DevotedMC/master
Browse files Browse the repository at this point in the history
Merging in for 4.0.7-snapshot release
  • Loading branch information
ProgrammerDan committed Apr 24, 2016
2 parents 6aae2d3 + 11c4ff3 commit 34752a1
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 220 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.lishid</groupId>
<artifactId>orebfuscator</artifactId>
<version>4.0.5-SNAPSHOT</version>
<version>4.0.7-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Orebfuscator4</name>
Expand Down
52 changes: 13 additions & 39 deletions src/main/java/com/lishid/orebfuscator/Orebfuscator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -45,8 +41,11 @@ 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;

private boolean isProtocolLibFound;
public boolean getIsProtocolLibFound() {
return this.isProtocolLibFound;
}

@Override
public void onEnable() {
Expand All @@ -57,49 +56,24 @@ public void onEnable() {
// Load configurations
OrebfuscatorConfig.load();

//Make sure cache is cleared if config was changed since last start
checkCacheAndConfigSynchronized();
this.isProtocolLibFound = pm.getPlugin("ProtocolLib") != null;

if (!this.isProtocolLibFound) {
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);
}
Expand Down
30 changes: 9 additions & 21 deletions src/main/java/com/lishid/orebfuscator/OrebfuscatorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import com.lishid.orebfuscator.cache.ObfuscatedDataCache;

public class OrebfuscatorConfig {
// Constant/persistent data
private static final int CONFIG_VERSION = 10;
private static final int CONFIG_VERSION = 12;
private static Random random = new Random();
private static int AvailableProcessors = Runtime.getRuntime().availableProcessors();

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -378,24 +377,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);
}

Expand Down Expand Up @@ -460,6 +441,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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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) {
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -165,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);
Expand All @@ -179,8 +186,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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.minecraft.server.v1_9_R1.Packet;
import net.minecraft.server.v1_9_R1.TileEntity;

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;
Expand All @@ -30,8 +31,8 @@
//Volatile

public class MinecraftInternals {
public static void updateBlockTileEntity(org.bukkit.block.Block block, Player player) {
CraftWorld world = (CraftWorld) block.getWorld();
public static void updateBlockTileEntity(Block block, Player player) {
CraftWorld world = (CraftWorld) player.getWorld();
TileEntity tileEntity = world.getTileEntityAt(block.getX(), block.getY(), block.getZ());
if (tileEntity == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Loading

0 comments on commit 34752a1

Please sign in to comment.