Development Assistance Multiple Items - Percentage & Randoms

Discussion in 'Plugin Help/Development/Requests' started by someguyonthestreet, Apr 22, 2015.

Thread Status:
Not open for further replies.
  1. Hello,
    I'm trying to send a player an item when they click on a specific chest. Its kind of like a crate plugin but it deals by percentage. The problem is that it gives more than one item.
    Code:
    Code:
    @EventHandler
          public void onPlayerClick(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (event.getClickedBlock().getType() == Material.CHEST) {
                   Block chest = event.getClickedBlock();
                   Location normalChest = new Location(player.getWorld(), getConfig().getInt("normal.x"), getConfig().getInt("normal.y"), getConfig().getInt("normal.z"));
                   Location donatorChest = new Location(player.getWorld(), getConfig().getInt("donator.x"), getConfig().getInt("donator.y"), getConfig().getInt("donator.z"));
                   if (chest.getType() == Material.CHEST) {
                   if (chest.getLocation().equals(normalChest)) {
                       event.setCancelled(true);
                       if (checkNormalKeys(player) >= 1) {
                          
                           removeKey(player, Type.NORMAL);
                          
                           Random random = new Random();
                           int chance = random.nextInt(100 ) + 1;
                           if (chance <= 1) {
                               int chance1 = random.nextInt(3 ) + 1;
                               if (chance1 == 1) {
                                   addItem(player, Material.DIAMOND_HELMET, "", Enchantment.PROTECTION_ENVIRONMENTAL, 3);
                                   addItem(player, Material.DIAMOND_CHESTPLATE, "", Enchantment.PROTECTION_ENVIRONMENTAL, 3);
                                   addItem(player, Material.DIAMOND_LEGGINGS, "", Enchantment.PROTECTION_ENVIRONMENTAL, 3);
                                   addItem(player, Material.DIAMOND_BOOTS, "", Enchantment.PROTECTION_ENVIRONMENTAL, 3);
                                   player.sendMessage(format("&6You have won the protection 3 diamond armor loot!"));
                               }
                              
                               if (chance1 == 2) {
                                   addKey(player, 5, Type.NORMAL);
                                   player.sendMessage(format("&6You have won 5 normal crate keys!"));
                               }
    
                               if (chance1 == 3) {
                                   player.getInventory().addItem(new ItemStack(Material.MOB_SPAWNER, 1, (short) 57));
                                   player.sendMessage(format("&6You have won a zombie pigman spawner!"));
                               }
                           }
                          
                           if (chance <= 10) {
                               int chance10 = random.nextInt(2 ) + 1;
                              
                               if (chance10 == 1) {
                                   addItem(player, Material.IRON_HELMET, "", Enchantment.PROTECTION_ENVIRONMENTAL, 4);
                                   addItem(player, Material.IRON_CHESTPLATE, "", Enchantment.PROTECTION_ENVIRONMENTAL, 4);
                                   addItem(player, Material.IRON_LEGGINGS, "", Enchantment.PROTECTION_ENVIRONMENTAL, 4);
                                   addItem(player, Material.IRON_BOOTS, "", Enchantment.PROTECTION_ENVIRONMENTAL, 4);
                                   player.sendMessage(format("&6You have won the protection 4 iron armor loot!"));
                              
                               }
                              
                               if (chance10 == 2) {
                                   addItem(player, Material.DIAMOND_HELMET, "", Enchantment.PROTECTION_ENVIRONMENTAL, 2);
                                   addItem(player, Material.DIAMOND_CHESTPLATE, "", Enchantment.PROTECTION_ENVIRONMENTAL, 2);
                                   addItem(player, Material.DIAMOND_LEGGINGS, "", Enchantment.PROTECTION_ENVIRONMENTAL, 2);
                                   addItem(player, Material.DIAMOND_BOOTS, "", Enchantment.PROTECTION_ENVIRONMENTAL, 2);
                                   player.sendMessage(format("&6You have won the protection 2 diamond armor loot!"));
                              
                               }
                           }
                          
                           if (chance <= 25) {
                               Economy eco = new Economy();
                               try {
                                eco.add(player.getName(), 1000);
                                player.sendMessage(format("&6You have won $1000!"));
                            } catch (NoLoanPermittedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (UserDoesNotExistException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                           }
                          
                           if (chance <= 75) {
                               int chance75 = random.nextInt(5 ) + 1;
                              
                               if (chance75 == 1) {
                                   player.getInventory().addItem(new ItemStack(Material.OBSIDIAN, 16));
                                   player.sendMessage(format("&6You have won 16 obsidian!"));
                               }
                              
                               if (chance75 == 2) {
                                   player.getInventory().addItem(new ItemStack(Material.BEDROCK, 2));
                                   player.sendMessage(format("&6You have won 2 bedrock!"));
                               }
                              
                               if (chance75 == 3) {
                                   player.getInventory().addItem(new ItemStack(Material.GOLDEN_APPLE, 2));
                                   player.sendMessage(format("&6You have won 2 golden apples!"));
                               }
                              
                               if (chance75 == 4) {
                                   player.getInventory().addItem(new ItemStack(Material.EXP_BOTTLE, 32));
                                   player.sendMessage(format("&6You have won 32 exp bottles!"));
                               }
                              
                               if (chance75 == 5) {
                                   addKey(player, 1, Type.NORMAL);
                                   player.sendMessage(format("&6You have won 1 normal crate key!"));
                               }
                           }
                          
                           if (chance <= 90) {
                               Economy eco = new Economy();
                              
                               try {
                                eco.add(player.getName(), 100);
                                player.sendMessage(format("&6You have won $100!"));
                            } catch (NoLoanPermittedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (UserDoesNotExistException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                           }
                           player.updateInventory();
                           } else {
                           Location loc1 = chest.getLocation();
                           Location loc2 = player.getLocation();
                           
                           double deltaX = loc2.getX() - loc1.getX();
                           double deltaZ = loc2.getZ() - loc1.getZ();
                           
                           Vector vec = new Vector(deltaX, 0, deltaZ);
                           vec.normalize();
                           player.setVelocity(vec.multiply(5 / (Math.sqrt(Math.pow(deltaX, 2.0) + Math.pow(deltaZ, 2.0)))));
                           
                           player.sendMessage(format("&cYou do not have enough keys!"));
                       }
                   } else if (chest.getLocation().equals(donatorChest)) {
                       event.setCancelled(true);
                       if (checkDonatorKeys(player) >= 1) {
                          
                           removeKey(player, Type.DONATOR);
                           player.sendMessage(format("&aYou have opened a donator crate!"));
                          
                           Random random = new Random();
                           int chance = random.nextInt(100 ) + 1;
                           if (chance <= 1) {
                               int chance1 = random.nextInt(4) + 1;
                              
                               if (chance1 == 1) {
                                   player.getInventory().addItem(new ItemStack(Material.BEDROCK, 32));
                                   player.sendMessage(format("&6You have won 32 bedrock!"));
                               }
                              
                               if (chance1 == 2) {
                                   addItem(player, Material.DIAMOND_HELMET, "", Enchantment.PROTECTION_ENVIRONMENTAL, 7);
                                   addItem(player, Material.DIAMOND_CHESTPLATE, "", Enchantment.PROTECTION_ENVIRONMENTAL, 7);
                                   addItem(player, Material.DIAMOND_LEGGINGS, "", Enchantment.PROTECTION_ENVIRONMENTAL, 7);
                                   addItem(player, Material.DIAMOND_BOOTS, "", Enchantment.PROTECTION_ENVIRONMENTAL, 7);
                                   player.sendMessage(format("&6You have won the protection 7 diamond armor loot!"));
                              
                               }
    
                               if (chance1 == 3) {
                                   ItemStack bow = new ItemStack(Material.BOW);
                                   ItemMeta bowmeta = bow.getItemMeta();
                                   bowmeta.setDisplayName(format("&bGod Bow"));
                                   bow.addEnchantment(Enchantment.ARROW_DAMAGE, 7);
                                   bow.addEnchantment(Enchantment.ARROW_KNOCKBACK, 6);
                                   bow.addEnchantment(Enchantment.ARROW_FIRE, 1);
                                   bow.addEnchantment(Enchantment.ARROW_INFINITE, 1);
                                   bow.setItemMeta(bowmeta);
                                   player.getInventory().addItem(bow);
                                   player.sendMessage(format("&6You have won the god bow!"));
                               }
    
                               if (chance1 == 4) {
                                   Economy eco = new Economy();
                                   try {
                                    eco.add(player.getName(), 10000);
                                    player.sendMessage(format("&6You have won $10000!"));
                                } catch (NoLoanPermittedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                } catch (UserDoesNotExistException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                               }
                           }
                          
                           if (chance <= 10) {
                               player.getInventory().addItem(new ItemStack(Material.GOLD_INGOT, 80));
                               player.sendMessage(format("&6You have won 80 gold ingots"));
                           }
                          
                           if (chance <= 25) {
                               int chance25 = random.nextInt(6) + 1;
                              
                               if (chance25 == 1) {
                                   ItemStack bow = new ItemStack(Material.BOW);
                                   ItemMeta bowmeta = bow.getItemMeta();
                                   bowmeta.setDisplayName(format("&bGod Bow"));
                                   bow.addEnchantment(Enchantment.ARROW_DAMAGE, 6);
                                   bow.addEnchantment(Enchantment.ARROW_KNOCKBACK, 5);
                                   bow.addEnchantment(Enchantment.ARROW_FIRE, 1);
                                   bow.addEnchantment(Enchantment.ARROW_INFINITE, 1);
                                   bow.setItemMeta(bowmeta);
                                   player.getInventory().addItem(bow);
                                   player.sendMessage(format("&6You have won the god bow!"));
                               }
                              
                               if (chance25 == 2) {
                                   player.getInventory().addItem(new ItemStack(Material.GOLD_INGOT, 40));
                                   player.sendMessage(format("&6You have won 40 gold ingots!"));
                               }
                              
                               if (chance25 == 3) {
                                   player.getInventory().addItem(new ItemStack(Material.DIAMOND, 128));
                                   player.sendMessage(format("&6You have won 128 diamonds!"));
                               }
                              
                               if (chance25 == 4) {
                                   player.getInventory().addItem(new ItemStack(Material.OBSIDIAN, 380));
                                   player.sendMessage(format("&6You have won 380 obsidian!"));
                               }
                              
                               if (chance25 == 5) {
                                   player.getInventory().addItem(new ItemStack(Material.GOLDEN_APPLE, 5));
                                   player.sendMessage(format("&6You have won 5 golden apples!"));
                               }
                              
                               if (chance25 == 5) {
                                   player.getInventory().addItem(new ItemStack(Material.TNT, 96));
                                   player.sendMessage(format("&6You have won 96 tnt!"));
                               }
                           }
                          
                           if (chance <= 50) {
                               addKey(player, 2, Type.DONATOR);
                               player.sendMessage(format("&6You have won 2 donator crate keys!"));
                           }
                          
                           if (chance <= 75) {
                               Economy eco = new Economy();
                              
                               try {
                                eco.add(player.getName(), 5000);
                                player.sendMessage(format("&6You have won $5000!"));
                            } catch (NoLoanPermittedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (UserDoesNotExistException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                           }
                          
                           if (chance <= 90) {
                               Economy eco = new Economy();
                              
                               try {
                                eco.add(player.getName(), 500);
                                player.sendMessage(format("&6You have won $500!"));
                            } catch (NoLoanPermittedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (UserDoesNotExistException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                           }
                          
                           player.updateInventory();
                          
                       } else {
                           Location loc1 = chest.getLocation();
                           Location loc2 = player.getLocation();
                           
                           double deltaX = loc2.getX() - loc1.getX();
                           double deltaZ = loc2.getZ() - loc1.getZ();
                           
                           Vector vec = new Vector(deltaX, 0, deltaZ);
                           vec.normalize();
                           player.setVelocity(vec.multiply(5 / (Math.sqrt(Math.pow(deltaX, 2.0) + Math.pow(deltaZ, 2.0)))));
                           
                           player.sendMessage(format("&cYou do not have enough keys!"));
                       }
                       }
                   }
                }
            }
        }
     
  2. Offline

    Corndogoz

    does this actually give the player diamond armor enchanted with protection 3?
    Also just saying, your code is very confusing
     
  3. Yes I have tested all the items and they work. In that case i'm using my own method instead of creating multiple item stacks. But like I said in the main post, it will give two items like the diamond armor set and money. I don't know what the problem is
     
  4. Offline

    Corndogoz

    can you give me your addItem method
     
  5. I got it to fix, forgot to add return;
     
Thread Status:
Not open for further replies.

Share This Page