mirror of
https://github.com/songoda/FabledSkyBlock.git
synced 2024-09-03 18:40:35 +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.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<Block, Location> blocks = SelectionLocation.getBlocks(originLocation, positions[0], positions[1]);
|
||||
LinkedHashMap<Entity, Location> entities = SelectionLocation.getEntities(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],
|
||||
positions[1]);
|
||||
|
||||
List<BlockData> blockData = new ArrayList<>();
|
||||
List<EntityData> entityData = new ArrayList<>();
|
||||
List<BlockData> blockData = new ArrayList<>();
|
||||
List<EntityData> 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<Storage>() {
|
||||
}.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<Storage>() {
|
||||
}.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> blockData = new Gson().fromJson(storage.getBlocks(),
|
||||
new TypeToken<List<BlockData>>() {
|
||||
}.getType());
|
||||
List<BlockData> blockData = new Gson().fromJson(storage.getBlocks(),
|
||||
new TypeToken<List<BlockData>>() {
|
||||
}.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<EntityData>) new Gson().fromJson(storage.getEntities(),
|
||||
new TypeToken<List<EntityData>>() {
|
||||
}.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<EntityData>) new Gson().fromJson(storage.getEntities(),
|
||||
new TypeToken<List<EntityData>>() {
|
||||
}.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<String> itemLore = new ArrayList<>();
|
||||
List<String> 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};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user