diff --git a/src/main/java/gripe/_90/appliede/AppliedE.java b/src/main/java/gripe/_90/appliede/AppliedE.java index 1d40e72..881363f 100644 --- a/src/main/java/gripe/_90/appliede/AppliedE.java +++ b/src/main/java/gripe/_90/appliede/AppliedE.java @@ -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; @@ -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; @@ -95,12 +91,7 @@ public final class AppliedE { public static final RegistryObject 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); }); diff --git a/src/main/java/gripe/_90/appliede/integration/ae2wtlib/AE2WTIntegration.java b/src/main/java/gripe/_90/appliede/integration/ae2wtlib/AE2WTIntegration.java index eaa585d..6dd269a 100644 --- a/src/main/java/gripe/_90/appliede/integration/ae2wtlib/AE2WTIntegration.java +++ b/src/main/java/gripe/_90/appliede/integration/ae2wtlib/AE2WTIntegration.java @@ -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); } diff --git a/src/main/java/gripe/_90/appliede/me/strategy/EMCContainerItemStrategy.java b/src/main/java/gripe/_90/appliede/me/misc/EMCContainerItemStrategy.java similarity index 98% rename from src/main/java/gripe/_90/appliede/me/strategy/EMCContainerItemStrategy.java rename to src/main/java/gripe/_90/appliede/me/misc/EMCContainerItemStrategy.java index f5b0ca8..307d320 100644 --- a/src/main/java/gripe/_90/appliede/me/strategy/EMCContainerItemStrategy.java +++ b/src/main/java/gripe/_90/appliede/me/misc/EMCContainerItemStrategy.java @@ -1,4 +1,4 @@ -package gripe._90.appliede.me.strategy; +package gripe._90.appliede.me.misc; import org.jetbrains.annotations.Nullable; diff --git a/src/main/java/gripe/_90/appliede/me/strategy/EMCExportStrategy.java b/src/main/java/gripe/_90/appliede/me/strategy/EMCExportStrategy.java deleted file mode 100644 index 105c441..0000000 --- a/src/main/java/gripe/_90/appliede/me/strategy/EMCExportStrategy.java +++ /dev/null @@ -1,82 +0,0 @@ -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; - -import appeng.api.behaviors.StackExportStrategy; -import appeng.api.behaviors.StackTransferContext; -import appeng.api.config.Actionable; -import appeng.api.stacks.AEKey; -import appeng.api.storage.StorageHelper; - -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 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 be = level.getBlockEntity(pos); - - if (be == null) { - return 0; - } - - var transferred = new AtomicLong(0); - - be.getCapability(PECapabilities.EMC_STORAGE_CAPABILITY).ifPresent(emcStorage -> { - var insertable = emcStorage.insertEmc(maxAmount, IEmcStorage.EmcAction.SIMULATE); - var extracted = StorageHelper.poweredExtraction( - context.getEnergySource(), - context.getInternalStorage().getInventory(), - EMCKey.BASE, - insertable, - context.getActionSource(), - Actionable.MODULATE); - - if (extracted > 0) { - emcStorage.insertEmc(extracted, IEmcStorage.EmcAction.EXECUTE); - } - - transferred.addAndGet(extracted); - }); - - return transferred.get(); - } - - @Override - public long push(AEKey what, long maxAmount, Actionable mode) { - if (!(what instanceof EMCKey)) { - return 0; - } - - var be = level.getBlockEntity(pos); - - 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); - } - - transferred.addAndGet(inserted); - }); - - return transferred.get(); - } -} diff --git a/src/main/java/gripe/_90/appliede/me/strategy/EMCImportStrategy.java b/src/main/java/gripe/_90/appliede/me/strategy/EMCImportStrategy.java deleted file mode 100644 index 1597901..0000000 --- a/src/main/java/gripe/_90/appliede/me/strategy/EMCImportStrategy.java +++ /dev/null @@ -1,54 +0,0 @@ -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; - -import appeng.api.behaviors.StackImportStrategy; -import appeng.api.behaviors.StackTransferContext; -import appeng.api.config.Actionable; -import appeng.api.storage.StorageHelper; - -import gripe._90.appliede.me.key.EMCKey; -import gripe._90.appliede.me.key.EMCKeyType; - -import moze_intel.projecte.api.capabilities.PECapabilities; -import moze_intel.projecte.api.capabilities.block_entity.IEmcStorage; - -@SuppressWarnings("UnstableApiUsage") -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 be = level.getBlockEntity(pos); - - if (be == null) { - return false; - } - - var remainingTransfer = context.getOperationsRemaining() * EMCKeyType.TYPE.getAmountPerOperation(); - var transferred = new AtomicLong(0); - - be.getCapability(PECapabilities.EMC_STORAGE_CAPABILITY).ifPresent(emcStorage -> { - var inserted = StorageHelper.poweredInsert( - context.getEnergySource(), - context.getInternalStorage().getInventory(), - EMCKey.BASE, - Math.min(remainingTransfer, emcStorage.getStoredEmc()), - context.getActionSource(), - Actionable.MODULATE); - emcStorage.extractEmc(inserted, IEmcStorage.EmcAction.EXECUTE); - - var opsUsed = Math.max(1, inserted / EMCKeyType.TYPE.getAmountPerOperation()); - context.reduceOperationsRemaining(opsUsed); - transferred.set(inserted); - }); - - return transferred.get() > 0; - } -} diff --git a/src/main/java/gripe/_90/appliede/part/EMCExportBusPart.java b/src/main/java/gripe/_90/appliede/part/EMCExportBusPart.java index f522929..6a78178 100644 --- a/src/main/java/gripe/_90/appliede/part/EMCExportBusPart.java +++ b/src/main/java/gripe/_90/appliede/part/EMCExportBusPart.java @@ -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; @@ -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; @@ -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 MENU = MenuTypeBuilder.create(IOBusMenu::new, EMCExportBusPart.class).build("emc_export_bus"); @@ -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); } @@ -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()); @@ -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 diff --git a/src/main/java/gripe/_90/appliede/part/EMCImportBusPart.java b/src/main/java/gripe/_90/appliede/part/EMCImportBusPart.java index 6bf9510..e035a94 100644 --- a/src/main/java/gripe/_90/appliede/part/EMCImportBusPart.java +++ b/src/main/java/gripe/_90/appliede/part/EMCImportBusPart.java @@ -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 net.minecraft.resources.ResourceLocation; import net.minecraft.world.inventory.MenuType; @@ -14,6 +12,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.definitions.AEItems; import appeng.core.settings.TickRates; @@ -25,8 +24,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 EMCImportBusPart extends IOBusPart { public static final MenuType MENU = MenuTypeBuilder.create(IOBusMenu::new, EMCImportBusPart.class).build("emc_import_bus"); @@ -44,7 +48,7 @@ public class EMCImportBusPart extends IOBusPart { new PartModel(MODEL_BASE, AppEng.makeId("part/import_bus_has_channel")); public EMCImportBusPart(IPartItem partItem) { - super(TickRates.ImportBus, AEItemKey.filter(), partItem); + super(TickRates.ImportBus, key -> AEItemKey.is(key) || key == EMCKey.BASE, partItem); } @Override @@ -52,7 +56,6 @@ protected MenuType getMenuType() { return MENU; } - @SuppressWarnings("DuplicatedCode") @Override protected boolean doBusWork(IGrid grid) { var adjacentPos = getHost().getBlockEntity().getBlockPos().relative(getSide()); @@ -63,15 +66,35 @@ protected boolean doBusWork(IGrid grid) { return false; } - var doneWork = new AtomicBoolean(false); + var emcStorage = blockEntity.getCapability(PECapabilities.EMC_STORAGE_CAPABILITY, facing); + var itemHandler = blockEntity.getCapability(ForgeCapabilities.ITEM_HANDLER, facing); + var doneWork = false; + + var networkEmc = grid.getService(KnowledgeService.class).getStorage(); + var remaining = new AtomicInteger(getOperationsPerTick()); - blockEntity.getCapability(ForgeCapabilities.ITEM_HANDLER, facing).ifPresent(itemHandler -> { - var emcStorage = grid.getService(KnowledgeService.class).getStorage(); - var adjacentStorage = ExternalStorageFacade.of(itemHandler); - var remaining = getOperationsPerTick(); + emcStorage.ifPresent(handler -> { + if (!getFilter().isEmpty() && getFilter().isListed(EMCKey.BASE) == isUpgradedWith(AEItems.INVERTER_CARD)) { + return; + } + + var emcRemaining = remaining.get() * EMCKeyType.TYPE.getAmountPerOperation(); + var inserted = StorageHelper.poweredInsert( + grid.getEnergyService(), + grid.getStorageService().getInventory(), + EMCKey.BASE, + Math.min(emcRemaining, handler.getStoredEmc()), + source, + Actionable.MODULATE); + handler.extractEmc(inserted, IEmcStorage.EmcAction.EXECUTE); + remaining.addAndGet((int) -Math.max(1, inserted / EMCKeyType.TYPE.getAmountPerOperation())); + }); - for (var slot = 0; slot < itemHandler.getSlots() && remaining > 0; slot++) { - var item = AEItemKey.of(itemHandler.getStackInSlot(slot)); + itemHandler.ifPresent(handler -> { + var adjacentStorage = ExternalStorageFacade.of(handler); + + for (var slot = 0; slot < handler.getSlots() && remaining.get() > 0; slot++) { + var item = AEItemKey.of(handler.getStackInSlot(slot)); if (item == null) { continue; @@ -81,22 +104,22 @@ protected boolean doBusWork(IGrid grid) { continue; } - var amount = adjacentStorage.extract(item, remaining, Actionable.SIMULATE, source); + var amount = adjacentStorage.extract(item, remaining.get(), Actionable.SIMULATE, source); if (amount > 0) { var mayLearn = isUpgradedWith(AppliedE.LEARNING_CARD.get()); - amount = emcStorage.insertItem(item, amount, Actionable.MODULATE, source, mayLearn); + amount = networkEmc.insertItem(item, amount, Actionable.MODULATE, source, mayLearn); adjacentStorage.extract(item, amount, Actionable.MODULATE, source); - remaining -= Ints.saturatedCast(amount); + remaining.addAndGet(-(int) amount); } } - - if (remaining < getOperationsPerTick()) { - doneWork.set(true); - } }); - return doneWork.get(); + if (remaining.get() < getOperationsPerTick()) { + doneWork = true; + } + + return doneWork; } @Override