Skip to content

Commit

Permalink
Move EMC import/export functionality to Transmutation buses instead o…
Browse files Browse the repository at this point in the history
…f regular AE2 buses
  • Loading branch information
62832 committed Jun 15, 2024
1 parent 066e79f commit 02426aa
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 226 deletions.
11 changes: 1 addition & 10 deletions src/main/java/gripe/_90/appliede/AppliedE.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import net.minecraftforge.registries.RegistryObject;

import appeng.api.behaviors.ContainerItemStrategy;
import appeng.api.behaviors.StackExportStrategy;
import appeng.api.behaviors.StackImportStrategy;
import appeng.api.client.AEKeyRendering;
import appeng.api.networking.GridServices;
import appeng.api.parts.IPart;
Expand Down Expand Up @@ -61,11 +59,9 @@
import gripe._90.appliede.integration.ae2wtlib.AE2WTIntegration;
import gripe._90.appliede.me.key.EMCKey;
import gripe._90.appliede.me.key.EMCKeyType;
import gripe._90.appliede.me.misc.EMCContainerItemStrategy;
import gripe._90.appliede.me.misc.LearnAllItemsPacket;
import gripe._90.appliede.me.service.KnowledgeService;
import gripe._90.appliede.me.strategy.EMCContainerItemStrategy;
import gripe._90.appliede.me.strategy.EMCExportStrategy;
import gripe._90.appliede.me.strategy.EMCImportStrategy;
import gripe._90.appliede.menu.EMCInterfaceMenu;
import gripe._90.appliede.menu.EMCSetStockAmountMenu;
import gripe._90.appliede.menu.TransmutationTerminalMenu;
Expand Down Expand Up @@ -95,12 +91,7 @@ public final class AppliedE {
public static final RegistryObject<Item> EMC_MODULE = ITEMS.register("emc_module", () -> {
AEKeyTypes.register(EMCKeyType.TYPE);
GridServices.register(KnowledgeService.class, KnowledgeService.class);

// external storage strategy not provided so as not to clash with service's mounted EMC storage
StackImportStrategy.register(EMCKeyType.TYPE, EMCImportStrategy::new);
StackExportStrategy.register(EMCKeyType.TYPE, EMCExportStrategy::new);
ContainerItemStrategy.register(EMCKeyType.TYPE, EMCKey.class, EMCContainerItemStrategy.INSTANCE);

return part(EMCModulePart.class, EMCModulePart::new);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
import gripe._90.appliede.integration.Addons;

public class AE2WTIntegration {
private static final Item TERMINAL;
private static final Item TERMINAL = new WTTItem();

static {
TERMINAL = new WTTItem();
GridLinkables.register(TERMINAL, WirelessTerminalItem.LINKABLE_HANDLER);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gripe._90.appliede.me.strategy;
package gripe._90.appliede.me.misc;

import org.jetbrains.annotations.Nullable;

Expand Down

This file was deleted.

This file was deleted.

135 changes: 78 additions & 57 deletions src/main/java/gripe/_90/appliede/part/EMCExportBusPart.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package gripe._90.appliede.part;

import java.util.concurrent.atomic.AtomicBoolean;

import com.google.common.primitives.Ints;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,6 +19,7 @@
import appeng.api.parts.IPartItem;
import appeng.api.parts.IPartModel;
import appeng.api.stacks.AEItemKey;
import appeng.api.storage.StorageHelper;
import appeng.core.AppEng;
import appeng.core.settings.TickRates;
import appeng.items.parts.PartModels;
Expand All @@ -30,8 +29,13 @@
import appeng.parts.automation.IOBusPart;

import gripe._90.appliede.AppliedE;
import gripe._90.appliede.me.key.EMCKey;
import gripe._90.appliede.me.key.EMCKeyType;
import gripe._90.appliede.me.service.KnowledgeService;

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

public class EMCExportBusPart extends IOBusPart {
public static final MenuType<IOBusMenu> MENU =
MenuTypeBuilder.create(IOBusMenu::new, EMCExportBusPart.class).build("emc_export_bus");
Expand All @@ -53,7 +57,7 @@ public class EMCExportBusPart extends IOBusPart {
private int nextSlot = 0;

public EMCExportBusPart(IPartItem<?> partItem) {
super(TickRates.ExportBus, AEItemKey.filter(), partItem);
super(TickRates.ExportBus, key -> AEItemKey.is(key) || key == EMCKey.BASE, partItem);
getConfigManager().registerSetting(Settings.SCHEDULING_MODE, SchedulingMode.DEFAULT);
}

Expand All @@ -69,7 +73,6 @@ public void writeToNBT(CompoundTag extra) {
extra.putInt("nextSlot", nextSlot);
}

@SuppressWarnings("DuplicatedCode")
@Override
protected boolean doBusWork(IGrid grid) {
var adjacentPos = getHost().getBlockEntity().getBlockPos().relative(getSide());
Expand All @@ -80,65 +83,83 @@ protected boolean doBusWork(IGrid grid) {
return false;
}

var doneWork = new AtomicBoolean(false);

blockEntity.getCapability(ForgeCapabilities.ITEM_HANDLER, facing).ifPresent(itemHandler -> {
var emcStorage = grid.getService(KnowledgeService.class).getStorage();
var schedulingMode = getConfigManager().getSetting(Settings.SCHEDULING_MODE);
var remaining = getOperationsPerTick();
var slot = 0;

for (slot = 0; slot < availableSlots() && remaining > 0; slot++) {
var startingSlot =
switch (schedulingMode) {
case RANDOM -> getLevel().getRandom().nextInt(availableSlots());
case ROUNDROBIN -> (nextSlot + slot) % availableSlots();
default -> slot;
};
var what = getConfig().getKey(startingSlot);

if (!(what instanceof AEItemKey item)) {
continue;
}

var stack = item.toStack(remaining);
var extracted = emcStorage.extractItem(item, remaining, Actionable.SIMULATE, source, true);
var remainder = ItemHandlerHelper.insertItem(itemHandler, stack, true);
var wasInserted = extracted - remainder.getCount();

if (wasInserted > 0) {
extracted = emcStorage.extractItem(item, remaining, Actionable.MODULATE, source, true);
remainder = ItemHandlerHelper.insertItem(itemHandler, stack, false);
wasInserted = extracted - remainder.getCount();

if (wasInserted < extracted) {
var leftover = extracted - wasInserted;
emcStorage.insertItem(item, leftover, Actionable.MODULATE, source, false);

if (leftover > 0) {
LOGGER.error(
"Storage export: adjacent block unexpectedly refused insert, voided {}x{}",
leftover,
item);
var doneWork = false;
var emcStorage = blockEntity.getCapability(PECapabilities.EMC_STORAGE_CAPABILITY, facing);
var itemHandler = blockEntity.getCapability(ForgeCapabilities.ITEM_HANDLER, facing);

var networkEmc = grid.getService(KnowledgeService.class).getStorage();
var schedulingMode = getConfigManager().getSetting(Settings.SCHEDULING_MODE);
var remaining = new AtomicInteger(getOperationsPerTick());
var slot = 0;

for (slot = 0; slot < availableSlots() && remaining.get() > 0; slot++) {
var startingSlot =
switch (schedulingMode) {
case RANDOM -> getLevel().getRandom().nextInt(availableSlots());
case ROUNDROBIN -> (nextSlot + slot) % availableSlots();
default -> slot;
};
var what = getConfig().getKey(startingSlot);

if (what == EMCKey.BASE) {
emcStorage.ifPresent(handler -> {
var rem = remaining.get() * EMCKeyType.TYPE.getAmountPerOperation();
var insertable = handler.insertEmc(rem, IEmcStorage.EmcAction.SIMULATE);
var extracted = StorageHelper.poweredExtraction(
grid.getEnergyService(),
grid.getStorageService().getInventory(),
EMCKey.BASE,
insertable,
source,
Actionable.MODULATE);

if (extracted > 0) {
handler.insertEmc(extracted, IEmcStorage.EmcAction.EXECUTE);
remaining.addAndGet((int) -Math.max(1, extracted / EMCKeyType.TYPE.getAmountPerOperation()));
}
});
} else if (what instanceof AEItemKey item) {
itemHandler.ifPresent(handler -> {
var rem = remaining.get();
var stack = item.toStack(rem);
var extracted = networkEmc.extractItem(item, rem, Actionable.SIMULATE, source, true);
var remainder = ItemHandlerHelper.insertItem(handler, stack, true);
var wasInserted = extracted - remainder.getCount();

if (wasInserted > 0) {
extracted = networkEmc.extractItem(item, rem, Actionable.MODULATE, source, true);
remainder = ItemHandlerHelper.insertItem(handler, stack, false);
wasInserted = extracted - remainder.getCount();

if (wasInserted < extracted) {
var leftover = extracted - wasInserted;
networkEmc.insertItem(item, leftover, Actionable.MODULATE, source, false);

if (leftover > 0) {
LOGGER.error(
"Storage export: adjacent block unexpectedly refused insert, voided {}x{}",
leftover,
item);
}
}
}
}

if (wasInserted > 0) {
remaining -= Ints.saturatedCast(wasInserted);
}
if (wasInserted > 0) {
remaining.addAndGet(-(int) wasInserted);
}
});
}
}

if (remaining < getOperationsPerTick()) {
if (schedulingMode == SchedulingMode.ROUNDROBIN) {
nextSlot = (nextSlot + slot) % availableSlots();
}

doneWork.set(true);
if (remaining.get() < getOperationsPerTick()) {
if (schedulingMode == SchedulingMode.ROUNDROBIN) {
nextSlot = (nextSlot + slot) % availableSlots();
}
});

return doneWork.get();
doneWork = true;
}

return doneWork;
}

@Override
Expand Down
Loading

0 comments on commit 02426aa

Please sign in to comment.