How do I give the player a collection/list of ItemStacks?

Discussion in 'Plugin Development' started by pingpong1999, May 7, 2016.

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

    pingpong1999

    Hey guys! I have a custom enchant plugin for my server. One of the enchants is a laser enchant which throws and egg and has a 5% chance of exploding and a 95% chance of just normal mining in a + shape. another enchant is a drill which also mines in a + shape, but you have to manually mine and the pick doesn't shoot.

    So just a recap, laser shoots, drill is normal mining. I was wondering, How do I give the player all the items that have dropped. For example, if the laser explodes how do I get all the items from that explosion (with a yield of 100) and give it directly to the player. Also how would I give items to the player if the block is broken with breakNaturally(). Also would it be possible to apply fortune or some multiplier to the idrops

    Code:
        int cooldownTime = 30;
        @EventHandler
        public void onInteract(PlayerInteractEvent e){
            Player player = e.getPlayer();
            ItemStack item = e.getPlayer().getItemInHand();
            ItemMeta meta2 = item.getItemMeta();
            List<String> lore = meta2.getLore();
          
            Map<String, Long> cooldowns = new HashMap<String, Long>();
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(lore.toString().contains("Laser")){
                    player.launchProjectile(Egg.class);
                }
            if(cooldowns.get(player) - System.currentTimeMillis() * 1000 < cooldownTime) {
                player.sendMessage(ChatColor.YELLOW + "Wait for cooldown! " + System.currentTimeMillis() * 1000 + "s");
            }
            }
        }
      
      
        @EventHandler
        public void onEggThrow(ProjectileHitEvent e){
            Player player = (Player) e.getEntity().getShooter();
            if(e.getEntity() instanceof Egg){
                Location playerloc = player.getLocation();
                player.playSound(playerloc, Sound.DIG_STONE, 10, 10);
                Entity egg = e.getEntity();
                Location location = egg.getLocation();
                World world = e.getEntity().getWorld();
              
                Random rand = new Random();
                int number = rand.nextInt(100) + 1;
                if(number <= 5){
               Entity tnt = world.spawn(location, TNTPrimed.class);
               ((TNTPrimed)tnt).setFuseTicks(0);
                }else{
                    Location block = e.getEntity().getLocation().getBlock().getLocation().subtract(0.0, 1.0, 0.0);
                    Location surroundXp = e.getEntity().getLocation().getBlock().getLocation().subtract(0.0, 0.0, 1.0);
                    Location surroundXm = e.getEntity().getLocation().getBlock().getLocation().add(1.0, 0.0, 0.0);
                    Location surroundZp = e.getEntity().getLocation().getBlock().getLocation().add(0.0, 0.0, 1.0);
                    Location surroundZm = e.getEntity().getLocation().getBlock().getLocation().subtract(1.0, 0.0, 0.0);
                    if(canBuild(player, surroundXm) == true){
                    surroundXm.getBlock().breakNaturally();
                    surroundXm.getBlock().getDrops().
                    }
                    if(canBuild(player, surroundXp) == true){
                    surroundXp.getBlock().breakNaturally();
                    }
                    if(canBuild(player, surroundZp) == true){
                    surroundZp.getBlock().breakNaturally();
                    }
                    if(canBuild(player, surroundZm) == true){
                    surroundZm.getBlock().breakNaturally();
                    }
                    if(canBuild(player, block) == true){
                        block.getBlock().breakNaturally();
                    }
                }
          
    @EventHandler
        public void onBreak(BlockBreakEvent e){
            ItemStack item = e.getPlayer().getItemInHand();
            ItemMeta meta2 = item.getItemMeta();
            List<String> lore = meta2.getLore();
            if(lore.toString().contains("Drill")){
            Location surroundXp = e.getBlock().getLocation().subtract(0.0, 0.0, 1.0);
            Location surroundXm = e.getBlock().getLocation().add(1.0, 0.0, 0.0);
            Location surroundZp = e.getBlock().getLocation().add(0.0, 0.0, 1.0);
            Location surroundZm = e.getBlock().getLocation().subtract(1.0, 0.0, 0.0);
            Player player = e.getPlayer();
            if(canBuild(player, surroundXm) == true){
            surroundXm.getBlock().breakNaturally(item);
            }
            if(canBuild(player, surroundXp) == true){
            surroundXp.getBlock().breakNaturally(item);
            }
            if(canBuild(player, surroundZp) == true){
            surroundZp.getBlock().breakNaturally(item);
            }
            if(canBuild(player, surroundZm) == true){
            surroundZm.getBlock().breakNaturally(item);
            }
            }else if(lore.toString().contains("Laser")){
                e.setCancelled(true);
          
            }
        }
    
    here is my code. Also if there is a more efficient method that does EXACTLY the same thing, just tell me.
     
    Last edited: May 7, 2016
  2. Offline

    mine-care

    Try 'EntityExplodeEvent'.
    Also since you mentioned efficiency here are a few fixes:
    == 'returns' a boolean of wether or not the right hand side is the same as the left hand side so lets say that the method returns true: true == true => true. So why not directly: if(canBuild(..,..)){...}

    How do you know that the shooter is a Player?
     
  3. Offline

    pingpong1999

    Thanks! But wouldn't I use EntityExplodeEvent for say, recovering an entity that has exploded?
    And this still doesn't answer my main question :p, how do I had a list of items (a drop from an ore etc) to the player.
     
  4. Offline

    mine-care

  5. Offline

    pingpong1999

    @mine-care Cheers!
    I always forget to look at docs.

    @mine-care I had a good look at the docs and couldn't quite find out how to work it... It does not give much detail on how to do it... more help? :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 7, 2016
  6. Offline

    mine-care

    which part? the block list from the explode event? Or the addItem?
     
  7. Offline

    pingpong1999

    The add item, I know how to do the basic add 1 item, but I dont know how to add a list. and the doc about it doesn't show much. I will probably need help on the explode event aswell :p. Sorry about that, I just dont understand the docs
     
  8. Offline

    timtower Administrator Administrator Moderator

    @pingpong1999 You can also add the items 1 by 1.
    And list.toArray ?
     
  9. Offline

    pingpong1999

    When you say 1 by 1, how would i do that?
    I was thinking .getBlock().getDrops().forEach(); but I do not know how I can implement that
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    pingpong1999

    would this work?
    Code:
                    for(surroundXm.getBlock().getDrops().toArray();;){
                        player.getInventory().addItem((ItemStack[]) surroundXm.getBlock().getDrops().toArray());
                    }
    
    (sorry) im not very good with lists and stuff.
     
  12. Offline

    I Al Istannen

  13. Offline

    pingpong1999

    @I Al Istannen @timtower @mine-care Honestly, I have no idea how to do this... I have tried everything and searched everywhere. I'd hate to say it. But I think I give up
     
  14. Offline

    mine-care

    @pingpong1999
    A step by step guide.
    1. get the list of blocks
    2. find the drop of each of them (i dont know how)
    3. add it to the inventory.

    What i would do:
    Code:
    loop for Block b : dropedBlocksList:
    playerInventory#addItem( ItemStack(b#getType)) 
    end loop
    
     
Thread Status:
Not open for further replies.

Share This Page