Jump to content

Recommended Posts

Posted

Hi all,

I'm trying to add some properties to the player, and it's not correctly working.  Through some system.out.println()'s, I know it is saving, loading, and initializing my properties, it's just not applying them to the player.  I remember reading something old about properties, and packets, but I can't find it anymore and I'm not sure. 

My properties code:

 

package ninjapancakes87.civilwar;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;

public class ExtendedProperties implements IExtendedEntityProperties{

/**The number of soldiers hired by the player*/
private int armySize;

/**Whether the player has hired a Union or Confederate soldier*/
private boolean isUnion;

public int getArmySize(){
	return this.armySize;
}

public void setArmySize(int par1){
	this.armySize = par1;
}

public boolean getIsUnion(){
	return this.isUnion;
}

public void setIsUnion(boolean par1){
	this.isUnion = par1;
}

@Override
public void saveNBTData(NBTTagCompound compound) {
	compound.setBoolean("isUnion", isUnion);
	compound.setInteger("armySize", armySize);
}

@Override
public void loadNBTData(NBTTagCompound compound) {
	this.isUnion = compound.getBoolean("isUnion");
	this.armySize = compound.getInteger("armySize");
}

@Override
public void init(Entity entity, World world) {
	if(entity instanceof EntityPlayer){
		EntityPlayer player = (EntityPlayer)entity;
	}
}
}

 

I previously had it work by calling EntityPlayer.getEntityData(), but that doesn't vary between worlds like I want it to.

Posted

If everything is saving and loading properly, then you're fine. The problem is that the client-side data is not automatically synchronized, so you have to do it yourself.

 

Either:

a) send a packet to the client with the data and set it manually; this is best done from somewhere like the EntityJoinWorldEvent to avoid NPE of the client-side properties

 

b) use DataWatcher to store your data instead of class fields; advantage is that everything else is automatic; disadvantage is you are using an extremely limited resource (data watcher slots)

 

There are tutorials on the Forge wiki about both packets and data watcher, both of which are referenced on this list:

http://mazetar.com/mctuts/displayTutorials.php

 

Alternatively, you can check out my tutorial on IExtendedEntityProperties here:

http://www.minecraftforum.net/topic/1952901-eventhandler-and-iextendedentityproperties/#entry24051513

Posted

If everything is saving and loading properly, then you're fine. The problem is that the client-side data is not automatically synchronized, so you have to do it yourself.

 

Either:

a) send a packet to the client with the data and set it manually; this is best done from somewhere like the EntityJoinWorldEvent to avoid NPE of the client-side properties

 

b) use DataWatcher to store your data instead of class fields; advantage is that everything else is automatic; disadvantage is you are using an extremely limited resource (data watcher slots)

 

There are tutorials on the Forge wiki about both packets and data watcher, both of which are referenced on this list:

http://mazetar.com/mctuts/displayTutorials.php

 

Alternatively, you can check out my tutorial on IExtendedEntityProperties here:

http://www.minecraftforum.net/topic/1952901-eventhandler-and-iextendedentityproperties/#entry24051513

I do know how to use datawatcher (never used packets, so I'm going to avoid that route), but thanks for the tip.  I looked at your tutorial, and the only question I have is should I add the watcher in the init method of my ExtendedProperties class?

Posted

You can add datawatcher in either the init or in the constructor, doesn't really matter since init is called during class construction anyway.

Thanks!

 

But on a side note, in your tutorial it does say you haven't found a use for your Init method. You have figured out what that does, correct?

Posted

But on a side note, in your tutorial it does say you haven't found a use for your Init method. You have figured out what that does, correct?

It's the same as entityInit in Entity classes; it gets called during entity construction, so you could put the exact same code in your constructor and it would have the same effect. The benefit of the init method in IExtendedEntityProperties is that it provides a world object, but I haven't found it (the extra world parameter) particularly useful because the entity itself isn't yet finished constructing.

Posted

But on a side note, in your tutorial it does say you haven't found a use for your Init method. You have figured out what that does, correct?

It's the same as entityInit in Entity classes; it gets called during entity construction, so you could put the exact same code in your constructor and it would have the same effect. The benefit of the init method in IExtendedEntityProperties is that it provides a world object, but I haven't found it (the extra world parameter) particularly useful because the entity itself isn't yet finished constructing.

I know, just wanted to make sure :P

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.

×
×
  • Create New...

Important Information

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