Jump to content

Recommended Posts

Posted (edited)

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
Posted

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/

Posted
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: {}

 

Posted (edited)
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/

Posted
21 minutes ago, jabelar said:

So Item is zero so you need to go next step and see if the tag data was correct.

I've tried doing some things only to mess up the code.. it seems no matter how I approach this it doesnt wish to sync properly..

Posted

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/

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

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

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