Tweaked performance of data save and campfire check

This commit is contained in:
Sn0wStorm 2019-11-24 16:06:17 +01:00
parent a1c0116650
commit 32b6c3c4b1
6 changed files with 56 additions and 29 deletions

View File

@ -26,7 +26,7 @@ import java.util.UUID;
public class BCauldron {
public static final byte EMPTY = 0, SOME = 1, FULL = 2;
private static Set<UUID> plInteracted = new HashSet<>();
private static Set<UUID> plInteracted = new HashSet<>(); // Interact Event helper
public static Map<Block, BCauldron> bcauldrons = new HashMap<>(); // All active cauldrons. Mapped to their block for fast retrieve
private BIngredients ingredients = new BIngredients();

View File

@ -15,6 +15,7 @@ import com.dre.brewery.utility.LegacyUtil;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.HandlerList;
@ -315,6 +316,11 @@ public class P extends JavaPlugin {
e.printStackTrace();
}*/
if (use1_14) {
// Campfires are weird
// Initialize once now so it doesn't lag later when we check for campfires under Cauldrons
getServer().createBlockData(Material.CAMPFIRE);
}
// load the Config
try {
@ -575,17 +581,32 @@ public class P extends JavaPlugin {
public class BreweryRunnable implements Runnable {
@Override
public void run() {
//long t1 = System.nanoTime();
BConfig.reloader = null;
for (BCauldron cauldron : BCauldron.bcauldrons.values()) {
cauldron.onUpdate();// runs every min to update cooking time
}
//long t2 = System.nanoTime();
Barrel.onUpdate();// runs every min to check and update ageing time
//long t3 = System.nanoTime();
if (use1_14) MCBarrel.onUpdate();
//long t4 = System.nanoTime();
BPlayer.onUpdate();// updates players drunkeness
//long t5 = System.nanoTime();
debugLog("Update");
//long t6 = System.nanoTime();
DataSave.autoSave();
//long t7 = System.nanoTime();
/*P.p.log("BreweryRunnable: " +
"t1: " + (t2 - t1) / 1000000.0 + "ms" +
" | t2: " + (t3 - t2) / 1000000.0 + "ms" +
" | t3: " + (t4 - t3) / 1000000.0 + "ms" +
" | t4: " + (t5 - t4) / 1000000.0 + "ms" +
" | t5: " + (t6 - t5) / 1000000.0 + "ms" +
" | t6: " + (t7 - t6) / 1000000.0 + "ms" );*/
}
}

View File

@ -32,8 +32,18 @@ public class BData {
File file = new File(P.p.getDataFolder(), "data.yml");
if (file.exists()) {
long t1 = System.currentTimeMillis();
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
long t2 = System.currentTimeMillis();
if (t2 - t1 > 5000) {
// Spigot is very slow at loading inventories from yml. Notify Admin that loading Data took long
P.p.log("Bukkit took " + (t2 - t1) / 1000.0 + "s to load the Data File,");
P.p.log("consider switching to Paper, or have less items in Barrels");
}
Brew.installTime = data.getLong("installTime", System.currentTimeMillis());
MCBarrel.mcBarrelTime = data.getLong("MCBarrelTime", 0);

View File

@ -1,12 +1,7 @@
package com.dre.brewery.filedata;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import com.dre.brewery.MCBarrel;
import com.dre.brewery.*;
import com.dre.brewery.utility.BUtil;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
@ -14,12 +9,8 @@ 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;
import java.util.ArrayList;
import java.util.List;
public class DataSave extends BukkitRunnable {
@ -42,6 +33,7 @@ public class DataSave extends BukkitRunnable {
@Override
public void run() {
long saveTime = System.nanoTime();
FileConfiguration oldData;
if (read != null) {
if (!read.done) {
@ -103,6 +95,9 @@ public class DataSave extends BukkitRunnable {
configFile.set("Version", dataVersion);
collected = true;
P.p.debugLog("saving: " + ((System.nanoTime() - saveTime) / 1000000.0) + "ms");
if (P.p.isEnabled()) {
P.p.getServer().getScheduler().runTaskAsynchronously(P.p, new WriteData(configFile));
} else {
@ -126,7 +121,6 @@ public class DataSave extends BukkitRunnable {
// 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) {
@ -134,24 +128,17 @@ public class DataSave extends BukkitRunnable {
}
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);
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);
}
P.p.debugLog("saving: " + ((System.nanoTime() - time) / 1000000.0) + "ms");
}
public static void autoSave() {

View File

@ -18,6 +18,12 @@ public class ReadOldData extends BukkitRunnable {
@Override
public void run() {
File datafile = new File(P.p.getDataFolder(), "data.yml");
if (!datafile.exists()) {
data = new YamlConfiguration();
done = true;
return;
}
data = YamlConfiguration.loadConfiguration(datafile);
if (DataSave.lastBackup > 10) {

View File

@ -8,6 +8,9 @@ import org.bukkit.configuration.file.FileConfiguration;
import com.dre.brewery.P;
/**
* Writes the collected Data to file in Async Thread
*/
public class WriteData implements Runnable {
private FileConfiguration data;