Jump to content

[1.7.10] NBT saving issue - value reads and writes but is thrown away on log off


Recommended Posts

Posted

Ok so I have a custom entity that is tameable using my own custom system. When the entity is tamed it gets the name of the player that tamed it and this name is displayed above the entity via a text box (like minecraft does). This works perfectly while the player is logged in. The problem starts when I log off the name is thrown away into oblivion, And I know that it is my entity that remains in the world since its UUID's match before log on and after log off.

 

This is the order of events:

 

Player tames entity -> entity is written to NBT -> If I call readEntityFromNBT at this point I get "ON READ THE OWNER NAME IS  Player567" outprinted to consol -> The Owner name shows up above my entity and all is well. Then I hit the Esc key -> writeEntityToNBT is called -> I get "ON WRITE THE OWNER NAME IS  Player567" outprinted to the consol -> and I click Log off. So everything should be good right? But it isnt when I log back on and I see my entity the Owner name does not show up above my entity, the readEntityFromNBT method outprints "ON READ THE OWNER NAME IS        " with no name, and when I hit esc the writeEntityToNBT is called -> I get "ON WRITE THE OWNER NAME IS  " outprinted to the consol with no name? Why is it being thrown out into oblivion when I log off???? and while I am logged on and I tamed it I can call the read method whenever I feel like it and I always get the owner name printing to consol?

 

Please note my entity class is highly customized and HUGE but this is the relevant code for the owner name and the NBT stuff (I had to remove a lot of code to make this more easily readable) Any help is appreciated

 

public class EntityCustom extends EntityAnimal implements IEntityAdditionalSpawnData{

//IDs for DataWatcher
private static final int ownerDataID = 25;

public EntityCustom(World world) {
	super(world);
	this.setSize(0.9F, 0.9F); // hitbox size?
	}
}

protected void entityInit() {
	super.entityInit();
	this.dataWatcher.addObject(ownerDataID, "");

}

@Override
public String getCustomNameTag(){
	if(this.getOwnerName().length() > 0){
		return this.getOwnerName() + "'s " + this.getName();
	}
	return "";
}

//Add a textbox on top of entity if it has an owner
public boolean hasCustomNameTag(){
	return this.getOwnerName().length() > 0;
	//return true;
}

@Override
public boolean getAlwaysRenderNameTag(){
	return this.dataWatcher.getWatchableObjectByte(11) == 1;
}

public void setNameTagVisible(boolean visible){
	this.dataWatcher.updateObject(11, (byte)(visible ? 1 : 0));
}

public String getName(){
	return this.getEntityString();
}

public String getOwnerName(){
	return this.dataWatcher.getWatchableObjectString(this.ownerDataID);
}

public EntityLivingBase getOwner(){
	return this.worldObj.getPlayerEntityByName(this.getOwnerName());
}

public void setOwner(String ownerName){
	this.dataWatcher.updateObject(this.ownerDataID, ownerName);
}


@Override
public void readEntityFromNBT(NBTTagCompound nbtCompound)
{   
	super.readEntityFromNBT(nbtCompound);

	nbtCompound.getString("EntName");

	stats.ownerName = nbtCompound.getString("ownerName");
	System.out.println("ON READ THE OWNER NAME IS   " +stats.ownerName);

}

@Override
public void writeEntityToNBT(NBTTagCompound nbtCompound)
{ 
	super.writeEntityToNBT(nbtCompound);
	nbtCompound.setString("EntName", EntityList.getEntityString(this));
	nbtCompound.setString("ownerName", this.getOwnerName());
	System.out.println("ON WRITE THE OWNER NAME IS   " + this.getOwnerName());
}

@Override
public EntityCustom createChild(EntityAgeable entityageable) {
	return new EntityCustom(this.worldObj);
}
}

Posted

Also this seems to only happen in singleplayer, when I am in a lan environment and I have 2 clients, when 1 client tames then logs off then logs back on that client players name persists. But if all players log off then restart the lan world the names disappear.

Posted

I must say I haven't really read whole post, but I've looked at code.

 

You are doing it wrong way.

It' s better off to store direct link to entity objects and only when writing them (to NBT) to actually encode them into UUIDs.

 

Goal: Make entity have owner, like the dog can have one.

The design is quite simple:

Inside your CustomEntity create field EntityPlayer owner.

When player tames entity (which should happen server side), change owner to player.

In some #setOwner(EntityPlayer owner) method make it send packet from server to client with entityId of entity whos owner has to be changed and entityId of player who will claim ownership.

