Jump to content

Recommended Posts

Posted

Basically I am trying to save the players "balance" inside a LivingEntity capability. Currently anytime I run my tests it seems the capability is not getting attached to the player. Currently im not getting any errors, just nothing

My provider class:

Spoiler
package com.tompkins_development.forge.farming_valley.capabilities.coin;

import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CoinsCapabilityProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> {

    public static Capability<Coins> COINS = CapabilityManager.get(new CapabilityToken<Coins>() {});

    private Coins coins = null;
    private LazyOptional<Coins> optional = LazyOptional.of(this::createCoins);

    private Coins createCoins() {
        if(this.coins == null) {
            this.coins = new Coins();
        }

        return this.coins;
    }

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

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag nbt = new CompoundTag();
        createCoins().serializeNBT(nbt);
        return nbt;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        createCoins().deserializeNBT(nbt);
    }
}

 

My Coin class:

Spoiler
package com.tompkins_development.forge.farming_valley.capabilities.coin;

import com.tompkins_development.forge.farming_valley.enums.Season;
import net.minecraft.nbt.CompoundTag;

public class Coins {

    private int balance;
    private static final String NBT_BALANCE = "balance";

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    public CompoundTag serializeNBT(CompoundTag nbt) {
        nbt.putInt(NBT_BALANCE, balance);
        return nbt;
    }

    public void deserializeNBT(CompoundTag nbt) {
        this.balance = nbt.getInt(NBT_BALANCE);
    }
}

 

 

My CapabilityEvent class

Spoiler
package com.tompkins_development.forge.farming_valley.capabilities;

import com.tompkins_development.forge.farming_valley.FarmingValleyMod;
import com.tompkins_development.forge.farming_valley.capabilities.coin.CoinsCapabilityProvider;
import com.tompkins_development.forge.farming_valley.capabilities.season.SeasonCapabilityProvider;
import com.tompkins_development.forge.farming_valley.capabilities.sleeping.SleepingCapabilityProvider;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.capabilities.RegisterCapabilitiesEvent;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = FarmingValleyMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class CapabilityEvents {

    @SubscribeEvent
    public static void attachLevel(final AttachCapabilitiesEvent<Level> event) {
        if(!event.getObject().getCapability(SeasonCapabilityProvider.SEASON_AND_DAY).isPresent()) {
            event.addCapability(new ResourceLocation(FarmingValleyMod.MOD_ID, "season"), new SeasonCapabilityProvider());
        }
        if(!event.getObject().getCapability(SleepingCapabilityProvider.SLEEPING).isPresent()) {
            event.addCapability(new ResourceLocation(FarmingValleyMod.MOD_ID, "sleeping"), new SleepingCapabilityProvider());
        }
    }

    @SubscribeEvent
    public static void attach(final AttachCapabilitiesEvent<LivingEntity> event) {
        if (event.getObject() instanceof Player player) {
            if (!player.getCapability(CoinsCapabilityProvider.COINS).isPresent()) {
                event.addCapability(new ResourceLocation(FarmingValleyMod.MOD_ID, "balance"), new CoinsCapabilityProvider());
            }
        }
    }

    @SubscribeEvent
    public static void register(RegisterCapabilitiesEvent event) {
        event.register(SeasonCapabilityProvider.class);
        event.register(SleepingCapabilityProvider.class);
        event.register(CoinsCapabilityProvider.class);
    }

}

 

And finally my Tester class snippet:

Spoiler
        @SubscribeEvent
        public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
            if (event.side != LogicalSide.SERVER) return;
            ServerPlayer player = (ServerPlayer) event.player;
            ServerLevel level = (ServerLevel) event.player.getLevel();

            player.getCapability(CoinsCapabilityProvider.COINS).ifPresent(coinProvider -> {
                coinProvider.setBalance(coinProvider.getBalance() + 1);
                System.out.println(coinProvider.getBalance());
            });
        }

 

 

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.