Added support for loading old Structure files and Added Convert Command

This should fix the stone block issue. 

Its recommended to backup your structure files before converting them 

Use this command to convert a structure file
/is admin structure convert namehere.structure
This commit is contained in:
Hexeption 2020-02-15 19:42:31 +00:00 committed by Brianna
parent ede8c332a4
commit d6cdb061ba
3 changed files with 109 additions and 18 deletions

View File

@ -1,14 +1,21 @@
package com.songoda.skyblock.command.commands.admin; package com.songoda.skyblock.command.commands.admin;
import com.google.gson.Gson;
import com.songoda.skyblock.command.SubCommand; import com.songoda.skyblock.command.SubCommand;
import com.songoda.skyblock.config.FileManager.Config; import com.songoda.skyblock.config.FileManager.Config;
import com.songoda.skyblock.message.MessageManager; import com.songoda.skyblock.message.MessageManager;
import com.songoda.skyblock.playerdata.PlayerData; import com.songoda.skyblock.playerdata.PlayerData;
import com.songoda.skyblock.sound.SoundManager; import com.songoda.skyblock.sound.SoundManager;
import com.songoda.skyblock.utils.ChatComponent; import com.songoda.skyblock.utils.ChatComponent;
import com.songoda.skyblock.utils.Compression;
import com.songoda.skyblock.utils.structure.Storage;
import com.songoda.skyblock.utils.structure.Structure;
import com.songoda.skyblock.utils.structure.StructureUtil; import com.songoda.skyblock.utils.structure.StructureUtil;
import com.songoda.skyblock.utils.version.Sounds; import com.songoda.skyblock.utils.version.Sounds;
import com.songoda.skyblock.utils.world.LocationUtil; import com.songoda.skyblock.utils.world.LocationUtil;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.HoverEvent;
@ -19,7 +26,6 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.util.logging.Level; import java.util.logging.Level;
public class StructureCommand extends SubCommand { public class StructureCommand extends SubCommand {
@ -82,6 +88,22 @@ public class StructureCommand extends SubCommand {
ChatColor.translateAlternateColorCodes('&', configLoad.getString( ChatColor.translateAlternateColorCodes('&', configLoad.getString(
"Command.Island.Admin.Structure.Save.Info.Message"))) "Command.Island.Admin.Structure.Save.Info.Message")))
.create())).getTextComponent()); .create())).getTextComponent());
player.spigot()
.sendMessage(
new ChatComponent(
prefix.replace("%info",
ChatColor.translateAlternateColorCodes('&', configLoad.getString(
"Command.Island.Admin.Structure.Convert.Info.Message")))
+ "/island admin structure convert"
+ suffix.replace("%info", ChatColor.translateAlternateColorCodes(
'&',
configLoad.getString(
"Command.Island.Admin.Structure.Save.Convert.Message"))),
false, null, null,
new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(
ChatColor.translateAlternateColorCodes('&', configLoad.getString(
"Command.Island.Admin.Structure.Convert.Info.Message")))
.create())).getTextComponent());
} else { } else {
messageManager.sendMessage(player, helpLines); messageManager.sendMessage(player, helpLines);
} }
@ -176,6 +198,49 @@ public class StructureCommand extends SubCommand {
soundManager.playSound(player, Sounds.ANVIL_LAND.bukkitSound(), 1.0F, 1.0F); soundManager.playSound(player, Sounds.ANVIL_LAND.bukkitSound(), 1.0F, 1.0F);
} }
} else if (args[0].equalsIgnoreCase("convert")) {
if (args.length == 2) {
File structureFile = new File(new File(skyblock.getDataFolder().toString() + "/structures"), args[1]);
if (!structureFile.exists()) {
messageManager.sendMessage(player,
configLoad.getString("Command.Island.Admin.Structure.Convert.Invalid.Message")
.replace("%name", args[1]));
soundManager.playSound(player, Sounds.ANVIL_LAND.bukkitSound(), 1.0F, 1.0F);
return;
}
byte[] content = new byte[(int) structureFile.length()];
try {
FileInputStream fileInputStream = new FileInputStream(structureFile);
fileInputStream.read(content);
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileOutputStream fileOutputStream = new FileOutputStream(structureFile, false);
fileOutputStream.write(Base64.getEncoder().encode(Compression.decompress(content).getBytes()));
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
messageManager.sendMessage(player,
configLoad.getString("Command.Island.Admin.Structure.Convert.Converted.Failed.Message")
.replace("%name", args[1]));
soundManager.playSound(player, Sounds.ANVIL_LAND.bukkitSound(), 1.0F, 1.0F);
e.printStackTrace();
}
messageManager.sendMessage(player,
configLoad.getString("Command.Island.Admin.Structure.Convert.Converted.Successful.Message")
.replace("%name", args[1]));
soundManager.playSound(player, Sounds.VILLAGER_YES.bukkitSound(), 1.0F, 1.0F);
} else {
messageManager.sendMessage(player,
configLoad.getString("Command.Island.Admin.Structure.Convert.Invalid.Message"));
soundManager.playSound(player, Sounds.ANVIL_LAND.bukkitSound(), 1.0F, 1.0F);
}
return; return;
} }
@ -206,6 +271,7 @@ public class StructureCommand extends SubCommand {
@Override @Override
public String[] getArguments() { public String[] getArguments() {
return new String[]{"tool", "save"}; return new String[]{"tool", "save", "convert"};
} }
} }

