Skip to content

Commit

Permalink
Fix a potential race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Apr 2, 2024
1 parent ab1fed8 commit c3b13aa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
9 changes: 9 additions & 0 deletions verification/src/changes/accepted-cli-public-api-changes.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,14 @@
"CONSTRUCTOR_REMOVED"
]
}
],
"Constructor added with required data to not trigger race condition": [
{
"type": "com.sk89q.worldedit.cli.schematic.ClipboardWorld",
"member": "Constructor com.sk89q.worldedit.cli.schematic.ClipboardWorld(java.io.File,com.sk89q.worldedit.extent.clipboard.Clipboard,java.lang.String)",
"changes": [
"CONSTRUCTOR_REMOVED"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public CLIConfiguration getConfiguration() {

@Override
public String getVersion() {
if (app.getInternalVersion() == null) {
return "unknown"; // Run from IDE
}
return app.getInternalVersion();
}

Expand All @@ -136,7 +139,7 @@ public String getPlatformName() {

@Override
public String getPlatformVersion() {
return app.getInternalVersion();
return this.getVersion();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ public static void main(String[] args) {
try (ClipboardReader clipboardReader = format.getReader(Files.newInputStream(file.toPath(), StandardOpenOption.READ))) {
world = new ClipboardWorld(
file,
format,
clipboardReader.read(),
file.getName()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
Expand All @@ -56,13 +56,15 @@
public class ClipboardWorld extends AbstractWorld implements Clipboard, CLIWorld {

private final File file;
private final ClipboardFormat format;
private final Clipboard clipboard;
private final String name;

private boolean dirty = false;

public ClipboardWorld(File file, Clipboard clipboard, String name) {
public ClipboardWorld(File file, ClipboardFormat format, Clipboard clipboard, String name) {
this.file = file;
this.format = format;
this.clipboard = clipboard;
this.name = name;
}
Expand All @@ -74,7 +76,7 @@ public String getName() {

@Override
public String id() {
return getName().replace(" ", "_").toLowerCase(Locale.ROOT);
return name.replace(" ", "_").toLowerCase(Locale.ROOT);
}

@Override
Expand Down Expand Up @@ -208,7 +210,7 @@ public BlockVector3 getMinimumPoint() {
@Override
public void save(boolean force) {
if (dirty || force) {
try (ClipboardWriter writer = ClipboardFormats.findByFile(file).getWriter(new FileOutputStream(file))) {
try (ClipboardWriter writer = format.getWriter(new FileOutputStream(file))) {
writer.write(this);
dirty = false;
} catch (IOException e) {
Expand Down

0 comments on commit c3b13aa

Please sign in to comment.