Skip to content

Commit

Permalink
Clear cache if config was updated since previous server start
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey-Terzi committed Apr 18, 2016
1 parent 0b37dc3 commit e0a850f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 27 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.1-SNAPSHOT</version>
<version>4.0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Orebfuscator4</name>
Expand Down
56 changes: 32 additions & 24 deletions src/main/java/com/lishid/orebfuscator/Orebfuscator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

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;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.PluginManager;
Expand All @@ -29,10 +31,10 @@
import com.lishid.orebfuscator.commands.OrebfuscatorCommandExecutor;
import com.lishid.orebfuscator.hithack.BlockHitManager;
import com.lishid.orebfuscator.hook.ProtocolLibHook;
import com.lishid.orebfuscator.internal.MinecraftInternals;
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 @@ -54,6 +56,9 @@ public void onEnable() {
instance = this;
// Load configurations
OrebfuscatorConfig.load();

//Make sure cache is cleared if config was changed since last start
checkCacheAndConfigSynchronized();

// Orebfuscator events
pm.registerEvents(new OrebfuscatorPlayerListener(), this);
Expand All @@ -65,28 +70,31 @@ public void onEnable() {
(new ProtocolLibHook()).register(this);
usePL = true;
}

/* NoLagg is deprecated now
if (pm.getPlugin("NoLagg") != null && !usePL) {
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
@Override
public void run() {
Orebfuscator.log("WARNING! NoLagg Absolutely NEED ProtocolLib to work with Orebfuscator!");
}
}, 0, 60 * 1000);// Warn every minute
}
*/

// Disable spigot's built-in orebfuscator since it has limited functionality
try {
Class.forName("org.spigotmc.AntiXray");
Orebfuscator.log("Spigot found! Automatically disabling built-in AntiXray.");
for (World world : getServer().getWorlds()) {
MinecraftInternals.tryDisableSpigotAntiXray(world);
}
} catch (Exception e) {
// Spigot not found
}
}

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();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -547,7 +548,7 @@ private static FileConfiguration getConfig() {
return Orebfuscator.instance.getConfig();
}

public static int clamp(int value, int min, int max) {
private static int clamp(int value, int min, int max) {
if (value < min)
value = min;
if (value > max)
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/lishid/orebfuscator/utils/FileHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.lishid.orebfuscator.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileHelper {
public static String readFile(File file) throws IOException {
if(!file.exists()) return null;

StringBuilder text = new StringBuilder("");

BufferedReader reader = new BufferedReader(new FileReader(file));

try {
String line;

while ((line = reader.readLine()) != null) {
text.append(line);
text.append("\n");
}
}
finally {
reader.close();
}

return text.toString();
}

public static void delete(File file) {
if (file.isDirectory()) {
for (File child : file.listFiles())
delete(child);
}

file.delete();
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: {project.name}
name: ${project.name}
main: com.lishid.orebfuscator.Orebfuscator
version: ${project.version}
author: lishid
Expand Down

0 comments on commit e0a850f

Please sign in to comment.