Autorefill chest

Discussion in 'Plugin Development' started by tacos1223, Jan 31, 2016.

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

    tacos1223

    I can't quite come up with a method on how to do this. What I tried was cancelling the event if the player opened the chest in the coord specified and opened up a player gui, but that didn't seem to work. Here is my code:
    Code:
        private void chestOne(Player player)
        {
           
            Inventory chest1 = Bukkit.createInventory(null,  27, ChatColor.BLUE + "Chest One");
           
            ItemStack chest = new ItemStack(Material.DIAMOND_CHESTPLATE);       
            ItemMeta chestMeta = chest.getItemMeta();
            chestMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 3, true);
            chestMeta.addEnchant(Enchantment.DURABILITY, 3, true);
           
            ItemStack legs = new ItemStack(Material.DIAMOND_LEGGINGS);       
            ItemMeta legsMeta = legs.getItemMeta();
            legsMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 3, true);
            legsMeta.addEnchant(Enchantment.DURABILITY, 3, true);
           
            ItemStack helm = new ItemStack(Material.DIAMOND_HELMET);       
            ItemMeta helmMeta = helm.getItemMeta();
            helmMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 3, true);
            helmMeta.addEnchant(Enchantment.DURABILITY, 3, true);
           
            ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS);       
            ItemMeta bootsMeta = boots.getItemMeta();
            bootsMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 3, true);
            bootsMeta.addEnchant(Enchantment.DURABILITY, 3, true);
           
            ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);       
            ItemMeta swordMeta = sword.getItemMeta();
            swordMeta.addEnchant(Enchantment.DAMAGE_ALL, 2, true);
            swordMeta.addEnchant(Enchantment.DURABILITY, 3, true);
            swordMeta.addEnchant(Enchantment.FIRE_ASPECT, 2, true);
           
            ItemStack fish = new ItemStack(Material.COOKED_FISH);
            fish.setAmount(64);
           
            ItemStack chicken = new ItemStack(Material.COOKED_CHICKEN);
            chicken.setAmount(64);
           
            ItemStack potato = new ItemStack(Material.BAKED_POTATO);
            potato.setAmount(64);
           
            ItemStack steak = new ItemStack(Material.COOKED_BEEF);
            steak.setAmount(64);
           
            ItemStack carrot = new ItemStack(Material.GOLDEN_CARROT);
            carrot.setAmount(64);
           
            ItemStack porkchop = new ItemStack(Material.GRILLED_PORK);
            porkchop.setAmount(64);
           
            ItemStack pearls = new ItemStack(Material.ENDER_PEARL);
            pearls.setAmount(16);
           
            chest1.setItem(1, helm);
            chest1.setItem(2, chest);
            chest1.setItem(3, legs);
            chest1.setItem(4, boots);
            chest1.setItem(5, sword);
            chest1.setItem(8, fish);
            chest1.setItem(9, chicken);
            chest1.setItem(17, potato);
            chest1.setItem(18, steak);
            chest1.setItem(19, pearls);
            chest1.setItem(26, carrot);
            chest1.setItem(27, porkchop);
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
    if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK))
    {
        Block b = event.getClickedBlock();
         if (b.getType() == Material.CHEST)
         {
             int xcoord = event.getClickedBlock().getLocation().getBlockX();
             int ycoord = event.getClickedBlock().getLocation().getBlockY();
             int zcoord = event.getClickedBlock().getLocation().getBlockZ();
             player.sendMessage("x coord is: " + xcoord);
             player.sendMessage("y coord is: " + ycoord);
             player.sendMessage("z coord is: " + zcoord);
            
             if(xcoord == -1190 && ycoord == 83 && zcoord == 620)
             {
            event.setCancelled(true);
            chestOne(player);
             }
         }
        }
      }
    Any ideas?
    Thanks!
     
  2. Offline

    JTGaming2012

    I think rather than cancelling the event, you can just open the inventory a tick after the player interacted with the block.
    For example:
    Code:
    private void chestOne(Player player)
        {
     
            Inventory chest1 = Bukkit.createInventory(null,  27, ChatColor.BLUE + "Chest One");
     
            ItemStack chest = new ItemStack(Material.DIAMOND_CHESTPLATE);
            ItemMeta chestMeta = chest.getItemMeta();
            chestMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 3, true);
            chestMeta.addEnchant(Enchantment.DURABILITY, 3, true);
    
             //Rest of stuff (cba to type it all)
    
             player.openInventory(chest1); //This line will make the player open your custom        inventory
    
    }
    And then where you have put 'chestOne(player)', replace it with this:
    Code:
    new BukkitRunnable() {
                public void run() {
                    chestOne(player);
                }
            }.runTaskLater(<plugin instance>, 1l);
    And you will have to make the player variable 'final'

    So overall, it'll look like this:
    Code:
        private void chestOne(Player player)
        {
         
            Inventory chest1 = Bukkit.createInventory(null,  27, ChatColor.BLUE + "Chest One");
         
            ItemStack chest = new ItemStack(Material.DIAMOND_CHESTPLATE);    
            ItemMeta chestMeta = chest.getItemMeta();
            chestMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 3, true);
            chestMeta.addEnchant(Enchantment.DURABILITY, 3, true);
         
            ItemStack legs = new ItemStack(Material.DIAMOND_LEGGINGS);    
            ItemMeta legsMeta = legs.getItemMeta();
            legsMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 3, true);
            legsMeta.addEnchant(Enchantment.DURABILITY, 3, true);
         
            ItemStack helm = new ItemStack(Material.DIAMOND_HELMET);    
            ItemMeta helmMeta = helm.getItemMeta();
            helmMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 3, true);
            helmMeta.addEnchant(Enchantment.DURABILITY, 3, true);
         
            ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS);    
            ItemMeta bootsMeta = boots.getItemMeta();
            bootsMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 3, true);
            bootsMeta.addEnchant(Enchantment.DURABILITY, 3, true);
         
            ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);    
            ItemMeta swordMeta = sword.getItemMeta();
            swordMeta.addEnchant(Enchantment.DAMAGE_ALL, 2, true);
            swordMeta.addEnchant(Enchantment.DURABILITY, 3, true);
            swordMeta.addEnchant(Enchantment.FIRE_ASPECT, 2, true);
         
            ItemStack fish = new ItemStack(Material.COOKED_FISH);
            fish.setAmount(64);
         
            ItemStack chicken = new ItemStack(Material.COOKED_CHICKEN);
            chicken.setAmount(64);
         
            ItemStack potato = new ItemStack(Material.BAKED_POTATO);
            potato.setAmount(64);
         
            ItemStack steak = new ItemStack(Material.COOKED_BEEF);
            steak.setAmount(64);
         
            ItemStack carrot = new ItemStack(Material.GOLDEN_CARROT);
            carrot.setAmount(64);
         
            ItemStack porkchop = new ItemStack(Material.GRILLED_PORK);
            porkchop.setAmount(64);
         
            ItemStack pearls = new ItemStack(Material.ENDER_PEARL);
            pearls.setAmount(16);
         
            chest1.setItem(1, helm);
            chest1.setItem(2, chest);
            chest1.setItem(3, legs);
            chest1.setItem(4, boots);
            chest1.setItem(5, sword);
            chest1.setItem(8, fish);
            chest1.setItem(9, chicken);
            chest1.setItem(17, potato);
            chest1.setItem(18, steak);
            chest1.setItem(19, pearls);
            chest1.setItem(26, carrot);
            chest1.setItem(27, porkchop);
           
             player.openInventory(chest1);
    
        }
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event)
        {
            final Player player = event.getPlayer();
    if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK))
    {
        Block b = event.getClickedBlock();
         if (b.getType() == Material.CHEST)
         {
             int xcoord = event.getClickedBlock().getLocation().getBlockX();
             int ycoord = event.getClickedBlock().getLocation().getBlockY();
             int zcoord = event.getClickedBlock().getLocation().getBlockZ();
             player.sendMessage("x coord is: " + xcoord);
             player.sendMessage("y coord is: " + ycoord);
             player.sendMessage("z coord is: " + zcoord);
         
             if(xcoord == -1190 && ycoord == 83 && zcoord == 620)
             {
            event.setCancelled(true);
            new BukkitRunnable() {
                     public void run() {
                              chestOne(player);
                     }
             }.runTaskLater(<main instance>, 1l);
         }
        }
      }
     
    Last edited: Feb 1, 2016
    tacos1223 likes this.
  3. Offline

    boomboompower

    @tacos1223 read what JTGaming2012 Said

    Spoonfeeding be like
     
Thread Status:
Not open for further replies.

Share This Page