View File

@ -6,6 +6,7 @@ import com.google.gson.JsonSyntaxException;
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.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;
@ -14,6 +15,8 @@ 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 java.io.FileInputStream;
import java.util.Base64;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
@ -29,7 +32,6 @@ 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;
import java.util.logging.Level; import java.util.logging.Level;
@ -103,6 +105,21 @@ public final class StructureUtil {
} }
public static Structure loadStructure(File configFile) throws IOException { public static Structure loadStructure(File configFile) throws IOException {
byte[] content = new byte[(int) configFile.length()];
FileInputStream fileInputStream = new FileInputStream(configFile);
fileInputStream.read(content);
fileInputStream.close();
Storage storage;
if (!org.bukkit.craftbukkit.libs.org.apache.commons.codec.binary.Base64.isBase64(content)) {
try {
storage = new Gson().fromJson(Compression.decompress(content), Storage.class);
} catch (JsonSyntaxException e) {
e.printStackTrace();
return null;
}
} else {
String base64 = getBase64String(configFile); String base64 = getBase64String(configFile);
if (base64 == null) { if (base64 == null) {
@ -113,15 +130,13 @@ public final class StructureUtil {
if (base64 == null) { if (base64 == null) {
throw new IllegalArgumentException("Couldn't load the default structure file."); throw new IllegalArgumentException("Couldn't load the default structure file.");
} }
Storage storage;
try { try {
storage = new Gson().fromJson(new String(Base64.getDecoder().decode(base64.getBytes(StandardCharsets.UTF_8))), Storage.class); storage = new Gson().fromJson(new String(Base64.getDecoder().decode(base64.getBytes(StandardCharsets.UTF_8))), Storage.class);
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
}
return new Structure(storage, configFile.getName()); return new Structure(storage, configFile.getName());
} }

View File

@ -226,6 +226,16 @@ Command:
Message: '&bSkyBlock &8| &cError&8: &eBoth selected positions have to be in the same World.' Message: '&bSkyBlock &8| &cError&8: &eBoth selected positions have to be in the same World.'
Info: Info:
Message: '&f&oCreate Structures for Islands.' Message: '&f&oCreate Structures for Islands.'
Convert:
Info:
Message: '&f&oConverts old Structure files.'
Invalid:
Message: '&bSkyBlock &8| &aInfo&8: &eThe Structure with the name ''&d%name&e'' was not found!'
Converted:
Successful:
Message: '&bSkyBlock &8| &aInfo&8: &eThe Structure with the name ''&d%name&e'' has been converted!'
Failed:
Message: '&bSkyBlock &8| &cError&8: &eAn error occurred when trying to convert the Structure.'
SetSize: SetSize:
Set: Set:
Message: '&bSkyBlock &8| &aInfo&8: &eYou have set &d%player''s &eIsland size to &d%size&e.' Message: '&bSkyBlock &8| &aInfo&8: &eYou have set &d%player''s &eIsland size to &d%size&e.'