Jump to content

Recommended Posts

Posted

Hey everyone!

 

I am still just starting to develop on Minecraft and maybe its my inexperience, but the forge documentation feels like it has no reference links or additional documentation to help me understand what I am actually using. So I read up on Forge's BlockState information page and wanted to just create a basic custom block that had an addition property. The property I created is an integer that ranges from 0-3. What I want to happen is that every time I place a block, this property (named "life") is initiated to the value 3. Then, every time I right click the block this value is decremented until it hits 0 and is destroyed. Here is what I have in the block class so far. 

public class ExampleBlock extends Block {

    private IProperty<Integer> property = IntegerProperty.create("life",0,3);

    public ExampleBlock() {
        super(Block.Properties.create(Material.GLASS).harvestTool(ToolType.AXE).harvestLevel(0).hardnessAndResistance(0.5f, 20.0f).lightValue(16).sound(SoundType.GLASS));
        this.setRegistryName("dlm", "example_block");
    }

    @Override
    protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
        builder.add(property);
    }
}

 

So I have having a couple issues with this...

 

1. I can not figure out where I can initialize that "life" property to the value 3.

 

2. All functions like "onBlockClicked" or "func_225533_a" (I guess this is my onBlockActivated??? To be honest I see no mention of that function anywhere in the block class) are deprecated. I heard this is done because these functions are supposed to be called in BlockState but creating my own custom BlockState has become a nightmare since I do not know how to properly create a ImmutableMap and I have no clue how to use a collection apparently. My basic idea was to steal the already created BlockState from this block and just pass in the new property into my own new BlockState. But when writing my own BlockState class I realized I can't access a property that the state has no mention of.

 

Can anyone help me figure out how to property add functionality to my new block?

 

Thank you!

Posted

You should be able to use the 'getStateForPlacement' method found in Block.java to set the life value.
This is the code I used for setting the direction of a block

    public BlockState getStateForPlacement(BlockItemUseContext context) {
        return this.getDefaultState().with(FACING, context.getPlacementHorizontalFacing().getOpposite());
    }

 

For your second problem maybe try using the 'interactWith' method which is found in the AbstractFurnaceBlock.java
This is the code I have for a TileEntity I created

    protected void interactWith(World worldIn, BlockPos pos, PlayerEntity player) {
        TileEntity tileentity = worldIn.getTileEntity(pos);
        if (tileentity instanceof BrewerTileEntity) {
            player.openContainer((INamedContainerProvider)tileentity);
            player.addStat(Stats.INTERACT_WITH_FURNACE);
        }

    }

 

  • Thanks 1
Posted (edited)
1 hour ago, Dizzylizard22 said:

2. All functions like "onBlockClicked" or "func_225533_a" (I guess this is my onBlockActivated??? To be honest I see no mention of that function anywhere in the block class) are deprecated.

 

You can override those. Mojang use deprecation too much. The really deprecated ones have comments from forge.

Btw, If you don't have the onBlockActivated in your list you should use the latest forge version instead of the recommended one.

 

 

To set the value:

world.setBlockState(pos, this.getDefaultState.with(LIFE, value));

 

Edited by QuantumSoul
Posted

@TheRedWaffle I attempted using your suggestions. I believe I was able to initialize my property. Now lets say I want to decrement the value every time I click the block. Here is what I have so far.

public class ExampleBlock extends Block {

    private IProperty<Integer> property = IntegerProperty.create("life",0,3);

    public ExampleBlock() {
        super(Block.Properties.create(Material.GLASS).harvestTool(ToolType.AXE).harvestLevel(0).hardnessAndResistance(0.5f, 20.0f).lightValue(16).sound(SoundType.GLASS));
        this.setRegistryName("dlm", "example_block");
    }

    @Override
    protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
        builder.add(property);
    }

    @Nullable
    @Override
    public BlockState getStateForPlacement(BlockItemUseContext context) {
        return this.getDefaultState().with(property,3);
    }

    @Override
    public void onBlockClicked(BlockState state, World worldIn, BlockPos pos, PlayerEntity player) {
        state.get(property)  // How would I decrement this value?
    }

}

 

 

Also,

1 hour ago, TheRedWaffle said:

AbstractFurnaceBlock.java

I do not see any mention to this class or see any documentation online. Not sure how I could implement interactWith without that information.

 

@QuantumSoul I went ahead and updated to the lasted mdk. After poking around a bit, I found that onBlockActivated is not in the Block class. I can only find it in the BlockState class. Not sure how I could override it without making an entirely new BlockState

Posted (edited)

You need to:

  1. Get the current value
  2. Subtract 1
  3. Deal with what happens when this makes it go below the minimum allowed value
  4. Set the new value

  

14 minutes ago, Dizzylizard22 said:

I do not see any mention to this class or see any documentation online. Not sure how I could implement interactWith without that information.

Its a vanilla class. Go find it inside your IDE.

 

Edited by Draco18s
  • Thanks 1

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
11 minutes ago, Dizzylizard22 said:

@QuantumSoulI went ahead and updated to the lasted mdk. After poking around a bit, I found that onBlockActivated is not in the Block class. I can only find it in the BlockState class. Not sure how I could override it without making an entirely new BlockState

It is in the Block class

public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult blockRayTraceResult)

 

 

For the decrementation,

you get the value with:

value = state.get(property)

you set it with

worldIn.setBlockState(pos, state.with(property, value));

You can figure the rest by yourself

  • Thanks 1

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.