I alredy told you how to dynamically update stuff - you can do that here too. (I guess you can also use DataWatchers to update entity's owner - still using entityId of player)

 

Now saving - NBT are only server side, same goes for UUID. UUIDs will only work and retrieve correct data on server side, and only when server is in online mode.

When entity is unloaded you can get owner filed and player.getUUID(), save it to NBT as string or 2x longs (UUID.getMost/LeastSignificantBits()).

 

Similar stuff happens in ThrownEntities, probably dogs and stuff like that.

EDIT

Oh and why is it better?

- Never use direct player names, use UUIDs (always).

- Sending one entityId is better than sending string (in both cases - nick or UUID)

- I never liked DataWatchers and strongly suggest using them ONLY when value is updated a LOT, not stuff like owner.

1.7.10 is no longer supported by forge, you are on your own.

Posted

oh ya oops that is my custom stats class I forgot to include that

 

public Stats stats = new Stats();

 

and the class is

 

public class PokemonStats {

        public int modelID;
public String ownerName = "";

//current stats
public int level = 0;
public int hp, currentHp;
public int att, currentAtt;
public int def, currentDef;
public int spAtt, currentSpAtt;
public int spDef, currentSpDef;
public int spd, currentSpd;

//determinant values. These should be initialized when the entity is created, and should
//never change.
public int DV_HP;
public int DV_Att;
public int DV_Def;
public int DV_SpAtt;
public int DV_SpDef;
public int DV_Spd;

//effort values - gained through battling
public int EV_HP;
public int EV_Att;
public int EV_Def;
public int EV_SpAtt;
public int EV_SpDef;
public int EV_Spd;

//total experience gained from battling
public int exp;

//important: make sure the stats are written/read in the same order. If they are not, they will be mixed up!
public void fromBytes(ByteBuf buf){
	try{
	modelID = buf.readInt();
	level = buf.readInt();

	hp = buf.readInt();
	currentHp = buf.readInt();
	att = buf.readInt(); 
	currentAtt = buf.readInt();
	def = buf.readInt(); 
	currentDef = buf.readInt();
	spAtt = buf.readInt(); 
	currentSpAtt = buf.readInt();
	spDef = buf.readInt(); 
	currentSpDef = buf.readInt();
	spd = buf.readInt(); 
	currentSpd = buf.readInt();

	DV_HP = buf.readInt();
	DV_Att = buf.readInt();
	DV_Def = buf.readInt();
	DV_SpAtt = buf.readInt();
	DV_SpDef = buf.readInt();
	DV_Spd = buf.readInt();

	EV_HP = buf.readInt();
	EV_Att = buf.readInt();
	EV_Def = buf.readInt();
	EV_SpAtt = buf.readInt();
	EV_SpDef = buf.readInt();
	EV_Spd = buf.readInt();

	exp = buf.readInt();
	//read string owner name. Need to store and read the length of string.
	int ownerNameLength = buf.readInt();
	StringBuilder stringBuilder = new StringBuilder();
	for(int i=0;i<ownerNameLength;i++){
		stringBuilder.append(buf.readChar());
	}
	ownerName = stringBuilder.toString();
	}
	catch(Exception e){
		System.out.println(e.toString());
	}
}

//important: make sure the stats are written/read in the same order. If they are not, they will be mixed up!
public void toBytes(ByteBuf buf){
	buf.writeInt(modelxID);
	buf.writeInt(level);	

	buf.writeInt(hp);
	buf.writeInt(currentHp);
	buf.writeInt(att);
	buf.writeInt(currentAtt);
	buf.writeInt(def);
	buf.writeInt(currentDef);
	buf.writeInt(spAtt);
	buf.writeInt(currentSpAtt);
	buf.writeInt(spDef);
	buf.writeInt(currentSpDef);
	buf.writeInt(spd);
	buf.writeInt(currentSpd);

	buf.writeInt(DV_HP);
	buf.writeInt(DV_Att);
	buf.writeInt(DV_Def);
	buf.writeInt(DV_SpAtt);
	buf.writeInt(DV_SpDef);
	buf.writeInt(DV_Spd);

	buf.writeInt(EV_HP);
	buf.writeInt(EV_Att);
	buf.writeInt(EV_Def);
	buf.writeInt(EV_SpAtt);
	buf.writeInt(EV_SpDef);
	buf.writeInt(EV_Spd);

	buf.writeInt(exp);

	buf.writeInt(ownerName.length());
		for(int i=0;i<ownerName.length();i++){
			buf.writeChar(ownerName.charAt(i));
		}
	}
}

Posted

I guess the goal is to store the Owner name inside the stats of the custom entity since they can be accessed easily and are persistent

For player, nickname is not constant, it can be different when his nickname changes. Only UUID is persistent.

So save both of them: nickname and UUID.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted

An entity's NBT data is persistent - that's the whole point of saving to and loading from NBT. You should only need to store the UUID of the owner in NBT, nowhere else, just like in EntityTameable. If you want the player's nickname, you can always get that later as you need it based on the current owner.

Of course it should be in normal case, but in this case the owner's name is also needed when the player is not in the game. So the player's nickname should be saved alongside.

(+ The nickname should be updated when the player logs in, because it could be changed)

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted

The situation seems simple enough: if you need the nickname while not logged in, then save/load it via NBT like you do the UUID and store it in another DataWatcher slot; whenever setOwner or the like is called, simply have it update the nickname field as well.

 

Unless you are doing something funky behind the scenes, that's really all it takes: two data watcher fields, one each for the UUID and nickname, and saving / loading each of those to NBT. Just like EntityWolf/Tameable.

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.