From 57c72711bb6382f44f25390e0bced771151046b0 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Fri, 22 Mar 2024 20:19:32 +0000 Subject: [PATCH] Use a platform method to register item properties The two mod loaders expose different methods for this (Forge's method takes a ItemPropertyFunction, Fabric's a ClampedItemPropertyFunction). This is fine in a Gradle build, as the methods are compatible. However, when running from IntelliJ, we get crashes as the common code tries to reference the wrong method. We now pass in the method reference instead, ensuring we use the right method on each loader. --- .../computercraft/client/ClientRegistry.java | 20 ++++++++++++++----- .../computercraft-common.accesswidener | 1 - .../client/ComputerCraftClient.java | 3 ++- .../client/ForgeClientRegistry.java | 3 ++- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/projects/common/src/client/java/dan200/computercraft/client/ClientRegistry.java b/projects/common/src/client/java/dan200/computercraft/client/ClientRegistry.java index 286343dbe..5e393cbfa 100644 --- a/projects/common/src/client/java/dan200/computercraft/client/ClientRegistry.java +++ b/projects/common/src/client/java/dan200/computercraft/client/ClientRegistry.java @@ -78,8 +78,10 @@ public final class ClientRegistry { /** * Register any client-side objects which must be done on the main thread. + * + * @param itemProperties Callback to register item properties. */ - public static void registerMainThread() { + public static void registerMainThread(RegisterItemProperty itemProperties) { MenuScreens.>register(ModRegistry.Menus.COMPUTER.get(), ComputerScreen::new); MenuScreens.>register(ModRegistry.Menus.POCKET_COMPUTER.get(), ComputerScreen::new); MenuScreens.>register(ModRegistry.Menus.POCKET_COMPUTER_NO_TERM.get(), NoTermComputerScreen::new); @@ -91,14 +93,14 @@ public final class ClientRegistry { MenuScreens.>register(ModRegistry.Menus.VIEW_COMPUTER.get(), ComputerScreen::new); - registerItemProperty("state", + registerItemProperty(itemProperties, "state", new UnclampedPropertyFunction((stack, world, player, random) -> { var computer = ClientPocketComputers.get(stack); return (computer == null ? ComputerState.OFF : computer.getState()).ordinal(); }), ModRegistry.Items.POCKET_COMPUTER_NORMAL, ModRegistry.Items.POCKET_COMPUTER_ADVANCED ); - registerItemProperty("coloured", + registerItemProperty(itemProperties, "coloured", (stack, world, player, random) -> IColouredItem.getColourBasic(stack) != -1 ? 1 : 0, ModRegistry.Items.POCKET_COMPUTER_NORMAL, ModRegistry.Items.POCKET_COMPUTER_ADVANCED ); @@ -119,9 +121,17 @@ public final class ClientRegistry { } @SafeVarargs - private static void registerItemProperty(String name, ClampedItemPropertyFunction getter, Supplier... items) { + private static void registerItemProperty(RegisterItemProperty itemProperties, String name, ClampedItemPropertyFunction getter, Supplier... items) { var id = new ResourceLocation(ComputerCraftAPI.MOD_ID, name); - for (var item : items) ItemProperties.register(item.get(), id, getter); + for (var item : items) itemProperties.register(item.get(), id, getter); + } + + /** + * Register an item property via {@link ItemProperties#register}. Forge and Fabric expose different methods, so we + * supply this via mod-loader-specific code. + */ + public interface RegisterItemProperty { + void register(Item item, ResourceLocation name, ClampedItemPropertyFunction property); } public static void registerReloadListeners(Consumer register, Minecraft minecraft) { diff --git a/projects/common/src/main/resources/computercraft-common.accesswidener b/projects/common/src/main/resources/computercraft-common.accesswidener index 71c43c6fe..f99a8f0e1 100644 --- a/projects/common/src/main/resources/computercraft-common.accesswidener +++ b/projects/common/src/main/resources/computercraft-common.accesswidener @@ -7,7 +7,6 @@ accessWidener v1 named # Additional access wideners for vanilla code. This is a effectively the subset of Fabric's transitive access wideners # that we actually use -accessible method net/minecraft/client/renderer/item/ItemProperties register (Lnet/minecraft/world/item/Item;Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/client/renderer/item/ClampedItemPropertyFunction;)V accessible method net/minecraft/client/renderer/blockentity/BlockEntityRenderers register (Lnet/minecraft/world/level/block/entity/BlockEntityType;Lnet/minecraft/client/renderer/blockentity/BlockEntityRendererProvider;)V accessible class net/minecraft/world/item/CreativeModeTab$Output accessible field net/minecraft/world/item/CreativeModeTabs OP_BLOCKS Lnet/minecraft/resources/ResourceKey; diff --git a/projects/fabric/src/client/java/dan200/computercraft/client/ComputerCraftClient.java b/projects/fabric/src/client/java/dan200/computercraft/client/ComputerCraftClient.java index ec16c4ecb..4a4f89f73 100644 --- a/projects/fabric/src/client/java/dan200/computercraft/client/ComputerCraftClient.java +++ b/projects/fabric/src/client/java/dan200/computercraft/client/ComputerCraftClient.java @@ -27,6 +27,7 @@ import net.fabricmc.fabric.api.event.client.player.ClientPickBlockGatherCallback import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.item.ItemProperties; import net.minecraft.world.item.ItemStack; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.HitResult; @@ -45,7 +46,7 @@ public class ComputerCraftClient { ClientRegistry.register(); ClientRegistry.registerTurtleModellers(FabricComputerCraftAPIClient::registerTurtleUpgradeModeller); ClientRegistry.registerItemColours(ColorProviderRegistry.ITEM::register); - ClientRegistry.registerMainThread(); + ClientRegistry.registerMainThread(ItemProperties::register); PreparableModelLoadingPlugin.register(CustomModelLoader::prepare, (state, context) -> { ClientRegistry.registerExtraModels(context::addModels); diff --git a/projects/forge/src/client/java/dan200/computercraft/client/ForgeClientRegistry.java b/projects/forge/src/client/java/dan200/computercraft/client/ForgeClientRegistry.java index 89e9ea8b2..cd6977a35 100644 --- a/projects/forge/src/client/java/dan200/computercraft/client/ForgeClientRegistry.java +++ b/projects/forge/src/client/java/dan200/computercraft/client/ForgeClientRegistry.java @@ -8,6 +8,7 @@ import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.api.client.turtle.RegisterTurtleModellersEvent; import dan200.computercraft.client.model.turtle.TurtleModelLoader; import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.item.ItemProperties; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.client.event.ModelEvent; import net.minecraftforge.client.event.RegisterClientReloadListenersEvent; @@ -82,6 +83,6 @@ public final class ForgeClientRegistry { @SubscribeEvent public static void setupClient(FMLClientSetupEvent event) { ClientRegistry.register(); - event.enqueueWork(ClientRegistry::registerMainThread); + event.enqueueWork(() -> ClientRegistry.registerMainThread(ItemProperties::register)); } }