Best way to store BlockStates?

Hey,
I want to save blocks in my database, so
I have to use multiple columns, if i save them by using blockt traits.

Question:
How can I serialize / deserialize BlockStates?

Thanks :slightly_smiling:

BlockState is DataSerializable, so you can do something like so:

ConfigurateTranslator translator = ConfigurateTranslator.instance();
ConfigNode serialized = translator.translateData(state.toContainer());

//Presumably you could reconstruct the instance like so:
DataManger manager = Sponge.getDataManager();
Optional<BlockState> optionalDeserialized = manager.deserialize(BlockState.class, translator.translateFrom(serialized);

And ConfigNodes can be easily converted to and from Strings, I mean that is their whole purpose.

1 Like

Thanks for your help!
This code works, but I need to know, how I can create a ConfigurationNode object with my serialized string

Example (this is, how I have to use the code):

  1. Serialize

    ConfigurateTranslator translator = ConfigurateTranslator.instance();
    ConfigNode serialized = translator.translateData(state.toContainer());
    String blockState_string = serialized.getString(); // or serialized.getValue().toString();

  2. Deserialize

    DataManager manager = Sponge.getDataManager();
    Optional optionalDeserialized = manager.deserialize(BlockState.class, translator.translateFrom(blockState_string));

Problem: .translateFrom(blockState_string) will throw an error, because blockState_string is a String and I need to use a ConfigurationNode object…
How do i build one with my blockState_string?

Edit: I looked around, but could not find a solution to get a ConfigurationNode with a String…
I also want to ask, if my way to get a String with a ConfigurationNode is correct…

Thanks :slight_smile:

Pretty sure this will work:

HoconConfigurationLoader.builder().setSource(() -> new BufferedReader(new StringReader(blockState_string))).build().load()
1 Like

I’d like to remind everyone that BlockStates are CatalogTypes, so their getId() is searchable (and retrievable) from the GameRegistry. To put it simply, you wouldn’t even need to store the BlockType at that point.

1 Like

There is a problem! :frowning:

Code:

String serialized = "{ContentVersion=1, BlockType=minecraft:glass, UnsafeMeta=0}";
ConfigurateTranslator translator = ConfigurateTranslator.instance();
DataManager manager = Sponge.getDataManager();

    ConfigurationNode deserialized_node = HoconConfigurationLoader.builder().setSource(() -> new BufferedReader(new StringReader(serialized ))).build().load();
    Optional<BlockState> deserializedOptional = manager.deserialize(BlockState.class, translator.translateFrom(deserialized_node));

    if(deserializedOptional.isPresent()) {
        return deserializedOptional.get();
    } else {
        return BlockTypes.AIR.getDefaultState();
    }

Error:

[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]: java.io.IOException: configurate.typesafe.config.ConfigException$Parse: Reader: 1: Expecting close brace } or a comma, got ':' (if you intended ':' to be part of a key or string value, try enclosing the key or value in double quotes, or you may be able to rename the file .properties rather than .conf)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at ninja.leaping.configurate.loader.AbstractConfigurationLoader.load(AbstractConfigurationLoader.java:157)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at ninja.leaping.configurate.loader.ConfigurationLoader.load(ConfigurationLoader.java:42)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at me.Hitzk0pf.CaveCore.obj.tools.CaveManager.deserializeBlockState(CaveManager.java:247)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo.process(BlockInfo.java:100)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at org.spongepowered.api.command.dispatcher.SimpleDispatcher.process(SimpleDispatcher.java:331)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at org.spongepowered.common.command.SpongeCommandManager.process(SpongeCommandManager.java:252)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.command.ServerCommandManager.func_71556_a(SourceFile:83)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.network.NetHandlerPlayServer.func_147361_d(NetHandlerPlayServer.java:809)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.network.NetHandlerPlayServer.func_147354_a(NetHandlerPlayServer.java:788)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.network.play.client.C01PacketChatMessage.func_148833_a(SourceFile:37)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.network.play.client.C01PacketChatMessage.func_148833_a(SourceFile:9)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at org.spongepowered.common.network.PacketUtil.onProcessPacket(PacketUtil.java:106)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.network.PacketThreadUtil$1.onProcessPacket(SourceFile:51)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.network.PacketThreadUtil$1.run(SourceFile:13)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.util.Util.func_181617_a(SourceFile:44)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:660)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:344)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:605)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:481)
[14:31:25] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at java.lang.Thread.run(Thread.java:745)
[14:31:25] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]: Caused by: configurate.typesafe.config.ConfigException$Parse: Reader: 1: Expecting close brace } or a comma, got ':' (if you intended ':' to be part of a key or string value, try enclosing the key or value in double quotes, or you may be able to rename the file .properties rather than .conf)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseError(ConfigDocumentParser.java:201)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseError(ConfigDocumentParser.java:197)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseObject(ConfigDocumentParser.java:475)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseValue(ConfigDocumentParser.java:247)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.ConfigDocumentParser$ParseContext.parse(ConfigDocumentParser.java:580)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.ConfigDocumentParser.parse(ConfigDocumentParser.java:14)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.Parseable.rawParseValue(Parseable.java:260)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.Parseable.rawParseValue(Parseable.java:248)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.Parseable.parseValue(Parseable.java:180)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.Parseable.parseValue(Parseable.java:174)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.Parseable.parse(Parseable.java:299)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.ConfigFactory.parseReader(ConfigFactory.java:668)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at ninja.leaping.configurate.hocon.HoconConfigurationLoader.loadInternal(HoconConfigurationLoader.java:104)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at ninja.leaping.configurate.hocon.HoconConfigurationLoader.loadInternal(HoconConfigurationLoader.java:54)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at ninja.leaping.configurate.loader.AbstractConfigurationLoader.load(AbstractConfigurationLoader.java:149)
[14:31:26] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    ... 21 more

