1.16.5 How Do I Save and Load BlockData from a YML file

Discussion in 'Spigot Plugin Development' started by JusticeKing, Jul 12, 2021.

  1. I am trying to save and load blockdata from a yml file. When I try to convert the string back into BlockData, I get this error.
    Code (Text):
    null
    org.bukkit.command.CommandException: Unhandled exception executing command 'bv' in plugin BuildingVillagers v1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:826) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.PlayerConnection.handleCommand(PlayerConnection.java:2185) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.PlayerConnection.c(PlayerConnection.java:2000) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:1953) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.PacketPlayInChat.a(PacketPlayInChat.java:49) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.PacketPlayInChat.a(PacketPlayInChat.java:7) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda$ensureMainThread$1(PlayerConnectionUtils.java:35) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:1266) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:1259) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.MinecraftServer.sleepForTick(MinecraftServer.java:1220) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:1134) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:291) ~[patched_1.16.5.jar:git-Paper-778]
            at java.lang.Thread.run(Thread.java:831) [?:?]
    Caused by: java.lang.ClassCastException: class java.lang.String cannot be cast to class org.bukkit.block.data.BlockData (java.lang.String is in module java.base of loader 'bootstrap'; org.bukkit.block.data.BlockData is in unnamed module of loader 'app')
            at com.JusticeKing.BVillagers.Main.LoadBuildings(Main.java:169) ~[?:?]
            at com.JusticeKing.BVillagers.Main.onCommand(Main.java:426) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.16.5.jar:git-Paper-778]
            ... 19 more
    The problem is, I tried saving the BlockData without converting it to a string first, but that gave another error.
    This is the code I am currently using:
    Code (Text):
    block.set("Data", b.blocks[i].data.getAsString());
    Here is the block in the yml file:
    Code (Text):
    '107':
        Type: OAK_STAIRS
        Data: CraftBlockData{minecraft:oak_stairs[facing=north,half=bottom,shape=straight,waterlogged=false]}
    If you know a way to fix this error or know a better way to save the BlockData, please tell.
     
  2. Why do you wanna save the block data?
     
  3. It's for a plugin that I am working on. I need to save the blockdata so I can access it later.
     
  4. Code (Text):
    java.lang.IllegalArgumentException: Could not parse data: CraftBlockData{minecraft:oak_planks}
            at org.bukkit.craftbukkit.v1_16_R3.block.data.CraftBlockData.createNewData(CraftBlockData.java:532) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.craftbukkit.v1_16_R3.block.data.CraftBlockData.lambda$newData$1(CraftBlockData.java:507) ~[patched_1.16.5.jar:git-Paper-778]
            at java.util.HashMap.computeIfAbsent(HashMap.java:1224) ~[?:?]
            at org.bukkit.craftbukkit.v1_16_R3.block.data.CraftBlockData.newData(CraftBlockData.java:507) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.createBlockData(CraftServer.java:2236) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.createBlockData(CraftServer.java:2229) ~[patched_1.16.5.jar:git-Paper-778]
            at com.JusticeKing.BVillagers.Main.LoadBuildings(Main.java:170) ~[?:?]
            at com.JusticeKing.BVillagers.Main.onEnable(Main.java:76) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:370) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:500) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugin(CraftServer.java:518) ~[patched_1.16.5.jar:git-Paper-778]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugins(CraftServer.java:432) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.MinecraftServer.loadWorld(MinecraftServer.java:594) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.DedicatedServer.init(DedicatedServer.java:298) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:1069) ~[patched_1.16.5.jar:git-Paper-778]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:291) ~[patched_1.16.5.jar:git-Paper-778]
            at java.lang.Thread.run(Thread.java:831) [?:?]
    Caused by: com.mojang.brigadier.exceptions.CommandSyntaxException: Unknown block type 'minecraft:' at position 0: <--[HERE]
    When I save the blockdata into the file, I use the getAsString method. The output is something like this: CraftBlockData{minecraft:eek:ak_planks}

    Do I have to change the string? I also checked the javadocs, but it doesn't explain the format the data string needs to be in.