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

OK so I'm either being incredibly thick or just plain blind but I can't for the life of me figure out how to call the stone variants when checking the item in my player's hand correctly. getStateFromMeta is deprecated (and only returned the wrong one anyway so that's useless...) so I was wondering if anyone can help me out?

Hi !

 

You compare the blockstate with Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.STONE). Here I put EnumType.STONE, other variants work the same way.

  • Author

Thanks for the detailed reply Koward but I have already tried that for several hours now.

 

Here's a snippet of my code:

 

Item item = heldItem.getItem();

if (item == Item.getItemFromBlock(Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE)))

 

The error message states it needs a block and this code returns a BlockState which is incompatible.

 

You see my issue?

I'm sorry, I read your post too fast.

 

Use EntityPlayer : public ItemStack getHeldItemMainhand() (or Offhand if you want).

You get an ItemStack, which has a metadata value. Use ItemStack : public int getMetadata(), and compare it with the metadata of one of the BlockStone.EnumType you want (getMetadata() too).

 

This way, no deprecation at all, you're as ready as possible for the future.

Hi

 

@SwordKorn you can do it the way you were trying, it's just a bit more involved.

    ItemStack heldItem = entityPlayer.getHeldItemMainhand();
    IBlockState iBlockStateAndesite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE);
    Item itemStone = Item.getItemFromBlock(Blocks.STONE);
    ItemStack itemStackAndesite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateAndesite));  // see Block.getItem()
    boolean heldItemIsAndesite = ItemStack.areItemsEqual(heldItem, itemStackAndesite);

 

I think Koward's suggestion is probably clearer.

 

-TGG

 

  • Author

Both excellent suggestions and I tried them both.

 

Sad to report it doesn't work though so I'll expand on my code for reference:

    @Override
    public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
        if (heldItem == null) {
            return false;
        }else{
            if (!world.isRemote) {
                if (world.getBlockState(pos) == BlockReg.tableBlock0.getDefaultState()) {
                    Item item = heldItem.getItem();

                    if (item == Item.getItemFromBlock(Blocks.COBBLESTONE)) {
                        world.setBlockState(pos, BlockReg.tableBlock1.getDefaultState());
                    }

                    if (item == Item.getItemFromBlock(Blocks.STONE)) {
                        world.setBlockState(pos, BlockReg.tableBlock2.getDefaultState());
                        System.out.println("Boolean is FALSE" + itemIsAndesite + itemIsGranite + itemIsDiorite);
                    }

                    if (item == Item.getItemFromBlock(Blocks.STONEBRICK)) {
                        world.setBlockState(pos, BlockReg.tableBlock3.getDefaultState());
                    }

                    /*if (itemIsAndesite) {
                        heldItem = andesite;
                        world.setBlockState(pos, BlockReg.tableBlock4.getDefaultState());
                        System.out.println(heldItem);
                    }

                    if (itemIsDiorite) {
                        world.setBlockState(pos, BlockReg.tableBlock5.getDefaultState());
                        System.out.println("Boolean is TRUE");
                    }

                    if (itemIsGranite) {
                        world.setBlockState(pos, BlockReg.tableBlock6.getDefaultState());
                        System.out.println("Boolean is TRUE");
                    }*/
                }
            }
            return true;
        }
    }

You are not comparing metadata from the stacks so what you are doing is hey is this a ston block which will be true for diorite andesite granite and smooth stone.

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

Well no, I'm not anymore. I took TheGreyGhost's code advice and implemented that to no avail so I removed that block of code.

 

Sufficed to say; when I was using that code the boolean states never switched from false defaulting to the main STONE conditional.

  • Author

Oh sorry diesie... I will. Let me rewrite it and I'll modify this post when I've reimplemented everything

 

