mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-12-01 06:23:22 +01:00
f1ee558e3a
- Switched from YamlConfiguration to BreezeConfiguration (from ChestShop 4) - Fixed getDouble() - Instead of checking for Admin Shops, we just pass in a new AdminShop Container - Created events for shop creation, protection checks and protection creation - Expanded string data value parsing, for example - you can use "Ocelot Monster" on the sign - Collected all external plugin wrappers in a single folder - Instead of using statics, now we use objects - Fixed enchantments for armour - Made config more readable - Added a setting for removing empty shops - Switched from System.out to logger - Also, switched from ugly file logging to Java's native one (FileHandler) - Added an option to tax transactions even when SERVER_ECONOMY_ACCOUNT is empty - Changed the Container interface
155 lines
3.6 KiB
Java
155 lines
3.6 KiB
Java
package com.Acrobot.ChestShop.Config;
|
|
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* @author Acrobot
|
|
*/
|
|
public class BreezeConfiguration extends YamlConfiguration {
|
|
protected final File file;
|
|
protected final Map<String, Value> defaultValues = new LinkedHashMap<String, Value>();
|
|
|
|
protected BreezeConfiguration(File file) {
|
|
this.file = file;
|
|
}
|
|
|
|
/**
|
|
* Adds default values for the config
|
|
*
|
|
* @param map The default values to add
|
|
*/
|
|
public void addDefaultValues(Map<String, ? extends Value> map) {
|
|
defaultValues.putAll(map);
|
|
}
|
|
|
|
/**
|
|
* Creates a new BreezeConfiguration object
|
|
*
|
|
* @param file file to load config from
|
|
* @return BreezeConfiguration object
|
|
*/
|
|
public static BreezeConfiguration loadConfiguration(File file) {
|
|
if (file == null) {
|
|
throw new IllegalArgumentException("File cannot be null");
|
|
}
|
|
|
|
BreezeConfiguration config = new BreezeConfiguration(file);
|
|
|
|
config.load();
|
|
|
|
return config;
|
|
}
|
|
|
|
/**
|
|
* Creates a new BreezeConfiguration object
|
|
*
|
|
* @param file file to load config from
|
|
* @param defaults default values
|
|
* @return BreezeConfiguration object
|
|
*/
|
|
public static BreezeConfiguration loadConfiguration(File file, Map<String, Value> defaults) {
|
|
if (file == null) {
|
|
throw new IllegalArgumentException("File cannot be null");
|
|
}
|
|
|
|
BreezeConfiguration config = new BreezeConfiguration(file);
|
|
|
|
config.addDefaultValues(defaults);
|
|
|
|
config.load();
|
|
|
|
return config;
|
|
}
|
|
|
|
/**
|
|
* Loads the config
|
|
*/
|
|
public void load() {
|
|
try {
|
|
if (!file.getParentFile().exists()) {
|
|
file.getParentFile().mkdir();
|
|
}
|
|
if (!file.exists()) {
|
|
file.createNewFile();
|
|
}
|
|
|
|
super.load(file);
|
|
} catch (InvalidConfigurationException e) {
|
|
e.printStackTrace();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
if (createDefaultValues()) {
|
|
load();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reloads (saves and loads) the config
|
|
*/
|
|
public void reload() {
|
|
save();
|
|
load();
|
|
}
|
|
|
|
/**
|
|
* Creates default values
|
|
*
|
|
* @return were any values added?
|
|
*/
|
|
private boolean createDefaultValues() {
|
|
boolean changed = false;
|
|
|
|
if (!file.exists()) {
|
|
try {
|
|
file.createNewFile();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
try {
|
|
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
|
|
|
|
for (Map.Entry<String, Value> entry : defaultValues.entrySet()) {
|
|
if (this.contains(entry.getKey())) {
|
|
continue;
|
|
}
|
|
|
|
changed = true;
|
|
bw.write('\n' + entry.getKey() + ": " + entry.getValue().retrieveValue());
|
|
}
|
|
|
|
bw.close();
|
|
|
|
if (changed) {
|
|
load();
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return changed;
|
|
}
|
|
|
|
/**
|
|
* Saves the config
|
|
*/
|
|
public void save() {
|
|
try {
|
|
super.save(file);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|