diff --git a/src/com/dre/brewery/BIngredients.java b/src/com/dre/brewery/BIngredients.java index 1859824..d68d6f0 100644 --- a/src/com/dre/brewery/BIngredients.java +++ b/src/com/dre/brewery/BIngredients.java @@ -6,6 +6,9 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.potion.PotionEffectType; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.*; public class BIngredients { @@ -321,6 +324,37 @@ public class BIngredients { return copy; } + public void testStore(DataOutputStream out) throws IOException { + out.writeInt(cookedTime); + out.writeByte(ingredients.size()); + for (ItemStack item : ingredients) { + out.writeUTF(item.getType().name()); + out.writeShort(item.getDurability()); + out.writeShort(item.getAmount()); + } + } + + public void testLoad(DataInputStream in) throws IOException { + if (in.readInt() != cookedTime) { + P.p.log("cookedtime wrong"); + } + if (in.readUnsignedByte() != ingredients.size()) { + P.p.log("size wrong"); + return; + } + for (ItemStack item : ingredients) { + if (!in.readUTF().equals(item.getType().name())) { + P.p.log("name wrong"); + } + if (in.readShort() != item.getDurability()) { + P.p.log("dur wrong"); + } + if (in.readShort() != item.getAmount()) { + P.p.log("amount wrong"); + } + } + } + // saves data into main Ingredient section. Returns the save id public int save(ConfigurationSection config) { String path = "Ingredients." + id; diff --git a/src/com/dre/brewery/Brew.java b/src/com/dre/brewery/Brew.java index 60994b4..58f371a 100644 --- a/src/com/dre/brewery/Brew.java +++ b/src/com/dre/brewery/Brew.java @@ -12,6 +12,9 @@ import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionType; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -649,6 +652,79 @@ public class Brew { return P.p.color(color); } + public void testStore(DataOutputStream out) throws IOException { + out.writeByte(1); // Version + out.writeByte(86); // Parity + out.writeInt(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 += (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)); + } + ingredients.testStore(out); + } + + public void testLoad(DataInputStream in) throws IOException { + if (in.readByte() != 1) { + P.p.log("unknown version"); + return; + } + if (in.readByte() != 86) { + P.p.log("parity check failed"); + return; + } + if (in.readInt() != quality) { + P.p.log("quality wrong"); + } + int bools = in.readUnsignedByte(); + if ((bools & 1) != 0) { + if (in.readByte() != distillRuns) { + P.p.log("distillruns wrong"); + } + } + if ((bools & 2) != 0) { + if (in.readFloat() != ageTime) { + P.p.log("agetime wrong"); + } + } + if ((bools & 4) != 0) { + if (in.readFloat() != wood) { + P.p.log("wood wrong"); + } + } + if ((bools & 8) != 0) { + if (!in.readUTF().equals(currentRecipe.getName(5))) { + P.p.log("currecipe wrong"); + } + } + if ((bools & 16) != 0 && !unlabeled) { + P.p.log("unlabeled wrong"); + } + if ((bools & 32) != 0 && !persistent) { + P.p.log("persistent wrong"); + } + if ((bools & 64) != 0 && !stat) { + P.p.log("stat wrong"); + } + ingredients.testLoad(in); + } + // Saves all data public static void save(ConfigurationSection config) { for (Map.Entry entry : potions.entrySet()) { diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index 1d7c286..094d75f 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -309,7 +309,9 @@ public class InventoryListener implements Listener { try { DataInputStream in = new DataInputStream(new Base91DecoderStream(new LoreReader(potion))); - if (in.readByte() == 27 && in.skip(48) > 0) { + brew.testLoad(in); + + /*if (in.readByte() == 27 && in.skip(48) > 0) { in.mark(100); if (in.readUTF().equals("TESTHalloª∆Ω") && in.readInt() == 34834 && in.skip(4) > 0 && in.readLong() == Long.MAX_VALUE) { in.reset(); @@ -323,7 +325,7 @@ public class InventoryListener implements Listener { } } else { P.p.log("false1"); - } + }*/ in.close(); } catch (IOException e) { @@ -331,9 +333,12 @@ public class InventoryListener implements Listener { try { - DataOutputStream out = new DataOutputStream(new Base91EncoderStream(new LoreWriter(potion, 0))); + DataOutputStream out = new DataOutputStream(new Base91EncoderStream(new LoreWriter(potion, 2))); - out.writeByte(27); + brew.testStore(out); + + + /*out.writeByte(27); out.writeLong(1111); //skip out.writeLong(1111); //skip out.writeLong(1111); //skip @@ -343,7 +348,7 @@ public class InventoryListener implements Listener { out.writeUTF("TESTHalloª∆Ω"); out.writeInt(34834); out.writeInt(6436); //skip - out.writeLong(Long.MAX_VALUE); + out.writeLong(Long.MAX_VALUE);*/ out.close(); item.setItemMeta(potion);