PLEASE HELP

Discussion in 'Plugin Development' started by nick6899, Feb 27, 2015.

Thread Status:
Not open for further replies.
  1. I'm doing a Op Prison Server and im doing a few small projects but i can not seem to get this, im trying to add random items to a chest A.K.A treasure. but it is very not adding it to the chest. please help



    This Is The Code...

    Code:
    package me.nick6899.Prison;
    
    import java.util.Arrays;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.event.inventory.InventoryOpenEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Mining implements Listener {
          public Main plugin;
         
          public Mining(Main plugin) {
            this.plugin = plugin;
          }
       
           public static Inventory myInventory = Bukkit.createInventory(null, 27, "{Cyber Crystals} Treasure!");
            static {
                }
         
         
        @EventHandler
        public void OnBlockMine(BlockBreakEvent e) {
            Player p = (Player) e.getPlayer();
           
            Random random = new Random();
            int randomNumber = random.nextInt(5 - 1) + 1;
           
           
            ItemStack Chest = new ItemStack(Material.CHEST, 1);
            ItemMeta Chestmeta = Chest.getItemMeta();
            Chestmeta.setDisplayName(ChatColor.BOLD.toString() + ChatColor.AQUA + "{Cyber Crystals} " + ChatColor.GOLD + "Mining Treasure");
               Chestmeta.setLore(Arrays.asList(ChatColor.UNDERLINE.toString() + ChatColor.ITALIC + ChatColor.GRAY + "Right Click To Recieve Treasure!"));
               Chest.setItemMeta(Chestmeta);
           
           
            if(randomNumber == 3) {
                e.getBlock().getWorld().dropItem(e.getBlock().getLocation(), Chest);
                p.sendMessage(ChatColor.BOLD.toString() + ChatColor.AQUA + "{Cyber Crystals} " + ChatColor.GRAY + "You Have Found A Mining Treasure, Open It To Recieve Amazing Treasure!");
            }
        }
       
       
        @EventHandler
        public void onPlaceOfChest(BlockPlaceEvent e) {
            Player p = (Player) e.getPlayer();
            Block block = (Block) e.getBlock();
           
            if(block.getType().equals(Material.CHEST)) {
                e.setCancelled(true);
            }
        }
       
       
        @EventHandler
        public void rightClickToRecieveTreasure(PlayerInteractEvent e) {
            Player p = (Player) e.getPlayer();
           
        if(e.getAction() == Action.RIGHT_CLICK_AIR) {
            if(p.getItemInHand().getType().equals(Material.CHEST)) {
                if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.BOLD.toString() + ChatColor.AQUA + "{Cyber Crystals} " + ChatColor.GOLD + "Mining Treasure")){
                    p.openInventory(myInventory);
                    ItemStack is = p.getItemInHand();
                    if(is.getAmount() > 1)
                        is.setAmount(is.getAmount() - 1);
                    else p.setItemInHand(null);
                }
            }
        }else if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if(p.getItemInHand().getType().equals(Material.CHEST)) {
                if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.BOLD.toString() + ChatColor.AQUA + "{Cyber Crystals} " + ChatColor.GOLD + "Mining Treasure")){
                    p.openInventory(myInventory);
                    ItemStack is = p.getItemInHand();
                    if(is.getAmount() > 1)
                        is.setAmount(is.getAmount() - 1);
                    else p.setItemInHand(null);
                   
                }
            }
        }
    }
       
       
        @EventHandler
        public void OnInventoryClose(InventoryCloseEvent e) {
            Inventory inv = (Inventory) e.getInventory();
           
            if(inv.getName().equals("{Cyber Crystals} Treasure!")) {
                inv.clear();
            }
        }
       
        @EventHandler
        public void OnInventoryOpen(InventoryOpenEvent e) {
            Inventory inv = (Inventory) e.getInventory();
           
            Random random = new Random();
            int Chance = random.nextInt(5 - 1) + 1;
            int ItemAmount = random.nextInt(64 - 1) + 1;
           
            if(inv.getName().contains("{Cyber Crystals} Treasure!")) {
               
                    if (Chance == 1) {
                        ItemStack GoldBlock = new ItemStack(Material.GOLD_BLOCK, ItemAmount);
                        ItemMeta GoldBlockmeta = GoldBlock.getItemMeta();
                        //nick insert meta value here if needed
                        GoldBlock.setItemMeta(GoldBlockmeta);
                        myInventory.addItem(GoldBlock);
                   
                }else if (Chance == 2) {
                   
                   
                   
                }else if(Chance == 3) {
                   
                   
                   
                }else if(Chance == 4) {
                   
                   
                   
                }
            }
        }
    }
     
  2. Offline

    mine-care

    a emty useless static block?
    an unnessesary casting? (its like saying i have an object that you told me is an apple and then i cast it to apple? what is da point?)
    Same as above here ^
    And here^
    few nice npe's can be thrown from here ^ 1. you dont know if player's item in hand is null, 2. you dont know if it has itemmeta 3. you dont know if it has displayname;

    OMG! again! ^
    and again!!!!! ^ (line 108)

    After a few tips about your code, lets move in to the main problem here.
    Are you trying to create a custom inventory and when chest is right clicked the inverntory opens? if yes it doesnt work because interaction event is called firstly to all plugins and then to bukkit, that means that you do stuff with it and when you open the inventory, in a few miliseconds bukkit opens the chest inventory over yours so it doesnt show. Add a delay on player#openInventory(inventory); like 1 tick or so.
    Also you have a static that is a clear abuse. You dont need it there, it will only cause you problems. Just remove it,
    Why is the inventory global? should it be the same for all players or individual?
    is this supose to block chest placement for all chests no matter what?

    Please refer to those:
    http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
    http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
    http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
     
  3. Offline

    tomudding

    What you do is check for an inventory that is being opened (InventoryOpenEvent) but then you add your Gold(_Block) to some kind of another inventory that is not the inventory "{Cyber Crystals} Treasure!".

    ^ You create the inventory?!

    And you add the gold while the inventory is nowhere.
     
  4. @mine_care, its when the player right clicks with an item in his or her hand

    ok im really really confuzed, i just exported the plugin and it works just the way it was when i posted this, WTF

    OK , i just figured it out, it works when i right click on a block, but when i right click the air it just opens the inventory with no items

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

Share This Page