From bfe859d3bc87845b24be88bc836ebce4cfe4e0c5 Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Wed, 4 Sep 2013 23:32:09 +0200 Subject: [PATCH] Optimized Storage of BIngredients --- src/com/dre/brewery/BCauldron.java | 19 ++++++--- src/com/dre/brewery/BIngredients.java | 29 ++++++++----- src/com/dre/brewery/BPlayer.java | 11 +++-- src/com/dre/brewery/Brew.java | 7 ++-- src/com/dre/brewery/P.java | 59 ++++++++++++++++++++------- 5 files changed, 88 insertions(+), 37 deletions(-) diff --git a/src/com/dre/brewery/BCauldron.java b/src/com/dre/brewery/BCauldron.java index 03c3b4c..35f1a85 100644 --- a/src/com/dre/brewery/BCauldron.java +++ b/src/com/dre/brewery/BCauldron.java @@ -15,14 +15,13 @@ import com.dre.brewery.BIngredients; public class BCauldron { public static CopyOnWriteArrayList bcauldrons = new CopyOnWriteArrayList(); - private BIngredients ingredients; + private BIngredients ingredients = new BIngredients(); private Block block; - private int state; + private int state = 1; + private boolean someRemoved = false; public BCauldron(Block block, Material ingredient) { this.block = block; - this.state = 1; - this.ingredients = new BIngredients(); add(ingredient); bcauldrons.add(this); } @@ -41,11 +40,19 @@ public class BCauldron { || block.getRelative(BlockFace.DOWN).getType() == Material.LAVA) { // add a minute to cooking time state++; + if (someRemoved) { + ingredients = ingredients.clone(); + someRemoved = false; + } } } // add an ingredient to the cauldron public void add(Material ingredient) { + if (someRemoved) { + ingredients = ingredients.clone(); + someRemoved = false; + } ingredients.add(ingredient); block.getWorld().playEffect(block.getLocation(), Effect.EXTINGUISH, 0); if (state > 1) { @@ -98,6 +105,8 @@ public class BCauldron { if (block.getData() == 0) { bcauldrons.remove(bcauldron); + } else { + bcauldron.someRemoved = true; } return true; } @@ -154,7 +163,7 @@ public class BCauldron { if (cauldron.state != 1) { config.set(prefix + ".state", cauldron.state); } - cauldron.ingredients.save(config.createSection(prefix + ".ingredients")); + config.set(prefix + ".ingredients", cauldron.ingredients.serializeIngredients()); id++; } } diff --git a/src/com/dre/brewery/BIngredients.java b/src/com/dre/brewery/BIngredients.java index c551b78..dbe96d9 100644 --- a/src/com/dre/brewery/BIngredients.java +++ b/src/com/dre/brewery/BIngredients.java @@ -17,19 +17,25 @@ public class BIngredients { public static ArrayList possibleIngredients = new ArrayList(); public static ArrayList recipes = new ArrayList(); public static Map cookedNames = new HashMap(); + private static int lastId = 0; + private int id; private Map ingredients = new HashMap(); private int cookedTime; // Represents ingredients in Cauldron, Brew // Init a new BIngredients public BIngredients() { + this.id = lastId; + lastId++; } // Load from File public BIngredients(Map ingredients, int cookedTime) { this.ingredients = ingredients; this.cookedTime = cookedTime; + this.id = lastId; + lastId++; } //returns the recipe with the given name @@ -69,7 +75,7 @@ public class BIngredients { // Potion is best with cooking only int quality = (int) Math.round((getIngredientQuality(cookRecipe) + getCookingQuality(cookRecipe, false)) / 2.0); P.p.log("cooked potion has Quality: " + quality); - Brew brew = new Brew(uid, quality, cookRecipe, clone()); + Brew brew = new Brew(uid, quality, cookRecipe, this); Brew.addOrReplaceEffects(potionMeta, brew.getEffects()); cookedName = cookRecipe.getName(quality); @@ -77,7 +83,7 @@ public class BIngredients { } else { // new base potion - new Brew(uid, clone()); + new Brew(uid, this); if (state <= 1) { cookedName = "Schlammiger Sud"; @@ -298,22 +304,23 @@ public class BIngredients { return copy; } - // saves data into ingredient section of Brew/BCauldron - public void save(ConfigurationSection config) { + // saves data into main Ingredient section. Returns the save id + public int save(ConfigurationSection config) { + String path = "Ingredients." + id; if (cookedTime != 0) { - config.set("cookedTime", cookedTime); + config.set(path + ".cookedTime", cookedTime); } + config.set(path + ".mats", serializeIngredients()); + return id; + } - // convert the ingredient Material to id + // convert the ingredient Material to id + public Map serializeIngredients() { Map mats = new HashMap(); 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)); - } + return mats; } } \ No newline at end of file diff --git a/src/com/dre/brewery/BPlayer.java b/src/com/dre/brewery/BPlayer.java index 9e4fbf4..d2c7994 100644 --- a/src/com/dre/brewery/BPlayer.java +++ b/src/com/dre/brewery/BPlayer.java @@ -156,6 +156,9 @@ public class BPlayer { } } } else { + if (offlineDrunk == 0) { + return true; + } quality = getQuality(); if (drunkeness <= -offlineDrunk) { if (drunkeness <= -hangoverTime) { @@ -438,8 +441,9 @@ public class BPlayer { // #### Sheduled #### public static void drunkeness() { - for (String name : players.keySet()) { - BPlayer bplayer = players.get(name); + for (Map.Entry entry : players.entrySet()) { + String name = entry.getKey(); + BPlayer bplayer = entry.getValue(); if (bplayer.drunkeness > 30) { if (bplayer.offlineDrunk == 0) { @@ -462,7 +466,8 @@ public class BPlayer { public static void onUpdate() { if (!players.isEmpty()) { int soberPerMin = 2; - for (Iterator> iter = players.entrySet().iterator(); iter.hasNext();) { + Iterator> iter = players.entrySet().iterator(); + while (iter.hasNext()) { Map.Entry entry = iter.next(); String name = entry.getKey(); BPlayer bplayer = entry.getValue(); diff --git a/src/com/dre/brewery/Brew.java b/src/com/dre/brewery/Brew.java index 171da5f..9208437 100644 --- a/src/com/dre/brewery/Brew.java +++ b/src/com/dre/brewery/Brew.java @@ -468,9 +468,10 @@ public class Brew { // Saves all data public static void save(ConfigurationSection config) { - for (int uid : potions.keySet()) { + for (Map.Entry entry : potions.entrySet()) { + int uid = entry.getKey(); + Brew brew = entry.getValue(); ConfigurationSection idConfig = config.createSection("" + uid); - Brew brew = potions.get(uid); // not saving unneccessary data if (brew.quality != 0) { idConfig.set("quality", brew.quality); @@ -488,7 +489,7 @@ public class Brew { idConfig.set("unlabeled", true); } // save the ingredients - brew.ingredients.save(idConfig.createSection("ingredients")); + idConfig.set("ingId", brew.ingredients.save(config.getParent())); } } diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index a751763..052d681 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -191,12 +191,32 @@ public class P extends JavaPlugin { FileConfiguration data = YamlConfiguration.loadConfiguration(file); + // loading Ingredients into ingMap + Map ingMap = new HashMap(); + ConfigurationSection section = data.getConfigurationSection("Ingredients"); + if (section != null) { + for (String id : section.getKeys(false)) { + ConfigurationSection matSection = section.getConfigurationSection(id + ".mats"); + if (matSection != null) { + // matSection has all the materials + amount as Integers + Map ingredients = new HashMap(); + for (String ingredient : matSection.getKeys(false)) { + // convert to Material + ingredients.put(Material.getMaterial(parseInt(ingredient)), matSection.getInt(ingredient)); + } + ingMap.put(id, new BIngredients(ingredients, section.getInt(id + ".cookedTime", 0))); + } else { + errorLog("Ingredient id: '" + id + "' incomplete in data.yml"); + } + } + } + // loading Brew - ConfigurationSection section = data.getConfigurationSection("Brew"); + section = data.getConfigurationSection("Brew"); if (section != null) { // All sections have the UID as name for (String uid : section.getKeys(false)) { - BIngredients ingredients = loadIngredients(section.getConfigurationSection(uid + ".ingredients")); + BIngredients ingredients = getIngredients(ingMap, section.getString(uid + ".ingId")); int quality = section.getInt(uid + ".quality", 0); int distillRuns = section.getInt(uid + ".distillRuns", 0); float ageTime = (float) section.getDouble(uid + ".ageTime", 0.0); @@ -234,21 +254,30 @@ public class P extends JavaPlugin { } } - // 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 ingredients = new HashMap(); - 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)); + // returns Ingredients by id from the specified ingMap + public BIngredients getIngredients(Map ingMap, String id) { + if (!ingMap.isEmpty()) { + if (ingMap.containsKey(id)) { + return ingMap.get(id); } } - errorLog("Ingredient section not found or incomplete in data.yml"); + errorLog("Ingredient id: '" + id + "' not found in data.yml"); + return new BIngredients(); + } + + // loads BIngredients from an ingredient section + public BIngredients loadIngredients(ConfigurationSection section) { + if (section != null) { + // has all the materials + amount as Integers + Map ingredients = new HashMap(); + for (String ingredient : section.getKeys(false)) { + // convert to Material + ingredients.put(Material.getMaterial(parseInt(ingredient)), section.getInt(ingredient)); + } + return new BIngredients(ingredients, 0); + } else { + errorLog("Cauldron is missing Ingredient Section"); + } return new BIngredients(); }