diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index e464b27..af3c17a 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -11,6 +11,8 @@ import com.dre.brewery.integration.WGBarrel7; import com.dre.brewery.integration.WGBarrelNew; import com.dre.brewery.integration.WGBarrelOld; import com.dre.brewery.listeners.*; +import com.dre.brewery.lore.LoreInputStream; +import com.dre.brewery.lore.LoreOutputStream; import org.apache.commons.lang.math.NumberUtils; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; @@ -99,6 +101,7 @@ public class P extends JavaPlugin { test[1] = 6; test[2] = 12; test[3] = 21; + test[127] = 99; data.write(test); data.writeInt(123324); @@ -107,6 +110,18 @@ public class P extends JavaPlugin { data.close(); meta.getLore(); + LoreInputStream in = new LoreInputStream(meta); + DataInputStream dataIn = new DataInputStream(in); + + P.p.log(dataIn.readInt() + ", " + dataIn.readLong() + ", "); + byte[] testIn = new byte[128]; + dataIn.read(testIn); + P.p.log(testIn[1] + ", " + testIn[2] + ", " + testIn[3] + ", " + testIn[127]); + + P.p.log(dataIn.readInt() + ", " + dataIn.readLong() + ", "); + + dataIn.close(); + /*basE91 basE91 = new basE91(); diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index 7df7504..5564009 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -7,6 +7,8 @@ import com.dre.brewery.Brew; import com.dre.brewery.MCBarrel; import com.dre.brewery.P; import com.dre.brewery.integration.LogBlockBarrel; +import com.dre.brewery.lore.LoreInputStream; +import com.dre.brewery.lore.LoreOutputStream; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Sound; @@ -34,6 +36,9 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.scheduler.BukkitRunnable; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -298,6 +303,37 @@ public class InventoryListener implements Listener { } } brew.touch(); + + try { + LoreInputStream loreIn = new LoreInputStream(potion); + DataInputStream in = new DataInputStream(loreIn); + + if (in.readByte() == 27 && in.readUTF().equals("TESTHalloª∆Ω") && in.readInt() == 34834 && in.readLong() == Long.MAX_VALUE) { + P.p.log("true"); + } else { + P.p.log("false"); + } + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + + LoreOutputStream lore = new LoreOutputStream(potion, 3); + DataOutputStream out = new DataOutputStream(lore); + + out.writeByte(27); + out.writeUTF("TESTHalloª∆Ω"); + out.writeInt(34834); + out.writeLong(Long.MAX_VALUE); + + out.close(); + item.setItemMeta(potion); + + } catch (IOException e) { + e.printStackTrace(); + } } } } diff --git a/src/com/dre/brewery/lore/LoreInputStream.java b/src/com/dre/brewery/lore/LoreInputStream.java new file mode 100644 index 0000000..d346917 --- /dev/null +++ b/src/com/dre/brewery/lore/LoreInputStream.java @@ -0,0 +1,140 @@ +package com.dre.brewery.lore; + +import org.bukkit.inventory.meta.ItemMeta; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +public class LoreInputStream extends InputStream { + + private static final basE91 DECODER = new basE91(); + + private byte[] decbuf = new byte[18]; + private byte[] buf = new byte[16]; + private int reader = 0; + private int count = 0; + private ByteArrayInputStream readStream; + + public LoreInputStream(ItemMeta meta) throws IOException { + if (meta.hasLore()) { + List lore = meta.getLore(); + for (String line : lore) { + if (line.startsWith("§%")) { + StringBuilder build = new StringBuilder((int) (line.length() / 2F)); + byte skip = 2; + for (char c : line.toCharArray()) { + if (skip > 0) { + skip--; + continue; + } + if (c == '§') continue; + build.append(c); + } + readStream = new ByteArrayInputStream(build.toString().getBytes()); + break; + } + } + } + if (readStream == null) throw new IOException("Meta has no data in lore"); + } + + private void decode() throws IOException { + reader = 0; + count = readStream.read(decbuf); + if (count < 1) { + count = DECODER.decEnd(buf); + if (count < 1) { + count = -1; + } + return; + } + count = DECODER.decode(decbuf, count, buf); + } + + @Override + public int read() throws IOException { + if (count == -1) return -1; + if (count == 0 || reader == count) { + decode(); + return read(); + } + return buf[reader++] & 0xFF; + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + if (b == null) throw new NullPointerException(); + if (off < 0 || len < 0 || len > b.length - off) throw new IndexOutOfBoundsException(); + if (len == 0) return 0; + + if (count == -1) return -1; + if (count == 0 || reader == count) { + decode(); + if (count == -1) return -1; + } + + if (count > 0 && count - reader >= len) { + // enough data in buffer, copy it out directly + System.arraycopy(buf, reader, b, off, len); + reader += len; + return len; + } + + int out = 0; + int writeSize; + while (count > 0) { + writeSize = Math.min(len, count - reader); + System.arraycopy(buf, reader, b, off + out, writeSize); + out += writeSize; + len -= writeSize; + if (len > 0) { + decode(); + } else { + reader += writeSize; + break; + } + } + return out; + } + + @Override + public long skip(long n) throws IOException { + return super.skip(n); + } + + @Override + public int available() throws IOException { + return Math.round(readStream.available() * 0.813F); // Ratio encoded to decoded with random data + } + + @Override + public void close() throws IOException { + count = -1; + DECODER.decReset(); + buf = null; + decbuf = null; + readStream = null; + } + + @Override + public synchronized void mark(int readlimit) { + if (!markSupported()) return; + readStream.mark(readlimit); + DECODER.decMark(); + } + + @Override + public synchronized void reset() throws IOException { + if (!markSupported()) super.reset(); + readStream.reset(); + DECODER.decUnmark(); + } + + @Override + public boolean markSupported() { + return readStream.markSupported(); + } +} diff --git a/src/com/dre/brewery/lore/LoreOutputStream.java b/src/com/dre/brewery/lore/LoreOutputStream.java index 128f460..6352c74 100644 --- a/src/com/dre/brewery/lore/LoreOutputStream.java +++ b/src/com/dre/brewery/lore/LoreOutputStream.java @@ -92,7 +92,8 @@ public class LoreOutputStream extends OutputStream { stream.flush(); String s = stream.toString(); - StringBuilder loreLineBuilder = new StringBuilder(s.length() * 2); + StringBuilder loreLineBuilder = new StringBuilder((s.length() * 2) + 6); + loreLineBuilder.append("§%"); for (char c : s.toCharArray()) { loreLineBuilder.append('§').append(c); } diff --git a/src/com/dre/brewery/lore/basE91.java b/src/com/dre/brewery/lore/basE91.java index be38b14..12a0b94 100644 --- a/src/com/dre/brewery/lore/basE91.java +++ b/src/com/dre/brewery/lore/basE91.java @@ -34,6 +34,7 @@ package com.dre.brewery.lore; public class basE91 { private int ebq, en, dbq, dn, dv; + private int[] marker = null; public final byte[] enctab; private final byte[] dectab; @@ -122,6 +123,17 @@ public class basE91 dv = -1; } + public void decMark() { + marker = new int[] {dbq, dn, dv}; + } + + public void decUnmark() { + if (marker == null) return; + dbq = marker[0]; + dn = marker[1]; + dv = marker[2]; + } + public basE91() { int i;