Jump to content

Recommended Posts

Posted

I'm trying to attach a capability to a chunk, but I can't do it, I did it the following way and it doesn't work, what's wrong? How do I solve it?

 

public class BloodDensityProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> {
    public static Capability<TitanBloodDensity> TITAN_BLOOD_HANDLER = CapabilityManager.get(new CapabilityToken<>() {});

    private TitanBloodDensity density;
    private final LazyOptional<TitanBloodDensity> optional = LazyOptional.of(this::getDensity);

    public TitanBloodDensity getDensity() {
        if (density == null) density = new TitanBloodDensity();
        return density;
    }

    @Override
    public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
        if (cap == TITAN_BLOOD_HANDLER) return optional.cast();
        else return LazyOptional.empty();
    }

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag nbt = new CompoundTag();
        nbt.putInt("density", getDensity().getAmount());

        return nbt;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        getDensity().set(nbt.getInt("density"));
    }
}
public class TitanBloodDensity implements ITitanBloodHandler {

    private int bloodAmount = 0;

    @Override
    public int getAmount() {
        return bloodAmount;
    }

    @Override
    public void sub(int amount) {
        bloodAmount -= amount;
    }

    @Override
    public void add(int amount) {
        bloodAmount += amount;
    }

    @Override
    public void set(int value) {
        bloodAmount = value;
    }
}
@Mod.EventBusSubscriber(modid = MODID)
public class ModEvent {

    [...]

    @SubscribeEvent
    public void onAttachChunkCapabilitiesChunk(AttachCapabilitiesEvent<LevelChunk> event) {
        if (!event.getObject().getCapability(TITAN_BLOOD_HANDLER).isPresent()) {
            event.addCapability(new ResourceLocation(MODID, "proprieties/blood_density"), new BloodDensityProvider());
        }
    }

    [...]

    @SubscribeEvent
    public static void onRegisterCapabilities(RegisterCapabilitiesEvent event) {
        event.register(PlayerGCTXProvider.class);
        event.register(BloodDensityProvider.class);
    }

    [...]
}
public class SprayerBlockEntity extends FluidHandlerBlockEntity implements MenuProvider {
    [...]

    public boolean trySpray() {
        if (tank.getFluidAmount() >= BUCKET_VOLUME) {
            assert level != null;

            LazyOptional<TitanBloodDensity> capability = level.getChunkAt(getBlockPos())
                                                         .getCapability(TITAN_BLOOD_HANDLER);
            capability.ifPresent(handler -> {
                handler.add(100);
                tank.drain(BUCKET_VOLUME, IFluidHandler.FluidAction.EXECUTE);
            });

            System.out.println(capability.isPresent()); // false

            return capability.isPresent();
        }

        return false;
    }
}

I did the same method for the player and it worked, why doesn't it work with chunk?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.