Solved Drop Inventory 1.7.10

Discussion in 'Plugin Development' started by maliimaloo, Jan 7, 2019.

  1. Offline

    maliimaloo

    Good morning to you;

    I have a small problem that when I try to drop my item out of the inventory, I get a mistake in my console.


    Code:java
    1. package com.maliimaloo.eventListener;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.inventory.InventoryClickEvent;
    10. import org.bukkit.event.player.PlayerDropItemEvent;
    11. import org.bukkit.event.player.PlayerPickupItemEvent;
    12.  
    13. import com.maliimaloo.Main;
    14. import com.maliimaloo.Utils.SettingsManager;
    15.  
    16. public class Armure implements Listener {
    17.  
    18. public Armure(Main main) {
    19. }
    20.  
    21. SettingsManager settings = SettingsManager.getInstance();
    22. @EventHandler
    23. public void pickItem(PlayerPickupItemEvent event)
    24. {
    25. Player player = (Player) event.getPlayer();
    26. String itemName = event.getItem().getItemStack().getItemMeta().getDisplayName();
    27.  
    28. List<String> getArmure = new ArrayList<String>();
    29. for (String str : settings.getArmureConfig().getConfigurationSection("Armure").getKeys(false)) {
    30. getArmure.add(str);
    31. }
    32.  
    33. for (int a = 0; a < getArmure.size(); a++)
    34. {
    35. String configHelmetName = settings.getArmureConfig().getString("Armure." + getArmure.get(a) + ".ItemName");
    36.  
    37. if (configHelmetName.equalsIgnoreCase(itemName))
    38. {
    39. int levelRequis = settings.getArmureConfig().getInt("Armure." + getArmure.get(a) + ".LevelRequis");
    40. int levelActuel = player.getLevel();
    41.  
    42. if (levelActuel < levelRequis) {
    43. event.setCancelled(true);
    44. return;
    45. }
    46. }
    47. }
    48. }
    49. @EventHandler
    50. public void dropItem(PlayerDropItemEvent event)
    51. {
    52. Player player = (Player) event.getPlayer();
    53.  
    54. String itemName = event.getItemDrop().getItemStack().getItemMeta().getDisplayName();
    55.  
    56. List<String> getArmure = new ArrayList<String>();
    57. for (String str : settings.getArmureConfig().getConfigurationSection("Armure").getKeys(false)) {
    58. getArmure.add(str);
    59. }
    60.  
    61. for (int a = 0; a < getArmure.size(); a++)
    62. {
    63. String configHelmetName = settings.getArmureConfig().getString("Armure." + getArmure.get(a) + ".ItemName");
    64.  
    65. if (configHelmetName.equalsIgnoreCase(itemName))
    66. {
    67. int levelRequis = settings.getArmureConfig().getInt("Armure." + getArmure.get(a) + ".LevelRequis");
    68. int levelActuel = player.getLevel();
    69.  
    70. if (levelActuel < levelRequis) {
    71. event.setCancelled(true);
    72. player.closeInventory();
    73. return;
    74. }
    75. }
    76. }
    77. }
    78.  
    79. @EventHandler
    80. public void armureClick(InventoryClickEvent event) {
    81. if (event.getInventory() == null)
    82. return;
    83.  
    84. Player player = (Player) event.getWhoClicked();
    85. String itemName = null;
    86.  
    87. if(event.getCurrentItem().equals(null))
    88. {
    89. return;
    90. }
    91. else if(!(event.getCurrentItem().hasItemMeta()))
    92. {
    93. String itemCurrentName = String.valueOf(event.getCurrentItem());
    94. String[] itemCurrentNameSplit = itemCurrentName.split(" x");
    95. itemName = itemCurrentNameSplit[0].replace("ItemStack{", "").replaceAll("_", " ");
    96. }
    97. else if(event.getCurrentItem().hasItemMeta())
    98. {
    99. itemName = event.getCurrentItem().getItemMeta().getDisplayName();
    100. }
    101.  
    102. List<String> getArmure = new ArrayList<String>();
    103. for (String str : settings.getArmureConfig().getConfigurationSection("Armure").getKeys(false)) {
    104. getArmure.add(str);
    105. }
    106.  
    107. for (int a = 0; a < getArmure.size(); a++)
    108. {
    109. String configHelmetName = settings.getArmureConfig().getString("Armure." + getArmure.get(a) + ".ItemName");
    110.  
    111. if (configHelmetName.equalsIgnoreCase(itemName))
    112. {
    113. int levelRequis = settings.getArmureConfig().getInt("Armure." + getArmure.get(a) + ".LevelRequis");
    114. int levelActuel = player.getLevel();
    115.  
    116. if (levelActuel < levelRequis) {
    117. event.setCancelled(true);
    118. player.closeInventory();
    119. player.sendMessage("§9[OmegaArmure] §cVous devez être level: §9" + levelRequis + " §cpour l'équiper.");
    120. return;
    121. }
    122. }
    123. }
    124. }
    125. }
    126.  
     

    Attached Files:

    Last edited by a moderator: Jan 7, 2019
  2. Offline

    timtower Ninja on the waves Moderator

    @maliimaloo You never check if the item has a displayname.
     
  3. Offline

    maliimaloo

    Actually, I check it with the (event.GetCurrentItem.hasItemMeta) ??

    The problem only occurs when I click on the item and throw it out the window
     
  4. Offline

    timtower Ninja on the waves Moderator

    Not in the other events.
    Null check can't be done with equals, that is probably the issue here, you need to use event.getCurrentItem() == null instead of event.getCurrentItem().equals(null)
     
  5. Offline

    maliimaloo

    Exactly, I’d reread the code, but I didn’t pay attention to that line.

    For lack of my inattention ^^
     

Share This Page