Search in sources :

Example 6 with WPObject

use of org.pepsoft.worldpainter.objects.WPObject in project WorldPainter by Captain-Chaos.

the class Bo2LayerEditor method reset.

@Override
public void reset() {
    List<WPObject> objects = new ArrayList<>();
    fieldName.setText(layer.getName());
    selectedColour = layer.getColour();
    List<File> files = layer.getFiles();
    if (files != null) {
        if (files.isEmpty()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Existing layer contains new style objects");
            }
            // New layer; files stored in object attributes
            objects.addAll(layer.getObjectProvider().getAllObjects());
        } else {
            // Old layer; files stored separately
            int missingFiles = 0;
            CustomObjectManager customObjectManager = CustomObjectManager.getInstance();
            if ((files.size() == 1) && files.get(0).isDirectory()) {
                logger.info("Existing custom object layer contains old style directory; migrating to new style");
                File[] filesInDir = files.get(0).listFiles((FilenameFilter) CustomObjectManager.getInstance().getFileFilter());
                // noinspection ConstantConditions // Cannot happen as we already checked that files.get(0) is an extant directory
                for (File file : filesInDir) {
                    try {
                        objects.add(customObjectManager.loadObject(file));
                    } catch (IOException e) {
                        logger.error("I/O error while trying to load custom object " + file, e);
                        missingFiles++;
                    }
                }
            } else {
                logger.info("Existing custom object layer contains old style file list; migrating to new style");
                for (File file : files) {
                    if (file.exists()) {
                        try {
                            objects.add(customObjectManager.loadObject(file));
                        } catch (IOException e) {
                            logger.error("I/O error while trying to load custom object " + file, e);
                            missingFiles++;
                        }
                    } else {
                        missingFiles++;
                    }
                }
            }
            if (missingFiles > 0) {
                JOptionPane.showMessageDialog(this, "This is an old custom object layer and " + missingFiles + " objects\ncould NOT be restored because they were missing or\nreading them resulted in an I/O error.\n\nYou will have to re-add these objects before\nsaving the settings, otherwise the existing object\ndata will be gone. You may also cancel the dialog\nwithout affecting the object data.", "Missing Files", JOptionPane.WARNING_MESSAGE);
            }
        }
    } else {
        logger.info("Existing custom object layer contains very old style objects with no file information; migrating to new style");
        // Very old layer; no file information at all
        objects.addAll(layer.getObjectProvider().getAllObjects());
    }
    listModel.clear();
    for (WPObject object : objects) {
        listModel.addElement(object.clone());
    }
    spinnerBlocksPerAttempt.setValue(layer.getDensity());
    setLabelColour();
    refreshLeafDecaySettings();
    settingsChanged();
}
Also used : CustomObjectManager(org.pepsoft.worldpainter.plugins.CustomObjectManager) WPObject(org.pepsoft.worldpainter.objects.WPObject) IOException(java.io.IOException) File(java.io.File)

Example 7 with WPObject

use of org.pepsoft.worldpainter.objects.WPObject in project WorldPainter by Captain-Chaos.

the class Bo2LayerEditor method reloadObjects.

