Save ingredients of Brews

This commit is contained in:
Sn0wStorm 2016-06-17 00:04:41 +02:00
parent 4b18dceada
commit cdb36b1736
3 changed files with 58 additions and 78 deletions

View File

@ -262,7 +262,7 @@ public class BRecipe {
BIngredients bIngredients = new BIngredients(list, cookingTime); BIngredients bIngredients = new BIngredients(list, cookingTime);
Brew brew = new Brew(bIngredients, quality, distillruns, getAge(), wood, getName(5), false, false, true); Brew brew = new Brew(bIngredients, quality, distillruns, getAge(), wood, getName(5), false, true);
Brew.PotionColor.fromString(getColor()).colorBrew(potionMeta, potion, false); Brew.PotionColor.fromString(getColor()).colorBrew(potionMeta, potion, false);
potionMeta.setDisplayName(P.p.color("&f" + getName(quality))); potionMeta.setDisplayName(P.p.color("&f" + getName(quality)));

View File

@ -41,8 +41,8 @@ public class Brew {
private float wood; private float wood;
private BRecipe currentRecipe; private BRecipe currentRecipe;
private boolean unlabeled; private boolean unlabeled;
private boolean persistent; private boolean persistent; // Only for legacy
private boolean stat; // static potions should not be changed private boolean immutable; // static/immutable potions should not be changed
//private int lastUpdate; // last update in hours after install time //private int lastUpdate; // last update in hours after install time
public Brew(BIngredients ingredients) { public Brew(BIngredients ingredients) {
@ -57,15 +57,14 @@ public class Brew {
} }
// loading with all values set // loading with all values set
public Brew(BIngredients ingredients, int quality, byte distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean persistent, boolean stat) { public Brew(BIngredients ingredients, int quality, byte distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean immutable) {
this.ingredients = ingredients; this.ingredients = ingredients;
this.quality = quality; this.quality = quality;
this.distillRuns = distillRuns; this.distillRuns = distillRuns;
this.ageTime = ageTime; this.ageTime = ageTime;
this.wood = wood; this.wood = wood;
this.unlabeled = unlabeled; this.unlabeled = unlabeled;
this.persistent = persistent; this.immutable = immutable;
this.stat = stat;
setRecipeFromString(recipe); setRecipeFromString(recipe);
} }
@ -191,7 +190,7 @@ public class Brew {
if (quality > 0) { if (quality > 0) {
currentRecipe = ingredients.getBestRecipe(wood, ageTime, distillRuns > 0); currentRecipe = ingredients.getBestRecipe(wood, ageTime, distillRuns > 0);
if (currentRecipe != null) { if (currentRecipe != null) {
if (!stat) { if (!immutable) {
this.quality = calcQuality(); this.quality = calcQuality();
} }
P.p.log("Brew was made from Recipe: '" + name + "' which could not be found. '" + currentRecipe.getName(5) + "' used instead!"); P.p.log("Brew was made from Recipe: '" + name + "' which could not be found. '" + currentRecipe.getName(5) + "' used instead!");
@ -232,9 +231,8 @@ public class Brew {
brew.distillRuns = distillRuns; brew.distillRuns = distillRuns;
brew.ageTime = ageTime; brew.ageTime = ageTime;
brew.unlabeled = unlabeled; brew.unlabeled = unlabeled;
if (!brew.persistent) { brew.persistent = persistent;
brew.stat = stat; brew.immutable = immutable;
}
return brew; return brew;
} }
@ -248,8 +246,7 @@ public class Brew {
", wood=" + wood + ", wood=" + wood +
", currentRecipe=" + currentRecipe + ", currentRecipe=" + currentRecipe +
", unlabeled=" + unlabeled + ", unlabeled=" + unlabeled +
", persistent=" + persistent + ", immutable=" + immutable +
", stat=" + stat +
'}'; '}';
} }
@ -321,7 +318,7 @@ public class Brew {
} }
public boolean canDistill() { public boolean canDistill() {
if (stat) return false; if (immutable) return false;
if (currentRecipe != null) { if (currentRecipe != null) {
return currentRecipe.getDistillRuns() > distillRuns; return currentRecipe.getDistillRuns() > distillRuns;
} else { } else {
@ -392,14 +389,14 @@ public class Brew {
} }
public boolean isStatic() { public boolean isStatic() {
return stat; return immutable;
} }
// Set the Static flag, so potion is unchangeable // Set the Static flag, so potion is unchangeable
public void setStatic(boolean stat, ItemStack potion) { public void setStatic(boolean immutable, ItemStack potion) {
this.stat = stat; this.immutable = immutable;
if (currentRecipe != null && canDistill()) { if (currentRecipe != null && canDistill()) {
if (stat) { if (immutable) {
PotionColor.fromString(currentRecipe.getColor()).colorBrew(((PotionMeta) potion.getItemMeta()), potion, false); PotionColor.fromString(currentRecipe.getColor()).colorBrew(((PotionMeta) potion.getItemMeta()), potion, false);
} else { } else {
PotionColor.fromString(currentRecipe.getColor()).colorBrew(((PotionMeta) potion.getItemMeta()), potion, true); PotionColor.fromString(currentRecipe.getColor()).colorBrew(((PotionMeta) potion.getItemMeta()), potion, true);
@ -426,9 +423,7 @@ public class Brew {
// distill custom potion in given slot // distill custom potion in given slot
public void distillSlot(ItemStack slotItem, PotionMeta potionMeta) { public void distillSlot(ItemStack slotItem, PotionMeta potionMeta) {
if (stat) { if (immutable) return;
return;
}
distillRuns += 1; distillRuns += 1;
BRecipe recipe = ingredients.getdistillRecipe(wood, ageTime); BRecipe recipe = ingredients.getdistillRecipe(wood, ageTime);
@ -484,9 +479,7 @@ public class Brew {
// Ageing Section ------------------ // Ageing Section ------------------
public void age(ItemStack item, float time, byte woodType) { public void age(ItemStack item, float time, byte woodType) {
if (stat) { if (immutable) return;
return;
}
PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); PotionMeta potionMeta = (PotionMeta) item.getItemMeta();
ageTime += time; ageTime += time;
@ -739,8 +732,7 @@ public class Brew {
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
return null; return null;
} }
DataInputStream in = new DataInputStream(new Base91DecoderStream(loreStream)); try (DataInputStream in = new DataInputStream(new Base91DecoderStream(loreStream))) {
try {
if (in.readByte() != 86) { if (in.readByte() != 86) {
P.p.errorLog("parity check failed on Brew while loading, trying to load anyways!"); P.p.errorLog("parity check failed on Brew while loading, trying to load anyways!");
} }
@ -758,18 +750,10 @@ public class Brew {
} catch (IOException e) { } catch (IOException e) {
P.p.errorLog("IO Error while loading Brew"); P.p.errorLog("IO Error while loading Brew");
e.printStackTrace(); e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
return null; return null;
} }
// TODO load and save Ingredients
private void loadFromStream(DataInputStream in) throws IOException { private void loadFromStream(DataInputStream in) throws IOException {
quality = in.readByte(); quality = in.readByte();
int bools = in.readUnsignedByte(); int bools = in.readUnsignedByte();
@ -788,14 +772,19 @@ public class Brew {
setRecipeFromString(null); setRecipeFromString(null);
} }
unlabeled = (bools & 16) != 0; unlabeled = (bools & 16) != 0;
persistent = (bools & 32) != 0; //persistent = (bools & 32) != 0;
stat = (bools & 64) != 0; immutable = (bools & 32) != 0;
ingredients = BIngredients.load(in);
} }
// Save brew data into meta/lore // Save brew data into meta/lore
public void save(ItemMeta meta) { public void save(ItemMeta meta) {
LoreSaveStream loreStream = new LoreSaveStream(meta, 0); try (DataOutputStream out = new DataOutputStream(new Base91EncoderStream(new LoreSaveStream(meta, 0)))) {
saveToStream(new DataOutputStream(new Base91EncoderStream(loreStream))); saveToStream(out);
} catch (IOException e) {
P.p.errorLog("IO Error while saving Brew");
e.printStackTrace();
}
} }
// Save brew data into the meta/lore of the specified item // Save brew data into the meta/lore of the specified item
@ -811,50 +800,41 @@ public class Brew {
item.setItemMeta(meta); item.setItemMeta(meta);
} }
public void saveToStream(DataOutputStream out) { public void saveToStream(DataOutputStream out) throws IOException {
try { out.writeByte(86); // Parity/sanity
out.writeByte(86); // Parity/sanity out.writeByte(1); // Version
out.writeByte(1); // Version if (quality > 10) {
if (quality > 10) { quality = 10;
quality = 10;
}
out.writeByte((byte) quality);
int bools = 0;
bools |= ((distillRuns != 0) ? 1 : 0);
bools |= (ageTime > 0 ? 2 : 0);
bools |= (wood != -1 ? 4 : 0);
bools |= (currentRecipe != null ? 8 : 0);
bools |= (unlabeled ? 16 : 0);
bools |= (persistent ? 32 : 0); // TODO remove persistence
bools |= (stat ? 64 : 0);
out.writeByte(bools);
if (distillRuns != 0) {
out.writeByte(distillRuns);
}
if (ageTime > 0) {
out.writeFloat(ageTime);
}
if (wood != -1) {
out.writeFloat(wood);
}
if (currentRecipe != null) {
out.writeUTF(currentRecipe.getName(5));
}
} catch (IOException e) {
P.p.errorLog("IO Error while saving Brew");
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
out.writeByte((byte) quality);
int bools = 0;
bools |= ((distillRuns != 0) ? 1 : 0);
bools |= (ageTime > 0 ? 2 : 0);
bools |= (wood != -1 ? 4 : 0);
bools |= (currentRecipe != null ? 8 : 0);
bools |= (unlabeled ? 16 : 0);
//bools |= (persistent ? 32 : 0);
bools |= (immutable ? 32 : 0);
out.writeByte(bools);
if (distillRuns != 0) {
out.writeByte(distillRuns);
}
if (ageTime > 0) {
out.writeFloat(ageTime);
}
if (wood != -1) {
out.writeFloat(wood);
}
if (currentRecipe != null) {
out.writeUTF(currentRecipe.getName(5));
}
ingredients.save(out);
} }
// Load potion data from data file for backwards compatibility // Load potion data from data file for backwards compatibility
public static void loadLegacy(BIngredients ingredients, int uid, int quality, byte distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean persistent, boolean stat) { public static void loadLegacy(BIngredients ingredients, int uid, int quality, byte distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean persistent, boolean stat) {
Brew brew = new Brew(ingredients, quality, distillRuns, ageTime, wood, recipe, unlabeled, persistent, stat); Brew brew = new Brew(ingredients, quality, distillRuns, ageTime, wood, recipe, unlabeled, stat);
brew.persistent = persistent;
legacyPotions.put(uid, brew); legacyPotions.put(uid, brew);
} }
@ -904,7 +884,7 @@ public class Brew {
if (brew.persistent) { if (brew.persistent) {
idConfig.set("persist", true); idConfig.set("persist", true);
} }
if (brew.stat) { if (brew.immutable) {
idConfig.set("stat", true); idConfig.set("stat", true);
} }
/*if (brew.lastUpdate > 0) { /*if (brew.lastUpdate > 0) {

View File

@ -297,7 +297,7 @@ public class InventoryListener implements Listener {
} }
} }
P.p.log(brew.toString()); P.p.log(brew.toString());
//P.p.log(potion.getLore().get(0)); P.p.log(potion.getLore().get(0).replaceAll("§", ""));
//brew.touch(); //brew.touch();
/*try { /*try {