Solved Clear all drops & dropItemNaturally

Discussion in 'Plugin Development' started by vasil7112, Aug 28, 2013.

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

    vasil7112

    Dear developers,
    I am trying to drop some items, and check if an item is dropped in a location. If an item is dropped in that location then, i don't want to drop it again.
    Here is my code:
    Code:
    Location loc = new Location(Bukkit.getWorld("Knife"), -620, 9, 197);
          Location loc2 = new Location(Bukkit.getWorld("Knife"), -619, 9, 196);
          Location loc3 = new Location(Bukkit.getWorld("Knife"), -619, 9, 197);
          Location loc4 = new Location(Bukkit.getWorld("Knife"), -619, 9, 198);
          Location loc5 = new Location(Bukkit.getWorld("Knife"), -620, 9, 197);
          Location loc6 = new Location(Bukkit.getWorld("Knife"), -620, 9, 198);
          Location loc7 = new Location(Bukkit.getWorld("Knife"), -621, 9, 196);
          Location loc8 = new Location(Bukkit.getWorld("Knife"), -621, 9, 197);
          Location loc9 = new Location(Bukkit.getWorld("Knife"), -621, 9, 198);
          int rand = random.nextInt(4) + 1;
          if(!(loc.getBlock().getDrops().size() >= 1) && !(loc2.getBlock().getDrops().size() >= 1) && !(loc3.getBlock().getDrops().size() >= 1) && !(loc4.getBlock().getDrops().size() >= 1) && !(loc5.getBlock().getDrops().size() >= 1) && !(loc6.getBlock().getDrops().size() >= 1) && !(loc7.getBlock().getDrops().size() >= 1) && !(loc8.getBlock().getDrops().size() >= 1) && !(loc9.getBlock().getDrops().size() >= 1)){
              if(rand == 1){
                  ItemStack stack = new ItemStack(Material.DIAMOND_BOOTS, 1);
                  loc.getWorld().dropItemNaturally(loc, stack);
              }else if(rand == 2){
                  ItemStack stack = new ItemStack(Material.GOLD_BOOTS, 1);
                  loc.getWorld().dropItemNaturally(loc, stack);
              }else if(rand == 3){
                  ItemStack stack = new ItemStack(Material.SNOW_BALL, 1);
                  loc.getWorld().dropItemNaturally(loc, stack);
              }else if(rand == 4){
                  ItemStack stack = new ItemStack(Material.COMPASS, 1);
                  loc.getWorld().dropItemNaturally(loc, stack);
              }
          }
    For some reason this drops the items, but doesn't check if there are already dropped items :/

    Also, an extra question. If i put to drop the item to the loc, it will drop it near the loc(1-2 blocks away). Is there any possibility i can make it to drop to a specific block?
    Kind regards
     
  2. Offline

    Tarestudio

    vasil7112
    Block.getDrops() gives you the Items it drops if you 'destroy' it with the right tool. You want to check if at the location is an Entity instanceof Item representing an certain ItemStack. If you want to place it, dont Drop, just spawn ;)
     
  3. Offline

    vasil7112

    1: can you give me an example about the enity instance of?
    2: What do you mean just spawn an item there? Using the dropItem instead of dropItemNaturaly?

    EDIT: Thanks alot for helping:)
     
  4. Offline

    fireblast709

    world.dropItem(Location, ItemStack) will drop at the exact location. Moreover, to stop the block itself from dropping items, there is a note in the javadocs for BlockBreakEvent:
     
  5. Offline

    Tarestudio

    vasil7112
    Maybe I think the wrong way and there is an easier one...
    So to get the Items, Bukkit.getWorld("Knife").getEntitiesByClass(Item.class), returning a Collection of entities. Loop through this and check the location. Because you used this method, the check for instanceof is not needed.
    For the second, just as fireblast709 recommended, use world.dropItem(Location, ItemStack)
     
  6. Offline

    vasil7112

    Yeah i got the second, the first one is what i am really having touble with :/
     
  7. Offline

    Tarestudio

    vasil7112
    An Item hovering over the ground is not a Block, its an Entity. There is no method I know to check if there is an Item at a given Location. So you have to check the entities on your own.
    Just written from mind, dont know if this works
    Code:java
    1. for (Entity entity : Bukkit.getWorld("Knife").getEntitiesByClass(Item.class)) {
    2. if (entity.getLocation().getBlockX() == -619 && entity.getLocation().getBlockY() == 9 && entity.getLocation().getBlockZ() == 197) {
    3. return;
    4. }
    5. }
    6. int rand = random.nextInt(4) + 1;
    7. switch (rand) {
    8. case 1:
    9. ItemStack stack = new ItemStack(Material.DIAMOND_BOOTS, 1);
    10. break;
    11. case 2:
    12. ItemStack stack = new ItemStack(Material.GOLD_BOOTS, 1);
    13. break;
    14. case 3:
    15. ItemStack stack = new ItemStack(Material.SNOW_BALL, 1);
    16. break;
    17. default:
    18. ItemStack stack = new ItemStack(Material.COMPASS, 1);
    19. break;
    20. }
    21. Bukkit.getWorld("Knife").dropItem(loc, stack);[syntax][/syntax]
     
  8. Offline

    vasil7112

     
  9. Offline

    fireblast709

    People really like to do it the hard way huh
    Code:
    @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
    public void onBreak(BlockBreakEvent event)
    {
        Block b = event.getBlock();
        if(the drops should be replaced)
        {
            event.setCancelled(true);
            b.setType(Material.AIR);
            b.getWorld().dropItem(b.getLocation().add(0.5, 0.1, 0.5), the ItemStack)
        }
    }
     
  10. Offline

    vasil7112

    Hah, i didn't want custom drop on block break:), I am doing the following:
    Every 15s drop a Powerup(an item), if the powerup is already dropped, don't drop another one.
    I've achieved that now, thanks though:)
     
Thread Status:
Not open for further replies.

Share This Page