Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Null" value when first join server #178

Open
Sam-Chai opened this issue Jun 19, 2024 · 0 comments
Open

"Null" value when first join server #178

Sam-Chai opened this issue Jun 19, 2024 · 0 comments

Comments

@Sam-Chai
Copy link

#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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant