Test storing and loading a brew from lore

This commit is contained in:
Sn0wStorm 2016-05-30 22:12:34 +02:00
parent 6311b9ed15
commit ed40a1f608
3 changed files with 120 additions and 5 deletions

View File

@ -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;

View File

@ -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<Integer, Brew> entry : potions.entrySet()) {

View File

@ -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);