Get BlockState from id and variant

Hey,
I know, that there are things like BlockTypes, but I can’t use it in this case…

Example:
Id: minecraft:stone
variant: granite
So I want to get the BlockState of granite to place a granite block in my map by using
player.getLocation().setBlock(BLOCKSTATE)

Thanks :slight_smile:

BlockState granite = BlockTypes.STONE.getDefaultState().with(Keys.STONE_TYPE, StoneTypes.GRANITE).get();

This wouldn’t work, because I have to use the blocks id & variant

OK so you have a string instead?
If you know it’s stone, then you can get the StoneType

Optional<StoneType> optStoneType = Sponge.getRegistry().getType(StoneType.class, "granite");
if (optStoneType.isPresent()) {
    Optional<BlockType> optBlock = Sponge.getRegistry().getType(BlockType.class, "minecraft:stone");
    if (optBlock.isPresent()) {
        Optional<BlockState> optState = optBlock.get().getDefaultState().with(Keys.STONE_TYPE, optStoneType.get());
        if (optState.isPresent()) {
            BlockState blockState = optState.get();
        }
    }
}

I don’t know, that it’s stone, it could be any block…
Is there no other way? :frowning:

Right, then you can use block traits.
Block traits allow for simple strings to be given without knowing any prior information

Optional<BlockType> optBlock = Sponge.getRegistry().getType(BlockType.class, "minecraft:stone");
if (optBlock.isPresent()) {
    Optional<BlockTrait> optTrait = Sponge.getRegistry().getType(BlockTrait.class, "variant");
    if (optTrait.isPresent()) {
        Optional<BlockState> optState = optBlock.get().getDefaultState().withTrait(optTrait.get(), "granite");
        if (optState.isPresent()) {
            BlockState blockState = optState.get();
        }
    }
}
1 Like

So I tried It with following code:

Optional<BlockType> optBlock = Sponge.getRegistry().getType(BlockType.class, "minecraft:stone");
            if (optBlock.isPresent()) {
                Optional<BlockTrait> optTrait = Sponge.getRegistry().getType(BlockTrait.class, "variant");
                if (optTrait.isPresent()) {
                    Optional<BlockState> optState = optBlock.get().getDefaultState().withTrait(optTrait.get(), "granite");
                    if (optState.isPresent()) {
                        BlockState blockState = optState.get();
                         player.getLocation().setBlock(blockState);
                    }
                }
            }

And I got following error:

[19:26:05] [Server thread/ERROR] [Sponge]: Error occurred while executing command 'bi' for source EntityPlayerMP['Hitzk0pf'/1116, l='spawn', x=-1678,09, y=47,24, z=107,60]: null
java.lang.NullPointerException
        at java.util.Objects.requireNonNull(Objects.java:203) ~[?:1.8.0_72]
        at java.util.Optional.<init>(Optional.java:96) ~[?:1.8.0_72]
        at java.util.Optional.of(Optional.java:108) ~[?:1.8.0_72]
        at org.spongepowered.common.registry.SpongeGameRegistry.getRegistryModuleFor(SpongeGameRegistry.java:205) ~[SpongeGameRegistry.class:1.8.9-1732-3.1.0-BETA-1139]
        at org.spongepowered.common.registry.SpongeGameRegistry.getType(SpongeGameRegistry.java:226) ~[SpongeGameRegistry.class:1.8.9-1732-3.1.0-BETA-1139]
        at me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo.process(BlockInfo.java:81) ~[BlockInfo.class:?]
        at org.spongepowered.api.command.dispatcher.SimpleDispatcher.process(SimpleDispatcher.java:331) ~[SimpleDispatcher.class:1.8.9-1732-3.1.0-BETA-1139]
        at org.spongepowered.common.command.SpongeCommandManager.process(SpongeCommandManager.java:252) [SpongeCommandManager.class:1.8.9-1732-3.1.0-BETA-1139]
        at net.minecraft.command.ServerCommandManager.func_71556_a(SourceFile:83) [bd.class:?]
        at net.minecraft.network.NetHandlerPlayServer.func_147361_d(NetHandlerPlayServer.java:809) [lm.class:?]
        at net.minecraft.network.NetHandlerPlayServer.func_147354_a(NetHandlerPlayServer.java:788) [lm.class:?]
        at net.minecraft.network.play.client.C01PacketChatMessage.func_148833_a(SourceFile:37) [ie.class:?]
        at net.minecraft.network.play.client.C01PacketChatMessage.func_148833_a(SourceFile:9) [ie.class:?]
        at org.spongepowered.common.network.PacketUtil.onProcessPacket(PacketUtil.java:106) [PacketUtil.class:1.8.9-1732-3.1.0-BETA-1139]
        at net.minecraft.network.PacketThreadUtil$1.onProcessPacket(SourceFile:51) [fh$1.class:?]
        at net.minecraft.network.PacketThreadUtil$1.run(SourceFile:13) [fh$1.class:?]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_72]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_72]
        at net.minecraft.util.Util.func_181617_a(SourceFile:44) [g.class:?]
        at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:660) [MinecraftServer.class:?]
        at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:344) [ko.class:?]
        at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:605) [MinecraftServer.class:?]
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:481) [MinecraftServer.class:?]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_72]

Line 81:
Optional<BlockTrait> optTrait = Sponge.getRegistry().getType(BlockTrait.class, "variant");

OK try this:

Optional<BlockType> optBlock = Sponge.getRegistry().getType(BlockType.class, "minecraft:stone");
if (optBlock.isPresent()) {
    BlockState defaultState = optBlock.get().getDefaultState();
    Optional<BlockTrait<?>> optTrait = defaultState.getTrait("variant");
    if (optTrait.isPresent()) {
        Optional<BlockState> optState = defaultState.withTrait(optTrait.get(), "granite");
        if (optState.isPresent()) {
            BlockState blockState = optState.get();
        }
    }
}
2 Likes

I can’t mark it as solution, but now my code works. Thanks! :slightly_smiling:

In the future, you can post your questions/issues in the Plugin Development category. This will allow you to mark solutions as solved. :wink:

1 Like

Moved accordingly and marked @simon816’s response as the solution :slight_smile: