Jump to content

Recommended Posts

Posted (edited)

Hi

 

I'm trying to do a TileEntity on one of my blocks which is only supposed to store a single value and then, later on, attach a TileEntityRenderer to display that value (and of course do stuff depending on that value). I've created the TileEntityType without any errors in the log, added the override functions in my block class, but whenever I try and get the data it crashes on a NullPointerException and doing exceptions such as creating a new one only results in it creating a new TileEntity.

 

TreeTapTileEntity.java

Spoiler

public class TreeTapTileEntity extends TileEntity
{

    private int volume = 0;

    public TreeTapTileEntity()
    {
        super(IntercraftTileEntities.TREETAP);
    }


    public CompoundNBT write(CompoundNBT compound)
    {
        compound.putInt("volume", this.volume);

        return compound;
    }

    public void read(CompoundNBT compound)
    {
        this.volume = compound.getInt("volume");
    }
}

 

 

 

TreeTap.java

Spoiler

@Override
public boolean onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit)
{
    TileEntity tile = worldIn.getTileEntity(pos);
    System.out.println(tile.getTileData().getInt("volume"));
}

@Override
public boolean hasTileEntity()
{
    return true;
}


@Override
public TileEntity createTileEntity(BlockState state, IBlockReader world)
{
    return new TreeTapTileEntity();
}

 

 

 

IntercraftTileEntites.java

Spoiler

public static final TileEntityType<TreeTapTileEntity> TREETAP;


static {
    TREETAP = buildTE("treetap",TileEntityType.Builder.create(TreeTapTileEntity::new,IntercraftBlocks.TREETAP));
}

private static TileEntityType buildTE(String registryName, TileEntityType.Builder builder)
{

    TileEntityType type = builder.build(null);
    type.setRegistryName(Reference.MODID,registryName);
    return type;
}

 

 

 

Edited by Simon_kungen
Title typo.
Posted
44 minutes ago, Simon_kungen said:

whenever I try and get the data it crashes on a NullPointerException

Crashes doesnt post crash. What value is null? Is it the te or is it the tile data?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
1 minute ago, Animefan8888 said:

It has a BlockState parameter

Like this?

@Override
public boolean hasTileEntity(BlockState state)
{
    return true;
}

 

It doesn't appear to crash anymore. But I don't seem to get my value out of that particular block.

 

TreeTapTileEntity.java

public class TreeTapTileEntity extends TileEntity
{

    private int volume = 0;

    public TreeTapTileEntity()
    {
        super(IntercraftTileEntities.TREETAP);
    }


    @Override
    public CompoundNBT write(CompoundNBT compound)
    {
        compound.putInt("volume", this.volume);

        return compound;
    }

    @Override
    public void read(CompoundNBT compound)
    {
        this.volume = compound.getInt("volume");
    }


    public int getVolume()
    {
        return this.volume;
    }
}

 

TreeTap.java

public boolean onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit)
{
    TreeTapTileEntity tile = (TreeTapTileEntity) worldIn.getTileEntity(pos);
    System.out.println(tile.getVolume());
}

 

Posted
10 minutes ago, Simon_kungen said:

But I don't seem to get my value out of that particular block.

What do you mean? From the code you've shown I do you mean it's always 0? Because of course it would be you never set it to anything else.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
1 minute ago, Animefan8888 said:

What do you mean? From the code you've shown I do you mean it's always 0? Because of course it would be you never set it to anything else.

Oh, sorry, what I meant is that it keeps setting back to the default value 0 every time I re-loads the game/world. I added the ITickableTileEntity interface to it as I'm going to need it.

public class TreeTapTileEntity extends TileEntity implements ITickableTileEntity
{

    private int volume = 0;
    private boolean canFill = false;

    public TreeTapTileEntity()
    {
        super(IntercraftTileEntities.TREETAP);
    }


    @Override
    public void tick()
    {
        if (canFill) {
            volume++;
        }
    }


    @Override
    public CompoundNBT write(CompoundNBT compound)
    {
        compound.putInt("volume", this.volume);
        compound.putBoolean("can_fill",this.canFill);

        return compound;
    }

    @Override
    public void read(CompoundNBT compound)
    {
        this.volume = compound.getInt("volume");
        this.canFill = compound.getBoolean("can_fill");
    }


    public int getVolume()
    {
        return this.volume;
    }

    public void setCanFill(boolean bool)
    {
        canFill = bool;
    }
}

 

Posted
32 minutes ago, Simon_kungen said:

Oh, sorry, what I meant is that it keeps setting back to the default value 0 every time I re-loads the game/world. I added the ITickableTileEntity interface to it as I'm going to need it.

If I'm not mistaking you also have to call the supers on read and write.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.