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

View File

@ -1,5 +1,5 @@
name: Brewery name: Brewery
version: 0.1 version: 0.2
main: com.dre.brewery.P main: com.dre.brewery.P
authors: [Frank Baumann] authors: [Frank Baumann]
softdepend: [Vault] softdepend: [Vault]

View File

@ -8,6 +8,7 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.Effect; import org.bukkit.Effect;
import org.bukkit.configuration.ConfigurationSection;
import com.dre.brewery.BIngredients; import com.dre.brewery.BIngredients;
@ -26,6 +27,14 @@ public class BCauldron {
bcauldrons.add(this); bcauldrons.add(this);
} }
//loading from file
public BCauldron(Block block,BIngredients ingredients,int state){
this.block = block;
this.state = state;
this.ingredients = ingredients;
bcauldrons.add(this);
}
public void onUpdate(){ public void onUpdate(){
//Check if fire still alive //Check if fire still alive
@ -98,6 +107,20 @@ public class BCauldron {
} }
} }
public static void save(ConfigurationSection config){
int id = 0;
for(BCauldron cauldron:bcauldrons){
//cauldrons are randomly listed
ConfigurationSection section = config.createSection(""+id);
section.set("block",cauldron.block.getWorld().getName()+"/"+cauldron.block.getX()+"/"+cauldron.block.getY()+"/"+cauldron.block.getZ());
if(cauldron.state != 1){
section.set("state",cauldron.state);
}
cauldron.ingredients.save(section.createSection("ingredients"));
id++;
}
}
//bukkit bug not updating the inventory while executing event, have to schedule the give //bukkit bug not updating the inventory while executing event, have to schedule the give
public static void giveItem(final Player player,final ItemStack item){ public static void giveItem(final Player player,final ItemStack item){
P.p.getServer().getScheduler().runTaskLater(P.p, new Runnable() { P.p.getServer().getScheduler().runTaskLater(P.p, new Runnable() {

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
@ -258,6 +259,24 @@ public class BIngredients {
return 0; return 0;
} }
//saves data into ingredient section of Brew/BCauldron
public void save(ConfigurationSection config){
if(cookedTime != 0){
config.set("cookedTime", cookedTime);
}
//convert the ingredient Material to id
Map<Integer,Integer> mats = new HashMap<Integer,Integer>();
for(Material mat:ingredients.keySet()){
mats.put(mat.getId(),ingredients.get(mat));
}
//list all Material ids with their amount
ConfigurationSection matSection = config.createSection("mats");
for(int id:mats.keySet()){
matSection.set(""+id,mats.get(id));
}
}

View File

@ -1,11 +1,13 @@
package com.dre.brewery; package com.dre.brewery;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.Map;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.configuration.ConfigurationSection;
public class Barrel { public class Barrel {
@ -20,6 +22,35 @@ private float time;
this.spigot = spigot; this.spigot = spigot;
} }
//load from file
public Barrel(Block spigot,Map<String,Object> items,float time){
this.spigot = spigot;
if(isLarge()){
this.inventory = org.bukkit.Bukkit.createInventory(null, 27, "Fass");
} else {
this.inventory = org.bukkit.Bukkit.createInventory(null, 9, "Fass");
}
for(String slot:items.keySet()){
if(items.get(slot) instanceof ItemStack){
this.inventory.setItem(P.p.parseInt(slot), (ItemStack)items.get(slot));
}
}
this.time = time;
barrels.add(this);
}
//load from file (without inventory)
public Barrel(Block spigot,float time){
this.spigot = spigot;
if(isLarge()){
this.inventory = org.bukkit.Bukkit.createInventory(null, 27, "Fass");
} else {
this.inventory = org.bukkit.Bukkit.createInventory(null, 9, "Fass");
}
this.time = time;
barrels.add(this);
}
public static void onUpdate(){ public static void onUpdate(){
Block broken; Block broken;
for(Barrel barrel:barrels){ for(Barrel barrel:barrels){
@ -109,6 +140,43 @@ private float time;
barrels.remove(this); barrels.remove(this);
} }
//Saves all data
public static void save(ConfigurationSection config){
int id = 0;
for(Barrel barrel:barrels){
//barrels are listed randomly
ConfigurationSection idConfig = config.createSection(""+id);
//block: worldname/x/y/z
idConfig.set("spigot",barrel.spigot.getWorld().getName()+"/"+barrel.spigot.getX()+"/"+barrel.spigot.getY()+"/"+barrel.spigot.getZ());
if(barrel.time != 0){
idConfig.set("time", barrel.time);
}
//not saving the inventory if there is none, or it is empty
if(barrel.inventory != null){
int slot = 0;
ItemStack item = null;
ConfigurationSection invConfig = null;
while(slot < barrel.inventory.getSize()){
item = barrel.inventory.getItem(slot);
if(item != null){
if(invConfig == null){
//create section only when items in inventory
invConfig = idConfig.createSection("inv");
}
//ItemStacks are configurationSerializeable, makes them really easy to save
invConfig.set(slot+"",item);
}
slot++;
}
}
id++;
}
}
//direction of the barrel from the spigot //direction of the barrel from the spigot
public static int getDirection(Block spigot){ public static int getDirection(Block spigot){
int direction = 0;//1=x+ 2=x- 3=z+ 4=z- int direction = 0;//1=x+ 2=x- 3=z+ 4=z-
@ -192,6 +260,21 @@ private float time;
return 0; return 0;
} }
//returns the fence above/below a block, itself if there is none
public static Block getSpigotOfSign(Block block){
int y = -2;
while(y <= 1){
//Fence and Netherfence
if(block.getRelative(0,y,0).getTypeId() == 85 ||
block.getRelative(0,y,0).getTypeId() == 113){
return (block.getRelative(0,y,0));
}
y++;
}
return block;
}
//returns null if Barrel is correctly placed; the block that is missing when not //returns null if Barrel is correctly placed; the block that is missing when not
//the barrel needs to be formed correctly //the barrel needs to be formed correctly
public static Block getBrokenBlock(Block spigot){ public static Block getBrokenBlock(Block spigot){
@ -354,19 +437,4 @@ private float time;
} }
//returns the fence above/below a block, itself if there is none
public static Block getSpigotOfSign(Block block){
int y = -2;
while(y <= 1){
//Fence and Netherfence
if(block.getRelative(0,y,0).getTypeId() == 85 ||
block.getRelative(0,y,0).getTypeId() == 113){
return (block.getRelative(0,y,0));
}
y++;
}
return block;
}
} }

View File

@ -4,6 +4,7 @@ import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.util.ArrayList; import java.util.ArrayList;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
@ -37,6 +38,16 @@ public class Brew {
potions.put(uid,this); potions.put(uid,this);
} }
//loading from file
public Brew(int uid,BIngredients ingredients,int quality,int distillRuns,float ageTime,int alcohol){
this.ingredients = ingredients;
this.quality = quality;
this.distillRuns = distillRuns;
this.ageTime = ageTime;
this.alcohol = alcohol;
potions.put(uid,this);
}
//remove potion from map (drinking, despawning, should be more!) //remove potion from map (drinking, despawning, should be more!)
public static void remove(ItemStack item){ public static void remove(ItemStack item){
PotionMeta meta = (PotionMeta) item.getItemMeta(); PotionMeta meta = (PotionMeta) item.getItemMeta();
@ -60,7 +71,7 @@ public class Brew {
public static Brew getByUID(int uid){ public static Brew getByUID(int uid){
if(uid < -1){ if(uid < -1){
if(!potions.containsKey(uid)){ if(!potions.containsKey(uid)){
P.p.log("Database failure! unable to find UID "+uid+" of a custom Potion in the db!"); P.p.errorLog("Database failure! unable to find UID "+uid+" of a custom Potion!");
return null;//throw some exception? return null;//throw some exception?
} }
} else { } else {
@ -206,6 +217,29 @@ public class Brew {
} }
} }
//Saves all data
public static void save(ConfigurationSection config){
for(int uid:potions.keySet()){
ConfigurationSection idConfig = config.createSection(""+uid);
Brew brew = potions.get(uid);
//not saving unneccessary data
if(brew.quality != 0){
idConfig.set("quality", brew.quality);
}
if(brew.distillRuns != 0){
idConfig.set("distillRuns", brew.distillRuns);
}
if(brew.ageTime != 0){
idConfig.set("ageTime", brew.ageTime);
}
if(brew.alcohol != 0){
idConfig.set("alcohol", brew.alcohol);
}
//save the ingredients
brew.ingredients.save(idConfig.createSection("ingredients"));
}
}

View File

@ -1,5 +1,8 @@
package com.dre.brewery; package com.dre.brewery;
import java.util.Map;
import java.util.HashMap;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -17,10 +20,9 @@ import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import java.io.IOException; import java.io.IOException;
import org.bukkit.inventory.ItemStack;
public class P extends JavaPlugin{ public class P extends JavaPlugin{
public static P p; public static P p;
public static int lastBackup = 0;
//Listeners //Listeners
public BlockListener blockListener; public BlockListener blockListener;
@ -33,6 +35,7 @@ public class P extends JavaPlugin{
p = this; p = this;
readConfig(); readConfig();
readData();
//Listeners //Listeners
blockListener = new BlockListener(); blockListener = new BlockListener();
@ -58,20 +61,7 @@ public class P extends JavaPlugin{
//Stop shedulers //Stop shedulers
p.getServer().getScheduler().cancelTasks(this); p.getServer().getScheduler().cancelTasks(this);
saveData();
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();
}
this.log(this.getDescription().getName()+" disabled!"); this.log(this.getDescription().getName()+" disabled!");
} }
@ -84,6 +74,10 @@ public class P extends JavaPlugin{
this.msg(Bukkit.getConsoleSender(), msg); 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(){ 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){ public int parseInt(String string){
return NumberUtils.toInt(string, 0); return NumberUtils.toInt(string, 0);
} }
@ -130,6 +253,8 @@ public class P extends JavaPlugin{
cauldron.onUpdate();//runs every min to update cooking time cauldron.onUpdate();//runs every min to update cooking time
} }
Barrel.onUpdate();//runs every min to check and update ageing time Barrel.onUpdate();//runs every min to check and update ageing time
saveData();//save all data
} }
} }