private void reloadObjects() {
    StringBuilder noFiles = new StringBuilder();
    StringBuilder notFound = new StringBuilder();
    StringBuilder errors = new StringBuilder();
    int[] indices;
    if (listObjects.getSelectedIndex() != -1) {
        indices = listObjects.getSelectedIndices();
    } else {
        indices = new int[listModel.getSize()];
        for (int i = 0; i < indices.length; i++) {
            indices[i] = i;
        }
    }
    CustomObjectManager customObjectManager = CustomObjectManager.getInstance();
    for (int index : indices) {
        WPObject object = (WPObject) listModel.getElementAt(index);
        File file = object.getAttribute(ATTRIBUTE_FILE);
        if (file != null) {
            if (file.isFile() && file.canRead()) {
                try {
                    Map<String, Serializable> existingAttributes = object.getAttributes();
                    object = customObjectManager.loadObject(file);
                    if (existingAttributes != null) {
                        Map<String, Serializable> attributes = object.getAttributes();
                        if (attributes == null) {
                            attributes = new HashMap<>();
                        }
                        attributes.putAll(existingAttributes);
                        object.setAttributes(attributes);
                    }
                    listModel.setElementAt(object, index);
                } catch (IOException e) {
                    logger.error("I/O error while reloading " + file, e);
                    errors.append(file.getPath()).append('\n');
                }
            } else {
                notFound.append(file.getPath()).append('\n');
            }
        } else {
            noFiles.append(object.getName()).append('\n');
        }
    }
    if ((noFiles.length() > 0) || (notFound.length() > 0)) {
        StringBuilder message = new StringBuilder();
        message.append("Not all files could be reloaded!\n");
        if (noFiles.length() > 0) {
            message.append("\nThe following objects came from an old layer and have no filename stored:\n");
            message.append(noFiles);
        }
        if (notFound.length() > 0) {
            message.append("\nThe following files were missing or not accessible:\n");
            message.append(notFound);
        }
        JOptionPane.showMessageDialog(this, message, "Not All Files Reloaded", JOptionPane.ERROR_MESSAGE);
    } else {
        JOptionPane.showMessageDialog(this, indices.length + " objects successfully reloaded", "Success", JOptionPane.INFORMATION_MESSAGE);
    }
    refreshLeafDecaySettings();
}
Also used : Serializable(java.io.Serializable) CustomObjectManager(org.pepsoft.worldpainter.plugins.CustomObjectManager) WPObject(org.pepsoft.worldpainter.objects.WPObject) IOException(java.io.IOException) File(java.io.File)

Example 8 with WPObject

use of org.pepsoft.worldpainter.objects.WPObject in project WorldPainter by Captain-Chaos.

the class Bo2LayerEditor method setLeavesNoDecay.

private void setLeavesNoDecay() {
    for (Enumeration<WPObject> e = (Enumeration<WPObject>) listModel.elements(); e.hasMoreElements(); ) {
        WPObject object = e.nextElement();
        object.setAttribute(ATTRIBUTE_LEAF_DECAY_MODE, LEAF_DECAY_OFF);
    }
    refreshLeafDecaySettings();
}
Also used : WPObject(org.pepsoft.worldpainter.objects.WPObject)

Example 9 with WPObject

use of org.pepsoft.worldpainter.objects.WPObject in project WorldPainter by Captain-Chaos.

the class Bo2LayerEditor method resetLeafDecay.

private void resetLeafDecay() {
    for (Enumeration<WPObject> e = (Enumeration<WPObject>) listModel.elements(); e.hasMoreElements(); ) {
        WPObject object = e.nextElement();
        object.getAttributes().remove(ATTRIBUTE_LEAF_DECAY_MODE);
    }
    refreshLeafDecaySettings();
}
Also used : WPObject(org.pepsoft.worldpainter.objects.WPObject)

Example 10 with WPObject

use of org.pepsoft.worldpainter.objects.WPObject in project WorldPainter by Captain-Chaos.

the class EditObjectAttributes method ok.

