Search in sources :

Example 1 with OperationCancelled

use of org.pepsoft.util.ProgressReceiver.OperationCancelled in project WorldPainter by Captain-Chaos.

the class Export method main.

public static void main(String[] args) throws IOException, ClassNotFoundException, OperationCancelled, CertificateException {
    // Logger rootLogger = Logger.getLogger("");
    // rootLogger.setLevel(Level.OFF);
    // Load or initialise configuration
    // This will migrate the configuration directory if necessary
    Configuration config = Configuration.load();
    if (config == null) {
        System.out.println("Creating new configuration");
        config = new Configuration();
    }
    Configuration.setInstance(config);
    System.out.println("Installation ID: " + config.getUuid());
    // Load trusted WorldPainter root certificate
    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    X509Certificate trustedCert = (X509Certificate) certificateFactory.generateCertificate(ClassLoader.getSystemResourceAsStream("wproot.pem"));
    // Load the plugins
    File pluginsDir = new File(Configuration.getConfigDir(), "plugins");
    if (pluginsDir.isDirectory()) {
        PluginManager.loadPlugins(pluginsDir, trustedCert.getPublicKey());
    }
    WPPluginManager.initialise(config.getUuid());
    File worldFile = new File(args[0]);
    System.out.println("Loading " + worldFile);
    World2 world;
    try (ObjectInputStream in = new ObjectInputStream(new GZIPInputStream(new FileInputStream(worldFile)))) {
        world = (World2) in.readObject();
    }
    for (int i = 0; i < Terrain.CUSTOM_TERRAIN_COUNT; i++) {
        MixedMaterial material = world.getMixedMaterial(i);
        Terrain.setCustomMaterial(i, material);
    }
    if (world.getPlatform() == null) {
        if (world.getMaxHeight() == Constants.DEFAULT_MAX_HEIGHT_2) {
            world.setPlatform(DefaultPlugin.JAVA_ANVIL);
        } else {
            world.setPlatform(DefaultPlugin.JAVA_MCREGION);
        }
    }
    File exportDir;
    if (args.length > 1) {
        exportDir = new File(args[1]);
    } else {
        File minecraftDir = MinecraftUtil.findMinecraftDir();
        exportDir = new File(minecraftDir, "saves");
    }
    System.out.println("Exporting to " + exportDir);
    System.out.println("+---------+---------+---------+---------+---------+");
    JavaWorldExporter exporter = new JavaWorldExporter(world);
    exporter.export(exportDir, world.getName(), exporter.selectBackupDir(new File(exportDir, FileUtils.sanitiseName(world.getName()))), new ProgressReceiver() {

        @Override
        public void setProgress(float progressFraction) throws OperationCancelled {
            int progress = (int) (progressFraction * 50);
            while (progress > previousProgress) {
                System.out.print('.');
                previousProgress++;
            }
        }

        @Override
        public void exceptionThrown(Throwable exception) {
            exception.printStackTrace();
            System.exit(1);
        }

        @Override
        public void reset() {
            System.out.println();
            previousProgress = -1;
        }

        @Override
        public void done() {
        }

        @Override
        public void setMessage(String message) throws OperationCancelled {
        }

        @Override
        public void checkForCancellation() throws OperationCancelled {
        }

        @Override
        public void subProgressStarted(SubProgressReceiver subProgressReceiver) throws OperationCancelled {
        }

        private int previousProgress = -1;
    });
    System.out.println();
    System.out.println("World " + world.getName() + " exported successfully");
}
Also used : JavaWorldExporter(org.pepsoft.worldpainter.exporting.JavaWorldExporter) OperationCancelled(org.pepsoft.util.ProgressReceiver.OperationCancelled) SubProgressReceiver(org.pepsoft.util.SubProgressReceiver) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) SubProgressReceiver(org.pepsoft.util.SubProgressReceiver) ProgressReceiver(org.pepsoft.util.ProgressReceiver) File(java.io.File) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with OperationCancelled

use of org.pepsoft.util.ProgressReceiver.OperationCancelled in project WorldPainter by Captain-Chaos.