EDIT

    @Override
    public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
        if (heldItem == null) {
            return false;
        }else{
            if (!world.isRemote) {
                if (world.getBlockState(pos) == BlockReg.tableBlock0.getDefaultState()) {
                    Item item = heldItem.getItem();
                    heldItem = player.getHeldItemMainhand();

                    IBlockState iBlockStateAndesite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE);
                    IBlockState iBlockStateGranite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.GRANITE);
                    IBlockState iBlockStateDiorite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.DIORITE);

                    Item itemStone = Item.getItemFromBlock(Blocks.STONE);

                    ItemStack andesite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateAndesite));
                    ItemStack granite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateGranite));
                    ItemStack diorite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateDiorite));

                    boolean itemIsAndesite = ItemStack.areItemsEqual(heldItem, andesite);
                    boolean itemIsGranite = ItemStack.areItemsEqual(heldItem, granite);
                    boolean itemIsDiorite = ItemStack.areItemsEqual(heldItem, diorite);

                    if (item == Item.getItemFromBlock(Blocks.COBBLESTONE)) {
                        world.setBlockState(pos, BlockReg.tableBlock1.getDefaultState());
                    }

                    if (item == Item.getItemFromBlock(Blocks.STONE)) {
                        world.setBlockState(pos, BlockReg.tableBlock2.getDefaultState());
                        System.out.println(itemIsAndesite + " " + itemIsGranite + " " + itemIsDiorite);
                    }

                    if (item == Item.getItemFromBlock(Blocks.STONEBRICK)) {
                        world.setBlockState(pos, BlockReg.tableBlock3.getDefaultState());
                    }

                    if (itemIsAndesite) {
                        world.setBlockState(pos, BlockReg.tableBlock4.getDefaultState());
                        System.out.println("Boolean is TRUE");
                    }

                    if (itemIsDiorite) {
                        world.setBlockState(pos, BlockReg.tableBlock5.getDefaultState());
                        System.out.println("Boolean is TRUE");
                    }

                    if (itemIsGranite) {
                        world.setBlockState(pos, BlockReg.tableBlock6.getDefaultState());
                        System.out.println("Boolean is TRUE");
                    }
                }
            }
            return true;
        }
    }

  • Author

And I apologise for the messy over use of conditionals. Just want to get it working before I try clean up with a case and select

  • Author

I just tried that and it came up with an interesting behaviour!

 

All the other block types didn't work at all and normal Stone called them all so that was fun!

 

Trying to work around that as I speak.

  • Author
            if (!world.isRemote) {
                if (world.getBlockState(pos) == BlockReg.tableBlock0.getDefaultState()) {
                    Item item = heldItem.getItem();

                    IBlockState iBlockStateStone = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.STONE);
                    IBlockState iBlockStateAndesite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE);
                    IBlockState iBlockStateGranite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.GRANITE);
                    IBlockState iBlockStateDiorite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.DIORITE);

                    Item itemStone = Item.getItemFromBlock(Blocks.STONE);

                    ItemStack stone = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateStone));
                    ItemStack andesite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateAndesite));
                    ItemStack granite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateGranite));
                    ItemStack diorite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateDiorite));

                    boolean itemIsStone = ItemStack.areItemsEqual(heldItem, stone);
                    boolean itemIsAndesite = ItemStack.areItemsEqual(heldItem, andesite);
                    boolean itemIsGranite = ItemStack.areItemsEqual(heldItem, granite);
                    boolean itemIsDiorite = ItemStack.areItemsEqual(heldItem, diorite);

                    if (item == Item.getItemFromBlock(Blocks.COBBLESTONE)) {
                        world.setBlockState(pos, BlockReg.tableBlock1.getDefaultState());
                    }

                    if (itemIsStone) {
                        world.setBlockState(pos, BlockReg.tableBlock2.getDefaultState());
                    }

                    if (itemIsAndesite) {
                        world.setBlockState(pos, BlockReg.tableBlock4.getDefaultState());
                        System.out.println("Boolean is TRUE");
                    }

                    if (itemIsGranite) {
                        world.setBlockState(pos, BlockReg.tableBlock6.getDefaultState());
                        System.out.println("Boolean is TRUE");
                    }

                    if (itemIsDiorite) {
                        world.setBlockState(pos, BlockReg.tableBlock5.getDefaultState());
                        System.out.println("Boolean is TRUE");
                    }

                    if (item == Item.getItemFromBlock(Blocks.STONEBRICK)) {
                        world.setBlockState(pos, BlockReg.tableBlock3.getDefaultState());
                    }
                }
            }
            return true;
        }

  • Author

It doesn't. This way causes the bug I described above.

 

Using this method, only regular stone will activate ALL the booleans, and no other variant accesses their relevant boolean firing them all as false.

 

It's a weird one for sure!

  • Author

Well actually... now that I think about it...

 

