From 253802546cdf87b978a264c2b3cbd1f5787b09c9 Mon Sep 17 00:00:00 2001 From: Ramid Khan Date: Sat, 24 Feb 2024 09:45:14 +1100 Subject: [PATCH] Initial 1.20.4 port --- .gitignore | 2 +- build.gradle | 147 ++++++++---------- gradle.properties | 20 ++- src/main/java/me/ramidzkh/mekae2/AMItems.java | 58 ++++--- src/main/java/me/ramidzkh/mekae2/AMMenus.java | 4 +- .../ramidzkh/mekae2/AppliedMekanistics.java | 111 +++++++------ .../mekae2/AppliedMekanisticsClient.java | 8 +- .../me/ramidzkh/mekae2/MekCapabilities.java | 42 +++-- .../mekae2/ae2/AMChemicalStackRenderer.java | 4 +- .../ae2/ChemicalContainerItemStrategy.java | 104 ++++++++----- .../mekae2/ae2/ChemicalP2PTunnelPart.java | 56 +++++-- .../me/ramidzkh/mekae2/ae2/MekanismKey.java | 4 +- .../ramidzkh/mekae2/ae2/MekanismKeyType.java | 8 +- .../ae2/MultipleCapabilityP2PTunnelPart.java | 81 +++++----- .../MekanismExternalStorageStrategy.java | 20 ++- .../stack/MekanismStackExportStrategy.java | 20 ++- .../stack/MekanismStackImportStrategy.java | 18 +-- .../mekae2/data/BlockModelProvider.java | 4 +- .../mekae2/data/BlockTagsProvider.java | 4 +- .../mekae2/data/ItemModelProvider.java | 13 +- .../mekae2/data/ItemTagsProvider.java | 2 +- .../mekae2/data/MekAE2DataGenerators.java | 4 +- .../ramidzkh/mekae2/data/RecipeProvider.java | 22 +-- .../mekae2/integration/jade/AMJadePlugin.java | 4 +- .../mekae2/integration/jei/AMJEIPlugin.java | 34 ---- .../jei/ChemicalIngredientConverter.java | 42 ----- .../mekae2/qio/QioStorageAdapter.java | 24 ++- .../me/ramidzkh/mekae2/qio/QioSupport.java | 86 ++++------ src/main/resources/META-INF/mods.toml | 24 ++- 29 files changed, 477 insertions(+), 493 deletions(-) delete mode 100644 src/main/java/me/ramidzkh/mekae2/integration/jei/AMJEIPlugin.java delete mode 100644 src/main/java/me/ramidzkh/mekae2/integration/jei/ChemicalIngredientConverter.java diff --git a/.gitignore b/.gitignore index 9f0ce6e..9e516a5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ .vscode/ build/ libs/ -run/ +runs/ diff --git a/build.gradle b/build.gradle index 0124cb4..c06432c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,8 +1,8 @@ plugins { - id("net.neoforged.gradle") version "[6.0.18,6.2)" + id("net.neoforged.gradle.userdev") version "7.0.57" id("com.matthewprenger.cursegradle") version "1.4.0" - id("com.diffplug.spotless") version "6.22.0" - id("com.modrinth.minotaur") version "2.8.4" + id("com.diffplug.spotless") version "6.23.3" + id("com.modrinth.minotaur") version "2.8.7" } group = "me.ramidzkh" @@ -19,19 +19,30 @@ repositories { } } - maven { - name = "Jared maven" - url = uri("https://maven.blamejared.com/") + exclusiveContent { + forRepository { + maven { + name = "Jared maven" + url = uri("https://maven.blamejared.com") + } + } - content { + filter { + includeGroup("mezz.jei") includeGroup("mezz.jei") } } - maven { - url "https://www.cursemaven.com" - content { - includeGroup "curse.maven" + exclusiveContent { + forRepository { + maven { + name = "CurseForge" + url = uri("https://www.cursemaven.com") + } + } + + filter { + includeGroup("curse.maven") } } @@ -42,18 +53,20 @@ repositories { } dependencies { - minecraft("net.neoforged:forge:${minecraft_version}-${neoforge_version}") + implementation("net.neoforged:neoforge:${neoforge_version}") // We depend on many AE2 internals, such as using their basic cell drive, thus not using classifier = "api" - implementation(fg.deobf("appeng:appliedenergistics2-forge:${ae2_version}")) + implementation("appeng:appliedenergistics2-neoforge:${ae2_version}") - compileOnly(fg.deobf("mekanism:Mekanism:${minecraft_version}-${mekanism_version}:api")) - runtimeOnly(fg.deobf("mekanism:Mekanism:${minecraft_version}-${mekanism_version}:all")) + compileOnly("mekanism:Mekanism:${minecraft_version}-${mekanism_version}:api") + runtimeOnly("mekanism:Mekanism:${minecraft_version}-${mekanism_version}:all") { + exclude(group: "com.blamejared.crafttweaker") + } - compileOnly(fg.deobf("mezz.jei:jei-${minecraft_version}-common-api:${jei_version}")) - runtimeOnly(fg.deobf("mezz.jei:jei-${minecraft_version}-forge:${jei_version}")) + compileOnly("mezz.jei:jei-${minecraft_version}-common-api:${jei_version}") + runtimeOnly("mezz.jei:jei-${minecraft_version}-neoforge:${jei_version}") - implementation(fg.deobf("curse.maven:jade-324717:${jade_id}")) + compileOnly("curse.maven:jade-324717:${jade_id}") } sourceSets { @@ -65,85 +78,51 @@ sourceSets { } java { - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } } //////////////////// -// Forge/Minecraft -minecraft { - mappings(channel: "official", version: "${minecraft_version}") - - /** - * Configures properties common to all run configurations - */ - def commonRunProperties = { - workingDirectory(project.file("run")) - - property("forge.logging.console.level", "debug") - property("fml.earlyprogresswindow", "false") - jvmArgs("--add-opens", "java.base/sun.security.util=ALL-UNNAMED") - jvmArgs("--add-opens", "java.base/java.util.jar=ALL-UNNAMED") - - // See https://github.com/Vazkii/Patchouli#mixin-troubleshooting - property("mixin.env.remapRefMap", "true") - property("mixin.env.refMapRemappingFile", "${projectDir}/build/createSrgToMcp/output.srg") - // property "mixin.debug.export", "true" - - mods { - appmek { - source(sourceSets.main) - } - } +// Minecraft +runs { + configureEach { + systemProperty("forge.logging.console.level", "debug") + modSource(project.sourceSets.main) } - runs { - client { - with(commonRunProperties) - } + client { + } - server { - with(commonRunProperties) - } + server { + programArgument '--nogui' + } - data { - with(commonRunProperties) - args("--mod", "appmek", "--all", "--output", file("src/generated/resources/"), "--existing", file("src/main/resources")) - } + data { + programArguments.addAll "--mod", "appmek", "--all", "--output", file("src/generated/resources/").getAbsolutePath(), "--existing", file("src/main/resources/").getAbsolutePath() } } -tasks.withType(JavaCompile) { +tasks.withType(JavaCompile).configureEach { options.encoding = "UTF-8" - options.release.set(17) -} - -processResources { - inputs.property("version", project.version) - - exclude(".cache") - - filesMatching("META-INF/mods.toml") { - expand("version": project.version) - } } -jar { - finalizedBy("reobfJar") - - from(sourceSets.main.output.classesDirs) - from(sourceSets.main.output.resourcesDir) - - manifest { - attributes([ - "Specification-Title" : "Applied Mekanistics", - "Specification-Vendor" : "ramidzkh", - "Specification-Version" : "${project.version}", - "Implementation-Title" : "${project.name}", - "Implementation-Version" : "${project.version}", - "Implementation-Vendor" : "ramidzkh", - "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") - ]) +// This block of code expands all declared replace properties in the specified resource targets. +// A missing property will result in an error. Properties are expanded using ${} Groovy notation. +// When "copyIdeResources" is enabled, this will also run before the game launches in IDE environments. +// See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html +tasks.withType(ProcessResources).configureEach { + var replaceProperties = [ + version: version, + loader_version_range: loader_version_range, + neo_version_range: neo_version_range, + ae2_version_range: ae2_version_range, + mekanism_version_range: mekanism_version_range, + ] + inputs.properties(replaceProperties) + + filesMatching(["META-INF/mods.toml"]) { + expand replaceProperties } } diff --git a/gradle.properties b/gradle.properties index 0fc520b..f64ebeb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,11 +1,17 @@ -minecraft_version=1.20.1 -neoforge_version=47.1.79 -ae2_version=15.0.11 -mekanism_version=10.4.0.14 -jei_version=15.2.0.27 -jade_id=4768593 +minecraft_version=1.20.4 +neoforge_version=20.4.118-beta +ae2_version=17.0.5-beta +mekanism_version=10.4.6.homebaked +jei_version=17.0.0.30 +jade_id=4980188 -org.gradle.daemon=false +loader_version_range=[2,) +neo_version_range=[20.4,) +ae2_version_range=[17.0.0,18.0.0) +mekanism_version_range=[10.4.0,10.5.0) + +org.gradle.caching=true +org.gradle.parallel=true # Temp fix for Spotless / Remove Unused Imports: # https://github.com/diffplug/spotless/issues/834 diff --git a/src/main/java/me/ramidzkh/mekae2/AMItems.java b/src/main/java/me/ramidzkh/mekae2/AMItems.java index 2d11fc6..16f6051 100644 --- a/src/main/java/me/ramidzkh/mekae2/AMItems.java +++ b/src/main/java/me/ramidzkh/mekae2/AMItems.java @@ -6,16 +6,19 @@ import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Rarity; -import net.minecraftforge.eventbus.api.IEventBus; -import net.minecraftforge.registries.DeferredRegister; -import net.minecraftforge.registries.ForgeRegistries; -import net.minecraftforge.registries.RegistryObject; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.neoforge.registries.DeferredHolder; +import net.neoforged.neoforge.registries.DeferredItem; +import net.neoforged.neoforge.registries.DeferredRegister; import me.ramidzkh.mekae2.ae2.ChemicalP2PTunnelPart; import me.ramidzkh.mekae2.item.ChemicalPortableCellItem; import me.ramidzkh.mekae2.item.ChemicalStorageCell; import appeng.api.parts.PartModels; +import appeng.block.AEBaseBlock; +import appeng.block.AEBaseBlockItem; +import appeng.items.AEBaseItem; import appeng.items.materials.MaterialItem; import appeng.items.parts.PartItem; import appeng.items.parts.PartModelsHelper; @@ -26,8 +29,7 @@ public class AMItems { private static final DeferredRegister TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, AppliedMekanistics.ID); - private static final DeferredRegister ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, - AppliedMekanistics.ID); + private static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(AppliedMekanistics.ID); public static void initialize(IEventBus bus) { TABS.register(bus); @@ -42,62 +44,72 @@ private static Item.Properties properties() { return new Item.Properties(); } - public static final RegistryObject CREATIVE_TAB = TABS.register("main", + public static final DeferredHolder CREATIVE_TAB = TABS.register("main", () -> CreativeModeTab.builder() .title(AMText.CREATIVE_TAB.formatted()) .icon(() -> new ItemStack(AMItems.CHEMICAL_CELL_64K.get())) .displayItems((params, output) -> { for (var entry : ITEMS.getEntries()) { - output.accept(entry.get()); + var item = entry.get(); + + // For block items, the block controls the creative tab + if (item instanceof AEBaseBlockItem baseItem + && baseItem.getBlock() instanceof AEBaseBlock baseBlock) { + baseBlock.addToMainCreativeTab(output); + } else if (item instanceof AEBaseItem baseItem) { + baseItem.addToMainCreativeTab(output); + } else { + output.accept(item); + } } }) .build()); - public static final RegistryObject CHEMICAL_CELL_HOUSING = ITEMS.register("chemical_cell_housing", + public static final DeferredItem CHEMICAL_CELL_HOUSING = ITEMS.register("chemical_cell_housing", AMItems::basic); - public static final RegistryObject CHEMICAL_CELL_CREATIVE = ITEMS.register("creative_chemical_cell", + public static final DeferredItem CHEMICAL_CELL_CREATIVE = ITEMS.register("creative_chemical_cell", () -> new CreativeCellItem(properties().stacksTo(1).rarity(Rarity.EPIC))); - public static final RegistryObject CHEMICAL_CELL_1K = ITEMS.register("chemical_storage_cell_1k", + public static final DeferredItem CHEMICAL_CELL_1K = ITEMS.register("chemical_storage_cell_1k", () -> new ChemicalStorageCell(properties().stacksTo(1), StorageTier.SIZE_1K, CHEMICAL_CELL_HOUSING.get())); - public static final RegistryObject CHEMICAL_CELL_4K = ITEMS.register("chemical_storage_cell_4k", + public static final DeferredItem CHEMICAL_CELL_4K = ITEMS.register("chemical_storage_cell_4k", () -> new ChemicalStorageCell(properties().stacksTo(1), StorageTier.SIZE_4K, CHEMICAL_CELL_HOUSING.get())); - public static final RegistryObject CHEMICAL_CELL_16K = ITEMS.register("chemical_storage_cell_16k", + public static final DeferredItem CHEMICAL_CELL_16K = ITEMS.register("chemical_storage_cell_16k", () -> new ChemicalStorageCell(properties().stacksTo(1), StorageTier.SIZE_16K, CHEMICAL_CELL_HOUSING.get())); - public static final RegistryObject CHEMICAL_CELL_64K = ITEMS.register("chemical_storage_cell_64k", + public static final DeferredItem CHEMICAL_CELL_64K = ITEMS.register("chemical_storage_cell_64k", () -> new ChemicalStorageCell(properties().stacksTo(1), StorageTier.SIZE_64K, CHEMICAL_CELL_HOUSING.get())); - public static final RegistryObject CHEMICAL_CELL_256K = ITEMS.register("chemical_storage_cell_256k", + public static final DeferredItem CHEMICAL_CELL_256K = ITEMS.register("chemical_storage_cell_256k", () -> new ChemicalStorageCell(properties().stacksTo(1), StorageTier.SIZE_256K, CHEMICAL_CELL_HOUSING.get())); - public static final RegistryObject PORTABLE_CHEMICAL_CELL_1K = ITEMS.register( + public static final DeferredItem PORTABLE_CHEMICAL_CELL_1K = ITEMS.register( "portable_chemical_storage_cell_1k", () -> new ChemicalPortableCellItem(18, AMMenus.PORTABLE_CHEMICAL_CELL_TYPE, StorageTier.SIZE_1K, properties().stacksTo(1), 0)); - public static final RegistryObject PORTABLE_CHEMICAL_CELL_4K = ITEMS.register( + public static final DeferredItem PORTABLE_CHEMICAL_CELL_4K = ITEMS.register( "portable_chemical_storage_cell_4k", () -> new ChemicalPortableCellItem(18, AMMenus.PORTABLE_CHEMICAL_CELL_TYPE, StorageTier.SIZE_4K, properties().stacksTo(1), 0)); - public static final RegistryObject PORTABLE_CHEMICAL_CELL_16K = ITEMS.register( + public static final DeferredItem PORTABLE_CHEMICAL_CELL_16K = ITEMS.register( "portable_chemical_storage_cell_16k", () -> new ChemicalPortableCellItem(18, AMMenus.PORTABLE_CHEMICAL_CELL_TYPE, StorageTier.SIZE_16K, properties().stacksTo(1), 0)); - public static final RegistryObject PORTABLE_CHEMICAL_CELL_64K = ITEMS.register( + public static final DeferredItem PORTABLE_CHEMICAL_CELL_64K = ITEMS.register( "portable_chemical_storage_cell_64k", () -> new ChemicalPortableCellItem(18, AMMenus.PORTABLE_CHEMICAL_CELL_TYPE, StorageTier.SIZE_64K, properties().stacksTo(1), 0)); - public static final RegistryObject PORTABLE_CHEMICAL_CELL_256K = ITEMS.register( + public static final DeferredItem PORTABLE_CHEMICAL_CELL_256K = ITEMS.register( "portable_chemical_storage_cell_256k", () -> new ChemicalPortableCellItem(18, AMMenus.PORTABLE_CHEMICAL_CELL_TYPE, StorageTier.SIZE_256K, properties().stacksTo(1), 0)); - public static final RegistryObject> CHEMICAL_P2P_TUNNEL = Util.make(() -> { + public static final DeferredItem> CHEMICAL_P2P_TUNNEL = Util.make(() -> { PartModels.registerModels(PartModelsHelper.createModels(ChemicalP2PTunnelPart.class)); return ITEMS.register("chemical_p2p_tunnel", () -> new PartItem<>(properties(), ChemicalP2PTunnelPart.class, ChemicalP2PTunnelPart::new)); }); - public static RegistryObject get(Tier tier) { + public static DeferredItem get(Tier tier) { return switch (tier) { case _1K -> CHEMICAL_CELL_1K; case _4K -> CHEMICAL_CELL_4K; @@ -107,7 +119,7 @@ public static RegistryObject get(Tier tier) { }; } - public static RegistryObject getPortableCell(Tier tier) { + public static DeferredItem getPortableCell(Tier tier) { return switch (tier) { case _1K -> PORTABLE_CHEMICAL_CELL_1K; case _4K -> PORTABLE_CHEMICAL_CELL_4K; diff --git a/src/main/java/me/ramidzkh/mekae2/AMMenus.java b/src/main/java/me/ramidzkh/mekae2/AMMenus.java index f012530..2de6ba5 100644 --- a/src/main/java/me/ramidzkh/mekae2/AMMenus.java +++ b/src/main/java/me/ramidzkh/mekae2/AMMenus.java @@ -1,8 +1,8 @@ package me.ramidzkh.mekae2; import net.minecraft.world.inventory.MenuType; -import net.minecraftforge.eventbus.api.IEventBus; -import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; import appeng.api.implementations.menuobjects.IPortableTerminal; import appeng.client.gui.me.common.MEStorageScreen; diff --git a/src/main/java/me/ramidzkh/mekae2/AppliedMekanistics.java b/src/main/java/me/ramidzkh/mekae2/AppliedMekanistics.java index b79ea23..1a8e62b 100644 --- a/src/main/java/me/ramidzkh/mekae2/AppliedMekanistics.java +++ b/src/main/java/me/ramidzkh/mekae2/AppliedMekanistics.java @@ -1,31 +1,27 @@ package me.ramidzkh.mekae2; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import java.util.function.Function; import net.minecraft.core.Direction; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.ItemLike; +import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.entity.BlockEntity; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.common.capabilities.ICapabilityProvider; -import net.minecraftforge.common.util.LazyOptional; -import net.minecraftforge.event.AttachCapabilitiesEvent; -import net.minecraftforge.fml.DistExecutor; -import net.minecraftforge.fml.common.Mod; -import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; -import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; -import net.minecraftforge.registries.RegisterEvent; - -import me.ramidzkh.mekae2.ae2.ChemicalContainerItemStrategy; -import me.ramidzkh.mekae2.ae2.GenericStackChemicalStorage; -import me.ramidzkh.mekae2.ae2.MekanismKey; -import me.ramidzkh.mekae2.ae2.MekanismKeyType; +import net.neoforged.api.distmarker.Dist; +import net.neoforged.bus.api.EventPriority; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.fml.common.Mod; +import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; +import net.neoforged.fml.loading.FMLEnvironment; +import net.neoforged.neoforge.capabilities.BlockCapability; +import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; +import net.neoforged.neoforge.registries.RegisterEvent; + +import me.ramidzkh.mekae2.ae2.*; import me.ramidzkh.mekae2.ae2.stack.MekanismExternalStorageStrategy; import me.ramidzkh.mekae2.ae2.stack.MekanismStackExportStrategy; import me.ramidzkh.mekae2.ae2.stack.MekanismStackImportStrategy; @@ -33,10 +29,12 @@ import me.ramidzkh.mekae2.qio.QioSupport; import appeng.api.behaviors.ContainerItemStrategy; +import appeng.api.behaviors.GenericInternalInventory; import appeng.api.behaviors.GenericSlotCapacities; import appeng.api.client.StorageCellModels; import appeng.api.features.P2PTunnelAttunement; import appeng.api.implementations.blockentities.IChestOrDrive; +import appeng.api.parts.RegisterPartCapabilitiesEvent; import appeng.api.stacks.AEKeyType; import appeng.api.stacks.AEKeyTypes; import appeng.api.storage.StorageCells; @@ -44,7 +42,7 @@ import appeng.api.storage.cells.ICellGuiHandler; import appeng.api.storage.cells.ICellHandler; import appeng.api.upgrades.Upgrades; -import appeng.capabilities.Capabilities; +import appeng.capabilities.AppEngCapabilities; import appeng.core.AppEng; import appeng.core.definitions.AEItems; import appeng.core.localization.GuiText; @@ -58,9 +56,11 @@ public class AppliedMekanistics { public static final String ID = "appmek"; - public AppliedMekanistics() { - var bus = FMLJavaModLoadingContext.get().getModEventBus(); + public static ResourceLocation id(String path) { + return new ResourceLocation(ID, path); + } + public AppliedMekanistics(IEventBus bus) { AMItems.initialize(bus); AMMenus.initialize(bus); @@ -81,9 +81,10 @@ public AppliedMekanistics() { ContainerItemStrategy.register(MekanismKeyType.TYPE, MekanismKey.class, new ChemicalContainerItemStrategy()); GenericSlotCapacities.register(MekanismKeyType.TYPE, GenericSlotCapacities.getMap().get(AEKeyType.fluids())); - MinecraftForge.EVENT_BUS.addGenericListener(BlockEntity.class, this::initializeCapabilities); + bus.addListener(EventPriority.LOWEST, this::registerGenericAdapters); - QioSupport.initialize(); + bus.addListener(QioSupport::onBlockEntityCapability); + bus.addListener(this::registerPartCapabilities); bus.addListener((FMLCommonSetupEvent event) -> { event.enqueueWork(this::initializeModels); @@ -91,37 +92,35 @@ public AppliedMekanistics() { event.enqueueWork(this::initializeAttunement); }); - DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> AppliedMekanisticsClient::initialize); + if (FMLEnvironment.dist == Dist.CLIENT) { + AppliedMekanisticsClient.initialize(bus); + } } - public static ResourceLocation id(String path) { - return new ResourceLocation(ID, path); + 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(), + ChemicalP2PTunnelPart.class); } - private void initializeCapabilities(AttachCapabilitiesEvent event) { - var blockEntity = event.getObject(); - - event.addCapability(id("generic_inv_wrapper"), new ICapabilityProvider() { - @NotNull - @Override - public LazyOptional getCapability(@NotNull Capability capability, @Nullable Direction side) { - if (capability == MekCapabilities.GAS_HANDLER_CAPABILITY) { - return blockEntity.getCapability(Capabilities.GENERIC_INTERNAL_INV, side) - .lazyMap(GenericStackChemicalStorage.OfGas::new).cast(); - } else if (capability == MekCapabilities.INFUSION_HANDLER_CAPABILITY) { - return blockEntity.getCapability(Capabilities.GENERIC_INTERNAL_INV, side) - .lazyMap(GenericStackChemicalStorage.OfInfusion::new).cast(); - } else if (capability == MekCapabilities.PIGMENT_HANDLER_CAPABILITY) { - return blockEntity.getCapability(Capabilities.GENERIC_INTERNAL_INV, side) - .lazyMap(GenericStackChemicalStorage.OfPigment::new).cast(); - } else if (capability == MekCapabilities.SLURRY_HANDLER_CAPABILITY) { - return blockEntity.getCapability(Capabilities.GENERIC_INTERNAL_INV, side) - .lazyMap(GenericStackChemicalStorage.OfSlurry::new).cast(); - } - - return LazyOptional.empty(); + private void registerGenericAdapters(RegisterCapabilitiesEvent event) { + for (var block : BuiltInRegistries.BLOCK) { + if (event.isBlockRegistered(AppEngCapabilities.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); } - }); + } } private void initializeModels() { @@ -175,4 +174,18 @@ private void initializeUpgrades() { private void initializeAttunement() { P2PTunnelAttunement.registerAttunementTag(AMItems.CHEMICAL_P2P_TUNNEL::get); } + + private static void registerGenericInvAdapter(RegisterCapabilitiesEvent event, Block block, + BlockCapability capability, Function adapter) { + if (!event.isBlockRegistered(capability, block)) { + event.registerBlock(capability, (level, pos, state, blockEntity, context) -> { + var genericInv = level.getCapability(AppEngCapabilities.GENERIC_INTERNAL_INV, pos, state, + blockEntity, context); + if (genericInv != null) { + return adapter.apply(genericInv); + } + return null; + }, block); + } + } } diff --git a/src/main/java/me/ramidzkh/mekae2/AppliedMekanisticsClient.java b/src/main/java/me/ramidzkh/mekae2/AppliedMekanisticsClient.java index 5757d0e..8034b6f 100644 --- a/src/main/java/me/ramidzkh/mekae2/AppliedMekanisticsClient.java +++ b/src/main/java/me/ramidzkh/mekae2/AppliedMekanisticsClient.java @@ -1,7 +1,7 @@ package me.ramidzkh.mekae2; -import net.minecraftforge.client.event.RegisterColorHandlersEvent; -import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.neoforge.client.event.RegisterColorHandlersEvent; import me.ramidzkh.mekae2.ae2.AMChemicalStackRenderer; @@ -10,9 +10,7 @@ public class AppliedMekanisticsClient { - public static void initialize() { - var bus = FMLJavaModLoadingContext.get().getModEventBus(); - + public static void initialize(IEventBus bus) { bus.addListener(AppliedMekanisticsClient::registerItemColors); AMChemicalStackRenderer.initialize(bus); } diff --git a/src/main/java/me/ramidzkh/mekae2/MekCapabilities.java b/src/main/java/me/ramidzkh/mekae2/MekCapabilities.java index d315474..197fd11 100644 --- a/src/main/java/me/ramidzkh/mekae2/MekCapabilities.java +++ b/src/main/java/me/ramidzkh/mekae2/MekCapabilities.java @@ -1,9 +1,16 @@ package me.ramidzkh.mekae2; -import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.common.capabilities.CapabilityManager; -import net.minecraftforge.common.capabilities.CapabilityToken; +import java.util.List; +import org.jetbrains.annotations.Nullable; + +import net.minecraft.core.Direction; +import net.minecraft.resources.ResourceLocation; +import net.neoforged.neoforge.capabilities.BlockCapability; +import net.neoforged.neoforge.capabilities.ItemCapability; + +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; @@ -11,18 +18,25 @@ public class MekCapabilities { - public static final Capability GAS_HANDLER_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() { - }); + 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 Capability INFUSION_HANDLER_CAPABILITY = CapabilityManager - .get(new CapabilityToken<>() { - }); + public static final List> HANDLERS = List.of(GAS, INFUSION, PIGMENT, SLURRY); - public static final Capability PIGMENT_HANDLER_CAPABILITY = CapabilityManager - .get(new CapabilityToken<>() { - }); + public record CapSet>(BlockCapability block, + ItemCapability item) { + public CapSet(ResourceLocation name, Class handlerClass) { + this(BlockCapability.createSided(name, handlerClass), ItemCapability.createVoid(name, handlerClass)); + } + } - public static final Capability SLURRY_HANDLER_CAPABILITY = CapabilityManager - .get(new CapabilityToken<>() { - }); + private static ResourceLocation rl(String path) { + return new ResourceLocation(MekanismAPI.MEKANISM_MODID, path); + } } diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/AMChemicalStackRenderer.java b/src/main/java/me/ramidzkh/mekae2/ae2/AMChemicalStackRenderer.java index 2c59804..ece95bd 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/AMChemicalStackRenderer.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/AMChemicalStackRenderer.java @@ -13,8 +13,8 @@ import net.minecraft.client.renderer.texture.TextureAtlas; import net.minecraft.network.chat.Component; import net.minecraft.world.level.Level; -import net.minecraftforge.eventbus.api.IEventBus; -import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; import appeng.api.client.AEKeyRenderHandler; import appeng.api.client.AEKeyRendering; diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalContainerItemStrategy.java b/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalContainerItemStrategy.java index c204acc..3b5ed5c 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalContainerItemStrategy.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalContainerItemStrategy.java @@ -1,8 +1,5 @@ package me.ramidzkh.mekae2.ae2; -import java.util.List; -import java.util.stream.Stream; - import org.jetbrains.annotations.Nullable; import net.minecraft.sounds.SoundEvents; @@ -32,14 +29,11 @@ public GenericStack getContainedStack(ItemStack stack) { return null; } - for (var capability : List.of(MekCapabilities.GAS_HANDLER_CAPABILITY, - MekCapabilities.INFUSION_HANDLER_CAPABILITY, MekCapabilities.PIGMENT_HANDLER_CAPABILITY, - MekCapabilities.SLURRY_HANDLER_CAPABILITY)) { - var contained = stack.getCapability(capability) - .map(handler -> handler.extractChemical(Long.MAX_VALUE, Action.SIMULATE)); + for (var capability : MekCapabilities.HANDLERS) { + var handler = stack.getCapability(capability.item()); - if (contained.isPresent()) { - var chemical = contained.get(); + if (handler != null) { + var chemical = handler.extractChemical(Long.MAX_VALUE, Action.SIMULATE); var key = MekanismKey.of(chemical); if (key != null) { @@ -56,11 +50,10 @@ public GenericStack getContainedStack(ItemStack stack) { public ItemStack findCarriedContext(Player player, AbstractContainerMenu menu) { var carried = menu.getCarried(); - if (Stream - .of(MekCapabilities.GAS_HANDLER_CAPABILITY, MekCapabilities.INFUSION_HANDLER_CAPABILITY, - MekCapabilities.PIGMENT_HANDLER_CAPABILITY, MekCapabilities.SLURRY_HANDLER_CAPABILITY) - .anyMatch(capability -> carried.getCapability(capability).isPresent())) { - return carried; + for (var capability : MekCapabilities.HANDLERS) { + if (carried.getCapability(capability.item()) != null) { + return carried; + } } return null; @@ -70,11 +63,10 @@ public ItemStack findCarriedContext(Player player, AbstractContainerMenu menu) { public @Nullable ItemStack findPlayerSlotContext(Player player, int slot) { var carried = player.getInventory().getItem(slot); - if (Stream - .of(MekCapabilities.GAS_HANDLER_CAPABILITY, MekCapabilities.INFUSION_HANDLER_CAPABILITY, - MekCapabilities.PIGMENT_HANDLER_CAPABILITY, MekCapabilities.SLURRY_HANDLER_CAPABILITY) - .anyMatch(capability -> carried.getCapability(capability).isPresent())) { - return carried; + for (var capability : MekCapabilities.HANDLERS) { + if (carried.getCapability(capability.item()) != null) { + return carried; + } } return null; @@ -86,17 +78,37 @@ public long extract(ItemStack context, MekanismKey what, long amount, Actionable var action = Action.fromFluidAction(mode.getFluidAction()); if (stack instanceof GasStack gas) { - return context.getCapability(MekCapabilities.GAS_HANDLER_CAPABILITY) - .map(handler -> handler.extractChemical(gas, action).getAmount()).orElse(0L); + 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) { - return context.getCapability(MekCapabilities.INFUSION_HANDLER_CAPABILITY) - .map(handler -> handler.extractChemical(infusion, action).getAmount()).orElse(0L); + var handler = context.getCapability(MekCapabilities.INFUSION.item()); + + if (handler != null) { + return handler.extractChemical(infusion, action).getAmount(); + } else { + return 0L; + } } else if (stack instanceof PigmentStack pigment) { - return context.getCapability(MekCapabilities.PIGMENT_HANDLER_CAPABILITY) - .map(handler -> handler.extractChemical(pigment, action).getAmount()).orElse(0L); + 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) { - return context.getCapability(MekCapabilities.SLURRY_HANDLER_CAPABILITY) - .map(handler -> handler.extractChemical(slurry, action).getAmount()).orElse(0L); + var handler = context.getCapability(MekCapabilities.SLURRY.item()); + + if (handler != null) { + return handler.extractChemical(slurry, action).getAmount(); + } else { + return 0L; + } } else { throw new UnsupportedOperationException(); } @@ -108,17 +120,37 @@ public long insert(ItemStack context, MekanismKey what, long amount, Actionable var action = Action.fromFluidAction(mode.getFluidAction()); if (stack instanceof GasStack gas) { - return context.getCapability(MekCapabilities.GAS_HANDLER_CAPABILITY) - .map(handler -> amount - handler.insertChemical(gas, action).getAmount()).orElse(0L); + 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) { - return context.getCapability(MekCapabilities.INFUSION_HANDLER_CAPABILITY) - .map(handler -> amount - handler.insertChemical(infusion, action).getAmount()).orElse(0L); + 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) { - return context.getCapability(MekCapabilities.PIGMENT_HANDLER_CAPABILITY) - .map(handler -> amount - handler.insertChemical(pigment, action).getAmount()).orElse(0L); + 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) { - return context.getCapability(MekCapabilities.SLURRY_HANDLER_CAPABILITY) - .map(handler -> amount - handler.insertChemical(slurry, action).getAmount()).orElse(0L); + var handler = context.getCapability(MekCapabilities.SLURRY.item()); + + if (handler != null) { + return amount - handler.insertChemical(slurry, action).getAmount(); + } else { + return 0L; + } } else { throw new UnsupportedOperationException(); } diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalP2PTunnelPart.java b/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalP2PTunnelPart.java index b7d0b22..cabdd6a 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalP2PTunnelPart.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/ChemicalP2PTunnelPart.java @@ -2,7 +2,8 @@ import java.util.List; -import net.minecraftforge.common.capabilities.Capability; +import net.minecraft.core.Direction; +import net.neoforged.neoforge.capabilities.BlockCapability; import me.ramidzkh.mekae2.AppliedMekanistics; import me.ramidzkh.mekae2.MekCapabilities; @@ -36,15 +37,15 @@ public class ChemicalP2PTunnelPart extends MultipleCapabilityP2PTunnelPart partItem) { super(partItem, self -> List.of( - new CapabilitySet<>(MekCapabilities.GAS_HANDLER_CAPABILITY, new InputChemicalHandler.OfGas(self), + new CapabilitySet<>(MekCapabilities.GAS.block(), new InputChemicalHandler.OfGas(self), new OutputChemicalHandler.OfGas(self), NullChemicalHandler.GAS), - new CapabilitySet<>(MekCapabilities.INFUSION_HANDLER_CAPABILITY, + new CapabilitySet<>(MekCapabilities.INFUSION.block(), new InputChemicalHandler.OfInfusion(self), new OutputChemicalHandler.OfInfusion(self), NullChemicalHandler.INFUSION), - new CapabilitySet<>(MekCapabilities.PIGMENT_HANDLER_CAPABILITY, + new CapabilitySet<>(MekCapabilities.PIGMENT.block(), new InputChemicalHandler.OfPigment(self), new OutputChemicalHandler.OfPigment(self), NullChemicalHandler.PIGMENT), - new CapabilitySet<>(MekCapabilities.SLURRY_HANDLER_CAPABILITY, new InputChemicalHandler.OfSlurry(self), + new CapabilitySet<>(MekCapabilities.SLURRY.block(), new InputChemicalHandler.OfSlurry(self), new OutputChemicalHandler.OfSlurry(self), NullChemicalHandler.SLURRY))); } @@ -58,12 +59,33 @@ public IPartModel getStaticModels() { return MODELS.getModel(this.isPowered(), this.isActive()); } + @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()); + } + private static abstract sealed class InputChemicalHandler, S extends ChemicalStack, H extends IChemicalHandler> implements IChemicalHandler { private final ChemicalP2PTunnelPart part; - private final Capability capability; + private final BlockCapability capability; - protected InputChemicalHandler(ChemicalP2PTunnelPart part, Capability capability) { + protected InputChemicalHandler(ChemicalP2PTunnelPart part, BlockCapability capability) { this.part = part; this.capability = capability; } @@ -132,28 +154,28 @@ public S extractChemical(int tank, long maxAmount, Action action) { private static final class OfGas extends InputChemicalHandler implements IGasHandler { private OfGas(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.GAS_HANDLER_CAPABILITY); + super(part, MekCapabilities.GAS.block()); } } private static final class OfInfusion extends InputChemicalHandler implements IInfusionHandler { private OfInfusion(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.INFUSION_HANDLER_CAPABILITY); + super(part, MekCapabilities.INFUSION.block()); } } private static final class OfPigment extends InputChemicalHandler implements IPigmentHandler { private OfPigment(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.PIGMENT_HANDLER_CAPABILITY); + super(part, MekCapabilities.PIGMENT.block()); } } private static final class OfSlurry extends InputChemicalHandler implements ISlurryHandler { private OfSlurry(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.SLURRY_HANDLER_CAPABILITY); + super(part, MekCapabilities.SLURRY.block()); } } } @@ -161,9 +183,9 @@ private OfSlurry(ChemicalP2PTunnelPart part) { private static abstract sealed class OutputChemicalHandler, S extends ChemicalStack, H extends IChemicalHandler> implements IChemicalHandler { private final ChemicalP2PTunnelPart part; - private final Capability capability; + private final BlockCapability capability; - private OutputChemicalHandler(ChemicalP2PTunnelPart part, Capability capability) { + private OutputChemicalHandler(ChemicalP2PTunnelPart part, BlockCapability capability) { this.part = part; this.capability = capability; } @@ -223,28 +245,28 @@ public S extractChemical(int tank, long maxAmount, Action action) { private static final class OfGas extends OutputChemicalHandler implements IGasHandler { private OfGas(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.GAS_HANDLER_CAPABILITY); + super(part, MekCapabilities.GAS.block()); } } private static final class OfInfusion extends OutputChemicalHandler implements IInfusionHandler { private OfInfusion(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.INFUSION_HANDLER_CAPABILITY); + super(part, MekCapabilities.INFUSION.block()); } } private static final class OfPigment extends OutputChemicalHandler implements IPigmentHandler { private OfPigment(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.PIGMENT_HANDLER_CAPABILITY); + super(part, MekCapabilities.PIGMENT.block()); } } private static final class OfSlurry extends OutputChemicalHandler implements ISlurryHandler { private OfSlurry(ChemicalP2PTunnelPart part) { - super(part, MekCapabilities.SLURRY_HANDLER_CAPABILITY); + super(part, MekCapabilities.SLURRY.block()); } } } diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKey.java b/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKey.java index 3bb106c..1407062 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKey.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKey.java @@ -6,6 +6,7 @@ import javax.annotation.Nullable; import net.minecraft.core.BlockPos; +import net.minecraft.core.GlobalPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; @@ -15,7 +16,6 @@ import net.minecraft.world.level.Level; import me.ramidzkh.mekae2.util.ChemicalBridge; -import mekanism.api.Coord4D; import mekanism.api.MekanismAPI; import mekanism.api.chemical.ChemicalStack; import mekanism.api.chemical.gas.Gas; @@ -116,7 +116,7 @@ protected Component computeDisplayName() { @Override public void addDrops(long amount, List drops, Level level, BlockPos pos) { if (stack instanceof GasStack gasStack) { - IRadiationManager.INSTANCE.dumpRadiation(new Coord4D(pos, level), + IRadiationManager.INSTANCE.dumpRadiation(GlobalPos.of(level.dimension(), pos), ChemicalBridge.withAmount(gasStack, amount)); } } diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKeyType.java b/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKeyType.java index 73c854a..55bd5f7 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKeyType.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/MekanismKeyType.java @@ -57,10 +57,10 @@ public AEKey loadKeyFromTag(CompoundTag tag) { @Override public Stream> getTagNames() { return Streams.concat( - MekanismAPI.gasRegistry().tags().getTagNames(), - MekanismAPI.infuseTypeRegistry().tags().getTagNames(), - MekanismAPI.pigmentRegistry().tags().getTagNames(), - MekanismAPI.slurryRegistry().tags().getTagNames()); + MekanismAPI.GAS_REGISTRY.getTagNames(), + MekanismAPI.INFUSE_TYPE_REGISTRY.getTagNames(), + MekanismAPI.PIGMENT_REGISTRY.getTagNames(), + MekanismAPI.SLURRY_REGISTRY.getTagNames()); } // Copied from AEFluidKeys diff --git a/src/main/java/me/ramidzkh/mekae2/ae2/MultipleCapabilityP2PTunnelPart.java b/src/main/java/me/ramidzkh/mekae2/ae2/MultipleCapabilityP2PTunnelPart.java index 6aeaa2b..dfd9cfa 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/MultipleCapabilityP2PTunnelPart.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/MultipleCapabilityP2PTunnelPart.java @@ -6,46 +6,43 @@ import java.util.stream.Collectors; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.world.level.BlockGetter; -import net.minecraft.world.level.block.Blocks; -import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.common.util.LazyOptional; +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 access to the adjacent capability in case P2P input/output faces touch - int accessDepth = 0; + 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); - this.capabilities = capabilities.apply((P) this).stream() - .collect(Collectors.toMap(CapabilitySet::capability, set -> set.toInner((P) this))); + var part = (P) this; + this.capabilities = capabilities.apply(part).stream() + .collect(Collectors.toMap(CapabilitySet::capability, set -> set.toInner(part))); } - @Override - protected float getPowerDrainPerTick() { - return 2.0f; + private CapabilitySetInner getSet(BlockCapability capability) { + return (CapabilitySetInner) capabilities.get(capability); } - public final LazyOptional getCapability(Capability capability) { - var set = capabilities.get(capability); + protected T getCapability(BlockCapability capability) { + var set = getSet(capability); - if (set != null) { - if (isOutput()) { - return LazyOptional.of(set::outputHandler).cast(); - } else { - return LazyOptional.of(set::inputHandler).cast(); - } + if (isOutput()) { + return set.outputHandler(); + } else { + return set.inputHandler(); } - - return LazyOptional.empty(); } /** @@ -53,9 +50,9 @@ public final LazyOptional getCapability(Capability capability) { * tunnel while the returned object has not been closed, further calls to {@link CapabilityGuard#get()} will return * a dummy capability. */ - protected final CapabilityGuard getAdjacentCapability(Capability capability) { + protected final CapabilityGuard getAdjacentCapability(BlockCapability capability) { accessDepth++; - return (CapabilityGuard) capabilities.get(capability).guard(); + return getSet(capability).guard(); } /** @@ -63,10 +60,9 @@ protected final CapabilityGuard getAdjacentCapability(Capability ca * 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(Capability capability) { - var input = getInput(); - return input == null ? (CapabilityGuard) capabilities.get(capability).empty() - : input.getAdjacentCapability(capability); + protected final CapabilityGuard getInputCapability(BlockCapability capability) { + var input = (MultipleCapabilityP2PTunnelPart

) getInput(); + return input == null ? getSet(capability).empty() : input.getAdjacentCapability(capability); } /** @@ -77,7 +73,7 @@ BlockPos getFacingPos() { } // Send a block update on p2p status change, or any update on another endpoint. - protected void sendBlockUpdate() { + void sendBlockUpdate() { // Prevent recursive block updates. if (!inBlockUpdate) { inBlockUpdate = true; @@ -85,8 +81,7 @@ protected void sendBlockUpdate() { 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) - var self = getBlockEntity(); - self.getLevel().updateNeighborsAt(self.getBlockPos(), Blocks.AIR); + getHost().notifyNeighborNow(getSide()); } finally { inBlockUpdate = false; } @@ -95,7 +90,13 @@ protected void sendBlockUpdate() { @Override public void onTunnelNetworkChange() { - sendBlockUpdate(); + // 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(); + } + }); } /** @@ -131,7 +132,8 @@ public void onNeighborChanged(BlockGetter level, BlockPos pos, BlockPos neighbor } } - public record CapabilitySet(Capability capability, C inputHandler, C outputHandler, C emptyHandler) { + 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()), @@ -149,9 +151,9 @@ private record CapabilitySetInner> implements AutoCloseable { protected final C emptyHandler; private final P part; - private final Capability capability; + private final BlockCapability capability; - public CapabilityGuard(P part, Capability capability, C emptyHandler) { + public CapabilityGuard(P part, BlockCapability capability, C emptyHandler) { this.part = part; this.capability = capability; this.emptyHandler = emptyHandler; @@ -165,12 +167,11 @@ public C get() { throw new IllegalStateException("get was called after closing the wrapper"); } else if (part.accessDepth == 1) { if (part.isActive()) { - var self = part.getBlockEntity(); - var te = self.getLevel().getBlockEntity(part.getFacingPos()); + var cap = part.getLevel().getCapability(capability, part.getFacingPos(), + part.getSide().getOpposite()); - if (te != null) { - return te.getCapability(capability, part.getSide().getOpposite()) - .orElse(emptyHandler); + if (cap != null) { + return cap; } } @@ -195,7 +196,7 @@ public void close() { */ private static class EmptyCapabilityGuard> extends CapabilityGuard implements AutoCloseable { - public EmptyCapabilityGuard(P part, Capability capability, C emptyHandler) { + public EmptyCapabilityGuard(P part, BlockCapability capability, C emptyHandler) { super(part, capability, emptyHandler); } 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 a016482..b479f38 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismExternalStorageStrategy.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismExternalStorageStrategy.java @@ -5,28 +5,26 @@ import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.server.level.ServerLevel; +import net.neoforged.neoforge.capabilities.BlockCapabilityCache; import me.ramidzkh.mekae2.MekCapabilities; import mekanism.api.chemical.IChemicalHandler; import appeng.api.behaviors.ExternalStorageStrategy; import appeng.api.storage.MEStorage; -import appeng.util.BlockApiCache; public class MekanismExternalStorageStrategy implements ExternalStorageStrategy { - private final BlockApiCache[] lookups; - private final Direction fromSide; + private final BlockCapabilityCache[] lookups; public MekanismExternalStorageStrategy(ServerLevel level, BlockPos fromPos, Direction fromSide) { - this.lookups = new BlockApiCache[] { - BlockApiCache.create(MekCapabilities.GAS_HANDLER_CAPABILITY, level, fromPos), - BlockApiCache.create(MekCapabilities.INFUSION_HANDLER_CAPABILITY, level, fromPos), - BlockApiCache.create(MekCapabilities.PIGMENT_HANDLER_CAPABILITY, level, fromPos), - BlockApiCache.create(MekCapabilities.SLURRY_HANDLER_CAPABILITY, level, fromPos) }; - this.fromSide = 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) }; } @Nullable @@ -35,8 +33,8 @@ public MEStorage createWrapper(boolean extractableOnly, Runnable injectOrExtract var handlers = new IChemicalHandler[4]; var empty = true; - for (int i = 0; i < 4; i++) { - var storage = lookups[i].find(fromSide); + for (var i = 0; i < 4; i++) { + var storage = lookups[i].getCapability(); if (storage == null) { continue; 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 c45e757..eeb7e10 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismStackExportStrategy.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismStackExportStrategy.java @@ -6,6 +6,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.server.level.ServerLevel; +import net.neoforged.neoforge.capabilities.BlockCapabilityCache; import me.ramidzkh.mekae2.MekCapabilities; import me.ramidzkh.mekae2.ae2.MekanismKey; @@ -17,23 +18,20 @@ import appeng.api.config.Actionable; import appeng.api.stacks.AEKey; import appeng.api.storage.StorageHelper; -import appeng.util.BlockApiCache; public class MekanismStackExportStrategy implements StackExportStrategy { private static final Logger LOGGER = LoggerFactory.getLogger(MekanismStackExportStrategy.class); - private final BlockApiCache[] lookups; - private final Direction fromSide; + private final BlockCapabilityCache[] lookups; public MekanismStackExportStrategy(ServerLevel level, BlockPos fromPos, Direction fromSide) { - this.lookups = new BlockApiCache[] { - BlockApiCache.create(MekCapabilities.GAS_HANDLER_CAPABILITY, level, fromPos), - BlockApiCache.create(MekCapabilities.INFUSION_HANDLER_CAPABILITY, level, fromPos), - BlockApiCache.create(MekCapabilities.PIGMENT_HANDLER_CAPABILITY, level, fromPos), - BlockApiCache.create(MekCapabilities.SLURRY_HANDLER_CAPABILITY, level, fromPos) }; - this.fromSide = 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) }; } @Override @@ -42,7 +40,7 @@ public long transfer(StackTransferContext context, AEKey what, long amount) { return 0; } - var storage = lookups[mekanismKey.getForm()].find(fromSide); + var storage = lookups[mekanismKey.getForm()].getCapability(); if (storage == null) { return 0; @@ -91,7 +89,7 @@ public long push(AEKey what, long amount, Actionable mode) { return 0; } - var storage = lookups[mekanismKey.getForm()].find(fromSide); + var storage = lookups[mekanismKey.getForm()].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 06a9ba1..5ccbf47 100644 --- a/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismStackImportStrategy.java +++ b/src/main/java/me/ramidzkh/mekae2/ae2/stack/MekanismStackImportStrategy.java @@ -3,6 +3,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.server.level.ServerLevel; +import net.neoforged.neoforge.capabilities.BlockCapabilityCache; import me.ramidzkh.mekae2.MekCapabilities; import me.ramidzkh.mekae2.ae2.MekanismKey; @@ -14,23 +15,20 @@ import appeng.api.behaviors.StackTransferContext; import appeng.api.config.Actionable; import appeng.core.AELog; -import appeng.util.BlockApiCache; @SuppressWarnings("UnstableApiUsage") public class MekanismStackImportStrategy implements StackImportStrategy { - private final BlockApiCache[] lookups; - private final Direction fromSide; + private final BlockCapabilityCache[] lookups; public MekanismStackImportStrategy(ServerLevel level, BlockPos fromPos, Direction fromSide) { - this.lookups = new BlockApiCache[] { - BlockApiCache.create(MekCapabilities.GAS_HANDLER_CAPABILITY, level, fromPos), - BlockApiCache.create(MekCapabilities.INFUSION_HANDLER_CAPABILITY, level, fromPos), - BlockApiCache.create(MekCapabilities.PIGMENT_HANDLER_CAPABILITY, level, fromPos), - BlockApiCache.create(MekCapabilities.SLURRY_HANDLER_CAPABILITY, level, fromPos) }; - this.fromSide = 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) }; } @Override @@ -40,7 +38,7 @@ public boolean transfer(StackTransferContext context) { } for (var lookup : lookups) { - var adjacentHandler = lookup.find(fromSide); + var adjacentHandler = lookup.getCapability(); if (adjacentHandler == null) { continue; diff --git a/src/main/java/me/ramidzkh/mekae2/data/BlockModelProvider.java b/src/main/java/me/ramidzkh/mekae2/data/BlockModelProvider.java index ad9c500..65942d5 100644 --- a/src/main/java/me/ramidzkh/mekae2/data/BlockModelProvider.java +++ b/src/main/java/me/ramidzkh/mekae2/data/BlockModelProvider.java @@ -4,14 +4,14 @@ import net.minecraft.data.PackOutput; import net.minecraft.resources.ResourceLocation; -import net.minecraftforge.common.data.ExistingFileHelper; +import net.neoforged.neoforge.common.data.ExistingFileHelper; import me.ramidzkh.mekae2.AMItems; import me.ramidzkh.mekae2.AppliedMekanistics; import appeng.core.AppEng; -public class BlockModelProvider extends net.minecraftforge.client.model.generators.BlockModelProvider { +public class BlockModelProvider extends net.neoforged.neoforge.client.model.generators.BlockModelProvider { private static final ResourceLocation DRIVE_CELL = AppEng.makeId("block/drive/drive_cell"); diff --git a/src/main/java/me/ramidzkh/mekae2/data/BlockTagsProvider.java b/src/main/java/me/ramidzkh/mekae2/data/BlockTagsProvider.java index b0bf6ab..77dca2f 100644 --- a/src/main/java/me/ramidzkh/mekae2/data/BlockTagsProvider.java +++ b/src/main/java/me/ramidzkh/mekae2/data/BlockTagsProvider.java @@ -6,11 +6,11 @@ import net.minecraft.core.HolderLookup; import net.minecraft.data.PackOutput; -import net.minecraftforge.common.data.ExistingFileHelper; +import net.neoforged.neoforge.common.data.ExistingFileHelper; import me.ramidzkh.mekae2.AppliedMekanistics; -public class BlockTagsProvider extends net.minecraftforge.common.data.BlockTagsProvider { +public class BlockTagsProvider extends net.neoforged.neoforge.common.data.BlockTagsProvider { public BlockTagsProvider(PackOutput output, CompletableFuture lookupProvider, @Nullable ExistingFileHelper existingFileHelper) { diff --git a/src/main/java/me/ramidzkh/mekae2/data/ItemModelProvider.java b/src/main/java/me/ramidzkh/mekae2/data/ItemModelProvider.java index 7ce1542..6d59a94 100644 --- a/src/main/java/me/ramidzkh/mekae2/data/ItemModelProvider.java +++ b/src/main/java/me/ramidzkh/mekae2/data/ItemModelProvider.java @@ -2,16 +2,15 @@ import net.minecraft.data.PackOutput; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.Item; -import net.minecraftforge.common.data.ExistingFileHelper; -import net.minecraftforge.registries.RegistryObject; +import net.neoforged.neoforge.common.data.ExistingFileHelper; +import net.neoforged.neoforge.registries.DeferredItem; import me.ramidzkh.mekae2.AMItems; import me.ramidzkh.mekae2.AppliedMekanistics; import appeng.core.AppEng; -public class ItemModelProvider extends net.minecraftforge.client.model.generators.ItemModelProvider { +public class ItemModelProvider extends net.neoforged.neoforge.client.model.generators.ItemModelProvider { private static final ResourceLocation P2P_TUNNEL_BASE_ITEM = AppEng.makeId("item/p2p_tunnel_base"); private static final ResourceLocation P2P_TUNNEL_BASE_PART = AppEng.makeId("part/p2p/p2p_tunnel_base"); @@ -50,17 +49,17 @@ protected void registerModels() { .texture("type", OSMIUM_BLOCK); } - private void cell(RegistryObject cell, String background) { + private void cell(DeferredItem cell, String background) { singleTexture(cell.getId().getPath(), mcLoc("item/generated"), "layer0", AppliedMekanistics.id(background)) .texture("layer1", STORAGE_CELL_LED); } - private void portableCell(RegistryObject portable, String background) { + private void portableCell(DeferredItem portable, String background) { singleTexture(portable.getId().getPath(), mcLoc("item/generated"), "layer0", AppliedMekanistics.id(background)) .texture("layer1", PORTABLE_CELL_LED); } - private void flatSingleLayer(RegistryObject item, String texture) { + private void flatSingleLayer(DeferredItem item, String texture) { singleTexture(item.getId().getPath(), mcLoc("item/generated"), "layer0", AppliedMekanistics.id(texture)); } } diff --git a/src/main/java/me/ramidzkh/mekae2/data/ItemTagsProvider.java b/src/main/java/me/ramidzkh/mekae2/data/ItemTagsProvider.java index 8369bed..3c37ba1 100644 --- a/src/main/java/me/ramidzkh/mekae2/data/ItemTagsProvider.java +++ b/src/main/java/me/ramidzkh/mekae2/data/ItemTagsProvider.java @@ -8,7 +8,7 @@ import net.minecraft.data.PackOutput; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.block.Block; -import net.minecraftforge.common.data.ExistingFileHelper; +import net.neoforged.neoforge.common.data.ExistingFileHelper; import me.ramidzkh.mekae2.AMItems; import me.ramidzkh.mekae2.AppliedMekanistics; diff --git a/src/main/java/me/ramidzkh/mekae2/data/MekAE2DataGenerators.java b/src/main/java/me/ramidzkh/mekae2/data/MekAE2DataGenerators.java index 1fc7d42..5f8d306 100644 --- a/src/main/java/me/ramidzkh/mekae2/data/MekAE2DataGenerators.java +++ b/src/main/java/me/ramidzkh/mekae2/data/MekAE2DataGenerators.java @@ -1,6 +1,6 @@ package me.ramidzkh.mekae2.data; -import net.minecraftforge.data.event.GatherDataEvent; +import net.neoforged.neoforge.data.event.GatherDataEvent; public class MekAE2DataGenerators { @@ -17,6 +17,6 @@ public static void onGatherData(GatherDataEvent event) { generator.addProvider(true, new BlockModelProvider(packOutput, existingFileHelper)); generator.addProvider(true, new ItemModelProvider(packOutput, existingFileHelper)); - generator.addProvider(true, new RecipeProvider(packOutput)); + generator.addProvider(true, new RecipeProvider(packOutput, lookupProvider)); } } diff --git a/src/main/java/me/ramidzkh/mekae2/data/RecipeProvider.java b/src/main/java/me/ramidzkh/mekae2/data/RecipeProvider.java index 0d2ca07..923c442 100644 --- a/src/main/java/me/ramidzkh/mekae2/data/RecipeProvider.java +++ b/src/main/java/me/ramidzkh/mekae2/data/RecipeProvider.java @@ -1,16 +1,18 @@ package me.ramidzkh.mekae2.data; import java.util.Locale; -import java.util.function.Consumer; +import java.util.concurrent.CompletableFuture; +import net.minecraft.core.HolderLookup; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.data.PackOutput; -import net.minecraft.data.recipes.FinishedRecipe; import net.minecraft.data.recipes.RecipeCategory; +import net.minecraft.data.recipes.RecipeOutput; import net.minecraft.data.recipes.ShapedRecipeBuilder; import net.minecraft.data.recipes.ShapelessRecipeBuilder; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.ItemTags; -import net.minecraftforge.common.Tags; +import net.neoforged.neoforge.common.Tags; import me.ramidzkh.mekae2.AMItems; import me.ramidzkh.mekae2.AppliedMekanistics; @@ -20,12 +22,12 @@ public class RecipeProvider extends net.minecraft.data.recipes.RecipeProvider { - public RecipeProvider(PackOutput output) { - super(output); + public RecipeProvider(PackOutput output, CompletableFuture lookupProvider) { + super(output, lookupProvider); } @Override - protected void buildRecipes(Consumer consumer) { + protected void buildRecipes(RecipeOutput output) { ShapedRecipeBuilder.shaped(RecipeCategory.MISC, AMItems.CHEMICAL_CELL_HOUSING::get) .pattern("QRQ") .pattern("R R") @@ -34,7 +36,7 @@ protected void buildRecipes(Consumer consumer) { .define('R', Tags.Items.DUSTS_REDSTONE) .define('O', ItemTags.create(new ResourceLocation("forge", "ingots/osmium"))) .unlockedBy("has_dusts/redstone", has(Tags.Items.DUSTS_REDSTONE)) - .save(consumer, AppliedMekanistics.id("chemical_cell_housing")); + .save(output, AppliedMekanistics.id("chemical_cell_housing")); var housing = AMItems.CHEMICAL_CELL_HOUSING.get(); @@ -53,15 +55,15 @@ protected void buildRecipes(Consumer consumer) { .requires(housing) .requires(cellComponent) .unlockedBy("has_cell_component" + tierName, has(cellComponent)) - .save(consumer); + .save(output); ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, AMItems.getPortableCell(tier)::get) .requires(AEBlocks.CHEST) .requires(cellComponent) .requires(AEBlocks.ENERGY_CELL) .requires(housing) - .unlockedBy("has_" + housing.builtInRegistryHolder().key().location().getPath(), has(housing)) + .unlockedBy("has_" + BuiltInRegistries.ITEM.getKey(housing).getPath(), has(housing)) .unlockedBy("has_energy_cell", has(AEBlocks.ENERGY_CELL)) - .save(consumer); + .save(output); } } } 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 6658e20..75785f5 100644 --- a/src/main/java/me/ramidzkh/mekae2/integration/jade/AMJadePlugin.java +++ b/src/main/java/me/ramidzkh/mekae2/integration/jade/AMJadePlugin.java @@ -23,12 +23,12 @@ public class AMJadePlugin implements IWailaPlugin { @Override public void registerClient(IWailaClientRegistration registration) { - registration.addTooltipCollectedCallback((tooltip, accessor) -> { + registration.addTooltipCollectedCallback((box, accessor) -> { var target = accessor.getTarget(); if (target instanceof InterfaceLogicHost || target instanceof PatternProviderLogicHost) { for (var loc : CHEMICALS) { - tooltip.remove(loc); + box.getTooltip().remove(loc); } } }); diff --git a/src/main/java/me/ramidzkh/mekae2/integration/jei/AMJEIPlugin.java b/src/main/java/me/ramidzkh/mekae2/integration/jei/AMJEIPlugin.java deleted file mode 100644 index 966b805..0000000 --- a/src/main/java/me/ramidzkh/mekae2/integration/jei/AMJEIPlugin.java +++ /dev/null @@ -1,34 +0,0 @@ -package me.ramidzkh.mekae2.integration.jei; - -import net.minecraft.resources.ResourceLocation; - -import me.ramidzkh.mekae2.AppliedMekanistics; -import mekanism.api.IMekanismAccess; -import mezz.jei.api.IModPlugin; -import mezz.jei.api.JeiPlugin; -import mezz.jei.api.runtime.IJeiRuntime; - -import appeng.api.integrations.jei.IngredientConverters; - -@JeiPlugin -public class AMJEIPlugin implements IModPlugin { - - @Override - public void onRuntimeAvailable(IJeiRuntime jeiRuntime) { - var helper = IMekanismAccess.INSTANCE.jeiHelper(); - - IngredientConverters - .register(new ChemicalIngredientConverter<>(helper.getGasStackHelper().getIngredientType())); - IngredientConverters - .register(new ChemicalIngredientConverter<>(helper.getInfusionStackHelper().getIngredientType())); - IngredientConverters - .register(new ChemicalIngredientConverter<>(helper.getPigmentStackHelper().getIngredientType())); - IngredientConverters - .register(new ChemicalIngredientConverter<>(helper.getSlurryStackHelper().getIngredientType())); - } - - @Override - public ResourceLocation getPluginUid() { - return AppliedMekanistics.id("jei"); - } -} diff --git a/src/main/java/me/ramidzkh/mekae2/integration/jei/ChemicalIngredientConverter.java b/src/main/java/me/ramidzkh/mekae2/integration/jei/ChemicalIngredientConverter.java deleted file mode 100644 index 661c8a0..0000000 --- a/src/main/java/me/ramidzkh/mekae2/integration/jei/ChemicalIngredientConverter.java +++ /dev/null @@ -1,42 +0,0 @@ -package me.ramidzkh.mekae2.integration.jei; - -import org.jetbrains.annotations.Nullable; - -import me.ramidzkh.mekae2.ae2.MekanismKey; -import mekanism.api.chemical.ChemicalStack; -import mezz.jei.api.ingredients.IIngredientType; - -import appeng.api.integrations.jei.IngredientConverter; -import appeng.api.stacks.AEKey; -import appeng.api.stacks.GenericStack; - -public record ChemicalIngredientConverter>( - IIngredientType type) implements IngredientConverter { - - @Override - public IIngredientType getIngredientType() { - return type; - } - - @Nullable - @Override - public S getIngredientFromStack(GenericStack stack) { - if (stack.what() instanceof MekanismKey key && type.getIngredientClass().isInstance(key.getStack())) { - return (S) key.withAmount(Math.max(1, stack.amount())); - } else { - return null; - } - } - - @Nullable - @Override - public GenericStack getStackFromIngredient(S ingredient) { - AEKey what = MekanismKey.of(ingredient); - - if (what != null) { - return new GenericStack(what, ingredient.getAmount()); - } - - return null; - } -} diff --git a/src/main/java/me/ramidzkh/mekae2/qio/QioStorageAdapter.java b/src/main/java/me/ramidzkh/mekae2/qio/QioStorageAdapter.java index ff73d9f..0f32388 100644 --- a/src/main/java/me/ramidzkh/mekae2/qio/QioStorageAdapter.java +++ b/src/main/java/me/ramidzkh/mekae2/qio/QioStorageAdapter.java @@ -16,6 +16,7 @@ import mekanism.api.inventory.IHashedItem; import mekanism.api.inventory.qio.IQIOComponent; import mekanism.api.inventory.qio.IQIOFrequency; +import mekanism.api.security.ISecurityObject; import mekanism.api.security.ISecurityUtils; import mekanism.api.security.SecurityMode; @@ -30,8 +31,10 @@ * This generic trick allows us to capture both the BE and the IQIOComponent without depending on the actual Mekanism * block entity class. */ -public class QioStorageAdapter implements MEStorage { +public class QioStorageAdapter implements MEStorage { + private static final Map CACHE = new WeakHashMap<>(); + private final DASHBOARD dashboard; private final @Nullable Direction queriedSide; private final @Nullable UUID owner; @@ -48,20 +51,25 @@ public IQIOFrequency getFrequency() { if (dashboard.getBlockState().getValue(BlockStateProperties.FACING).getOpposite() != queriedSide) { return null; } + // Check that it has a frequency. var freq = dashboard.getQIOFrequency(); + if (freq == null || !freq.isValid()) { return null; } + // Check security. var utils = ISecurityUtils.INSTANCE; - var securityMode = utils.getSecurityMode(dashboard, dashboard.getLevel().isClientSide()); + var securityMode = utils.getSecurityMode(() -> dashboard, () -> dashboard, dashboard.getLevel().isClientSide()); + if (securityMode != SecurityMode.PUBLIC) { // Private or trusted: the player who placed the storage bus must have dashboard access. - if (!utils.canAccess(owner, dashboard, dashboard.getLevel().isClientSide())) { + if (!utils.canAccessObject(owner, dashboard, dashboard.getLevel().isClientSide())) { return null; } } + return freq; } @@ -69,11 +77,14 @@ public IQIOFrequency getFrequency() { public long insert(AEKey what, long amount, Actionable mode, IActionSource source) { if (what instanceof AEItemKey itemKey && amount > 0) { var freq = getFrequency(); + if (freq == null) { return 0; } + return freq.massInsert(itemKey.toStack(), amount, Action.fromFluidAction(mode.getFluidAction())); } + return 0; } @@ -81,22 +92,25 @@ public long insert(AEKey what, long amount, Actionable mode, IActionSource sourc public long extract(AEKey what, long amount, Actionable mode, IActionSource source) { if (what instanceof AEItemKey itemKey && amount > 0) { var freq = getFrequency(); + if (freq == null) { return 0; } + return freq.massExtract(itemKey.toStack(), amount, Action.fromFluidAction(mode.getFluidAction())); } + return 0; } @Override public void getAvailableStacks(KeyCounter out) { var freq = getFrequency(); + if (freq == null) { return; } - // Fixes #19 freq.forAllHashedStored((type, count) -> { // noinspection ConstantConditions out.add(CACHE.computeIfAbsent(type, it -> AEItemKey.of(it.getInternalStack())), count); @@ -106,9 +120,11 @@ public void getAvailableStacks(KeyCounter out) { @Override public Component getDescription() { var freq = getFrequency(); + if (freq == null) { throw new IllegalStateException("Unexpected null frequency!"); } + return AMText.QIO_FREQUENCY.formatted(freq.getName()); } } diff --git a/src/main/java/me/ramidzkh/mekae2/qio/QioSupport.java b/src/main/java/me/ramidzkh/mekae2/qio/QioSupport.java index 0fa8f43..493d6db 100644 --- a/src/main/java/me/ramidzkh/mekae2/qio/QioSupport.java +++ b/src/main/java/me/ramidzkh/mekae2/qio/QioSupport.java @@ -1,76 +1,50 @@ package me.ramidzkh.mekae2.qio; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import net.minecraft.core.Direction; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.block.entity.BlockEntity; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.common.capabilities.CapabilityManager; -import net.minecraftforge.common.capabilities.CapabilityToken; -import net.minecraftforge.common.capabilities.ICapabilityProvider; -import net.minecraftforge.common.util.LazyOptional; -import net.minecraftforge.event.AttachCapabilitiesEvent; -import net.minecraftforge.registries.ForgeRegistries; +import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; -import me.ramidzkh.mekae2.AppliedMekanistics; +import mekanism.api.MekanismAPI; import mekanism.api.inventory.qio.IQIOComponent; +import mekanism.api.security.ISecurityObject; import appeng.api.networking.GridHelper; -import appeng.api.storage.MEStorage; +import appeng.capabilities.AppEngCapabilities; public class QioSupport { - public static final Capability STORAGE = CapabilityManager - .get(new CapabilityToken<>() { - }); - private static final ResourceLocation DASHBOARD = new ResourceLocation("mekanism", "qio_dashboard"); - - public static void initialize() { - MinecraftForge.EVENT_BUS.addGenericListener(BlockEntity.class, QioSupport::onBlockEntityCapability); - } - - public static void onBlockEntityCapability(AttachCapabilitiesEvent event) { - var object = event.getObject(); - - if (object instanceof IQIOComponent - && DASHBOARD.equals(ForgeRegistries.BLOCK_ENTITY_TYPES.getKey(object.getType()))) { - event.addCapability(AppliedMekanistics.id("qio_storage_monitorable"), new ICapabilityProvider() { - @NotNull - @Override - public LazyOptional getCapability(@NotNull Capability capability, @Nullable Direction arg) { - out: if (capability == STORAGE && arg != null) { - // guess the source... - // if you're trying to qio across a compact machine wall or something, sorry! - var host = GridHelper.getNodeHost(object.getLevel(), object.getBlockPos().relative(arg)); - if (host == null) { - break out; - } + public static void onBlockEntityCapability(RegisterCapabilitiesEvent event) { + event.registerBlock(AppEngCapabilities.ME_STORAGE, (level, pos, state, be, side) -> { + if (be != null && side != null) { + // guess the source... + // if you're trying to qio across a compact machine wall or something, sorry! + var host = GridHelper.getNodeHost(level, pos.relative(side)); - var source = host.getGridNode(arg.getOpposite()); + if (host == null) { + return null; + } - // I don't know of any full-block nodes which query inventories, but we'll see - if (source == null) { - source = host.getGridNode(null); - } + var source = host.getGridNode(side.getOpposite()); - if (source == null) { - break out; - } + // I don't know of any full-block nodes which query inventories, but we'll see + if (source == null) { + source = host.getGridNode(null); + } - var adapter = new QioStorageAdapter<>((BlockEntity & IQIOComponent) object, arg, - source.getOwningPlayerProfileId()); + if (source == null) { + return null; + } - if (adapter.getFrequency() != null) { - return LazyOptional.of(() -> adapter).cast(); - } - } + var adapter = new QioStorageAdapter<>((BlockEntity & IQIOComponent & ISecurityObject) be, side, + source.getOwningPlayerProfileId()); - return LazyOptional.empty(); + if (adapter.getFrequency() != null) { + return adapter; } - }); - } + } + + return null; + }, BuiltInRegistries.BLOCK.get(new ResourceLocation(MekanismAPI.MEKANISM_MODID, "qio_dashboard"))); } } diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 8d72233..e59a0c5 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -1,11 +1,6 @@ modLoader = "javafml" -loaderVersion = "[43,)" -#updateJSONURL="" +loaderVersion = "${loader_version_range}" issueTrackerURL = "https://github.com/AppliedEnergistics/Applied-Mekanistics/issues" -displayURL = "https://github.com/AppliedEnergistics/Applied-Mekanistics#readme" -logoFile = "logo.png" -#credits="Thanks for this example mod goes to Java" -authors = "ramidzkh" license = "See GitHub repository for details" [[mods]] @@ -14,24 +9,27 @@ version = "${version}" displayName = "Applied Mekanistics" description = "Use Mekanism resources through your ME network" updateJSONURL = "https://api.modrinth.com/updates/applied-mekanistics/forge_updates.json" +displayURL = "https://github.com/AppliedEnergistics/Applied-Mekanistics#readme" +logoFile = "logo.png" +authors = "ramidzkh" [[dependencies.appmek]] -modId = "forge" -mandatory = true -versionRange = "[47.0.0,48.0.0)" +modId = "neoforge" +type = "required" +versionRange = "${neo_version_range}" ordering = "NONE" side = "BOTH" [[dependencies.appmek]] modId = "ae2" -mandatory = false -versionRange = "[15.0.0,16.0.0)" +type = "required" +versionRange = "${ae2_version_range}" ordering = "AFTER" side = "BOTH" [[dependencies.appmek]] modId = "mekanism" -mandatory = true -versionRange = "[10.4.0,11.0.0)" +type = "required" +versionRange = "${mekanism_version_range}" ordering = "AFTER" side = "BOTH"