Spawning a chest with items

Discussion in 'Plugin Development' started by plisov, Apr 18, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    plisov

    Hi all,

    I've been trying to spawn a chest with items and for some reason, it stopped working. It worked for a while but then it threw me a
    Code:
    org.bukkit.craftbukkit.v1_15_R1.block.CraftBlockState cannot be cast to org.bukkit.block.Chest
    error in the console.

    Here is my method.

    Code:
        private void setupChest(double x, double y, double z) {
    
            Location chestLocation = new Location(Bukkit.getServer().getWorld(ServerManager.worldName), x + 4, y - 1, z + 3);
    
            chestLocation.getBlock().setType(Material.CHEST);
            if(chestLocation.getBlock().getState() instanceof Chest) {
                Chest chest = (Chest) chestLocation.getBlock().getState();
                Inventory inv = chest.getBlockInventory();
    
                chest.getInventory().clear();
    
                inv.addItem(new ItemStack(Material.STONE, 2));
    
                player.sendMessage(ChatColor.GREEN + "Setup chest");
            } else {
                player.sendMessage(Placeholders.errorPrefix + "Error while setting up chest");
            }
        }
    ServerManager.worldName is just a world name for a world that exists.

    I call this method with setupChest(x, y, z);

    The x, y, and z variables get doubles which are stored in a config.

    What I noticed was that the location which is specified in the method doesn't change to a chest.

    I've also tried chest.getInventory() with similar results.

    Any help is much appreciated.
     
  2. Offline

    CraftCreeper6

    @plisov
    You've imported the wrong Block.
     
  3. Offline

    plisov

    @CraftCreeper6
    I have
    Code:
    import org.bukkit.block.Chest;
    
    imported
     
  4. Offline

    CraftCreeper6

    @plisov
    And which BlockState did you import?
     
  5. Offline

    plisov

    @CraftCreeper6
    I don't have a BlockState imported. Should it be
    Code:
                Chest chest = (BlockState) chestLocation.getBlock().getState();
    instead of
    Code:
                Chest chest = (Chest) chestLocation.getBlock().getState();
    
     
  6. Offline

    CraftCreeper6

    @plisov
    No,
    List your imports please.
     
  7. Offline

    plisov

    Here are my imports
    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Chest;
    import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
    import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
     
  8. Offline

    CraftCreeper6

    @plisov
    Can you post the whole stacktrace and tell me which line is throwing the error?
     
  9. Offline

    plisov

    Here is the full stacktrace
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'claim' in plugin PlisovSkyblock v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:711) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.PlayerConnection.handleCommand(PlayerConnection.java:1657) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1497) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:918) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:911) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:895) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:828) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_15_R1.block.CraftBlockState cannot be cast to org.bukkit.block.Chest
            at me.plisov.skyblock.island.IslandMethods.setupChest(IslandMethods.java:122) ~[?:?]
            at me.plisov.skyblock.island.IslandMethods.setupIsland(IslandMethods.java:108) ~[?:?]
            at me.plisov.skyblock.island.IslandManager.setup(IslandManager.java:27) ~[?:?]
            at me.plisov.skyblock.commands.ClaimCommand.onCommand(ClaimCommand.java:25) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
            ... 17 more
    Line 27 in IslandManager is islandMethod.setupIsland()
    in
    Code:
        public void setup() {
            if(!playerManager.getPlayerConfig().getFile().exists()) {
                playerManager.setupPlayerConfig();
            }
            islandMethods.updateGlobalCoords();
            islandMethods.setupIsland();
            playerManager.setHasIsland(true);
            islandMethods.setupBorder();
            RegionUpdate.updateSpawnIsland(player);
            islandMethods.sendPlayerToIsland();
        }
    Line 108 in IslandMethods is setupChest(x, y, z); in
    Code:
        public void setupIsland() {
            double x = islandManager.getLastIslandX();
            double y = islandManager.getLastIslandY();
            double z = islandManager.getLastIslandZ();
    
            double vectMinX = x - 25;
            double vectMinY = y - 256;
            double vectMinZ = z;
    
            double vectMaxX = x + 24;
            double vectMaxY = y + 256;
            double vectMaxZ = z + 24;
    
            BlockVector3 vectorMin = BlockVector3.at(vectMinX, vectMinY, vectMinZ);
            BlockVector3 vectorMax = BlockVector3.at(vectMaxX, vectMaxY, vectMaxZ);
    
            playerConfig.set("Island.Vector.Minimum.X", vectMinX);
            playerConfig.set("Island.Vector.Minimum.Y", vectMinY);
            playerConfig.set("Island.Vector.Minimum.Z", vectMinZ);
    
            playerConfig.set("Island.Vector.Maximum.X", vectMaxX);
            playerConfig.set("Island.Vector.Maximum.Y", vectMaxY);
            playerConfig.set("Island.Vector.Maximum.Z", vectMaxZ);
    
            playerConfig.set("SpawnIsland.Vector.Minimum.X", vectMinX);
            playerConfig.set("SpawnIsland.Vector.Minimum.Y", vectMinY);
            playerConfig.set("SpawnIsland.Vector.Minimum.Z", vectMinZ);
    
            playerConfig.set("SpawnIsland.Vector.Maximum.X", vectMaxX);
            playerConfig.set("SpawnIsland.Vector.Maximum.Y", vectMaxY);
            playerConfig.set("SpawnIsland.Vector.Maximum.Z", vectMaxZ - 79);
    
            playerConfig.set("Home.X", x);
            playerConfig.set("Home.Y", y);
            playerConfig.set("Home.Z", z);
    
            playerConfig.save();
    
            globalCoords.set("LastIsland.X", x + 400);
            globalCoords.set("LastIsland.Y", y);
            globalCoords.set("LastIsland.Z", z + 400);
    
            globalCoords.save();
    
            setupChest(x, y, z);
    
            setupWorldGuardRegion(vectorMin, vectorMax, x, y, z);
    
            player.sendMessage(ChatColor.GREEN + "Setup island");
        }
    Line 122 in IslandMethods is Chest chest = (Chest) chestLocation.getBlock().getState() in the setupChest method
     
  10. Offline

    caderapee

    @plisov Did you try another version ? Because in 1.12, it works. May be they changed the enum name or the way to cast. Try to do a block#update before to cast maybe
     
Thread Status:
Not open for further replies.

Share This Page