config woo
This commit is contained in:
parent
9140fa06a6
commit
0950b1b2dd
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
# Ignore Maven target directory
|
# Ignore Maven target directory
|
||||||
target/
|
target/
|
||||||
|
out/
|
||||||
|
|
||||||
# Ignore IntelliJ IDEA project files
|
# Ignore IntelliJ IDEA project files
|
||||||
.idea/
|
.idea/
|
||||||
|
|
|
||||||
30
src/main/java/eu/creeper/mc/creeperPit/config/Init.java
Normal file
30
src/main/java/eu/creeper/mc/creeperPit/config/Init.java
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
package eu.creeper.mc.creeperPit.config;
|
||||||
|
|
||||||
|
import eu.creeper.mc.creeperPit.config.manager.Directory;
|
||||||
|
import eu.creeper.mc.creeperPit.config.manager.JSON;
|
||||||
|
import eu.creeper.mc.creeperPit.config.manager.YAML;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class Init {
|
||||||
|
private final Directory dir;
|
||||||
|
|
||||||
|
public final JSON effectShop;
|
||||||
|
public final JSON itemShop;
|
||||||
|
public final YAML worldguard;
|
||||||
|
|
||||||
|
public Init(JavaPlugin plugin) {
|
||||||
|
this.dir = new Directory(plugin);
|
||||||
|
dir.createFolder("shop");
|
||||||
|
|
||||||
|
File shopCfgs = dir.getSubFolder("shop");
|
||||||
|
|
||||||
|
File file = new File(shopCfgs, "effects.json");
|
||||||
|
this.effectShop = new JSON(plugin, file);
|
||||||
|
file = new File(shopCfgs, "items.json");
|
||||||
|
this.itemShop = new JSON(plugin, file);
|
||||||
|
file = new File(dir.getPluginFolder(), "worldguard.yml");
|
||||||
|
this.worldguard = new YAML(plugin, file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
package eu.creeper.mc.creeperPit.config.manager;
|
||||||
|
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class Directory {
|
||||||
|
private final JavaPlugin plugin;
|
||||||
|
|
||||||
|
public Directory(JavaPlugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getPluginFolder() {
|
||||||
|
File folder = plugin.getDataFolder();
|
||||||
|
if (!folder.exists()) {
|
||||||
|
folder.mkdirs();
|
||||||
|
}
|
||||||
|
return folder;
|
||||||
|
}
|
||||||
|
public File createFolder(String name) {
|
||||||
|
File folder = new File(plugin.getDataFolder(), name);
|
||||||
|
if (!folder.exists()) {
|
||||||
|
folder.mkdirs();
|
||||||
|
}
|
||||||
|
return folder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getSubFolder(String name) {
|
||||||
|
File sub = new File(getPluginFolder(), name);
|
||||||
|
if (!sub.exists()) {
|
||||||
|
sub.mkdirs();
|
||||||
|
}
|
||||||
|
return sub;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean exists(String name) {
|
||||||
|
return new File(getPluginFolder(), name).exists();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
package eu.creeper.mc.creeperPit.config.manager;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
|
||||||
|
public class JSON {
|
||||||
|
private final JavaPlugin plugin;
|
||||||
|
private final File file;
|
||||||
|
private final Gson gson;
|
||||||
|
|
||||||
|
public JSON(JavaPlugin plugin, File file) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.file = file;
|
||||||
|
this.gson = new GsonBuilder().setPrettyPrinting().create();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(Object data) {
|
||||||
|
try {
|
||||||
|
if (!plugin.getDataFolder().exists()) {
|
||||||
|
plugin.getDataFolder().mkdirs();
|
||||||
|
}
|
||||||
|
try (FileWriter writer = new FileWriter(file)) {
|
||||||
|
gson.toJson(data, writer);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
plugin.getLogger().severe("Erreur sauvegarde JSON : " + file.getName());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T load(Class<T> clazz, T defaultValue) {
|
||||||
|
try {
|
||||||
|
if (!file.exists()) {
|
||||||
|
save(defaultValue);
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
try (FileReader reader = new FileReader(file)) {
|
||||||
|
return gson.fromJson(reader, clazz);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
plugin.getLogger().severe("Erreur chargement JSON : " + file.getName());
|
||||||
|
e.printStackTrace();
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
package eu.creeper.mc.creeperPit.config.manager;
|
||||||
|
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class YAML {
|
||||||
|
|
||||||
|
private final JavaPlugin plugin;
|
||||||
|
private final File file;
|
||||||
|
private FileConfiguration config;
|
||||||
|
|
||||||
|
public YAML(JavaPlugin plugin, File file) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.file = file;
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void load() {
|
||||||
|
try {
|
||||||
|
if (!plugin.getDataFolder().exists()) {
|
||||||
|
plugin.getDataFolder().mkdirs();
|
||||||
|
}
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.createNewFile();
|
||||||
|
}
|
||||||
|
config = YamlConfiguration.loadConfiguration(file);
|
||||||
|
} catch (Exception e) {
|
||||||
|
plugin.getLogger().severe("Erreur chargement YAML : " + file.getName());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save() {
|
||||||
|
try {
|
||||||
|
config.save(file);
|
||||||
|
} catch (Exception e) {
|
||||||
|
plugin.getLogger().severe("Erreur sauvegarde YAML : " + file.getName());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FileConfiguration get() {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,10 +8,16 @@ import java.util.List;
|
||||||
|
|
||||||
public class WGManager implements Listener {
|
public class WGManager implements Listener {
|
||||||
private static final List<Region> regions = new ArrayList<>();
|
private static final List<Region> regions = new ArrayList<>();
|
||||||
|
public static List<Region> getRegions() {
|
||||||
|
return regions;
|
||||||
|
}
|
||||||
|
|
||||||
public static void registerRegion(Region region) {
|
public static void registerRegion(Region region) {
|
||||||
regions.add(region);
|
regions.add(region);
|
||||||
}
|
}
|
||||||
|
public static void unregisterRegion(Region region) {
|
||||||
|
regions.remove(region);
|
||||||
|
}
|
||||||
|
|
||||||
public static Region getRegionAt(Location loc) {
|
public static Region getRegionAt(Location loc) {
|
||||||
for (Region region : regions) {
|
for (Region region : regions) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user