mirror of
https://github.com/DieReicheErethons/Brewery.git
synced 2024-10-01 04:27:34 +02:00
Split LoreIn/OutputStream
Implemented skip, mark and reset
This commit is contained in:
parent
c43c9b1203
commit
26b607c17a
@ -6,8 +6,10 @@ import com.dre.brewery.integration.WGBarrel;
|
|||||||
import com.dre.brewery.integration.WGBarrelNew;
|
import com.dre.brewery.integration.WGBarrelNew;
|
||||||
import com.dre.brewery.integration.WGBarrelOld;
|
import com.dre.brewery.integration.WGBarrelOld;
|
||||||
import com.dre.brewery.listeners.*;
|
import com.dre.brewery.listeners.*;
|
||||||
import com.dre.brewery.lore.LoreInputStream;
|
import com.dre.brewery.lore.Base91DecoderStream;
|
||||||
import com.dre.brewery.lore.LoreOutputStream;
|
import com.dre.brewery.lore.Base91EncoderStream;
|
||||||
|
import com.dre.brewery.lore.LoreReader;
|
||||||
|
import com.dre.brewery.lore.LoreWriter;
|
||||||
import org.apache.commons.lang.math.NumberUtils;
|
import org.apache.commons.lang.math.NumberUtils;
|
||||||
import org.bukkit.*;
|
import org.bukkit.*;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
@ -66,8 +68,7 @@ public class P extends JavaPlugin {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
ItemMeta meta = new ItemStack(Material.POTION).getItemMeta();
|
ItemMeta meta = new ItemStack(Material.POTION).getItemMeta();
|
||||||
LoreOutputStream out = new LoreOutputStream(meta, 3);
|
DataOutputStream data = new DataOutputStream(new Base91EncoderStream(new LoreWriter(meta, 3)));
|
||||||
DataOutputStream data = new DataOutputStream(out);
|
|
||||||
|
|
||||||
data.writeInt(2);
|
data.writeInt(2);
|
||||||
data.writeLong(5);
|
data.writeLong(5);
|
||||||
@ -85,10 +86,10 @@ public class P extends JavaPlugin {
|
|||||||
data.close();
|
data.close();
|
||||||
meta.getLore();
|
meta.getLore();
|
||||||
|
|
||||||
LoreInputStream in = new LoreInputStream(meta);
|
DataInputStream dataIn = new DataInputStream(new Base91DecoderStream(new LoreReader(meta)));
|
||||||
DataInputStream dataIn = new DataInputStream(in);
|
|
||||||
|
|
||||||
P.p.log(dataIn.readInt() + ", " + dataIn.readLong() + ", ");
|
P.p.log(dataIn.readInt() + ", " + dataIn.readLong() + ", ");
|
||||||
|
|
||||||
byte[] testIn = new byte[128];
|
byte[] testIn = new byte[128];
|
||||||
dataIn.read(testIn);
|
dataIn.read(testIn);
|
||||||
P.p.log(testIn[1] + ", " + testIn[2] + ", " + testIn[3] + ", " + testIn[127]);
|
P.p.log(testIn[1] + ", " + testIn[2] + ", " + testIn[3] + ", " + testIn[127]);
|
||||||
|
@ -2,8 +2,10 @@ package com.dre.brewery.listeners;
|
|||||||
|
|
||||||
import com.dre.brewery.*;
|
import com.dre.brewery.*;
|
||||||
import com.dre.brewery.integration.LogBlockBarrel;
|
import com.dre.brewery.integration.LogBlockBarrel;
|
||||||
import com.dre.brewery.lore.LoreInputStream;
|
import com.dre.brewery.lore.Base91DecoderStream;
|
||||||
import com.dre.brewery.lore.LoreOutputStream;
|
import com.dre.brewery.lore.Base91EncoderStream;
|
||||||
|
import com.dre.brewery.lore.LoreReader;
|
||||||
|
import com.dre.brewery.lore.LoreWriter;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
@ -237,34 +239,51 @@ public class InventoryListener implements Listener {
|
|||||||
brew.touch();
|
brew.touch();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
LoreInputStream loreIn = new LoreInputStream(potion);
|
DataInputStream in = new DataInputStream(new Base91DecoderStream(new LoreReader(potion)));
|
||||||
DataInputStream in = new DataInputStream(loreIn);
|
|
||||||
|
|
||||||
if (in.readByte() == 27 && in.readUTF().equals("TESTHalloª∆Ω") && in.readInt() == 34834 && in.readLong() == Long.MAX_VALUE) {
|
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();
|
||||||
|
if (in.readUTF().equals("TESTHalloª∆Ω")) {
|
||||||
P.p.log("true");
|
P.p.log("true");
|
||||||
} else {
|
} else {
|
||||||
P.p.log("false");
|
P.p.log("false3");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
P.p.log("false2");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
P.p.log("false1");
|
||||||
|
}
|
||||||
|
|
||||||
in.close();
|
in.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
LoreOutputStream lore = new LoreOutputStream(potion, 3);
|
DataOutputStream out = new DataOutputStream(new Base91EncoderStream(new LoreWriter(potion, 0)));
|
||||||
DataOutputStream out = new DataOutputStream(lore);
|
|
||||||
|
|
||||||
out.writeByte(27);
|
out.writeByte(27);
|
||||||
|
out.writeLong(1111); //skip
|
||||||
|
out.writeLong(1111); //skip
|
||||||
|
out.writeLong(1111); //skip
|
||||||
|
out.writeLong(1111); //skip
|
||||||
|
out.writeLong(1111); //skip
|
||||||
|
out.writeLong(1111); //skip
|
||||||
out.writeUTF("TESTHalloª∆Ω");
|
out.writeUTF("TESTHalloª∆Ω");
|
||||||
out.writeInt(34834);
|
out.writeInt(34834);
|
||||||
|
out.writeInt(6436); //skip
|
||||||
out.writeLong(Long.MAX_VALUE);
|
out.writeLong(Long.MAX_VALUE);
|
||||||
|
|
||||||
out.close();
|
out.close();
|
||||||
item.setItemMeta(potion);
|
item.setItemMeta(potion);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException h) {
|
||||||
e.printStackTrace();
|
h.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,10 @@
|
|||||||
package com.dre.brewery.lore;
|
package com.dre.brewery.lore;
|
||||||
|
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import java.io.FilterInputStream;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class LoreInputStream extends InputStream {
|
public class Base91DecoderStream extends FilterInputStream {
|
||||||
|
|
||||||
private static final basE91 DECODER = new basE91();
|
private static final basE91 DECODER = new basE91();
|
||||||
|
|
||||||
@ -16,34 +12,15 @@ public class LoreInputStream extends InputStream {
|
|||||||
private byte[] buf = new byte[16];
|
private byte[] buf = new byte[16];
|
||||||
private int reader = 0;
|
private int reader = 0;
|
||||||
private int count = 0;
|
private int count = 0;
|
||||||
private ByteArrayInputStream readStream;
|
private byte[] markBuf = null;
|
||||||
|
|
||||||
public LoreInputStream(ItemMeta meta) throws IOException {
|
public Base91DecoderStream(InputStream in) {
|
||||||
if (meta.hasLore()) {
|
super(in);
|
||||||
List<String> 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 {
|
private void decode() throws IOException {
|
||||||
reader = 0;
|
reader = 0;
|
||||||
count = readStream.read(decbuf);
|
count = in.read(decbuf);
|
||||||
if (count < 1) {
|
if (count < 1) {
|
||||||
count = DECODER.decEnd(buf);
|
count = DECODER.decEnd(buf);
|
||||||
if (count < 1) {
|
if (count < 1) {
|
||||||
@ -102,39 +79,68 @@ public class LoreInputStream extends InputStream {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long skip(long n) throws IOException {
|
public long skip(long n) throws IOException {
|
||||||
return super.skip(n);
|
if (count == -1) return 0;
|
||||||
|
if (count > 0 && count - reader >= n) {
|
||||||
|
reader += n;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
long skipped = count - reader;
|
||||||
|
decode();
|
||||||
|
|
||||||
|
while (count > 0) {
|
||||||
|
if (count > n - skipped) {
|
||||||
|
reader = (int) (n - skipped);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
skipped += count;
|
||||||
|
decode();
|
||||||
|
}
|
||||||
|
return skipped;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int available() throws IOException {
|
public int available() throws IOException {
|
||||||
return Math.round(readStream.available() * 0.813F); // Ratio encoded to decoded with random data
|
return Math.round(in.available() * 0.813F); // Ratio encoded to decoded with random data
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
|
in.close();
|
||||||
count = -1;
|
count = -1;
|
||||||
DECODER.decReset();
|
DECODER.decReset();
|
||||||
buf = null;
|
buf = null;
|
||||||
decbuf = null;
|
decbuf = null;
|
||||||
readStream = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void mark(int readlimit) {
|
public synchronized void mark(int readlimit) {
|
||||||
if (!markSupported()) return;
|
if (!markSupported()) return;
|
||||||
readStream.mark(readlimit);
|
if (count == -1) return;
|
||||||
|
in.mark(readlimit);
|
||||||
DECODER.decMark();
|
DECODER.decMark();
|
||||||
|
if (count > 0 && reader < count) {
|
||||||
|
markBuf = new byte[count - reader];
|
||||||
|
System.arraycopy(buf, reader, markBuf, 0, markBuf.length);
|
||||||
|
} else {
|
||||||
|
markBuf = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void reset() throws IOException {
|
public synchronized void reset() throws IOException {
|
||||||
if (!markSupported()) super.reset();
|
if (!markSupported()) throw new IOException("mark and reset not supported");
|
||||||
readStream.reset();
|
in.reset();
|
||||||
DECODER.decUnmark();
|
DECODER.decUnmark();
|
||||||
|
reader = 0;
|
||||||
|
count = 0;
|
||||||
|
if (markBuf != null) {
|
||||||
|
System.arraycopy(markBuf, 0, buf, 0, markBuf.length);
|
||||||
|
count = markBuf.length;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean markSupported() {
|
public boolean markSupported() {
|
||||||
return readStream.markSupported();
|
return in.markSupported();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,12 +1,11 @@
|
|||||||
package com.dre.brewery.lore;
|
package com.dre.brewery.lore;
|
||||||
|
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.FilterOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import java.io.*;
|
public class Base91EncoderStream extends FilterOutputStream {
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class LoreOutputStream extends OutputStream {
|
|
||||||
|
|
||||||
private static final basE91 ENCODER = new basE91();
|
private static final basE91 ENCODER = new basE91();
|
||||||
|
|
||||||
@ -15,18 +14,13 @@ public class LoreOutputStream extends OutputStream {
|
|||||||
private int writer = 0;
|
private int writer = 0;
|
||||||
private int encoded = 0;
|
private int encoded = 0;
|
||||||
|
|
||||||
private ItemMeta meta;
|
public Base91EncoderStream(OutputStream out) {
|
||||||
private final int line;
|
super(out);
|
||||||
private ByteArrayOutputStream stream = new ByteArrayOutputStream(128);
|
|
||||||
|
|
||||||
public LoreOutputStream(ItemMeta meta, int line) {
|
|
||||||
this.meta = meta;
|
|
||||||
this.line = line;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void encFlush() {
|
private void encFlush() throws IOException {
|
||||||
encoded = ENCODER.encode(buf, writer, encBuf);
|
encoded = ENCODER.encode(buf, writer, encBuf);
|
||||||
stream.write(encBuf, 0, encoded);
|
out.write(encBuf, 0, encoded);
|
||||||
writer = 0;
|
writer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +54,7 @@ public class LoreOutputStream extends OutputStream {
|
|||||||
// Buffer is too full, so flush and encode data directly
|
// Buffer is too full, so flush and encode data directly
|
||||||
encFlush();
|
encFlush();
|
||||||
encoded = ENCODER.encode(b, len, encBuf);
|
encoded = ENCODER.encode(b, len, encBuf);
|
||||||
stream.write(encBuf, 0, encoded);
|
out.write(encBuf, 0, encoded);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,47 +72,22 @@ public class LoreOutputStream extends OutputStream {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void flush() throws IOException {
|
public void flush() throws IOException {
|
||||||
super.flush();
|
|
||||||
if (writer > 0) {
|
if (writer > 0) {
|
||||||
encFlush();
|
encFlush();
|
||||||
}
|
}
|
||||||
|
|
||||||
encoded = ENCODER.encEnd(encBuf);
|
encoded = ENCODER.encEnd(encBuf);
|
||||||
if (encoded > 0) {
|
if (encoded > 0) {
|
||||||
stream.write(encBuf, 0, encoded);
|
out.write(encBuf, 0, encoded);
|
||||||
}
|
}
|
||||||
if (stream.size() <= 0) return;
|
super.flush();
|
||||||
|
|
||||||
stream.flush();
|
|
||||||
String s = stream.toString();
|
|
||||||
|
|
||||||
StringBuilder loreLineBuilder = new StringBuilder((s.length() * 2) + 6);
|
|
||||||
loreLineBuilder.append("§%");
|
|
||||||
for (char c : s.toCharArray()) {
|
|
||||||
loreLineBuilder.append('§').append(c);
|
|
||||||
}
|
|
||||||
List<String> lore;
|
|
||||||
if (meta.hasLore()) {
|
|
||||||
lore = meta.getLore();
|
|
||||||
} else {
|
|
||||||
lore = new ArrayList<>();
|
|
||||||
}
|
|
||||||
while (lore.size() < line) {
|
|
||||||
lore.add("");
|
|
||||||
}
|
|
||||||
//TODO when existing data string in lore
|
|
||||||
lore.add(line, loreLineBuilder.toString());
|
|
||||||
meta.setLore(lore);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
super.close();
|
super.close();
|
||||||
stream.close();
|
|
||||||
ENCODER.encReset();
|
ENCODER.encReset();
|
||||||
buf = null;
|
buf = null;
|
||||||
encBuf = null;
|
encBuf = null;
|
||||||
meta = null;
|
|
||||||
stream = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
36
src/com/dre/brewery/lore/LoreReader.java
Normal file
36
src/com/dre/brewery/lore/LoreReader.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package com.dre.brewery.lore;
|
||||||
|
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class LoreReader extends ByteArrayInputStream {
|
||||||
|
|
||||||
|
public LoreReader(ItemMeta meta) throws IOException {
|
||||||
|
super(loreToBytes(meta));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] loreToBytes(ItemMeta meta) throws IOException {
|
||||||
|
if (meta.hasLore()) {
|
||||||
|
List<String> 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);
|
||||||
|
}
|
||||||
|
return build.toString().getBytes();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IOException("Meta has no data in lore");
|
||||||
|
}
|
||||||
|
}
|
@ -2,24 +2,58 @@ package com.dre.brewery.lore;
|
|||||||
|
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class LoreWriter extends OutputStream {
|
public class LoreWriter extends ByteArrayOutputStream {
|
||||||
|
|
||||||
ItemMeta meta;
|
private ItemMeta meta;
|
||||||
|
private int line;
|
||||||
|
private boolean flushed = false;
|
||||||
|
|
||||||
public LoreWriter(ItemMeta meta) {
|
public LoreWriter(ItemMeta meta, int line) {
|
||||||
|
super(128);
|
||||||
this.meta = meta;
|
this.meta = meta;
|
||||||
|
this.line = line;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writes to the Lore
|
||||||
|
// Without calling this, the ItemMeta remains unchanged
|
||||||
|
@Override
|
||||||
|
public void flush() throws IOException {
|
||||||
|
super.flush();
|
||||||
|
if (size() <= 0) return;
|
||||||
|
if (flushed) {
|
||||||
|
// Dont write twice
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
flushed = true;
|
||||||
|
String s = toString();
|
||||||
|
|
||||||
|
StringBuilder loreLineBuilder = new StringBuilder((s.length() * 2) + 6);
|
||||||
|
loreLineBuilder.append("§%");
|
||||||
|
for (char c : s.toCharArray()) {
|
||||||
|
loreLineBuilder.append('§').append(c);
|
||||||
|
}
|
||||||
|
List<String> lore;
|
||||||
|
if (meta.hasLore()) {
|
||||||
|
lore = meta.getLore();
|
||||||
|
} else {
|
||||||
|
lore = new ArrayList<>();
|
||||||
|
}
|
||||||
|
while (lore.size() < line) {
|
||||||
|
lore.add("");
|
||||||
|
}
|
||||||
|
//TODO when existing data string in lore
|
||||||
|
lore.add(line, loreLineBuilder.toString());
|
||||||
|
meta.setLore(lore);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(int b) throws IOException {
|
public void close() throws IOException {
|
||||||
|
super.close();
|
||||||
}
|
meta = null;
|
||||||
|
|
||||||
@Override
|
|
||||||
public void write(byte[] b, int off, int len) throws IOException {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user