From 1ae8197ea835931d7bebe227583ad55027f6d195 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 30 Sep 2019 12:05:25 -0600 Subject: [PATCH] Compression is scuffed --- .../songoda/skyblock/utils/Compression.java | 42 +++ .../com/songoda/skyblock/utils/GZipUtil.java | 36 -- .../utils/structure/StructureUtil.java | 310 +++++++++--------- 3 files changed, 202 insertions(+), 186 deletions(-) create mode 100644 src/main/java/com/songoda/skyblock/utils/Compression.java delete mode 100644 src/main/java/com/songoda/skyblock/utils/GZipUtil.java diff --git a/src/main/java/com/songoda/skyblock/utils/Compression.java b/src/main/java/com/songoda/skyblock/utils/Compression.java new file mode 100644 index 00000000..53c1027a --- /dev/null +++ b/src/main/java/com/songoda/skyblock/utils/Compression.java @@ -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; + } + +} diff --git a/src/main/java/com/songoda/skyblock/utils/GZipUtil.java b/src/main/java/com/songoda/skyblock/utils/GZipUtil.java deleted file mode 100644 index bccbfcfb..00000000 --- a/src/main/java/com/songoda/skyblock/utils/GZipUtil.java +++ /dev/null @@ -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)); - } -} diff --git a/src/main/java/com/songoda/skyblock/utils/structure/StructureUtil.java b/src/main/java/com/songoda/skyblock/utils/structure/StructureUtil.java index 0f920bf2..d1daaf8a 100644 --- a/src/main/java/com/songoda/skyblock/utils/structure/StructureUtil.java +++ b/src/main/java/com/songoda/skyblock/utils/structure/StructureUtil.java @@ -4,7 +4,7 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.songoda.skyblock.SkyBlock; 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.world.LocationUtil; 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.entity.EntityData; import com.songoda.skyblock.utils.world.entity.EntityUtil; + import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -28,197 +29,206 @@ import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Base64; import java.util.LinkedHashMap; import java.util.List; public final class StructureUtil { - public static void saveStructure(File configFile, org.bukkit.Location originLocation, - org.bukkit.Location[] positions) throws Exception { - if (!configFile.exists()) { - configFile.createNewFile(); - } + public static void saveStructure(File configFile, org.bukkit.Location originLocation, + org.bukkit.Location[] positions) throws Exception { + if (!configFile.exists()) { + configFile.createNewFile(); + } - LinkedHashMap blocks = SelectionLocation.getBlocks(originLocation, positions[0], positions[1]); - LinkedHashMap entities = SelectionLocation.getEntities(originLocation, positions[0], - positions[1]); + LinkedHashMap blocks = SelectionLocation.getBlocks(originLocation, positions[0], positions[1]); + LinkedHashMap entities = SelectionLocation.getEntities(originLocation, positions[0], + positions[1]); - List blockData = new ArrayList<>(); - List entityData = new ArrayList<>(); + List blockData = new ArrayList<>(); + List entityData = new ArrayList<>(); - String originBlockLocation = ""; + String originBlockLocation = ""; - for (Block blockList : blocks.keySet()) { - Location location = blocks.get(blockList); + for (Block blockList : blocks.keySet()) { + Location location = blocks.get(blockList); - if (location.isOriginLocation()) { - originBlockLocation = location.getX() + ":" + location.getY() + ":" + location.getZ() + ":" - + positions[0].getWorld().getName(); + if (location.isOriginLocation()) { + originBlockLocation = location.getX() + ":" + location.getY() + ":" + location.getZ() + ":" + + positions[0].getWorld().getName(); - if (blockList.getType() == Material.AIR) { - blockData.add(BlockUtil.convertBlockToBlockData(blockList, location.getX(), location.getY(), - location.getZ())); - } - } + if (blockList.getType() == Material.AIR) { + blockData.add(BlockUtil.convertBlockToBlockData(blockList, location.getX(), location.getY(), + location.getZ())); + } + } - if (blockList.getType() == Material.AIR) { - continue; - } + if (blockList.getType() == Material.AIR) { + continue; + } - blockData.add( - BlockUtil.convertBlockToBlockData(blockList, location.getX(), location.getY(), location.getZ())); - } + blockData.add( + BlockUtil.convertBlockToBlockData(blockList, location.getX(), location.getY(), location.getZ())); + } - for (Entity entityList : entities.keySet()) { - if (entityList.getType() == EntityType.PLAYER) { - continue; - } + for (Entity entityList : entities.keySet()) { + if (entityList.getType() == EntityType.PLAYER) { + continue; + } - Location location = entities.get(entityList); - entityData.add(EntityUtil.convertEntityToEntityData(entityList, location.getX(), location.getY(), - location.getZ())); - } + Location location = entities.get(entityList); + entityData.add(EntityUtil.convertEntityToEntityData(entityList, location.getX(), location.getY(), + location.getZ())); + } - if (!originBlockLocation.isEmpty()) { - originBlockLocation = originBlockLocation + ":" + originLocation.getYaw() + ":" + originLocation.getPitch(); - } + if (!originBlockLocation.isEmpty()) { + originBlockLocation = originBlockLocation + ":" + originLocation.getYaw() + ":" + originLocation.getPitch(); + } - String JSONString = new Gson().toJson(new Storage(new Gson().toJson(blockData), new Gson().toJson(entityData), - originBlockLocation, System.currentTimeMillis(), NMSUtil.getVersionNumber()), new TypeToken() { - }.getType()); + String JSONString = new Gson().toJson(new Storage(new Gson().toJson(blockData), new Gson().toJson(entityData), + originBlockLocation, System.currentTimeMillis(), NMSUtil.getVersionNumber()), Storage.class); - FileOutputStream fileOutputStream = new FileOutputStream(configFile, false); - fileOutputStream.write(GZipUtil.compress(JSONString.getBytes(StandardCharsets.UTF_8))); - fileOutputStream.flush(); - fileOutputStream.close(); - } + FileOutputStream fileOutputStream = new FileOutputStream(configFile, false); + fileOutputStream.write(Base64.getEncoder().encode(JSONString.getBytes(StandardCharsets.UTF_8))); + fileOutputStream.flush(); + fileOutputStream.close(); + } - public static Structure loadStructure(File configFile) throws IOException { - if (!configFile.exists()) { - return null; - } + public static Structure loadStructure(File configFile) throws IOException { + if (!configFile.exists()) + return null; - byte[] content = new byte[(int) configFile.length()]; + byte[] content = new byte[(int) configFile.length()]; - FileInputStream fileInputStream = new FileInputStream(configFile); - fileInputStream.read(content); - fileInputStream.close(); + FileInputStream fileInputStream = new FileInputStream(configFile); + fileInputStream.read(content); + fileInputStream.close(); - String JSONString = new String(GZipUtil.decompress(content)); - Storage storage = new Gson().fromJson(JSONString, new TypeToken() { - }.getType()); + String json; + try { + 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); + } - return new Structure(storage, configFile.getName()); - } + Storage storage = new Gson().fromJson(json, Storage.class); + return new Structure(storage, configFile.getName()); + } - @SuppressWarnings("unchecked") - public static Float[] pasteStructure(Structure structure, org.bukkit.Location location, BlockDegreesType type) { - Storage storage = structure.getStructureStorage(); + @SuppressWarnings("unchecked") + public static Float[] pasteStructure(Structure structure, org.bukkit.Location location, BlockDegreesType type) { + Storage storage = structure.getStructureStorage(); - String[] originLocationPositions = null; + String[] originLocationPositions = null; - if (!storage.getOriginLocation().isEmpty()) { - originLocationPositions = storage.getOriginLocation().split(":"); - } + if (!storage.getOriginLocation().isEmpty()) { + originLocationPositions = storage.getOriginLocation().split(":"); + } - float yaw = 0.0F, pitch = 0.0F; + float yaw = 0.0F, pitch = 0.0F; - if (originLocationPositions.length == 6) { - yaw = Float.valueOf(originLocationPositions[4]); - pitch = Float.valueOf(originLocationPositions[5]); - } + if (originLocationPositions.length == 6) { + yaw = Float.valueOf(originLocationPositions[4]); + pitch = Float.valueOf(originLocationPositions[5]); + } - List blockData = new Gson().fromJson(storage.getBlocks(), - new TypeToken>() { - }.getType()); + List blockData = new Gson().fromJson(storage.getBlocks(), + new TypeToken>() { + }.getType()); - for (BlockData blockDataList : blockData) { - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> { - try { - org.bukkit.Location blockRotationLocation = LocationUtil - .rotateLocation(new org.bukkit.Location(location.getWorld(), blockDataList.getX(), - blockDataList.getY(), blockDataList.getZ()), type); - org.bukkit.Location blockLocation = new org.bukkit.Location(location.getWorld(), - location.getX() - Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[0])), - location.getY() - Integer.valueOf(storage.getOriginLocation().split(":")[1]), - location.getZ() + Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[2]))); - blockLocation.add(blockRotationLocation); - BlockUtil.convertBlockDataToBlock(blockLocation.getBlock(), blockDataList); - } catch (Exception e) { - SkyBlock.getInstance().getLogger().warning("Unable to convert BlockData to Block for type {" + blockDataList.getMaterial() + - ":" + blockDataList.getData() + "} in structure {" + structure.getStructureFile() + "}"); - } - }); - } + for (BlockData blockDataList : blockData) { + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> { + try { + org.bukkit.Location blockRotationLocation = LocationUtil + .rotateLocation(new org.bukkit.Location(location.getWorld(), blockDataList.getX(), + blockDataList.getY(), blockDataList.getZ()), type); + org.bukkit.Location blockLocation = new org.bukkit.Location(location.getWorld(), + location.getX() - Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[0])), + location.getY() - Integer.valueOf(storage.getOriginLocation().split(":")[1]), + location.getZ() + Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[2]))); + blockLocation.add(blockRotationLocation); + BlockUtil.convertBlockDataToBlock(blockLocation.getBlock(), blockDataList); + } catch (Exception e) { + SkyBlock.getInstance().getLogger().warning("Unable to convert BlockData to Block for type {" + blockDataList.getMaterial() + + ":" + blockDataList.getData() + "} in structure {" + structure.getStructureFile() + "}"); + } + }); + } - Bukkit.getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> { - for (EntityData entityDataList : (List) new Gson().fromJson(storage.getEntities(), - new TypeToken>() { - }.getType())) { - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> { - try { - org.bukkit.Location blockRotationLocation = LocationUtil - .rotateLocation(new org.bukkit.Location(location.getWorld(), entityDataList.getX(), - entityDataList.getY(), entityDataList.getZ()), type); - org.bukkit.Location blockLocation = new org.bukkit.Location(location.getWorld(), - location.getX() - Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[0])), - location.getY() - Integer.valueOf(storage.getOriginLocation().split(":")[1]), - location.getZ() + Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[2]))); - blockLocation.add(blockRotationLocation); - EntityUtil.convertEntityDataToEntity(entityDataList, blockLocation, type); - } catch (Exception e) { - SkyBlock.getInstance().getLogger().warning("Unable to convert EntityData to Entity for type {" + entityDataList.getEntityType() + - "} in structure {" + structure.getStructureFile() + "}"); - } - }); - } - }, 60L); + Bukkit.getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> { + for (EntityData entityDataList : (List) new Gson().fromJson(storage.getEntities(), + new TypeToken>() { + }.getType())) { + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> { + try { + org.bukkit.Location blockRotationLocation = LocationUtil + .rotateLocation(new org.bukkit.Location(location.getWorld(), entityDataList.getX(), + entityDataList.getY(), entityDataList.getZ()), type); + org.bukkit.Location blockLocation = new org.bukkit.Location(location.getWorld(), + location.getX() - Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[0])), + location.getY() - Integer.valueOf(storage.getOriginLocation().split(":")[1]), + location.getZ() + Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[2]))); + blockLocation.add(blockRotationLocation); + EntityUtil.convertEntityDataToEntity(entityDataList, blockLocation, type); + } catch (Exception e) { + SkyBlock.getInstance().getLogger().warning("Unable to convert EntityData to Entity for type {" + entityDataList.getEntityType() + + "} in structure {" + structure.getStructureFile() + "}"); + } + }); + } + }, 60L); - return new Float[]{yaw, pitch}; - } + return new Float[]{yaw, pitch}; + } - public static ItemStack getTool() throws Exception { - SkyBlock skyblock = SkyBlock.getInstance(); + public static ItemStack getTool() throws Exception { + SkyBlock skyblock = SkyBlock.getInstance(); - FileManager fileManager = skyblock.getFileManager(); + FileManager fileManager = skyblock.getFileManager(); - FileManager.Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")); - FileConfiguration configLoad = config.getFileConfiguration(); + FileManager.Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")); + FileConfiguration configLoad = config.getFileConfiguration(); - ItemStack is = new ItemStack( - Material.valueOf(fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")) - .getFileConfiguration().getString("Island.Admin.Structure.Selector"))); - ItemMeta im = is.getItemMeta(); - im.setDisplayName(ChatColor.translateAlternateColorCodes('&', - configLoad.getString("Island.Structure.Tool.Item.Displayname"))); + ItemStack is = new ItemStack( + Material.valueOf(fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")) + .getFileConfiguration().getString("Island.Admin.Structure.Selector"))); + ItemMeta im = is.getItemMeta(); + im.setDisplayName(ChatColor.translateAlternateColorCodes('&', + configLoad.getString("Island.Structure.Tool.Item.Displayname"))); - List itemLore = new ArrayList<>(); + List itemLore = new ArrayList<>(); - for (String itemLoreList : configLoad.getStringList("Island.Structure.Tool.Item.Lore")) { - itemLore.add(ChatColor.translateAlternateColorCodes('&', itemLoreList)); - } + for (String itemLoreList : configLoad.getStringList("Island.Structure.Tool.Item.Lore")) { + itemLore.add(ChatColor.translateAlternateColorCodes('&', itemLoreList)); + } - im.setLore(itemLore); - is.setItemMeta(im); + im.setLore(itemLore); + is.setItemMeta(im); - return is; - } + return is; + } - public static org.bukkit.Location[] getFixedLocations(org.bukkit.Location location1, - org.bukkit.Location location2) { - org.bukkit.Location location1Fixed = location1.clone(); - org.bukkit.Location location2Fixed = location2.clone(); + public static org.bukkit.Location[] getFixedLocations(org.bukkit.Location location1, + org.bukkit.Location location2) { + org.bukkit.Location location1Fixed = location1.clone(); + org.bukkit.Location location2Fixed = location2.clone(); - if (location1.getX() > location2.getX()) { - location1Fixed.setX(location2.getX()); - location2Fixed.setX(location1.getX()); - } + if (location1.getX() > location2.getX()) { + location1Fixed.setX(location2.getX()); + location2Fixed.setX(location1.getX()); + } - if (location1.getZ() < location2.getZ()) { - location1Fixed.setZ(location2.getZ()); - location2Fixed.setZ(location1.getZ()); - } + if (location1.getZ() < location2.getZ()) { + location1Fixed.setZ(location2.getZ()); + location2Fixed.setZ(location1.getZ()); + } - return new org.bukkit.Location[]{location1Fixed, location2Fixed}; - } + return new org.bukkit.Location[]{location1Fixed, location2Fixed}; + } }