Solved Setting chest's contents

Discussion in 'Plugin Development' started by JjPwN1, May 12, 2013.

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

    JjPwN1

    Code:
                    Block blockchest = Bukkit.getWorld(pg.world).getBlockAt(getChestLocation(intchest));
                    Chest chest = (Chest) blockchest.getState();
    Code:
    chest.getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
    I am creating a little hunger games plugin, and my chest refilling method worked perfectly until I added multiple world support. Then, when using the above code, an error is returned:
    Code:
    Console    : 2013-05-12 23:00:23 [WARNING] [PwnGames] Task #109 for PwnGames v0.1 generated an exception
    Console    : java.lang.ClassCastException: org.bukkit.craftbukkit.v1_5_R3.block.CraftBlockState cannot be cast to org.bukkit.block.Chest
    Console    : at com.pwncraftpvp.PwnGames.PwnGamesPlayer$10.run(PwnGamesPlayer.java:203)
    Console    : at org.bukkit.craftbukkit.v1_5_R3.scheduler.CraftTask.run(CraftTask.java:53)
    Console    : at org.bukkit.craftbukkit.v1_5_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
    Console    : at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:513)
    Console    : at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
    Console    : at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
    Console    : at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
    My getChestLocation() code is as follows:
    Code:
        public Location getChestLocation(int chest){
            return new Location(Bukkit.getWorld(pg.world), pg.getConfig().getInt(pg.world + ".chest" + chest + ".x"), pg.getConfig().getInt(pg.world + "chest" + chest + ".y"), pg.getConfig().getInt(pg.world + "chest" + chest + ".z"));
        }
     
  2. Offline

    savagesun

    It appears that whatever block you are getting is not a chest. It would be best to use (blockchest instanceof Chest) to check before you cast.
     
  3. Offline

    JjPwN1

    Well, I added some code to try and diagnose the problem, and here's what I get:

    Screen shot 2013-05-13 at 8.36.01 PM.png

    The full code for the chest refilling method is:
    Code:
            taskID = pg.getServer().getScheduler().scheduleSyncRepeatingTask(pg, new Runnable(){
                public void run(){
                    Block blockchest = Bukkit.getWorld(pg.world).getBlockAt(getChestLocation(intchest));
                    if(blockchest instanceof Chest){
                        Chest chest = (Chest) blockchest.getState();
                        if(getChestTier(intchest) == 1){
                            int chance = 1 + (int)(Math.random() * ((8 - 1) + 1));
                            // Lots of code to set items in the chest inventory
                        }else if(getChestTier(intchest) == 2){
                            int chance = 1 + (int)(Math.random() * ((8 - 1) + 1));
                            // Lots of code to set items in the chest inventory
                        }
                        intchest++;
                    }else{
                        broadcastMessage("That is not a chest! It's a " + blockchest.getType() + "! Located: " + getChestLocation(intchest).getX() + "x, " + getChestLocation(intchest).getY() + "y, " + getChestLocation(intchest).getZ() + "z. Chest #: " + intchest);
                    }
                }
            }, 5, 5);
    The full code for getChestLocation() is:
    Code:
        public Location getChestLocation(int chest){
            return new Location(Bukkit.getWorld(pg.world), pg.getConfig().getInt(pg.world + ".chest" + chest + ".x"), pg.getConfig().getInt(pg.world + "chest" + chest + ".y"), pg.getConfig().getInt(pg.world + "chest" + chest + ".z"));
        }
    pg.world is a String which, after the lobby is ended and the voting system chooses the winning map, is set to the exact world name.
     
  4. Offline

    savagesun

    Well I imagine that the error in your code is not the refilling method. Your results suggest that your getChestLocation(...) method is fetching a block incorrectly.
     
  5. Offline

    JjPwN1

    I find it peculiar, really. Because my method that teleports a player to a plate where the game starts works completely fine, and that method (teleportToPlate()) is practically the same thing as getChestLocation():
    Code:
        public void teleportToPlate(Player player){
            player.teleport(new Location(Bukkit.getWorld(pg.world), pg.getConfig().getInt(pg.world + ".plate" + plate + ".x"), pg.getConfig().getInt(pg.world + ".plate" + plate + ".y"), pg.getConfig().getInt(pg.world + ".plate" + plate + ".z"), pg.getConfig().getInt(pg.world + ".plate" + plate + ".yaw"), pg.getConfig().getInt(pg.world + ".plate" + plate + ".pitch")));
        }
     
  6. Offline

    savagesun

    It would help more if you just posted the getChestLocation(...) method. Maybe you are loading the chest positions from the configuration file incorrectly, I don't have much experience with the yml configuration system.
     
  7. Offline

    sirrus86

    To start you're missing a period before "chest", you have it right for x but not y and z.
    Code:
    public Location getChestLocation(int chest){
        return new Location(Bukkit.getWorld(pg.world), pg.getConfig().getInt(pg.world + ".chest" + chest + ".x"), pg.getConfig().getInt(pg.world + "chest" + chest + ".y"), pg.getConfig().getInt(pg.world + "chest" + chest + ".z"));
    }
    Second, how is this structured in your config? As is it looks like this:
    Code:
    world:
        chest1:
            x: x
            y: y
            z: z
    Keep in mind that getInt() won't return null, it'll return 0 if it can't get a value (hence why it might be looking at the block at 0,0,0 repeatedly as it did in your screenshot).
     
    JjPwN1 likes this.
  8. Offline

    JjPwN1

    Yes, that's how my config is set up.

    I looked over my code again and again and even looked directly at the error but never actually noticed it. The chests now refill normally, thanks!
     
Thread Status:
Not open for further replies.

Share This Page