diff --git a/src/main/java/world/bentobox/bentobox/managers/BlueprintsManager.java b/src/main/java/world/bentobox/bentobox/managers/BlueprintsManager.java index 28d0f9976..a7f7c6513 100644 --- a/src/main/java/world/bentobox/bentobox/managers/BlueprintsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/BlueprintsManager.java @@ -44,7 +44,6 @@ import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock; import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBundle; import world.bentobox.bentobox.database.json.BentoboxTypeAdapterFactory; import world.bentobox.bentobox.database.objects.Island; -import world.bentobox.bentobox.schems.SchemToBlueprint; import world.bentobox.bentobox.util.Util; /** @@ -186,8 +185,6 @@ public class BlueprintsManager { Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { // Load bundles blueprintBundles.put(addon, new ArrayList<>()); - // See if there are any schems that need converting - new SchemToBlueprint(plugin).convertSchems(addon); if (!loadBundles(addon)) { makeDefaults(addon); loadBundles(addon); diff --git a/src/main/java/world/bentobox/bentobox/schems/Converter.java b/src/main/java/world/bentobox/bentobox/schems/Converter.java deleted file mode 100644 index f4f1a6db8..000000000 --- a/src/main/java/world/bentobox/bentobox/schems/Converter.java +++ /dev/null @@ -1,212 +0,0 @@ -package world.bentobox.bentobox.schems; - -import java.util.AbstractMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Objects; -import java.util.stream.Collectors; - -import org.bukkit.Bukkit; -import org.bukkit.DyeColor; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.Horse; -import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; -import org.eclipse.jdt.annotation.NonNull; - -import world.bentobox.bentobox.blueprints.Blueprint; -import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock; -import world.bentobox.bentobox.blueprints.dataobjects.BlueprintCreatureSpawner; -import world.bentobox.bentobox.blueprints.dataobjects.BlueprintEntity; - -/** - * This class converts a schem to a blueprint - * @author tastybento - * @since 1.5.0 - */ -public class Converter { - - private static final String ATTACHED_YAML_PREFIX = "attached."; - private static final String BEDROCK = "bedrock"; - private static final String BLOCKS_YAML_PREFIX = "blocks."; - private static final String COLOR = "color"; - private static final String ENTITIES_YAML_PREFIX = "entities."; - private static final String INVENTORY = "inventory"; - private static final String LINES = "lines"; - - public Blueprint convert(@NonNull YamlConfiguration bc) { - Blueprint bp = new Blueprint(); - // Bedrock - if (bc.contains(BEDROCK)) { - bp.setBedrock(getVector(bc.getString(BEDROCK))); - } - // Normal blocks - if (bc.isConfigurationSection(BLOCKS_YAML_PREFIX)) { - bp.setBlocks(bc.getConfigurationSection(BLOCKS_YAML_PREFIX).getKeys(false).stream() - // make configuration section from key - .map(k -> bc.getConfigurationSection(BLOCKS_YAML_PREFIX + k)) - // Check the config section contains block data key "bd" - .filter(cs -> cs.contains("bd")) - // Convert it - .map(this::convertBlock) - // Collect into a map - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); - // Legacy entities - Map> le = bc.getConfigurationSection(BLOCKS_YAML_PREFIX).getKeys(false).stream() - // make configuration section from key - .map(k -> bc.getConfigurationSection(BLOCKS_YAML_PREFIX + k)) - // Check the config section contains block data key "entities" - .filter(cs -> cs.contains("entity")) - // Convert it - .map(this::convertLegacyEntity) - // Collect into a map - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); - bp.setEntities(le); - } - // Attached blocks - if (bc.isConfigurationSection(ATTACHED_YAML_PREFIX)) { - bp.setAttached(bc.getConfigurationSection(ATTACHED_YAML_PREFIX).getKeys(false).stream() - // make configuration section from key - .map(k -> bc.getConfigurationSection(ATTACHED_YAML_PREFIX + k)) - // Check the config section contains block data key "bd" - .filter(cs -> cs.contains("bd")) - // Convert it - .map(this::convertBlock) - // Collect into a map - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); - } - // Entities - if (bc.isConfigurationSection(ENTITIES_YAML_PREFIX)) { - bp.setEntities(bc.getConfigurationSection(ENTITIES_YAML_PREFIX).getKeys(false).stream() - // make configuration section from key - .map(k -> bc.getConfigurationSection(ENTITIES_YAML_PREFIX + k)) - // Convert it - .map(this::convertEntity) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); - } - return bp; - - } - - private Entry> convertLegacyEntity(ConfigurationSection config) { - ConfigurationSection en = config.getConfigurationSection("entity"); - // Vector - Vector vector = getVector(config.getName()); - // Create a list of entities at this position - List list = en.getKeys(false).stream() - .map(en::getConfigurationSection) - .map(this::createEntity) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - return new AbstractMap.SimpleEntry<>(vector, list); - - } - - private Entry convertBlock(ConfigurationSection config) { - String blockData = config.getString("bd"); - // Make block - BlueprintBlock block = new BlueprintBlock(blockData); - // Signs - if (config.contains(LINES)) { - block.setSignLines(config.getStringList(LINES)); - } - // Chests, in general - if (config.isConfigurationSection(INVENTORY)) { - ConfigurationSection inv = config.getConfigurationSection(INVENTORY); - block.setInventory( - inv.getKeys(false).stream() - .collect(Collectors.toMap(Integer::valueOf, i -> (ItemStack)inv.get(i))) - ); - } - // Mob spawners - if (blockData.equals("minecraft:spawner")) { - BlueprintCreatureSpawner spawner = new BlueprintCreatureSpawner(); - spawner.setSpawnedType(EntityType.valueOf(config.getString("spawnedType", "PIG"))); - spawner.setMaxNearbyEntities(config.getInt("maxNearbyEntities", 16)); - spawner.setMaxSpawnDelay(config.getInt("maxSpawnDelay", 2*60*20)); - spawner.setMinSpawnDelay(config.getInt("minSpawnDelay", 5*20)); - spawner.setDelay(config.getInt("delay", -1)); - spawner.setRequiredPlayerRange(config.getInt("requiredPlayerRange", 16)); - spawner.setSpawnRange(config.getInt("spawnRange", 4)); - block.setCreatureSpawner(spawner); - } - - // Vector - Vector vector = getVector(config.getName()); - - // Return entry - return new AbstractMap.SimpleEntry<>(vector, block); - - } - - private Entry> convertEntity(ConfigurationSection en) { - // Position - Vector vector = getVector(en.getName()); - // Create a list of entities at this position - List list = en.getKeys(false).stream() - .map(en::getConfigurationSection) - .map(this::createEntity) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - return new AbstractMap.SimpleEntry<>(vector, list); - - } - - - /** - * Try to create a blueprint entity - * @param cs - yaml configuration section - * @return blueprint entity, or null if it fails - */ - private BlueprintEntity createEntity(ConfigurationSection cs) { - try { - BlueprintEntity be = new BlueprintEntity(); - if (cs.contains("type")) { - be.setType(EntityType.valueOf(cs.getString("type"))); - } - if (cs.contains("name")) { - be.setCustomName(cs.getString("name")); - } - if (cs.contains(COLOR)) { - be.setColor(DyeColor.valueOf(cs.getString(COLOR))); - } - if (cs.contains("tamed")) { - be.setTamed(cs.getBoolean("tamed")); - } - if (cs.contains("chest")) { - be.setChest(cs.getBoolean("chest")); - } - if (!cs.getBoolean("adult")) { - be.setAdult(false); - } - if (cs.contains("style")) { - be.setStyle(Horse.Style.valueOf(cs.getString("style", "NONE"))); - } - if (cs.contains("domestication")) { - be.setDomestication(cs.getInt("domestication")); - } - if (cs.isConfigurationSection(INVENTORY)) { - ConfigurationSection inv = cs.getConfigurationSection(INVENTORY); - be.setInventory(inv.getKeys(false).stream() - .collect(Collectors.toMap(Integer::valueOf, i -> (ItemStack)inv.get(i)))); - } - return be; - } catch (Exception e) { - Bukkit.getLogger().severe("Failed to import entity, skipping..."); - } - return null; - } - - private Vector getVector(String name) { - String[] pos = name.split(","); - int x = Integer.parseInt(pos[0]); - int y = Integer.parseInt(pos[1]); - int z = Integer.parseInt(pos[2]); - return new Vector(x,y,z); - } - -} diff --git a/src/main/java/world/bentobox/bentobox/schems/SchemLoader.java b/src/main/java/world/bentobox/bentobox/schems/SchemLoader.java deleted file mode 100644 index 3044eb06d..000000000 --- a/src/main/java/world/bentobox/bentobox/schems/SchemLoader.java +++ /dev/null @@ -1,102 +0,0 @@ -package world.bentobox.bentobox.schems; - -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.YamlConfiguration; - -import world.bentobox.bentobox.BentoBox; - -/** - * This class loads schems so they can be converted to blueprints - * @author tastybento - * @since 1.5.0 - */ -public class SchemLoader { - - private static final String LOAD_ERROR = "Could not load schems file - does not exist : "; - - private YamlConfiguration blockConfig; - - private BentoBox plugin; - - private File schemFolder; - - public SchemLoader(BentoBox plugin, File schemFolder) { - this.plugin = plugin; - this.schemFolder = schemFolder; - blockConfig = new YamlConfiguration(); - } - - /** - * @return the blockConfig - */ - public YamlConfiguration getBlockConfig() { - return blockConfig; - } - - /** - * Load a file to clipboard - * @param fileName - filename in schems folder - * @throws FileNotFoundException - if a file cannot be found - * @throws IOException - if there's a load error with unziping or name - * @throws InvalidConfigurationException - the YAML of the schem is at fault - */ - public void load(String fileName) throws IOException, InvalidConfigurationException { - File zipFile = new File(schemFolder, fileName + ".schem"); - if (!zipFile.exists()) { - plugin.logError(LOAD_ERROR + zipFile.getName()); - throw new FileNotFoundException(LOAD_ERROR + zipFile.getName()); - } - unzip(zipFile.getAbsolutePath()); - File file = new File(schemFolder, fileName); - if (!file.exists()) { - plugin.logError(LOAD_ERROR + file.getName()); - throw new FileNotFoundException(LOAD_ERROR + file.getName()); - } - blockConfig.load(file); - Files.delete(file.toPath()); - } - - private void unzip(final String zipFilePath) throws IOException { - Path path = Paths.get(zipFilePath); - if (!(path.toFile().exists())) { - throw new FileNotFoundException("No file exists to unzip!"); - } - try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath))) { - ZipEntry entry = zipInputStream.getNextEntry(); - while (entry != null) { - Path filePath = Paths.get(path.getParent().toString(), entry.getName()); - if (!entry.isDirectory()) { - unzipFiles(zipInputStream, filePath); - } else { - Files.createDirectories(filePath); - } - - zipInputStream.closeEntry(); - entry = zipInputStream.getNextEntry(); - } - } - } - - private void unzipFiles(final ZipInputStream zipInputStream, final Path unzipFilePath) throws IOException { - try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(unzipFilePath.toAbsolutePath().toString()))) { - byte[] bytesIn = new byte[1024]; - int read; - while ((read = zipInputStream.read(bytesIn)) != -1) { - bos.write(bytesIn, 0, read); - } - } - } - -} diff --git a/src/main/java/world/bentobox/bentobox/schems/SchemToBlueprint.java b/src/main/java/world/bentobox/bentobox/schems/SchemToBlueprint.java deleted file mode 100644 index e3aeaa333..000000000 --- a/src/main/java/world/bentobox/bentobox/schems/SchemToBlueprint.java +++ /dev/null @@ -1,130 +0,0 @@ -package world.bentobox.bentobox.schems; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FilenameFilter; -import java.io.IOException; -import java.nio.file.Files; -import java.util.Arrays; -import java.util.Objects; - -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.World; -import org.eclipse.jdt.annotation.NonNull; - -import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.api.addons.GameModeAddon; -import world.bentobox.bentobox.blueprints.Blueprint; -import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBundle; -import world.bentobox.bentobox.managers.BlueprintsManager; - -public class SchemToBlueprint { - - public static final @NonNull String DEFAULT_SCHEM_NAME = "island"; - public static final @NonNull String FILE_EXTENSION = ".schem"; - public static final @NonNull String FOLDER_NAME = "schems"; - - private BentoBox plugin; - - /** - * @param plugin - plugin - */ - public SchemToBlueprint(BentoBox plugin) { - this.plugin = plugin; - } - - /** - * Converts schems to blueprints and blueprint bundles - * - * @param addon - GameModeAddon - */ - public void convertSchems(GameModeAddon addon) { - File schems = new File(addon.getDataFolder(), FOLDER_NAME); - if (!schems.exists()) { - return; - } - // Convert all schems in folder - // Look through the folder - FilenameFilter schemFilter = (File dir, String name) -> name.toLowerCase(java.util.Locale.ENGLISH).endsWith(FILE_EXTENSION) - && !name.toLowerCase(java.util.Locale.ENGLISH).startsWith("nether-") - && !name.toLowerCase(java.util.Locale.ENGLISH).startsWith("end-"); - - Arrays.stream(Objects.requireNonNull(schems.list(schemFilter))) - .map(name -> name.substring(0, name.length() - FILE_EXTENSION.length())) - .forEach(name -> importSchemSet(addon, schems, name)); - - File newDir = new File(addon.getDataFolder(), FOLDER_NAME + "_converted"); - try { - Files.move(schems.toPath(), newDir.toPath()); - } catch (IOException e) { - plugin.logError("Could not move schems folder: " + e.getLocalizedMessage()); - } - } - - /** - * Imports one schem set to the game mode - * - * @param addon - game mode addon - * @param schems - schems folder - * @param name - name of the schematic - */ - private void importSchemSet(GameModeAddon addon, File schems, String name) { - // Make a new blueprint bundle - BlueprintBundle bb = new BlueprintBundle(); - // TODO: This is just placeholder text - if (name.equalsIgnoreCase(DEFAULT_SCHEM_NAME)) { - bb.setUniqueId(BlueprintsManager.DEFAULT_BUNDLE_NAME); - bb.setDisplayName(ChatColor.YELLOW + "The Original"); - bb.setDescription(ChatColor.AQUA + "Standard set of islands"); - bb.setIcon(Material.GRASS); - } else { - bb.setUniqueId(name); - bb.setDisplayName(name + " island"); - bb.setIcon(Material.GRASS_PATH); - } - Blueprint bp = loadSchemSaveBlueprint(addon, schems, name); - if (bp != null) { - bb.setBlueprint(World.Environment.NORMAL, bp); - plugin.getBlueprintsManager().saveBlueprint(addon, bp); - bb.setDescription(ChatColor.GREEN + "Includes an Overworld island"); - } - bp = loadSchemSaveBlueprint(addon, schems, "nether-" + name); - if (bp != null) { - bb.setBlueprint(World.Environment.NETHER, bp); - plugin.getBlueprintsManager().saveBlueprint(addon, bp); - bb.setDescription(ChatColor.RED + "Includes a Nether island"); - } - bp = loadSchemSaveBlueprint(addon, schems, "end-" + name); - if (bp != null) { - bb.setBlueprint(World.Environment.THE_END, bp); - plugin.getBlueprintsManager().saveBlueprint(addon, bp); - bb.setDescription(ChatColor.GOLD + "Includes an End island"); - } - // Add it to the blueprint manager - plugin.getBlueprintsManager().saveBlueprintBundle(addon, bb); - - // Done! - } - - private Blueprint loadSchemSaveBlueprint(GameModeAddon addon, File schems, String name) { - try { - SchemLoader loader = new SchemLoader(plugin, schems); - loader.load(name); - plugin.log("Loaded " + name + FILE_EXTENSION); - // Convert blueprint - plugin.log("Converting " + name + FILE_EXTENSION + " to a blueprint"); - Blueprint bp = new Converter().convert(loader.getBlockConfig()); - bp.setName(name); - plugin.log("Saving blueprint"); - plugin.getBlueprintsManager().saveBlueprint(addon, bp); - return bp; - } catch (FileNotFoundException ignore) { - // Ignore - } catch (Exception e) { - plugin.logError("Could not convert " + name + " schem, skipping! " + e.getLocalizedMessage()); - } - return null; - } - -}