Every ItemStack variant calls on itemStone which only checks for the default state of Stone in the player's hand. Therefore calling on itemStone may make them only actve when the default variant is located. In which case, I'm back to square one more or less.

  • Author

Well I have deduced that:

                    Item itemStone = Item.getItemFromBlock(Blocks.STONE);

May be the reason only the default state activates all booleans. I came to this conclusion when I looked at:

                    ItemStack stone = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateStone));
                    ItemStack andesite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateAndesite));
                    ItemStack granite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateGranite));
                    ItemStack diorite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateDiorite));

As you can see, every instance of the ItemStack calls on the default variant and as my IDE so kindly pointed out;

 

"Argument 'itemStone' might be null"

  • Author

So... OH I get you!

 

So the metadata is returning the stack size! That makes perfect sense now I think about it! Good catch Diesie!

 

Hmm... so now it comes down to a way to simply ignore that value or set it to an integer value that just fills in a value based on reading the stack size in the slot.

 

I'll do some more messing around and let you know what I come up with.

  • Author

Yes You just told me what I just said...

 

I was passing the metadata value in as the stack size

  • Author

Well that was incredibly simple once you pointed that out diesie!

 

Here's my code for people wanting to know how I made this work and I'm marking this as solved! Thanks very much everyone for all your help!

 

    @Override
    public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
        if (heldItem == null) {
            return false;
        }else{
            if (!world.isRemote) {
                if (!player.capabilities.isCreativeMode) {
                    if (world.getBlockState(pos) == BlockReg.tableBlock0.getDefaultState()) {
                        Item item = heldItem.getItem();

                        IBlockState iBlockStateStone = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.STONE);
                        IBlockState iBlockStateAndesite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE);
                        IBlockState iBlockStateGranite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.GRANITE);
                        IBlockState iBlockStateDiorite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.DIORITE);

                        Item itemStone = Item.getItemFromBlock(Blocks.STONE);

                        ItemStack stone = new ItemStack(itemStone, 1, Blocks.STONE.damageDropped(iBlockStateStone));
                        ItemStack andesite = new ItemStack(itemStone, 1, Blocks.STONE.damageDropped(iBlockStateAndesite));
                        ItemStack granite = new ItemStack(itemStone, 1, Blocks.STONE.damageDropped(iBlockStateGranite));
                        ItemStack diorite = new ItemStack(itemStone, 1, Blocks.STONE.damageDropped(iBlockStateDiorite));

                        boolean itemIsStone = ItemStack.areItemsEqual(heldItem, stone);
                        boolean itemIsAndesite = ItemStack.areItemsEqual(heldItem, andesite);
                        boolean itemIsGranite = ItemStack.areItemsEqual(heldItem, granite);
                        boolean itemIsDiorite = ItemStack.areItemsEqual(heldItem, diorite);

                        if (item == Item.getItemFromBlock(Blocks.COBBLESTONE)) {
                            world.setBlockState(pos, BlockReg.tableBlock1.getDefaultState());
                            heldItem.stackSize -= 1;
                        }

                        if (itemIsStone) {
                            world.setBlockState(pos, BlockReg.tableBlock2.getDefaultState());
                            heldItem.stackSize -= 1;
                        }

                        if (itemIsAndesite) {
                            world.setBlockState(pos, BlockReg.tableBlock4.getDefaultState());
                            heldItem.stackSize -= 1;
                            System.out.println("Boolean is TRUE");
                        }

                        if (itemIsGranite) {
                            world.setBlockState(pos, BlockReg.tableBlock6.getDefaultState());
                            heldItem.stackSize -= 1;
                            System.out.println("Boolean is TRUE");
                        }

                        if (itemIsDiorite) {
                            world.setBlockState(pos, BlockReg.tableBlock5.getDefaultState());
                            heldItem.stackSize -= 1;
                            System.out.println("Boolean is TRUE");
                        }

                        if (item == Item.getItemFromBlock(Blocks.STONEBRICK)) {
                            world.setBlockState(pos, BlockReg.tableBlock3.getDefaultState());
                            heldItem.stackSize -= 1;
                        }
                    }
                }
            }
            return true;
        }
    }

Now you can move your 4 IBlockState's to the class level, adding static and final attributes (It's really not necessary to construct them every time that method is called). Likewise the item-stacks.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

  • Author

Yeah thanks Jerry! I intended to get it working first THEN worry about tidying up after so it's on my list of to-dos!

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.