Jump to content

[1.8] Problem Saving Tile Entities


Kalman98

Recommended Posts

Hello Minecraft Forge Forums people, thanks for checking my post. I don't ask for help online unless I absolutely can not figure something out, and right now I absolutely can not figure something out. So here's my problem: I'm modding in 1.8 and have created a block that can remove blocks from the world and same them in an ArrayList. I can also put the blocks back where they came from later. What I can't figure out is how to put tile entities back out again. I can copy the blockstates and tile entities into their own ArrayLists, but I can only place the blocks back out again. Here is some of my code:

 

CustomTileEntity.class:

 

package com.kalman98.custommod.tileentities;

import java.util.ArrayList;

import com.kalman98.custommod.blocks.CustomModBlockList;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.server.gui.IUpdatePlayerListBox;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;


public class CustomTileEntity extends TileEntity implements IUpdatePlayerListBox {


public boolean opening = false;
public boolean closing = false;
public boolean open = true;
public int x, y, z, index;

public BlockPos maxBlockPos;
public BlockPos minBlockPos;

public ArrayList<IBlockState> blocks = new ArrayList<IBlockState>();
public ArrayList<TileEntity> tileentities = new ArrayList<TileEntity>();

public World worldIn;

public CustomTileEntity() {

}

@Override
public void update() {

	this.worldIn = this.worldObj;
	if (!worldIn.isRemote) {


		if (this.open && !this.closing) {
			if (!this.blocks.isEmpty()) this.blocks.clear(); 
			if (!this.tileentities.isEmpty()) this.tileentities.clear();
		}
		if (this.closing) {
		//	System.out.println("<Custom Block> I'm closing!");

			if (this.z < this.maxBlockPos.getZ()) z ++;
			else if (this.z == this.maxBlockPos.getZ() && this.y < this.maxBlockPos.getY()) {
				this.z = this.minBlockPos.getZ();
				this.y ++;
			} else if (this.z == this.maxBlockPos.getZ() && this.y == this.maxBlockPos.getY() && this.x < this.maxBlockPos.getX()) {
				this.z = this.minBlockPos.getZ();
				this.y = this.minBlockPos.getY();
				this.x ++;
			} 
			System.out.println(this.x + ", " + this.y + ", " + this.z);
			if (worldIn.getBlockState(new BlockPos(x, y, z)).getBlock() != CustomModBlockList.blockCustomBlock) System.out.println(worldIn.getBlockState(new BlockPos(x, y, z)).getBlock());
			System.out.println(Block.getStateId(worldIn.getBlockState(new BlockPos(x, y, z))));
			this.blocks.add(worldIn.getBlockState(new BlockPos(x, y, z)));
			if (worldIn.getTileEntity(new BlockPos(x, y, z)) != null) {
				this.tileentities.add(worldIn.getTileEntity(new BlockPos(x, y, z)));
				System.out.println("Saving Tile Entity! " + tileentities.size());
			}
			else this.tileentities.add(null);
			if (worldIn.getBlockState(new BlockPos(x, y, z)).getBlock() != CustomModBlockList.blockCustomBlock) worldIn.destroyBlock(new BlockPos(x, y, z), false);

			if (this.z == this.maxBlockPos.getZ() && this.y == this.maxBlockPos.getY() && this.x == this.maxBlockPos.getX()) {
				System.out.println("Ding");
				this.open = false;
				this.closing = false;
				this.x = minBlockPos.getX();
				this.y = minBlockPos.getY();
				this.z = minBlockPos.getZ();
			}

		} else if (this.opening) {

			if (this.z < this.maxBlockPos.getZ()) z ++;
			else if (this.z == this.maxBlockPos.getZ() && this.y < this.maxBlockPos.getY()) {
				this.z = this.minBlockPos.getZ();
				this.y ++;
			} else if (this.z == this.maxBlockPos.getZ() && this.y == this.maxBlockPos.getY() && this.x < this.maxBlockPos.getX()) {
				this.z = this.minBlockPos.getZ();
				this.y = this.minBlockPos.getY();
				this.x ++;
			} 

			System.out.println("<Custom Block> I'm opening!");

			if (worldIn.getBlockState(new BlockPos(x, y, z)).getBlock() != CustomModBlockList.blockCustomBlock && this.blocks.get(index).getBlock() != Blocks.air) worldIn.setBlockState(new BlockPos(x, y, z), this.blocks.get(this.index));
			if (worldIn.getBlockState(new BlockPos(x, y, z)).getBlock() != CustomModBlockList.blockCustomBlock && this.tileentities.get(index) != null) {
				NBTTagCompound nbttagcompound = new NBTTagCompound();
				this.tileentities.get(index).writeToNBT(nbttagcompound);
				worldIn.getTileEntity(new BlockPos(x, y, z)).readFromNBT(nbttagcompound);

				//worldIn.setTileEntity(new BlockPos(x, y, z), this.tileentities.get(index));
				System.out.println("Placing a tile entity at " + x + ", " + y + ", " + z + "! It's " + this.tileentities.get(index));
				//System.out.println(this.tileentities.get(index).getTileData());
			}

			this.index ++;

			if (this.z == this.maxBlockPos.getZ() && this.y == this.maxBlockPos.getY() && this.x == this.maxBlockPos.getX()) {
				System.out.println("Ding");
				this.open = true;
				this.opening = false;
				this.x = minBlockPos.getX();
				this.y = minBlockPos.getY();
				this.z = minBlockPos.getZ();
			}

		}
	}


}
	@Override
	public void writeToNBT(NBTTagCompound parentNBTTagCompound)
	{
		super.writeToNBT(parentNBTTagCompound);
		parentNBTTagCompound.setBoolean("open", this.open);

		NBTTagList nbttaglist = new NBTTagList();
		for (int i = 0; i < this.blocks.size(); i ++) {
			NBTTagCompound nbttagcompound = new NBTTagCompound();
			NBTTagCompound nbttagcompound1 = new NBTTagCompound();
			nbttagcompound.setInteger("Slot", i);
			nbttagcompound.setInteger("id", Block.getStateId(this.blocks.get(i)));
			try {
				this.tileentities.get(i).writeToNBT(nbttagcompound1);
				nbttagcompound.setTag("tag", nbttagcompound1);
			} catch (Exception ex) {

			}
			nbttaglist.appendTag(nbttagcompound);
		}
		parentNBTTagCompound.setTag("Blocks", nbttaglist);		
	}

