From 7d690f13c2e35bf37665af646bf59e0d9f18268d Mon Sep 17 00:00:00 2001 From: 90 Date: Sat, 30 Dec 2023 22:17:56 +0000 Subject: [PATCH] Remove unsafe LazyOptional::resolve calls Fixes #13 --- .../arseng/mixin/RelaySplitterTileMixin.java | 19 ++++++++----------- .../_90/arseng/mixin/ScribesTileMixin.java | 19 +++++++++++++------ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/main/java/gripe/_90/arseng/mixin/RelaySplitterTileMixin.java b/src/main/java/gripe/_90/arseng/mixin/RelaySplitterTileMixin.java index 7a448db..9679365 100644 --- a/src/main/java/gripe/_90/arseng/mixin/RelaySplitterTileMixin.java +++ b/src/main/java/gripe/_90/arseng/mixin/RelaySplitterTileMixin.java @@ -52,7 +52,6 @@ void addCapToProcessTo(CallbackInfo ci) { if (posList.isEmpty()) return; var stale = new ArrayList(); - var ratePer = getTransferRate() / posList.size(); for (var pos : posList) { if (!Objects.requireNonNull(level).isLoaded(pos)) continue; @@ -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) { diff --git a/src/main/java/gripe/_90/arseng/mixin/ScribesTileMixin.java b/src/main/java/gripe/_90/arseng/mixin/ScribesTileMixin.java index 16b7737..49532f5 100644 --- a/src/main/java/gripe/_90/arseng/mixin/ScribesTileMixin.java +++ b/src/main/java/gripe/_90/arseng/mixin/ScribesTileMixin.java @@ -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; @@ -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; } }