Jump to content

Adding custom properties to blockstates


Dizzylizard22

Recommended Posts

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!

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

@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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Museumbola adalah situs judi slot BANK BCA tergacor 2024 dengan minimal deposit  10 ribu rupiah saja anda sudah bisa spin slot gacor di Museumbola. Mulai dari Pragmatic, Pg Soft, Habanero, Playstar, Spadegaming, dan masih banyak lagi. Pilihan slot yang begitu banyak tidak akan membuat anda bosan. Segera daftarkan BANK BCA anda dan jadi member aktif Museumbola. anda akan diberi kesempatan mendapatkan bonus deposit harian setiap hari. Segera Klik DAFTAR sekarang juga sebelum terlambat.  
    • Slot BANK BCA adalah pilihan tergacor untuk memulai bermain slot judi Online dimuka bumi saat ini. Jika anda mempunyai BANK BCA, Anda berkesempatan mendapatkan Akun Pro atau ID pro untuk bermain slot. Tunggu apa lagi? Segera daftarkan diri anda sekarang dan dapatkan kemewahan menang maxwin di Museumbola.
    • SLOT SCATTER HITAM : SLOT MAHJONG SCATTER HITAM - MAHJONG WAYS SCATTER HITAM - SLOT PRAGMATIC SCATTER HITAM KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << SITUS SLOT GACOR 88 MAXWIN X500 HARI INI TERBAIK DAN TERPERCAYA GAMPANG MENANG Dunia Game gacor terus bertambah besar seiring berjalannya waktu, dan sudah tentu dunia itu terus berkembang serta merta bersamaan dengan berkembangnya SLOT GACOR sebagai website number #1 yang pernah ada dan tidak pernah mengecewakan sekalipun. Dengan banyaknya member yang sudah mempercayakan untuk terus menghasilkan uang bersama dengan SLOT GACOR pastinya mereka sudah percaya untuk bermain Game online bersama dengan kami dengan banyaknya testimoni yang sudah membuktikan betapa seringnya member mendapatkan jackpot besar yang bisa mencapai ratusan juta rupiah. Best online Game website that give you more money everyday, itu lah slogan yang tepat untuk bermain bersama SLOT GACOR yang sudah pasti menang setiap harinya dan bisa menjadikan bandar ini sebagai patokan untuk mendapatkan penghasilan tambahan yang efisien dan juga sesuatu hal yang fix setiap hari nya. Kami juga mendapatkan julukan sebagai Number #1 website bocor yang berarti terus memberikan member uang asli dan jackpot setiap hari nya, tidak lupa bocor itu juga bisa diartikan dalam bentuk berbagi promosi untuk para official member yang terus setia bermain bersama dengan kami. Berbagai provider Game terus bertambah banyak setiap harinya dan terus melakukan support untuk membuat para official member terus bisa menang dan terus maxwin dalam bentuk apapun maka itu langsung untuk feel free to try yourself, play with SLOT GACOR now or never !
    • SLOT 1000 : SLOT1000 DEPOSIT 1000 VIA DANA - SLOT 1000 VIA QRIS - SLOT DEPOSIT QRIS 1000 - SLOT DEPO 1K - SLOT 1000 BET KECIL KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << SITUS SLOT GACOR 88 MAXWIN X500 HARI INI TERBAIK DAN TERPERCAYA GAMPANG MENANG Dunia Game gacor terus bertambah besar seiring berjalannya waktu, dan sudah tentu dunia itu terus berkembang serta merta bersamaan dengan berkembangnya SLOT GACOR sebagai website number #1 yang pernah ada dan tidak pernah mengecewakan sekalipun. Dengan banyaknya member yang sudah mempercayakan untuk terus menghasilkan uang bersama dengan SLOT GACOR pastinya mereka sudah percaya untuk bermain Game online bersama dengan kami dengan banyaknya testimoni yang sudah membuktikan betapa seringnya member mendapatkan jackpot besar yang bisa mencapai ratusan juta rupiah. Best online Game website that give you more money everyday, itu lah slogan yang tepat untuk bermain bersama SLOT GACOR yang sudah pasti menang setiap harinya dan bisa menjadikan bandar ini sebagai patokan untuk mendapatkan penghasilan tambahan yang efisien dan juga sesuatu hal yang fix setiap hari nya. Kami juga mendapatkan julukan sebagai Number #1 website bocor yang berarti terus memberikan member uang asli dan jackpot setiap hari nya, tidak lupa bocor itu juga bisa diartikan dalam bentuk berbagi promosi untuk para official member yang terus setia bermain bersama dengan kami. Berbagai provider Game terus bertambah banyak setiap harinya dan terus melakukan support untuk membuat para official member terus bisa menang dan terus maxwin dalam bentuk apapun maka itu langsung untuk feel free to try yourself, play with SLOT GACOR now or never !
    • SLOT DEMO PRINCESS 1000 : DEMO SLOT STARLIGHT PRINCESS x 1000 RUPIAH GRATIS TANPA DEPOSIT KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << KLIK DISINI DAFTAR DISINI SLOT VVIP << SITUS SLOT GACOR 88 MAXWIN X500 HARI INI TERBAIK DAN TERPERCAYA GAMPANG MENANG Dunia Game gacor terus bertambah besar seiring berjalannya waktu, dan sudah tentu dunia itu terus berkembang serta merta bersamaan dengan berkembangnya SLOT GACOR sebagai website number #1 yang pernah ada dan tidak pernah mengecewakan sekalipun. Dengan banyaknya member yang sudah mempercayakan untuk terus menghasilkan uang bersama dengan SLOT GACOR pastinya mereka sudah percaya untuk bermain Game online bersama dengan kami dengan banyaknya testimoni yang sudah membuktikan betapa seringnya member mendapatkan jackpot besar yang bisa mencapai ratusan juta rupiah. Best online Game website that give you more money everyday, itu lah slogan yang tepat untuk bermain bersama SLOT GACOR yang sudah pasti menang setiap harinya dan bisa menjadikan bandar ini sebagai patokan untuk mendapatkan penghasilan tambahan yang efisien dan juga sesuatu hal yang fix setiap hari nya. Kami juga mendapatkan julukan sebagai Number #1 website bocor yang berarti terus memberikan member uang asli dan jackpot setiap hari nya, tidak lupa bocor itu juga bisa diartikan dalam bentuk berbagi promosi untuk para official member yang terus setia bermain bersama dengan kami. Berbagai provider Game terus bertambah banyak setiap harinya dan terus melakukan support untuk membuat para official member terus bisa menang dan terus maxwin dalam bentuk apapun maka itu langsung untuk feel free to try yourself, play with SLOT GACOR now or never !
  • Topics

×
×
  • Create New...

Important Information

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