use of org.pepsoft.worldpainter.vo.EventVO in project WorldPainter by Captain-Chaos.
the class App method save.
/**
* Save the world to the specified file, overwriting it if it already exists
* without asking for confirmation. Shows a progress indicator while saving.
*
* @param file The file to which to save the world.
*/
private boolean save(File file) {
// Check for write access to directory
if (!file.getParentFile().isDirectory()) {
showMessageDialog(this, strings.getString("the.selected.path.does.not.exist"), strings.getString("non.existant.path"), ERROR_MESSAGE);
return false;
}
if (!file.getParentFile().canWrite()) {
showMessageDialog(this, strings.getString("you.do.not.have.write.access"), strings.getString("access.denied"), ERROR_MESSAGE);
return false;
}
// Normalise the filename
String name = file.getName();
name = name.trim();
if (name.isEmpty()) {
// NOI18N
name = strings.getString("generated.world") + ".world";
} else {
name = FileUtils.sanitiseName(name);
}
final File normalisedFile = new File(file.getParentFile(), name);
// Make sure the world name is always the same as the file name, to
// avoid confusion (unless the only difference is illegal filename
// characters changed into underscores
int p = name.lastIndexOf('.');
if (p != -1) {
name = name.substring(0, p).trim();
}
String worldName = world.getName();
if (worldName.length() != name.length()) {
world.setName(name);
// NOI18N
setTitle("WorldPainter - " + name + " - " + dimension.getName());
} else {
for (int i = 0; i < name.length(); i++) {
if ((name.charAt(i) != '_') && (name.charAt(i) != worldName.charAt(i))) {
world.setName(name);
// NOI18N
setTitle("WorldPainter - " + name + " - " + dimension.getName());
break;
}
}
}
logger.info("Saving world " + world.getName() + " to " + file.getAbsolutePath());
saveCustomMaterials();
saveCustomBiomes();
// currently in use
if (!paletteManager.isEmpty()) {
List<CustomLayer> customLayers = new ArrayList<>();
for (Palette palette : paletteManager.getPalettes()) {
customLayers.addAll(palette.getLayers());
}
dimension.setCustomLayers(customLayers);
} else {
dimension.setCustomLayers(Collections.EMPTY_LIST);
}
if (dimension != null) {
Point viewPosition = view.getViewCentreInWorldCoords();
if (viewPosition != null) {
this.dimension.setLastViewPosition(viewPosition);
}
}
ProgressDialog.executeTask(this, new ProgressTask<java.lang.Void>() {
@Override
public String getName() {
return strings.getString("saving.world");
}
@Override
public java.lang.Void execute(ProgressReceiver progressReceiver) throws OperationCancelled {
try {
Configuration config = Configuration.getInstance();
if ((config.getWorldFileBackups() > 0) && normalisedFile.isFile()) {
progressReceiver.setMessage(strings.getString("creating.backup.s"));
for (int i = config.getWorldFileBackups(); i > 0; i--) {
File nextBackupFile = (i > 1) ? BackupUtil.getBackupFile(normalisedFile, i - 1) : normalisedFile;
if (nextBackupFile.isFile()) {
File backupFile = BackupUtil.getBackupFile(normalisedFile, i);
if (backupFile.isFile()) {
if (!backupFile.delete()) {
throw new RuntimeException("Could not delete old backup file " + backupFile);
}
}
if (!nextBackupFile.renameTo(backupFile)) {
throw new RuntimeException("Could not move " + nextBackupFile + " to " + backupFile);
}
}
}
progressReceiver.setMessage(null);
}
world.addHistoryEntry(HistoryEntry.WORLD_SAVED, normalisedFile);
WorldIO worldIO = new WorldIO(world);
worldIO.save(new FileOutputStream(normalisedFile));
Map<String, byte[]> layoutData = config.getJideLayoutData();
if (layoutData == null) {
layoutData = new HashMap<>();
}
layoutData.put(world.getName(), dockingManager.getLayoutRawData());
config.setJideLayoutData(layoutData);
return null;
} catch (IOException e) {
throw new RuntimeException("I/O error saving file (message: " + e.getMessage() + ")", e);
}
}
}, false);
// Log an event
Configuration config = Configuration.getInstance();
if (config != null) {
EventVO event = new EventVO(EVENT_KEY_ACTION_SAVE_WORLD).addTimestamp();
event.setAttribute(ATTRIBUTE_KEY_MAX_HEIGHT, world.getMaxHeight());
Dimension loadedDimension = world.getDimension(0);
event.setAttribute(ATTRIBUTE_KEY_TILES, loadedDimension.getTiles().size());
logLayers(loadedDimension, event, "");
loadedDimension = world.getDimension(1);
if (loadedDimension != null) {
event.setAttribute(ATTRIBUTE_KEY_NETHER_TILES, loadedDimension.getTiles().size());
logLayers(loadedDimension, event, "nether.");
}
loadedDimension = world.getDimension(2);
if (loadedDimension != null) {
event.setAttribute(ATTRIBUTE_KEY_END_TILES, loadedDimension.getTiles().size());
logLayers(loadedDimension, event, "end.");
}
if (world.getImportedFrom() != null) {
event.setAttribute(ATTRIBUTE_KEY_IMPORTED_WORLD, true);
}
config.logEvent(event);
}
if (currentUndoManager != null) {
currentUndoManager.armSavePoint();
}
world.setDirty(false);
lastSelectedFile = file;
addRecentlyUsedWorld(file);
Configuration.getInstance().setWorldDirectory(file.getParentFile());
return true;
}
use of org.pepsoft.worldpainter.vo.EventVO in project WorldPainter by Captain-Chaos.
the class DonationDialog method donate.
private void donate() {
try {
DesktopUtils.open(new URL("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VZ7WNQVPXDZHY"));
Configuration.getInstance().setDonationStatus(Configuration.DonationStatus.DONATED);
JOptionPane.showMessageDialog(this, "The donation PayPal page has been opened in your browser.\n\nThank you very much for donating!", "Thank You", JOptionPane.INFORMATION_MESSAGE);
cancelled = false;
Configuration.getInstance().logEvent(new EventVO(Constants.EVENT_KEY_DONATION_DONATE).addTimestamp());
dispose();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
Aggregations