	// This is where you load the data that you saved in writeToNBT
	@Override
	public void readFromNBT(NBTTagCompound parentNBTTagCompound)
	{
		super.readFromNBT(parentNBTTagCompound);
		this.open = parentNBTTagCompound.getBoolean("open");

		NBTTagList nbttaglist = parentNBTTagCompound.getTagList("Blocks", 10);
		for (int i = 0; i < nbttaglist.tagCount(); i ++) {
			NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
			this.blocks.add(Block.getStateById(nbttagcompound.getByte("id")));
			NBTTagCompound nbttagcompound1 = (NBTTagCompound) nbttagcompound.getTag("tag");
			if (nbttagcompound1 != null) { 
				TileEntity tileentity = TileEntity.createAndLoadEntity(nbttagcompound1);
				this.tileentities.add(tileentity);
				System.out.println("The id of the loaded tile entity is " + tileentity);
			}
		}
	}

    /**
     * Allows for a specialized description packet to be created. This is often used to sync tile entity data from the
     * server to the client easily. For example this is used by signs to synchronise the text to be displayed.
     */
    public Packet getDescriptionPacket()
    {
        NBTTagCompound nbttagcompound = new NBTTagCompound();
        this.writeToNBT(nbttagcompound);
        nbttagcompound.removeTag("open");
        nbttagcompound.setBoolean("open", this.open);
        NBTTagList nbttaglist = new NBTTagList();
		for (int i = 0; i < this.blocks.size(); i ++) {
		NBTTagCompound nbttagcompound1 = new NBTTagCompound();
		nbttagcompound1.setInteger("id", Block.getStateId(this.blocks.get(i)));
		nbttaglist.appendTag(nbttagcompound1);
		}
		nbttagcompound.setTag("Blocks", nbttaglist);
        
        return new S35PacketUpdateTileEntity(this.pos, 5, nbttagcompound);
    }

	@Override
	public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
		readFromNBT(pkt.getNbtCompound());
	}

}

 

 

 

 

CustomBlock.class:

 

package com.kalman98.custommod.blocks;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;

import com.kalman98.custommod.tileentities.CustomTileEntity;

public class CustomBlock extends BlockContainer {


protected CustomBlock(Material materialIn) {

	super(materialIn);
}

//@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {

	return new CustomTileEntity();
}

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) {
	if (!worldIn.isRemote) {
		CustomTileEntity tileentity = (CustTileEntity) worldIn.getTileEntity(pos);
		if (!tileentity.opening && !tileentity.closing) {
			tileentity.x = pos.getX();
			tileentity.y = pos.getY();
			tileentity.z = pos.getZ();
			tileentity.index = 0;
			tileentity.maxBlockPos = new BlockPos(pos.getX() + 4, pos.getY() + 4, pos.getZ() + 4);
			tileentity.minBlockPos = pos;
			if (tileentity.open) {
				tileentity.closing = true;
			} else if (!tileentity.open) {
				tileentity.opening = true;
			}
		}
	}
	return true;
}
}

 

 

 

