Skip to content

Commit

Permalink
Run CapabilityHooks::registerVanillaProviders at LOW priority (#1296)
Browse files Browse the repository at this point in the history
  • Loading branch information
TelepathicGrunt authored Jul 13, 2024
1 parent 82f02ae commit c77cf95
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c77cf95

Please sign in to comment.