Skip to content

Commit

Permalink
wip: fix capability loader on forge
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Jan 9, 2024
1 parent 9824201 commit 2d926c8
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ public void onInitialize() {
Balm.initialize(CookingForBlockheads.MOD_ID, CookingForBlockheads::initialize);

registerProvider("kitchen_item_provider", KitchenItemProvider.class,
ModBlockEntities.counter.get(), ModBlockEntities.milkJar.get(), ModBlockEntities.fridge.get(),
ModBlockEntities.toolRack.get(), ModBlockEntities.fruitBasket.get(), ModBlockEntities.oven.get(),
ModBlockEntities.sink.get(), ModBlockEntities.spiceRack.get(), ModBlockEntities.cabinet.get(),
ModBlockEntities.cowJar.get(), ModBlockEntities.cuttingBoard.get());
ModBlockEntities.milkJar.get(), ModBlockEntities.cowJar.get(), ModBlockEntities.fridge.get(),
ModBlockEntities.sink.get(), ModBlockEntities.cuttingBoard.get());

registerProvider("kitchen_smelting_provider", KitchenItemProcessor.class, ModBlockEntities.oven.get());
registerProvider("kitchen_item_processor", KitchenItemProcessor.class, ModBlockEntities.oven.get());

registerLookup(new ResourceLocation("balm", "fluid_tank"), FluidTank.class,
ModBlockEntities.sink.get(), ModBlockEntities.milkJar.get(), ModBlockEntities.cowJar.get());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package net.blay09.mods.cookingforblockheads.compat;
package net.blay09.mods.cookingforblockheads;

import net.blay09.mods.cookingforblockheads.CookingForBlockheads;
import net.blay09.mods.cookingforblockheads.ForgeCookingForBlockheads;
import net.blay09.mods.cookingforblockheads.api.capability.*;
import net.blay09.mods.cookingforblockheads.api.KitchenItemProvider;
import net.blay09.mods.cookingforblockheads.tag.ModBlockTags;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand All @@ -15,46 +14,27 @@
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.Nullable;

@Mod.EventBusSubscriber(modid = CookingForBlockheads.MOD_ID)
public class CompatCapabilityLoader {
public class CapabilityBinder {

private static ResourceLocation itemProviderResourceKey;
private static ResourceLocation connectorResourceKey;
private static final ResourceLocation itemProviderResourceKey = new ResourceLocation(CookingForBlockheads.MOD_ID, "kitchen_item_provider");

@SubscribeEvent
public static void attachTileEntityCapabilities(AttachCapabilitiesEvent<BlockEntity> event) {
BlockEntity blockEntity = event.getObject();

ResourceLocation blockEntityRegistryName = ForgeRegistries.BLOCK_ENTITY_TYPES.getKey(blockEntity.getType());
if (JsonCompatLoader.kitchenItemProviders.contains(blockEntityRegistryName)) {
if (itemProviderResourceKey == null) {
itemProviderResourceKey = new ResourceLocation(CookingForBlockheads.MOD_ID, "kitchen_item_provider");
}

if (blockEntity.getBlockState().is(ModBlockTags.KITCHEN_ITEM_PROVIDERS)) {
event.addCapability(itemProviderResourceKey, new KitchenItemCapabilityProvider(blockEntity));
}

if (JsonCompatLoader.kitchenConnectors.contains(blockEntityRegistryName)) {
if (connectorResourceKey == null) {
connectorResourceKey = new ResourceLocation(CookingForBlockheads.MOD_ID, "kitchen_connector");
}

if (connectorCapabilityProvider == null) {
connectorCapabilityProvider = new KitchenConnectorCapabilityProvider();
}

event.addCapability(connectorResourceKey, connectorCapabilityProvider);
}
}

private final static ItemStackHandler emptyItemHandler = new ItemStackHandler(0);

private final static class KitchenItemCapabilityProvider implements ICapabilityProvider {

private final LazyOptional<IKitchenItemProvider> itemProviderCap;
private final LazyOptional<KitchenItemProvider> itemProviderCap;

public KitchenItemCapabilityProvider(final BlockEntity entity) {
itemProviderCap = LazyOptional.of(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ForgeCookingForBlockheads {
public static Capability<KitchenItemProvider> KITCHEN_ITEM_PROVIDER_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {
});

public static Capability<KitchenItemProcessor> KITCHEN_SMELTING_PROVIDER_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {
public static Capability<KitchenItemProcessor> KITCHEN_ITEM_PROCESSOR_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {
});

public ForgeCookingForBlockheads() {
Expand All @@ -36,10 +36,8 @@ public ForgeCookingForBlockheads() {
Balm.initialize(CookingForBlockheads.MOD_ID, CookingForBlockheads::initialize);
DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> BalmClient.initialize(CookingForBlockheads.MOD_ID, CookingForBlockheadsClient::initialize));

FMLJavaModLoadingContext.get().getModEventBus().addListener(IMCHandler::processIMC);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::registerCapabilities);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
MinecraftForge.EVENT_BUS.addListener(IMCHandler::onFoodRegistryInit);

ForgeBalmProviders providers = (ForgeBalmProviders) Balm.getProviders();
providers.register(KitchenItemProvider.class, new CapabilityToken<>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public NeoForgeCookingForBlockheads(IEventBus eventBus) {
Balm.initialize(CookingForBlockheads.MOD_ID, CookingForBlockheads::initialize);
DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> BalmClient.initialize(CookingForBlockheads.MOD_ID, CookingForBlockheadsClient::initialize));

eventBus.addListener(IMCHandler::processIMC);
eventBus.addListener(this::enqueueIMC);
NeoForge.EVENT_BUS.addListener(IMCHandler::onFoodRegistryInit);
}

private void enqueueIMC(InterModEnqueueEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.world.level.block.Block;

public class ModBlockTags {
public static final TagKey<Block> KITCHEN_ITEM_PROVIDERS = TagKey.create(Registries.BLOCK, new ResourceLocation(CookingForBlockheads.MOD_ID, "kitchen_item_providers"));
public static final TagKey<Block> KITCHEN_CONNECTORS = TagKey.create(Registries.BLOCK, new ResourceLocation(CookingForBlockheads.MOD_ID, "kitchen_connectors"));
public static final TagKey<Block> DYEABLE = TagKey.create(Registries.BLOCK, new ResourceLocation(CookingForBlockheads.MOD_ID, "dyeable"));
}

0 comments on commit 2d926c8

Please sign in to comment.