Merge pull request #11 from SBDPlugins/feature/removeprotocollib
Replaced config handler (fixes resets), bumped to v1.4.1
This commit is contained in:
commit
7311ddbeec
3 changed files with 30 additions and 39 deletions
2
pom.xml
2
pom.xml
|
@ -24,7 +24,7 @@
|
||||||
|
|
||||||
<groupId>tech.sbdevelopment</groupId>
|
<groupId>tech.sbdevelopment</groupId>
|
||||||
<artifactId>MapReflectionAPI</artifactId>
|
<artifactId>MapReflectionAPI</artifactId>
|
||||||
<version>1.4</version>
|
<version>1.4.1</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>MapReflectionAPI</name>
|
<name>MapReflectionAPI</name>
|
||||||
|
|
|
@ -37,7 +37,6 @@ public class Configuration {
|
||||||
|
|
||||||
private Configuration(JavaPlugin plugin) {
|
private Configuration(JavaPlugin plugin) {
|
||||||
this.file = new YamlFile(plugin, "config");
|
this.file = new YamlFile(plugin, "config");
|
||||||
this.file.loadDefaults();
|
|
||||||
reload();
|
reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,12 +18,15 @@
|
||||||
|
|
||||||
package tech.sbdevelopment.mapreflectionapi.utils;
|
package tech.sbdevelopment.mapreflectionapi.utils;
|
||||||
|
|
||||||
import com.google.common.io.ByteStreams;
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class YamlFile {
|
public class YamlFile {
|
||||||
private final JavaPlugin plugin;
|
private final JavaPlugin plugin;
|
||||||
|
@ -35,56 +38,45 @@ public class YamlFile {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
|
||||||
if (!plugin.getDataFolder().exists() && !plugin.getDataFolder().mkdir()) {
|
saveDefaultFile();
|
||||||
plugin.getLogger().severe("Couldn't generate the pluginfolder!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.file = new File(plugin.getDataFolder(), name + ".yml");
|
|
||||||
if (!this.file.exists()) {
|
|
||||||
try {
|
|
||||||
if (!this.file.createNewFile()) {
|
|
||||||
plugin.getLogger().severe("Couldn't generate the " + name + ".yml!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
plugin.getLogger().info("Generating the " + name + ".yml...");
|
|
||||||
} catch (IOException e) {
|
|
||||||
plugin.getLogger().severe("Couldn't generate the " + name + ".yml!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadDefaults() {
|
public void reloadFile() {
|
||||||
try {
|
if (this.file == null)
|
||||||
InputStream in = plugin.getResource(name + ".yml");
|
this.file = new File(this.plugin.getDataFolder(), name + ".yml");
|
||||||
if (in == null) {
|
|
||||||
plugin.getLogger().severe("Expected the resource " + name + ".yml, but it was not found in the plugin JAR!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
OutputStream out = new FileOutputStream(this.file);
|
this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file);
|
||||||
ByteStreams.copy(in, out);
|
|
||||||
reload();
|
InputStream defaultStream = this.plugin.getResource(name + ".yml");
|
||||||
} catch (IOException e) {
|
if (defaultStream != null) {
|
||||||
plugin.getLogger().severe("Couldn't load the default " + name + ".yml!");
|
YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defaultStream));
|
||||||
|
this.fileConfiguration.setDefaults(defaultConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileConfiguration getFile() {
|
public FileConfiguration getFile() {
|
||||||
|
if (this.fileConfiguration == null)
|
||||||
|
reloadFile();
|
||||||
|
|
||||||
return this.fileConfiguration;
|
return this.fileConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save() {
|
public void saveFile() {
|
||||||
|
if (this.fileConfiguration == null || this.file == null)
|
||||||
|
return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.fileConfiguration.save(this.file);
|
this.fileConfiguration.save(this.file);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
plugin.getLogger().severe("Couldn't save the " + name + ".yml!");
|
plugin.getLogger().log(Level.SEVERE, "Couldn't save the file " + this.name + ".yml.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reload() {
|
public void saveDefaultFile() {
|
||||||
this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file);
|
if (this.file == null)
|
||||||
|
this.file = new File(this.plugin.getDataFolder(), name + ".yml");
|
||||||
|
|
||||||
|
if (!this.file.exists())
|
||||||
|
this.plugin.saveResource(name + ".yml", false);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue