Started LoreOutputStream storing data with Base91

This commit is contained in:
Sn0wStorm 2016-05-28 14:08:33 +02:00
parent e5438e7ae3
commit 75d0f2f201
4 changed files with 391 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import com.dre.brewery.lore.LoreOutputStream;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
@ -85,6 +86,109 @@ public class P extends JavaPlugin {
//P.p.log("§" + (use1_9 ? "a":"c") + "1.9 " + "§" + (use1_11 ? "a":"c") + "1.11 " + "§" + (use1_13 ? "a":"c") + "1.13 " + "§" + (use1_14 ? "a":"c") + "1.14");
try {
ItemMeta meta = new ItemStack(Material.POTION).getItemMeta();
LoreOutputStream out = new LoreOutputStream(meta, 3);
DataOutputStream data = new DataOutputStream(out);
data.writeInt(2);
data.writeLong(5);
byte[] test = new byte[128];
test[1] = 6;
test[2] = 12;
test[3] = 21;
data.write(test);
data.writeInt(123324);
data.writeLong(12343843);
data.close();
meta.getLore();
/*basE91 basE91 = new basE91();
int[] input = new int[] {12, 65, 324, 5, 12, 129459, 1234567, Integer.MIN_VALUE, Integer.MAX_VALUE};
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream data = new DataOutputStream(stream);
for (int i = 0; i < input.length; i++) {
data.writeInt(input[i]);
}
data.flush();
data.close();
byte[] in = stream.toByteArray();
byte[] out = new byte[4096];
int lenght = basE91.encode(in, in.length, out);
basE91.encEnd(out);
String done = new String(out, 0, lenght);
byte[] tin = done.getBytes();
byte[] tout = new byte[4096];
lenght = basE91.decode(tin, tin.length, tout);
basE91.decEnd(tout);
ByteArrayInputStream tstream = new ByteArrayInputStream(tout, 0, lenght);
DataInputStream tdata = new DataInputStream(tstream);
int[] test = new int[4096];
for (int j = 0; j < 6; j++) {
if (tstream.available() <= 0) break;
test[j] = tdata.readInt();
}
tdata.close();
test = test;*/
/*basE91 basE91 = new basE91();
int[] input = new int[] {12, 65, 324, 5, 12, 129459, 1234567, Integer.MIN_VALUE, Integer.MAX_VALUE};
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DataOutputStream data = new DataOutputStream(stream);
for (int i = 0; i < input.length; i++) {
data.writeInt(input[i]);
}
data.flush();
data.close();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(stream.toByteArray());
encode(in, out, in.available());
in.close();
out.flush();
out.close();
String done = new String(out.toByteArray());
ByteArrayInputStream tin = new ByteArrayInputStream(done.getBytes());
ByteArrayOutputStream tout = new ByteArrayOutputStream();
decode(tin, tout, tin.available());
tin.close();
tout.flush();
tout.close();
ByteArrayInputStream tstream = new ByteArrayInputStream(tout.toByteArray());
DataInputStream tdata = new DataInputStream(tstream);
int[] test = new int[4096];
for (int j = 0; j < 9; j++) {
if (tstream.available() <= 0) break;
test[j] = tdata.readInt();
}
tdata.close();
test = test;*/
} catch (IOException e) {
e.printStackTrace();
}
// load the Config
try {
if (!readConfig()) {

View File

@ -0,0 +1,123 @@
package com.dre.brewery.lore;
import org.bukkit.inventory.meta.ItemMeta;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class LoreOutputStream extends OutputStream {
private static final basE91 ENCODER = new basE91();
private byte[] buf = new byte[16];
private byte[] encBuf = new byte[24];
private int writer = 0;
private int encoded = 0;
private ItemMeta meta;
private final int line;
private ByteArrayOutputStream stream = new ByteArrayOutputStream(128);
public LoreOutputStream(ItemMeta meta, int line) {
this.meta = meta;
this.line = line;
}
private void encFlush() {
encoded = ENCODER.encode(buf, writer, encBuf);
stream.write(encBuf, 0, encoded);
writer = 0;
}
@Override
public void write(int b) throws IOException {
buf[writer++] = (byte) b;
if (writer >= buf.length) {
encFlush();
}
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
if (len == 0) return;
if (b == null) throw new NullPointerException();
if (len < 0 || off < 0 || (off + len) > b.length || off > b.length || (off + len) < 0) {
throw new IndexOutOfBoundsException();
}
if (buf.length - writer >= len) {
// Enough space in the buffer, copy it in
System.arraycopy(b, off, buf, writer, len);
writer += len;
if (writer >= buf.length) {
encFlush();
}
return;
}
if (off == 0 && buf.length >= len) {
// Buffer is too full, so flush and encode data directly
encFlush();
encoded = ENCODER.encode(b, len, encBuf);
stream.write(encBuf, 0, encoded);
return;
}
// More data than space in the Buffer
ByteArrayInputStream in = new ByteArrayInputStream(b, off, len);
while (true) {
writer += in.read(buf, writer, buf.length - writer);
if (writer >= buf.length) {
encFlush();
} else {
break;
}
}
}
@Override
public void flush() throws IOException {
super.flush();
if (writer > 0) {
encFlush();
}
encoded = ENCODER.encEnd(encBuf);
if (encoded > 0) {
stream.write(encBuf, 0, encoded);
}
if (stream.size() <= 0) return;
stream.flush();
String s = stream.toString();
StringBuilder loreLineBuilder = new StringBuilder(s.length() * 2);
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
public void close() throws IOException {
super.close();
stream.close();
ENCODER.encReset();
buf = null;
encBuf = null;
meta = null;
stream = null;
}
}

View File

@ -0,0 +1,25 @@
package com.dre.brewery.lore;
import org.bukkit.inventory.meta.ItemMeta;
import java.io.IOException;
import java.io.OutputStream;
public class LoreWriter extends OutputStream {
ItemMeta meta;
public LoreWriter(ItemMeta meta) {
this.meta = meta;
}
@Override
public void write(int b) throws IOException {
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
}
}

View File

@ -0,0 +1,139 @@
package com.dre.brewery.lore;
/*
* basE91 encoding/decoding routines
*
* Copyright (c) 2000-2006 Joachim Henke
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Joachim Henke nor the names of his contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
public class basE91
{
private int ebq, en, dbq, dn, dv;
public final byte[] enctab;
private final byte[] dectab;
public int encode(byte[] ib, int n, byte[] ob)
{
int i, c = 0;
for (i = 0; i < n; ++i) {
ebq |= (ib[i] & 255) << en;
en += 8;
if (en > 13) {
int ev = ebq & 8191;
if (ev > 88) {
ebq >>= 13;
en -= 13;
} else {
ev = ebq & 16383;
ebq >>= 14;
en -= 14;
}
ob[c++] = enctab[ev % 91];
ob[c++] = enctab[ev / 91];
}
}
return c;
}
public int encEnd(byte[] ob)
{
int c = 0;
if (en > 0) {
ob[c++] = enctab[ebq % 91];
if (en > 7 || ebq > 90)
ob[c++] = enctab[ebq / 91];
}
encReset();
return c;
}
public void encReset()
{
ebq = 0;
en = 0;
}
public int decode(byte[] ib, int n, byte[] ob)
{
int i, c = 0;
for (i = 0; i < n; ++i) {
if (dectab[ib[i]] == -1)
continue;
if (dv == -1)
dv = dectab[ib[i]];
else {
dv += dectab[ib[i]] * 91;
dbq |= dv << dn;
dn += (dv & 8191) > 88 ? 13 : 14;
do {
ob[c++] = (byte) dbq;
dbq >>= 8;
dn -= 8;
} while (dn > 7);
dv = -1;
}
}
return c;
}
public int decEnd(byte[] ob)
{
int c = 0;
if (dv != -1)
ob[c++] = (byte) (dbq | dv << dn);
decReset();
return c;
}
public void decReset()
{
dbq = 0;
dn = 0;
dv = -1;
}
public basE91()
{
int i;
String ts = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!$%&()*+,-./:;<=>?@[\\]^_`{|}~";
enctab = ts.getBytes();
dectab = new byte[256];
for (i = 0; i < 256; ++i)
dectab[i] = -1;
for (i = 0; i < 91; ++i)
dectab[enctab[i]] = (byte) i;
encReset();
decReset();
}
}