private void ok() {
    boolean singleSelection = objects.size() == 1;
    for (WPObject object : objects) {
        if (singleSelection && (!fieldName.getText().trim().isEmpty())) {
            object.setName(fieldName.getText().trim());
        }
        Map<String, Serializable> attributes = object.getAttributes();
        if (attributes == null) {
            attributes = new HashMap<>();
        }
        if (checkBoxFrequencyActive.isSelected()) {
            int frequency = (Integer) spinnerFrequency.getValue();
            if (frequency != 100) {
                attributes.put(ATTRIBUTE_FREQUENCY.key, frequency);
            } else {
                attributes.remove(ATTRIBUTE_FREQUENCY.key);
            }
        }
        Point3i offset = offsets.get(object);
        if ((offset != null) && ((offset.x != 0) || (offset.y != 0) || (offset.z != 0))) {
            attributes.put(ATTRIBUTE_OFFSET.key, offset);
        } else {
            attributes.remove(ATTRIBUTE_OFFSET.key);
        }
        if (!checkBoxRandomRotation.isMixed()) {
            attributes.put(ATTRIBUTE_RANDOM_ROTATION.key, checkBoxRandomRotation.isSelected());
        }
        if (!checkBoxOnAir.isMixed()) {
            attributes.put(ATTRIBUTE_NEEDS_FOUNDATION.key, !checkBoxOnAir.isSelected());
        }
        if (!checkBoxUnderLava.isMixed()) {
            attributes.put(ATTRIBUTE_SPAWN_IN_LAVA.key, checkBoxUnderLava.isSelected());
        }
        if (!checkBoxUnderWater.isMixed()) {
            attributes.put(ATTRIBUTE_SPAWN_IN_WATER.key, checkBoxUnderWater.isSelected());
        }
        if (!checkBoxOnSolidLand.isMixed()) {
            attributes.put(ATTRIBUTE_SPAWN_ON_LAND.key, checkBoxOnSolidLand.isSelected());
        }
        if (!checkBoxOnWater.isMixed()) {
            attributes.put(ATTRIBUTE_SPAWN_ON_WATER.key, checkBoxOnWater.isSelected());
        }
        if (!checkBoxOnLava.isMixed()) {
            attributes.put(ATTRIBUTE_SPAWN_ON_LAVA.key, checkBoxOnLava.isSelected());
        }
        if (singleSelection || comboBoxCollisionMode.getSelectedIndex() > 0) {
            attributes.put(ATTRIBUTE_COLLISION_MODE.key, comboBoxCollisionMode.getSelectedIndex() + (singleSelection ? 1 : 0));
        }
        if (singleSelection || comboBoxUndergroundMode.getSelectedIndex() > 0) {
            attributes.put(ATTRIBUTE_UNDERGROUND_MODE.key, comboBoxUndergroundMode.getSelectedIndex() + (singleSelection ? 1 : 0));
        }
        if (singleSelection || comboBoxLeafDecayMode.getSelectedIndex() > 0) {
            attributes.put(ATTRIBUTE_LEAF_DECAY_MODE.key, comboBoxLeafDecayMode.getSelectedIndex() + (singleSelection ? 1 : 0));
        }
        if (singleSelection) {
            if (checkBoxReplace.isSelected()) {
                attributes.put(ATTRIBUTE_REPLACE_WITH_AIR.key, new int[] { comboBoxReplaceBlockId.getSelectedIndex(), (Integer) spinnerReplaceData.getValue() });
            } else {
                attributes.remove(ATTRIBUTE_REPLACE_WITH_AIR.key);
            }
        }
        if (!checkBoxExtendFoundation.isMixed()) {
            attributes.put(ATTRIBUTE_EXTEND_FOUNDATION.key, checkBoxExtendFoundation.isSelected());
        }
        if (!attributes.isEmpty()) {
            object.setAttributes(attributes);
        } else {
            object.setAttributes(null);
        }
    }
    cancelled = false;
    dispose();
}
Also used : Point3i(javax.vecmath.Point3i) Serializable(java.io.Serializable) WPObject(org.pepsoft.worldpainter.objects.WPObject)

Aggregations

WPObject (org.pepsoft.worldpainter.objects.WPObject)17 File (java.io.File)4 Point3i (javax.vecmath.Point3i)3 CustomObjectManager (org.pepsoft.worldpainter.plugins.CustomObjectManager)3 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 MirroredObject (org.pepsoft.worldpainter.objects.MirroredObject)2 RotatedObject (org.pepsoft.worldpainter.objects.RotatedObject)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 CertificateException (java.security.cert.CertificateException)1 CertificateFactory (java.security.cert.CertificateFactory)1 X509Certificate (java.security.cert.X509Certificate)1 Dimension (org.pepsoft.worldpainter.Dimension)1 Fixup (org.pepsoft.worldpainter.exporting.Fixup)1 Bo2Layer (org.pepsoft.worldpainter.layers.Bo2Layer)1 Bo2ObjectProvider (org.pepsoft.worldpainter.layers.bo2.Bo2ObjectProvider)1