From 1944864b925beae516621be1eaa520cfee7da8e7 Mon Sep 17 00:00:00 2001 From: Ramid Khan Date: Wed, 21 Aug 2024 13:57:01 +1000 Subject: [PATCH] Update for Mekanism chemical variant unification Update to 1.21.1 --- build.gradle | 6 +- gradle.properties | 16 +- src/main/java/me/ramidzkh/mekae2/AMMenus.java | 2 +- .../ramidzkh/mekae2/AppliedMekanistics.java | 18 +- .../me/ramidzkh/mekae2/MekCapabilities.java | 17 +- .../ae2/ChemicalContainerItemStrategy.java | 114 ++------ .../mekae2/ae2/ChemicalP2PTunnelPart.java | 261 +++++------------- .../ae2/GenericStackChemicalStorage.java | 83 ++---- .../me/ramidzkh/mekae2/ae2/MekanismKey.java | 82 +++--- .../ramidzkh/mekae2/ae2/MekanismKeyType.java | 9 +- .../ae2/MultipleCapabilityP2PTunnelPart.java | 212 -------------- .../ae2/stack/ChemicalHandlerFacade.java | 50 +--- .../MekanismExternalStorageStrategy.java | 26 +- .../stack/MekanismStackExportStrategy.java | 12 +- .../stack/MekanismStackImportStrategy.java | 96 +++---- .../mekae2/integration/jade/AMJadePlugin.java | 14 +- .../ramidzkh/mekae2/util/ChemicalBridge.java | 12 - .../me/ramidzkh/mekae2/util/package-info.java | 7 - 18 files changed, 230 insertions(+), 807 deletions(-) delete mode 100644 src/main/java/me/ramidzkh/mekae2/ae2/MultipleCapabilityP2PTunnelPart.java delete mode 100644 src/main/java/me/ramidzkh/mekae2/util/ChemicalBridge.java delete mode 100644 src/main/java/me/ramidzkh/mekae2/util/package-info.java diff --git a/build.gradle b/build.gradle index e346dc9..5918497 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,12 @@ plugins { - id("net.neoforged.moddev") version "0.1.126" + id("net.neoforged.moddev") version "1.0.17" id("com.matthewprenger.cursegradle") version "1.4.0" id("com.diffplug.spotless") version "6.25.0" id("com.modrinth.minotaur") version "2.8.7" } group = "me.ramidzkh" -version = System.getenv("APPMEK_VERSION") ?: "0.0.0" +version = System.getenv("APPMEK_VERSION") ?: "1.6.0" repositories { maven { @@ -52,7 +52,7 @@ dependencies { compileOnly("mekanism:Mekanism:${minecraft_version}-${mekanism_version}:api") runtimeOnly("mekanism:Mekanism:${minecraft_version}-${mekanism_version}:all") - implementation("dev.emi:emi-neoforge:${project.emi_version}+${project.minecraft_version}") + implementation("dev.emi:emi-neoforge:${project.emi_version}") compileOnly("curse.maven:jade-324717:${jade_id}") } diff --git a/gradle.properties b/gradle.properties index d7caa40..3ddc5cd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,14 +1,14 @@ -minecraft_version=1.21 -neoforge_version=21.0.87-beta -ae2_version=19.0.11-alpha -mekanism_version=10.6.5.52 -emi_version=1.1.10 -jade_id=5493270 +minecraft_version=1.21.1 +neoforge_version=21.1.22 +ae2_version=19.0.20-beta +mekanism_version=10.7.0.55 +emi_version=1.1.12+1.21 +jade_id=5427817 loader_version_range=[1,) neo_version_range=[21.0,) -ae2_version_range=[19.0.11-alpha,20.0.0) -mekanism_version_range=[10.6.5,11-) +ae2_version_range=[19.0.20-beta,20.0.0) +mekanism_version_range=[10.7.0,11-) org.gradle.caching=true org.gradle.configuration-cache=true diff --git a/src/main/java/me/ramidzkh/mekae2/AMMenus.java b/src/main/java/me/ramidzkh/mekae2/AMMenus.java index a6ceb35..d686b3c 100644 --- a/src/main/java/me/ramidzkh/mekae2/AMMenus.java +++ b/src/main/java/me/ramidzkh/mekae2/AMMenus.java @@ -14,7 +14,7 @@ public class AMMenus { public static final MenuType PORTABLE_CHEMICAL_CELL_TYPE = MenuTypeBuilder .create(MEStorageMenu::new, IPortableTerminal.class) - .build("portable_chemical_cell"); + .build(AppliedMekanistics.id("portable_chemical_cell")); @SuppressWarnings("RedundantTypeArguments") public static void initialize(IEventBus bus) { diff --git a/src/main/java/me/ramidzkh/mekae2/AppliedMekanistics.java b/src/main/java/me/ramidzkh/mekae2/AppliedMekanistics.java index 5bb54a4..066f3fc 100644 --- a/src/main/java/me/ramidzkh/mekae2/AppliedMekanistics.java +++ b/src/main/java/me/ramidzkh/mekae2/AppliedMekanistics.java @@ -85,27 +85,15 @@ public AppliedMekanistics(IEventBus bus) { } private void registerPartCapabilities(RegisterPartCapabilitiesEvent event) { - event.register(MekCapabilities.GAS.block(), (part, context) -> part.getGasHandler(), - ChemicalP2PTunnelPart.class); - event.register(MekCapabilities.INFUSION.block(), (part, context) -> part.getInfuseHandler(), - ChemicalP2PTunnelPart.class); - event.register(MekCapabilities.PIGMENT.block(), (part, context) -> part.getPigmentHandler(), - ChemicalP2PTunnelPart.class); - event.register(MekCapabilities.SLURRY.block(), (part, context) -> part.getSlurryHandler(), + event.register(MekCapabilities.CHEMICAL.block(), (part, context) -> part.getExposedApi(), ChemicalP2PTunnelPart.class); } private void registerGenericAdapters(RegisterCapabilitiesEvent event) { for (var block : BuiltInRegistries.BLOCK) { if (event.isBlockRegistered(AECapabilities.GENERIC_INTERNAL_INV, block)) { - registerGenericInvAdapter(event, block, MekCapabilities.GAS.block(), - GenericStackChemicalStorage.OfGas::new); - registerGenericInvAdapter(event, block, MekCapabilities.INFUSION.block(), - GenericStackChemicalStorage.OfInfusion::new); - registerGenericInvAdapter(event, block, MekCapabilities.PIGMENT.block(), - GenericStackChemicalStorage.OfPigment::new); - registerGenericInvAdapter(event, block, MekCapabilities.SLURRY.block(), - GenericStackChemicalStorage.OfSlurry::new); + registerGenericInvAdapter(event, block, MekCapabilities.CHEMICAL.block(), + GenericStackChemicalStorage::new); } } } diff --git a/src/main/java/me/ramidzkh/mekae2/MekCapabilities.java b/src/main/java/me/ramidzkh/mekae2/MekCapabilities.java index 8d168b3..03a8fa4 100644 --- a/src/main/java/me/ramidzkh/mekae2/MekCapabilities.java +++ b/src/main/java/me/ramidzkh/mekae2/MekCapabilities.java @@ -1,7 +1,5 @@ package me.ramidzkh.mekae2; -import java.util.List; - import org.jetbrains.annotations.Nullable; import net.minecraft.core.Direction; @@ -11,25 +9,16 @@ import mekanism.api.MekanismAPI; import mekanism.api.chemical.IChemicalHandler; -import mekanism.api.chemical.gas.IGasHandler; -import mekanism.api.chemical.infuse.IInfusionHandler; -import mekanism.api.chemical.pigment.IPigmentHandler; -import mekanism.api.chemical.slurry.ISlurryHandler; public class MekCapabilities { private MekCapabilities() { } - public static final CapSet GAS = new CapSet<>(rl("gas_handler"), IGasHandler.class); - public static final CapSet INFUSION = new CapSet<>(rl("infusion_handler"), - IInfusionHandler.class); - public static final CapSet PIGMENT = new CapSet<>(rl("pigment_handler"), IPigmentHandler.class); - public static final CapSet SLURRY = new CapSet<>(rl("slurry_handler"), ISlurryHandler.class); - - public static final List> HANDLERS = List.of(GAS, INFUSION, PIGMENT, SLURRY); + public static final CapSet CHEMICAL = new CapSet<>(rl("chemical_handler"), + IChemicalHandler.class); - public record CapSet>(BlockCapability block, + public record CapSet(BlockCapability block, ItemCapability item) { public CapSet(ResourceLocation name, Class handlerClass) { this(BlockCapability.createSided(name, handlerClass), ItemCapability.createVoid(name, handlerClass)); diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalContainerItemStrategy.java b/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalContainerItemStrategy.java index 3b5ed5c..ee35e04 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalContainerItemStrategy.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalContainerItemStrategy.java @@ -10,10 +10,6 @@ import me.ramidzkh.mekae2.MekCapabilities; import mekanism.api.Action; -import mekanism.api.chemical.gas.GasStack; -import mekanism.api.chemical.infuse.InfusionStack; -import mekanism.api.chemical.pigment.PigmentStack; -import mekanism.api.chemical.slurry.SlurryStack; import appeng.api.behaviors.ContainerItemStrategy; import appeng.api.config.Actionable; @@ -29,16 +25,14 @@ public GenericStack getContainedStack(ItemStack stack) { return null; } - for (var capability : MekCapabilities.HANDLERS) { - var handler = stack.getCapability(capability.item()); + var handler = stack.getCapability(MekCapabilities.CHEMICAL.item()); - if (handler != null) { - var chemical = handler.extractChemical(Long.MAX_VALUE, Action.SIMULATE); - var key = MekanismKey.of(chemical); + if (handler != null) { + var chemical = handler.extractChemical(Long.MAX_VALUE, Action.SIMULATE); + var key = MekanismKey.of(chemical); - if (key != null) { - return new GenericStack(key, chemical.getAmount()); - } + if (key != null) { + return new GenericStack(key, chemical.getAmount()); } } @@ -50,10 +44,8 @@ public GenericStack getContainedStack(ItemStack stack) { public ItemStack findCarriedContext(Player player, AbstractContainerMenu menu) { var carried = menu.getCarried(); - for (var capability : MekCapabilities.HANDLERS) { - if (carried.getCapability(capability.item()) != null) { - return carried; - } + if (carried.getCapability(MekCapabilities.CHEMICAL.item()) != null) { + return carried; } return null; @@ -63,10 +55,8 @@ public ItemStack findCarriedContext(Player player, AbstractContainerMenu menu) { public @Nullable ItemStack findPlayerSlotContext(Player player, int slot) { var carried = player.getInventory().getItem(slot); - for (var capability : MekCapabilities.HANDLERS) { - if (carried.getCapability(capability.item()) != null) { - return carried; - } + if (carried.getCapability(MekCapabilities.CHEMICAL.item()) != null) { + return carried; } return null; @@ -74,86 +64,26 @@ public ItemStack findCarriedContext(Player player, AbstractContainerMenu menu) { @Override public long extract(ItemStack context, MekanismKey what, long amount, Actionable mode) { - var stack = what.withAmount(amount); - var action = Action.fromFluidAction(mode.getFluidAction()); - - if (stack instanceof GasStack gas) { - var handler = context.getCapability(MekCapabilities.GAS.item()); - - if (handler != null) { - return handler.extractChemical(gas, action).getAmount(); - } else { - return 0L; - } - } else if (stack instanceof InfusionStack infusion) { - var handler = context.getCapability(MekCapabilities.INFUSION.item()); + var handler = context.getCapability(MekCapabilities.CHEMICAL.item()); - if (handler != null) { - return handler.extractChemical(infusion, action).getAmount(); - } else { - return 0L; - } - } else if (stack instanceof PigmentStack pigment) { - var handler = context.getCapability(MekCapabilities.PIGMENT.item()); - - if (handler != null) { - return handler.extractChemical(pigment, action).getAmount(); - } else { - return 0L; - } - } else if (stack instanceof SlurryStack slurry) { - var handler = context.getCapability(MekCapabilities.SLURRY.item()); - - if (handler != null) { - return handler.extractChemical(slurry, action).getAmount(); - } else { - return 0L; - } - } else { - throw new UnsupportedOperationException(); + if (handler == null) { + return 0L; } + + return handler.extractChemical(what.withAmount(amount), Action.fromFluidAction(mode.getFluidAction())) + .getAmount(); } @Override public long insert(ItemStack context, MekanismKey what, long amount, Actionable mode) { - var stack = what.withAmount(amount); - var action = Action.fromFluidAction(mode.getFluidAction()); - - if (stack instanceof GasStack gas) { - var handler = context.getCapability(MekCapabilities.GAS.item()); - - if (handler != null) { - return amount - handler.insertChemical(gas, action).getAmount(); - } else { - return 0L; - } - } else if (stack instanceof InfusionStack infusion) { - var handler = context.getCapability(MekCapabilities.INFUSION.item()); - - if (handler != null) { - return amount - handler.insertChemical(infusion, action).getAmount(); - } else { - return 0L; - } - } else if (stack instanceof PigmentStack pigment) { - var handler = context.getCapability(MekCapabilities.PIGMENT.item()); - - if (handler != null) { - return amount - handler.insertChemical(pigment, action).getAmount(); - } else { - return 0L; - } - } else if (stack instanceof SlurryStack slurry) { - var handler = context.getCapability(MekCapabilities.SLURRY.item()); + var handler = context.getCapability(MekCapabilities.CHEMICAL.item()); - if (handler != null) { - return amount - handler.insertChemical(slurry, action).getAmount(); - } else { - return 0L; - } - } else { - throw new UnsupportedOperationException(); + if (handler == null) { + return 0L; } + + return amount - handler.insertChemical(what.withAmount(amount), Action.fromFluidAction(mode.getFluidAction())) + .getAmount(); } @Override diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalP2PTunnelPart.java b/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalP2PTunnelPart.java index 6bfa622..339027b 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalP2PTunnelPart.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalP2PTunnelPart.java @@ -2,330 +2,197 @@ import java.util.List; -import net.minecraft.core.Direction; -import net.neoforged.neoforge.capabilities.BlockCapability; - import me.ramidzkh.mekae2.AppliedMekanistics; import me.ramidzkh.mekae2.MekCapabilities; -import me.ramidzkh.mekae2.util.ChemicalBridge; import mekanism.api.Action; -import mekanism.api.chemical.Chemical; import mekanism.api.chemical.ChemicalStack; import mekanism.api.chemical.IChemicalHandler; -import mekanism.api.chemical.gas.Gas; -import mekanism.api.chemical.gas.GasStack; -import mekanism.api.chemical.gas.IGasHandler; -import mekanism.api.chemical.infuse.IInfusionHandler; -import mekanism.api.chemical.infuse.InfuseType; -import mekanism.api.chemical.infuse.InfusionStack; -import mekanism.api.chemical.pigment.IPigmentHandler; -import mekanism.api.chemical.pigment.Pigment; -import mekanism.api.chemical.pigment.PigmentStack; -import mekanism.api.chemical.slurry.ISlurryHandler; -import mekanism.api.chemical.slurry.Slurry; -import mekanism.api.chemical.slurry.SlurryStack; import appeng.api.config.PowerUnit; import appeng.api.parts.IPartItem; import appeng.api.parts.IPartModel; +import appeng.api.stacks.AEKeyType; import appeng.items.parts.PartModels; +import appeng.parts.p2p.CapabilityP2PTunnelPart; import appeng.parts.p2p.P2PModels; -public class ChemicalP2PTunnelPart extends MultipleCapabilityP2PTunnelPart { +public class ChemicalP2PTunnelPart extends CapabilityP2PTunnelPart { private static final P2PModels MODELS = new P2PModels(AppliedMekanistics.id("part/chemical_p2p_tunnel")); - - public ChemicalP2PTunnelPart(IPartItem partItem) { - super(partItem, self -> List.of( - new CapabilitySet<>(MekCapabilities.GAS.block(), new InputChemicalHandler.OfGas(self), - new OutputChemicalHandler.OfGas(self), NullChemicalHandler.GAS), - new CapabilitySet<>(MekCapabilities.INFUSION.block(), - new InputChemicalHandler.OfInfusion(self), new OutputChemicalHandler.OfInfusion(self), - NullChemicalHandler.INFUSION), - new CapabilitySet<>(MekCapabilities.PIGMENT.block(), - new InputChemicalHandler.OfPigment(self), new OutputChemicalHandler.OfPigment(self), - NullChemicalHandler.PIGMENT), - new CapabilitySet<>(MekCapabilities.SLURRY.block(), new InputChemicalHandler.OfSlurry(self), - new OutputChemicalHandler.OfSlurry(self), NullChemicalHandler.SLURRY))); - } + private static final IChemicalHandler NULL_FLUID_HANDLER = new NullFluidHandler(); @PartModels public static List getModels() { return MODELS.getModels(); } - @Override - public IPartModel getStaticModels() { - return MODELS.getModel(this.isPowered(), this.isActive()); + public ChemicalP2PTunnelPart(IPartItem partItem) { + super(partItem, MekCapabilities.CHEMICAL.block()); + inputHandler = new InputFluidHandler(); + outputHandler = new OutputFluidHandler(); + emptyHandler = NULL_FLUID_HANDLER; } @Override - protected float getPowerDrainPerTick() { - return 4.0f; - } - - public IGasHandler getGasHandler() { - return getCapability(MekCapabilities.GAS.block()); - } - - public IInfusionHandler getInfuseHandler() { - return getCapability(MekCapabilities.INFUSION.block()); - } - - public IPigmentHandler getPigmentHandler() { - return getCapability(MekCapabilities.PIGMENT.block()); - } - - public ISlurryHandler getSlurryHandler() { - return getCapability(MekCapabilities.SLURRY.block()); + public IPartModel getStaticModels() { + return MODELS.getModel(this.isPowered(), this.isActive()); } - private static abstract sealed class InputChemicalHandler, S extends ChemicalStack, H extends IChemicalHandler> - implements IChemicalHandler { - private final ChemicalP2PTunnelPart part; - private final BlockCapability capability; - - protected InputChemicalHandler(ChemicalP2PTunnelPart part, BlockCapability capability) { - this.part = part; - this.capability = capability; - } + private class InputFluidHandler implements IChemicalHandler { @Override - public int getTanks() { + public int getChemicalTanks() { return 1; } @Override - public S getChemicalInTank(int tank) { - return getEmptyStack(); + public ChemicalStack getChemicalInTank(int tank) { + return ChemicalStack.EMPTY; } @Override - public void setChemicalInTank(int tank, S stack) { + public void setChemicalInTank(int tank, ChemicalStack stack) { throw new UnsupportedOperationException(); } @Override - public long getTankCapacity(int tank) { - return Long.MAX_VALUE; + public long getChemicalTankCapacity(int tank) { + return Integer.MAX_VALUE; } @Override - public boolean isValid(int tank, S stack) { + public boolean isValid(int tank, ChemicalStack stack) { return true; } @Override - public S insertChemical(int tank, S stack, Action action) { - var total = 0L; - - var outputTunnels = part.getOutputs().size(); + public ChemicalStack insertChemical(int tank, ChemicalStack stack, Action action) { + var outputTunnels = getOutputs().size(); var amount = stack.getAmount(); - if (outputTunnels == 0 || amount == 0) { + if (outputTunnels == 0 || stack.isEmpty()) { return stack; } var amountPerOutput = amount / outputTunnels; - var overflow = amountPerOutput == 0 ? amount : amount % amountPerOutput; + var overflow = amountPerOutput == 0L ? amount : amount % amountPerOutput; + var total = 0L; - for (var target : part.getOutputs()) { - try (var capabilityGuard = target.getAdjacentCapability(capability)) { + for (var target : getOutputs()) { + try (var capabilityGuard = target.getAdjacentCapability()) { var output = capabilityGuard.get(); var toSend = amountPerOutput + overflow; - overflow = output.insertChemical(ChemicalBridge.withAmount(stack, toSend), action).getAmount(); + overflow = output.insertChemical(stack.copyWithAmount(toSend), action).getAmount(); total += toSend - overflow; } } - if (action == Action.EXECUTE) { - part.queueTunnelDrain(PowerUnit.AE, (double) total / MekanismKeyType.TYPE.getAmountPerOperation()); + if (action.execute()) { + queueTunnelDrain(PowerUnit.FE, (double) total / MekanismKeyType.TYPE.getAmountPerOperation()); } - return ChemicalBridge.withAmount(stack, stack.getAmount() - total); + return stack.copyWithAmount(amount - total); } @Override - public S extractChemical(int tank, long maxAmount, Action action) { - return getEmptyStack(); - } - - private static final class OfGas extends InputChemicalHandler - implements IGasHandler { - private OfGas(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.GAS.block()); - } - } - - private static final class OfInfusion extends InputChemicalHandler - implements IInfusionHandler { - private OfInfusion(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.INFUSION.block()); - } - } - - private static final class OfPigment extends InputChemicalHandler - implements IPigmentHandler { - private OfPigment(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.PIGMENT.block()); - } - } - - private static final class OfSlurry extends InputChemicalHandler - implements ISlurryHandler { - private OfSlurry(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.SLURRY.block()); - } + public ChemicalStack extractChemical(int tank, long amount, Action action) { + return ChemicalStack.EMPTY; } } - private static abstract sealed class OutputChemicalHandler, S extends ChemicalStack, H extends IChemicalHandler> - implements IChemicalHandler { - private final ChemicalP2PTunnelPart part; - private final BlockCapability capability; - - private OutputChemicalHandler(ChemicalP2PTunnelPart part, BlockCapability capability) { - this.part = part; - this.capability = capability; - } + private class OutputFluidHandler implements IChemicalHandler { @Override - public int getTanks() { - try (var input = part.getInputCapability(capability)) { - return input.get().getTanks(); + public int getChemicalTanks() { + try (var input = getInputCapability()) { + return input.get().getChemicalTanks(); } } @Override - public S getChemicalInTank(int tank) { - try (var input = part.getInputCapability(capability)) { + public ChemicalStack getChemicalInTank(int tank) { + try (var input = getInputCapability()) { return input.get().getChemicalInTank(tank); } } @Override - public void setChemicalInTank(int tank, S stack) { - throw new UnsupportedOperationException(); + public void setChemicalInTank(int tank, ChemicalStack stack) { + try (var input = getInputCapability()) { + input.get().setChemicalInTank(tank, stack); + } } @Override - public long getTankCapacity(int tank) { - try (var input = part.getInputCapability(capability)) { - return input.get().getTankCapacity(tank); + public long getChemicalTankCapacity(int tank) { + try (var input = getInputCapability()) { + return input.get().getChemicalTankCapacity(tank); } } @Override - public boolean isValid(int tank, S stack) { - try (var input = part.getInputCapability(capability)) { + public boolean isValid(int tank, ChemicalStack stack) { + try (var input = getInputCapability()) { return input.get().isValid(tank, stack); } } @Override - public S insertChemical(int tank, S stack, Action action) { + public ChemicalStack insertChemical(int tank, ChemicalStack stack, Action action) { return stack; } @Override - public S extractChemical(int tank, long maxAmount, Action action) { - try (var input = part.getInputCapability(capability)) { - var result = input.get().extractChemical(tank, maxAmount, action); + public ChemicalStack extractChemical(int tank, long amount, Action action) { + try (var input = getInputCapability()) { + var result = input.get().extractChemical(tank, amount, action); if (action.execute()) { - part.queueTunnelDrain(PowerUnit.AE, - (float) result.getAmount() / MekanismKeyType.TYPE.getAmountPerOperation()); + queueTunnelDrain(PowerUnit.FE, + (double) result.getAmount() / AEKeyType.fluids().getAmountPerOperation()); } return result; } } - - private static final class OfGas extends OutputChemicalHandler - implements IGasHandler { - private OfGas(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.GAS.block()); - } - } - - private static final class OfInfusion extends OutputChemicalHandler - implements IInfusionHandler { - private OfInfusion(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.INFUSION.block()); - } - } - - private static final class OfPigment extends OutputChemicalHandler - implements IPigmentHandler { - private OfPigment(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.PIGMENT.block()); - } - } - - private static final class OfSlurry extends OutputChemicalHandler - implements ISlurryHandler { - private OfSlurry(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.SLURRY.block()); - } - } } - private static abstract sealed class NullChemicalHandler, S extends ChemicalStack> - implements IChemicalHandler { - - private static final IGasHandler GAS = new OfGas(); - private static final IInfusionHandler INFUSION = new OfInfusion(); - private static final IPigmentHandler PIGMENT = new OfPigment(); - private static final ISlurryHandler SLURRY = new OfSlurry(); - + private static class NullFluidHandler implements IChemicalHandler { @Override - public int getTanks() { + public int getChemicalTanks() { return 0; } @Override - public S getChemicalInTank(int tank) { - return getEmptyStack(); + public ChemicalStack getChemicalInTank(int tank) { + return ChemicalStack.EMPTY; } @Override - public void setChemicalInTank(int tank, S stack) { + public void setChemicalInTank(int tank, ChemicalStack stack) { throw new UnsupportedOperationException(); } @Override - public long getTankCapacity(int tank) { + public long getChemicalTankCapacity(int tank) { return 0; } @Override - public boolean isValid(int tank, S stack) { + public boolean isValid(int tank, ChemicalStack stack) { return false; } @Override - public S insertChemical(int tank, S stack, Action action) { + public ChemicalStack insertChemical(int tank, ChemicalStack stack, Action action) { return stack; } @Override - public S extractChemical(int tank, long maxAmount, Action action) { - return getEmptyStack(); - } - - private static final class OfGas extends NullChemicalHandler implements IGasHandler { - } - - private static final class OfInfusion extends NullChemicalHandler - implements IInfusionHandler { - } - - private static final class OfPigment extends NullChemicalHandler - implements IPigmentHandler { - } - - private static final class OfSlurry extends NullChemicalHandler implements ISlurryHandler { + public ChemicalStack extractChemical(int tank, long amount, Action action) { + return ChemicalStack.EMPTY; } } + } diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/GenericStackChemicalStorage.java b/src/main/java/me/ramidzkh/mekae2/ae2/GenericStackChemicalStorage.java index 6ab63de..477e707 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/GenericStackChemicalStorage.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/GenericStackChemicalStorage.java @@ -1,70 +1,52 @@ package me.ramidzkh.mekae2.ae2; -import me.ramidzkh.mekae2.util.ChemicalBridge; import mekanism.api.Action; -import mekanism.api.chemical.Chemical; import mekanism.api.chemical.ChemicalStack; import mekanism.api.chemical.IChemicalHandler; -import mekanism.api.chemical.gas.Gas; -import mekanism.api.chemical.gas.GasStack; -import mekanism.api.chemical.gas.IGasHandler; -import mekanism.api.chemical.infuse.IInfusionHandler; -import mekanism.api.chemical.infuse.InfuseType; -import mekanism.api.chemical.infuse.InfusionStack; -import mekanism.api.chemical.pigment.IPigmentHandler; -import mekanism.api.chemical.pigment.Pigment; -import mekanism.api.chemical.pigment.PigmentStack; -import mekanism.api.chemical.slurry.ISlurryHandler; -import mekanism.api.chemical.slurry.Slurry; -import mekanism.api.chemical.slurry.SlurryStack; import appeng.api.behaviors.GenericInternalInventory; import appeng.api.config.Actionable; -@SuppressWarnings("UnstableApiUsage") -public abstract sealed class GenericStackChemicalStorage, S extends ChemicalStack> - implements IChemicalHandler { +public class GenericStackChemicalStorage implements IChemicalHandler { private final GenericInternalInventory inv; - private final byte form; - private GenericStackChemicalStorage(GenericInternalInventory inv, byte form) { + public GenericStackChemicalStorage(GenericInternalInventory inv) { this.inv = inv; - this.form = form; } @Override - public int getTanks() { + public int getChemicalTanks() { return inv.size(); } @Override - public S getChemicalInTank(int tank) { - if (inv.getKey(tank) instanceof MekanismKey what && what.getForm() == form) { - return (S) what.withAmount(inv.getAmount(tank)); + public ChemicalStack getChemicalInTank(int tank) { + if (inv.getKey(tank) instanceof MekanismKey what) { + return what.withAmount(inv.getAmount(tank)); } - return getEmptyStack(); + return ChemicalStack.EMPTY; } @Override - public void setChemicalInTank(int tank, S stack) { + public void setChemicalInTank(int tank, ChemicalStack stack) { throw new UnsupportedOperationException(); } @Override - public long getTankCapacity(int tank) { + public long getChemicalTankCapacity(int tank) { return inv.getCapacity(MekanismKeyType.TYPE); } @Override - public boolean isValid(int tank, S stack) { + public boolean isValid(int tank, ChemicalStack stack) { var what = MekanismKey.of(stack); return what == null || inv.isAllowedIn(tank, what); } @Override - public S insertChemical(int tank, S stack, Action action) { + public ChemicalStack insertChemical(int tank, ChemicalStack stack, Action action) { var what = MekanismKey.of(stack); if (what == null) { @@ -75,51 +57,24 @@ public S insertChemical(int tank, S stack, Action action) { - inv.insert(tank, what, stack.getAmount(), Actionable.of(action.toFluidAction())); if (remainder == 0) { - return getEmptyStack(); + return ChemicalStack.EMPTY; } - return ChemicalBridge.withAmount(stack, remainder); + return stack.copyWithAmount(remainder); } @Override - public S extractChemical(int tank, long amount, Action action) { - if (!(inv.getKey(tank) instanceof MekanismKey what && what.getForm() == form)) { - return getEmptyStack(); + public ChemicalStack extractChemical(int tank, long amount, Action action) { + if (!(inv.getKey(tank) instanceof MekanismKey what)) { + return ChemicalStack.EMPTY; } var extracted = inv.extract(tank, what, amount, Actionable.of(action.toFluidAction())); - if (extracted > 0) { - return (S) what.withAmount(extracted); + if (extracted == 0) { + return ChemicalStack.EMPTY; } - return getEmptyStack(); - } - - public static final class OfGas extends GenericStackChemicalStorage implements IGasHandler { - public OfGas(GenericInternalInventory inv) { - super(inv, MekanismKey.GAS); - } - } - - public static final class OfInfusion extends GenericStackChemicalStorage - implements IInfusionHandler { - public OfInfusion(GenericInternalInventory inv) { - super(inv, MekanismKey.INFUSION); - } - } - - public static final class OfPigment extends GenericStackChemicalStorage - implements IPigmentHandler { - public OfPigment(GenericInternalInventory inv) { - super(inv, MekanismKey.PIGMENT); - } - } - - public static final class OfSlurry extends GenericStackChemicalStorage - implements ISlurryHandler { - public OfSlurry(GenericInternalInventory inv) { - super(inv, MekanismKey.SLURRY); - } + return what.withAmount(extracted); } } diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKey.java b/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKey.java index c3254b7..ae5dadb 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKey.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKey.java @@ -3,12 +3,15 @@ import java.util.List; import java.util.Objects; -import javax.annotation.Nullable; - +import com.mojang.datafixers.util.Pair; import com.mojang.serialization.Codec; +import com.mojang.serialization.DataResult; +import com.mojang.serialization.DynamicOps; import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; +import org.jetbrains.annotations.Nullable; + import net.minecraft.core.BlockPos; import net.minecraft.core.GlobalPos; import net.minecraft.core.HolderLookup; @@ -22,18 +25,9 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; -import me.ramidzkh.mekae2.util.ChemicalBridge; import mekanism.api.MekanismAPI; import mekanism.api.chemical.Chemical; import mekanism.api.chemical.ChemicalStack; -import mekanism.api.chemical.gas.Gas; -import mekanism.api.chemical.gas.GasStack; -import mekanism.api.chemical.infuse.InfuseType; -import mekanism.api.chemical.infuse.InfusionStack; -import mekanism.api.chemical.pigment.Pigment; -import mekanism.api.chemical.pigment.PigmentStack; -import mekanism.api.chemical.slurry.Slurry; -import mekanism.api.chemical.slurry.SlurryStack; import mekanism.api.radiation.IRadiationManager; import appeng.api.stacks.AEKey; @@ -43,23 +37,31 @@ public class MekanismKey extends AEKey { public static final MapCodec MAP_CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( - Chemical.BOXED_CODEC.fieldOf("id").forGetter(key -> key.getStack().getChemical())) + Codec.withAlternative(Chemical.CODEC, new Codec<>() { + @Override + public DataResult> decode(DynamicOps ops, T input) { + return ops.getMap(input) + .flatMap(map -> ops.getStringValue(map.get("chemical_type")).flatMap(type -> { + return MekanismAPI.CHEMICAL_REGISTRY.byNameCodec().decode(ops, map.get(type)); + })); + } + + @Override + public DataResult encode(Chemical input, DynamicOps ops, T prefix) { + return DataResult.error(() -> "should not encode"); + } + }).fieldOf("id").forGetter(key -> key.getStack().getChemical())) .apply(instance, chemical -> MekanismKey.of(chemical.getStack(1)))); public static final Codec CODEC = MAP_CODEC.codec(); - public static final byte GAS = 0; - public static final byte INFUSION = 1; - public static final byte PIGMENT = 2; - public static final byte SLURRY = 3; + private final ChemicalStack stack; - private final ChemicalStack stack; - - private MekanismKey(ChemicalStack stack) { + private MekanismKey(ChemicalStack stack) { this.stack = stack; } @Nullable - public static MekanismKey of(ChemicalStack stack) { + public static MekanismKey of(ChemicalStack stack) { if (stack.isEmpty()) { return null; } @@ -67,22 +69,12 @@ public static MekanismKey of(ChemicalStack stack) { return new MekanismKey(stack.copy()); } - public ChemicalStack getStack() { + public ChemicalStack getStack() { return stack; } - public ChemicalStack withAmount(long amount) { - return ChemicalBridge.withAmount(stack, amount); - } - - public byte getForm() { - return switch (stack) { - case GasStack ignored -> GAS; - case InfusionStack ignored -> INFUSION; - case PigmentStack ignored -> PIGMENT; - case SlurryStack ignored -> SLURRY; - default -> throw new UnsupportedOperationException(); - }; + public ChemicalStack withAmount(long amount) { + return stack.copyWithAmount(amount); } @Override @@ -95,6 +87,7 @@ public AEKey dropSecondary() { return this; } + @Nullable public static MekanismKey fromTag(HolderLookup.Provider registries, CompoundTag tag) { var ops = registries.createSerializationContext(NbtOps.INSTANCE); @@ -124,10 +117,7 @@ public ResourceLocation getId() { @Override public void addDrops(long amount, List drops, Level level, BlockPos pos) { - if (stack instanceof GasStack gasStack) { - IRadiationManager.INSTANCE.dumpRadiation(GlobalPos.of(level.dimension(), pos), - ChemicalBridge.withAmount(gasStack, amount)); - } + IRadiationManager.INSTANCE.dumpRadiation(GlobalPos.of(level.dimension(), pos), withAmount(amount)); } @Override @@ -137,17 +127,8 @@ protected Component computeDisplayName() { @Override public boolean isTagged(TagKey tag) { - return switch (stack.getChemical()) { - case Gas gas -> tag.registry().equals(MekanismAPI.GAS_REGISTRY_NAME) - && gas.is((TagKey) tag); - case InfuseType infuse -> tag.registry().equals(MekanismAPI.INFUSE_TYPE_REGISTRY_NAME) - && infuse.is((TagKey) tag); - case Pigment pigment -> tag.registry().equals(MekanismAPI.PIGMENT_REGISTRY_NAME) - && pigment.is((TagKey) tag); - case Slurry slurry -> tag.registry().equals(MekanismAPI.SLURRY_REGISTRY_NAME) - && slurry.is((TagKey) tag); - default -> throw new UnsupportedOperationException(); - }; + // This will just return false for incorrectly cast tags + return stack.is((TagKey) tag); } @Override @@ -162,12 +143,11 @@ public boolean hasComponents() { @Override public void writeToPacket(RegistryFriendlyByteBuf data) { - ChemicalStack.BOXED_STREAM_CODEC.encode(data, stack); + ChemicalStack.STREAM_CODEC.encode(data, stack); } public static MekanismKey fromPacket(RegistryFriendlyByteBuf data) { - var stack = ChemicalStack.BOXED_STREAM_CODEC.decode(data); - return new MekanismKey(stack); + return new MekanismKey(ChemicalStack.STREAM_CODEC.decode(data)); } @Override diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKeyType.java b/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKeyType.java index ba53861..58d83ae 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKeyType.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKeyType.java @@ -3,7 +3,6 @@ import java.util.Objects; import java.util.stream.Stream; -import com.google.common.collect.Streams; import com.mojang.serialization.MapCodec; import net.minecraft.core.HolderLookup; @@ -52,7 +51,7 @@ public MekanismKey readFromPacket(RegistryFriendlyByteBuf input) { } @Override - public MekanismKey loadKeyFromTag(HolderLookup.Provider registries, CompoundTag tag) { + public AEKey loadKeyFromTag(HolderLookup.Provider registries, CompoundTag tag) { return MekanismKey.fromTag(registries, tag); } @@ -64,11 +63,7 @@ public int getAmountPerUnit() { @Override public Stream> getTagNames() { - return Streams.concat( - MekanismAPI.GAS_REGISTRY.getTagNames(), - MekanismAPI.INFUSE_TYPE_REGISTRY.getTagNames(), - MekanismAPI.PIGMENT_REGISTRY.getTagNames(), - MekanismAPI.SLURRY_REGISTRY.getTagNames()); + return MekanismAPI.CHEMICAL_REGISTRY.getTagNames().map(t -> t); } // Copied from AEFluidKey diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/MultipleCapabilityP2PTunnelPart.java b/src/main/java/me/ramidzkh/mekae2/ae2/MultipleCapabilityP2PTunnelPart.java deleted file mode 100644 index dfd9cfa..0000000 --- a/src/main/java/me/ramidzkh/mekae2/ae2/MultipleCapabilityP2PTunnelPart.java +++ /dev/null @@ -1,212 +0,0 @@ -package me.ramidzkh.mekae2.ae2; - -import java.util.Collection; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; - -import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; -import net.minecraft.world.level.BlockGetter; -import net.neoforged.neoforge.capabilities.BlockCapability; - -import appeng.api.parts.IPartItem; -import appeng.hooks.ticking.TickHandler; -import appeng.parts.p2p.P2PTunnelPart; - -public class MultipleCapabilityP2PTunnelPart

> extends P2PTunnelPart

{ - - private final Map, CapabilitySetInner> capabilities; - - // Prevents recursive block updates. - private boolean inBlockUpdate = false; - // Prevents recursive access to the adjacent capability in case P2P input/output faces touch - int accessDepth = 0; - - public MultipleCapabilityP2PTunnelPart(IPartItem partItem, - Function>> capabilities) { - super(partItem); - var part = (P) this; - this.capabilities = capabilities.apply(part).stream() - .collect(Collectors.toMap(CapabilitySet::capability, set -> set.toInner(part))); - } - - private CapabilitySetInner getSet(BlockCapability capability) { - return (CapabilitySetInner) capabilities.get(capability); - } - - protected T getCapability(BlockCapability capability) { - var set = getSet(capability); - - if (isOutput()) { - return set.outputHandler(); - } else { - return set.inputHandler(); - } - } - - /** - * Return the capability connected to this side of this P2P connection. If this method is called again on this - * tunnel while the returned object has not been closed, further calls to {@link CapabilityGuard#get()} will return - * a dummy capability. - */ - protected final CapabilityGuard getAdjacentCapability(BlockCapability capability) { - accessDepth++; - return getSet(capability).guard(); - } - - /** - * Returns the capability attached to the input side of this tunnel's P2P connection. If this method is called again - * on this tunnel while the returned object has not been closed, further calls to {@link CapabilityGuard#get()} will - * return a dummy capability. - */ - protected final CapabilityGuard getInputCapability(BlockCapability capability) { - var input = (MultipleCapabilityP2PTunnelPart

) getInput(); - return input == null ? getSet(capability).empty() : input.getAdjacentCapability(capability); - } - - /** - * The position right in front of this P2P tunnel. - */ - BlockPos getFacingPos() { - return getHost().getLocation().getPos().relative(getSide()); - } - - // Send a block update on p2p status change, or any update on another endpoint. - void sendBlockUpdate() { - // Prevent recursive block updates. - if (!inBlockUpdate) { - inBlockUpdate = true; - - try { - // getHost().notifyNeighbors() would queue a callback, but we want to do an update synchronously! - // (otherwise we can't detect infinite recursion, it would just queue updates endlessly) - getHost().notifyNeighborNow(getSide()); - } finally { - inBlockUpdate = false; - } - } - } - - @Override - public void onTunnelNetworkChange() { - // This might be invoked while the network is being unloaded and we don't want to send a block update then, so - // we delay it until the next tick. - TickHandler.instance().addCallable(getLevel(), () -> { - if (getMainNode().isReady()) { // Check that the p2p tunnel is still there. - sendBlockUpdate(); - } - }); - } - - /** - * Forward block updates from the attached tile's position to the other end of the tunnel. Required for TE's on the - * other end to know that the available caps may have changed. - */ - @Override - public void onNeighborChanged(BlockGetter level, BlockPos pos, BlockPos neighbor) { - // We only care about block updates on the side this tunnel is facing - if (!getFacingPos().equals(neighbor)) { - return; - } - - // Prevent recursive block updates. - if (!inBlockUpdate) { - inBlockUpdate = true; - - try { - if (isOutput()) { - var input = getInput(); - - if (input != null) { - input.sendBlockUpdate(); - } - } else { - for (var output : getOutputs()) { - output.sendBlockUpdate(); - } - } - } finally { - inBlockUpdate = false; - } - } - } - - public record CapabilitySet(BlockCapability capability, C inputHandler, C outputHandler, - C emptyHandler) { - private

> CapabilitySetInner toInner(P part) { - return new CapabilitySetInner<>(new CapabilityGuard<>(part, capability(), emptyHandler()), - new EmptyCapabilityGuard<>(part, capability(), emptyHandler()), - inputHandler(), - outputHandler()); - } - } - - private record CapabilitySetInner>(CapabilityGuard guard, - CapabilityGuard empty, - C inputHandler, - C outputHandler) { - } - - protected static class CapabilityGuard> implements AutoCloseable { - protected final C emptyHandler; - private final P part; - private final BlockCapability capability; - - public CapabilityGuard(P part, BlockCapability capability, C emptyHandler) { - this.part = part; - this.capability = capability; - this.emptyHandler = emptyHandler; - } - - /** - * Get the capability, or a null handler if not available. Use within the scope of the enclosing AdjCapability. - */ - public C get() { - if (part.accessDepth == 0) { - throw new IllegalStateException("get was called after closing the wrapper"); - } else if (part.accessDepth == 1) { - if (part.isActive()) { - var cap = part.getLevel().getCapability(capability, part.getFacingPos(), - part.getSide().getOpposite()); - - if (cap != null) { - return cap; - } - } - - return emptyHandler; - } else { - // This capability is already in use (as the nesting is > 1), so we return an empty handler to prevent - // infinite recursion. - return emptyHandler; - } - } - - @Override - public void close() { - if (--part.accessDepth < 0) { - throw new IllegalStateException("Close has been called multiple times"); - } - } - } - - /** - * This specialization is used when the tunnel is not connected. - */ - private static class EmptyCapabilityGuard> - extends CapabilityGuard implements AutoCloseable { - public EmptyCapabilityGuard(P part, BlockCapability capability, C emptyHandler) { - super(part, capability, emptyHandler); - } - - @Override - public void close() { - } - - @Override - public C get() { - return emptyHandler; - } - } -} diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/stack/ChemicalHandlerFacade.java b/src/main/java/me/ramidzkh/mekae2/ae2/stack/ChemicalHandlerFacade.java index e3453f4..eb05799 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/stack/ChemicalHandlerFacade.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/stack/ChemicalHandlerFacade.java @@ -1,7 +1,5 @@ package me.ramidzkh.mekae2.ae2.stack; -import org.jetbrains.annotations.Nullable; - import net.minecraft.network.chat.Component; import me.ramidzkh.mekae2.AMText; @@ -16,7 +14,7 @@ import appeng.api.storage.MEStorage; import appeng.core.localization.GuiText; -public record ChemicalHandlerFacade(@Nullable IChemicalHandler[] handlers, boolean extractableOnly, +public record ChemicalHandlerFacade(IChemicalHandler handler, boolean extractableOnly, Runnable changeListener) implements MEStorage { @Override @@ -25,14 +23,8 @@ public long insert(AEKey what, long amount, Actionable mode, IActionSource sourc return 0; } - var handler = handlers[key.getForm()]; - - if (handler == null) { - return 0; - } - - var inserted = amount - handler.insertChemical(key.withAmount(amount), - Action.fromFluidAction(mode.getFluidAction())).getAmount(); + var inserted = amount - handler + .insertChemical(key.withAmount(amount), Action.fromFluidAction(mode.getFluidAction())).getAmount(); if (inserted > 0 && mode == Actionable.MODULATE) { if (this.changeListener != null) { @@ -49,14 +41,8 @@ public long extract(AEKey what, long amount, Actionable mode, IActionSource sour return 0; } - var handler = handlers[key.getForm()]; - - if (handler == null) { - return 0; - } - - var extracted = handler.extractChemical(key.withAmount(amount), - Action.fromFluidAction(mode.getFluidAction())).getAmount(); + var extracted = handler.extractChemical(key.withAmount(amount), Action.fromFluidAction(mode.getFluidAction())) + .getAmount(); if (extracted > 0 && mode == Actionable.MODULATE) { if (this.changeListener != null) { @@ -74,26 +60,20 @@ public Component getDescription() { @Override public void getAvailableStacks(KeyCounter out) { - for (var handler : handlers) { - if (handler == null) { + for (var i = 0; i < handler.getChemicalTanks(); i++) { + var stack = handler.getChemicalInTank(i); + var key = MekanismKey.of(stack); + + if (key == null) { continue; } - for (var i = 0; i < handler.getTanks(); i++) { - // Skip resources that cannot be extracted if that filter was enabled - var stack = handler.getChemicalInTank(i); - var key = MekanismKey.of(stack); - - if (key == null) { - continue; - } - - if (extractableOnly && handler.extractChemical(stack, Action.SIMULATE).isEmpty()) { - continue; - } - - out.add(key, stack.getAmount()); + // Skip resources that cannot be extracted if that filter was enabled + if (extractableOnly && handler.extractChemical(stack, Action.SIMULATE).isEmpty()) { + continue; } + + out.add(key, stack.getAmount()); } } } diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismExternalStorageStrategy.java b/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismExternalStorageStrategy.java index b479f38..e4bd02d 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismExternalStorageStrategy.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismExternalStorageStrategy.java @@ -15,39 +15,23 @@ public class MekanismExternalStorageStrategy implements ExternalStorageStrategy { - private final BlockCapabilityCache[] lookups; + private final BlockCapabilityCache lookup; public MekanismExternalStorageStrategy(ServerLevel level, BlockPos fromPos, Direction fromSide) { - this.lookups = new BlockCapabilityCache[] { - BlockCapabilityCache.create(MekCapabilities.GAS.block(), level, fromPos, fromSide), - BlockCapabilityCache.create(MekCapabilities.INFUSION.block(), level, fromPos, fromSide), - BlockCapabilityCache.create(MekCapabilities.PIGMENT.block(), level, fromPos, fromSide), - BlockCapabilityCache.create(MekCapabilities.SLURRY.block(), level, fromPos, fromSide) }; + this.lookup = BlockCapabilityCache.create(MekCapabilities.CHEMICAL.block(), level, fromPos, fromSide); } @Nullable @Override public MEStorage createWrapper(boolean extractableOnly, Runnable injectOrExtractCallback) { - var handlers = new IChemicalHandler[4]; - var empty = true; + var storage = lookup.getCapability(); - for (var i = 0; i < 4; i++) { - var storage = lookups[i].getCapability(); - - if (storage == null) { - continue; - } - - handlers[i] = storage; - empty = false; - } - - if (empty) { + if (storage == null) { return null; } - return new ChemicalHandlerFacade(handlers, extractableOnly, injectOrExtractCallback); + return new ChemicalHandlerFacade(storage, extractableOnly, injectOrExtractCallback); } } diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismStackExportStrategy.java b/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismStackExportStrategy.java index eeb7e10..96661a6 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismStackExportStrategy.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismStackExportStrategy.java @@ -22,16 +22,12 @@ public class MekanismStackExportStrategy implements StackExportStrategy { private static final Logger LOGGER = LoggerFactory.getLogger(MekanismStackExportStrategy.class); - private final BlockCapabilityCache[] lookups; + private final BlockCapabilityCache lookup; public MekanismStackExportStrategy(ServerLevel level, BlockPos fromPos, Direction fromSide) { - this.lookups = new BlockCapabilityCache[] { - BlockCapabilityCache.create(MekCapabilities.GAS.block(), level, fromPos, fromSide), - BlockCapabilityCache.create(MekCapabilities.INFUSION.block(), level, fromPos, fromSide), - BlockCapabilityCache.create(MekCapabilities.PIGMENT.block(), level, fromPos, fromSide), - BlockCapabilityCache.create(MekCapabilities.SLURRY.block(), level, fromPos, fromSide) }; + this.lookup = BlockCapabilityCache.create(MekCapabilities.CHEMICAL.block(), level, fromPos, fromSide); } @Override @@ -40,7 +36,7 @@ public long transfer(StackTransferContext context, AEKey what, long amount) { return 0; } - var storage = lookups[mekanismKey.getForm()].getCapability(); + var storage = lookup.getCapability(); if (storage == null) { return 0; @@ -89,7 +85,7 @@ public long push(AEKey what, long amount, Actionable mode) { return 0; } - var storage = lookups[mekanismKey.getForm()].getCapability(); + var storage = lookup.getCapability(); if (storage == null) { return 0; diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismStackImportStrategy.java b/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismStackImportStrategy.java index 5ccbf47..bc5be4b 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismStackImportStrategy.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismStackImportStrategy.java @@ -19,16 +19,12 @@ @SuppressWarnings("UnstableApiUsage") public class MekanismStackImportStrategy implements StackImportStrategy { - private final BlockCapabilityCache[] lookups; + private final BlockCapabilityCache lookup; public MekanismStackImportStrategy(ServerLevel level, BlockPos fromPos, Direction fromSide) { - this.lookups = new BlockCapabilityCache[] { - BlockCapabilityCache.create(MekCapabilities.GAS.block(), level, fromPos, fromSide), - BlockCapabilityCache.create(MekCapabilities.INFUSION.block(), level, fromPos, fromSide), - BlockCapabilityCache.create(MekCapabilities.PIGMENT.block(), level, fromPos, fromSide), - BlockCapabilityCache.create(MekCapabilities.SLURRY.block(), level, fromPos, fromSide) }; + this.lookup = BlockCapabilityCache.create(MekCapabilities.CHEMICAL.block(), level, fromPos, fromSide); } @Override @@ -37,61 +33,59 @@ public boolean transfer(StackTransferContext context) { return false; } - for (var lookup : lookups) { - var adjacentHandler = lookup.getCapability(); + var adjacentHandler = lookup.getCapability(); - if (adjacentHandler == null) { - continue; - } + if (adjacentHandler == null) { + return false; + } - var remainingTransferAmount = context.getOperationsRemaining() - * (long) MekanismKeyType.TYPE.getAmountPerOperation(); + var remainingTransferAmount = context.getOperationsRemaining() + * (long) MekanismKeyType.TYPE.getAmountPerOperation(); - var inv = context.getInternalStorage(); + var inv = context.getInternalStorage(); - // Try to find an extractable resource that fits our filter - for (var i = 0; i < adjacentHandler.getTanks() && remainingTransferAmount > 0; i++) { - var stack = adjacentHandler.getChemicalInTank(i); - var resource = MekanismKey.of(stack); + // Try to find an extractable resource that fits our filter + for (var i = 0; i < adjacentHandler.getChemicalTanks() && remainingTransferAmount > 0; i++) { + var stack = adjacentHandler.getChemicalInTank(i); + var resource = MekanismKey.of(stack); - if (resource == null - // Regard a filter that is set on the bus - || !context.isInFilter(resource)) { - continue; - } + if (resource == null + // Regard a filter that is set on the bus + || !context.isInFilter(resource)) { + continue; + } + + // Check how much of *this* resource we can actually insert into the network, it might be 0 + // if the cells are partitioned or there's not enough types left, etc. + var amountForThisResource = inv.getInventory().insert(resource, remainingTransferAmount, + Actionable.SIMULATE, + context.getActionSource()); + + // Try to simulate-extract it + var amount = adjacentHandler + .extractChemical(resource.withAmount(amountForThisResource), Action.EXECUTE) + .getAmount(); - // Check how much of *this* resource we can actually insert into the network, it might be 0 - // if the cells are partitioned or there's not enough types left, etc. - var amountForThisResource = inv.getInventory().insert(resource, remainingTransferAmount, - Actionable.SIMULATE, + if (amount > 0) { + var inserted = inv.getInventory().insert(resource, amount, Actionable.MODULATE, context.getActionSource()); - // Try to simulate-extract it - var amount = adjacentHandler - .extractChemical(resource.withAmount(amountForThisResource), Action.EXECUTE) - .getAmount(); - - if (amount > 0) { - var inserted = inv.getInventory().insert(resource, amount, Actionable.MODULATE, - context.getActionSource()); - - if (inserted < amount) { - // Be nice and try to give the overflow back - var leftover = amount - inserted; - leftover = adjacentHandler - .insertChemical(resource.withAmount(leftover), Action.EXECUTE).getAmount(); - - if (leftover > 0) { - AELog.warn( - "Extracted %dx%s from adjacent storage and voided it because network refused insert", - leftover, resource); - } - } + if (inserted < amount) { + // Be nice and try to give the overflow back + var leftover = amount - inserted; + leftover = adjacentHandler + .insertChemical(resource.withAmount(leftover), Action.EXECUTE).getAmount(); - var opsUsed = Math.max(1, inserted / MekanismKeyType.TYPE.getAmountPerOperation()); - context.reduceOperationsRemaining(opsUsed); - remainingTransferAmount -= inserted; + if (leftover > 0) { + AELog.warn( + "Extracted %dx%s from adjacent storage and voided it because network refused insert", + leftover, resource); + } } + + var opsUsed = Math.max(1, inserted / MekanismKeyType.TYPE.getAmountPerOperation()); + context.reduceOperationsRemaining(opsUsed); + remainingTransferAmount -= inserted; } } diff --git a/src/main/java/me/ramidzkh/mekae2/integration/jade/AMJadePlugin.java b/src/main/java/me/ramidzkh/mekae2/integration/jade/AMJadePlugin.java index cd987f3..2fe8a70 100644 --- a/src/main/java/me/ramidzkh/mekae2/integration/jade/AMJadePlugin.java +++ b/src/main/java/me/ramidzkh/mekae2/integration/jade/AMJadePlugin.java @@ -2,6 +2,7 @@ import net.minecraft.resources.ResourceLocation; +import mekanism.api.MekanismAPI; import snownee.jade.api.IWailaClientRegistration; import snownee.jade.api.IWailaPlugin; import snownee.jade.api.WailaPlugin; @@ -14,12 +15,9 @@ */ @WailaPlugin public class AMJadePlugin implements IWailaPlugin { - private static final ResourceLocation[] CHEMICALS = { - ResourceLocation.fromNamespaceAndPath("mekanism", "gas"), - ResourceLocation.fromNamespaceAndPath("mekanism", "infuse_type"), - ResourceLocation.fromNamespaceAndPath("mekanism", "pigment"), - ResourceLocation.fromNamespaceAndPath("mekanism", "slurry"), - }; + + private static final ResourceLocation CHEMICAL = ResourceLocation.fromNamespaceAndPath(MekanismAPI.MEKANISM_MODID, + "chemical"); @Override public void registerClient(IWailaClientRegistration registration) { @@ -27,9 +25,7 @@ public void registerClient(IWailaClientRegistration registration) { var target = accessor.getTarget(); if (target instanceof InterfaceLogicHost || target instanceof PatternProviderLogicHost) { - for (var loc : CHEMICALS) { - box.getTooltip().remove(loc); - } + box.getTooltip().remove(CHEMICAL); } }); } diff --git a/src/main/java/me/ramidzkh/mekae2/util/ChemicalBridge.java b/src/main/java/me/ramidzkh/mekae2/util/ChemicalBridge.java deleted file mode 100644 index 1c85482..0000000 --- a/src/main/java/me/ramidzkh/mekae2/util/ChemicalBridge.java +++ /dev/null @@ -1,12 +0,0 @@ -package me.ramidzkh.mekae2.util; - -import mekanism.api.chemical.ChemicalStack; - -public interface ChemicalBridge { - - static > S withAmount(S stack, long amount) { - var copy = (S) stack.copy(); - copy.setAmount(amount); - return copy; - } -} diff --git a/src/main/java/me/ramidzkh/mekae2/util/package-info.java b/src/main/java/me/ramidzkh/mekae2/util/package-info.java deleted file mode 100644 index a75d397..0000000 --- a/src/main/java/me/ramidzkh/mekae2/util/package-info.java +++ /dev/null @@ -1,7 +0,0 @@ -@ParametersAreNonnullByDefault -@MethodsReturnNonnullByDefault -package me.ramidzkh.mekae2.util; - -import javax.annotation.ParametersAreNonnullByDefault; - -import net.minecraft.MethodsReturnNonnullByDefault;