CaveManager.java:247:
ConfigurationNode deserialized_node = HoconConfigurationLoader.builder().setSource(() -> new BufferedReader(new StringReader(blockState))).build().load();

You need to put quotes around strings

Should be:

String serialized = "{ContentVersion=1, BlockType=\"minecraft:glass\", UnsafeMeta=0}";
1 Like

thanks for your answer!
I already tried it, but how do I have to do this, when I get the string from a variable? Is this correct?:
String serialized = "\"" + string_old + "\""

Full method:

public static BlockState deserializeBlockState(String blockState) throws IOException {
    ConfigurateTranslator translator = ConfigurateTranslator.instance();
    DataManager manager = Sponge.getDataManager();

    ConfigurationNode deserialized_node = HoconConfigurationLoader.builder().setSource(() -> new BufferedReader(new StringReader("\"" + blockState + "\""))).build().load();
    Optional<BlockState> deserializedOptional = manager.deserialize(BlockState.class, translator.translateFrom(deserialized_node));

    if(deserializedOptional.isPresent()) {
        return deserializedOptional.get();
    } else {
        return BlockTypes.AIR.getDefaultState();
    }

}

Error:

[15:27:39] [Server thread/INFO] [CaveCore]: {ContentVersion=1, BlockType=minecraft:glass, UnsafeMeta=0}
[15:27:39] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]: java.io.IOException: configurate.typesafe.config.ConfigException$Parse: Reader: 1: Key '"{ContentVersion=1, BlockType=minecraft:glass, UnsafeMeta=0}"' may not be followed by token: end of file
[15:27:39] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at ninja.leaping.configurate.loader.AbstractConfigurationLoader.load(AbstractConfigurationLoader.java:157)
[15:27:39] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at ninja.leaping.configurate.loader.ConfigurationLoader.load(ConfigurationLoader.java:42)
[15:27:39] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at me.Hitzk0pf.CaveCore.obj.tools.CaveManager.deserializeBlockState(CaveManager.java:247)
[15:27:39] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo.process(BlockInfo.java:100)
[15:27:39] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at org.spongepowered.api.command.dispatcher.SimpleDispatcher.process(SimpleDispatcher.java:331)
[15:27:39] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at org.spongepowered.common.command.SpongeCommandManager.process(SpongeCommandManager.java:252)
[15:27:39] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.command.ServerCommandManager.func_71556_a(SourceFile:83)
[15:27:39] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.network.NetHandlerPlayServer.func_147361_d(NetHandlerPlayServer.java:809)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.network.NetHandlerPlayServer.func_147354_a(NetHandlerPlayServer.java:788)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.network.play.client.C01PacketChatMessage.func_148833_a(SourceFile:37)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.network.play.client.C01PacketChatMessage.func_148833_a(SourceFile:9)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at org.spongepowered.common.network.PacketUtil.onProcessPacket(PacketUtil.java:106)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.network.PacketThreadUtil$1.onProcessPacket(SourceFile:51)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.network.PacketThreadUtil$1.run(SourceFile:13)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.util.Util.func_181617_a(SourceFile:44)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:660)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:344)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:605)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:481)
[15:27:40] [Server thread/INFO] [STDERR]: [me.Hitzk0pf.CaveCore.commands.Cave.BlockInfo:process:102]:   at java.lang.Thread.run(Thread.java:745)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]: Caused by: configurate.typesafe.config.ConfigException$Parse: Reader: 1: Key '"{ContentVersion=1, BlockType=minecraft:glass, UnsafeMeta=0}"' may not be followed by token: end of file
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseError(ConfigDocumentParser.java:201)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseError(ConfigDocumentParser.java:197)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseObject(ConfigDocumentParser.java:408)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.ConfigDocumentParser$ParseContext.parse(ConfigDocumentParser.java:595)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.ConfigDocumentParser.parse(ConfigDocumentParser.java:14)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.Parseable.rawParseValue(Parseable.java:260)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.Parseable.rawParseValue(Parseable.java:248)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.Parseable.parseValue(Parseable.java:180)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.Parseable.parseValue(Parseable.java:174)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.impl.Parseable.parse(Parseable.java:299)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at configurate.typesafe.config.ConfigFactory.parseReader(ConfigFactory.java:668)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at ninja.leaping.configurate.hocon.HoconConfigurationLoader.loadInternal(HoconConfigurationLoader.java:104)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at ninja.leaping.configurate.hocon.HoconConfigurationLoader.loadInternal(HoconConfigurationLoader.java:54)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    at ninja.leaping.configurate.loader.AbstractConfigurationLoader.load(AbstractConfigurationLoader.java:149)
[15:27:40] [Server thread/INFO] [STDERR]: [java.lang.Throwable:printStackTrace:634]:    ... 21 more

