Skip to content

Commit

Permalink
Change ChunkMapMixin#read to asynchronously load data
Browse files Browse the repository at this point in the history
There's no reason to sync read the data, as the return value
is a CompletableFuture - and Vanilla performs this function
asynchronously as well.
  • Loading branch information
Spottedleaf committed Aug 22, 2024
1 parent 1b189fc commit 0266b87
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ private void loadTable(final Map<Map<Property<?>, Comparable<?>>, S> map, final
@Overwrite
public <T extends Comparable<T>, V extends T> S setValue(final Property<T> property, final V value) {
final S ret = this.optimisedTable.set(this.tableIndex, property, value);
if (ret == null) {
throw new IllegalArgumentException("Cannot set property " + property + " to " + value + " on " + this.owner);
if (ret != null) {
return ret;
}
return ret;
throw new IllegalArgumentException("Cannot set property " + property + " to " + value + " on " + this.owner);
}

/**
Expand All @@ -101,11 +101,14 @@ public <T extends Comparable<T>, V extends T> S setValue(final Property<T> prope
*/
@Overwrite
public <T extends Comparable<T>, V extends T> S trySetValue(final Property<T> property, final V value) {
final S ret = property == null ? (S)(StateHolder<O, S>)(Object)this : this.optimisedTable.trySet(this.tableIndex, property, value, (S)(StateHolder<O, S>)(Object)this);
if (ret == null) {
throw new IllegalArgumentException("Cannot set property " + property + " to " + value + " on " + this.owner);
if (property == null) {
return (S)(StateHolder<O, S>)(Object)this;
}
final S ret = this.optimisedTable.trySet(this.tableIndex, property, value, (S)(StateHolder<O, S>)(Object)this);
if (ret != null) {
return ret;
}
return ret;
throw new IllegalArgumentException("Cannot set property " + property + " to " + value + " on " + this.owner);
}

/**
Expand All @@ -124,11 +127,10 @@ public <T extends Comparable<T>> Optional<T> getOptionalValue(final Property<T>
@Overwrite
public <T extends Comparable<T>> T getValue(final Property<T> property) {
final T ret = this.optimisedTable.get(this.tableIndex, property);
if (ret == null) {
throw new IllegalArgumentException("Cannot get property " + property + " as it does not exist in " + this.owner);
} else {
if (ret != null) {
return ret;
}
throw new IllegalArgumentException("Cannot get property " + property + " as it does not exist in " + this.owner);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,18 +507,20 @@ public void dumpChunks(final Writer writer) throws IOException {

@Override
public CompletableFuture<Optional<CompoundTag>> read(final ChunkPos pos) {
try {
return CompletableFuture.completedFuture(
Optional.ofNullable(
MoonriseRegionFileIO.loadData(
this.level, pos.x, pos.z, MoonriseRegionFileIO.RegionFileType.CHUNK_DATA,
MoonriseRegionFileIO.getIOBlockingPriorityForCurrentThread()
)
)
);
} catch (final Throwable thr) {
return CompletableFuture.failedFuture(thr);
}
final CompletableFuture<Optional<CompoundTag>> ret = new CompletableFuture<>();

MoonriseRegionFileIO.loadDataAsync(
this.level, pos.x, pos.z, MoonriseRegionFileIO.RegionFileType.CHUNK_DATA,
(final CompoundTag data, final Throwable thr) -> {
if (thr != null) {
ret.completeExceptionally(thr);
} else {
ret.complete(Optional.ofNullable(data));
}
}, false
);

return ret;
}

@Override
Expand Down

0 comments on commit 0266b87

Please sign in to comment.