Skip to content

Instantly share code, notes, and snippets.

@117
Created January 9, 2016 00:52
Show Gist options
  • Save 117/8b3fd5964f92bf90428a to your computer and use it in GitHub Desktop.
Save 117/8b3fd5964f92bf90428a to your computer and use it in GitHub Desktop.
Automatically update config files when changes are made.
import org.bukkit.plugin.java.JavaPlugin;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
public class Updater implements Runnable {
private JavaPlugin plugin;
private MessageDigest digest;
private File config;
private String hash;
private String _hash;
public Updater(JavaPlugin plugin) {
this.plugin = plugin;
plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, this, 20L, 20L);
}
@Override
public void run() {
config = (config != null) ? config : getConfigFile();
try (InputStream input = new FileInputStream(config)) {
digest = (digest != null) ? digest : MessageDigest.getInstance("SHA-1");
byte[] buffer = new byte[(int) config.length()];
int len = input.read(buffer);
while (len != -1) {
digest.update(buffer, 0, len);
len = input.read(buffer);
}
hash = new HexBinaryAdapter().marshal(digest.digest());
} catch (Throwable thrown) {
thrown.printStackTrace();
}
_hash = (!hash.equals(_hash)) ? update() : hash;
}
public File getConfigFile() {
return new File(plugin.getDataFolder().getPath() + "/config.yml");
}
public String update() {
plugin.reloadConfig();
return hash;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment