diff --git a/projects/common/build.gradle.kts b/projects/common/build.gradle.kts index 2cf545c63..46120488f 100644 --- a/projects/common/build.gradle.kts +++ b/projects/common/build.gradle.kts @@ -36,6 +36,7 @@ dependencies { implementation(commonClasses(project(":common-api"))) clientImplementation(clientClasses(project(":common-api"))) + compileOnly(libs.mixin) compileOnly(libs.bundles.externalMods.common) clientCompileOnly(variantOf(libs.emi) { classifier("api") }) diff --git a/projects/common/src/main/java/dan200/computercraft/mixin/V1460Mixin.java b/projects/common/src/main/java/dan200/computercraft/mixin/V1460Mixin.java new file mode 100644 index 000000000..282b552d7 --- /dev/null +++ b/projects/common/src/main/java/dan200/computercraft/mixin/V1460Mixin.java @@ -0,0 +1,53 @@ +// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers +// +// SPDX-License-Identifier: MPL-2.0 + +package dan200.computercraft.mixin; + +import com.mojang.datafixers.DSL; +import com.mojang.datafixers.schemas.Schema; +import com.mojang.datafixers.types.templates.TypeTemplate; +import dan200.computercraft.shared.ModRegistry; +import dan200.computercraft.shared.peripheral.diskdrive.DiskDriveBlockEntity; +import dan200.computercraft.shared.peripheral.printer.PrinterBlockEntity; +import dan200.computercraft.shared.turtle.blocks.TurtleBlockEntity; +import net.minecraft.util.datafix.fixes.References; +import net.minecraft.util.datafix.schemas.V1460; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.util.Map; +import java.util.function.Supplier; + +/** + * Register our item-contianing block entities. + * + * @see DiskDriveBlockEntity + * @see PrinterBlockEntity + * @see TurtleBlockEntity + */ +@Mixin(V1460.class) +class V1460Mixin { + @Inject(at = @At("RETURN"), method = "registerBlockEntities") + @SuppressWarnings("UnusedMethod") + private void registerBlockEntities(Schema schema, CallbackInfoReturnable>> ci) { + var map = ci.getReturnValue(); + + // Basic inventories + registerInventory(schema, map, ModRegistry.BlockEntities.TURTLE_NORMAL.id().toString()); + registerInventory(schema, map, ModRegistry.BlockEntities.TURTLE_ADVANCED.id().toString()); + registerInventory(schema, map, ModRegistry.BlockEntities.PRINTER.id().toString()); + + // Disk drives contain a single item + schema.register(map, ModRegistry.BlockEntities.DISK_DRIVE.id().toString(), () -> DSL.optionalFields( + "Item", References.ITEM_STACK.in(schema) + )); + } + + @Shadow + protected static void registerInventory(Schema schema, Map> map, String name) { + } +} diff --git a/projects/common/src/main/resources/computercraft.mixins.json b/projects/common/src/main/resources/computercraft.mixins.json new file mode 100644 index 000000000..adade5ea5 --- /dev/null +++ b/projects/common/src/main/resources/computercraft.mixins.json @@ -0,0 +1,13 @@ +{ + "required": true, + "package": "dan200.computercraft.mixin", + "minVersion": "0.8", + "compatibilityLevel": "JAVA_17", + "injectors": { + "defaultRequire": 1 + }, + "mixins": [ + "V1460Mixin" + ], + "refmap": "computercraft.refmap.json" +} diff --git a/projects/fabric/src/main/resources/fabric.mod.json b/projects/fabric/src/main/resources/fabric.mod.json index 0a93dd75c..33e99e61b 100644 --- a/projects/fabric/src/main/resources/fabric.mod.json +++ b/projects/fabric/src/main/resources/fabric.mod.json @@ -37,6 +37,7 @@ ] }, "mixins": [ + "computercraft.mixins.json", "computercraft.fabric.mixins.json", { "config": "computercraft-client.fabric.mixins.json", diff --git a/projects/forge/src/main/resources/META-INF/mods.toml b/projects/forge/src/main/resources/META-INF/mods.toml index 7e3e156c6..5502fb35f 100644 --- a/projects/forge/src/main/resources/META-INF/mods.toml +++ b/projects/forge/src/main/resources/META-INF/mods.toml @@ -30,5 +30,8 @@ CC: Tweaked is a fork of ComputerCraft, adding programmable computers, turtles a ordering="NONE" side="BOTH" +[[mixins]] +config = "computercraft.mixins.json" + [[mixins]] config = "computercraft-client.forge.mixins.json"