Skip to content

Commit

Permalink
Add EMCStorage wrapper to transmutation interface for instant item → …
Browse files Browse the repository at this point in the history
…EMC conversion
  • Loading branch information
62832 committed May 11, 2024
1 parent 2a0ea4d commit 116683e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
58 changes: 56 additions & 2 deletions src/main/java/gripe/_90/appliede/me/misc/EMCInterfaceLogic.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gripe._90.appliede.me.misc;

import java.util.List;
import java.util.Objects;

import org.jetbrains.annotations.Nullable;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.capabilities.Capability;
Expand Down Expand Up @@ -36,6 +38,7 @@
import appeng.util.Platform;

import gripe._90.appliede.AppliedE;
import gripe._90.appliede.me.service.EMCStorage;
import gripe._90.appliede.me.service.KnowledgeService;

import moze_intel.projecte.api.proxy.IEMCProxy;
Expand All @@ -55,6 +58,11 @@ public class EMCInterfaceLogic implements IActionHost, IGridTickable, IUpgradeab
private final LazyOptional<IItemHandler> storageHolder;
private final LazyOptional<MEStorage> localInvHolder;

@Nullable
private WrappedEMCStorage emcStorage;

private boolean hasConfig = false;

public EMCInterfaceLogic(IManagedGridNode node, EMCInterfaceLogicHost host, Item is) {
this(node, host, is, 9);
}
Expand All @@ -68,7 +76,7 @@ public EMCInterfaceLogic(IManagedGridNode node, EMCInterfaceLogicHost host, Item

config = ConfigInventory.configStacks(AEItemKey.filter(), slots, this::onConfigRowChanged, false);
storage = ConfigInventory.storage(this::storageFilter, slots, this::onStorageChanged);
upgrades = UpgradeInventories.forMachine(is, 1, host::saveChanges);
upgrades = UpgradeInventories.forMachine(is, 1, this::onUpgradesChanged);

localInvHandler = new DelegatingMEInventory(storage);
plannedWork = new GenericStack[slots];
Expand All @@ -77,7 +85,7 @@ public EMCInterfaceLogic(IManagedGridNode node, EMCInterfaceLogicHost host, Item
storage.useRegisteredCapacities();

storageHolder = LazyOptional.of(() -> storage).lazyMap(GenericStackItemStorage::new);
localInvHolder = LazyOptional.of(() -> localInvHandler);
localInvHolder = LazyOptional.of(this::getInventory);
}

public ConfigInventory getConfig() {
Expand All @@ -88,6 +96,10 @@ public ConfigInventory getStorage() {
return storage;
}

public MEStorage getInventory() {
return hasConfig ? localInvHandler : emcStorage;
}

@Override
public IUpgradeInventory getUpgrades() {
return upgrades;
Expand Down Expand Up @@ -124,6 +136,7 @@ public void readFromNBT(CompoundTag tag) {
storage.readFromChildTag(tag, "storage");
upgrades.readFromNBT(tag, "upgrades");

hasConfig = !config.isEmpty();
updatePlan();
notifyNeighbours();
}
Expand Down Expand Up @@ -286,6 +299,7 @@ private boolean acquireFromNetwork(IGrid grid, int slot, AEKey what, long amount
}

private void onConfigRowChanged() {
hasConfig = !config.isEmpty();
host.saveChanges();
updatePlan();
notifyNeighbours();
Expand All @@ -296,6 +310,14 @@ private void onStorageChanged() {
updatePlan();
}

private void onUpgradesChanged() {
if (emcStorage != null) {
emcStorage.setMayLearn(upgrades.isInstalled(AppliedE.LEARNING_CARD.get()));
}

host.saveChanges();
}

public void notifyNeighbours() {
mainNode.ifPresent((grid, node) -> {
if (node.isActive()) {
Expand All @@ -310,6 +332,13 @@ public void notifyNeighbours() {
}
}

public void gridChanged() {
emcStorage = new WrappedEMCStorage(Objects.requireNonNull(mainNode.getGrid())
.getService(KnowledgeService.class)
.getStorage());
notifyNeighbours();
}

public void addDrops(List<ItemStack> drops) {
for (var i = 0; i < storage.size(); i++) {
var stack = storage.getStack(i);
Expand Down Expand Up @@ -345,4 +374,29 @@ public void invalidateCaps() {
storageHolder.invalidate();
localInvHolder.invalidate();
}

private static class WrappedEMCStorage implements MEStorage {
private final EMCStorage storage;
private boolean mayLearn;

private WrappedEMCStorage(EMCStorage storage) {
this.storage = storage;
}

private void setMayLearn(boolean mayLearn) {
this.mayLearn = mayLearn;
}

@Override
public long insert(AEKey what, long amount, Actionable mode, IActionSource source) {
return what instanceof AEItemKey item
? storage.insertItem(item, amount, mode, source, mayLearn)
: storage.insert(what, amount, mode, source);
}

@Override
public Component getDescription() {
return storage.getDescription();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void onStateChanged(EMCInterfaceLogicHost host, IGridNode node, State sta

@Override
public void onGridChanged(EMCInterfaceLogicHost host, IGridNode node) {
host.getInterfaceLogic().notifyNeighbours();
host.getInterfaceLogic().gridChanged();
}
};

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/gripe/_90/appliede/me/service/EMCStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void getAvailableStacks(KeyCounter out) {

@Override
public long insert(AEKey what, long amount, Actionable mode, IActionSource source) {
if (amount == 0 || !(what instanceof EMCKey emc)) {
if (amount <= 0 || !(what instanceof EMCKey emc)) {
return 0;
}

Expand All @@ -85,7 +85,7 @@ public long insert(AEKey what, long amount, Actionable mode, IActionSource sourc

@Override
public long extract(AEKey what, long amount, Actionable mode, IActionSource source) {
if (amount == 0) {
if (amount <= 0) {
return 0;
}

Expand Down Expand Up @@ -151,7 +151,7 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour

public long insertItem(
AEItemKey what, long amount, Actionable mode, IActionSource source, boolean mayLearn, Runnable onLearn) {
if (service.getProviders().isEmpty()) {
if (amount <= 0 || service.getProviders().isEmpty()) {
return 0;
}

Expand Down Expand Up @@ -227,7 +227,7 @@ public long extractItem(AEItemKey what, long amount, Actionable mode, IActionSou
return 0;
}

if (!service.knowsItem(what)) {
if (amount <= 0 || !service.knowsItem(what)) {
return 0;
}

Expand Down Expand Up @@ -281,7 +281,7 @@ private long getAmountAfterPowerExpenditure(long maxAmount) {
return (double) maxAmount <= availablePower ? maxAmount : (long) availablePower;
}

public int getHighestTier() {
int getHighestTier() {
return highestTier;
}

Expand Down

0 comments on commit 116683e

Please sign in to comment.