Solved PlayerDropItemEvent Bugging? Or..

Discussion in 'Plugin Development' started by Pizza371, Oct 22, 2013.

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

    Pizza371

    Well I have this..:
    public void onDrop(PlayerDropItemEvent e) {
    if(this.isRandomizeEnabled(e.getPlayer())) {
    e.getPlayer().sendMessage(this.title + "You cannot modify your inventory while Randomize is enabled!");
    e.setCancelled(true);
    }
    }
    It won't cancel the item drop if you press 'Q' in your hotbar, any ideaS?
     
  2. Offline

    Minecrell

    Pizza371
    Did you register your events? Do you have @EventHandler above your method?
     
  3. Offline

    Pizza371

    Minecrell I didn't explain myself well enough:
    It does call, I do get the message, but only when i do it with the Inventory GUI open, but when I press 'Q' on the hotbar, it doesnt work :(
     
  4. Offline

    Minecrell

    Pizza371
    Do you get the message if you press 'Q' on the hotbar (or only in the inventory)?
     
  5. Offline

    Pizza371

    Minecrell nothing gets executed when I press Q
     
  6. Offline

    Minecrell

    Pizza371
    Maybe isRandomizeEnabled() will return false after you close the inventory? (I don't know where you set it)
     
  7. Offline

    Dread9Nought

    Code:
    @EventHandler
    public void onDrop(PlayerDropItemEvent e) {
              e.getPlayer().sendMessage("I work");
    }
    
    If that code ^ doesn't work then you probably aren't registering the event(
    onEnable() --> "getServer().getPluginManager.registerEvents(<Listener>, this);")

    , if it does, then the "if(this.isRandomizeEnabled(e.getPlayer()))" is returning false for whatever reason
     
  8. Offline

    Pizza371

    Dread9Nought: It does work but only when I open my inventory and drag stuff out
    Minecrell
    private boolean isRandomizeEnabled(Player p) {
    if(this.randomizeEnabled.contains(p.getName())) return true;
    return false;
    }
    it just an easy way of getting whether a player is in a list.. cuz.. im lazy
     
  9. Offline

    Minecrell

    Pizza371
    Where do you add them to the list?
     
  10. Offline

    Pizza371

    Minecrell
    Code:java
    1. private void toggleRandomizer(Player p) {
    2. if(this.isRandomizeEnabled(p)) {
    3. p.sendMessage(this.title + "Randomize toggled [" + ChatColor.DARK_RED + "OFF" + ChatColor.WHITE + "]");
    4. this.randomizeEnabled.remove(p.getName()); //remove
    5. if(this.armourcontents.containsKey(p.getName())) {
    6. p.getInventory().setArmorContents(this.armourcontents.get(p.getName()));
    7. this.armourcontents.remove(p.getName());
    8. }
    9. } else {
    10. p.sendMessage(this.title + "Randomize toggled [" + ChatColor.GREEN + "ON" + ChatColor.WHITE + "]");
    11. this.randomizeEnabled.add(p.getName()); //add
    12. this.armourcontents.put(p.getName(), p.getInventory().getArmorContents());
    13. }
    14. }[/syntax=java]
     
  11. Offline

    Minecrell

    Pizza371
    What happens if you remove the if clause and always send a message like this:
    Code:java
    1. @EventHandler
    2. public void onPlayerDropItem(PlayerDropItemEvent event) {
    3. event.getPlayer().sendMessage(ChatColor.GREEN + "You've dropped an item!");
    4. }

    Will you get the message when using 'Q' on the hotbar then?
     
  12. Offline

    Pizza371

    You can still drop items with 'Q' with
    public void onDrop(PlayerDropItemEvent e) {
    e.setCancelled(true);
    }
    Minecrell
     
  13. Offline

    Minecrell

    Pizza371
    Tested it myself, and it's working for me... Can you post your complete class? Do you have any other plugins installed on the server?
     
  14. Offline

    Pizza371

    Minecrell
    Code:java
    1. package me.nalo.randomizeme;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. import java.util.Random;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Material;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.inventory.InventoryClickEvent;
    17. import org.bukkit.event.player.PlayerDropItemEvent;
    18. import org.bukkit.event.player.PlayerQuitEvent;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22. public class Main extends JavaPlugin implements Listener {
    23. private final List<String> randomizeEnabled = new ArrayList<String>();
    24. private final HashMap<String, ItemStack[]> armourcontents = new HashMap<String, ItemStack[]>();
    25. private final Material[] leggings = new Material[] {
    26. Material.DIAMOND_LEGGINGS, Material.IRON_LEGGINGS, Material.IRON_LEGGINGS, Material.GOLD_LEGGINGS
    27. };
    28. private final Material[] chestplates = new Material[] {
    29. Material.DIAMOND_CHESTPLATE, Material.IRON_CHESTPLATE, Material.LEATHER_CHESTPLATE, Material.GOLD_CHESTPLATE
    30. };
    31. private final Material[] boots = new Material[] {
    32. Material.DIAMOND_BOOTS, Material.IRON_BOOTS, Material.LEATHER_BOOTS, Material.GOLD_BOOTS
    33. };
    34. private final String title = "[" + ChatColor.AQUA + "RandomizeMe" + ChatColor.WHITE + "] ";
    35. @Override
    36. public void onEnable() {
    37. getServer().getPluginManager().registerEvents(this, this);
    38. startRandomizePlayersSchedular();
    39. }
    40. @Override
    41. public void onDisable() {
    42. safeDisable();
    43. }
    44. private void startRandomizePlayersSchedular() {
    45. Bukkit.getScheduler().runTaskTimerAsynchronously(this, new Runnable() {
    46. Random rdm = new Random();
    47. int length = Material.values().length;
    48. @Override
    49. public void run() {
    50. for(Player p : Bukkit.getOnlinePlayers()) {
    51. if(isRandomizeEnabled(p)) {
    52. p.getInventory().setHelmet(new ItemStack(Material.values()[rdm.nextInt(length)]));
    53. p.getInventory().setChestplate(new ItemStack(chestplates[rdm.nextInt(chestplates.length)]));
    54. p.getInventory().setLeggings(new ItemStack(leggings[rdm.nextInt(leggings.length)]));
    55. p.getInventory().setBoots(new ItemStack(boots[rdm.nextInt(boots.length)]));
    56. }
    57. }
    58. }
    59. }, 0L, 5L);
    60. }
    61. private void safeDisable() {
    62. for(Player p : Bukkit.getOnlinePlayers()) {
    63. if(this.isRandomizeEnabled(p)) {
    64. toggleRandomizer(p);
    65. }
    66. }
    67. }
    68. private void toggleRandomizer(Player p) {
    69. if(this.isRandomizeEnabled(p)) {
    70. p.sendMessage(this.title + "Randomize toggled [" + ChatColor.DARK_RED + "OFF" + ChatColor.WHITE + "]");
    71. this.randomizeEnabled.remove(p.getName());
    72. if(this.armourcontents.containsKey(p.getName())) {
    73. p.getInventory().setArmorContents(this.armourcontents.get(p.getName()));
    74. this.armourcontents.remove(p.getName());
    75. }
    76. } else {
    77. p.sendMessage(this.title + "Randomize toggled [" + ChatColor.GREEN + "ON" + ChatColor.WHITE + "]");
    78. this.randomizeEnabled.add(p.getName());
    79. this.armourcontents.put(p.getName(), p.getInventory().getArmorContents());
    80. }
    81. }
    82. private boolean isRandomizeEnabled(Player p) {
    83. if(this.randomizeEnabled.contains(p.getName())) return true;
    84. return false;
    85. }
    86. public boolean onCommand(CommandSender s, Command cmd, String cmdname, String[] args) {
    87. if(cmdname.equalsIgnoreCase("randomizeme")) {
    88. if(!(s instanceof Player)) return true;
    89. Player p = (Player) s;
    90. if(p.hasPermission("RandomizeMe.use") || p.isOp()) {
    91. this.toggleRandomizer(p);
    92. } else {
    93. p.sendMessage(this.title + "No permission!");
    94. }
    95. }
    96. return false;
    97. }
    98. @EventHandler
    99. public void onLeave(PlayerQuitEvent e) {
    100. if(this.isRandomizeEnabled(e.getPlayer())) this.toggleRandomizer(e.getPlayer());
    101. }
    102. @EventHandler
    103. public void onClick(InventoryClickEvent e) {
    104. if(this.isRandomizeEnabled((Player)e.getWhoClicked())) {
    105. ((Player)e.getWhoClicked()).sendMessage(this.title + "You cannot modify your inventory while Randomize is enabled!");
    106. e.setCancelled(true);
    107. }
    108. }
    109. public void onDrop(PlayerDropItemEvent e) {
    110. e.setCancelled(true);
    111. }
    112. }

    it's proably a stupid mistake then knowing me :c
     
  15. Offline

    Minecrell

    Pizza371
    The second one (onDrop) doesn't have the @EventHandler annotation. :p
     
    Pizza371 likes this.
  16. Offline

    Pizza371

    Minecrell *epic facepalm* sorry :(
    I thought it was working because of the onClick method above it (when I dragged it out it must've lagged and went back as if onDrop cought it, thanks!..
    :L..
     
    Minecrell likes this.
Thread Status:
Not open for further replies.

Share This Page