Implemented saving

This commit is contained in:
Sn0wStorm
2013-04-30 21:41:16 +02:00
parent 2894985e71
commit c8a20f51bc
6 changed files with 303 additions and 34 deletions
+142 -17
View File
@@ -1,5 +1,8 @@
package com.dre.brewery;
import java.util.Map;
import java.util.HashMap;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -17,10 +20,9 @@ import org.bukkit.ChatColor;
import org.bukkit.Material;
import java.io.IOException;
import org.bukkit.inventory.ItemStack;
public class P extends JavaPlugin{
public static P p;
public static int lastBackup = 0;
//Listeners
public BlockListener blockListener;
@@ -33,6 +35,7 @@ public class P extends JavaPlugin{
p = this;
readConfig();
readData();
//Listeners
blockListener = new BlockListener();
@@ -57,26 +60,13 @@ public class P extends JavaPlugin{
//Stop shedulers
p.getServer().getScheduler().cancelTasks(this);
File datafile = new File(p.getDataFolder(), "data.yml");
FileConfiguration configFile = new YamlConfiguration();
//braucht eine gute db
ItemStack test = new ItemStack(2);//speichert später die custom potions (nicht als itemstack)
configFile.set("ItemStack.Stack", test);
try {
configFile.save(datafile);
} catch (IOException e) {
e.printStackTrace();
}
saveData();
this.log(this.getDescription().getName()+" disabled!");
}
public void msg(CommandSender sender,String msg){
public void msg(CommandSender sender,String msg){
sender.sendMessage(ChatColor.DARK_GREEN+"[Brewery] "+ChatColor.WHITE+msg);
}
@@ -84,6 +74,10 @@ public class P extends JavaPlugin{
this.msg(Bukkit.getConsoleSender(), msg);
}
public void errorLog(String msg){
Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN+"[Brewery] "+ChatColor.DARK_RED+"ERROR: "+ChatColor.RED+msg);
}
public void readConfig(){
@@ -112,6 +106,135 @@ public class P extends JavaPlugin{
}
//load all Data
public void readData(){
File file=new File(p.getDataFolder(), "data.yml");
if(file.exists()){
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
//loading Brew
ConfigurationSection section = data.getConfigurationSection("Brew");
if(section != null){
//All sections have the UID as name
for(String uid:section.getKeys(false)) {
new Brew(
parseInt(uid), loadIngredients(section.getConfigurationSection(uid+".ingredients")),
section.getInt(uid+".quality",0), section.getInt(uid+".distillRuns",0), (float)section.getDouble(uid+".ageTime",0.0), section.getInt(uid+".alcohol",0));
}
}
//loading BCauldron
section = data.getConfigurationSection("BCauldron");
if(section != null){
for(String cauldron:section.getKeys(false)) {
//block is splitted into worldname/x/y/z
String block = section.getString(cauldron+".block");
if(block != null){
String[] splitted = block.split("/");
if(splitted.length == 4){
new BCauldron(
getServer().getWorld(splitted[0]).getBlockAt(parseInt(splitted[1]),parseInt(splitted[2]),parseInt(splitted[3])),
loadIngredients(section.getConfigurationSection(cauldron+".ingredients")), section.getInt(cauldron+".state",1));
} else {
errorLog("Incomplete Block-Data in data.yml: "+section.getCurrentPath()+"."+cauldron);
}
} else {
errorLog("Missing Block-Data in data.yml: "+section.getCurrentPath()+"."+cauldron);
}
}
}
//loading Barrel
section = data.getConfigurationSection("Barrel");
if(section != null){
for(String barrel:section.getKeys(false)) {
//block spigot is splitted into worldname/x/y/z
String spigot = section.getString(barrel+".spigot");
if(spigot != null){
String[] splitted = spigot.split("/");
if(splitted.length == 4){
//load itemStacks from invSection
ConfigurationSection invSection = section.getConfigurationSection(barrel+".inv");
if(invSection != null){
//Map<String,ItemStack> inventory = section.getValues(barrel+"inv");
new Barrel(
getServer().getWorld(splitted[0]).getBlockAt(parseInt(splitted[1]),parseInt(splitted[2]),parseInt(splitted[3])),
invSection.getValues(true), (float)section.getDouble(barrel+".time",0.0));
} else {
//errorLog("Inventory of "+section.getCurrentPath()+"."+barrel+" in data.yml is missing");
//Barrel has no inventory
new Barrel(
getServer().getWorld(splitted[0]).getBlockAt(parseInt(splitted[1]),parseInt(splitted[2]),parseInt(splitted[3])),
(float)section.getDouble(barrel+".time",0.0));
}
} else {
errorLog("Incomplete Block-Data in data.yml: "+section.getCurrentPath()+"."+barrel);
}
} else {
errorLog("Missing Block-Data in data.yml: "+section.getCurrentPath()+"."+barrel);
}
}
}
} else {
errorLog("No data.yml found, will create new one!");
}
}
//loads BIngredients from ingredient section
public BIngredients loadIngredients(ConfigurationSection config){
if(config != null){
ConfigurationSection matSection = config.getConfigurationSection("mats");
if(matSection != null){
//matSection has all the materials + amount in Integer form
Map<Material,Integer> ingredients = new HashMap<Material,Integer>();
for(String ingredient:matSection.getKeys(false)){
//convert to Material
ingredients.put(Material.getMaterial(parseInt(ingredient)), matSection.getInt(ingredient));
}
return new BIngredients(ingredients, config.getInt("cookedTime",0));
}
}
errorLog("Ingredient section not found or incomplete in data.yml");
return new BIngredients();
}
//save all Data
public void saveData(){
File datafile = new File(p.getDataFolder(), "data.yml");
if(datafile.exists()){
if(lastBackup > 10){
datafile.renameTo(new File(p.getDataFolder(), "dataBackup.yml"));
} else {
lastBackup++;
}
}
FileConfiguration configFile = new YamlConfiguration();
if(!Brew.potions.isEmpty()){
Brew.save(configFile.createSection("Brew"));
}
if(!BCauldron.bcauldrons.isEmpty()){
BCauldron.save(configFile.createSection("BCauldron"));
}
if(!Barrel.barrels.isEmpty()){
Barrel.save(configFile.createSection("Barrel"));
}
//BPlayer is not yet saved, as it is WIP
try {
configFile.save(datafile);
} catch (IOException e) {
e.printStackTrace();
}
}
public int parseInt(String string){
return NumberUtils.toInt(string, 0);
}
@@ -130,6 +253,8 @@ public class P extends JavaPlugin{
cauldron.onUpdate();//runs every min to update cooking time
}
Barrel.onUpdate();//runs every min to check and update ageing time
saveData();//save all data
}
}