mirror of
https://github.com/DieReicheErethons/Brewery.git
synced 2024-11-16 10:35:22 +01:00
Created LoreInputStream
This commit is contained in:
parent
34cbdf51dd
commit
c43c9b1203
@ -6,6 +6,7 @@ import com.dre.brewery.integration.WGBarrel;
|
||||
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.bukkit.*;
|
||||
@ -75,6 +76,7 @@ public class P extends JavaPlugin {
|
||||
test[1] = 6;
|
||||
test[2] = 12;
|
||||
test[3] = 21;
|
||||
test[127] = 99;
|
||||
data.write(test);
|
||||
|
||||
data.writeInt(123324);
|
||||
@ -83,6 +85,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();
|
||||
|
@ -2,6 +2,8 @@ package com.dre.brewery.listeners;
|
||||
|
||||
import com.dre.brewery.*;
|
||||
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.block.Block;
|
||||
@ -19,6 +21,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.UUID;
|
||||
@ -230,6 +235,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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
140
src/com/dre/brewery/lore/LoreInputStream.java
Normal file
140
src/com/dre/brewery/lore/LoreInputStream.java
Normal file
@ -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();
|
||||
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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user