Solved Spawning Chest with items

Discussion in 'Plugin Development' started by 1Camer0471, Dec 15, 2014.

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

    1Camer0471

    Hello, I am writing a plugin that spawns a chest after a countdown, the countdown works, and the chest spawns, but the items I want in it do not spawn in the chest!
    Here is the code:
    Code (open)

    public boolean onCommand(CommandSender sender, Command cmd, String label,
    String[] args) {
    final Player player = (Player) sender;
    final World world = player.getWorld();


    if (cmd.getName().equalsIgnoreCase("timer")) {

    player.sendMessage("DEFENCE");
    player.sendMessage("By Camer047");

    if (args.length == 1 && args[0].equalsIgnoreCase("start")) {

    this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    public void run() {
    if (time != -1) {
    if (time != 0) {
    Bukkit.getServer().broadcastMessage("" + time);
    time --;
    } else {

    //TEMP
    Bukkit.broadcastMessage("Chest " + chestnumber + " Spawned");






    //Create Random function
    Random random = new Random();
    int spawncords = 4;
    final int spawncordsfinal = random.nextInt(spawncords);
    time = getConfig().getInt("timer");


    if (spawncordsfinal == 1) {

    chestnumber = 1;

    //Set as block: Chest
    Block block = world.getBlockAt(getConfig().getInt("Chest1X"), getConfig().getInt("Chest1Y"), getConfig().getInt("Chest1Z"));

    block.setType(Material.CHEST);
    Chest chest = (Chest) block;
    Inventory inv = chest.getInventory();

    //Declare Variable "Test Item"
    ItemStack testItem1 = new ItemStack(Material.DIAMOND_SWORD, 1);
    ItemMeta meta = testItem1.getItemMeta();
    //Set Item Name
    meta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "This is Chest one!");


    //Set the item lore
    ArrayList <String> lore = new ArrayList <String>();
    lore.add(ChatColor.GREEN + "" + player.getName() + "'s " + ChatColor.BLUE + "Diamond Sword");
    lore.add(ChatColor.WHITE + "Atleast this one works :/");
    meta.setLore(lore);
    testItem1.setItemMeta(meta);

    inv.addItem(testItem1);



    }


    Here is the Error:
    Error (open)

    [18:25:01 WARN]: [Defence] Task #18 for Defence v0.01 generated an exception
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R3.block.CraftBlock ca
    nnot be cast to org.bukkit.block.Chest
    at me.Camer047.DefenceClass$1.run(DefenceClass.java:106) ~[?:?]
    at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java
    :53) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:345) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688b
    d4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    00) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
     
  2. Offline

    Skionz

    @1Camer0471 Cast the Block's state to a Chest, not the block itself.
     
    1Camer0471 likes this.
  3. Offline

    1Camer0471

    I dont understand, could you elaborate?
     
  4. Offline

    mythbusterma

    1Camer0471 and Skionz like this.
  5. Offline

    1Camer0471

  6. Offline

    Skionz

    @1Camer0471 Add a debug line checking if the block state is an instance of Chest.
     
    1Camer0471 likes this.
  7. Offline

    1Camer0471

    Could I have an example? Sorry, I'm a bit new to Java

    I put: if (block.getState() instanceof Chest){
    //code here
    }
    is this good?
     
    Last edited by a moderator: Dec 16, 2014
  8. Offline

    TheCodingCat

    Code:
    if (<Block>.getState() instanceof Chest)
     
    1Camer0471 likes this.
  9. Offline

    1Camer0471

    Yea I said that, it was awaiting moderation, so you couldent see it, but thanks anyways

    Hmm, i pout in the debug line, I am not getting ay errors, but the chest is not spawning :/
    Code:
    Code (open)

    Block block = world.getBlockAt(getConfig().getInt("Chest1X"), getConfig().getInt("Chest1Y"), getConfig().getInt("Chest1Z"));
    if (block.getState() instanceof Chest) {
    block.setType(Material.CHEST);
    Chest chest = (Chest) block;
    Inventory inv = chest.getInventory();


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  10. Offline

    JordyPwner

    try with a little delay? Had same issue with enderchest :p added delay and its fixed
     
    1Camer0471 likes this.
  11. Offline

    1Camer0471

    So add in a an Async delayed task for the debug line?
     
  12. Offline

    JordyPwner

    Sorry totally wrong can you post your code in java syntax? its not readable without ;3 (readable but bad)
     
    1Camer0471 likes this.
  13. Offline

    SuperOriginal

    @1Camer0471 You're checking if it's a chest.... Before you set it to a chest. :confused:
     
    1Camer0471 likes this.
  14. Offline

    1Camer0471

    Code:
    Block block = world.getBlockAt(getConfig().getInt("Chest1X"), getConfig().getInt("Chest1Y"), getConfig().getInt("Chest1Z"));
                                      if (block.getState() instanceof Chest) {
                                       block.setType(Material.CHEST);
                                       Chest chest = (Chest) block;
                                       Inventory inv = chest.getInventory();
    How could I check if Block instanceof chest before I define variable block?
     
  15. Offline

    mythbusterma

    @1Camer0471

    You could, oh I don't know...check its Material? Think before you ask.
     
    1Camer0471 likes this.
  16. Offline

    1Camer0471

    I thought that I was supposed to put if block.getState() instnaceof Chest above Block block = ...
    what I'm asking is: How can I do this?
    should I use:
    Code:
    Block block = world.getBlockAt(getConfig().getInt("Chest1X"), getConfig().getInt("Chest1Y"), getConfig().getInt("Chest1Z"));
                                      if (block.getState() instanceof Chest) {
                                       block.setType(Material.CHEST);
    if (block.getType() == Material.CHEST){
                                       Chest chest = (Chest) block;
                                       Inventory inv = chest.getInventory();
     
  17. Offline

    mythbusterma

    @1Camer0471

    Wait, you've already checked if it was an instance of Chest, and then needlessly set the type of the block to CHEST, then you perform an illegal cast from block to chest.

    Try and think about this for a second.
     
    Last edited: Dec 16, 2014
    1Camer0471 likes this.
  18. Offline

    1Camer0471

    Do you mean illegal cast? because thats what people told me to do, and its not needless to set the type of the block to chest, if I didn't do that then there would be no block.
     
  19. Offline

    mythbusterma

    @1Camer0471

    I did. You put the code to change it to a chest inside of code that checks if it's a chest. It will never have an effect.
     
    1Camer0471 likes this.
  20. Offline

    1Camer0471

    OMG IM SO STUPID, I DINT CATCH THAT IM SO SORRY

    But how is the cast illegal?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  21. Offline

    mythbusterma

    @1Camer0471

    A Block can never be a Chest, as Chest inherits from BlockState, not Block.
     
    1Camer0471 likes this.
  22. Offline

    1Camer0471

    So what should i do? I tried Chest chest = block.getState(); but it gave me an error.
     
  23. Offline

    Skionz

    @1Camer0471 Because getState returns a BlockState not a Chest. Cast it.
     
    1Camer0471 likes this.
  24. Offline

    1Camer0471

    like:
    Chest chest = (Chest) block.getState(); ?

    THANKS EVERYONE! ITS FIXED! TO ANYONE WITH THE SAME PROBLEM:
    YOU HAVE TO CAST WHEN GETTING THE BLOCK STATE:
    Chest chest = (Chest) block.getState();

    TO EVERYONE THAT HELPED ME:
    Enjoy your free likes! :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
Thread Status:
Not open for further replies.

Share This Page