Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

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.

1 hour ago, Simon_kungen said:

public boolean hasTileEntity()

Try overriding the BlockState sensitive version

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.

  • Author
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());
}

 

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.

  • Author
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;
    }
}

 

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.

  • Author
8 minutes ago, Animefan8888 said:

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

Yup, totally missed that, thanks!

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.