mirror of
https://github.com/exituser/Brewery.git
synced 2026-09-17 07:49:03 +00:00
Created LoreInputStream
This commit is contained in:
@@ -11,6 +11,8 @@ import com.dre.brewery.integration.WGBarrel7;
|
|||||||
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.LoreOutputStream;
|
||||||
import org.apache.commons.lang.math.NumberUtils;
|
import org.apache.commons.lang.math.NumberUtils;
|
||||||
import org.bstats.bukkit.Metrics;
|
import org.bstats.bukkit.Metrics;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
@@ -99,6 +101,7 @@ public class P extends JavaPlugin {
|
|||||||
test[1] = 6;
|
test[1] = 6;
|
||||||
test[2] = 12;
|
test[2] = 12;
|
||||||
test[3] = 21;
|
test[3] = 21;
|
||||||
|
test[127] = 99;
|
||||||
data.write(test);
|
data.write(test);
|
||||||
|
|
||||||
data.writeInt(123324);
|
data.writeInt(123324);
|
||||||
@@ -107,6 +110,18 @@ public class P extends JavaPlugin {
|
|||||||
data.close();
|
data.close();
|
||||||
meta.getLore();
|
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();
|
/*basE91 basE91 = new basE91();
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import com.dre.brewery.Brew;
|
|||||||
import com.dre.brewery.MCBarrel;
|
import com.dre.brewery.MCBarrel;
|
||||||
import com.dre.brewery.P;
|
import com.dre.brewery.P;
|
||||||
import com.dre.brewery.integration.LogBlockBarrel;
|
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.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.Sound;
|
import org.bukkit.Sound;
|
||||||
@@ -34,6 +36,9 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
import org.bukkit.inventory.meta.PotionMeta;
|
import org.bukkit.inventory.meta.PotionMeta;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
@@ -298,6 +303,37 @@ public class InventoryListener implements Listener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
brew.touch();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<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 {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -92,7 +92,8 @@ public class LoreOutputStream extends OutputStream {
|
|||||||
stream.flush();
|
stream.flush();
|
||||||
String s = stream.toString();
|
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()) {
|
for (char c : s.toCharArray()) {
|
||||||
loreLineBuilder.append('§').append(c);
|
loreLineBuilder.append('§').append(c);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ package com.dre.brewery.lore;
|
|||||||
public class basE91
|
public class basE91
|
||||||
{
|
{
|
||||||
private int ebq, en, dbq, dn, dv;
|
private int ebq, en, dbq, dn, dv;
|
||||||
|
private int[] marker = null;
|
||||||
public final byte[] enctab;
|
public final byte[] enctab;
|
||||||
private final byte[] dectab;
|
private final byte[] dectab;
|
||||||
|
|
||||||
@@ -122,6 +123,17 @@ public class basE91
|
|||||||
dv = -1;
|
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()
|
public basE91()
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|||||||
Reference in New Issue
Block a user