mirror of
https://github.com/darksaid98/AlathranWars.git
synced 2026-12-03 22:15:24 +00:00
WarData separation pass 1 (INCOMPLETE)
This commit is contained in:
1 parent
2cb609acc6
commit
388c600ce5
2 files changed
+126
-13
No files matched your search
@@ -0,0 +1 @@
|
|||||||
|
id:
|
||||||
@@ -2,6 +2,8 @@ package me.ShermansWorld.AlathraWar.data;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import com.sun.tools.javac.util.Pair;
|
||||||
import org.bukkit.configuration.Configuration;
|
import org.bukkit.configuration.Configuration;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
@@ -10,17 +12,35 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
|||||||
import me.ShermansWorld.AlathraWar.Main;
|
import me.ShermansWorld.AlathraWar.Main;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public class WarData
|
public class WarData
|
||||||
{
|
{
|
||||||
private Main plugin;
|
private Main plugin;
|
||||||
private FileConfiguration dataConfig;
|
|
||||||
private File configFile;
|
private File configFile;
|
||||||
|
private FileConfiguration configConfig;
|
||||||
|
|
||||||
|
|
||||||
|
//Stored as a pair so theres no need to sync two lists
|
||||||
|
private final List<Pair<FileConfiguration, File>> warsData;
|
||||||
|
|
||||||
public WarData(final Main plugin) {
|
public WarData(final Main plugin) {
|
||||||
this.dataConfig = null;
|
|
||||||
this.configFile = null;
|
this.configFile = null;
|
||||||
|
this.configConfig = null;
|
||||||
|
this.warsData = new ArrayList<Pair<FileConfiguration, File>>();
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.saveDefaultConfig();
|
this.saveDefaultConfig();
|
||||||
}
|
}
|
||||||
@@ -29,27 +49,119 @@ public class WarData
|
|||||||
if (this.configFile == null) {
|
if (this.configFile == null) {
|
||||||
this.configFile = new File(this.plugin.getDataFolder(), "wars.yml");
|
this.configFile = new File(this.plugin.getDataFolder(), "wars.yml");
|
||||||
}
|
}
|
||||||
this.dataConfig = (FileConfiguration)YamlConfiguration.loadConfiguration(this.configFile);
|
|
||||||
final InputStream defaultStream = this.plugin.getResource("wars.yml");
|
|
||||||
if (defaultStream != null) {
|
|
||||||
final YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration((Reader)new InputStreamReader(defaultStream));
|
//Gather all war files in this
|
||||||
this.dataConfig.setDefaults((Configuration)defaultConfig);
|
warsData.clear();
|
||||||
|
Stream<Path> stream = null;
|
||||||
|
List<File> warsFiles = new ArrayList<File>();
|
||||||
|
try {
|
||||||
|
stream = Files.list(Paths.get(new URI(this.plugin.getDataFolder().toString() + "/wars/")));
|
||||||
|
warsFiles.addAll(stream.filter(file -> !Files.isDirectory(file))
|
||||||
|
.map(Path::toFile)
|
||||||
|
.filter(file -> {
|
||||||
|
if (file.getName().split(".").length > 1) {
|
||||||
|
if (file.getName().split(".")[1].equalsIgnoreCase("yml")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList()));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for(File file : warsFiles) {
|
||||||
|
this.warsData.add(i, new Pair(YamlConfiguration.loadConfiguration(file), file));
|
||||||
|
//template
|
||||||
|
final InputStream defaultStream = this.plugin.getResource("warstemplate.yml");
|
||||||
|
if (defaultStream != null) {
|
||||||
|
final YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration((Reader)new InputStreamReader(defaultStream));
|
||||||
|
this.warsData.get(i).fst.setDefaults((Configuration)defaultConfig);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileConfiguration getConfig() {
|
@Nullable
|
||||||
if (this.dataConfig == null) {
|
public FileConfiguration getConfig(int id) {
|
||||||
|
if (this.warsData.isEmpty() || this.warsData == null) {
|
||||||
this.reloadConfig();
|
this.reloadConfig();
|
||||||
}
|
}
|
||||||
return this.dataConfig;
|
|
||||||
|
|
||||||
|
for (Pair<FileConfiguration,File> file : this.warsData) {
|
||||||
|
if(file.fst.getInt("id") == id) {
|
||||||
|
return file.fst;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveConfig() {
|
public void saveConfig(int id) {
|
||||||
if (this.dataConfig == null || this.configFile == null) {
|
if (this.warsData.isEmpty() || this.warsData == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileConfiguration toSave = null;
|
||||||
|
|
||||||
|
for (int i = 0; i < warsData.size(); i++) {
|
||||||
|
if (warsData.get(i).fst.getInt("id") == id) {
|
||||||
|
try {
|
||||||
|
warsData.get(i).fst.save(warsData.get(i).snd);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Archive a wars file for later reference (deleted basically)
|
||||||
|
* Also removes the war from the data list
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
|
public void archiveWar(int id) {
|
||||||
|
if (this.warsData.isEmpty() || this.warsData == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < warsData.size(); i++) {
|
||||||
|
if (warsData.get(i).fst.getInt("id") == id) {
|
||||||
|
try {
|
||||||
|
Path archive = Paths.get(this.plugin.getDataFolder().toString() + "/wars/archive/" + warsData.get(i).snd.getName() + ".old");
|
||||||
|
Files.copy(warsData.get(i).snd.toPath(), archive, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
Files.delete(warsData.get(i).snd.toPath());
|
||||||
|
warsData.remove(i);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public FileConfiguration getConfigConfig() {
|
||||||
|
if (this.warsData.isEmpty() || this.warsData == null) {
|
||||||
|
this.reloadConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.configConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveConfigConfig() {
|
||||||
|
if (this.configConfig == null || this.configFile == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
this.getConfig().save(this.configFile);
|
this.getConfigConfig().save(this.configFile);
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
Reference in new issue
Block a user