Jump to content

Recommended Posts

Posted (edited)

Hello! I'm new here but not so new to modding! :P

 

I'm having a little headhake at this capability 'thing' that maybe u guys can help me. :)

 

Basicly my capability its working just fine (death, world restart, sleep and many other things).

Just 1 little problem: I have my GUI with the capability value in it, normal values (like "50 Money") and it stays like that at restarting the world and dying, but when i restart my minecraft and open the world, the value just go to the zeros (like "0 Money").

 

I hope you guys could give me a hand at this. :)

Thanks!

Edited by Deadzoke
Posted

You need to use packets to synchronize the value (known on the server) to the client (so your gui knows what it is)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (edited)

Thats the problem, i'm already doing that. At 1st i tought it was because i have 2 capabilities with the same id in the registerMessage method (smartass), but was the same.

In my preInit event:

    	Ignite.network = NetworkRegistry.INSTANCE.newSimpleChannel(Ignite.MODID);
    	Ignite.network.registerMessage(MessageSPHandler.class, MessageSP.class, 1, Side.CLIENT);

 

In my capability class:

	@Override
	public void synchronize() {
		Ignite.network.sendTo(new MessageSP(this.getSPtU(), this.getCurSPiT(), this.getMaxSPiT()), (EntityPlayerMP)this.player);
	}

 

Edited by Deadzoke
Posted

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.

Posted

So, I rewrited the code for the Proxies and now it is cleaner and without the CommonProxy class. :)
The events are separeted from the CapabilityHandler class and are now in the EventHandler class, turns out that there was a @EventBusSubscriber missing there. Now it works just fine with the events in the EventHandler class with the @EventBusSubscriber, and much more clean!
Many many thanks!

About the rest of the code, i will try to improve my codding skills in Java (this is not my main area).
I'm here to learn! :)

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.