From c77cf95248814571f80d9f5132c37bc95bda178b Mon Sep 17 00:00:00 2001 From: TelepathicGrunt <40846040+TelepathicGrunt@users.noreply.github.com> Date: Sat, 13 Jul 2024 13:54:48 -0400 Subject: [PATCH] Run CapabilityHooks::registerVanillaProviders at LOW priority (#1296) --- .../capabilities/CapabilityHooks.java | 44 +++++++++++-------- .../neoforge/common/NeoForgeMod.java | 2 + 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/neoforged/neoforge/capabilities/CapabilityHooks.java b/src/main/java/net/neoforged/neoforge/capabilities/CapabilityHooks.java index b0a0a9b1f2..73fe62a7f4 100644 --- a/src/main/java/net/neoforged/neoforge/capabilities/CapabilityHooks.java +++ b/src/main/java/net/neoforged/neoforge/capabilities/CapabilityHooks.java @@ -112,26 +112,8 @@ public static void registerVanillaProviders(RegisterCapabilitiesEvent event) { event.registerEntity(Capabilities.ItemHandler.ENTITY_AUTOMATION, entityType, (entity, ctx) -> new InvWrapper(entity)); } event.registerEntity(Capabilities.ItemHandler.ENTITY, EntityType.PLAYER, (player, ctx) -> new PlayerInvWrapper(player.getInventory())); - // Register to all entity types to make sure we support all living entity subclasses. - for (EntityType entityType : BuiltInRegistries.ENTITY_TYPE) { - event.registerEntity(Capabilities.ItemHandler.ENTITY, entityType, (entity, ctx) -> { - if (entity instanceof AbstractHorse horse) - return new InvWrapper(horse.getInventory()); - else if (entity instanceof LivingEntity livingEntity) - return new CombinedInvWrapper(new EntityHandsInvWrapper(livingEntity), new EntityArmorInvWrapper(livingEntity)); - - return null; - }); - } // Items - for (Item item : BuiltInRegistries.ITEM) { - if (item.getClass() == BucketItem.class) - event.registerItem(Capabilities.FluidHandler.ITEM, (stack, ctx) -> new FluidBucketWrapper(stack), item); - } - if (NeoForgeMod.MILK.isBound()) { - event.registerItem(Capabilities.FluidHandler.ITEM, (stack, ctx) -> new FluidBucketWrapper(stack), Items.MILK_BUCKET); - } event.registerItem(Capabilities.ItemHandler.ITEM, (stack, ctx) -> new ComponentItemHandler(stack, DataComponents.CONTAINER, 27), Items.SHULKER_BOX, Items.BLACK_SHULKER_BOX, @@ -152,6 +134,32 @@ else if (entity instanceof LivingEntity livingEntity) Items.YELLOW_SHULKER_BOX); } + public static void registerFallbackVanillaProviders(RegisterCapabilitiesEvent event) { + // Entities + // Register to all entity types to make sure we support all living entity subclasses. + for (EntityType entityType : BuiltInRegistries.ENTITY_TYPE) { + event.registerEntity(Capabilities.ItemHandler.ENTITY, entityType, (entity, ctx) -> { + if (entity instanceof AbstractHorse horse) + return new InvWrapper(horse.getInventory()); + else if (entity instanceof LivingEntity livingEntity) + return new CombinedInvWrapper(new EntityHandsInvWrapper(livingEntity), new EntityArmorInvWrapper(livingEntity)); + + return null; + }); + } + + // Items + for (Item item : BuiltInRegistries.ITEM) { + if (item.getClass() == BucketItem.class) + event.registerItem(Capabilities.FluidHandler.ITEM, (stack, ctx) -> new FluidBucketWrapper(stack), item); + } + + // We want mods to be able to override our milk cap by default + if (NeoForgeMod.MILK.isBound()) { + event.registerItem(Capabilities.FluidHandler.ITEM, (stack, ctx) -> new FluidBucketWrapper(stack), Items.MILK_BUCKET); + } + } + public static void invalidateCapsOnChunkLoad(ChunkEvent.Load event) { if (event.getLevel() instanceof ServerLevel sl) { sl.invalidateCapabilities(event.getChunk().getPos()); diff --git a/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java b/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java index 055d78b455..693d9973ea 100644 --- a/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java +++ b/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java @@ -73,6 +73,7 @@ import net.minecraft.world.level.pathfinder.PathType; import net.minecraft.world.phys.Vec3; import net.neoforged.api.distmarker.Dist; +import net.neoforged.bus.api.EventPriority; import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.CrashReportCallables; import net.neoforged.fml.ModContainer; @@ -617,6 +618,7 @@ public NeoForgeMod(IEventBus modEventBus, Dist dist, ModContainer container) { TagConventionLogWarning.init(); modEventBus.addListener(CapabilityHooks::registerVanillaProviders); + modEventBus.addListener(EventPriority.LOW, CapabilityHooks::registerFallbackVanillaProviders); modEventBus.addListener(CauldronFluidContent::registerCapabilities); // These 3 listeners use the default priority for now, can be re-evaluated later. NeoForge.EVENT_BUS.addListener(CapabilityHooks::invalidateCapsOnChunkLoad);