forked from LogBlock/LogBlock
cleanup (de)serializing of ItemStacks
This commit is contained in:
@@ -11,6 +11,7 @@ import java.text.ParseException;
|
|||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.logging.Level;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
@@ -20,6 +21,8 @@ import org.bukkit.configuration.InvalidConfigurationException;
|
|||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
import de.diddiz.LogBlock.LogBlock;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
public static String newline = System.getProperty("line.separator");
|
public static String newline = System.getProperty("line.separator");
|
||||||
|
|
||||||
@@ -225,14 +228,10 @@ public class Utils {
|
|||||||
if (data == null || data.length == 0) {
|
if (data == null || data.length == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
YamlConfiguration conf = new YamlConfiguration();
|
|
||||||
try {
|
try {
|
||||||
InputStreamReader reader = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(data)), "UTF-8");
|
return deserializeYamlConfiguration(data).getItemStack("stack");
|
||||||
conf.load(reader);
|
} catch (InvalidConfigurationException e) {
|
||||||
reader.close();
|
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Exception while deserializing ItemStack", e);
|
||||||
return conf.getItemStack("stack");
|
|
||||||
} catch (IOException | InvalidConfigurationException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -241,17 +240,32 @@ public class Utils {
|
|||||||
if (stack == null || BukkitUtils.isEmpty(stack.getType())) {
|
if (stack == null || BukkitUtils.isEmpty(stack.getType())) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
YamlConfiguration conf = new YamlConfiguration();
|
YamlConfiguration conf = new YamlConfiguration();
|
||||||
conf.set("stack", stack);
|
conf.set("stack", stack);
|
||||||
|
return serializeYamlConfiguration(conf);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static YamlConfiguration deserializeYamlConfiguration(byte[] data) throws InvalidConfigurationException {
|
||||||
|
YamlConfiguration conf = new YamlConfiguration();
|
||||||
|
try {
|
||||||
|
InputStreamReader reader = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(data)), "UTF-8");
|
||||||
|
conf.load(reader);
|
||||||
|
reader.close();
|
||||||
|
return conf;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("IOException should be impossible for ByteArrayInputStream", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] serializeYamlConfiguration(YamlConfiguration conf) {
|
||||||
|
try {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(new GZIPOutputStream(baos), "UTF-8");
|
OutputStreamWriter writer = new OutputStreamWriter(new GZIPOutputStream(baos), "UTF-8");
|
||||||
writer.write(conf.saveToString());
|
writer.write(conf.saveToString());
|
||||||
writer.close();
|
writer.close();
|
||||||
return baos.toByteArray();
|
return baos.toByteArray();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
throw new RuntimeException("IOException should be impossible for ByteArrayOutputStream", e);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user