From 30ec2b4f681b5aeabf8963ad005de5660037ec59 Mon Sep 17 00:00:00 2001 From: NotMyWing Date: Sun, 9 Jun 2024 17:23:08 +1100 Subject: [PATCH] Add Tunnel Conversion API Allows AE2FC Interfaces to convert P2P Tunnels, closes #73 --- src/main/java/co/neeve/nae2/NAE2API.java | 6 ++++ .../nae2/common/api/TunnelConversionAPI.java | 19 ++++++++++++ .../nae2/common/integration/ae2fc/AE2FC.java | 24 +++++++++++++++ ...nHelper.java => AE2FCInterfaceHelper.java} | 6 ++-- .../common/integration/ae2fc/UpgradeInit.java | 15 ---------- .../common/registration/Registration.java | 6 ++++ .../registration/definitions/Parts.java | 17 +++++++++++ .../registration/definitions/Upgrades.java | 6 ++-- .../MixinTunnelConversion.java} | 29 +++++++++++++------ .../MixinCraftingCPUCluster.java | 2 +- .../patterntransform/MixinCraftingGrid.java | 2 +- .../shared/MixinAE2FCInventoryAdaptor.java | 8 ++--- .../shared/MixinBlockingTermName.java | 10 +++---- .../ifacep2p/shared/MixinPushPattern.java | 12 ++++---- src/main/resources/mixins.nae2.ifacep2p.json | 1 - src/main/resources/mixins.nae2.json | 5 ++-- 16 files changed, 118 insertions(+), 50 deletions(-) create mode 100644 src/main/java/co/neeve/nae2/common/api/TunnelConversionAPI.java create mode 100644 src/main/java/co/neeve/nae2/common/integration/ae2fc/AE2FC.java rename src/main/java/co/neeve/nae2/common/integration/ae2fc/{AE2FCIntegrationHelper.java => AE2FCInterfaceHelper.java} (77%) delete mode 100644 src/main/java/co/neeve/nae2/common/integration/ae2fc/UpgradeInit.java rename src/main/java/co/neeve/nae2/mixin/{ifacep2p/shared/MixinPartP2PTunnel.java => core/MixinTunnelConversion.java} (50%) rename src/main/java/co/neeve/nae2/mixin/{ => core}/crafting/patterntransform/MixinCraftingCPUCluster.java (98%) rename src/main/java/co/neeve/nae2/mixin/{ => core}/crafting/patterntransform/MixinCraftingGrid.java (94%) diff --git a/src/main/java/co/neeve/nae2/NAE2API.java b/src/main/java/co/neeve/nae2/NAE2API.java index ad8c1ce..f6155af 100644 --- a/src/main/java/co/neeve/nae2/NAE2API.java +++ b/src/main/java/co/neeve/nae2/NAE2API.java @@ -1,9 +1,11 @@ package co.neeve.nae2; import co.neeve.nae2.common.api.ExposerAPI; +import co.neeve.nae2.common.api.TunnelConversionAPI; public class NAE2API { private final ExposerAPI exposerAPI = new ExposerAPI(); + private final TunnelConversionAPI tunnelConversionAPI = new TunnelConversionAPI(); NAE2API() {} @@ -15,4 +17,8 @@ public class NAE2API { public ExposerAPI exposer() { return this.exposerAPI; } + + public TunnelConversionAPI tunnelConversion() { + return this.tunnelConversionAPI; + } } diff --git a/src/main/java/co/neeve/nae2/common/api/TunnelConversionAPI.java b/src/main/java/co/neeve/nae2/common/api/TunnelConversionAPI.java new file mode 100644 index 0000000..c7bbccb --- /dev/null +++ b/src/main/java/co/neeve/nae2/common/api/TunnelConversionAPI.java @@ -0,0 +1,19 @@ +package co.neeve.nae2.common.api; + +import appeng.util.item.ItemStackHashStrategy; +import it.unimi.dsi.fastutil.objects.Object2ObjectMap; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap; +import net.minecraft.item.ItemStack; + +public class TunnelConversionAPI { + private final Object2ObjectMap tunnelMap = + new Object2ObjectOpenCustomHashMap<>(ItemStackHashStrategy.comparingAllButCount()); + + public void register(ItemStack catalyst, ItemStack tunnelStack) { + this.tunnelMap.put(catalyst, tunnelStack); + } + + public ItemStack getConversion(ItemStack catalyst) { + return this.tunnelMap.getOrDefault(catalyst, ItemStack.EMPTY); + } +} diff --git a/src/main/java/co/neeve/nae2/common/integration/ae2fc/AE2FC.java b/src/main/java/co/neeve/nae2/common/integration/ae2fc/AE2FC.java new file mode 100644 index 0000000..4babdaf --- /dev/null +++ b/src/main/java/co/neeve/nae2/common/integration/ae2fc/AE2FC.java @@ -0,0 +1,24 @@ +package co.neeve.nae2.common.integration.ae2fc; + +import co.neeve.nae2.NAE2; +import co.neeve.nae2.common.registration.definitions.Upgrades; +import com.glodblock.github.loader.FCBlocks; +import com.glodblock.github.loader.FCItems; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fml.relauncher.Side; + +public class AE2FC { + + public static void initInterfaceUpgrade(Upgrades.UpgradeType upgrade) { + upgrade.registerItem(new ItemStack(FCBlocks.DUAL_INTERFACE), 1); + upgrade.registerItem(new ItemStack(FCItems.PART_DUAL_INTERFACE), 1); + } + + public static void postInit(Side side) { + NAE2.definitions().parts().p2pTunnelInterface().maybeStack(1).ifPresent((p2pTunnel) -> { + var tc = NAE2.api().tunnelConversion(); + tc.register(new ItemStack(FCBlocks.DUAL_INTERFACE), p2pTunnel); + tc.register(new ItemStack(FCItems.PART_DUAL_INTERFACE), p2pTunnel); + }); + } +} diff --git a/src/main/java/co/neeve/nae2/common/integration/ae2fc/AE2FCIntegrationHelper.java b/src/main/java/co/neeve/nae2/common/integration/ae2fc/AE2FCInterfaceHelper.java similarity index 77% rename from src/main/java/co/neeve/nae2/common/integration/ae2fc/AE2FCIntegrationHelper.java rename to src/main/java/co/neeve/nae2/common/integration/ae2fc/AE2FCInterfaceHelper.java index 338ac03..fda3ee7 100644 --- a/src/main/java/co/neeve/nae2/common/integration/ae2fc/AE2FCIntegrationHelper.java +++ b/src/main/java/co/neeve/nae2/common/integration/ae2fc/AE2FCInterfaceHelper.java @@ -3,7 +3,7 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; -public class AE2FCIntegrationHelper { +public class AE2FCInterfaceHelper { private static TileEntity interfaceOverride = null; private static EnumFacing enumFacingOverride = null; @@ -12,7 +12,7 @@ public static TileEntity getInterfaceOverride() { } public static void setInterfaceOverride(TileEntity interfaceOverride) { - AE2FCIntegrationHelper.interfaceOverride = interfaceOverride; + AE2FCInterfaceHelper.interfaceOverride = interfaceOverride; } public static EnumFacing getEnumFacingOverride() { @@ -20,6 +20,6 @@ public static EnumFacing getEnumFacingOverride() { } public static void setEnumFacingOverride(EnumFacing enumFacingOverride) { - AE2FCIntegrationHelper.enumFacingOverride = enumFacingOverride; + AE2FCInterfaceHelper.enumFacingOverride = enumFacingOverride; } } diff --git a/src/main/java/co/neeve/nae2/common/integration/ae2fc/UpgradeInit.java b/src/main/java/co/neeve/nae2/common/integration/ae2fc/UpgradeInit.java deleted file mode 100644 index 119a634..0000000 --- a/src/main/java/co/neeve/nae2/common/integration/ae2fc/UpgradeInit.java +++ /dev/null @@ -1,15 +0,0 @@ -package co.neeve.nae2.common.integration.ae2fc; - -import co.neeve.nae2.common.registration.definitions.Upgrades; -import com.glodblock.github.loader.FCBlocks; -import com.glodblock.github.loader.FCItems; -import net.minecraft.item.ItemStack; - -public class UpgradeInit { - - public static void init(Upgrades.UpgradeType upgrade) { - upgrade.registerItem(new ItemStack(FCBlocks.DUAL_INTERFACE), 1); - upgrade.registerItem(new ItemStack(FCItems.PART_DUAL_INTERFACE), 1); - } - -} diff --git a/src/main/java/co/neeve/nae2/common/registration/Registration.java b/src/main/java/co/neeve/nae2/common/registration/Registration.java index 9d47b65..0794048 100644 --- a/src/main/java/co/neeve/nae2/common/registration/Registration.java +++ b/src/main/java/co/neeve/nae2/common/registration/Registration.java @@ -4,6 +4,8 @@ import appeng.bootstrap.components.*; import appeng.core.AEConfig; import appeng.core.features.AEFeature; +import appeng.util.Platform; +import co.neeve.nae2.common.integration.ae2fc.AE2FC; import co.neeve.nae2.common.recipes.handlers.DisassembleRecipe; import co.neeve.nae2.common.registration.definitions.*; import co.neeve.nae2.common.registration.registry.Registry; @@ -43,6 +45,10 @@ public Registration() { this.parts = new Parts(this.registry); this.upgrades = new Upgrades(this.registry); this.blocks = new Blocks(this.registry); + + if (Platform.isModLoaded("ae2fc")) { + this.registry.addBootstrapComponent((IPostInitComponent) AE2FC::postInit); + } } public void preInit(FMLPreInitializationEvent event) { diff --git a/src/main/java/co/neeve/nae2/common/registration/definitions/Parts.java b/src/main/java/co/neeve/nae2/common/registration/definitions/Parts.java index 8037f25..a204dc2 100644 --- a/src/main/java/co/neeve/nae2/common/registration/definitions/Parts.java +++ b/src/main/java/co/neeve/nae2/common/registration/definitions/Parts.java @@ -2,9 +2,12 @@ import appeng.api.AEApi; import appeng.api.parts.IPart; +import appeng.bootstrap.components.IPostInitComponent; +import appeng.core.Api; import appeng.core.features.DamagedItemDefinition; import appeng.core.localization.GuiText; import appeng.util.Platform; +import co.neeve.nae2.NAE2; import co.neeve.nae2.Tags; import co.neeve.nae2.common.features.Features; import co.neeve.nae2.common.features.IFeature; @@ -54,9 +57,23 @@ public Parts(Registry registry) { this.beamFormer = this.createPart(this.itemPart, PartType.BEAM_FORMER); this.p2pTunnelInterface = this.createPart(this.itemPart, PartType.P2P_TUNNEL_INTERFACE); + this.p2pTunnelInterface.maybeStack(1) + .ifPresent((tunnelStack) -> registry.addBootstrapComponent((IPostInitComponent) (r) -> { + var definitions = Api.INSTANCE.definitions(); + + definitions.blocks().iface().maybeStack(1) + .ifPresent((stack) -> registerTunnelConversion(tunnelStack, stack)); + + definitions.parts().iface().maybeStack(1) + .ifPresent((stack) -> registerTunnelConversion(tunnelStack, stack)); + })); this.exposer = this.createPart(this.itemPart, PartType.EXPOSER); } + private static void registerTunnelConversion(ItemStack tunnelStack, ItemStack stack) { + NAE2.api().tunnelConversion().register(stack, tunnelStack); + } + public static Optional getById(int itemDamage) { return Optional.ofNullable(PartType.getCachedValues().getOrDefault(itemDamage, null)); } diff --git a/src/main/java/co/neeve/nae2/common/registration/definitions/Upgrades.java b/src/main/java/co/neeve/nae2/common/registration/definitions/Upgrades.java index 6f03257..ff09998 100644 --- a/src/main/java/co/neeve/nae2/common/registration/definitions/Upgrades.java +++ b/src/main/java/co/neeve/nae2/common/registration/definitions/Upgrades.java @@ -13,7 +13,7 @@ import co.neeve.nae2.common.crafting.patterntransform.transformers.GregTechCircuitPatternTransformer; import co.neeve.nae2.common.features.IFeature; import co.neeve.nae2.common.features.subfeatures.UpgradeFeatures; -import co.neeve.nae2.common.integration.ae2fc.UpgradeInit; +import co.neeve.nae2.common.integration.ae2fc.AE2FC; import co.neeve.nae2.common.items.NAEBaseItemUpgrade; import co.neeve.nae2.common.registration.registry.Registry; import co.neeve.nae2.common.registration.registry.interfaces.DamagedDefinitions; @@ -68,7 +68,7 @@ public Upgrades(Registry registry) { UpgradeType.AUTO_COMPLETE.registerItem(blocks.iface(), 1); UpgradeType.AUTO_COMPLETE.registerItem(parts.iface(), 1); if (Platform.isModLoaded("ae2fc")) { - UpgradeInit.init(UpgradeType.AUTO_COMPLETE); + AE2FC.initInterfaceUpgrade(UpgradeType.AUTO_COMPLETE); } }); } @@ -85,7 +85,7 @@ public Upgrades(Registry registry) { UpgradeType.GREGTECH_CIRCUIT.registerItem(blocks.iface(), 1); UpgradeType.GREGTECH_CIRCUIT.registerItem(parts.iface(), 1); if (Platform.isModLoaded("ae2fc")) { - UpgradeInit.init(UpgradeType.GREGTECH_CIRCUIT); + AE2FC.initInterfaceUpgrade(UpgradeType.GREGTECH_CIRCUIT); } }); } diff --git a/src/main/java/co/neeve/nae2/mixin/ifacep2p/shared/MixinPartP2PTunnel.java b/src/main/java/co/neeve/nae2/mixin/core/MixinTunnelConversion.java similarity index 50% rename from src/main/java/co/neeve/nae2/mixin/ifacep2p/shared/MixinPartP2PTunnel.java rename to src/main/java/co/neeve/nae2/mixin/core/MixinTunnelConversion.java index 8235847..cc9ba42 100644 --- a/src/main/java/co/neeve/nae2/mixin/ifacep2p/shared/MixinPartP2PTunnel.java +++ b/src/main/java/co/neeve/nae2/mixin/core/MixinTunnelConversion.java @@ -1,7 +1,7 @@ -package co.neeve.nae2.mixin.ifacep2p.shared; +package co.neeve.nae2.mixin.core; +import appeng.api.config.TunnelType; import appeng.api.implementations.items.IMemoryCard; -import appeng.core.Api; import appeng.parts.p2p.PartP2PTunnel; import co.neeve.nae2.NAE2; import com.llamalad7.mixinextras.sugar.Local; @@ -11,19 +11,30 @@ import org.spongepowered.asm.mixin.injection.ModifyVariable; @Mixin(PartP2PTunnel.class) -public class MixinPartP2PTunnel { +public class MixinTunnelConversion { @ModifyVariable(method = "onPartActivate", at = @At(value = "LOAD"), name = "newType", remap = false) public ItemStack injectP2PTypes(ItemStack newType, @Local(ordinal = 0) ItemStack hand) { var item = hand.getItem(); if (!(item instanceof IMemoryCard)) { - var definitions = Api.INSTANCE.definitions(); + var conversion = NAE2.api().tunnelConversion().getConversion(hand); + if (!conversion.isEmpty()) { + return conversion; + } + } + + return newType; + } + + @ModifyVariable(method = "onPartActivate", at = @At(value = "LOAD"), name = "tt", remap = false) + public TunnelType maskTunnelType(TunnelType newType, @Local(ordinal = 0) ItemStack hand) { + var item = hand.getItem(); - var iface = definitions.blocks().iface().maybeStack(1); - var ifacePart = definitions.parts().iface().maybeStack(1); - if ((iface.isPresent() && hand.isItemEqual(iface.get())) - || (ifacePart.isPresent() && hand.isItemEqual(ifacePart.get()))) { - return NAE2.definitions().parts().p2pTunnelInterface().maybeStack(1).orElse(ItemStack.EMPTY); + if (!(item instanceof IMemoryCard)) { + var conversion = NAE2.api().tunnelConversion().getConversion(hand); + if (!conversion.isEmpty()) { + // Return whatever. + return TunnelType.ME; } } diff --git a/src/main/java/co/neeve/nae2/mixin/crafting/patterntransform/MixinCraftingCPUCluster.java b/src/main/java/co/neeve/nae2/mixin/core/crafting/patterntransform/MixinCraftingCPUCluster.java similarity index 98% rename from src/main/java/co/neeve/nae2/mixin/crafting/patterntransform/MixinCraftingCPUCluster.java rename to src/main/java/co/neeve/nae2/mixin/core/crafting/patterntransform/MixinCraftingCPUCluster.java index f4a5a27..bf4c94c 100644 --- a/src/main/java/co/neeve/nae2/mixin/crafting/patterntransform/MixinCraftingCPUCluster.java +++ b/src/main/java/co/neeve/nae2/mixin/core/crafting/patterntransform/MixinCraftingCPUCluster.java @@ -1,4 +1,4 @@ -package co.neeve.nae2.mixin.crafting.patterntransform; +package co.neeve.nae2.mixin.core.crafting.patterntransform; import appeng.api.networking.crafting.ICraftingMedium; import appeng.api.networking.crafting.ICraftingPatternDetails; diff --git a/src/main/java/co/neeve/nae2/mixin/crafting/patterntransform/MixinCraftingGrid.java b/src/main/java/co/neeve/nae2/mixin/core/crafting/patterntransform/MixinCraftingGrid.java similarity index 94% rename from src/main/java/co/neeve/nae2/mixin/crafting/patterntransform/MixinCraftingGrid.java rename to src/main/java/co/neeve/nae2/mixin/core/crafting/patterntransform/MixinCraftingGrid.java index 337c0d1..0251656 100644 --- a/src/main/java/co/neeve/nae2/mixin/crafting/patterntransform/MixinCraftingGrid.java +++ b/src/main/java/co/neeve/nae2/mixin/core/crafting/patterntransform/MixinCraftingGrid.java @@ -1,4 +1,4 @@ -package co.neeve.nae2.mixin.crafting.patterntransform; +package co.neeve.nae2.mixin.core.crafting.patterntransform; import appeng.api.networking.crafting.ICraftingMedium; import appeng.api.networking.crafting.ICraftingPatternDetails; diff --git a/src/main/java/co/neeve/nae2/mixin/ifacep2p/shared/MixinAE2FCInventoryAdaptor.java b/src/main/java/co/neeve/nae2/mixin/ifacep2p/shared/MixinAE2FCInventoryAdaptor.java index ec7d044..0ac23a3 100644 --- a/src/main/java/co/neeve/nae2/mixin/ifacep2p/shared/MixinAE2FCInventoryAdaptor.java +++ b/src/main/java/co/neeve/nae2/mixin/ifacep2p/shared/MixinAE2FCInventoryAdaptor.java @@ -1,7 +1,7 @@ package co.neeve.nae2.mixin.ifacep2p.shared; import appeng.helpers.IInterfaceHost; -import co.neeve.nae2.common.integration.ae2fc.AE2FCIntegrationHelper; +import co.neeve.nae2.common.integration.ae2fc.AE2FCInterfaceHelper; import com.glodblock.github.inventory.FluidConvertingInventoryAdaptor; import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; @@ -21,7 +21,7 @@ public class MixinAE2FCInventoryAdaptor { remap = true )) private static TileEntity wrapTileEntity(World instance, BlockPos blockPos, Operation original) { - var override = AE2FCIntegrationHelper.getInterfaceOverride(); + var override = AE2FCInterfaceHelper.getInterfaceOverride(); if (override != null) { return override; } @@ -36,9 +36,9 @@ private static TileEntity wrapTileEntity(World instance, BlockPos blockPos, Oper )) private static IInterfaceHost wrapGetInterfaceTE(TileEntity te, EnumFacing facing, Operation operation) { - var override = AE2FCIntegrationHelper.getInterfaceOverride(); + var override = AE2FCInterfaceHelper.getInterfaceOverride(); if (override != null) { - return operation.call(override, AE2FCIntegrationHelper.getEnumFacingOverride()); + return operation.call(override, AE2FCInterfaceHelper.getEnumFacingOverride()); } return operation.call(te, facing); diff --git a/src/main/java/co/neeve/nae2/mixin/ifacep2p/shared/MixinBlockingTermName.java b/src/main/java/co/neeve/nae2/mixin/ifacep2p/shared/MixinBlockingTermName.java index 8d18b94..ec09bf9 100644 --- a/src/main/java/co/neeve/nae2/mixin/ifacep2p/shared/MixinBlockingTermName.java +++ b/src/main/java/co/neeve/nae2/mixin/ifacep2p/shared/MixinBlockingTermName.java @@ -5,7 +5,7 @@ import appeng.helpers.DualityInterface; import appeng.helpers.IInterfaceHost; import appeng.util.Platform; -import co.neeve.nae2.common.integration.ae2fc.AE2FCIntegrationHelper; +import co.neeve.nae2.common.integration.ae2fc.AE2FCInterfaceHelper; import co.neeve.nae2.common.parts.p2p.PartP2PInterface; import com.llamalad7.mixinextras.injector.ModifyExpressionValue; import com.llamalad7.mixinextras.injector.wrapoperation.Operation; @@ -80,8 +80,8 @@ private TileEntity wrapEntityGetter(World instance, BlockPos bp, Operation