Solved Force Drop

Discussion in 'Plugin Development' started by Royal_Soda, Sep 30, 2012.

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

    Royal_Soda

    Hey, I'm wondering how am I able to force a player to drop an item in hand?
     
  2. Offline

    Timr

    Could do something easy like this, although I seem to recall a Bukkit function to do this:

    Code:java
    1. player.getWorld().dropItem(player.getLocation(), player.getItemInHand());
    2. player.setItemInHand(new ItemStack(Material.AIR));
     
  3. Offline

    Royal_Soda

    Timr - This doesn't seem to work, help please.
    Code:java
    1. @EventHandler
    2. public void onRightClick(PlayerInteractEvent event) {
    3.  
    4. if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    5.  
    6. if (event.getPlayer().getItemInHand().equals(Material.SLIME_BALL)) {
    7.  
    8. event.getPlayer().getWorld().dropItem(event.getPlayer().getLocation(), event.getPlayer().getItemInHand());
    9. event.getPlayer().setItemInHand(new ItemStack(Material.AIR));
    10.  
    11. }
    12.  
    13. }
    14.  
    15. }
     
  4. Offline

    Hertz

    royal

    maybe put something else or cancel the item drop


    1. event.getPlayer().setItemInHand(new ItemStack(Material.WOOD));
     
  5. Offline

    Royal_Soda

    Hertz - But I want the item to be dropped.
     
  6. Offline

    Timr

    I changed it up a bit, seems to work:

    Code:java
    1. @EventHandler
    2. public void onRightClick(PlayerInteractEvent event) {
    3. if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    4. if (event.getPlayer().getItemInHand().getType().equals(Material.SLIME_BALL)) {
    5. event.getPlayer().getWorld().dropItemNaturally(event.getPlayer().getLocation(), event.getPlayer().getItemInHand());
    6. event.getPlayer().setItemInHand(new ItemStack(Material.AIR));
    7. }
    8. }
    9. }
     
  7. Offline

    Loogeh

    Instead of doing if(event.getPlayer().getItemInHand().equals(Material.SLIME_BALL){

    do if(event.getPlayer().getItemInHand().getType() == Material.SLIME_BALL) {
    }
    Also, to set the item in hand as 'Air' instead of setting it as Material.AIR set it all null
     
  8. Offline

    Royal_Soda

    hmm, this is working now. But if the player is holding more than one slime ball, it drops all of them. I'd like for it to only drop one.
     
  9. Offline

    Timr

    Then do something along the lines of this:

    Code:java
    1. @EventHandler
    2. public void onRightClick(PlayerInteractEvent event) {
    3. Action a = event.getAction();
    4. Player p = event.getPlayer();
    5. if (a.equals(Action.RIGHT_CLICK_AIR) || a.equals(Action.RIGHT_CLICK_BLOCK)) {
    6. if (p.getItemInHand().getType().equals(Material.SLIME_BALL)) {
    7. if(p.getItemInHand().getAmount() > 1) {
    8. p.getWorld().dropItemNaturally(p.getLocation(), new ItemStack(p.getItemInHand().getType(), 1));
    9. p.setItemInHand(new ItemStack(p.getItemInHand().getType(), (p.getItemInHand().getAmount() - 1)));
    10. } else {
    11. p.getWorld().dropItemNaturally(p.getLocation(), new ItemStack(p.getItemInHand().getType(), 1));
    12. p.setItemInHand(null);
    13. }
    14. }
    15. }
    16. }
     
  10. Offline

    Royal_Soda

Thread Status:
Not open for further replies.

Share This Page