Skip to content

Instantly share code, notes, and snippets.

@Cryptite
Created February 24, 2018 20:42
Show Gist options
  • Save Cryptite/f8222a4ca92a6fc707847fe02094822e to your computer and use it in GitHub Desktop.
Save Cryptite/f8222a4ca92a6fc707847fe02094822e to your computer and use it in GitHub Desktop.
Threaded ConfigFile
package com.n9works.bukkit;
import com.google.common.collect.Iterables;
import com.n9works.bukkit.towns.Generator;
import com.n9works.bukkit.utils.StringLocation;
import com.n9works.bukkit.utils.blocks.StringBlock;
import net.md_5.bungee.api.ChatColor;
import org.apache.commons.configuration.PropertyConverter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.n9works.bukkit.SerializeInventory.fromBase64ToItem;
import static org.bukkit.ChatColor.*;
public class ConfigFile extends Thread {
public final TheArtifact plugin;
private final String fileName;
private final boolean threaded;
public File configFile;
private FileConfiguration fileConfiguration;
//Thread-Lock stuff
private final Object saveLock = new Object();
private volatile boolean saveNeeded;
public boolean shuttingDown;
public ConfigFile(String fileName) {
this(fileName, false);
}
public ConfigFile(String fileName, boolean threaded) {
plugin = (TheArtifact) Bukkit.getPluginManager().getPlugin("TheArtifact");
if (plugin == null) {
throw new IllegalArgumentException("plugin cannot be null");
}
plugin.configFiles.add(this);
this.fileName = fileName;
File dataFolder = plugin.getDataFolder();
if (dataFolder == null)
throw new IllegalStateException();
this.configFile = new File(plugin.getDataFolder(), fileName);
this.threaded = threaded;
}
public void run() {
while (!shuttingDown) {
if (saveNeeded) {
saveNeeded = false;
reallySave();
} else {
synchronized (this) {
try {
this.wait(1000); // sleep 1 second or until notified
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
System.out.println(GREEN + toString() + " shutdown.");
}
public synchronized void save() {
if (threaded) {
if (!isAlive()) {
start();
System.out.println(YELLOW + toString() + " thread started.");
}
saveNeeded = true;
this.notify();
} else {
reallySave();
}
}
public synchronized void saveAsync() { // synchronized in order to do the notify, but this will be damn near instantaneous since it's just setting the bool
Bukkit.getScheduler().runTaskAsynchronously(plugin, this::reallySave);
}
private void reallySave() {
synchronized (saveLock) {
if (fileConfiguration != null && configFile != null) {
try {
long now = System.currentTimeMillis();
getConfig().save(configFile);
System.out.println(GREEN + toString() + " THREAD saved " + configFile.getPath() + " in " + (System.currentTimeMillis() - now) + "ms");
} catch (IOException ex) {
System.out.println(RED + toString() + " could not save config to " + configFile);
ex.printStackTrace();
}
} else {
System.out.println(RED + toString() + " fileConfiguration or configFile are null?");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment