Jump to content

Tile entity has amnesia?


the
 Share

Recommended Posts

So I have a tile entity of a container block, and on the client side at least, it seems to think everything is 0, false, etc.

1.) For instance, I have the debug code
 

IBlockState state = world.getBlockState(pos);
if (state.getBlock() == ModBlocks.MY_BLOCK) { // you'd think this would always be true
	return state.getValue(MyBlock.ACTIVATED);
}
MyMod.LOGGER.info(pos.getX() + " " + pos.getY() + " " + pos.getZ() + " " + state.getBlock().getUnlocalizedName()); // debug
return false;


which not only always prints something to the logger, but the thing that it prints is actually
 

0 0 0 tile:bedrock

 

(or sometimes tile:air, depending on what is at 0,0,0)

 

The value of pos is not being changed/overwritten anywhere in my code.

2.) As well, the tile entity resets to default whenever the world is reloaded, forgetting all of the items inside it. While I am setting NBT as follows:
 

@Override
public void readFromNBT(NBTTagCompound compound) {
    super.readFromNBT(compound);
    if (compound.hasKey("ITEMS")) {
        itemStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("ITEMS"));
    }
    if (compound.hasKey("previousRedstoneState")) {
        previousRedstoneState = compound.getBoolean("previousRedstoneState");
    }
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
    super.writeToNBT(compound);
    compound.setTag("ITEMS", itemStackHandler.serializeNBT());
    compound.setBoolean("previousRedstoneState", previousRedstoneState);
    return compound;
}


my GUI for the block does not display the correct previousRedstoneState. It does get the items right, but only until world exit.

3.) I have tried sending packets using the MinecraftByExample method, in case the GUI was a server/client issue:
 

@Nullable
@Override
public SPacketUpdateTileEntity getUpdatePacket() {
    return new SPacketUpdateTileEntity(getPos(), getBlockMetadata(), getUpdateTag());
}

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

@Override
public NBTTagCompound getUpdateTag() {
    NBTTagCompound nbtTagCompound = new NBTTagCompound();
    writeToNBT(new NBTTagCompound());
    return nbtTagCompound;
}

@Override
public void handleUpdateTag(NBTTagCompound tag) {
    this.readFromNBT(tag);
}


This updates the GUI text that corresponds to the NBT, but not to the blockstate. As well, if I store a byte array in the NBT that is being created from analyzing the inventory of a neighbouring block, that part of the NBT does not update correctly.

 

I can supply additional context as necessary.


How can I medicate my poor block?

Link to comment
Share on other sites

The "pos" is the field of the tile entity class that is inherited from TileEntity (and returned by this.getPos(), which using instead does not change the problem, and which I would have originally used more formally if 1 was not debug code).

The issue is happening with the fields of the block's tile entity class.

Link to comment
Share on other sites

Changing the body of getUpdateTag to

return new NBTTagCompound();

still produces "0 0 0 tile:bedrock".

As for writing both items and previousRedstoneState to the NBT, I'm following both the capability system outlined in this tutorial and the method used by TileEntityNote of remembering if a block is currently being powered so that it does not trigger again upon world reload. I'm still finding my way around modding and I'd very much appreciate if you have a more elegant way to handle NBT, though I will optimize after I have the block working as intended.

The rest of my code for the tile entity class is linked here (and is pretty much a combination of that tutorial and vanilla's previousRedstoneState as well).

Link to comment
Share on other sites

3 minutes ago, the said:

Changing the body of getUpdateTag to


return new NBTTagCompound();

still produces "0 0 0 tile:bedrock".

Yes, of course. Did you even read what I said?

 

4 minutes ago, the said:

As for writing both items and previousRedstoneState to the NBT, I'm following both the capability system outlined in this tutorial and the method used by TileEntityNote of remembering if a block is currently being powered so that it does not trigger again upon world reload. I'm still finding my way around modding and I'd very much appreciate if you have a more elegant way to handle NBT, though I will optimize after I have the block working as intended.

That tutorial does not cover syncing of data to the client. I am not talking about what you save to the disk. Please read again what I said.

Link to comment
Share on other sites

I understand you are talking about what is being shared over the network, not what is being saved to the disk, though Minecraft modding is new to me - should I create a separate pair of read/write NBT functions that only do the redstone state, and then use those in the packet functions in place of readFromNBT/writeToNBT?

Also, is "new NBTTagCompound()" not an empty NBTTagCompound? I was under the impression that it was.

Link to comment
Share on other sites

2 minutes ago, the said:

I understand you are talking about what is being shared over the network, not what is being saved to the disk, though Minecraft modding is new to me - should I create a separate pair of read/write NBT functions that only do the redstone state, and then use those in the packet functions in place of readFromNBT/writeToNBT?

You already have separate methods for the client-side synchronization. But right now you are just delegating to readFromNBT and writeToNBT, which are the "save me to disk" methods.

You may find this explanation helpful.

 

3 minutes ago, the said:

Also, is "new NBTTagCompound()" not an empty NBTTagCompound? I was under the impression that it was.

Exactly, which is your issue. Do you even understand what any of your code does or are you blindly copying from tutorials?

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.