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.

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

Featured Replies

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!

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.

Why? It looks like you're trying to add a right-click action to your block. That is not the way to do it.

Edited by Leviathan143

  • Author

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...

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.