Cardinal Components API

Cardinal Components API

21M Downloads

"Null" value when first join server

Sam-Chai opened this issue ยท 0 comments

commented

#172

I changed my code, but nothing worked.
First, is my interface (I add CommonTickingComponent, AutoSyncedComponent into this):

public interface IPlayerEcoHelperData extends PlayerComponent, CommonTickingComponent, AutoSyncedComponent {
    Double getMoney();
    Boolean getIsFirstJoin();
    void setMoney(Double money);
    void setIsFirstJoin(Boolean isFirstJoin);

}

And this is my subclass:

public class PlayerEcoHelperData implements IPlayerEcoHelperData, AutoSyncedComponent {
    private final Player provider;
    private Double playerMoney;
    private Boolean playerIsFirstJoin = true;
    public PlayerEcoHelperData(Player provider) {
        this.provider = provider;
    }


    @Override
    public Double getMoney() {
        this.provider.syncComponent(CapabilityRegister.PLAYER_DATA);
        CapabilityRegister.PLAYER_DATA.sync(this.provider);
        return playerMoney;
    }

    @Override
    public Boolean getIsFirstJoin() {
        this.provider.syncComponent(CapabilityRegister.PLAYER_DATA);
        CapabilityRegister.PLAYER_DATA.sync(this.provider);
        return playerIsFirstJoin;
    }

    @Override
    public void setMoney(Double money) {
        this.playerMoney = money;
        this.provider.syncComponent(CapabilityRegister.PLAYER_DATA);
        CapabilityRegister.PLAYER_DATA.sync(this.provider);
    }

    @Override
    public void setIsFirstJoin(Boolean isFirstJoin) {
        this.playerIsFirstJoin = isFirstJoin;
        this.provider.syncComponent(CapabilityRegister.PLAYER_DATA);
        CapabilityRegister.PLAYER_DATA.sync(this.provider);
    }

    @Override
    public void readFromNbt(CompoundTag tag) {
        playerMoney = tag.getDouble("money");
        playerIsFirstJoin = tag.getBoolean("isFirstJoin");
    }

    @Override
    public void writeToNbt(CompoundTag tag) {
        tag.putDouble("money", playerMoney);
        tag.putBoolean("isFirstJoin", playerIsFirstJoin);
    }

    @Override
    public void tick() {
        this.provider.syncComponent(CapabilityRegister.PLAYER_DATA);
        CapabilityRegister.PLAYER_DATA.sync(this.provider);
    }
}

Registry class:

public class CapabilityRegister implements EntityComponentInitializer {
    public static final ComponentKey<IPlayerEcoHelperData> PLAYER_DATA = ComponentRegistry.getOrCreate(new ResourceLocation(EcoHelper.MODID, "player_eco_helper_data"), IPlayerEcoHelperData.class);

    @Override
    public void registerEntityComponentFactories(EntityComponentFactoryRegistry registry) {
//        registry.beginRegistration(Player.class, PLAYER_DATA).impl(PlayerEcoHelperData.class).end(PlayerEcoHelperData::new);
        registry.registerForPlayers(PLAYER_DATA, PlayerEcoHelperData::new, RespawnCopyStrategy.ALWAYS_COPY);
    }
}

I tried the registry.beginRegistration and the registry.registerForPlayers, but everything not change too much.
For now, when player join the server, the client-side tells me "Null" for player's balance. But on server-side (run a command on console), then returns correct balance. And then, client-side "Null" changes into the correct balance. (The same as when player dead or exit and re-join)