Jump to content

writeToNBT/readFromNBT not called?


ConsoleHack000

Recommended Posts

Hello everyone. I am making a block which can change its texture to that of some other block's. I use a TileEntity to store the metadata and the ID of the block of which I am showing the texture. The problem is that writeToNBT and readFromNBT methods are not called (checked using System.out.println). Here's the code of TileEntityCoolBlock.java

package net.net23.aregsroom.mc.lotostuff.entities.tile;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

/**
* Created by DeLL on 10/09/2016.
*/
public class TileEntityCoolBlock extends TileEntity{

    public int b = 4;
    public int meta = 0;

    public TileEntityCoolBlock() {
    }

    @Override
    public Packet getDescriptionPacket() {
        NBTTagCompound tag = new NBTTagCompound();
        this.writeToNBT(tag);
        return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, tag);
    }

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

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        meta = nbt.getInteger("meta");
        b = nbt.getInteger("id");
    }
    @Override
    public void writeToNBT(NBTTagCompound nbt) {
        nbt.setInteger("id",b);
        nbt.setInteger("meta",meta);
        System.out.println(nbt);
    }

}

And CoolBlock.java

package net.net23.aregsroom.mc.lotostuff.tools.coolblock;

import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.net23.aregsroom.mc.lotostuff.entities.tile.TileEntityCoolBlock;

import static net.net23.aregsroom.mc.lotostuff.Constants.tabMetal;

/**
* Created by DeLL on 04/09/2016.
*/
public class CoolBlock extends Block implements ITileEntityProvider{

    public CoolBlock() {
        super(Material.rock);
        setCreativeTab(tabMetal);
        setBlockName("coolblock");
        setHardness(2);
        setHarvestLevel("pickaxe", 2);
        setStepSound(Block.soundTypeMetal);
        setResistance(2);
        setLightOpacity(0);
        setBlockTextureName("ltstf:cool_block");
        GameRegistry.registerBlock(this, getUnlocalizedName().substring(5));
    }

    @Override
    public boolean isOpaqueCube() {
        return false;
    }

    @Override
    public boolean onBlockActivated(World w, int x, int y, int z, EntityPlayer p, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_) {
        if(!w.isRemote) return true;
        if(!(p.inventory.getCurrentItem().getItem() instanceof TextureExtractor)) return false;

        TileEntityCoolBlock b = new TileEntityCoolBlock();
        b.readFromNBT(p.inventory.getCurrentItem().getTagCompound()); // I can call it this way, and works fine, changes texture, but when I exit the world and enter it again it has the default texture.

        b.markDirty();
        w.setTileEntity(x,y,z,b);
        w.markBlockForUpdate(x,y,z);

        return true;
    }

    @Override
    public IIcon getIcon(IBlockAccess p, int x, int y, int z, int side) {
        TileEntityCoolBlock tecb = (TileEntityCoolBlock) p.getTileEntity(x,y,z);
        return Block.getBlockById(tecb.b).getIcon(side,tecb.meta);
    }
    @Override
    public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
        return new TileEntityCoolBlock();
    }
}

And I registered the tileentity like so:

GameRegistry.registerTileEntity(TileEntityCoolBlock.class, "ltstf-tileentity_coolblock");

in my preInit method. Any help would be really appreciated.

Currently developing a mod called Lot'O'Stuff for 1.7.10 (I know very well its outdated.)

Link to comment
Share on other sites

Your TileEntity's NBT methods aren't calling super, ergo there is some data loss (the TE's position in the world), and rather than try and recover what it can (because it doesn't know where it goes), Minecraft just creates a new one.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Your TileEntity's NBT methods aren't calling super, ergo there is some data loss (the TE's position in the world), and rather than try and recover what it can (because it doesn't know where it goes), Minecraft just creates a new one.

You mean This?

 @Override
    public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        meta = nbt.getInteger("meta");
        b = nbt.getInteger("id");
    }
    @Override
    public void writeToNBT(NBTTagCompound nbt) {
        super.writeToNBT(nbt);
        nbt.setInteger("id",b);
        nbt.setInteger("meta",meta);
    }

Doesn't seem to work. Still not getting called

Currently developing a mod called Lot'O'Stuff for 1.7.10 (I know very well its outdated.)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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