the class FillDialog method clearLayer.

private void clearLayer(ProgressReceiver progressReceiver) throws OperationCancelled {
    Layer layer = (Layer) comboBoxClearLayer.getSelectedItem();
    if (filter == null) {
        dimension.clearLayerData(layer);
    } else {
        if (layer.getDataSize() == Layer.DataSize.NIBBLE) {
            dimension.visitTiles().forFilter(filter).andDo(tile -> {
                final int worldTileX = tile.getX() << TILE_SIZE_BITS;
                final int worldTileY = tile.getY() << TILE_SIZE_BITS;
                for (int x = 0; x < TILE_SIZE; x++) {
                    for (int y = 0; y < TILE_SIZE; y++) {
                        int oldLayervalue = tile.getLayerValue(layer, x, y);
                        int layerValue;
                        if (filter == null) {
                            layerValue = 0;
                        } else {
                            layerValue = Math.min(oldLayervalue, 15 - (int) (filter.modifyStrength(worldTileX | x, worldTileY | y, 1.0f) * 15));
                        }
                        if (oldLayervalue != layerValue) {
                            tile.setLayerValue(layer, x, y, layerValue);
                        }
                    }
                }
            }, progressReceiver);
        } else if (layer.getDataSize() == Layer.DataSize.BIT) {
            dimension.visitTiles().forFilter(filter).andDo(tile -> {
                final int worldTileX = tile.getX() << TILE_SIZE_BITS;
                final int worldTileY = tile.getY() << TILE_SIZE_BITS;
                for (int x = 0; x < TILE_SIZE; x++) {
                    for (int y = 0; y < TILE_SIZE; y++) {
                        boolean set;
                        if (filter == null) {
                            set = true;
                        } else {
                            float strength = filter.modifyStrength(worldTileX | x, worldTileY | y, 1.0f);
                            set = (strength > 0.95f) || (Math.random() < strength);
                        }
                        if (set && tile.getBitLayerValue(layer, x, y)) {
                            tile.setBitLayerValue(layer, x, y, false);
                        }
                    }
                }
            }, progressReceiver);
        } else if (layer.getDataSize() == Layer.DataSize.BIT_PER_CHUNK) {
            dimension.visitTiles().forFilter(filter).andDo(tile -> {
                final int worldTileX = tile.getX() << TILE_SIZE_BITS;
                final int worldTileY = tile.getY() << TILE_SIZE_BITS;
                for (int x = 0; x < TILE_SIZE; x += 16) {
                    for (int y = 0; y < TILE_SIZE; y += 16) {
                        boolean set;
                        if (filter == null) {
                            set = true;
                        } else {
                            float strength = filter.modifyStrength(worldTileX | x, worldTileY | y, 1.0f);
                            set = (strength > 0.95f) || (Math.random() < strength);
                        }
                        if (set && tile.getBitLayerValue(layer, x, y)) {
                            tile.setBitLayerValue(layer, x, y, false);
                        }
                    }
                }
            }, progressReceiver);
        } else {
            throw new UnsupportedOperationException();
        }
    }
}
Also used : CustomBiomeManager(org.pepsoft.worldpainter.biomeschemes.CustomBiomeManager) BiomeHelper(org.pepsoft.worldpainter.biomeschemes.BiomeHelper) Arrays(java.util.Arrays) AutoBiomeScheme(org.pepsoft.worldpainter.biomeschemes.AutoBiomeScheme) Collection(java.util.Collection) Biome(org.pepsoft.worldpainter.layers.Biome) ProgressDialog(org.pepsoft.util.swing.ProgressDialog) Listener(org.pepsoft.worldpainter.panels.BrushOptions.Listener) Set(java.util.Set) DefaultFilter(org.pepsoft.worldpainter.panels.DefaultFilter) TILE_SIZE(org.pepsoft.worldpainter.Constants.TILE_SIZE) Layer(org.pepsoft.worldpainter.layers.Layer) TerrainListCellRenderer(org.pepsoft.worldpainter.themes.TerrainListCellRenderer) ProgressTask(org.pepsoft.util.swing.ProgressTask) FloodWithLava(org.pepsoft.worldpainter.layers.FloodWithLava) ObservableBoolean(org.pepsoft.util.ObservableBoolean) ProgressReceiver(org.pepsoft.util.ProgressReceiver) SelectionBlock(org.pepsoft.worldpainter.selection.SelectionBlock) OperationCancelled(org.pepsoft.util.ProgressReceiver.OperationCancelled) SelectionChunk(org.pepsoft.worldpainter.selection.SelectionChunk) SelectionHelper(org.pepsoft.worldpainter.selection.SelectionHelper) TILE_SIZE_BITS(org.pepsoft.worldpainter.Constants.TILE_SIZE_BITS) Filter(org.pepsoft.worldpainter.operations.Filter) javax.swing(javax.swing) Layer(org.pepsoft.worldpainter.layers.Layer)

