mirror of
https://github.com/songoda/UltimateStacker.git
synced 2024-12-31 21:07:47 +01:00
settings refactor
This commit is contained in:
parent
d628eb9f8c
commit
96f4576190
10
pom.xml
10
pom.xml
@ -102,6 +102,16 @@
|
||||
<artifactId>jobs</artifactId>
|
||||
<version>4.10.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.bgsoftware</groupId>
|
||||
<artifactId>WildStacker</artifactId>
|
||||
<version>2-9-0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>uk.antiperson</groupId>
|
||||
<artifactId>stackmob</artifactId>
|
||||
<version>4-0-2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>me.minebuilders</groupId>
|
||||
<artifactId>Clearlag</artifactId>
|
||||
|
@ -1,27 +1,21 @@
|
||||
package com.songoda.ultimatestacker.storage;
|
||||
|
||||
import com.songoda.core.configuration.Config;
|
||||
import com.songoda.ultimatestacker.UltimateStacker;
|
||||
import com.songoda.ultimatestacker.utils.ConfigWrapper;
|
||||
import com.songoda.ultimatestacker.utils.Methods;
|
||||
import com.songoda.ultimatestacker.entity.EntityStack;
|
||||
import com.songoda.ultimatestacker.spawner.SpawnerStack;
|
||||
import com.songoda.ultimatestacker.utils.Methods;
|
||||
import org.bukkit.Material;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Storage {
|
||||
|
||||
protected final UltimateStacker instance;
|
||||
protected final ConfigWrapper dataFile;
|
||||
protected final Config dataFile;
|
||||
|
||||
public Storage(UltimateStacker instance) {
|
||||
this.instance = instance;
|
||||
this.dataFile = new ConfigWrapper(instance, "", "data.yml");
|
||||
this.dataFile.createNewFile(null, "UltimateStacker Data File");
|
||||
this.dataFile.getConfig().options().copyDefaults(true);
|
||||
this.dataFile.saveConfig();
|
||||
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);
|
||||
|
@ -14,7 +14,6 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StorageMysql extends Storage {
|
||||
|
||||
|
@ -6,9 +6,19 @@ 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.*;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class StorageYaml extends Storage {
|
||||
|
||||
@ -21,21 +31,21 @@ public class StorageYaml extends Storage {
|
||||
|
||||
@Override
|
||||
public boolean containsGroup(String group) {
|
||||
return dataFile.getConfig().contains("data." + group);
|
||||
return dataFile.contains("data." + group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StorageRow> getRowsByGroup(String group) {
|
||||
List<StorageRow> rows = new ArrayList<>();
|
||||
ConfigurationSection currentSection = dataFile.getConfig().getConfigurationSection("data." + group);
|
||||
ConfigurationSection currentSection = dataFile.getConfigurationSection("data." + group);
|
||||
for (String key : currentSection.getKeys(false)) {
|
||||
|
||||
Map<String, StorageItem> items = new HashMap<>();
|
||||
ConfigurationSection currentSection2 = dataFile.getConfig().getConfigurationSection("data." + group + "." + key);
|
||||
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.getConfig().get(path) instanceof MemorySection
|
||||
? convertToInLineList(path) : dataFile.getConfig().get(path)));
|
||||
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);
|
||||
@ -46,8 +56,8 @@ public class StorageYaml extends Storage {
|
||||
|
||||
private String convertToInLineList(String path) {
|
||||
StringBuilder converted = new StringBuilder();
|
||||
for (String key : dataFile.getConfig().getConfigurationSection(path).getKeys(false)) {
|
||||
converted.append(key).append(":").append(dataFile.getConfig().getInt(path + "." + key)).append(";");
|
||||
for (String key : dataFile.getConfigurationSection(path).getKeys(false)) {
|
||||
converted.append(key).append(":").append(dataFile.getInt(path + "." + key)).append(";");
|
||||
}
|
||||
return converted.toString();
|
||||
}
|
||||
@ -85,19 +95,19 @@ public class StorageYaml extends Storage {
|
||||
if (toSave.containsKey(entry.getKey())) {
|
||||
Object newValue = toSave.get(entry.getKey());
|
||||
if (!entry.getValue().equals(newValue)) {
|
||||
dataFile.getConfig().set(entry.getKey(), newValue);
|
||||
dataFile.set(entry.getKey(), newValue);
|
||||
}
|
||||
toSave.remove(entry.getKey());
|
||||
} else {
|
||||
dataFile.getConfig().set(entry.getKey(), null);
|
||||
dataFile.set(entry.getKey(), null);
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Object> entry : toSave.entrySet()) {
|
||||
dataFile.getConfig().set(entry.getKey(), entry.getValue());
|
||||
dataFile.set(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
dataFile.saveConfig();
|
||||
dataFile.save();
|
||||
} catch (NullPointerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -125,10 +135,9 @@ public class StorageYaml extends Storage {
|
||||
|
||||
@Override
|
||||
public void closeConnection() {
|
||||
dataFile.saveConfig();
|
||||
dataFile.saveChanges();
|
||||
}
|
||||
|
||||
|
||||
private static void copyFile(File source, File dest) throws IOException {
|
||||
InputStream is = null;
|
||||
OutputStream os = null;
|
||||
|
Loading…
Reference in New Issue
Block a user