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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Make sure you have Optifine installed as a mod. Go into Options > Video Settings > Shaders > and then click the shader you want Make sure the shader.zip files are in the shaderpacks folder inside the minecraft folder
    • It sounds like you're probably registering the item in the wrong place, try looking at this tutorial for how to register items:  Forge Modding Tutorial - Minecraft 1.20: Custom Items & Creative Mode Tab | #2 This (free) tutorial series is excellent, by the way, and I'd highly recommend watching through some or all of the videos. There may also be an error in the code I showed above since I was in a hurry, but it should be enough for the general idea. I can't be more specific since I don't know exactly what you plan to do.
    • Realizing I was a victim of a scam was a devastating blow. My initial investment of $89,000, driven by dreams of financial success and the buzz surrounding a new cryptocurrency project, turned into a nightmare. The project promised high returns and rapid gains, attracting many eager investors like myself. However, as time passed and inconsistencies began to surface, it became evident that I had made a grave mistake by not thoroughly vetting the brokerage company handling the investment. Feeling anxious and betrayed, I desperately searched for a way to recover my funds. It was during this frantic search that I stumbled upon the Lee Ultimate Hacker tool through a Facebook post. With little left to lose, I decided to reach out to their team for help. To my relief, they were quick to respond and immediately started recovering my compromised email and regaining access to my cryptocurrency wallets. The team at Lee Ultimate Hacker was incredibly professional and transparent throughout the process. They meticulously traced the digital footprints left by the scammers, employing advanced technological methods to unravel the complex network that had ensnared my funds. Their expertise in cybersecurity and recovery strategies gradually began to turn the tide in my favor. Although the scammers had already siphoned off $30,000 worth of Bitcoin, Lee Ultimate Hacker was relentless in their pursuit. They managed to expose the fraudulent activities of the scam operators, revealing their identities and the mechanisms they used to lure investors. This exposure was crucial not only for my case but also as a warning to the wider community about the perils of unverified investment schemes. As we progressed, it became a race against time to retrieve the remaining $59,000 before the scammers could vanish completely. Each step forward was met with new challenges, as these criminals constantly shifted tactics and moved their digital assets to evade capture. Nonetheless, the determination and skill of the recovery team kept us hopeful. Throughout this ordeal, I learned the hard value of caution and due diligence in investment, especially within the volatile world of cryptocurrency. The experience has been incredibly taxing, both emotionally and financially, but the support and results provided by Lee Ultimate Hacker have been indispensable. The recovery process is ongoing, and while the final outcome remains uncertain, the progress made so far gives me hope. The battle to recover the full amount of my investment continues, and with the expertise of Lee Ultimate Hacker, I remain optimistic about the eventual recovery of my funds. Their commitment to their clients and proficiency in handling such complex cases truly sets them apart in the field of cyber recovery. LEEULTIMATEHACKER@ AOL. COM   Support @ leeultimatehacker . com.  telegram:LEEULTIMATE   wh@tsapp +1  (715) 314  -  9248     
    • Hi everyone. I’m excited to share my experience with CrackerWizard Recovery Firm. They helped me recover a substantial amount of crypto after falling victim to online scams disguised as Bitcoin investments. CrackerWizard’s exceptional service enabled me to retrieve my lost funds, despite the complex circumstances surrounding the case. With their dedicated team and advanced technology, they swiftly traced and recovered my assets. CrackerWizard is a reliable partner in the crypto world, highly recommended for anyone facing similar challenges. Contact them.
  • Topics

×
×
  • Create New...

Important Information

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