Solved Infinite Chests

Discussion in 'Plugin Development' started by Markyroson, Jun 27, 2015.

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

    Markyroson

    How would I go about creating an infinite chest as in the server owner would initially set a chest up with certain items and then it would have an infinite number of them. I know how to do that with dispensers and hoppers but tried the same process with chests and it did not work.
     
    mine-care likes this.
  2. Offline

    mine-care

    @Markyroson If i understood correctly you want to have chests with infinite resources in them.
    to do that, on InventoryOpenEvent if it is one of the set chests, cancel it and open a custom inventory that is full of the resource(s) set :- )
     
    Markyroson likes this.
  3. Offline

    Markyroson

    you understood correctly and thank you :)
     
  4. Offline

    mine-care

    Markyroson likes this.
  5. Offline

    Markyroson

    I setup a 'dumbie' project to try this out. I keep getting a null exception on line 19 (the line checking if the chest has the name "Infinite Chest". The thing is is that that is even happening when the chest I interact with IS an Infinite Chest so I threw in an else clause to see if that would help and it always just tells the player FAILED. To check that the code was not totally broken I removed the checks to see if it was the right type of chest and it worked fine.

    Main class (open)

    Code:
    public class Main extends JavaPlugin implements Listener{
        public void onEnable() {
            getServer().getPluginManager().registerEvents(
                      this, this);
        }
        @EventHandler
        public void onInventoryOpen(PlayerInteractEvent event) {
            Player player = (Player) event.getPlayer();
            if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                System.out.println("RIGHT CLICK AIR"); //shows up
                if(event.getItem().equals("Infinite Chest")) { //problem line
                    event.setCancelled(true);
                    player.closeInventory();
                    //open inventory
                    player.openInventory(ChestInv.chest);
                } else {
                    player.sendMessage("FAILED!");
                }
            }
        }
    }
    

    ChestInv Class Referenced in Main (open)

    Code:
    package me.Markyroson.chest;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    public class ChestInv {
        public static Inventory chest = Bukkit.createInventory(null, 9, "Infinite Chest2");
        static {
             chest.setItem(0, new ItemStack(Material.DIRT, 1));
             chest.setItem(2, new ItemStack(Material.DARK_OAK_STAIRS, 1));
             chest.setItem(8, new ItemStack(Material.GOLD_BLOCK, 1));
        }
    }
    


    Any ideas?
     
  6. Offline

    Zombie_Striker

    @Markyroson
    What are you even trying to do with that line? Let me explain what your doing on that line

    if(event.getItem().equals("Infinite Chest")) { //problem line
    IF the ITEM is the same as A STRING, then do something

    An Item is not a string, so it will always return false.
     
  7. because there is no item. You need to get the block. Use event.getClickedBlock()
    Next check if the block is an instance of Chest. Then cast to chest. Then use chest.getBlockInventory().getTitle().equals
     
  8. Offline

    Markyroson

    I did not realize that but seems pretty stupid now ;-) Thank you for pointing that out. :)

    Updated code:

    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = (Player) event.getPlayer();
    
            if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
                return;
            }
    
            Block block = (Block) event.getClickedBlock();
    
            if (!block.getType().equals(Material.CHEST)) {
                return;
            }
          
            // The method getItemMeta() is undefined for the type Block
            if (!block.getItemMeta().getDisplayName().equals("Infinite Chest")) {
                return;
            }
    
            event.setCancelled(true);
            player.sendMessage("Woo! It works! :)");
            ChestInv.getChestInv(player);
        }
    I tried getBlockInventory but it is also not defined.
     
  9. Because you didn't do what I told you.
    Code:
    Chest chest = (Chest) block.getState();
    if (chest.getBlockInventory()....
     
  10. Offline

    Markyroson

    Thank you for your help, it works now :)
     
Thread Status:
Not open for further replies.

Share This Page