Would this work?

Discussion in 'Plugin Development' started by XgXXSnipz, Jan 22, 2015.

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

    XgXXSnipz

    so im trying to make it so when a player clicks a chest it gives him 4 random items, I used some code from a minigame I made awhile back.. will it work

    Code:
    public ItemStack[] fillChest() {
            Material[] items1 = new Material[] { Material.GOLDEN_APPLE, Material.IRON_SWORD, Material.IRON_CHESTPLATE, Material.IRON_SWORD };
            Material[] items2 = new Material[] { Material.APPLE, Material.WOOD_SWORD, Material.LEATHER_CHESTPLATE, Material.BOW, Material.ARROW, Material.STONE_SWORD, Material.DIAMOND, Material.LEATHER_BOOTS, Material.IRON_INGOT };
           
            Random r = new Random();
           
            int numItems = r.nextInt(5) + 1; // A random number between 1 and 5.
           
            for (int i = 0; i < numItems; i++) {
                Material material = null;
               
                if (tier == 1) {
                    material = items1[r.nextInt(items1.length)]; // A random Materials from the items1 array.
                }
               
                else if (tier == 2) {
                    material = items2[r.nextInt(items2.length)]; // A random Materials from the items2 array.
                }
               
                ItemStack item = new ItemStack(material, 1);
               
                int index;
               
                do {
                    index = r.nextInt(chest.getInventory().getSize());
                } while (chest.getInventory().getItem(index) != null);
               
                chest.getInventory().setItem(index, item);
            }
            return null;
            }
    Code:
    if (e.getInventory().getHolder() instanceof Chest && canOpen.containsKey(p) && p.isSneaking()){
               e.setCancelled(true);
              
               p.getInventory().addItem(fillChest());
     
  2. Offline

    Skionz

  3. Offline

    XgXXSnipz

    @Skionz nope.. no work it doesnt even send a message when I place down my chest
     
  4. Offline

    Skionz

    @XgXXSnipz I don't see you ever send a message in the method you posted.
     
  5. Offline

    XgXXSnipz

    @Skionz
    Code:
    @EventHandler
        public void onBreak(BlockBreakEvent e){
            Player p = e.getPlayer();
           
            if(e.getBlock().equals(Material.CHEST) && p.isOp()
                    && p.isSneaking() || chestPlaced.containsKey(p)){
                e.getBlock().breakNaturally();
               
                p.sendMessage(ChatColor.YELLOW + "[" + ChatColor.BLUE + "NetherFactions" + ChatColor.YELLOW + "]" + ChatColor.GREEN + "Randomized Item chest destroyed!");
            }else
                return;
           
        }
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
            canOpen.put(p, p);
            if(canOpen.containsKey(p)){
                return;
            }
        }
       
        @EventHandler
        public void onPlace(BlockPlaceEvent e){
            Player p = e.getPlayer();
           
            if(e.getBlock().equals(Material.CHEST) && p.isOp() && p.isSneaking()){
                p.sendMessage(ChatColor.YELLOW + "[" + ChatColor.BLUE + "NetherFactions" + ChatColor.YELLOW + "]" + ChatColor.GREEN + "Randomized Item chest placed!");
               
                chestPlaced.put(p, p);
               
            }else
            return;
        }
        
     
  6. Offline

    Skionz

  7. Offline

    XgXXSnipz

    @Skionz its all in one class,
    Code:
    Bukkit.getServer().getPluginManager().registerEvents(this,this);
     
  8. Offline

    teej107

  9. Offline

    XgXXSnipz

    @teej107 I did... it no work still

    EDIT: does work just not the chest placing and stuff
     
    Last edited: Jan 22, 2015
  10. Offline

    BaconStripzMan

    No { after else :p try now
     
  11. Offline

    Konato_K

    @XgXXSnipz
    Code:
    e.getBlock().equals(Material.CHEST)
    I'm not a java expert, but I think a block can't be equals to a Material
     
    Skionz likes this.
  12. Offline

    XgXXSnipz

    Last edited by a moderator: Jan 23, 2015
  13. Offline

    Skionz

  14. Offline

    BaconStripzMan

    They can :p @XgXXSnipz is it working?
     
  15. Offline

    XgXXSnipz

    @Skionz well IDK what to put.... I cant do
    Code:
    BLock.
    
     
  16. Offline

    BaconStripzMan

    Material.CHEST
     
  17. Offline

    Skionz

    They can't.
     
  18. Offline

    BaconStripzMan

    oh..
     
  19. Offline

    Konato_K

    @BaconStripzMan No they can't, this is the code for equals in CraftBlock
    Code:
    public boolean equals(Object o)
      {
      if (o == this) return true;
      if (!(o instanceof CraftBlock)) return false;
      CraftBlock other = (CraftBlock)o;
    
      return (x == other.x) && (y == other.y) && (z == other.z) && (getWorld().equals(other.getWorld()));
      }
    
    I don't think Material extends CraftBlock

    @XgXXSnipz Try getting the type of the block and comparing that instead?
     
    Skionz likes this.
  20. Offline

    XgXXSnipz

  21. Offline

    Skionz

    @XgXXSnipz Read the above messages. It won't work. The solution is to compare the type to a different Material.
     
  22. Offline

    BaconStripzMan

    No my solution was he didnt have a { after else
     
    Skionz likes this.
  23. Offline

    XgXXSnipz

  24. Offline

    Skionz

    Its only one line. There doesn't need to be any brackets.
    @XgXXSnipz I wonder if the Block#getType method will work :eek:
     
    Konato_K and BaconStripzMan like this.
  25. Offline

    Konato_K

    BaconStripzMan and Skionz like this.
  26. Offline

    BaconStripzMan

  27. Offline

    XgXXSnipz

  28. Offline

    BaconStripzMan

    Looks good, does it work?
     
  29. Offline

    XgXXSnipz

    @BaconStripzMan testing now EDIT: Keep timing out on my server, may not be able to test
     
  30. Offline

    BaconStripzMan

    Wanna send it to me? I can test, just keep trying to join
     
Thread Status:
Not open for further replies.

Share This Page