Jump to content

Switch statement with World#getBlockState() to compare blocks not working (Constant Expression Required)


Recommended Posts

Posted

Hey everyone,

 

So I am trying to make a switch statement to compare the block that I am looking at, to the blocks within my mod. Yet, when I try make my first case, I get Constant Expression Required error. I'm not sure how to fix this. All my blocks in my mod are declared as final variables.

Here are the two things I've tried so far:

 

switch (worldIn.getBlockState(pos).getBlock()) {
            case ModBlocks.my_mod_block :
                Utils.getLogger().info("mod block right clicked");
                break;
        }
switch (true) {
            case (worldIn.getBlockState(pos).getBlock() == ModBlocks.my_mod_block) :
                Utils.getLogger().info("mod block right clicked");
                break;
        }

ModBlocks.class

public class ModBlocks {
    public static final Block my_mod_block = new MyModBlock("my_mod_block", Material.WOOD);

    public static final Block[] BLOCKS = {
            my_mod_block
    };
}

 

Thanks!

Posted

switch statements only work on primitive types, Strings, enums & the wrapper objects for primitive types(These are special cased). Since Block does not fall into any of those categories, you can't switch on a field/method return of type Block

 

What are you actually trying to do here? Blocks have a method that is called when they are right-clicked.

Posted

Thanks, diesieben for the actual answer, exactly what I am looking for!

 

Leviathan, I want to see what block I am looking at with the item I am holding from my mod... Not trying to add right click action... (This item is a tool) I'm not that stupid.

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.