Solved How to force drop?

Discussion in 'Spigot Plugin Development' started by QAQT0T, Jul 9, 2019.

  1. How to let a player drop item when it is not dead.
    Here is my code.

    Code (Text):

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Damageable;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.scheduler.BukkitRunnable;

    ......
    private void PlayerDie(EntityDamageEvent e) {

    ....................

            if (((Damageable)p).getHealth() - e.getFinalDamage() <= 0) {
                e.setCancelled(true);
                new BukkitRunnable() {
                    public void run() {
                        Main.getInstance().spawn(p);
                        DefaultItems.giveItem(p);
                        cancel();
                    }
               }.runTaskTimer(Main.getInstance(), 40, 40);
          # something drop...
    }
    I want to let it drop potion only.
     
  2. Try the "PlayerDropItemEvent". Maybe when the player dies, this event is triggered and you can control what should be dropped and what shouldn't.
     
  3. Code (Text):
    if(player.getInventory().getContents() == null)
                    return;
                for (ItemStack itemStack : player.getInventory().getContents()) {
                    if(itemstack you want to drop)
                        player.getWorld().dropItem(player.getLocation(), itemStack);
                }
     
  4. Code (Text):
        public static boolean isDropItem(PlayerDropItemEvent e) {
            Player p = e.getPlayer();
    if(e.getItemDrop().getItemStack().getItemMeta().getDisplayName().equalsIgnoreCase("§9§oNAME")){
                    e.getPlayer().getInventory().setItemInMainHand(null);
                    return true;
                }
                return false;
            }else{
                return false;
            }
        }
     
     
  5. You seems not know how Bukkit events work:
    - There is an action in the server or a packet is received
    - The event (if it exists) is triggered
    - If the event implements Cancellable and is cancelled the code is "returned"

    So create trigger an event will just... trigger it in other plugins; not trigger the action (here the drop)

    A methods in NMS exists to make the player drop an item but you can also remove the ItemStack (or decrement the amount) and use World#dropItem then set the velocity with the player's direction + maths
     
  6. I use 'EntityDamageEvent' .The final hit is canceled,so player is not dead,his items will not drop...I think the Dropitem event won't trigger.Have you got other ideas?
     
  7. You are missing a @EventHandler annotation above the method.
     
  8. Just demo code.
     
  9. I am pretty sure I know what I am talking about ;) I misunderstood the question sry for that.
     
    • Like Like x 1
    • Informative Informative x 1
  10. Code (Java):


    //A method which listens to an event should not return something and should not be static
    public static boolean isDropItem(PlayerDropItemEvent e) {
            Player p = e.getPlayer();
    if(e.getItemDrop().getItemStack().getItemMeta().getDisplayName().equalsIgnoreCase("§9§oNAME")) //This will throw an exception since the item meta and the display name can be null{
                    e.getPlayer().getInventory().setItemInMainHand(null); //This method does not exist.
                    return true;
                }
                return false; //Useless return and else statement
            }else{
                return false;
            }
        }
     
    This method is completely useless.
     
    #10 patri9ck, Jul 9, 2019
    Last edited: Jul 9, 2019
  11. You should check in the for loop if the item stack is not null before you drop it.
     
  12. yeah that was just a code snippet not something to copy
     
  13. If you want a player to loose all of his potions when he die use something like this:
    Code (Text):
    @EventHandler
    public void onHit(EntityDamageByEntityEvent event) {
          Entity hit = event.getEntity();
          if(!(hit instanceof Player)){
                 return;
           }
         
           Player damaged = (Player) hit;
           if(damaged.getHealth() - event-getFinalDamage() > 0) {
                return;
           }

          damaged.setHealth(20D);
          damaged.setHunger(20D);
          damaged.getPotionEffects().clear();
          damaged.setVelocity(new Vector(0, 0, 0));
          for(ItemStack item : player.getInventory().getContent()){
                if(item.getType() == Material.POTION) { //Not sure if there are more Material enums for potions
                    player.getInventory().removeItem(item);
               }
          }
    }
    Coded this on my phone so it will not be 100% accurate probably
     
    • Informative Informative x 1
  14. It's got working!Thank you!