So from CustomTileEntity, this code here is where the problem seems to be:

 

if (worldIn.getBlockState(new BlockPos(x, y, z)).getBlock() != CustomModBlockList.blockCustomBlock && this.blocks.get(index).getBlock() != Blocks.air) worldIn.setBlockState(new BlockPos(x, y, z), this.blocks.get(this.index));
			if (worldIn.getBlockState(new BlockPos(x, y, z)).getBlock() != CustomModBlockList.blockCustomBlock && this.tileentities.get(index) != null) {
				NBTTagCompound nbttagcompound = new NBTTagCompound();
				this.tileentities.get(index).writeToNBT(nbttagcompound);
				worldIn.getTileEntity(new BlockPos(x, y, z)).readFromNBT(nbttagcompound);

				//worldIn.setTileEntity(new BlockPos(x, y, z), this.tileentities.get(index));
				System.out.println("Placing a tile entity at " + x + ", " + y + ", " + z + "! It's " + this.tileentities.get(index));
				//System.out.println(this.tileentities.get(index).getTileData());
			}

 

What I originally tried here was this:

 

if (worldIn.getBlockState(new BlockPos(x, y, z)).getBlock() != CustomModBlockList.blockCustomBlock && this.blocks.get(index).getBlock() != Blocks.air) worldIn.setBlockState(new BlockPos(x, y, z), this.blocks.get(this.index));
if (worldIn.getBlockState(new BlockPos(x, y, z)).getBlock() != CustomModBlockList.blockCustomBlock && this.tileentities.get(index) != null)  worldIn.setTileEntity(new BlockPos(x, y, z), this.tileentities.get(index));

 

I tried using the code on a chest with items in it, and the chest copied just fine, but when it was placed out again it was empty. So I tried the code that you see in CustomTileEntity above just to see what would happen, and it worked... halfway. The chest full of items copied and placed back out right, and the items stayed in it. However, the items all had a count of 0. I tested it on a couple other tile entities, and it seemed to only copy half of the NBT information on them all. I've gone through all the Vanilla code I can think of that could help, and none of it has. So, do any of you know how I could do this?

 

 

Also, while I'm here, I might as well mention one other problem I'm having with the same block/tile entity. In the code above, I have a writeToNBT() method and a readFromNBT() method, so when the block gets unloaded it can remember which blocks it picked up. writeToNBT() works fine, like this:

 

	@Override
	public void writeToNBT(NBTTagCompound parentNBTTagCompound)
	{
		super.writeToNBT(parentNBTTagCompound);
		parentNBTTagCompound.setBoolean("open", this.open);

		NBTTagList nbttaglist = new NBTTagList();
		for (int i = 0; i < this.blocks.size(); i ++) {
			NBTTagCompound nbttagcompound = new NBTTagCompound();
			NBTTagCompound nbttagcompound1 = new NBTTagCompound();
			nbttagcompound.setInteger("Slot", i);
			nbttagcompound.setInteger("id", Block.getStateId(this.blocks.get(i)));
			try {
				this.tileentities.get(i).writeToNBT(nbttagcompound1);
				nbttagcompound.setTag("tag", nbttagcompound1);
			} catch (Exception ex) {

			}
			nbttaglist.appendTag(nbttagcompound);
		}
		parentNBTTagCompound.setTag("Blocks", nbttaglist);		
	}

 

And here's the readFromNBT() method:

 

	// This is where you load the data that you saved in writeToNBT
	@Override
	public void readFromNBT(NBTTagCompound parentNBTTagCompound)
	{
		super.readFromNBT(parentNBTTagCompound);
		this.open = parentNBTTagCompound.getBoolean("open");

		NBTTagList nbttaglist = parentNBTTagCompound.getTagList("Blocks", 10);
		for (int i = 0; i < nbttaglist.tagCount(); i ++) {
			NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
			this.blocks.add(Block.getStateById(nbttagcompound.getByte("id")));
			NBTTagCompound nbttagcompound1 = (NBTTagCompound) nbttagcompound.getTag("tag");
			if (nbttagcompound1 != null) { 
				TileEntity tileentity = TileEntity.createAndLoadEntity(nbttagcompound1);
				this.tileentities.add(tileentity);
				System.out.println("The id of the loaded tile entity is " + tileentity);
			}
		}
	}

 

I've checked with /blockdata and the boolean, blocks and tile entities save just fine. My problem here though is that the blocks lose their data values. So for instance, diorite loads back as plain stone, and colored wool is just white. I'm using the Block.getStateId() method, and then Block.getStateById() to load it back. Any ideas as to why it loses the data value, and how to fix it?

 

 

Thanks in advance for your help. I hope I posted this all correctly, seeing as it's my first post on here. If you need more code or details just ask, and I'll give it to you.

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.