Getting the BlockType from the blocks id


I’ve got another problem which I can’t solve…

I want to get the BlockType from the block id in order to place a certain block. I found out (by checking what someBlock.getType().getId() returns) that the id is something to “minecraft:dirt” for a dirt block, but it does not work like that.

    String blockId = "minecraft:dirt";   
    if (game.getRegistry().getType(BlockType.class, blockId).isPresent())
        BlockType type = game.getRegistry().getType(BlockType.class, blockId).get();

It doesn’t matter which value I enter for blockId, game.getRegistry().getType(BlockType.class, blockId).isPresent() will always return false which means that the block id I entered doesn’t exist.

So what am I doing wrong and what is the correct format of block ids? I’d really appreciate some help ;D

why use a string to get the block type, just do BlockType dirt = BlockTypes.DIRT;
Unless you are trying to do something different

1 Like

Hm well, actually I only have the String blockId which I get from a command argument. I got

String blockId="dirt";

which I want to turn into something like

BlockType type = BlockTypes.DIRT;

Is there any (good) approach to do this? The only one i could think of is my code above (which doesn’t work).

check if args equals something like “dirt” maybe then in that if block set BlockType blockId = BlockTypes.DIRT.
One question how are you creating the command a CommandCallable or CommandSpec

1 Like

That would be possible, but not such a great solution since it should be possible to use any block, not just dirt. In that case I’d have to check quite a number of block types ^^

I’m using CommandSpec

 CommandSpec myCommandSpec = CommandSpec.builder()
                .executor(new MyCommand())

            game.getCommandDispatcher().register(this, myCommandSpec, "set", "s");

Have you considered GenericArguments.catalogedType(Texts.of("block_name"), game, BlockType.class) instead? I figure that should do all the work for you.


what saladoc said would most likely work.

1 Like

It still won’t solve the underlying question which bugs me a little. In theory using the GameRegistry should work just as well. I guess someone more familiar with the implementation details needs to take a look at this…

1 Like

I see. If it is not in the game registry yet, tough luck. You need to wait until it is implemented (and maybe go check on Github if it is being worked on).

1 Like

Thank you! I had to solve the problem like @Azewilous suggested and set the type manually:

 String block_name; // from the commands arguments
 BlockType type = null;
       case "dirt": type = BlockTypes.DIRT;
       case "sometype": type = BlockTypes.SOMETYPE;
       // added most important block types

Another problem concerning the PlayerInteractBlockEvent came up (which I don’t want to open a new thread for). Something should be done when a player clicks the left / right mouse button (or whatever is configured as ATTACK and USE). The USE interaction type is being fired twice, the first one takes the actual block the player right clicked on (for example a grass block) and the second one is always bedrock [EDIT: It’s actually not ALWAYS bedrock… it’s only bedrock if there is bedrock in the way of the mouse pointer, below the other blocks. If there is no bedrock in the mouse pointers way, then it’s only fired once]. I guess it shouldn’t be like that, or am I doing something wrong here?

public void onInteract(PlayerInteractBlockEvent e) 
        // doStuff
        // this one works just fine
    } else if (e.getInteractionType().equals(EntityInteractionTypes.USE))
        // doStuff
        // this one gets fired twice

Forge issue for double event interaction

1 Like

no problem, I’m glad I could help