mirror of
https://github.com/DieReicheErethons/Brewery.git
synced 2024-09-29 04:07:30 +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 class BCauldron {
|
||||||
public static CopyOnWriteArrayList<BCauldron> bcauldrons = new CopyOnWriteArrayList<BCauldron>();
|
public static CopyOnWriteArrayList<BCauldron> bcauldrons = new CopyOnWriteArrayList<BCauldron>();
|
||||||
|
|
||||||
private BIngredients ingredients;
|
private BIngredients ingredients = new BIngredients();
|
||||||
private Block block;
|
private Block block;
|
||||||
private int state;
|
private int state = 1;
|
||||||
|
private boolean someRemoved = false;
|
||||||
|
|
||||||
public BCauldron(Block block, Material ingredient) {
|
public BCauldron(Block block, Material ingredient) {
|
||||||
this.block = block;
|
this.block = block;
|
||||||
this.state = 1;
|
|
||||||
this.ingredients = new BIngredients();
|
|
||||||
add(ingredient);
|
add(ingredient);
|
||||||
bcauldrons.add(this);
|
bcauldrons.add(this);
|
||||||
}
|
}
|
||||||
@ -41,11 +40,19 @@ public class BCauldron {
|
|||||||
|| block.getRelative(BlockFace.DOWN).getType() == Material.LAVA) {
|
|| block.getRelative(BlockFace.DOWN).getType() == Material.LAVA) {
|
||||||
// add a minute to cooking time
|
// add a minute to cooking time
|
||||||
state++;
|
state++;
|
||||||
|
if (someRemoved) {
|
||||||
|
ingredients = ingredients.clone();
|
||||||
|
someRemoved = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add an ingredient to the cauldron
|
// add an ingredient to the cauldron
|
||||||
public void add(Material ingredient) {
|
public void add(Material ingredient) {
|
||||||
|
if (someRemoved) {
|
||||||
|
ingredients = ingredients.clone();
|
||||||
|
someRemoved = false;
|
||||||
|
}
|
||||||
ingredients.add(ingredient);
|
ingredients.add(ingredient);
|
||||||
block.getWorld().playEffect(block.getLocation(), Effect.EXTINGUISH, 0);
|
block.getWorld().playEffect(block.getLocation(), Effect.EXTINGUISH, 0);
|
||||||
if (state > 1) {
|
if (state > 1) {
|
||||||
@ -98,6 +105,8 @@ public class BCauldron {
|
|||||||
|
|
||||||
if (block.getData() == 0) {
|
if (block.getData() == 0) {
|
||||||
bcauldrons.remove(bcauldron);
|
bcauldrons.remove(bcauldron);
|
||||||
|
} else {
|
||||||
|
bcauldron.someRemoved = true;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -154,7 +163,7 @@ public class BCauldron {
|
|||||||
if (cauldron.state != 1) {
|
if (cauldron.state != 1) {
|
||||||
config.set(prefix + ".state", cauldron.state);
|
config.set(prefix + ".state", cauldron.state);
|
||||||
}
|
}
|
||||||
cauldron.ingredients.save(config.createSection(prefix + ".ingredients"));
|
config.set(prefix + ".ingredients", cauldron.ingredients.serializeIngredients());
|
||||||
id++;
|
id++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,19 +17,25 @@ public class BIngredients {
|
|||||||
public static ArrayList<Material> possibleIngredients = new ArrayList<Material>();
|
public static ArrayList<Material> possibleIngredients = new ArrayList<Material>();
|
||||||
public static ArrayList<BRecipe> recipes = new ArrayList<BRecipe>();
|
public static ArrayList<BRecipe> recipes = new ArrayList<BRecipe>();
|
||||||
public static Map<Material, String> cookedNames = new HashMap<Material, String>();
|
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 Map<Material, Integer> ingredients = new HashMap<Material, Integer>();
|
||||||
private int cookedTime;
|
private int cookedTime;
|
||||||
|
|
||||||
// Represents ingredients in Cauldron, Brew
|
// Represents ingredients in Cauldron, Brew
|
||||||
// Init a new BIngredients
|
// Init a new BIngredients
|
||||||
public BIngredients() {
|
public BIngredients() {
|
||||||
|
this.id = lastId;
|
||||||
|
lastId++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load from File
|
// Load from File
|
||||||
public BIngredients(Map<Material, Integer> ingredients, int cookedTime) {
|
public BIngredients(Map<Material, Integer> ingredients, int cookedTime) {
|
||||||
this.ingredients = ingredients;
|
this.ingredients = ingredients;
|
||||||
this.cookedTime = cookedTime;
|
this.cookedTime = cookedTime;
|
||||||
|
this.id = lastId;
|
||||||
|
lastId++;
|
||||||
}
|
}
|
||||||
|
|
||||||
//returns the recipe with the given name
|
//returns the recipe with the given name
|
||||||
@ -69,7 +75,7 @@ public class BIngredients {
|
|||||||
// Potion is best with cooking only
|
// Potion is best with cooking only
|
||||||
int quality = (int) Math.round((getIngredientQuality(cookRecipe) + getCookingQuality(cookRecipe, false)) / 2.0);
|
int quality = (int) Math.round((getIngredientQuality(cookRecipe) + getCookingQuality(cookRecipe, false)) / 2.0);
|
||||||
P.p.log("cooked potion has Quality: " + quality);
|
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());
|
Brew.addOrReplaceEffects(potionMeta, brew.getEffects());
|
||||||
|
|
||||||
cookedName = cookRecipe.getName(quality);
|
cookedName = cookRecipe.getName(quality);
|
||||||
@ -77,7 +83,7 @@ public class BIngredients {
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
// new base potion
|
// new base potion
|
||||||
new Brew(uid, clone());
|
new Brew(uid, this);
|
||||||
|
|
||||||
if (state <= 1) {
|
if (state <= 1) {
|
||||||
cookedName = "Schlammiger Sud";
|
cookedName = "Schlammiger Sud";
|
||||||
@ -298,22 +304,23 @@ public class BIngredients {
|
|||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
// saves data into ingredient section of Brew/BCauldron
|
// saves data into main Ingredient section. Returns the save id
|
||||||
public void save(ConfigurationSection config) {
|
public int save(ConfigurationSection config) {
|
||||||
|
String path = "Ingredients." + id;
|
||||||
if (cookedTime != 0) {
|
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<Integer, Integer> serializeIngredients() {
|
||||||
Map<Integer, Integer> mats = new HashMap<Integer, Integer>();
|
Map<Integer, Integer> mats = new HashMap<Integer, Integer>();
|
||||||
for (Material mat : ingredients.keySet()) {
|
for (Material mat : ingredients.keySet()) {
|
||||||
mats.put(mat.getId(), ingredients.get(mat));
|
mats.put(mat.getId(), ingredients.get(mat));
|
||||||
}
|
}
|
||||||
// list all Material ids with their amount
|
return mats;
|
||||||
ConfigurationSection matSection = config.createSection("mats");
|
|
||||||
for (int id : mats.keySet()) {
|
|
||||||
matSection.set("" + id, mats.get(id));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -156,6 +156,9 @@ public class BPlayer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (offlineDrunk == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
quality = getQuality();
|
quality = getQuality();
|
||||||
if (drunkeness <= -offlineDrunk) {
|
if (drunkeness <= -offlineDrunk) {
|
||||||
if (drunkeness <= -hangoverTime) {
|
if (drunkeness <= -hangoverTime) {
|
||||||
@ -438,8 +441,9 @@ public class BPlayer {
|
|||||||
// #### Sheduled ####
|
// #### Sheduled ####
|
||||||
|
|
||||||
public static void drunkeness() {
|
public static void drunkeness() {
|
||||||
for (String name : players.keySet()) {
|
for (Map.Entry<String, BPlayer> entry : players.entrySet()) {
|
||||||
BPlayer bplayer = players.get(name);
|
String name = entry.getKey();
|
||||||
|
BPlayer bplayer = entry.getValue();
|
||||||
|
|
||||||
if (bplayer.drunkeness > 30) {
|
if (bplayer.drunkeness > 30) {
|
||||||
if (bplayer.offlineDrunk == 0) {
|
if (bplayer.offlineDrunk == 0) {
|
||||||
@ -462,7 +466,8 @@ public class BPlayer {
|
|||||||
public static void onUpdate() {
|
public static void onUpdate() {
|
||||||
if (!players.isEmpty()) {
|
if (!players.isEmpty()) {
|
||||||
int soberPerMin = 2;
|
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();
|
Map.Entry<String, BPlayer> entry = iter.next();
|
||||||
String name = entry.getKey();
|
String name = entry.getKey();
|
||||||
BPlayer bplayer = entry.getValue();
|
BPlayer bplayer = entry.getValue();
|
||||||
|
@ -468,9 +468,10 @@ public class Brew {
|
|||||||
|
|
||||||
// Saves all data
|
// Saves all data
|
||||||
public static void save(ConfigurationSection config) {
|
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);
|
ConfigurationSection idConfig = config.createSection("" + uid);
|
||||||
Brew brew = potions.get(uid);
|
|
||||||
// not saving unneccessary data
|
// not saving unneccessary data
|
||||||
if (brew.quality != 0) {
|
if (brew.quality != 0) {
|
||||||
idConfig.set("quality", brew.quality);
|
idConfig.set("quality", brew.quality);
|
||||||
@ -488,7 +489,7 @@ public class Brew {
|
|||||||
idConfig.set("unlabeled", true);
|
idConfig.set("unlabeled", true);
|
||||||
}
|
}
|
||||||
// save the ingredients
|
// 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);
|
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
|
// loading Brew
|
||||||
ConfigurationSection section = data.getConfigurationSection("Brew");
|
section = data.getConfigurationSection("Brew");
|
||||||
if (section != null) {
|
if (section != null) {
|
||||||
// All sections have the UID as name
|
// All sections have the UID as name
|
||||||
for (String uid : section.getKeys(false)) {
|
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 quality = section.getInt(uid + ".quality", 0);
|
||||||
int distillRuns = section.getInt(uid + ".distillRuns", 0);
|
int distillRuns = section.getInt(uid + ".distillRuns", 0);
|
||||||
float ageTime = (float) section.getDouble(uid + ".ageTime", 0.0);
|
float ageTime = (float) section.getDouble(uid + ".ageTime", 0.0);
|
||||||
@ -234,21 +254,30 @@ public class P extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// loads BIngredients from ingredient section
|
// returns Ingredients by id from the specified ingMap
|
||||||
public BIngredients loadIngredients(ConfigurationSection config) {
|
public BIngredients getIngredients(Map<String, BIngredients> ingMap, String id) {
|
||||||
if (config != null) {
|
if (!ingMap.isEmpty()) {
|
||||||
ConfigurationSection matSection = config.getConfigurationSection("mats");
|
if (ingMap.containsKey(id)) {
|
||||||
if (matSection != null) {
|
return ingMap.get(id);
|
||||||
// matSection has all the materials + amount in Integer form
|
}
|
||||||
|
}
|
||||||
|
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>();
|
Map<Material, Integer> ingredients = new HashMap<Material, Integer>();
|
||||||
for (String ingredient : matSection.getKeys(false)) {
|
for (String ingredient : section.getKeys(false)) {
|
||||||
// convert to Material
|
// 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();
|
return new BIngredients();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user