Skip to content

Commit

Permalink
Remove useless priority setting for ME Transmutation Interface
Browse files Browse the repository at this point in the history
Since Transmutation Interfaces don't actually interact with regular network-wide item storage to begin with and use solely the EMC-based storage provided by KnowledgeService, they aren't actually susceptible to the same "ping-pong" effect (of constantly stocking items taken from each other back and forth) that regular interfaces would be without their own priority system.
  • Loading branch information
62832 committed May 5, 2024
1 parent 49fea67 commit b54f540
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public class EMCInterfaceScreen<M extends EMCInterfaceMenu> extends AEBaseScreen

public EMCInterfaceScreen(M menu, Inventory playerInventory, Component title, ScreenStyle style) {
super(menu, playerInventory, title, style);

widgets.addOpenPriorityButton();
var configSlots = menu.getSlots(SlotSemantics.CONFIG);

for (int i = 0; i < configSlots.size(); i++) {
Expand Down
112 changes: 11 additions & 101 deletions src/main/java/gripe/_90/appliede/me/misc/EMCInterfaceLogic.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package gripe._90.appliede.me.misc;

import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;

import org.jetbrains.annotations.Nullable;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
Expand Down Expand Up @@ -38,20 +34,15 @@

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

@SuppressWarnings("UnstableApiUsage")
public class EMCInterfaceLogic implements IActionHost, IGridTickable {
protected final EMCInterfaceLogicHost host;
protected final IManagedGridNode mainNode;

@Nullable
private MEStorage localInvHandler;

private final ConfigInventory config;
private final ConfigInventory storage;

protected final IActionSource requestSource = new RequestSource();
private final MEStorage localInvHandler;
private final GenericStack[] plannedWork;
private int priority = 0;
private final IActionSource source = IActionSource.ofMachine(this);

private final LazyOptional<IItemHandler> storageHolder;
private final LazyOptional<MEStorage> localInvHolder;
Expand All @@ -60,20 +51,23 @@ public EMCInterfaceLogic(IManagedGridNode node, EMCInterfaceLogicHost host) {
this(node, host, 9);
}

@SuppressWarnings("UnstableApiUsage")
public EMCInterfaceLogic(IManagedGridNode node, EMCInterfaceLogicHost host, int slots) {
this.host = host;
config = ConfigInventory.configStacks(AEItemKey.filter(), slots, this::onConfigRowChanged, false);
storage = ConfigInventory.storage(new StorageFilter(), slots, this::onStorageChanged);
plannedWork = new GenericStack[slots];
mainNode = node.setFlags(GridFlags.REQUIRE_CHANNEL)
.addService(IGridTickable.class, this)
.setIdlePowerUsage(10);

config = ConfigInventory.configStacks(AEItemKey.filter(), slots, this::onConfigRowChanged, false);
storage = ConfigInventory.storage(new StorageFilter(), slots, this::onStorageChanged);
localInvHandler = new DelegatingMEInventory(storage);
plannedWork = new GenericStack[slots];

config.useRegisteredCapacities();
storage.useRegisteredCapacities();

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

public ConfigInventory getConfig() {
Expand All @@ -84,19 +78,9 @@ public ConfigInventory getStorage() {
return storage;
}

public int getPriority() {
return priority;
}

public void setPriority(int priority) {
this.priority = priority;
host.saveChanges();
}

public void readFromNBT(CompoundTag tag) {
config.readFromChildTag(tag, "config");
storage.readFromChildTag(tag, "storage");
priority = tag.getInt("priority");

updatePlan();
notifyNeighbours();
Expand All @@ -105,15 +89,6 @@ public void readFromNBT(CompoundTag tag) {
public void writeToNBT(CompoundTag tag) {
config.writeToChildTag(tag, "config");
storage.writeToChildTag(tag, "storage");
tag.putInt("priority", priority);
}

private MEStorage getInventory() {
if (localInvHandler == null) {
localInvHandler = new Inventory();
}

return localInvHandler;
}

@Nullable
Expand Down Expand Up @@ -230,7 +205,7 @@ private boolean tryUsePlan(int slot, AEKey what, int amount) {

var depositedItems = grid.getService(KnowledgeService.class)
.getStorage()
.insertItem(item, amount, Actionable.MODULATE, requestSource, false);
.insertItem(item, amount, Actionable.MODULATE, source, false);

if (depositedItems > 0) {
storage.extract(slot, what, depositedItems, Actionable.MODULATE);
Expand All @@ -253,7 +228,7 @@ private boolean acquireFromNetwork(IGrid grid, int slot, AEKey what, long amount

var acquiredItems = grid.getService(KnowledgeService.class)
.getStorage()
.extractItem(item, amount, Actionable.MODULATE, requestSource, true);
.extractItem(item, amount, Actionable.MODULATE, source, true);

if (acquiredItems > 0) {
var inserted = storage.insert(slot, what, acquiredItems, Actionable.MODULATE);
Expand Down Expand Up @@ -323,71 +298,6 @@ public void invalidateCaps() {
localInvHolder.invalidate();
}

private class Inventory extends DelegatingMEInventory {
private Inventory() {
super(storage);
}

@Override
public long insert(AEKey what, long amount, Actionable mode, IActionSource source) {
return getRequestInterfacePriority(source).isPresent() && isSameGrid(source)
? 0
: super.insert(what, amount, mode, source);
}

@Override
public long extract(AEKey what, long amount, Actionable mode, IActionSource source) {
var requestPriority = getRequestInterfacePriority(source);
return requestPriority.isPresent() && requestPriority.getAsInt() <= getPriority() && isSameGrid(source)
? 0
: super.extract(what, amount, mode, source);
}

private OptionalInt getRequestInterfacePriority(IActionSource source) {
return source.context(RequestContext.class)
.map(ctx -> OptionalInt.of(ctx.getPriority()))
.orElseGet(OptionalInt::empty);
}

private boolean isSameGrid(IActionSource source) {
return source.machine()
.map(IActionHost::getActionableNode)
.map(IGridNode::getGrid)
.orElse(null)
== mainNode.getGrid();
}

@Override
public Component getDescription() {
return host.getMainMenuIcon().getHoverName();
}
}

private class RequestSource implements IActionSource {
private final RequestContext context = new RequestContext();

@Override
public Optional<Player> player() {
return Optional.empty();
}

@Override
public Optional<IActionHost> machine() {
return Optional.of(EMCInterfaceLogic.this);
}

@Override
public <T> Optional<T> context(Class<T> key) {
return key == RequestContext.class ? Optional.of(key.cast(context)) : Optional.empty();
}
}

private class RequestContext {
public int getPriority() {
return priority;
}
}

private class StorageFilter implements AEKeyFilter {
@Override
public boolean matches(AEKey what) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

import appeng.api.networking.IGridNode;
import appeng.api.networking.IGridNodeListener;
import appeng.api.storage.ISubMenuHost;
import appeng.helpers.IConfigInvHost;
import appeng.helpers.IPriorityHost;
import appeng.helpers.externalstorage.GenericStackInv;
import appeng.menu.ISubMenu;
import appeng.menu.MenuOpener;
import appeng.menu.locator.MenuLocator;

import gripe._90.appliede.menu.EMCInterfaceMenu;

public interface EMCInterfaceLogicHost extends IPriorityHost, IConfigInvHost {
public interface EMCInterfaceLogicHost extends IConfigInvHost, ISubMenuHost {
IGridNodeListener<EMCInterfaceLogicHost> NODE_LISTENER = new IGridNodeListener<>() {
@Override
public void onSaveChanges(EMCInterfaceLogicHost host, IGridNode node) {
Expand All @@ -40,16 +40,6 @@ public void onGridChanged(EMCInterfaceLogicHost host, IGridNode node) {

EMCInterfaceLogic getInterfaceLogic();

@Override
default int getPriority() {
return getInterfaceLogic().getPriority();
}

@Override
default void setPriority(int priority) {
getInterfaceLogic().setPriority(priority);
}

@Override
default GenericStackInv getConfig() {
return getInterfaceLogic().getConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@
}
},
"widgets": {
"openPriority": {
"left": 154,
"top": 0,
"width": 16,
"height": 16
},
"amtButton1": {
"left": 8,
"top": 35,
Expand Down

0 comments on commit b54f540

Please sign in to comment.