mirror of
https://github.com/songoda/EpicFarming.git
synced 2024-11-27 21:15:28 +01:00
Mysql speed improved.
Data file will now periodically back up to prevent data loss. To be saved data is now cached before executed all at once as to prevent the possibility of data loss.
This commit is contained in:
parent
e79cc9552a
commit
be31a1ce5f
@ -144,44 +144,12 @@ public class EpicFarmingPlugin extends JavaPlugin implements EpicFarming {
|
||||
* Register Farms into FarmManger from configuration
|
||||
*/
|
||||
Bukkit.getScheduler().runTaskLater(this, () -> {
|
||||
// if (dataFile.getConfig().contains("Farms")) {
|
||||
// for (String locationStr : dataFile.getConfig().getConfigurationSection("Farms").getKeys(false)) {
|
||||
// Location location = Arconix.pl().getApi().serialize().unserializeLocation(locationStr);
|
||||
// if (location == null || location.getWorld() == null) continue;
|
||||
// int level = dataFile.getConfig().getInt("Farms." + locationStr + ".level");
|
||||
//
|
||||
// List<ItemStack> items = (List<ItemStack>) dataFile.getConfig().getList("Farms." + locationStr + ".Contents");
|
||||
//
|
||||
// String placedByStr = dataFile.getConfig().getString("Farms." + locationStr + ".placedBy");
|
||||
//
|
||||
// UUID placedBy = placedByStr == null ? null : UUID.fromString(placedByStr);
|
||||
//
|
||||
// EFarm farm = new EFarm(location, levelManager.getLevel(level), placedBy);
|
||||
// farm.loadInventory(items);
|
||||
//
|
||||
// farmManager.addFarm(location, farm);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Adding in Boosts
|
||||
// if (dataFile.getConfig().contains("data.boosts")) {
|
||||
// for (String key : dataFile.getConfig().getConfigurationSection("data.boosts").getKeys(false)) {
|
||||
// if (!dataFile.getConfig().contains("data.boosts." + key + ".Player")) continue;
|
||||
// BoostData boostData = new BoostData(
|
||||
// dataFile.getConfig().getInt("data.boosts." + key + ".Amount"),
|
||||
// Long.parseLong(key),
|
||||
// UUID.fromString(dataFile.getConfig().getString("data.boosts." + key + ".Player")));
|
||||
//
|
||||
// this.boostManager.addBoostToPlayer(boostData);
|
||||
// }
|
||||
// }
|
||||
if (storage.containsGroup("farms")) {
|
||||
for (StorageRow row : storage.getRowsByGroup("farms")) {
|
||||
Location location = Serialize.getInstance().unserializeLocation(row.getKey());
|
||||
if (location == null || location.getBlock() == null) return;
|
||||
|
||||
int level = row.get("level").asInt();
|
||||
Location loc = Arconix.pl().getApi().serialize().unserializeLocation(row.get("location").asString());
|
||||
List<ItemStack> items =row.get("contents").asItemStackList();
|
||||
UUID placedBY = UUID.fromString(row.get("placedby").asString());
|
||||
EFarm farm = new EFarm(location,levelManager.getLevel(level),placedBY);
|
||||
@ -249,12 +217,12 @@ public class EpicFarmingPlugin extends JavaPlugin implements EpicFarming {
|
||||
|
||||
public void onDisable() {
|
||||
saveToFile();
|
||||
this.storage.closeConnection();
|
||||
for (PlayerData playerData : playerActionManager.getRegisteredPlayers()) {
|
||||
if (playerData.getPlayer() != null)
|
||||
playerData.getPlayer().closeInventory();
|
||||
}
|
||||
CommandSender console = Bukkit.getConsoleSender();
|
||||
dataFile.saveConfig();
|
||||
console.sendMessage(Arconix.pl().getApi().format().formatText("&a============================="));
|
||||
console.sendMessage(Arconix.pl().getApi().format().formatText("&7EpicFarming " + this.getDescription().getVersion() + " by &5Brianna <3!"));
|
||||
console.sendMessage(Arconix.pl().getApi().format().formatText("&7Action: &cDisabling&7..."));
|
||||
@ -288,20 +256,15 @@ public class EpicFarmingPlugin extends JavaPlugin implements EpicFarming {
|
||||
private void saveToFile() {
|
||||
this.storage.closeConnection();
|
||||
checkStorage();
|
||||
// Wipe old kit information
|
||||
storage.clearFile();
|
||||
|
||||
/*
|
||||
* Dump FarmManager to file.
|
||||
*/
|
||||
for (Farm farm : farmManager.getFarms().values()) {
|
||||
if (farm.getLocation() == null
|
||||
|| farm.getLocation().getWorld() == null) continue;
|
||||
// String locationStr = Arconix.pl().getApi().serialize().serializeLocation(farm.getLocation());
|
||||
// dataFile.getConfig().set("Farms." + locationStr + ".level", farm.getLevel().getLevel());
|
||||
// dataFile.getConfig().set("Farms." + locationStr + ".placedBy", farm.getPlacedBy() == null ? null : farm.getPlacedBy().toString());
|
||||
// dataFile.getConfig().set("Farms." + locationStr + ".Contents", ((EFarm) farm).dumpInventory());
|
||||
String locstr = Arconix.pl().getApi().serialize().serializeLocation(farm.getLocation());
|
||||
storage.saveItem("farms",new StorageItem("location",locstr),
|
||||
storage.prepareSaveItem("farms",new StorageItem("location",locstr),
|
||||
new StorageItem("level",farm.getLevel().getLevel()),
|
||||
new StorageItem("placedby",farm.getPlacedBy().toString()),
|
||||
new StorageItem("contents",((EFarm)farm).dumpInventory()));
|
||||
@ -312,15 +275,13 @@ public class EpicFarmingPlugin extends JavaPlugin implements EpicFarming {
|
||||
*/
|
||||
for (BoostData boostData : boostManager.getBoosts()) {
|
||||
String endTime = String.valueOf(boostData.getEndTime());
|
||||
// dataFile.getConfig().set("data.boosts." + endTime + ".Player", boostData.getPlayer().toString());
|
||||
// dataFile.getConfig().set("data.boosts." + endTime + ".Amount", boostData.getMultiplier());
|
||||
storage.saveItem("boosts",new StorageItem("endtime",endTime),
|
||||
storage.prepareSaveItem("boosts",new StorageItem("endtime",endTime),
|
||||
new StorageItem("amount",boostData.getMultiplier()),
|
||||
new StorageItem("player",boostData.getPlayer()));
|
||||
}
|
||||
|
||||
//Save to file
|
||||
dataFile.saveConfig();
|
||||
storage.doSave();
|
||||
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ public abstract class Storage {
|
||||
public Storage(EpicFarmingPlugin instance) {
|
||||
this.instance = instance;
|
||||
this.dataFile = new ConfigWrapper(instance, "", "data.yml");
|
||||
this.dataFile.createNewFile(null, "EpicHoppers Data File");
|
||||
this.dataFile.createNewFile(null, "EpicFarming Data File");
|
||||
this.dataFile.getConfig().options().copyDefaults(true);
|
||||
this.dataFile.saveConfig();
|
||||
}
|
||||
@ -23,9 +23,9 @@ public abstract class Storage {
|
||||
|
||||
public abstract List<StorageRow> getRowsByGroup(String group);
|
||||
|
||||
public abstract void clearFile();
|
||||
public abstract void prepareSaveItem(String group, StorageItem... items);
|
||||
|
||||
public abstract void saveItem(String group, StorageItem... items);
|
||||
public abstract void doSave();
|
||||
|
||||
public abstract void closeConnection();
|
||||
|
||||
|
@ -10,6 +10,7 @@ import com.songoda.epicfarming.utils.MySQLDatabase;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -18,6 +19,7 @@ import java.util.Map;
|
||||
public class StorageMysql extends Storage {
|
||||
|
||||
private MySQLDatabase database;
|
||||
private static List<String> toSave = new ArrayList<>();
|
||||
|
||||
public StorageMysql(EpicFarmingPlugin instance) {
|
||||
super(instance);
|
||||
@ -28,13 +30,12 @@ public class StorageMysql extends Storage {
|
||||
public boolean containsGroup(String group) {
|
||||
try {
|
||||
DatabaseMetaData dbm = database.getConnection().getMetaData();
|
||||
ResultSet rs = dbm.getTables(null, null, instance.getConfig().getString("Database.Prefix")+group, null);
|
||||
ResultSet rs = dbm.getTables(null, null, instance.getConfig().getString("Database.Prefix") + group, null);
|
||||
if (rs.next()) {
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
instance.getServer().getPluginManager().disablePlugin(instance);
|
||||
}
|
||||
return false;
|
||||
|
||||
@ -64,43 +65,49 @@ public class StorageMysql extends Storage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearFile() {
|
||||
try {
|
||||
database.getConnection().createStatement().execute("TRUNCATE `" + instance.getConfig().getString("Database.Prefix") + "farms`");
|
||||
database.getConnection().createStatement().execute("TRUNCATE `" + instance.getConfig().getString("Database.Prefix") + "boosts`");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
public void prepareSaveItem(String group, StorageItem... items) {
|
||||
StringBuilder sql = new StringBuilder(String.format("INSERT INTO `" + instance.getConfig().getString("Database.Prefix") + "%s`", group));
|
||||
|
||||
sql.append(" (");
|
||||
|
||||
for (StorageItem item : items) {
|
||||
if (item == null || item.asObject() == null) continue;
|
||||
sql.append(String.format("`%s`, ", item.getKey()));
|
||||
}
|
||||
|
||||
sql = new StringBuilder(sql.substring(0, sql.length() - 2));
|
||||
|
||||
sql.append(") VALUES (");
|
||||
|
||||
for (StorageItem item : items) {
|
||||
if (item == null || item.asObject() == null) continue;
|
||||
sql.append(String.format("'%s', ", item.asObject().toString()));
|
||||
}
|
||||
|
||||
sql = new StringBuilder(sql.substring(0, sql.length() - 2));
|
||||
|
||||
sql.append(");");
|
||||
|
||||
toSave.add(sql.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveItem(String group, StorageItem... items) {
|
||||
public void doSave() {
|
||||
try {
|
||||
StringBuilder sql = new StringBuilder(String.format("INSERT INTO `" + instance.getConfig().getString("Database.Prefix") + "%s`", group));
|
||||
// Clear database
|
||||
database.getConnection().createStatement().execute("TRUNCATE `" + instance.getConfig().getString("Database.Prefix") + "farms`");
|
||||
database.getConnection().createStatement().execute("TRUNCATE `" + instance.getConfig().getString("Database.Prefix") + "boosts`");
|
||||
|
||||
sql.append(" (");
|
||||
Statement stmt = database.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
|
||||
|
||||
for (StorageItem item : items) {
|
||||
if (item == null || item.asObject() == null) continue;
|
||||
sql.append(String.format("`%s`, ", item.getKey()));
|
||||
for (String line : toSave) {
|
||||
stmt.addBatch(line);
|
||||
}
|
||||
|
||||
sql = new StringBuilder(sql.substring(0, sql.length() - 2));
|
||||
stmt.executeBatch();
|
||||
|
||||
sql.append(")");
|
||||
toSave.clear();
|
||||
|
||||
sql.append(" VALUES (");
|
||||
|
||||
for (StorageItem item : items) {
|
||||
if (item == null || item.asObject() == null) continue;
|
||||
sql.append(String.format("'%s', ", item.asObject().toString()));
|
||||
}
|
||||
|
||||
sql = new StringBuilder(sql.substring(0, sql.length() - 2));
|
||||
|
||||
sql.append(");");
|
||||
|
||||
database.getConnection().createStatement().execute(sql.toString());
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -115,4 +122,3 @@ public class StorageMysql extends Storage {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,16 +4,20 @@ import com.songoda.epicfarming.EpicFarmingPlugin;
|
||||
import com.songoda.epicfarming.storage.Storage;
|
||||
import com.songoda.epicfarming.storage.StorageItem;
|
||||
import com.songoda.epicfarming.storage.StorageRow;
|
||||
import com.songoda.epicfarming.utils.Debugger;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.MemorySection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
public class StorageYaml extends Storage {
|
||||
|
||||
private static final Map<String, Object> toSave = new HashMap<>();
|
||||
|
||||
public StorageYaml(EpicFarmingPlugin instance) {
|
||||
super(instance);
|
||||
}
|
||||
@ -44,23 +48,53 @@ public class StorageYaml extends Storage {
|
||||
}
|
||||
|
||||
private String convertToInLineList(String path) {
|
||||
String converted = "";
|
||||
StringBuilder converted = new StringBuilder();
|
||||
for (String key : dataFile.getConfig().getConfigurationSection(path).getKeys(false)) {
|
||||
converted += key + ":" + dataFile.getConfig().getInt(path + "." + key) + ";";
|
||||
converted.append(key).append(":").append(dataFile.getConfig().getInt(path + "." + key)).append(";");
|
||||
}
|
||||
return converted;
|
||||
return converted.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearFile() {
|
||||
dataFile.getConfig().set("data", null);
|
||||
public void prepareSaveItem(String group, StorageItem... items) {
|
||||
for (StorageItem item : items) {
|
||||
if (item == null || item.asObject() == null) continue;
|
||||
toSave.put("data." + group + "." + items[0].asString() + "." + item.getKey(), item.asObject());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveItem(String group, StorageItem... items) {
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
if (items[i] == null || items[i].asObject() == null) continue;
|
||||
dataFile.getConfig().set("data." + group + "." + items[0].asString() + "." + items[i].getKey(), items[i].asObject());
|
||||
public void doSave() {
|
||||
try {
|
||||
dataFile.getConfig().set("data", null); // Clear file
|
||||
|
||||
File data = new File(instance.getDataFolder() + "/data.yml");
|
||||
File dataClone = new File(instance.getDataFolder() + "/data-backup-" + System.currentTimeMillis() + ".yml");
|
||||
try {
|
||||
FileUtils.copyFile(data, dataClone);
|
||||
} catch (IOException e) {
|
||||
Debugger.runReport(e);
|
||||
}
|
||||
Deque<File> backups = new ArrayDeque<>();
|
||||
for (File file : Objects.requireNonNull(instance.getDataFolder().listFiles())) {
|
||||
if (file.getName().toLowerCase().contains("data-backup")) {
|
||||
backups.add(file);
|
||||
}
|
||||
}
|
||||
if (backups.size() > 5) {
|
||||
backups.getFirst().delete();
|
||||
}
|
||||
|
||||
|
||||
for (Map.Entry<String, Object> entry : toSave.entrySet()) {
|
||||
dataFile.getConfig().set(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
dataFile.saveConfig();
|
||||
|
||||
toSave.clear();
|
||||
} catch (NullPointerException e) {
|
||||
Debugger.runReport(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
name: EpicFarming
|
||||
description: EpicFarming
|
||||
main: com.songoda.epicfarming.EpicFarmingPlugin
|
||||
version: 2.0.12
|
||||
version: 2.0.13
|
||||
depend: [Arconix]
|
||||
soft-depend: [Multiverse-Core]
|
||||
author: Songoda
|
||||
|
Loading…
Reference in New Issue
Block a user