Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hello everyone. I create my capability for stats and should save it after player death, but if i try to get my values i took 0!

For example 

 

@SubscribeEvent
public void playerStatsEvent(TickEvent.PlayerTickEvent event) {
  
  // HERE I WILL GET WRONG STATS
  System.out.println(player.getCapability(StatCapability.CAPABILITY_STAT, null));
	
  if(!player.world.isRemote()){
    // HERE I WILL GET GOOD STATS
	System.out.println(player.getCapability(StatCapability.CAPABILITY_STAT, null));
  }
}

Cappa and Death event iclude

Interface

public interface IStat extends Callable{

    int getStr();
    void setStr(int stat);
    void setStatsByAnotherStats(IStat stats);
    void increaseStr(int statAdd);
    void clearAll();
}

Class stats

public class Stat implements IStat{

    private int str = 0;
    
    @Override
    public int getStr() {
        return str;
    }

    @Override
    public void setStr(int stat) {
        this.str = stat;
    }

    @Override
    public void setStatsByAnotherStats(IStat stats) {
        Stat stat = (Stat) stats;
        this.str = stat.str;
    }

    @Override
    public void increaseStr(int statAdd) {
        this.str += statAdd;
    }

    @Override
    public void clearAll() {
        this.str = 0;
    }

    @Override
    public String toString(){
        return "STR ->" + str;
    }

    @Override
    public Object call() throws Exception {
        return this;
    }
}

Capa 

public class StatCapability implements ICapabilitySerializable<NBTTagCompound> {

    @CapabilityInject(IStat.class)
    public static Capability<IStat> CAPABILITY_STAT;
    private IStat instance = CAPABILITY_STAT.getDefaultInstance();

    @Override
    public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
        return capability == CAPABILITY_STAT;
    }

    @Nullable
    @Override
    @SuppressWarnings("unchecked")
    public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
        if (capability == CAPABILITY_STAT) return  (T) this.instance;
        return null;
    }

    @Override
    public NBTTagCompound serializeNBT() {
        return (NBTTagCompound) CAPABILITY_STAT.getStorage().writeNBT(CAPABILITY_STAT, this.instance, null);
    }

    @Override
    public void deserializeNBT(NBTTagCompound nbt) {
        CAPABILITY_STAT.getStorage().readNBT(CAPABILITY_STAT, this.instance, null, nbt);
    }

Storage 

public class StatStorage implements Capability.IStorage<IStat> {

    @Nullable
    @Override
    public NBTBase writeNBT(Capability<IStat> capability, IStat instance, EnumFacing side) {
        NBTTagCompound nbt = new NBTTagCompound();
        nbt.setInteger("strength", instance.getStr());
        return nbt;
    }

    @Override
    public void readNBT(Capability<IStat> capability, IStat instance, EnumFacing side, NBTBase nbt) {
        NBTTagCompound tag = (NBTTagCompound) nbt;
        instance.setStr(tag.getInteger("strength"));
    }
}

PlayerDeath event

 @SubscribeEvent
    public void playerClone(PlayerEvent.Clone e) {
        IStat newStat = e.getEntityPlayer().getCapability(StatCapability.CAPABILITY_STAT,null);
        IStat oldStat = e.getOriginal().getCapability(StatCapability.CAPABILITY_STAT,null);
        newStat.setStatsByAnotherStats(oldStat);
    }

Attach

private static final ResourceLocation STATS = new ResourceLocation(Reference.MODID, "stats");

    @SubscribeEvent
    public void attachCapability(AttachCapabilitiesEvent<Entity> event) {
        if (event.getObject() instanceof EntityPlayer)
            event.addCapability(STATS, new StatCapability());
    }


 

35 minutes ago, kifor4ik said:

Hello everyone. I create my capability for stats and should save it after player death, but if i try to get my values i took 0!

the reason for that is your never sync the data of you capability to the client
if you want to sync the data you need a custom Packet and a SimpleChannel

  • Author
2 hours ago, Luis_ST said:

sync the data you need a custom

Maybe you know any cool guide to syns capa?)

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.