Skip to content

Commit

Permalink
Greatly reduce boilerplate and some non-API usage for import and expo…
Browse files Browse the repository at this point in the history
…rt buses
  • Loading branch information
62832 committed May 13, 2024
1 parent be39ec7 commit e6bd6ca
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 395 deletions.
45 changes: 25 additions & 20 deletions src/main/java/gripe/_90/appliede/me/strategy/EMCExportStrategy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gripe._90.appliede.me.strategy;

import java.util.concurrent.atomic.AtomicLong;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -9,32 +11,29 @@
import appeng.api.config.Actionable;
import appeng.api.stacks.AEKey;
import appeng.api.storage.StorageHelper;
import appeng.util.BlockApiCache;

import gripe._90.appliede.me.key.EMCKey;

import moze_intel.projecte.api.capabilities.PECapabilities;
import moze_intel.projecte.api.capabilities.block_entity.IEmcStorage;

@SuppressWarnings("UnstableApiUsage")
public class EMCExportStrategy implements StackExportStrategy {
private final BlockApiCache<IEmcStorage> apiCache;
private final Direction fromSide;

public EMCExportStrategy(ServerLevel level, BlockPos fromPos, Direction fromSide) {
apiCache = BlockApiCache.create(PECapabilities.EMC_STORAGE_CAPABILITY, level, fromPos);
this.fromSide = fromSide;
}

public record EMCExportStrategy(ServerLevel level, BlockPos pos, Direction side) implements StackExportStrategy {
@Override
public long transfer(StackTransferContext context, AEKey what, long maxAmount) {
if (!(what instanceof EMCKey)) {
return 0;
}

var emcStorage = apiCache.find(fromSide);
var be = level.getBlockEntity(pos);

if (be == null) {
return 0;
}

var transferred = new AtomicLong(0);

if (emcStorage != null) {
be.getCapability(PECapabilities.EMC_STORAGE_CAPABILITY).ifPresent(emcStorage -> {
var insertable = emcStorage.insertEmc(maxAmount, IEmcStorage.EmcAction.SIMULATE);
var extracted = StorageHelper.poweredExtraction(
context.getEnergySource(),
Expand All @@ -48,10 +47,10 @@ public long transfer(StackTransferContext context, AEKey what, long maxAmount) {
emcStorage.insertEmc(extracted, IEmcStorage.EmcAction.EXECUTE);
}

return extracted;
}
transferred.addAndGet(extracted);
});

return 0;
return transferred.get();
}

@Override
Expand All @@ -60,18 +59,24 @@ public long push(AEKey what, long maxAmount, Actionable mode) {
return 0;
}

var emcStorage = apiCache.find(fromSide);
var be = level.getBlockEntity(pos);

if (emcStorage != null) {
if (be == null) {
return 0;
}

var transferred = new AtomicLong(0);

be.getCapability(PECapabilities.EMC_STORAGE_CAPABILITY).ifPresent(emcStorage -> {
var inserted = Math.min(maxAmount, emcStorage.insertEmc(maxAmount, IEmcStorage.EmcAction.SIMULATE));

if (inserted > 0) {
emcStorage.insertEmc(inserted, IEmcStorage.EmcAction.EXECUTE);
}

return inserted;
}
transferred.addAndGet(inserted);
});

return 0;
return transferred.get();
}
}
50 changes: 23 additions & 27 deletions src/main/java/gripe/_90/appliede/me/strategy/EMCImportStrategy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gripe._90.appliede.me.strategy;

import java.util.concurrent.atomic.AtomicLong;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -8,7 +10,6 @@
import appeng.api.behaviors.StackTransferContext;
import appeng.api.config.Actionable;
import appeng.api.storage.StorageHelper;
import appeng.util.BlockApiCache;

import gripe._90.appliede.me.key.EMCKey;
import gripe._90.appliede.me.key.EMCKeyType;
Expand All @@ -17,42 +18,37 @@
import moze_intel.projecte.api.capabilities.block_entity.IEmcStorage;

@SuppressWarnings("UnstableApiUsage")
public class EMCImportStrategy implements StackImportStrategy {
private final BlockApiCache<IEmcStorage> apiCache;
private final Direction fromSide;

public EMCImportStrategy(ServerLevel level, BlockPos fromPos, Direction fromSide) {
apiCache = BlockApiCache.create(PECapabilities.EMC_STORAGE_CAPABILITY, level, fromPos);
this.fromSide = fromSide;
}

public record EMCImportStrategy(ServerLevel level, BlockPos pos, Direction side) implements StackImportStrategy {
@Override
public boolean transfer(StackTransferContext context) {
if (!context.isKeyTypeEnabled(EMCKeyType.TYPE)) {
return false;
}

var emcStorage = apiCache.find(fromSide);
var be = level.getBlockEntity(pos);

if (emcStorage == null) {
if (be == null) {
return false;
}

var remainingTransfer = context.getOperationsRemaining() * EMCKeyType.TYPE.getAmountPerOperation();
var inserted = Math.min(remainingTransfer, emcStorage.getStoredEmc());

StorageHelper.poweredInsert(
context.getEnergySource(),
context.getInternalStorage().getInventory(),
EMCKey.BASE,
inserted,
context.getActionSource(),
Actionable.MODULATE);
emcStorage.extractEmc(inserted, IEmcStorage.EmcAction.EXECUTE);

var opsUsed = Math.max(1, inserted / EMCKeyType.TYPE.getAmountPerOperation());
context.reduceOperationsRemaining(opsUsed);

return inserted > 0;
var inserted = new AtomicLong(0);

be.getCapability(PECapabilities.EMC_STORAGE_CAPABILITY).ifPresent(emcStorage -> {
inserted.set(Math.min(remainingTransfer, emcStorage.getStoredEmc()));
StorageHelper.poweredInsert(
context.getEnergySource(),
context.getInternalStorage().getInventory(),
EMCKey.BASE,
inserted.get(),
context.getActionSource(),
Actionable.MODULATE);
emcStorage.extractEmc(inserted.get(), IEmcStorage.EmcAction.EXECUTE);

var opsUsed = Math.max(1, inserted.get() / EMCKeyType.TYPE.getAmountPerOperation());
context.reduceOperationsRemaining(opsUsed);
});

return inserted.get() > 0;
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit e6bd6ca

Please sign in to comment.