mirror of
https://github.com/songoda/UltimateStacker.git
synced 2025-02-28 17:21:02 +01:00
Remove the old storage system.
This commit is contained in:
parent
4e889efcdb
commit
50f5a5d5b4
@ -34,14 +34,10 @@ import com.songoda.ultimatestacker.stackable.entity.EntityStack;
|
||||
import com.songoda.ultimatestacker.stackable.entity.EntityStackManager;
|
||||
import com.songoda.ultimatestacker.stackable.spawner.SpawnerStack;
|
||||
import com.songoda.ultimatestacker.stackable.spawner.SpawnerStackManager;
|
||||
import com.songoda.ultimatestacker.storage.Storage;
|
||||
import com.songoda.ultimatestacker.storage.StorageRow;
|
||||
import com.songoda.ultimatestacker.storage.types.StorageYaml;
|
||||
import com.songoda.ultimatestacker.tasks.StackingTask;
|
||||
import com.songoda.ultimatestacker.utils.Methods;
|
||||
import org.apache.commons.lang.WordUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Item;
|
||||
@ -50,7 +46,6 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.metadata.FixedMetadataValue;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
public class UltimateStacker extends SongodaPlugin {
|
||||
@ -215,31 +210,6 @@ public class UltimateStacker extends SongodaPlugin {
|
||||
|
||||
@Override
|
||||
public void onDataLoad() {
|
||||
// Legacy Data
|
||||
File folder = getDataFolder();
|
||||
File dataFile = new File(folder, "data.yml");
|
||||
|
||||
if (dataFile.exists()) {
|
||||
Storage storage = new StorageYaml(this);
|
||||
if (storage.containsGroup("spawners")) {
|
||||
for (StorageRow row : storage.getRowsByGroup("spawners")) {
|
||||
try {
|
||||
Location location = Methods.unserializeLocation(row.getKey());
|
||||
|
||||
SpawnerStack stack = new SpawnerStack(
|
||||
location,
|
||||
row.get("amount").asInt());
|
||||
|
||||
getDataManager().createSpawner(stack);
|
||||
} catch (Exception e) {
|
||||
console.sendMessage("Failed to load spawner.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
dataFile.delete();
|
||||
}
|
||||
|
||||
// Load current data.
|
||||
final boolean useSpawnerHolo = Settings.SPAWNER_HOLOGRAMS.getBoolean();
|
||||
this.dataManager.getSpawners((spawners) -> {
|
||||
|
@ -1,37 +0,0 @@
|
||||
package com.songoda.ultimatestacker.storage;
|
||||
|
||||
import com.songoda.core.configuration.Config;
|
||||
import com.songoda.ultimatestacker.UltimateStacker;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Storage {
|
||||
|
||||
protected final UltimateStacker instance;
|
||||
protected final Config dataFile;
|
||||
|
||||
public Storage(UltimateStacker instance) {
|
||||
this.instance = instance;
|
||||
this.dataFile = new Config(instance, "data.yml");
|
||||
this.dataFile.setHeader("UltimateStacker Data File");
|
||||
this.dataFile.setAutosave(true).setAutosaveInterval(120);
|
||||
}
|
||||
|
||||
public abstract boolean containsGroup(String group);
|
||||
|
||||
public abstract List<StorageRow> getRowsByGroup(String group);
|
||||
|
||||
public abstract void prepareSaveItem(String group, StorageItem... items);
|
||||
|
||||
public void updateData(UltimateStacker instance) {
|
||||
}
|
||||
|
||||
public abstract void doSave();
|
||||
|
||||
public abstract void save();
|
||||
|
||||
public abstract void makeBackup();
|
||||
|
||||
public abstract void closeConnection();
|
||||
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
package com.songoda.ultimatestacker.storage;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.EntityType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class StorageItem {
|
||||
|
||||
private final Object object;
|
||||
private String key = null;
|
||||
|
||||
public StorageItem(Object object) {
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
public StorageItem(String key, Object object) {
|
||||
this.key = key;
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
public StorageItem(String key, List<Material> material) {
|
||||
String object = "";
|
||||
for (Material m : material) {
|
||||
object += m.name() + ";";
|
||||
}
|
||||
this.key = key;
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
public StorageItem(String key, Map<EntityType, Integer> entityKills) {
|
||||
String object = "";
|
||||
for (Map.Entry<EntityType, Integer> entry : entityKills.entrySet()) {
|
||||
object += entry.getKey().name() + ":" + entry.getValue() + ";";
|
||||
}
|
||||
this.key = key;
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public String asString() {
|
||||
if (object == null) return null;
|
||||
return (String) object;
|
||||
}
|
||||
|
||||
public boolean asBoolean() {
|
||||
if (object == null) return false;
|
||||
return (boolean) object;
|
||||
}
|
||||
|
||||
public int asInt() {
|
||||
if (object == null) return 0;
|
||||
return (int) object;
|
||||
}
|
||||
|
||||
public Object asObject() {
|
||||
return object;
|
||||
}
|
||||
|
||||
public List<Material> asMaterialList() {
|
||||
List<Material> list = new ArrayList<>();
|
||||
if (object == null) return list;
|
||||
String[] stack = ((String) object).split(";");
|
||||
for (String item : stack) {
|
||||
if (item.equals("")) continue;
|
||||
list.add(Material.valueOf(item));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package com.songoda.ultimatestacker.storage;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
public class StorageRow {
|
||||
|
||||
private final String key;
|
||||
|
||||
private final Map<String, StorageItem> items;
|
||||
|
||||
public StorageRow(String key, Map<String, StorageItem> items) {
|
||||
this.key = key;
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public Map<String, StorageItem> getItems() {
|
||||
return Collections.unmodifiableMap(items);
|
||||
}
|
||||
|
||||
public StorageItem get(String key) {
|
||||
if (!items.containsKey(key) || items.get(key).asObject().toString().equals("")) return new StorageItem(null);
|
||||
return items.get(key);
|
||||
}
|
||||
}
|
@ -1,195 +0,0 @@
|
||||
package com.songoda.ultimatestacker.storage.types;
|
||||
|
||||
import com.songoda.ultimatestacker.UltimateStacker;
|
||||
import com.songoda.ultimatestacker.storage.Storage;
|
||||
import com.songoda.ultimatestacker.storage.StorageItem;
|
||||
import com.songoda.ultimatestacker.storage.StorageRow;
|
||||
import com.songoda.ultimatestacker.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;
|
||||
import java.util.Map;
|
||||
|
||||
public class StorageMysql extends Storage {
|
||||
|
||||
private static Map<String, StorageItem[]> toSave = new HashMap<>();
|
||||
private static Map<String, StorageItem[]> lastSave = null;
|
||||
private MySQLDatabase database;
|
||||
|
||||
public StorageMysql(UltimateStacker instance) {
|
||||
super(instance);
|
||||
this.database = new MySQLDatabase(instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsGroup(String group) {
|
||||
try {
|
||||
DatabaseMetaData dbm = database.getConnection().getMetaData();
|
||||
ResultSet rs = dbm.getTables(null, null, instance.getConfig().getString("Database.Prefix") + group, null);
|
||||
if (rs.next()) {
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StorageRow> getRowsByGroup(String group) {
|
||||
List<StorageRow> rows = new ArrayList<>();
|
||||
try {
|
||||
ResultSet set = database.getConnection().createStatement().executeQuery(String.format("SELECT * FROM `" + instance.getConfig().getString("Database.Prefix") + "%s`", group));
|
||||
while (set.next()) {
|
||||
Map<String, StorageItem> items = new HashMap<>();
|
||||
|
||||
String key = set.getString(1);
|
||||
for (int i = 2; i <= set.getMetaData().getColumnCount(); i++) {
|
||||
if (set.getObject(i) == null || set.getObject(i) == "") continue;
|
||||
StorageItem item = new StorageItem(set.getObject(i));
|
||||
items.put(set.getMetaData().getColumnName(i), item);
|
||||
}
|
||||
StorageRow row = new StorageRow(key, items);
|
||||
rows.add(row);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareSaveItem(String group, StorageItem... items) {
|
||||
toSave.put(group + "]" + items[0].asObject().toString(), items);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doSave() {
|
||||
this.updateData(instance);
|
||||
|
||||
if (lastSave == null)
|
||||
lastSave = new HashMap<>(toSave);
|
||||
|
||||
if (toSave.isEmpty()) return;
|
||||
Map<String, StorageItem[]> nextSave = new HashMap<>(toSave);
|
||||
|
||||
this.makeBackup();
|
||||
this.save();
|
||||
|
||||
toSave.clear();
|
||||
lastSave.clear();
|
||||
lastSave.putAll(nextSave);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
try {
|
||||
Statement stmt = database.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
|
||||
|
||||
last:
|
||||
for (Map.Entry<String, StorageItem[]> last : lastSave.entrySet()) {
|
||||
String lastKey = last.getKey().split("]")[0];
|
||||
String lastValue = last.getValue()[0].asObject().toString();
|
||||
|
||||
for (Map.Entry<String, StorageItem[]> to : toSave.entrySet()) {
|
||||
String toKey = to.getKey().split("]")[0];
|
||||
if (!toKey.equals(lastKey)
|
||||
|| !to.getValue()[0].asObject().equals(lastValue)
|
||||
|| to.getValue().length != last.getValue().length)
|
||||
continue;
|
||||
toSave.remove(to.getKey());
|
||||
for (int i = 0; i < to.getValue().length; i ++) {
|
||||
if (!to.getValue()[i].asObject().toString()
|
||||
.equals(last.getValue()[i].asObject().toString())) {
|
||||
//Update
|
||||
StorageItem[] items = to.getValue();
|
||||
StringBuilder sql = new StringBuilder(String.format("UPDATE `" + instance.getConfig().getString("Database.Prefix") + "%s`", toKey));
|
||||
|
||||
sql.append(" SET");
|
||||
|
||||
for (StorageItem item : items) {
|
||||
if (item == null || item.asObject() == null) continue;
|
||||
String key = item.getKey().split("]")[0];
|
||||
sql.append(String.format("`%s` = '%s', ", key, item.asObject().toString()));
|
||||
}
|
||||
|
||||
sql = new StringBuilder(sql.substring(0, sql.length() - 2));
|
||||
|
||||
sql.append(String.format(" WHERE `%s`='%s'", last.getValue()[0].getKey(), last.getValue()[0].asObject().toString()));
|
||||
|
||||
stmt.addBatch(sql.toString());
|
||||
|
||||
continue last;
|
||||
}
|
||||
}
|
||||
// Already up to date.
|
||||
|
||||
continue last;
|
||||
}
|
||||
//Was not found delete.
|
||||
StringBuilder sql = new StringBuilder(String.format("DELETE FROM `" + instance.getConfig().getString("Database.Prefix") + "%s`", lastKey));
|
||||
sql.append(String.format(" WHERE `%s`='%s'", last.getValue()[0].getKey(), last.getValue()[0].asObject().toString()));
|
||||
stmt.addBatch(sql.toString());
|
||||
|
||||
}
|
||||
|
||||
for (Map.Entry<String, StorageItem[]> to : toSave.entrySet()) {
|
||||
String toKey = to.getKey().split("]")[0];
|
||||
//Add
|
||||
StorageItem[] items = to.getValue();
|
||||
StringBuilder sql = new StringBuilder(String.format("INSERT INTO `" + instance.getConfig().getString("Database.Prefix") + "%s`", toKey));
|
||||
|
||||
sql.append(" (");
|
||||
|
||||
for (StorageItem item : items) {
|
||||
if (item == null || item.asObject() == null) continue;
|
||||
String key = item.getKey().split("]")[0];
|
||||
sql.append(String.format("`%s`, ", key));
|
||||
}
|
||||
|
||||
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(");");
|
||||
|
||||
stmt.addBatch(sql.toString());
|
||||
}
|
||||
|
||||
stmt.executeBatch();
|
||||
|
||||
toSave.clear();
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeBackup() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeConnection() {
|
||||
try {
|
||||
database.getConnection().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,147 +0,0 @@
|
||||
package com.songoda.ultimatestacker.storage.types;
|
||||
|
||||
import com.songoda.ultimatestacker.UltimateStacker;
|
||||
import com.songoda.ultimatestacker.storage.Storage;
|
||||
import com.songoda.ultimatestacker.storage.StorageItem;
|
||||
import com.songoda.ultimatestacker.storage.StorageRow;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.MemorySection;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class StorageYaml extends Storage {
|
||||
|
||||
private static final Map<String, Object> toSave = new HashMap<>();
|
||||
private static Map<String, Object> lastSave = null;
|
||||
|
||||
public StorageYaml(UltimateStacker instance) {
|
||||
super(instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsGroup(String group) {
|
||||
return dataFile.contains("data." + group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StorageRow> getRowsByGroup(String group) {
|
||||
List<StorageRow> rows = new ArrayList<>();
|
||||
ConfigurationSection currentSection = dataFile.getConfigurationSection("data." + group);
|
||||
for (String key : currentSection.getKeys(false)) {
|
||||
|
||||
Map<String, StorageItem> items = new HashMap<>();
|
||||
ConfigurationSection currentSection2 = dataFile.getConfigurationSection("data." + group + "." + key);
|
||||
for (String key2 : currentSection2.getKeys(false)) {
|
||||
String path = "data." + group + "." + key + "." + key2;
|
||||
items.put(key2, new StorageItem(dataFile.get(path) instanceof MemorySection
|
||||
? convertToInLineList(path) : dataFile.get(path)));
|
||||
}
|
||||
if (items.isEmpty()) continue;
|
||||
StorageRow row = new StorageRow(key, items);
|
||||
rows.add(row);
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
private String convertToInLineList(String path) {
|
||||
StringBuilder converted = new StringBuilder();
|
||||
for (String key : dataFile.getConfigurationSection(path).getKeys(false)) {
|
||||
converted.append(key).append(":").append(dataFile.getInt(path + "." + key)).append(";");
|
||||
}
|
||||
return converted.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
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 doSave() {
|
||||
this.updateData(instance);
|
||||
|
||||
if (lastSave == null)
|
||||
lastSave = new HashMap<>(toSave);
|
||||
|
||||
if (toSave.isEmpty()) return;
|
||||
Map<String, Object> nextSave = new HashMap<>(toSave);
|
||||
|
||||
this.makeBackup();
|
||||
this.save();
|
||||
|
||||
toSave.clear();
|
||||
lastSave.clear();
|
||||
lastSave.putAll(nextSave);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
try {
|
||||
for (Map.Entry<String, Object> entry : lastSave.entrySet()) {
|
||||
if (toSave.containsKey(entry.getKey())) {
|
||||
Object newValue = toSave.get(entry.getKey());
|
||||
if (!entry.getValue().equals(newValue)) {
|
||||
dataFile.set(entry.getKey(), newValue);
|
||||
}
|
||||
toSave.remove(entry.getKey());
|
||||
} else {
|
||||
dataFile.set(entry.getKey(), null);
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Object> entry : toSave.entrySet()) {
|
||||
dataFile.set(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
dataFile.save();
|
||||
} catch (NullPointerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeBackup() {
|
||||
File data = new File(instance.getDataFolder(), "data.yml");
|
||||
File dataClone = new File(instance.getDataFolder(), "data-backup-" + System.currentTimeMillis() + ".yml");
|
||||
try {
|
||||
copyFile(data, dataClone);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
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() > 3) {
|
||||
backups.getFirst().delete();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeConnection() {
|
||||
dataFile.saveChanges();
|
||||
}
|
||||
|
||||
private static void copyFile(File source, File dest) throws IOException {
|
||||
InputStream is = null;
|
||||
OutputStream os = null;
|
||||
try {
|
||||
is = new FileInputStream(source);
|
||||
os = new FileOutputStream(dest);
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
while ((length = is.read(buffer)) > 0) {
|
||||
os.write(buffer, 0, length);
|
||||
}
|
||||
} finally {
|
||||
is.close();
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user