Skip to content

Commit

Permalink
Remove unsafe LazyOptional::resolve calls
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
62832 committed Dec 30, 2023
1 parent 4777eaa commit 7d690f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
19 changes: 8 additions & 11 deletions src/main/java/gripe/_90/arseng/mixin/RelaySplitterTileMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ void addCapToProcessTo(CallbackInfo ci) {
if (posList.isEmpty()) return;

var stale = new ArrayList<BlockPos>();
var ratePer = getTransferRate() / posList.size();

for (var pos : posList) {
if (!Objects.requireNonNull(level).isLoaded(pos)) continue;
Expand All @@ -64,21 +63,19 @@ void addCapToProcessTo(CallbackInfo ci) {
continue;
}

var cap = be.getCapability(ArsEngCapabilities.SOURCE_TILE, IAdvancedSourceTile.getDirTo(getBlockPos(), pos))
.resolve();
var cap =
be.getCapability(ArsEngCapabilities.SOURCE_TILE, IAdvancedSourceTile.getDirTo(getBlockPos(), pos));
cap.ifPresent(sourceTile -> {
var fromTile = sendSource ? this : sourceTile;
var toTile = sendSource ? sourceTile : this;

if (cap.isPresent()) {
var fromTile = sendSource ? this : cap.get();
var toTile = sendSource ? cap.get() : this;

if (transferSource(fromTile, toTile, ratePer) > 0) {
if (transferSource(fromTile, toTile, getTransferRate() / posList.size()) > 0) {
var fromPos = sendSource ? worldPosition : pos;
var toPos = sendSource ? pos : worldPosition;
createParticles(fromPos, toPos);
}
} else {
stale.add(pos);
}
});
cap.addListener(sourceTile -> stale.add(pos));
}

for (var pos : stale) {
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/gripe/_90/arseng/mixin/ScribesTileMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand Down Expand Up @@ -52,18 +53,24 @@ private void takeFromInterfaces(CallbackInfo ci) {
var be = level.getBlockEntity(pos);

if (be != null) {
var centreCap = be.getCapability(Capabilities.STORAGE).resolve();
var hasExtracted = new AtomicBoolean(false);

if (centreCap.isPresent()) {
arseng$extract(centreCap.get(), pos);
be.getCapability(Capabilities.STORAGE).ifPresent(storage -> {
arseng$extract(storage, pos);
hasExtracted.set(true);
});

if (hasExtracted.get()) {
return;
}

for (var side : Direction.values()) {
var sidedCap = be.getCapability(Capabilities.STORAGE, side).resolve();
be.getCapability(Capabilities.STORAGE, side).ifPresent(storage -> {
arseng$extract(storage, pos);
hasExtracted.set(true);
});

if (sidedCap.isPresent()) {
arseng$extract(sidedCap.get(), pos);
if (hasExtracted.get()) {
return;
}
}
Expand Down

0 comments on commit 7d690f1

Please sign in to comment.