That won’t work because you’re wrapping the entire config object in quotes. You’ll need to change the actual string coming in to that method.
If you use the serializer and deserializer without modifying the input/output they should work fine together, so you might need to delete the serialized string and try exporting it again.

1 Like

Ok, when I serialize, I get a ConfigurationNode object, which I need to convert to a string (I have to save it in a database).
Is there something wrong in my “serialize” method?:

public static String serializeBlockState(BlockState blockState) {
        ConfigurateTranslator translator = ConfigurateTranslator.instance();
        ConfigurationNode serialized = translator.translateData(blockState.toContainer());

        return serialized.getString();
    }

This will just convert the node to a string , not in any particular format. Because you’re loading the value as HOCON you also need to serialize as HOCON.
Try this in place of serialized.getString():

StringWriter sw = new StringWriter();
try {
    HoconConfigurationLoader.builder().setSink(() -> new BufferedWriter(sw)).build().save(serialized);
} catch (IOException e) {
    e.printStackTrace();
}
return sw.toString();

If you want to squash the string down then use .setRenderOptions(ConfigRenderOptions.concise()) in the builder

Try just sending the blockState.getId() as a String and don’t deserialize but rather perform a simple thing like so:

ConfigurationNode node = ...
String stateId = node.getString("blockstateKey");
Optional<BlockState> blockState = Sponge.getRegistry().getType(BlockState.class, stateId);
if (blockState.isPresent()) {
  return blockState.get();
} else {
 // don't care what you do here, but the above will work perfectly fine IF
 // you store only the string id of the block state, don't care about the 
 // data container.
 return BlockTypes.STONE.getDefaultState();
}

Sorry for this question, but could you show me a full example please? At this time, I do not really know, how to deal with Configuration nodes…

→ I just need to know, how you create the node ConfigurationNode.

Thanks :slightly_smiling:

Did you read the docs on configurations?

Hey again,
yes I did. I think, the problem is, that I don’t store my blockStates in a file, so I can’t use node.getString("blockstateKey") (because there is no key).

(That’s what was confusing me).

I wrote a little method, that does not use ConfigurationNode to deserialize BlockState Ids.
Is there something I schould change / improve (is there “bad practise”?)

public static BlockState deserializeblockStateId(String blockStateId) throws IOException {

        Optional<BlockState> deserializedBlockState = Sponge.getRegistry().getType(BlockState.class, blockStateId);

        if (deserializedBlockState.isPresent()) {
            return deserializedBlockState.get();
        } else {
            return BlockTypes.SPONGE.getDefaultState();
        }
    }

Thanks :slight_smile:

You can shorten the code using Optional.orElse():

return Sponge.getRegistry().getType(BlockState.class, blockStateId).orElse(BlockTypes.SPONGE.getDefaultState();

1 Like