mirror of
https://github.com/songoda/FabledSkyBlock.git
synced 2024-09-18 18:00:58 +02:00
Compression is scuffed
This commit is contained in:
parent
e27b4f86ea
commit
1ae8197ea8
42
src/main/java/com/songoda/skyblock/utils/Compression.java
Normal file
42
src/main/java/com/songoda/skyblock/utils/Compression.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package com.songoda.skyblock.utils;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.zip.GZIPInputStream;
|
||||||
|
import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
|
public class Compression {
|
||||||
|
|
||||||
|
public static byte[] compress(String data) throws IOException {
|
||||||
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length());
|
||||||
|
GZIPOutputStream gzip = new GZIPOutputStream(bos);
|
||||||
|
gzip.write(data.getBytes());
|
||||||
|
gzip.close();
|
||||||
|
byte[] compressed = bos.toByteArray();
|
||||||
|
bos.close();
|
||||||
|
return compressed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String decompress(byte[] compressed) throws IOException {
|
||||||
|
ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
|
||||||
|
GZIPInputStream gis = new GZIPInputStream(bis);
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while((line = br.readLine()) != null) {
|
||||||
|
sb.append(line);
|
||||||
|
}
|
||||||
|
br.close();
|
||||||
|
gis.close();
|
||||||
|
bis.close();
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isOldCompression(byte[] compressed) {
|
||||||
|
return compressed[0] == (byte) GZIPInputStream.GZIP_MAGIC && compressed[1] == (byte) GZIPInputStream.GZIP_MAGIC >> 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,36 +0,0 @@
|
|||||||
package com.songoda.skyblock.utils;
|
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.zip.GZIPInputStream;
|
|
||||||
import java.util.zip.GZIPOutputStream;
|
|
||||||
|
|
||||||
public final class GZipUtil {
|
|
||||||
|
|
||||||
public static byte[] compress(byte[] data) throws IOException {
|
|
||||||
ByteArrayOutputStream obj = new ByteArrayOutputStream();
|
|
||||||
GZIPOutputStream gzip = new GZIPOutputStream(obj);
|
|
||||||
gzip.write(data);
|
|
||||||
gzip.flush();
|
|
||||||
gzip.close();
|
|
||||||
|
|
||||||
return obj.toByteArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] decompress(final byte[] compressedData) throws IOException {
|
|
||||||
if (isCompressed(compressedData)) {
|
|
||||||
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressedData));
|
|
||||||
return IOUtils.toByteArray(gis);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new byte[512];
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isCompressed(final byte[] compressedData) {
|
|
||||||
return (compressedData[0] == (byte) (GZIPInputStream.GZIP_MAGIC))
|
|
||||||
&& (compressedData[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
|
|
||||||
}
|
|
||||||
}
|
|
@ -4,7 +4,7 @@ import com.google.gson.Gson;
|
|||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import com.songoda.skyblock.SkyBlock;
|
import com.songoda.skyblock.SkyBlock;
|
||||||
import com.songoda.skyblock.config.FileManager;
|
import com.songoda.skyblock.config.FileManager;
|
||||||
import com.songoda.skyblock.utils.GZipUtil;
|
import com.songoda.skyblock.utils.Compression;
|
||||||
import com.songoda.skyblock.utils.version.NMSUtil;
|
import com.songoda.skyblock.utils.version.NMSUtil;
|
||||||
import com.songoda.skyblock.utils.world.LocationUtil;
|
import com.songoda.skyblock.utils.world.LocationUtil;
|
||||||
import com.songoda.skyblock.utils.world.block.BlockData;
|
import com.songoda.skyblock.utils.world.block.BlockData;
|
||||||
@ -12,6 +12,7 @@ import com.songoda.skyblock.utils.world.block.BlockDegreesType;
|
|||||||
import com.songoda.skyblock.utils.world.block.BlockUtil;
|
import com.songoda.skyblock.utils.world.block.BlockUtil;
|
||||||
import com.songoda.skyblock.utils.world.entity.EntityData;
|
import com.songoda.skyblock.utils.world.entity.EntityData;
|
||||||
import com.songoda.skyblock.utils.world.entity.EntityUtil;
|
import com.songoda.skyblock.utils.world.entity.EntityUtil;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
@ -28,6 +29,7 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Base64;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -84,19 +86,17 @@ public final class StructureUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String JSONString = new Gson().toJson(new Storage(new Gson().toJson(blockData), new Gson().toJson(entityData),
|
String JSONString = new Gson().toJson(new Storage(new Gson().toJson(blockData), new Gson().toJson(entityData),
|
||||||
originBlockLocation, System.currentTimeMillis(), NMSUtil.getVersionNumber()), new TypeToken<Storage>() {
|
originBlockLocation, System.currentTimeMillis(), NMSUtil.getVersionNumber()), Storage.class);
|
||||||
}.getType());
|
|
||||||
|
|
||||||
FileOutputStream fileOutputStream = new FileOutputStream(configFile, false);
|
FileOutputStream fileOutputStream = new FileOutputStream(configFile, false);
|
||||||
fileOutputStream.write(GZipUtil.compress(JSONString.getBytes(StandardCharsets.UTF_8)));
|
fileOutputStream.write(Base64.getEncoder().encode(JSONString.getBytes(StandardCharsets.UTF_8)));
|
||||||
fileOutputStream.flush();
|
fileOutputStream.flush();
|
||||||
fileOutputStream.close();
|
fileOutputStream.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Structure loadStructure(File configFile) throws IOException {
|
public static Structure loadStructure(File configFile) throws IOException {
|
||||||
if (!configFile.exists()) {
|
if (!configFile.exists())
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
byte[] content = new byte[(int) configFile.length()];
|
byte[] content = new byte[(int) configFile.length()];
|
||||||
|
|
||||||
@ -104,10 +104,20 @@ public final class StructureUtil {
|
|||||||
fileInputStream.read(content);
|
fileInputStream.read(content);
|
||||||
fileInputStream.close();
|
fileInputStream.close();
|
||||||
|
|
||||||
String JSONString = new String(GZipUtil.decompress(content));
|
String json;
|
||||||
Storage storage = new Gson().fromJson(JSONString, new TypeToken<Storage>() {
|
try {
|
||||||
}.getType());
|
json = Compression.decompress(content);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Bukkit.getConsoleSender().sendMessage("Could not load structure '" + configFile.getName() + "' Try using the '/is admin structure tool' command to make a new schematic of it.");
|
||||||
|
File defaultStructure = new File(SkyBlock.getInstance().getDataFolder() + "/" + "structures", "default.structure");
|
||||||
|
content = new byte[(int) defaultStructure.length()];
|
||||||
|
fileInputStream = new FileInputStream(defaultStructure);
|
||||||
|
fileInputStream.read(content);
|
||||||
|
fileInputStream.close();
|
||||||
|
json = Compression.decompress(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
Storage storage = new Gson().fromJson(json, Storage.class);
|
||||||
return new Structure(storage, configFile.getName());
|
return new Structure(storage, configFile.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user