Jump to content

NBT Help?


luisc99

Recommended Posts

Hey there!

 

I have made a new teleporter block which teleports players to a set of co-ordinates when they step on the block. As I have it set up now, the teleporting works fine however all the blocks in the world are synced together, and only one destination is applied. Also when the world is reloaded, via closing out of the game, the destination is also lost...

 

I understand that I will need to use NBT data for this, in order to keep the destination unique for that block, however I do not know how I would achieve this as I have very little knowledge over NBT and how it works. I was wondering if any of you nice people could help me, or give me some advice as to where to go next. I have enclosed below two files that I think are important in this issue, but I can always provide more if necessary!

 

As a side note, the ItemDecoTool.java seems to have some issues. All the text sent to the player appears twice, and when more than one mode is available only the last can be selected. I don't know how to solve this but I am sure it is just a derp on my behalf...

 

Finally I should point out that I am still in MC1.5.2, as I have not had time yet to upgrade to 1.6. I don't know if any of the methods I use in this will change (apart from textures), or if it will just be a straight port.

 

Thanks for reading, feel free to ask any questions!

Luis :D

 


 

My class files:

BlockTeleporter.java: http://paste.minecraftforge.net/view/a03995d7

ItemDecoTool.java: http://paste.minecraftforge.net/view/b97ca36d

Link to comment
Share on other sites

In order to use NBT, you'll need tile entities. However, let's start with the item. In order to stop the chat from showing twice, every time you send chat to the player, you should surround it with:

 

if(FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)

 

An important thing to remember for both Items and Blocks is that they are static objects - changing one will change all of them. If you change the mode for one player, it will also change it for other players, as the item with the ID 'x' will all be the same. Tile entities are how blocks become independent of being completely static - you can have multiple furnaces running out of sync with each other because they use tile entities. Items work the same way with ItemStacks. ItemStacks are all unique objects. For example, you could have two stacks of 32 wheat in your inventory, but they are not the same stack. Both tile entities and item stacks use NBT, so this is extremely helpful.

 

When changing mods, change the mode using NBT on the item stack. As such, you should pass the ItemStack parameter to activateTool and changeMode, and then using NBT:

 

public void changeMode(EntityPlayer player, ItemStack stack)
{
	NBTTagCompound nbt = stack.getTagCompound();
	if (nbt == null)
	{
		nbt = new NBTTagCompound();
		stack.setTagCompound(nbt);
	}
	int mode = nbt.getInteger("mode");
	if (mode < modes.length)
	{
		mode++;
	}
	else
	{
		mode = 0; // Use zero as your base instead of one
	}
	nbt.setInteger("mode", mode);
	if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
	{
		player.sendChatToPlayer(ChatColours.LightAqua + "Mode: " + modes[mode]);
	}
}

 

From that, you should be able to figure out how to change activateTool() and add the ItemStack parameter.

 

Remember, everything that should not be shared between items should be set to the ItemStack, and everything that should not be shared between blocks should be set to the TileEntity. When you set up said tile entity, you will need to super-override readFromNBT() and writeToNBT(), and you will need to add the fields:

 

private int teleportX;
private int teleportY;
private int teleportZ;

 

Those will need to be written to and read from the passed NBT in the two methods you need to super-override, like this:

 

public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setInteger("teleportX", this.teleportX);
nbt.setInteger("teleportY", this.teleportY);
nbt.setInteger("teleportZ", this.teleportZ);
}

 

A similar thing is done with read from, but instead of setting, you will get:

 

this.someVariable = nbt.getInteger("variableKey");

 

Hopefully this helps! If you need more, just ask. :)

Link to comment
Share on other sites

Thanks for your help! It works perfectly except the tool NBT tag never gets reset. When the mode needs to be reset back to 0, the NBT data does not and continues to be increased by 1, causing an OutOfBounds exception. Looking over the code, I cannot see anything that could cause this. Any help?

Link to comment
Share on other sites

Ok, the item works perfectly now however the blocks are still all synced. I have switched everything to the TE, and now the itemDecoTool.java passes the co-ords straight to the TE, and all the bloom does is read the co-ords when a player walks on the block. I can provide code, but it will have to be tomorrow when I have access to it again...

Link to comment
Share on other sites

Ok, the block syncing issue still exists, even though I have used a TE to store the data. Any suggestions? Sorry if I am becoming a but of an annoyance...

 

Improved Code:

ItemDecoTool.java: http://paste.minecraftforge.net/view/ec5af3ef

BlockTeleporter.java: http://paste.minecraftforge.net/view/4cca185c

TileTeleporter.java: http://paste.minecraftforge.net/view/14e85a69

Link to comment
Share on other sites

You are using static references for everything; fields, methods, etc. Most of the things you have as static should not be, such as teleporter_x, y, etc. That's why they are syncing. When you are setting the teleport_x of one, it's setting them all to that.

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.