mirror of
https://github.com/darksaid98/AlathranWars.git
synced 2026-12-02 06:28:52 +00:00
WarData separation pass 1 (INCOMPLETE)
This commit is contained in:
1 parent
2cb609acc6
commit
388c600ce5
2 files changed
+127
-14
No files matched your search
@@ -2,6 +2,8 @@ package me.ShermansWorld.AlathraWar.data;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import com.sun.tools.javac.util.Pair;
|
||||
import org.bukkit.configuration.Configuration;
|
||||
import java.io.Reader;
|
||||
import java.io.InputStreamReader;
|
||||
@@ -10,17 +12,35 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import me.ShermansWorld.AlathraWar.Main;
|
||||
|
||||
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 javax.annotation.Nullable;
|
||||
|
||||
public class WarData
|
||||
{
|
||||
private Main plugin;
|
||||
private FileConfiguration dataConfig;
|
||||
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) {
|
||||
this.dataConfig = null;
|
||||
this.configFile = null;
|
||||
this.configConfig = null;
|
||||
this.warsData = new ArrayList<Pair<FileConfiguration, File>>();
|
||||
this.plugin = plugin;
|
||||
this.saveDefaultConfig();
|
||||
}
|
||||
@@ -29,27 +49,119 @@ public class WarData
|
||||
if (this.configFile == null) {
|
||||
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));
|
||||
this.dataConfig.setDefaults((Configuration)defaultConfig);
|
||||
|
||||
|
||||
|
||||
//Gather all war files in this
|
||||
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() {
|
||||
if (this.dataConfig == null) {
|
||||
|
||||
@Nullable
|
||||
public FileConfiguration getConfig(int id) {
|
||||
if (this.warsData.isEmpty() || this.warsData == null) {
|
||||
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() {
|
||||
if (this.dataConfig == null || this.configFile == null) {
|
||||
public void saveConfig(int id) {
|
||||
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;
|
||||
}
|
||||
try {
|
||||
this.getConfig().save(this.configFile);
|
||||
this.getConfigConfig().save(this.configFile);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in new issue
Block a user