Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Help with nbt


yesod30
 Share

Recommended Posts

i'm trying to have an item that can "store" an area, selected by the player, and than place the block in another place choosed by the player. I already have the code to store block, but i need an help for the placing part. I wanted to use a tri-dimensional array, but it seems that nbt can store only normal array. So, there is actually a way to store a tri-dimensinal array, or i need to store more information(like the dimension of the selected cuboid) in the nbt?

 

Item class: http://pastebin.com/PdCywmtN

Link to comment
Share on other sites

NBT can store NBTList and arrays

An NBT containing a list of lists of arrays may as well be a 3D array.

 

Everything can be serialized, you just have to figure out how.

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.

Link to comment
Share on other sites

So i actually try, and i understand how to simulate a 3D array. I just have a problem: The array inserted in the list it's always the same. If i print the single array it actually works, but when it's put in the list It's just the same array repeated. This is the code of the method. Any clue?

 

private void storeBlock(ItemStack itemStack, World world)
{	if (itemStack.stackTagCompound.hasKey("FirstBlock") && itemStack.stackTagCompound.hasKey("LastBlock"))
{
	//acquisizione coordinate
	NBTTagList firstPositionTagList = (NBTTagList)itemStack.stackTagCompound.getTag("FirstBlock");
	NBTTagList lastPositionTagList = (NBTTagList)itemStack.stackTagCompound.getTag("LastBlock");
	NBTTagCompound firstPositionTag = (NBTTagCompound) firstPositionTagList.getCompoundTagAt(0);
	NBTTagCompound lastPositionTag = (NBTTagCompound) lastPositionTagList.getCompoundTagAt(0);

	int firstX = firstPositionTag.getInteger("xFirstBlock");
	int firstY = firstPositionTag.getInteger("yFirstBlock");
	int firstZ = firstPositionTag.getInteger("zFirstBlock");

	int lastX = lastPositionTag.getInteger("xLastBlock");
	int lastY = lastPositionTag.getInteger("yLastBlock");
	int lastZ = lastPositionTag.getInteger("zLastBlock");

	//controllo di quale è maggiore e quale minore
	int diffX = lastX - firstX;
	int diffY = lastY - firstY;
	int diffZ = lastZ - firstZ;

	if (firstX > lastX) 
	{ 
		diffX = firstX - lastX;
		int tmp = firstX;
		firstX = lastX;
		lastX = tmp;
	}

	if (firstY > lastY) 
	{ 
		diffY = firstY - lastY;	
		int tmp = firstY;
		firstY = lastY;
		lastY = tmp;
	}

	if (firstZ > lastZ) 
	{ 
		diffZ = firstZ - lastZ;
		int tmp = firstZ;
		firstZ = lastZ;
		lastZ = tmp;
	}

	//acquisizione blocchi

	int [] xArrayBlock = new int[(diffX + 1)];

	NBTTagList zLayerList = new NBTTagList();
	NBTTagCompound zLayer = new NBTTagCompound();

	NBTTagList xLayerList = new NBTTagList();
	NBTTagCompound xLayer = new NBTTagCompound();

// Just for reference		
//		NBTTagList positionTagList = (NBTTagList)itemStack.stackTagCompound.getTag(block);
//		NBTTagCompound positionTag = new NBTTagCompound();
//
//		positionTag.setInteger("x" + block, xBlockPos);
//		positionTag.setInteger("y" + block, yBlockPos);
//		positionTag.setInteger("z" + block, zBlockPos);
//		positionTagList.appendTag(positionTag);

	for (int i = firstY; i <= lastY; i++) 
	{
		for (int j = firstZ; j <= lastZ ; j++) 
		{
//				for (int l = 0 ; l < xArrayBlock.length ; l++)  
//				{   
				for (int k = firstX , l = 0 ; k <= lastX; k++, l++)
				{							
					xArrayBlock[l] = Block.getIdFromBlock(world.getBlock(k, i, j));
					//world.setBlockToAir(k, i, j);
				}

				xLayer.setIntArray("xLayer" + j, xArrayBlock);
				xLayerList.appendTag(xLayer);
//				}
				//zLayer.setTag("zLayer", xLayerList);
				//zLayerList.appendTag(zLayer);
		}
	}
	if (!world.isRemote)
		System.out.println(xLayerList);

	itemStack.stackTagCompound.setIntArray("blockArray", xArrayBlock );
	itemStack.stackTagCompound.setBoolean("blockStored", true);
}
}

Link to comment
Share on other sites

I can't read your code well enough to figure out what's wrong.

 

But, just as a dead-simple method that flattens things (but is not necessarily) the best:

 

for(x) {
  for(y) {
    for(z) {
      nbt.setInt(x+","+y+","+z, array[x][y][z]);
    }
  }
}

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.

Link to comment
Share on other sites

I understand what you are suggesting, and i think i'll go for that way. Just for curiosity, i'll try ti explain my code so maybe you can help me figure out what's wrong. It takes the starting coordinate block and the last coordinate block( two angles of a cuboid), and it scans all the block i sode the area with the for loop, it take the di of the block in the coordinate given by the loop, and store it inside the array and set the block to air. The "error" is present only when i use the list to store the array

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
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.

 Share



×
×
  • Create New...

Important Information

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