Example 3 with OperationCancelled

use of org.pepsoft.util.ProgressReceiver.OperationCancelled in project WorldPainter by Captain-Chaos.

the class ImportHeightMapDialog method exportToDimension.

private void exportToDimension() {
    if (currentDimension == null) {
        throw new IllegalStateException();
    }
    final HeightMapImporter importer = new HeightMapImporter();
    HeightMap heightMap = new BitmapHeightMap(selectedFile.getName(), image, 0, selectedFile, false, false);
    int scale = (Integer) spinnerScale.getValue();
    int offsetX = (Integer) spinnerOffsetX.getValue();
    int offsetY = (Integer) spinnerOffsetY.getValue();
    if ((scale != 100) || (offsetX != 0) || (offsetY != 0)) {
        ((BitmapHeightMap) heightMap).setSmoothScaling(true);
        heightMap = new TransformingHeightMap(heightMap.getName() + " transformed", heightMap, scale, scale, offsetX, offsetY, 0);
    }
    if (checkBoxInvert.isSelected()) {
        if (image.getSampleModel().getSampleSize(0) == 16) {
            heightMap = new DifferenceHeightMap(new ConstantHeightMap(65535f), heightMap);
        } else {
            heightMap = new DifferenceHeightMap(new ConstantHeightMap(255f), heightMap);
        }
    }
    importer.setHeightMap(heightMap);
    importer.setImageFile(selectedFile);
    String name = selectedFile.getName();
    int p = name.lastIndexOf('.');
    if (p != -1) {
        name = name.substring(0, p);
    }
    importer.setName(name);
    importer.setTileFactory(tileFactory);
    importer.setMaxHeight(Integer.parseInt((String) comboBoxHeight.getSelectedItem()));
    importer.setImageLowLevel((Integer) spinnerImageLow.getValue());
    importer.setImageHighLevel((Integer) spinnerImageHigh.getValue());
    importer.setWorldLowLevel((Integer) spinnerWorldLow.getValue());
    importer.setWorldWaterLevel((Integer) spinnerWorldMiddle.getValue());
    importer.setWorldHighLevel((Integer) spinnerWorldHigh.getValue());
    importer.setVoidBelowLevel(checkBoxVoid.isSelected() ? ((Integer) spinnerVoidBelow.getValue()) : 0);
    importer.setOnlyRaise(checkBoxOnlyRaise.isSelected());
    ProgressDialog.executeTask(this, new ProgressTask<Void>() {

        @Override
        public String getName() {
            return "Importing height map";
        }

        @Override
        public Void execute(ProgressReceiver progressReceiver) throws OperationCancelled {
            importer.importToDimension(currentDimension, checkBoxCreateTiles.isSelected(), progressReceiver);
            return null;
        }
    }, false);
    Configuration.getInstance().setHeightMapsDirectory(selectedFile.getParentFile());
    currentDimension.clearUndo();
    currentDimension.armSavePoint();
}
Also used : HeightMapImporter(org.pepsoft.worldpainter.importing.HeightMapImporter) OperationCancelled(org.pepsoft.util.ProgressReceiver.OperationCancelled) ProgressReceiver(org.pepsoft.util.ProgressReceiver) Void(org.pepsoft.worldpainter.layers.Void)

