DeathEvent itemdrops

Discussion in 'Plugin Development' started by leet4044, Mar 2, 2014.

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

    leet4044

    So, I'm trying to remove all items that aren't enchanted whenever a player dies, my current code doesn't remove non enchanted items, but does display the message and doesn't throw any errors in console. If anyone can help, I will be greatly thankful.
    Current Code:
    Code:java
    1. @EventHandler(priority = EventPriority.MONITOR)
    2. public void onPlayerDeath(PlayerDeathEvent e) {
    3. e.setDroppedExp(0);
    4. if (e.getEntity() instanceof Player) {
    5. for(ItemStack enchanted : e.getDrops()) {
    6. if (enchanted.hasItemMeta()) {
    7. if (enchanted.getItemMeta().hasEnchants() == true) {
    8. e.getDrops().remove(enchanted);
    9. Bukkit.broadcastMessage("Enchanted Item Test");
    10. }
    11. }
    12. }
    13. }
     
  2. Offline

    Gater12

    leet4044
    It is removing items that are enchanted.
     
  3. Offline

    Maurdekye

    leet4044 First of all, you don't need the "== true" in that if statement. After that, put a print statement right above the e.getDrops().remove(enchanted) line to check if it's actually being run.
     
  4. Offline

    StealerSlain

    this

    if(enchanted.getItemMeta().hasEnchants()==true){

    to this
    if(!enchanted.getItemMeta().hasEnchants()){
     
  5. Offline

    Jombi

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.MONITOR)
    3. public void onPlayerDeath(PlayerDeathEvent e) {
    4. e.setDroppedExp(0);
    5. if (e.getEntity() instanceof Player) {
    6. for(ItemStack enchanted : e.getDrops()) {
    7. if (enchanted.getItemMeta().hasEnchants()) {
    8. e.getDrops().remove(enchanted);
    9. Bukkit.broadcastMessage("Enchanted Item Test");
    10. }
    11. }
    12. }
     
  6. Offline

    leet4044

    Maurdekye StealerSlain I changed the code up, and added the message. It displays both messages but doesn't remove the items that aren't enchanted.
    Code:java
    1. for(ItemStack enchanted : e.getDrops()) {
    2. if (enchanted.hasItemMeta()) {
    3. if(!enchanted.getItemMeta().hasEnchants()) {
    4. Bukkit.broadcastMessage("Enchanted Item Test1");
    5. enchanted.setType(Material.AIR);
    6. e.getDrops().remove(enchanted);
    7. Bukkit.broadcastMessage("Enchanted Item Test2");
    8. }
    9. }
    10. }
     
  7. Offline

    HungerCraftNL

    Remove the ! on the rule 'enchanted.getItemMeta().hasEnchants()'
     
  8. Offline

    Jombi

    If you remove the 'not', you're going to tell the method to remove all items that ARE enchanted.
     
  9. Offline

    Maurdekye

    Jombi That's what he wants. I think.
     
  10. Offline

    Gater12

    Maurdekye
     
  11. Offline

    Jombi

    Maurdekye
    Nope. He wants to remove NON-enchanted items, so players only drop enchanted items when they die.

    Try this:
    Code:java
    1.  
    2. @EventHandler(priority=EventPriority.HIGHEST)
    3. public void onJoin(PlayerDeathEvent event){
    4. if(event.getEntity() instanceof Player){
    5. for(ItemStack item : event.getDrops()){
    6. if(item.getEnchantments().size() < 1){ //Item does not have enchantments
    7. event.getDrops().remove(item); // Remove item
    8. }
    9. }
    10. }
    11.  
    12. }
     
  12. Offline

    Maurdekye

    leet4044 Well, if it was working 'correctly', you should be getting ConcurrentModificationErrors, as you're modifying the size of getDrops() while looping over it. But you're not getting any, so i'm not entirely sure what's happenning. Are you catching any errors with a try-catch statement? Also, is it printing out both the first and the second message you wrote?
     
    AoH_Ruthless likes this.
  13. Offline

    Gater12

    If you are getting ConcurrentModificationError, you can clone the list loop through that list and perform you checks and set the drops to that list.
     
  14. Offline

    Maurdekye

    Gater12 The problem isn't that, it's that he isn't getting errors for it, even though he should. There's something more to his code that needs to be shown.
     
  15. Offline

    leet4044

    Maurdekye Okay, I've fixed the non enchanted item drops. Now when I check if an item that contains a certain lore/display name drops. It doesn't remove. But if it is enchanted it will remove?
    Current Code:
    Code:java
    1. @EventHandler(priority = EventPriority.MONITOR)
    2. public void onPlayerDeath(PlayerDeathEvent e) {
    3. e.setDroppedExp(0);
    4. Player p = (Player) e.getEntity();
    5. ItemStack[] invcheck = p.getInventory().getContents();
    6. ItemStack helmet = p.getInventory().getHelmet();
    7. ItemStack chest = p.getInventory().getChestplate();
    8. ItemStack leggings = p.getInventory().getLeggings();
    9. ItemStack boots = p.getInventory().getBoots();
    10. if (e.getEntity() instanceof Player) {
    11. for(ItemStack item:invcheck) {
    12. if(item != null) {
    13. if(!item.getItemMeta().hasEnchants()) {
    14. if ((p.getInventory().contains(item) || (p.getInventory().getHelmet() == item) || (p.getInventory().getChestplate() == item) || (p.getInventory().getLeggings() == item) || (p.getInventory().getBoots() == item))) {
    15. if ((item.getType() == Material.DIAMOND_SWORD) ||(item.getType() == Material.DIAMOND_AXE) || (item.getType() == Material.DIAMOND_AXE) || (item.getType() == Material.DIAMOND_HELMET) || (item.getType() == Material.DIAMOND_CHESTPLATE) || (item.getType() == Material.DIAMOND_LEGGINGS) || (item.getType() == Material.DIAMOND_BOOTS) || (item.getType() == Material.BOW) || (item.getType() == Material.ARROW)) {
    16. e.getDrops().remove(item);
    17. }
    18. else if((item.getType() == Material.GOLDEN_APPLE)) {
    19. if(!(item.getDurability() == 1)) {
    20. e.getDrops().remove(item);
    21. }
    22. }
    23. }
    24. } else {
    25. if (item.getItemMeta().getDisplayName().contains("§cCurrent Class:")) {
    26. e.getDrops().remove(item);
    27. }
    28. else if (item.getItemMeta().getLore().contains("§6Soulbound")) {
    29. e.getDrops().remove(item);
    30. }
    31. }
    32. }
    33. }
    34. if(!helmet.getItemMeta().hasEnchants()) {
    35. p.getInventory().setHelmet(new ItemStack(Material.AIR));
    36. e.getDrops().remove(helmet);
    37. }
    38. if(!chest.getItemMeta().hasEnchants()) {
    39. p.getInventory().setChestplate(new ItemStack(Material.AIR));
    40. e.getDrops().remove(chest);
    41. }
    42. if(!leggings.getItemMeta().hasEnchants()) {
    43. p.getInventory().setLeggings(new ItemStack(Material.AIR));
    44. e.getDrops().remove(leggings);
    45. }
    46. if(!boots.getItemMeta().hasEnchants()) {
    47. p.getInventory().setBoots(new ItemStack(Material.AIR));
    48. e.getDrops().remove(boots);
    49. }
    50. }
    51. }
     
  16. Offline

    unon1100

    leet4044

    Code:java
    1. if (ChatColor.stripColor(item.getItemMeta().getDisplayName()).equals("Current Class:")) {
    2. e.getDrops().remove(item);
    3. }
    4. else if (ChatColor.stripColor(item.getItemMeta().getItemLore()).equals("Soulbound")) {
    5. e.getDrops().remove(item);
    6. }
     
Thread Status:
Not open for further replies.

Share This Page