So, answering to your questions:
I call the syncronize() method in my CapabilityHandler class (that is called in preInit method, before anything else) in the onPlayerLoggedIn event:
@SubscribeEvent
public static void onPlayerLoggedIn(PlayerLoggedInEvent event) {
// Mana
if(event.player.hasCapability(CAP_MANA, null)) {
ICapMana capMana1 = event.player.getCapability(CAP_MANA, null);
capMana1.synchronize();
}
// Skill Points
if(event.player.hasCapability(CAP_SP, null)) {
ICapSP capSP1 = event.player.getCapability(CAP_SP, null);
capSP1.synchronize();
}
}
This are my message classes (Message and MessageHandler):
public class MessageSP implements IMessage {
private int SPtU;
private int curSPiT;
private int maxSPiT;
public MessageSP() {
}
public MessageSP(int SPtU, int curSPiT, int maxSPiT) {
this.SPtU = SPtU;
this.curSPiT = curSPiT;
this.maxSPiT = maxSPiT;
}
@Override
public void fromBytes(ByteBuf buf) {
this.SPtU = buf.readInt();
this.curSPiT = buf.readInt();
this.maxSPiT = buf.readInt();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(this.SPtU);
buf.writeInt(this.curSPiT);
buf.writeInt(this.maxSPiT);
}
public int getSPtU() {
return this.SPtU;
}
public int getcurSPiT() {
return this.curSPiT;
}
public int getmaxSPiT() {
return this.maxSPiT;
}
}
public class MessageSPHandler implements IMessageHandler<MessageSP, IMessage> {
@Override
public IMessage onMessage(MessageSP message, MessageContext ctx) {
Runnable action = new Runnable() {
@Override
public void run() {
SkillTreeGui.SPtU = message.getSPtU();
SkillTreeGui.curSPiT = message.getcurSPiT();
SkillTreeGui.maxSPiT = message.getmaxSPiT();
}
};
Ignite.proxy.getThreadListener(ctx).addScheduledTask(action);
return null;
}
}
This is my capability:
public class CapSP implements ICapSP {
public static final ResourceLocation ID = new ResourceLocation(Ignite.MODID, "CapSP");
private EntityPlayer player;
// Skill Points to Use
private int SPtU = 0;
// Skill Points in Tree
private int curSPiT = 0;
private int maxSPiT = 20;
public CapSP(EntityPlayer player) {
this.player = player;
}
@Override
public void addSPtU(int amount) {
this.setSPtU(this.getSPtU() + amount);
}
@Override
public void remSPtU(int amount) {
this.setSPtU(this.getSPtU() - amount);
}
@Override
public void setSPtU(int amount) {
if(amount <= 0) {
this.SPtU = 0;
} else {
this.SPtU = amount;
}
this.synchronize();
}
@Override
public int getSPtU() {
return this.SPtU;
}
//----------//
@Override
public void addSPiT(int amount) {
this.setCurSPiT(getCurSPiT() + amount);
}
@Override
public void remSPiT(int amount) {
this.setCurSPiT(getCurSPiT() - amount);
}
@Override
public void setCurSPiT(int amount) {
}
@Override
public int getCurSPiT() {
return this.curSPiT;
}
@Override
public void setMaxSPiT(int amount) {
}
@Override
public int getMaxSPiT() {
return this.maxSPiT;
}
@Override
public NBTBase writeNBT(EnumFacing facing) {
NBTTagCompound nbt = new NBTTagCompound();
nbt.setInteger("SPtU", this.getSPtU());
nbt.setInteger("CurrentSPiT", this.getCurSPiT());
nbt.setInteger("MaximumSPiT", this.getMaxSPiT());
return nbt;
}
@Override
public void readNBT(NBTBase nbt) {
this.SPtU = (((NBTTagCompound)nbt).getInteger("SPtU"));
this.curSPiT = (((NBTTagCompound)nbt).getInteger("CurrentSPiT"));
this.maxSPiT = (((NBTTagCompound)nbt).getInteger("MaximumSPiT"));
}
@Override
public ResourceLocation getID() {
return this.ID;
}
@Override
public void synchronize() {
Ignite.network.sendTo(new MessageSP(this.getSPtU(), this.getCurSPiT(), this.getMaxSPiT()), (EntityPlayerMP)this.player);
}
}
I call it in my GUI by linking the int SPt, the int curSPiT and the int maxSPiT int the MessageHandler.