Example 4 with OperationCancelled

use of org.pepsoft.util.ProgressReceiver.OperationCancelled in project WorldPainter by Captain-Chaos.

the class MapImportDialog method analyseMap.

private void analyseMap() {
    mapStatistics = null;
    resetStats();
    File levelDatFile = new File(fieldFilename.getText());
    final File worldDir = levelDatFile.getParentFile();
    // Check if it's a valid level.dat file before we commit
    int version;
    try {
        Level testLevel = Level.load(levelDatFile);
        version = testLevel.getVersion();
    } catch (IOException e) {
        logger.error("IOException while analysing map " + levelDatFile, e);
        JOptionPane.showMessageDialog(MapImportDialog.this, strings.getString("selected.file.is.not.a.valid.level.dat.file"), strings.getString("invalid.file"), JOptionPane.ERROR_MESSAGE);
        return;
    } catch (IllegalArgumentException e) {
        logger.error("IllegalArgumentException while analysing map " + levelDatFile, e);
        JOptionPane.showMessageDialog(MapImportDialog.this, strings.getString("selected.file.is.not.a.valid.level.dat.file"), strings.getString("invalid.file"), JOptionPane.ERROR_MESSAGE);
        return;
    } catch (NullPointerException e) {
        logger.error("NullPointerException while analysing map " + levelDatFile, e);
        JOptionPane.showMessageDialog(MapImportDialog.this, strings.getString("selected.file.is.not.a.valid.level.dat.file"), strings.getString("invalid.file"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    // Other sanity checks
    if ((version != SUPPORTED_VERSION_1) && (version != SUPPORTED_VERSION_2)) {
        logger.error("Unsupported Minecraft version while analysing map " + levelDatFile);
        JOptionPane.showMessageDialog(MapImportDialog.this, strings.getString("unsupported.minecraft.version"), strings.getString("unsupported.version"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    File regionDir = new File(worldDir, "region");
    if (!regionDir.isDirectory()) {
        logger.error("Region directory missing while analysing map " + levelDatFile);
        JOptionPane.showMessageDialog(MapImportDialog.this, strings.getString("the.region.folder.is.missing"), strings.getString("region.folder.missing"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    final Pattern regionFilePattern = (version == SUPPORTED_VERSION_1) ? Pattern.compile("r\\.-?\\d+\\.-?\\d+\\.mcr") : Pattern.compile("r\\.-?\\d+\\.-?\\d+\\.mca");
    final File[] regionFiles = regionDir.listFiles((dir, name) -> regionFilePattern.matcher(name).matches());
    if ((regionFiles == null) || (regionFiles.length == 0)) {
        logger.error("Region files missing while analysing map " + levelDatFile);
        JOptionPane.showMessageDialog(MapImportDialog.this, strings.getString("the.region.folder.contains.no.region.files"), strings.getString("region.files.missing"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    // Check for Nether and End
    boolean netherPresent = false, endPresent = false;
    File netherRegionDir = new File(worldDir, "DIM-1/region");
    if (netherRegionDir.isDirectory()) {
        File[] netherRegionFiles = netherRegionDir.listFiles((dir, name) -> regionFilePattern.matcher(name).matches());
        if ((netherRegionFiles != null) && (netherRegionFiles.length > 0)) {
            netherPresent = true;
        }
    }
    File endRegionDir = new File(worldDir, "DIM1/region");
    if (endRegionDir.isDirectory()) {
        File[] endRegionFiles = endRegionDir.listFiles((dir, name) -> regionFilePattern.matcher(name).matches());
        if ((endRegionFiles != null) && (endRegionFiles.length > 0)) {
            endPresent = true;
        }
    }
    checkBoxImportNether.setEnabled(netherPresent);
    checkBoxImportNether.setSelected(netherPresent);
    checkBoxImportEnd.setEnabled(endPresent);
    checkBoxImportEnd.setSelected(endPresent);
    mapStatistics = ProgressDialog.executeTask(this, new ProgressTask<MapStatistics>() {

        @Override
        public String getName() {
            return "Analyzing map...";
        }

        @Override
        public MapStatistics execute(ProgressReceiver progressReceiver) throws OperationCancelled {
            MapStatistics stats = new MapStatistics();
            int chunkCount = 0;
            List<Integer> xValues = new ArrayList<>(), zValues = new ArrayList<>();
            List<Point> chunks = new ArrayList<>();
            int count = 0;
            for (File file : regionFiles) {
                String[] nameFrags = file.getName().split("\\.");
                int regionX = Integer.parseInt(nameFrags[1]);
                int regionZ = Integer.parseInt(nameFrags[2]);
                try {
                    RegionFile regionFile = new RegionFile(file);
                    try {
                        for (int x = 0; x < 32; x++) {
                            for (int z = 0; z < 32; z++) {
                                if (regionFile.containsChunk(x, z)) {
                                    chunkCount++;
                                    int chunkX = regionX * 32 + x, chunkZ = regionZ * 32 + z;
                                    if (chunkX < stats.lowestChunkX) {
                                        stats.lowestChunkX = chunkX;
                                    }
                                    if (chunkX > stats.highestChunkX) {
                                        stats.highestChunkX = chunkX;
                                    }
                                    if (chunkZ < stats.lowestChunkZ) {
                                        stats.lowestChunkZ = chunkZ;
                                    }
                                    if (chunkZ > stats.highestChunkZ) {
                                        stats.highestChunkZ = chunkZ;
                                    }
                                    xValues.add(chunkX);
                                    zValues.add(chunkZ);
                                    chunks.add(new Point(chunkX, chunkZ));
                                }
                            }
                        }
                    } finally {
                        regionFile.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException("I/O error while analyzing map " + worldDir, e);
                }
                count++;
                progressReceiver.setProgress((float) count / (regionFiles.length + 1));
            }
            stats.chunkCount = chunkCount;
            if (chunkCount == 0) {
                // Completely empty map (wrong region file format)?
                progressReceiver.setProgress(1.0f);
                return stats;
            }
            Collections.sort(xValues);
            int p1 = xValues.size() / 4;
            float q1 = xValues.get(p1) * 0.75f + xValues.get(p1 + 1) * 0.25f;
            int p2 = xValues.size() / 2;
            float q2 = (xValues.get(p2) + xValues.get(p2 + 1)) / 2f;
            int p3 = xValues.size() * 3 / 4;
            float q3 = xValues.get(p3) * 0.25f + xValues.get(p3 + 1) * 0.75f;
            float iqr = q3 - q1;
            int lowerLimit = (int) (q2 - iqr * 1.5f);
            int upperLimit = (int) (q2 + iqr * 1.5f);
            for (Point chunk : chunks) {
                if ((chunk.x < lowerLimit) || (chunk.x > upperLimit)) {
                    stats.outlyingChunks.add(chunk);
                }
            }
            Collections.sort(zValues);
            p1 = zValues.size() / 4;
            q1 = zValues.get(p1) * 0.75f + zValues.get(p1 + 1) * 0.25f;
            p2 = zValues.size() / 2;
            q2 = (zValues.get(p2) + zValues.get(p2 + 1)) / 2f;
            p3 = zValues.size() * 3 / 4;
            q3 = zValues.get(p3) * 0.25f + zValues.get(p3 + 1) * 0.75f;
            iqr = q3 - q1;
            lowerLimit = (int) (q2 - iqr * 1.5f);
            upperLimit = (int) (q2 + iqr * 1.5f);
            for (Point chunk : chunks) {
                if ((chunk.y < lowerLimit) || (chunk.y > upperLimit)) {
                    stats.outlyingChunks.add(chunk);
                }
            }
            if (!stats.outlyingChunks.isEmpty()) {
                chunks.stream().filter(chunk -> !stats.outlyingChunks.contains(chunk)).forEach(chunk -> {
                    if (chunk.x < stats.lowestChunkXNoOutliers) {
                        stats.lowestChunkXNoOutliers = chunk.x;
                    }
                    if (chunk.x > stats.highestChunkXNoOutliers) {
                        stats.highestChunkXNoOutliers = chunk.x;
                    }
                    if (chunk.y < stats.lowestChunkZNoOutliers) {
                        stats.lowestChunkZNoOutliers = chunk.y;
                    }
                    if (chunk.y > stats.highestChunkZNoOutliers) {
                        stats.highestChunkZNoOutliers = chunk.y;
                    }
                });
            } else {
                stats.lowestChunkXNoOutliers = stats.lowestChunkX;
                stats.highestChunkXNoOutliers = stats.highestChunkX;
                stats.lowestChunkZNoOutliers = stats.lowestChunkZ;
                stats.highestChunkZNoOutliers = stats.highestChunkZ;
            }
            progressReceiver.setProgress(1.0f);
            return stats;
        }
    }, true);
    if ((mapStatistics != null) && (mapStatistics.chunkCount > 0)) {
        int width = mapStatistics.highestChunkXNoOutliers - mapStatistics.lowestChunkXNoOutliers + 1;
        int length = mapStatistics.highestChunkZNoOutliers - mapStatistics.lowestChunkZNoOutliers + 1;
        int area = (mapStatistics.chunkCount - mapStatistics.outlyingChunks.size());
        labelWidth.setText(FORMATTER.format(width * 16) + " blocks (from " + FORMATTER.format(mapStatistics.lowestChunkXNoOutliers << 4) + " to " + FORMATTER.format((mapStatistics.highestChunkXNoOutliers << 4) + 15) + "; " + FORMATTER.format(width) + " chunks)");
        labelLength.setText(FORMATTER.format(length * 16) + " blocks (from " + FORMATTER.format(mapStatistics.lowestChunkZNoOutliers << 4) + " to " + FORMATTER.format((mapStatistics.highestChunkZNoOutliers << 4) + 15) + "; " + FORMATTER.format(length) + " chunks)");
        labelArea.setText(FORMATTER.format(area * 256L) + " blocksĀ² (" + FORMATTER.format(area) + " chunks)");
        if (!mapStatistics.outlyingChunks.isEmpty()) {
            // There are outlying chunks
            int widthWithOutliers = mapStatistics.highestChunkX - mapStatistics.lowestChunkX + 1;
            int lengthWithOutliers = mapStatistics.highestChunkZ - mapStatistics.lowestChunkZ + 1;
            int areaOfOutliers = mapStatistics.outlyingChunks.size();
            labelOutliers1.setVisible(true);
            labelOutliers2.setVisible(true);
            labelWidthWithOutliers.setText(FORMATTER.format(widthWithOutliers * 16) + " blocks (" + FORMATTER.format(widthWithOutliers) + " chunks)");
            labelWidthWithOutliers.setVisible(true);
            labelOutliers3.setVisible(true);
            labelLengthWithOutliers.setText(FORMATTER.format(lengthWithOutliers * 16) + " blocks (" + FORMATTER.format(lengthWithOutliers) + " chunks)");
            labelLengthWithOutliers.setVisible(true);
            labelOutliers4.setVisible(true);
            labelAreaOutliers.setText(FORMATTER.format(areaOfOutliers * 256L) + " blocksĀ² (" + FORMATTER.format(areaOfOutliers) + " chunks)");
            labelAreaOutliers.setVisible(true);
            checkBoxImportOutliers.setVisible(true);
            // The dialog may need to become bigger:
            pack();
        }
    }
}
Also used : org.pepsoft.worldpainter(org.pepsoft.worldpainter) java.util(java.util) DocumentListener(javax.swing.event.DocumentListener) ProgressDialog(org.pepsoft.util.swing.ProgressDialog) IOException(java.io.IOException) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) NumberFormat(java.text.NumberFormat) InvocationTargetException(java.lang.reflect.InvocationTargetException) java.awt(java.awt) ProgressTask(org.pepsoft.util.swing.ProgressTask) List(java.util.List) ProgressReceiver(org.pepsoft.util.ProgressReceiver) OperationCancelled(org.pepsoft.util.ProgressReceiver.OperationCancelled) FileUtils(org.pepsoft.util.FileUtils) Level(org.pepsoft.minecraft.Level) Pattern(java.util.regex.Pattern) DocumentEvent(javax.swing.event.DocumentEvent) MinecraftUtil(org.pepsoft.worldpainter.util.MinecraftUtil) SUPPORTED_VERSION_2(org.pepsoft.minecraft.Constants.SUPPORTED_VERSION_2) RegionFile(org.pepsoft.minecraft.RegionFile) javax.swing(javax.swing) SUPPORTED_VERSION_1(org.pepsoft.minecraft.Constants.SUPPORTED_VERSION_1) Pattern(java.util.regex.Pattern) ProgressTask(org.pepsoft.util.swing.ProgressTask) IOException(java.io.IOException) RegionFile(org.pepsoft.minecraft.RegionFile) ProgressReceiver(org.pepsoft.util.ProgressReceiver) Level(org.pepsoft.minecraft.Level) File(java.io.File) RegionFile(org.pepsoft.minecraft.RegionFile)

Example 5 with OperationCancelled

use of org.pepsoft.util.ProgressReceiver.OperationCancelled in project WorldPainter by Captain-Chaos.

the class AbstractWorldExporter method secondPass.

protected List<Fixup> secondPass(List<Layer> secondaryPassLayers, Dimension dimension, MinecraftWorld minecraftWorld, Map<Layer, LayerExporter> exporters, Collection<Tile> tiles, Point regionCoords, ProgressReceiver progressReceiver) throws OperationCancelled {
    // Apply other secondary pass layers
    if (logger.isDebugEnabled()) {
        logger.debug("Start of second pass for region {},{}", regionCoords.x, regionCoords.y);
    }
    int layerCount = secondaryPassLayers.size(), counter = 0;
    Rectangle area = new Rectangle((regionCoords.x << 9) - 16, (regionCoords.y << 9) - 16, 544, 544);
    Rectangle exportedArea = new Rectangle((regionCoords.x << 9), (regionCoords.y << 9), 512, 512);
    List<Fixup> fixups = new ArrayList<>();
    // boolean frost = false;
    for (Layer layer : secondaryPassLayers) {
        // if (layer instanceof Frost) {
        // frost = true;
        // continue;
        // }
        @SuppressWarnings("unchecked") SecondPassLayerExporter exporter = (SecondPassLayerExporter) exporters.get(layer);
        if (logger.isDebugEnabled()) {
            logger.debug("Exporting layer {} for region {},{}", layer, regionCoords.x, regionCoords.y);
        }
        if (progressReceiver != null) {
            if (minecraftWorld instanceof InvertedWorld) {
                progressReceiver.setMessage("Exporting layer " + layer + " for ceiling");
            } else {
                progressReceiver.setMessage("Exporting layer " + layer);
            }
        }
        List<Fixup> layerFixups = exporter.render(dimension, area, exportedArea, minecraftWorld);
        if (layerFixups != null) {
            fixups.addAll(layerFixups);
        }
        if (progressReceiver != null) {
            counter++;
            progressReceiver.setProgress((float) counter / layerCount);
        }
    }
    // Garden / seeds first and second pass
    GardenExporter gardenExporter = new GardenExporter();
    Set<Seed> firstPassProcessedSeeds = new HashSet<>();
    Set<Seed> secondPassProcessedSeeds = new HashSet<>();
    tiles.stream().filter(tile -> tile.getLayers().contains(GardenCategory.INSTANCE)).forEach(tile -> {
        gardenExporter.firstPass(dimension, tile, minecraftWorld, firstPassProcessedSeeds);
        gardenExporter.secondPass(dimension, tile, minecraftWorld, secondPassProcessedSeeds);
    });
    // elegant
    if ((dimension.getDim() == 0) && world.isCreateGoodiesChest()) {
        Point goodiesPoint = (Point) world.getSpawnPoint().clone();
        goodiesPoint.translate(3, 3);
        int height = Math.min(dimension.getIntHeightAt(goodiesPoint) + 1, dimension.getMaxHeight() - 1);
        minecraftWorld.setMaterialAt(goodiesPoint.x, goodiesPoint.y, height, Material.CHEST_NORTH);
        Chunk chunk = minecraftWorld.getChunk(goodiesPoint.x >> 4, goodiesPoint.y >> 4);
        if ((chunk != null) && (chunk.getTileEntities() != null)) {
            Chest goodiesChest = createGoodiesChest();
            goodiesChest.setX(goodiesPoint.x);
            goodiesChest.setY(height);
            goodiesChest.setZ(goodiesPoint.y);
            chunk.getTileEntities().add(goodiesChest);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("End of second pass for region {},{}", regionCoords.x, regionCoords.y);
    }
    return fixups;
}
Also used : java.util(java.util) org.pepsoft.minecraft(org.pepsoft.minecraft) BLOCKS(org.pepsoft.minecraft.Block.BLOCKS) EventVO(org.pepsoft.worldpainter.vo.EventVO) LoggerFactory(org.slf4j.LoggerFactory) ParallelProgressManager(org.pepsoft.util.ParallelProgressManager) PlatformProvider(org.pepsoft.worldpainter.plugins.PlatformProvider) SimpleDateFormat(java.text.SimpleDateFormat) SubProgressReceiver(org.pepsoft.util.SubProgressReceiver) CustomLayer(org.pepsoft.worldpainter.layers.CustomLayer) Platform(org.pepsoft.worldpainter.Platform) PlatformManager(org.pepsoft.worldpainter.plugins.PlatformManager) CombinedLayer(org.pepsoft.worldpainter.layers.CombinedLayer) DateFormat(java.text.DateFormat) PrintWriter(java.io.PrintWriter) Constants(org.pepsoft.worldpainter.Constants) Box(org.pepsoft.util.Box) AttributeKeyVO(org.pepsoft.worldpainter.vo.AttributeKeyVO) Logger(org.slf4j.Logger) Seed(org.pepsoft.worldpainter.gardenofeden.Seed) java.util.concurrent(java.util.concurrent) GardenExporter(org.pepsoft.worldpainter.gardenofeden.GardenExporter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) GardenCategory(org.pepsoft.worldpainter.layers.GardenCategory) File(java.io.File) Layer(org.pepsoft.worldpainter.layers.Layer) Dimension(org.pepsoft.worldpainter.Dimension) java.awt(java.awt) List(java.util.List) Constants(org.pepsoft.minecraft.Constants) ProgressReceiver(org.pepsoft.util.ProgressReceiver) OperationCancelled(org.pepsoft.util.ProgressReceiver.OperationCancelled) Tile(org.pepsoft.worldpainter.Tile) World2(org.pepsoft.worldpainter.World2) NotNull(org.jetbrains.annotations.NotNull) CustomLayer(org.pepsoft.worldpainter.layers.CustomLayer) CombinedLayer(org.pepsoft.worldpainter.layers.CombinedLayer) Layer(org.pepsoft.worldpainter.layers.Layer) Seed(org.pepsoft.worldpainter.gardenofeden.Seed) GardenExporter(org.pepsoft.worldpainter.gardenofeden.GardenExporter)

Aggregations

OperationCancelled (org.pepsoft.util.ProgressReceiver.OperationCancelled)13 ProgressReceiver (org.pepsoft.util.ProgressReceiver)9 Layer (org.pepsoft.worldpainter.layers.Layer)5 javax.swing (javax.swing)4 ProgressDialog (org.pepsoft.util.swing.ProgressDialog)4 ProgressTask (org.pepsoft.util.swing.ProgressTask)4 File (java.io.File)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 java.util (java.util)3 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 List (java.util.List)3 Set (java.util.Set)3 SubProgressReceiver (org.pepsoft.util.SubProgressReceiver)3 EventVO (org.pepsoft.worldpainter.vo.EventVO)3 java.awt (java.awt)2 IOException (java.io.IOException)2 Void (java.lang.Void)2 FileFilter (javax.swing.filechooser.FileFilter)2 ObservableBoolean (org.pepsoft.util.ObservableBoolean)2