mirror of
https://github.com/DieReicheErethons/Brewery.git
synced 2024-09-28 03:57:28 +02:00
Optimized Storage of BIngredients
This commit is contained in:
parent
edfeab1fc8
commit
bfe859d3bc
@ -15,14 +15,13 @@ import com.dre.brewery.BIngredients;
|
||||
public class BCauldron {
|
||||
public static CopyOnWriteArrayList<BCauldron> bcauldrons = new CopyOnWriteArrayList<BCauldron>();
|
||||
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
@ -17,19 +17,25 @@ public class BIngredients {
|
||||
public static ArrayList<Material> possibleIngredients = new ArrayList<Material>();
|
||||
public static ArrayList<BRecipe> recipes = new ArrayList<BRecipe>();
|
||||
public static Map<Material, String> cookedNames = new HashMap<Material, String>();
|
||||
private static int lastId = 0;
|
||||
|
||||
private int id;
|
||||
private Map<Material, Integer> ingredients = new HashMap<Material, Integer>();
|
||||
private int cookedTime;
|
||||
|
||||
// Represents ingredients in Cauldron, Brew
|
||||
// Init a new BIngredients
|
||||
public BIngredients() {
|
||||
this.id = lastId;
|
||||
lastId++;
|
||||
}
|
||||
|
||||
// Load from File
|
||||
public BIngredients(Map<Material, Integer> 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
|
||||
public Map<Integer, Integer> serializeIngredients() {
|
||||
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));
|
||||
}
|
||||
return mats;
|
||||
}
|
||||
|
||||
}
|
@ -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<String, BPlayer> 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<Map.Entry<String, BPlayer>> iter = players.entrySet().iterator(); iter.hasNext();) {
|
||||
Iterator<Map.Entry<String, BPlayer>> iter = players.entrySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
Map.Entry<String, BPlayer> entry = iter.next();
|
||||
String name = entry.getKey();
|
||||
BPlayer bplayer = entry.getValue();
|
||||
|
@ -468,9 +468,10 @@ public class Brew {
|
||||
|
||||
// Saves all data
|
||||
public static void save(ConfigurationSection config) {
|
||||
for (int uid : potions.keySet()) {
|
||||
for (Map.Entry<Integer, Brew> 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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -191,12 +191,32 @@ public class P extends JavaPlugin {
|
||||
|
||||
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
// loading Ingredients into ingMap
|
||||
Map<String, BIngredients> ingMap = new HashMap<String, BIngredients>();
|
||||
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<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));
|
||||
}
|
||||
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
|
||||
// returns Ingredients by id from the specified ingMap
|
||||
public BIngredients getIngredients(Map<String, BIngredients> ingMap, String id) {
|
||||
if (!ingMap.isEmpty()) {
|
||||
if (ingMap.containsKey(id)) {
|
||||
return ingMap.get(id);
|
||||
}
|
||||
}
|
||||
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<Material, Integer> ingredients = new HashMap<Material, Integer>();
|
||||
for (String ingredient : matSection.getKeys(false)) {
|
||||
for (String ingredient : section.getKeys(false)) {
|
||||
// convert to Material
|
||||
ingredients.put(Material.getMaterial(parseInt(ingredient)), matSection.getInt(ingredient));
|
||||
ingredients.put(Material.getMaterial(parseInt(ingredient)), section.getInt(ingredient));
|
||||
}
|
||||
return new BIngredients(ingredients, config.getInt("cookedTime", 0));
|
||||
return new BIngredients(ingredients, 0);
|
||||
} else {
|
||||
errorLog("Cauldron is missing Ingredient Section");
|
||||
}
|
||||
}
|
||||
errorLog("Ingredient section not found or incomplete in data.yml");
|
||||
return new BIngredients();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user