Jump to content

Recommended Posts

Posted

My goal with this is to have a block with a "Health Value" to both determine whether it should break and it's current model. I currently have that value being set to 3000 when the block is placed down.

I don't understand why, but after mimicking both vanilla and other mod examples of saving NBT data in a TileEntity, mine just won't save. 

I have gotten the actual value/data to work since I can use the "/data get block" command on it, but it always resets to 0 when I close and reload the world.

 

 

This is my Block Class:

public class WarenaiBlock extends Block
{
    public static final IntegerProperty CRACKED_DIRTY_CLEAN_POLISHED = CustomBlockstateProperties.CRACKED_DIRTY_CLEAN_POLISHED;

    public WarenaiBlock(Properties properties) {
        super(properties);
        this.registerDefaultState(this.stateDefinition.any()
                .setValue(CRACKED_DIRTY_CLEAN_POLISHED, Integer.valueOf(3)));
    }

    protected void createBlockStateDefinition(StateContainer.Builder<Block, BlockState> stateBuilder) {
        stateBuilder.add(CRACKED_DIRTY_CLEAN_POLISHED);
    }

    public void onPlace(BlockState p_220082_1_, World p_220082_2_, BlockPos p_220082_3_, BlockState p_220082_4_, boolean p_220082_5_) {
        StrongBlockTileEntity.setInitialBlockHealth();
    }

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

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

 

 

This is my TileEntity class:

public class StrongBlockTileEntity extends TileEntity
{
    private static int strongBlockHealth;


    public StrongBlockTileEntity(TileEntityType<?> tileEntityType) {
        super(tileEntityType);
    }

    public StrongBlockTileEntity() { this(TileEntityTypesInit.WARENAI_BLOCK_BLACK_STRONGBLOCK_TILE.get()); }


    public static void damageStrongBlock(int damageAmount) {
        int currentBlockHealth = strongBlockHealth;
        strongBlockHealth = currentBlockHealth - damageAmount;

    }

    public void repairStrongBlock(int repairAmount) {
        int currentBlockHealth = strongBlockHealth;
        strongBlockHealth = currentBlockHealth + repairAmount;
    }



    public static int getStrongBlockHealth() { return strongBlockHealth; }

    public static void showBlockHealth() {
        System.out.println("Block Health is: " + getStrongBlockHealth());
    }

    public static void setInitialBlockHealth() { strongBlockHealth = 3000; }




    /**
     * Overrides
     */

    @Override
    public CompoundNBT save(CompoundNBT tags) {
        super.save(tags);
        tags.putInt("BlockHealth", this.strongBlockHealth);
        return tags;
    }

    @Override
    public void load(BlockState state, CompoundNBT tags) {
        super.load(state, tags);
        strongBlockHealth = tags.getInt("BlockHealth");
    }


}

 

 

I also have two "Development Tools" that I made to manipulate the BlockHealth Value which do work:

public class DamageItem extends Item
{
    public DamageItem(Properties properties) {
        super(properties);
    }


    @Override
    public ActionResultType useOn(ItemUseContext context) {
        World worldIn = context.getLevel();
        BlockPos blockPos = context.getClickedPos();
        BlockState blockState = worldIn.getBlockState(blockPos);

        //Block warenaiClassBlock = blockState.getBlock();
        boolean warenaiClassBlock = blockState.getBlock() instanceof WarenaiBlock;

        if (blockState.hasTileEntity() && warenaiClassBlock) {
            StrongBlockTileEntity strongBlockTile = new StrongBlockTileEntity();

            strongBlockTile.damageStrongBlock(50);
            //strongBlockTile.showBlockHealth();
            return ActionResultType.SUCCESS;

        } else {
            return ActionResultType.PASS;
        }
    }
}

 

 

Posted

use debugger, to look why the value is 0, but i think it's because strongBlockHealth is static.
is there a reason why the field is static or do you don't know basic java?

Posted

I am currently taking Java Classes with my highschool, so I still a little weak on remembering those things.

However, I had it static because the "setInitialBlockHealth()" method cannot be referenced when it isn't static by my "onPlace" method in my block class, so then I have to change all the other methods and the "strongBlockHealth" field to static to match. But that then causes the problem above.

The value also doesn't change at all when the field and methods are not static. 

Posted

With what I have above, the value does change and stays at the changed value until the world is unloaded. (If I use the Dev Item I made for damaging it 3 times to get to 2700 it will stay there)

Just doesn't save NBT wise

Posted (edited)

You can't use static methods and fields like this. Those should all be instance methods.

You want to set a default value? Supply it in the constructor.

This is also wrong

2 hours ago, FireTamer81 said:
StrongBlockTileEntity strongBlockTile = new StrongBlockTileEntity();
Edited by Draco18s

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.

Posted

Okay, non of the methods or fields are static now.

I put the default value in the constructor:

public StrongBlockTileEntity(TileEntityType<?> tileEntityType) {
        super(tileEntityType);
        this.strongBlockHealth = 3000;
    }

 

But now it doesn't change when I use the Dev Tool on it.

 

I also changed this part maybe an hour ago because I realized it was stupid. (I think the reason it was stupid was because I was supplying the class extending TileEntity instead of the TileEntityType RegistryObject, but not sure)

StrongBlockTileEntity strongBlockTile = new StrongBlockTileEntity();

 

Posted

Okay, I got it to work, but only because I found a tutorial for 1.11.2 that had markDirty(); on methods that changed the given values which I then tracked to .setChanged(); in the current version. 

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.