mirror of
https://github.com/exituser/Brewery.git
synced 2026-09-17 06:38:56 +00:00
Threaded savefile writes
This commit is contained in:
+7
-3
@@ -210,14 +210,18 @@ recipes:
|
||||
10:
|
||||
name: Stale Coffee/Coffee/Strong Coffee
|
||||
ingredients:
|
||||
- INK_SACK,3/6
|
||||
- MILK_BUCKET/1
|
||||
- INK_SACK,3/12
|
||||
- MILK_BUCKET/2
|
||||
cookingtime: 2
|
||||
color: BLACK
|
||||
difficulty: 3
|
||||
effects:
|
||||
- REGENERATION/1/2-5
|
||||
- SPEED/1-2/10-25
|
||||
- SPEED/1/30-140
|
||||
|
||||
# More Recipes ideas: Cachaca, Gin, Whiskey, Tequila, Cider, etc. as well as high quality abbreviations like golden vodka etc.
|
||||
# I will not add more Recipes to the default config, as they would be public and viewable by users to cheat.
|
||||
# It is up to the Serveradmin to change and add Recipes, so players cannot cheat from the default config.
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@ import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import com.dre.brewery.integration.LogBlockBarrel;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -30,14 +29,12 @@ import org.bukkit.block.Block;
|
||||
import org.mcstats.Metrics;
|
||||
|
||||
import com.dre.brewery.listeners.*;
|
||||
import com.dre.brewery.filedata.*;
|
||||
import com.dre.brewery.integration.LogBlockBarrel;
|
||||
|
||||
public class P extends JavaPlugin {
|
||||
public static P p;
|
||||
public static boolean debug;
|
||||
public static int lastBackup = 0;
|
||||
public static int lastSave = 1;
|
||||
public static int autosave = 3;
|
||||
final public static String dataVersion = "1.1";
|
||||
public static boolean useUUID;
|
||||
|
||||
// Third Party Enabled
|
||||
@@ -102,7 +99,7 @@ public class P extends JavaPlugin {
|
||||
p.getServer().getScheduler().cancelTasks(this);
|
||||
|
||||
// save Data to Disk
|
||||
saveData();
|
||||
DataSave.save(true);
|
||||
|
||||
// save LanguageReader
|
||||
languageReader.save();
|
||||
@@ -212,7 +209,7 @@ public class P extends JavaPlugin {
|
||||
useLB = config.getBoolean("useLogBlock", false) && getServer().getPluginManager().isPluginEnabled("LogBlock");
|
||||
|
||||
// various Settings
|
||||
autosave = config.getInt("autosave", 3);
|
||||
DataSave.autosave = config.getInt("autosave", 3);
|
||||
debug = config.getBoolean("debug", false);
|
||||
BPlayer.pukeItem = Material.matchMaterial(config.getString("pukeItem", "SOUL_SAND"));
|
||||
BPlayer.hangoverTime = config.getInt("hangoverDays", 0) * 24 * 60;
|
||||
@@ -287,11 +284,11 @@ public class P extends JavaPlugin {
|
||||
// Check if data is the newest version
|
||||
String version = data.getString("Version", null);
|
||||
if (version != null) {
|
||||
if (!version.equals(dataVersion)) {
|
||||
if (!version.equals(DataSave.dataVersion)) {
|
||||
P.p.log("Data File is being updated...");
|
||||
new DataUpdater(data, file).update(version);
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
P.p.log("Data Updated to version: " + dataVersion);
|
||||
P.p.log("Data Updated to version: " + DataSave.dataVersion);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -497,77 +494,6 @@ public class P extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
// save all Data
|
||||
public void saveData() {
|
||||
long time = System.nanoTime();
|
||||
File datafile = new File(p.getDataFolder(), "data.yml");
|
||||
|
||||
FileConfiguration oldData = YamlConfiguration.loadConfiguration(datafile);
|
||||
|
||||
if (datafile.exists()) {
|
||||
if (lastBackup > 10) {
|
||||
datafile.renameTo(new File(p.getDataFolder(), "dataBackup.yml"));
|
||||
lastBackup = 0;
|
||||
} else {
|
||||
lastBackup++;
|
||||
}
|
||||
}
|
||||
|
||||
FileConfiguration configFile = new YamlConfiguration();
|
||||
|
||||
if (!Brew.potions.isEmpty()) {
|
||||
Brew.save(configFile.createSection("Brew"));
|
||||
}
|
||||
|
||||
if (!BCauldron.bcauldrons.isEmpty() || oldData.contains("BCauldron")) {
|
||||
BCauldron.save(configFile.createSection("BCauldron"), oldData.getConfigurationSection("BCauldron"));
|
||||
}
|
||||
|
||||
if (!Barrel.barrels.isEmpty() || oldData.contains("Barrel")) {
|
||||
Barrel.save(configFile.createSection("Barrel"), oldData.getConfigurationSection("Barrel"));
|
||||
}
|
||||
|
||||
if (!BPlayer.isEmpty()) {
|
||||
BPlayer.save(configFile.createSection("Player"));
|
||||
}
|
||||
|
||||
if (!Wakeup.wakeups.isEmpty() || oldData.contains("Wakeup")) {
|
||||
Wakeup.save(configFile.createSection("Wakeup"), oldData.getConfigurationSection("Wakeup"));
|
||||
}
|
||||
|
||||
saveWorldNames(configFile, oldData.getConfigurationSection("Worlds"));
|
||||
|
||||
configFile.set("Version", dataVersion);
|
||||
|
||||
try {
|
||||
configFile.save(datafile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
lastSave = 1;
|
||||
|
||||
time = System.nanoTime() - time;
|
||||
float ftime = (float) (time / 1000000.0);
|
||||
p.debugLog("Writing Data to File (" + ftime + "ms)");
|
||||
}
|
||||
|
||||
public void saveWorldNames(FileConfiguration root, ConfigurationSection old) {
|
||||
if (old != null) {
|
||||
root.set("Worlds", old);
|
||||
}
|
||||
for (World world : p.getServer().getWorlds()) {
|
||||
String worldName = world.getName();
|
||||
if (worldName.startsWith("DXL_")) {
|
||||
worldName = getDxlName(worldName);
|
||||
root.set("Worlds." + worldName, 0);
|
||||
} else {
|
||||
worldName = world.getUID().toString();
|
||||
root.set("Worlds." + worldName, world.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Utility
|
||||
|
||||
public int parseInt(String string) {
|
||||
@@ -738,11 +664,7 @@ public class P extends JavaPlugin {
|
||||
|
||||
debugLog("Update");
|
||||
|
||||
if (lastSave >= autosave) {
|
||||
saveData();// save all data
|
||||
} else {
|
||||
lastSave++;
|
||||
}
|
||||
DataSave.autoSave();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-1
@@ -1,4 +1,4 @@
|
||||
package com.dre.brewery;
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
@@ -9,6 +9,8 @@ import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.dre.brewery.P;
|
||||
|
||||
public class ConfigUpdater {
|
||||
|
||||
private ArrayList<String> config = new ArrayList<String>();
|
||||
@@ -0,0 +1,160 @@
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import com.dre.brewery.BCauldron;
|
||||
import com.dre.brewery.BPlayer;
|
||||
import com.dre.brewery.Barrel;
|
||||
import com.dre.brewery.Brew;
|
||||
import com.dre.brewery.P;
|
||||
import com.dre.brewery.Wakeup;
|
||||
|
||||
public class DataSave extends BukkitRunnable {
|
||||
|
||||
public static int lastBackup = 0;
|
||||
public static int lastSave = 1;
|
||||
public static int autosave = 3;
|
||||
final public static String dataVersion = "1.1";
|
||||
public static DataSave running;
|
||||
|
||||
public ReadOldData read;
|
||||
private long time;
|
||||
public boolean collected = false;
|
||||
|
||||
// Not Thread-Safe! Needs to be run in main thread but uses async Read/Write
|
||||
public DataSave(ReadOldData read) {
|
||||
this.read = read;
|
||||
time = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
FileConfiguration oldData;
|
||||
if (read != null) {
|
||||
if (!read.done) {
|
||||
// Wait for async thread to load old data
|
||||
if (System.currentTimeMillis() - time > 30000) {
|
||||
P.p.errorLog("Old Data took too long to load!");
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
oldData = read.getData();
|
||||
} else {
|
||||
oldData = new YamlConfiguration();
|
||||
}
|
||||
try {
|
||||
cancel();
|
||||
} catch (IllegalStateException e) {
|
||||
}
|
||||
|
||||
FileConfiguration configFile = new YamlConfiguration();
|
||||
|
||||
if (!Brew.potions.isEmpty()) {
|
||||
Brew.save(configFile.createSection("Brew"));
|
||||
}
|
||||
|
||||
if (!BCauldron.bcauldrons.isEmpty() || oldData.contains("BCauldron")) {
|
||||
BCauldron.save(configFile.createSection("BCauldron"), oldData.getConfigurationSection("BCauldron"));
|
||||
}
|
||||
|
||||
if (!Barrel.barrels.isEmpty() || oldData.contains("Barrel")) {
|
||||
Barrel.save(configFile.createSection("Barrel"), oldData.getConfigurationSection("Barrel"));
|
||||
}
|
||||
|
||||
if (!BPlayer.isEmpty()) {
|
||||
BPlayer.save(configFile.createSection("Player"));
|
||||
}
|
||||
|
||||
if (!Wakeup.wakeups.isEmpty() || oldData.contains("Wakeup")) {
|
||||
Wakeup.save(configFile.createSection("Wakeup"), oldData.getConfigurationSection("Wakeup"));
|
||||
}
|
||||
|
||||
saveWorldNames(configFile, oldData.getConfigurationSection("Worlds"));
|
||||
configFile.set("Version", dataVersion);
|
||||
|
||||
collected = true;
|
||||
if (P.p.isEnabled()) {
|
||||
P.p.getServer().getScheduler().runTaskAsynchronously(P.p, new WriteData(configFile));
|
||||
} else {
|
||||
new WriteData(configFile).run();
|
||||
}
|
||||
}
|
||||
|
||||
// Finish the collection of data immediately
|
||||
public void now() {
|
||||
if (!read.done) {
|
||||
read.cancel();
|
||||
read.run();
|
||||
}
|
||||
if (!collected) {
|
||||
cancel();
|
||||
run();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Save all data. Takes a boolean whether all data should be collected in instantly
|
||||
public static void save(boolean collectInstant) {
|
||||
long time = System.nanoTime();
|
||||
if (running != null) {
|
||||
P.p.log("Another Save was started while a Save was in Progress");
|
||||
if (collectInstant) {
|
||||
running.now();
|
||||
}
|
||||
return;
|
||||
}
|
||||
File datafile = new File(P.p.getDataFolder(), "data.yml");
|
||||
|
||||
if (datafile.exists()) {
|
||||
ReadOldData read = new ReadOldData();
|
||||
if (collectInstant) {
|
||||
read.run();
|
||||
running = new DataSave(read);
|
||||
running.run();
|
||||
} else {
|
||||
read.runTaskAsynchronously(P.p);
|
||||
running = new DataSave(read);
|
||||
running.runTaskTimer(P.p, 1, 2);
|
||||
}
|
||||
} else {
|
||||
running = new DataSave(null);
|
||||
running.runTask(P.p);
|
||||
}
|
||||
P.p.debugLog("saving: " + ((System.nanoTime() - time) / 1000000.0) + "ms");
|
||||
}
|
||||
|
||||
public static void autoSave() {
|
||||
if (lastSave >= autosave) {
|
||||
save(false);// save all data
|
||||
} else {
|
||||
lastSave++;
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveWorldNames(FileConfiguration root, ConfigurationSection old) {
|
||||
if (old != null) {
|
||||
root.set("Worlds", old);
|
||||
}
|
||||
for (World world : P.p.getServer().getWorlds()) {
|
||||
String worldName = world.getName();
|
||||
if (worldName.startsWith("DXL_")) {
|
||||
worldName = P.p.getDxlName(worldName);
|
||||
root.set("Worlds." + worldName, 0);
|
||||
} else {
|
||||
worldName = world.getUID().toString();
|
||||
root.set("Worlds." + worldName, world.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -1,4 +1,4 @@
|
||||
package com.dre.brewery;
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -9,6 +9,8 @@ import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import com.dre.brewery.P;
|
||||
|
||||
public class DataUpdater {
|
||||
|
||||
private FileConfiguration data;
|
||||
@@ -40,7 +42,7 @@ public class DataUpdater {
|
||||
@SuppressWarnings("deprecation")
|
||||
public void update10() {
|
||||
|
||||
data.set("Version", P.dataVersion);
|
||||
data.set("Version", DataSave.dataVersion);
|
||||
|
||||
ConfigurationSection section = data.getConfigurationSection("Ingredients");
|
||||
try {
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.dre.brewery;
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import com.dre.brewery.P;
|
||||
|
||||
public class ReadOldData extends BukkitRunnable {
|
||||
|
||||
public FileConfiguration data;
|
||||
public boolean done = false;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
File datafile = new File(P.p.getDataFolder(), "data.yml");
|
||||
data = YamlConfiguration.loadConfiguration(datafile);
|
||||
|
||||
if (DataSave.lastBackup > 10) {
|
||||
datafile.renameTo(new File(P.p.getDataFolder(), "dataBackup.yml"));
|
||||
DataSave.lastBackup = 0;
|
||||
} else {
|
||||
DataSave.lastBackup++;
|
||||
}
|
||||
|
||||
done = true;
|
||||
}
|
||||
|
||||
public FileConfiguration getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.dre.brewery.filedata;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import com.dre.brewery.P;
|
||||
|
||||
public class WriteData implements Runnable {
|
||||
|
||||
private FileConfiguration data;
|
||||
|
||||
public WriteData(FileConfiguration data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
File datafile = new File(P.p.getDataFolder(), "data.yml");
|
||||
|
||||
try {
|
||||
data.save(datafile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
DataSave.lastSave = 1;
|
||||
DataSave.running = null;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import org.bukkit.World;
|
||||
import com.dre.brewery.P;
|
||||
import com.dre.brewery.BCauldron;
|
||||
import com.dre.brewery.Barrel;
|
||||
import com.dre.brewery.filedata.DataSave;
|
||||
|
||||
public class WorldListener implements Listener {
|
||||
|
||||
@@ -26,7 +27,7 @@ public class WorldListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onWorldUnload(WorldUnloadEvent event) {
|
||||
P.p.saveData();
|
||||
DataSave.save(true);
|
||||
Barrel.onUnload(event.getWorld().getName());
|
||||
BCauldron.onUnload(event.getWorld().getName());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user