Jump to content

[1.12.2] Saving NBT Data


Squirtle8459

Recommended Posts

For some odd reason, my TileEntity is not saving the item it stores when I reload my world, what am I doing wrong?

 

TileEntity:

package squirtle8459.passivepowerproduction.tileentity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import squirtle8459.passivepowerproduction.handler.powerGenHandler;

public class TileEntityMatterInterpreter extends TileEntityEnergyBase  implements ITickable {
		
	public TileEntityMatterInterpreter() {
		
	}
	
	public TileEntityMatterInterpreter(int capacity, int maxReceive, int maxExtract) {
		super(capacity, maxReceive, maxExtract);
	}
	
	public static final int SIZE = 1;
	
	public ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE) {
        @Override
        protected void onContentsChanged(int slot) {
        	sendUpdates();
        }
    };
	
    private void sendUpdates() {
		world.markBlockRangeForRenderUpdate(pos, pos);
		world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 3);
		world.scheduleBlockUpdate(pos,this.getBlockType(), 0, 0);
		TileEntityMatterInterpreter.this.markDirty();
	}
    
    @Override
	 public SPacketUpdateTileEntity getUpdatePacket() {
	 	BlockPos blockPos=getPos();;
	 	return new SPacketUpdateTileEntity(blockPos, 3, getUpdateTag());
	 }
    
    @Override
    public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
    	NBTTagCompound nbtTagCompound=pkt.getNbtCompound();
    	super.readFromNBT(nbtTagCompound);
    	readFromNBT(nbtTagCompound);
    }

    @Override
    public NBTTagCompound getUpdateTag() {
    	NBTTagCompound supertag=super.getUpdateTag();
    	writeToNBT(supertag);
    	return supertag;
    }

    @Override
    public void handleUpdateTag(NBTTagCompound tag) {
    	super.handleUpdateTag(tag);
    	readFromNBT(tag);
    }
    
	 @Override
	 public NBTTagCompound writeToNBT(NBTTagCompound tag) {
		  
		if(tag == null) tag = new NBTTagCompound();
		tag.setTag("Items", itemStackHandler.serializeNBT());
		return tag;
	 }
	 
	 @Override
	 public void readFromNBT(NBTTagCompound tag) {
					
		 itemStackHandler.deserializeNBT(tag.getCompoundTag("Items"));
		 if(tag.hasKey("energy")) this.energy = tag.getInteger("energy");     		
	 }

	 public boolean canInteractWith(EntityPlayer playerIn) {

		 return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
	 }

	 @Override
	 public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
	   if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
	     return true;
	   }
	   return super.hasCapability(capability, facing);
	 }
	 
	 @Override
	 public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
	   if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
	     return (T) itemStackHandler;
	   }
	   return super.getCapability(capability, facing);
	 }
	  
	public void update() {
		if(this.world == null)  return;
		ItemStack stack = itemStackHandler.getStackInSlot(0);
		int RF = powerGenHandler.getRFBase(stack);
		powerGenHandler.sendPower(world, pos, energy, capacity, RF);
	}

}

 

There's probably a bunch of redundant code in there somewhere :$

Edited by Squirtle8459
Link to comment
Share on other sites

I don't see anything obvious but have you traced your code? Using debugger or console statements you should simply confirm what is going on. If you print out what the tag is like in the write and read, if you confirm that the packets are working, and so forth, it should be really simple to see what is going wrong.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

5 minutes ago, jabelar said:

I don't see anything obvious but have you traced your code? Using debugger or console statements you should simply confirm what is going on. If you print out what the tag is like in the write and read, if you confirm that the packets are working, and so forth, it should be really simple to see what is going wrong.

When I insert an item into the tileentity, I get this from a print..

[09:20:49] [Server thread/INFO] [STDOUT]: [squirtle8459.passivepowerproduction.tileentity.TileEntityMatterInterpreter:writeToNBT:72]: [WRITE] ITEMS: {Size:1,Items:[{Slot:0,id:"minecraft:nether_star",Count:1b,Damage:0s}]}
[09:20:49] [main/INFO] [STDOUT]: [squirtle8459.passivepowerproduction.tileentity.TileEntityMatterInterpreter:readFromNBT:81]: [READ] ITEMS: {Size:1,Items:[{Slot:0,id:"minecraft:nether_star",Count:1b,Damage:0s}]}

When I reload the world..

 

The ReadFromNBT returns

[09:22:00] [Server thread/INFO] [STDOUT]: [squirtle8459.passivepowerproduction.tileentity.TileEntityMatterInterpreter:readFromNBT:81]: [READ] ITEMS: {}

 

Link to comment
Share on other sites

5 minutes ago, Squirtle8459 said:

[09:20:49] [Server thread/INFO] [STDOUT]: [squirtle8459.passivepowerproduction.tileentity.TileEntityMatterInterpreter:writeToNBT:72]: [WRITE] ITEMS: {Size:1,Items:[{Slot:0,id:"minecraft:nether_star",Count:1b,Damage:0s}]}
[09:20:49] [main/INFO] [STDOUT]: [squirtle8459.passivepowerproduction.tileentity.TileEntityMatterInterpreter:readFromNBT:81]: [READ] ITEMS: {Size:1,Items:[{Slot:0,id:"minecraft:nether_star",Count:1b,Damage:0s}]}

When I reload the world..

 

The ReadFromNBT returns


[09:22:00] [Server thread/INFO] [STDOUT]: [squirtle8459.passivepowerproduction.tileentity.TileEntityMatterInterpreter:readFromNBT:81]: [READ] ITEMS: {}

 

In your first snippet above, the read is on the client side and in your second it is on the server side. You need to trace what is happening on each side step by step. For example, in the second case on the server what was in the tag passed into the read from NBT?

 

You need to basically follow basic debugging. If a value isn't what you expect, then work backwards and see if the data going into the code is also not what you expect. So Item is zero so you need to go next step and see if the tag data was correct.

Edited by jabelar

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Code isn't magic, it is very logical. So you can solve any such problem just by observing the execution. You should not be trying to just modify code and see if it fixes it. Instead, put in console print statements at every point in the code that matters. For example, at the beginning of the read from NBT you should put a statement that prints out the tag, and then after the deserialization you should print out the item. Why worry about the item being zero if you find that the tag is empty? Or maybe the method isn't even being called when you expect? You need to work backwards to find at what point the data stops matching the expected behavior.

 

Your IDE (Eclipse or whatever you're using) also provides debugging mode which can help you trace the execution of the vanilla classes. Put a breakpoint on the read from NBT method and see what the data is. If the data is bad, then put a breakpoint where that method is called and see what is happening there. If that data is bad put a breakpoint at an earlier point.

 

In programming you should never just write code and say "the result is wrong so I'm stuck". Computers are fully logical, so the reason will be obvious if you just watch the execution.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.