mirror of
https://github.com/DieReicheErethons/Brewery.git
synced 2025-01-05 18:37:40 +01:00
Optimized LoreStreams
This commit is contained in:
parent
ca2c49ea08
commit
cdd50f8504
@ -653,8 +653,8 @@ public class Brew {
|
||||
}
|
||||
|
||||
public void testStore(DataOutputStream out) throws IOException {
|
||||
out.writeByte(1); // Version
|
||||
out.writeByte(86); // Parity
|
||||
out.writeByte(1); // Version
|
||||
out.writeInt(quality);
|
||||
int bools = 0;
|
||||
bools += (distillRuns != 0 ? 1 : 0);
|
||||
@ -681,14 +681,14 @@ public class Brew {
|
||||
}
|
||||
|
||||
public void testLoad(DataInputStream in) throws IOException {
|
||||
if (in.readByte() != 1) {
|
||||
P.p.log("unknown version");
|
||||
return;
|
||||
}
|
||||
if (in.readByte() != 86) {
|
||||
P.p.log("parity check failed");
|
||||
return;
|
||||
}
|
||||
if (in.readByte() != 1) {
|
||||
P.p.log("unknown version");
|
||||
return;
|
||||
}
|
||||
if (in.readInt() != quality) {
|
||||
P.p.log("quality wrong");
|
||||
}
|
||||
@ -723,6 +723,7 @@ public class Brew {
|
||||
P.p.log("stat wrong");
|
||||
}
|
||||
ingredients.testLoad(in);
|
||||
P.p.log("load successful");
|
||||
}
|
||||
|
||||
// Saves all data
|
||||
|
@ -13,8 +13,8 @@ import com.dre.brewery.integration.WGBarrelOld;
|
||||
import com.dre.brewery.listeners.*;
|
||||
import com.dre.brewery.lore.Base91DecoderStream;
|
||||
import com.dre.brewery.lore.Base91EncoderStream;
|
||||
import com.dre.brewery.lore.LoreReader;
|
||||
import com.dre.brewery.lore.LoreWriter;
|
||||
import com.dre.brewery.lore.LoreLoadStream;
|
||||
import com.dre.brewery.lore.LoreSaveStream;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -93,7 +93,7 @@ public class P extends JavaPlugin {
|
||||
|
||||
try {
|
||||
ItemMeta meta = new ItemStack(Material.POTION).getItemMeta();
|
||||
DataOutputStream data = new DataOutputStream(new Base91EncoderStream(new LoreWriter(meta, 3)));
|
||||
DataOutputStream data = new DataOutputStream(new Base91EncoderStream(new LoreSaveStream(meta, 3)));
|
||||
|
||||
data.writeInt(2);
|
||||
data.writeLong(5);
|
||||
@ -111,7 +111,7 @@ public class P extends JavaPlugin {
|
||||
data.close();
|
||||
meta.getLore();
|
||||
|
||||
DataInputStream dataIn = new DataInputStream(new Base91DecoderStream(new LoreReader(meta)));
|
||||
DataInputStream dataIn = new DataInputStream(new Base91DecoderStream(new LoreLoadStream(meta)));
|
||||
|
||||
P.p.log(dataIn.readInt() + ", " + dataIn.readLong() + ", ");
|
||||
|
||||
|
@ -9,8 +9,8 @@ import com.dre.brewery.P;
|
||||
import com.dre.brewery.integration.LogBlockBarrel;
|
||||
import com.dre.brewery.lore.Base91DecoderStream;
|
||||
import com.dre.brewery.lore.Base91EncoderStream;
|
||||
import com.dre.brewery.lore.LoreReader;
|
||||
import com.dre.brewery.lore.LoreWriter;
|
||||
import com.dre.brewery.lore.LoreLoadStream;
|
||||
import com.dre.brewery.lore.LoreSaveStream;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
@ -308,7 +308,7 @@ public class InventoryListener implements Listener {
|
||||
brew.touch();
|
||||
|
||||
try {
|
||||
DataInputStream in = new DataInputStream(new Base91DecoderStream(new LoreReader(potion)));
|
||||
DataInputStream in = new DataInputStream(new Base91DecoderStream(new LoreLoadStream(potion)));
|
||||
|
||||
brew.testLoad(in);
|
||||
|
||||
@ -329,12 +329,12 @@ public class InventoryListener implements Listener {
|
||||
}*/
|
||||
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalArgumentException argExc) {
|
||||
P.p.log("No Data in Lore");
|
||||
|
||||
try {
|
||||
|
||||
DataOutputStream out = new DataOutputStream(new Base91EncoderStream(new LoreWriter(potion, 2)));
|
||||
DataOutputStream out = new DataOutputStream(new Base91EncoderStream(new LoreSaveStream(potion, 2)));
|
||||
|
||||
brew.testStore(out);
|
||||
|
||||
@ -365,6 +365,8 @@ public class InventoryListener implements Listener {
|
||||
h.printStackTrace();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
50
src/com/dre/brewery/lore/LoreLoadStream.java
Normal file
50
src/com/dre/brewery/lore/LoreLoadStream.java
Normal file
@ -0,0 +1,50 @@
|
||||
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 LoreLoadStream extends ByteArrayInputStream {
|
||||
|
||||
public LoreLoadStream(ItemMeta meta) throws IllegalArgumentException {
|
||||
this(meta, -1);
|
||||
}
|
||||
|
||||
public LoreLoadStream(ItemMeta meta, int line) throws IllegalArgumentException {
|
||||
super(loreToBytes(meta, line));
|
||||
}
|
||||
|
||||
private static byte[] loreToBytes(ItemMeta meta, int lineNum) throws IllegalArgumentException {
|
||||
if (meta.hasLore()) {
|
||||
List<String> lore = meta.getLore();
|
||||
if (lineNum >= 0) {
|
||||
String line = lore.get(lineNum);
|
||||
if (line.startsWith("§%")) {
|
||||
return loreLineToBytes(line);
|
||||
}
|
||||
}
|
||||
for (String line : lore) {
|
||||
if (line.startsWith("§%")) {
|
||||
return loreLineToBytes(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Meta has no data in lore");
|
||||
}
|
||||
|
||||
private static byte[] loreLineToBytes(String line) {
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
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");
|
||||
}
|
||||
}
|
@ -5,15 +5,20 @@ import org.bukkit.inventory.meta.ItemMeta;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class LoreWriter extends ByteArrayOutputStream {
|
||||
public class LoreSaveStream extends ByteArrayOutputStream {
|
||||
|
||||
private ItemMeta meta;
|
||||
private int line;
|
||||
private boolean flushed = false;
|
||||
|
||||
public LoreWriter(ItemMeta meta, int line) {
|
||||
public LoreSaveStream(ItemMeta meta) {
|
||||
this(meta, -1);
|
||||
}
|
||||
|
||||
public LoreSaveStream(ItemMeta meta, int line) {
|
||||
super(128);
|
||||
this.meta = meta;
|
||||
this.line = line;
|
||||
@ -43,10 +48,24 @@ public class LoreWriter extends ByteArrayOutputStream {
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
int prev = 0;
|
||||
for (Iterator<String> iterator = lore.iterator(); iterator.hasNext(); ) {
|
||||
if (iterator.next().startsWith("§%")) {
|
||||
iterator.remove();
|
||||
break;
|
||||
}
|
||||
prev++;
|
||||
}
|
||||
if (line < 0) {
|
||||
if (prev >= 0) {
|
||||
line = prev;
|
||||
} else {
|
||||
line = lore.size();
|
||||
}
|
||||
}
|
||||
while (lore.size() < line) {
|
||||
lore.add("");
|
||||
}
|
||||
//TODO when existing data string in lore
|
||||
lore.add(line, loreLineBuilder.toString());
|
||||
meta.setLore(lore);
|
||||
}
|
Loading…
Reference in New Issue
Block a user