Jump to content

[1.6.4][SOLVED] Custom NBT's


Dispellz

Recommended Posts

I have a custom gui bar that renders on the screen at the same time xp bar does. Its a little mana indicator. The problem is I have to save it somehow when the player exits. My question is, do I need TileEntity to save NBTs? If yes, can you please point me to an example? Maybe there is a better way to do it? Thanks for the help!

Link to comment
Share on other sites

I've been figuring out the IExtendedEntityProperties and have a problem with them. I found coolAlias tutorial on that stuff and pretty much followed it (http://www.minecraftforum.net/topic/1952901-172164-eventhandler-and-iextendedentityproperties/).

 

This is pretty much what my class looks like:

 

package org.theya.sustain.gui;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;

public class PollutionBarData implements IExtendedEntityProperties{

public static final String POLLUTION_DATA_NAME = "PollutionBarData";

private final EntityPlayer player;

private int trashAmount;

public PollutionBarData(EntityPlayer player) {
	this.player = player;
	this.trashAmount = 0;
}

public static final void register(EntityPlayer player) {
	player.registerExtendedProperties(PollutionBarData.POLLUTION_DATA_NAME, new PollutionBarData(player));
}

public static final PollutionBarData get(EntityPlayer player) {
	return (PollutionBarData)player.getExtendedProperties(POLLUTION_DATA_NAME);
}

@Override
public void saveNBTData(NBTTagCompound compound) {
	NBTTagCompound properties = new NBTTagCompound();

	System.out.println("[NBT-SAVE] Trash amount saved: " + this.trashAmount);
	properties.setInteger("CurrentTrash", this.trashAmount);
	System.out.println("[NBT-SAVE][PROPS] Trash amount: " + properties.getInteger("CurrentTrash"));

	compound.setTag(POLLUTION_DATA_NAME, properties);
}

@Override
public void loadNBTData(NBTTagCompound compound) {
	NBTTagCompound properties = (NBTTagCompound)compound.getTag(POLLUTION_DATA_NAME);

	this.trashAmount = properties.getInteger("CurrentTrash");

	System.out.println("[NBT-READ] Trash amount: " + this.trashAmount);
}

public final void sync() {
	ByteArrayOutputStream bos = new ByteArrayOutputStream(;
	DataOutputStream outputStream = new DataOutputStream(bos);

	try {
		outputStream.writeInt(this.trashAmount);
		System.out.println("[sYNC] Writing amount: " + this.trashAmount);
	} catch(IOException e) {
		e.printStackTrace();
	}

	Packet250CustomPayload packet = new Packet250CustomPayload("SustainPollution", bos.toByteArray());

	if(!player.worldObj.isRemote) {
		EntityPlayerMP player1 = (EntityPlayerMP) player;
		PacketDispatcher.sendPacketToPlayer(packet, (Player)player1);
		System.out.println("packet sent to player: " + this.trashAmount);
	}
}

@Override
public void init(Entity entity, World world) {
}

public void setTrash(int amount) {
	this.trashAmount = amount;
	this.sync();
}

public int getTrash() {
	return trashAmount;
}
}

 

And here is the output:

 

2014-03-29 12:54:59 [iNFO] [ForgeModLoader] Loading dimension 0 (New World) (net.minecraft.server.integrated.IntegratedServer@68689d26)
2014-03-29 12:54:59 [iNFO] [ForgeModLoader] Loading dimension 1 (New World) (net.minecraft.server.integrated.IntegratedServer@68689d26)
2014-03-29 12:54:59 [iNFO] [ForgeModLoader] Loading dimension -1 (New World) (net.minecraft.server.integrated.IntegratedServer@68689d26)
2014-03-29 12:54:59 [iNFO] [Minecraft-Server] Preparing start region for level 0
2014-03-29 12:55:00 [iNFO] [sTDOUT] [NBT-READ] Trash amount: 9
2014-03-29 12:55:00 [iNFO] [sTDOUT] loading single player
2014-03-29 12:55:00 [iNFO] [Minecraft-Server] Player106[/127.0.0.1:0] logged in with entity id 282 at (168.69339168635545, 64.0, -100.73887055738551)
2014-03-29 12:55:00 [iNFO] [Minecraft-Server] Player106 joined the game
2014-03-29 12:55:00 [iNFO] [sTDOUT] [sYNC] Writing amount: 9
2014-03-29 12:55:00 [iNFO] [sTDOUT] packet sent to player: 9
2014-03-29 12:55:00 [iNFO] [sTDOUT] Setting up custom skins
2014-03-29 12:55:00 [iNFO] [sTDOUT] [sYNC] Writing amount: 9
2014-03-29 12:55:00 [iNFO] [sTDOUT] [PACKET] Trash from packet: 9
2014-03-29 12:55:00 [iNFO] [Minecraft-Client] [CHAT] Welcome Player106 to this world
2014-03-29 12:55:02 [iNFO] [sTDOUT] [sYNC] Writing amount: 10
2014-03-29 12:55:02 [iNFO] [sTDOUT] XYZ: 158, 63, -135
2014-03-29 12:55:17 [iNFO] [Minecraft-Server] Saving and pausing game...
2014-03-29 12:55:17 [iNFO] [sTDOUT] [NBT-SAVE] Trash amount saved: 9
2014-03-29 12:55:17 [iNFO] [sTDOUT] [NBT-SAVE][PROPS] Trash amount: 9
2014-03-29 12:55:17 [iNFO] [sTDOUT] [NBT-SAVE] Trash amount saved: 9
2014-03-29 12:55:17 [iNFO] [sTDOUT] [NBT-SAVE][PROPS] Trash amount: 9
2014-03-29 12:55:17 [iNFO] [Minecraft-Server] Saving chunks for level 'New World'/Overworld
2014-03-29 12:55:17 [iNFO] [Minecraft-Server] Saving chunks for level 'New World'/Nether
2014-03-29 12:55:17 [iNFO] [Minecraft-Server] Saving chunks for level 'New World'/The End
2014-03-29 12:55:23 [iNFO] [Minecraft-Server] Stopping server
2014-03-29 12:55:23 [iNFO] [Minecraft-Server] Saving players
2014-03-29 12:55:23 [iNFO] [sTDOUT] [NBT-SAVE] Trash amount saved: 9
2014-03-29 12:55:23 [iNFO] [sTDOUT] [NBT-SAVE][PROPS] Trash amount: 9
2014-03-29 12:55:23 [iNFO] [sTDOUT] [NBT-SAVE] Trash amount saved: 9
2014-03-29 12:55:23 [iNFO] [sTDOUT] [NBT-SAVE][PROPS] Trash amount: 9
2014-03-29 12:55:23 [iNFO] [Minecraft-Server] Player106 left the game
2014-03-29 12:55:23 [iNFO] [sTDOUT] [NBT-SAVE] Trash amount saved: 9
2014-03-29 12:55:23 [iNFO] [sTDOUT] [NBT-SAVE][PROPS] Trash amount: 9
2014-03-29 12:55:23 [iNFO] [sTDOUT] [NBT-SAVE] Trash amount saved: 9
2014-03-29 12:55:23 [iNFO] [sTDOUT] [NBT-SAVE][PROPS] Trash amount: 9
2014-03-29 12:55:23 [iNFO] [Minecraft-Server] Saving worlds
2014-03-29 12:55:23 [iNFO] [Minecraft-Server] Saving chunks for level 'New World'/Overworld
2014-03-29 12:55:23 [iNFO] [Minecraft-Server] Saving chunks for level 'New World'/Nether
2014-03-29 12:55:23 [iNFO] [Minecraft-Server] Saving chunks for level 'New World'/The End
2014-03-29 12:55:23 [iNFO] [ForgeModLoader] Unloading dimension 0
2014-03-29 12:55:23 [iNFO] [ForgeModLoader] Unloading dimension -1
2014-03-29 12:55:23 [iNFO] [ForgeModLoader] Unloading dimension 1
2014-03-29 12:55:23 [iNFO] [Minecraft-Client] Stopping!
2014-03-29 12:55:23 [iNFO] [sTDOUT] 
2014-03-29 12:55:23 [iNFO] [sTDOUT] SoundSystem shutting down...
2014-03-29 12:55:24 [iNFO] [sTDOUT]     Author: Paul Lamb, www.paulscode.com
2014-03-29 12:55:24 [iNFO] [sTDOUT] 
Java HotSpot(TM) 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release

 

The problem that I have is NBT load happens correctly and the trash amount is 9. Then it changes to 10 and the sync function tells that its 10. However, when I press ESC and the NBTs are saving, it tells me it saved it as 9. I've been trying to figure out what am I doing wrong here...

Link to comment
Share on other sites

The values are changed by the method setTrash and that is also when i sync it. The only other place with sync is in my event handler.

 

package org.theya.sustain.gui;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;

public class PollutionHandler {

@ForgeSubscribe
public void onEntityConstructing(EntityConstructing event) {
	if(event.entity instanceof EntityPlayer && PollutionBarData.get((EntityPlayer)event.entity) == null) {
		PollutionBarData.register((EntityPlayer)event.entity);
	}
	if(event.entity instanceof EntityPlayer && event.entity.getExtendedProperties(PollutionBarData.POLLUTION_DATA_NAME) == null) {
		event.entity.registerExtendedProperties(PollutionBarData.POLLUTION_DATA_NAME, new PollutionBarData((EntityPlayer)event.entity));
	}
}

@ForgeSubscribe
public void onEntityJoinWorld(EntityJoinWorldEvent event) {
	if(!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer) {
		PollutionBarData.get((EntityPlayer)event.entity).sync();
	}
}
}

 

I call set Trash in my CustomGui class and I do it like this:

 

PollutionBarData props = PollutionBarData.get((EntityPlayer)this.mc.getNetHandler().getPlayer());
props.setTrash(props.getTrash() + 1);

Link to comment
Share on other sites

The weird thing is EVERYTHING works correctly, except the fact that anytime i want to save to NBTs, the value of any variable is zero. The loading from NBTs works perfectly. If I assign the value of trashAmount to be lets say 10 INSIDE the NBT Save, then the value will save correctly. However when I dont assign it and just try to pull it, the int is always zero. No idea why this is happening and supposedly it works in the tutorial.

 

Anyhow, Yes, I need this to persist through players death. This will be a singleplayer mod ONLY if that makes any difference. TLHPoE, if you could link me to a tutorial or give me a general idea how players NBT works, I'd be grateful. Thanks guys!

Link to comment
Share on other sites

  • 3 months later...

Hi! so i'm making minecraft mod well i'm copying from youtube beacuse i'm learning on eclipse. So i've wrote everything same as the guy on youtube. When he statrs game everything goes well. But when i start minecraft first open and than close and it says:'Java HotSpot 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release' i'm not sure how to resolve this problem but if you can help me i'm glad! thanks :)

Link to comment
Share on other sites

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.