Compression is scuffed

This commit is contained in:
BuildTools 2019-09-30 12:05:25 -06:00
parent e27b4f86ea
commit 1ae8197ea8
3 changed files with 202 additions and 186 deletions

View 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;
}
}

View File

@ -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));
}
}

View File

@ -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,197 +29,206 @@ 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;
public final class StructureUtil { public final class StructureUtil {
public static void saveStructure(File configFile, org.bukkit.Location originLocation, public static void saveStructure(File configFile, org.bukkit.Location originLocation,
org.bukkit.Location[] positions) throws Exception { org.bukkit.Location[] positions) throws Exception {
if (!configFile.exists()) { if (!configFile.exists()) {
configFile.createNewFile(); configFile.createNewFile();
} }
LinkedHashMap<Block, Location> blocks = SelectionLocation.getBlocks(originLocation, positions[0], positions[1]); LinkedHashMap<Block, Location> blocks = SelectionLocation.getBlocks(originLocation, positions[0], positions[1]);
LinkedHashMap<Entity, Location> entities = SelectionLocation.getEntities(originLocation, positions[0], LinkedHashMap<Entity, Location> entities = SelectionLocation.getEntities(originLocation, positions[0],
positions[1]); positions[1]);
List<BlockData> blockData = new ArrayList<>(); List<BlockData> blockData = new ArrayList<>();
List<EntityData> entityData = new ArrayList<>(); List<EntityData> entityData = new ArrayList<>();
String originBlockLocation = ""; String originBlockLocation = "";
for (Block blockList : blocks.keySet()) { for (Block blockList : blocks.keySet()) {
Location location = blocks.get(blockList); Location location = blocks.get(blockList);
if (location.isOriginLocation()) { if (location.isOriginLocation()) {
originBlockLocation = location.getX() + ":" + location.getY() + ":" + location.getZ() + ":" originBlockLocation = location.getX() + ":" + location.getY() + ":" + location.getZ() + ":"
+ positions[0].getWorld().getName(); + positions[0].getWorld().getName();
if (blockList.getType() == Material.AIR) { if (blockList.getType() == Material.AIR) {
blockData.add(BlockUtil.convertBlockToBlockData(blockList, location.getX(), location.getY(), blockData.add(BlockUtil.convertBlockToBlockData(blockList, location.getX(), location.getY(),
location.getZ())); location.getZ()));
} }
} }
if (blockList.getType() == Material.AIR) { if (blockList.getType() == Material.AIR) {
continue; continue;
} }
blockData.add( blockData.add(
BlockUtil.convertBlockToBlockData(blockList, location.getX(), location.getY(), location.getZ())); BlockUtil.convertBlockToBlockData(blockList, location.getX(), location.getY(), location.getZ()));
} }
for (Entity entityList : entities.keySet()) { for (Entity entityList : entities.keySet()) {
if (entityList.getType() == EntityType.PLAYER) { if (entityList.getType() == EntityType.PLAYER) {
continue; continue;
} }
Location location = entities.get(entityList); Location location = entities.get(entityList);
entityData.add(EntityUtil.convertEntityToEntityData(entityList, location.getX(), location.getY(), entityData.add(EntityUtil.convertEntityToEntityData(entityList, location.getX(), location.getY(),
location.getZ())); location.getZ()));
} }
if (!originBlockLocation.isEmpty()) { if (!originBlockLocation.isEmpty()) {
originBlockLocation = originBlockLocation + ":" + originLocation.getYaw() + ":" + originLocation.getPitch(); originBlockLocation = originBlockLocation + ":" + originLocation.getYaw() + ":" + originLocation.getPitch();
} }
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()];
FileInputStream fileInputStream = new FileInputStream(configFile); FileInputStream fileInputStream = new FileInputStream(configFile);
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);
}
return new Structure(storage, configFile.getName()); Storage storage = new Gson().fromJson(json, Storage.class);
} return new Structure(storage, configFile.getName());
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Float[] pasteStructure(Structure structure, org.bukkit.Location location, BlockDegreesType type) { public static Float[] pasteStructure(Structure structure, org.bukkit.Location location, BlockDegreesType type) {
Storage storage = structure.getStructureStorage(); Storage storage = structure.getStructureStorage();
String[] originLocationPositions = null; String[] originLocationPositions = null;
if (!storage.getOriginLocation().isEmpty()) { if (!storage.getOriginLocation().isEmpty()) {
originLocationPositions = storage.getOriginLocation().split(":"); originLocationPositions = storage.getOriginLocation().split(":");
} }
float yaw = 0.0F, pitch = 0.0F; float yaw = 0.0F, pitch = 0.0F;
if (originLocationPositions.length == 6) { if (originLocationPositions.length == 6) {
yaw = Float.valueOf(originLocationPositions[4]); yaw = Float.valueOf(originLocationPositions[4]);
pitch = Float.valueOf(originLocationPositions[5]); pitch = Float.valueOf(originLocationPositions[5]);
} }
List<BlockData> blockData = new Gson().fromJson(storage.getBlocks(), List<BlockData> blockData = new Gson().fromJson(storage.getBlocks(),
new TypeToken<List<BlockData>>() { new TypeToken<List<BlockData>>() {
}.getType()); }.getType());
for (BlockData blockDataList : blockData) { for (BlockData blockDataList : blockData) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> { Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> {
try { try {
org.bukkit.Location blockRotationLocation = LocationUtil org.bukkit.Location blockRotationLocation = LocationUtil
.rotateLocation(new org.bukkit.Location(location.getWorld(), blockDataList.getX(), .rotateLocation(new org.bukkit.Location(location.getWorld(), blockDataList.getX(),
blockDataList.getY(), blockDataList.getZ()), type); blockDataList.getY(), blockDataList.getZ()), type);
org.bukkit.Location blockLocation = new org.bukkit.Location(location.getWorld(), org.bukkit.Location blockLocation = new org.bukkit.Location(location.getWorld(),
location.getX() - Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[0])), location.getX() - Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[0])),
location.getY() - Integer.valueOf(storage.getOriginLocation().split(":")[1]), location.getY() - Integer.valueOf(storage.getOriginLocation().split(":")[1]),
location.getZ() + Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[2]))); location.getZ() + Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[2])));
blockLocation.add(blockRotationLocation); blockLocation.add(blockRotationLocation);
BlockUtil.convertBlockDataToBlock(blockLocation.getBlock(), blockDataList); BlockUtil.convertBlockDataToBlock(blockLocation.getBlock(), blockDataList);
} catch (Exception e) { } catch (Exception e) {
SkyBlock.getInstance().getLogger().warning("Unable to convert BlockData to Block for type {" + blockDataList.getMaterial() + SkyBlock.getInstance().getLogger().warning("Unable to convert BlockData to Block for type {" + blockDataList.getMaterial() +
":" + blockDataList.getData() + "} in structure {" + structure.getStructureFile() + "}"); ":" + blockDataList.getData() + "} in structure {" + structure.getStructureFile() + "}");
} }
}); });
} }
Bukkit.getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> { Bukkit.getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> {
for (EntityData entityDataList : (List<EntityData>) new Gson().fromJson(storage.getEntities(), for (EntityData entityDataList : (List<EntityData>) new Gson().fromJson(storage.getEntities(),
new TypeToken<List<EntityData>>() { new TypeToken<List<EntityData>>() {
}.getType())) { }.getType())) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> { Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SkyBlock.getInstance(), () -> {
try { try {
org.bukkit.Location blockRotationLocation = LocationUtil org.bukkit.Location blockRotationLocation = LocationUtil
.rotateLocation(new org.bukkit.Location(location.getWorld(), entityDataList.getX(), .rotateLocation(new org.bukkit.Location(location.getWorld(), entityDataList.getX(),
entityDataList.getY(), entityDataList.getZ()), type); entityDataList.getY(), entityDataList.getZ()), type);
org.bukkit.Location blockLocation = new org.bukkit.Location(location.getWorld(), org.bukkit.Location blockLocation = new org.bukkit.Location(location.getWorld(),
location.getX() - Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[0])), location.getX() - Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[0])),
location.getY() - Integer.valueOf(storage.getOriginLocation().split(":")[1]), location.getY() - Integer.valueOf(storage.getOriginLocation().split(":")[1]),
location.getZ() + Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[2]))); location.getZ() + Math.abs(Integer.valueOf(storage.getOriginLocation().split(":")[2])));
blockLocation.add(blockRotationLocation); blockLocation.add(blockRotationLocation);
EntityUtil.convertEntityDataToEntity(entityDataList, blockLocation, type); EntityUtil.convertEntityDataToEntity(entityDataList, blockLocation, type);
} catch (Exception e) { } catch (Exception e) {
SkyBlock.getInstance().getLogger().warning("Unable to convert EntityData to Entity for type {" + entityDataList.getEntityType() + SkyBlock.getInstance().getLogger().warning("Unable to convert EntityData to Entity for type {" + entityDataList.getEntityType() +
"} in structure {" + structure.getStructureFile() + "}"); "} in structure {" + structure.getStructureFile() + "}");
} }
}); });
} }
}, 60L); }, 60L);
return new Float[]{yaw, pitch}; return new Float[]{yaw, pitch};
} }
public static ItemStack getTool() throws Exception { public static ItemStack getTool() throws Exception {
SkyBlock skyblock = SkyBlock.getInstance(); SkyBlock skyblock = SkyBlock.getInstance();
FileManager fileManager = skyblock.getFileManager(); FileManager fileManager = skyblock.getFileManager();
FileManager.Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")); FileManager.Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml"));
FileConfiguration configLoad = config.getFileConfiguration(); FileConfiguration configLoad = config.getFileConfiguration();
ItemStack is = new ItemStack( ItemStack is = new ItemStack(
Material.valueOf(fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")) Material.valueOf(fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml"))
.getFileConfiguration().getString("Island.Admin.Structure.Selector"))); .getFileConfiguration().getString("Island.Admin.Structure.Selector")));
ItemMeta im = is.getItemMeta(); ItemMeta im = is.getItemMeta();
im.setDisplayName(ChatColor.translateAlternateColorCodes('&', im.setDisplayName(ChatColor.translateAlternateColorCodes('&',
configLoad.getString("Island.Structure.Tool.Item.Displayname"))); configLoad.getString("Island.Structure.Tool.Item.Displayname")));
List<String> itemLore = new ArrayList<>(); List<String> itemLore = new ArrayList<>();
for (String itemLoreList : configLoad.getStringList("Island.Structure.Tool.Item.Lore")) { for (String itemLoreList : configLoad.getStringList("Island.Structure.Tool.Item.Lore")) {
itemLore.add(ChatColor.translateAlternateColorCodes('&', itemLoreList)); itemLore.add(ChatColor.translateAlternateColorCodes('&', itemLoreList));
} }
im.setLore(itemLore); im.setLore(itemLore);
is.setItemMeta(im); is.setItemMeta(im);
return is; return is;
} }
public static org.bukkit.Location[] getFixedLocations(org.bukkit.Location location1, public static org.bukkit.Location[] getFixedLocations(org.bukkit.Location location1,
org.bukkit.Location location2) { org.bukkit.Location location2) {
org.bukkit.Location location1Fixed = location1.clone(); org.bukkit.Location location1Fixed = location1.clone();
org.bukkit.Location location2Fixed = location2.clone(); org.bukkit.Location location2Fixed = location2.clone();
if (location1.getX() > location2.getX()) { if (location1.getX() > location2.getX()) {
location1Fixed.setX(location2.getX()); location1Fixed.setX(location2.getX());
location2Fixed.setX(location1.getX()); location2Fixed.setX(location1.getX());
} }
if (location1.getZ() < location2.getZ()) { if (location1.getZ() < location2.getZ()) {
location1Fixed.setZ(location2.getZ()); location1Fixed.setZ(location2.getZ());
location2Fixed.setZ(location1.getZ()); location2Fixed.setZ(location1.getZ());
} }
return new org.bukkit.Location[]{location1Fixed, location2Fixed}; return new org.bukkit.Location[]{location1Fixed, location2Fixed};
} }
} }