Solved Can't get arraylist to work the way I want

Discussion in 'Plugin Development' started by TheLovelySlothMan, Oct 19, 2016.

Thread Status:
Not open for further replies.
  1. So I've been messing with this quite a bit, but in the end I can't get it to work.

    Unfortunately, there are no errors popping up I can show.
    What I wanted was, when the player name gets stored in the arraylist, I want the player to be able to move items around in the inventory.

    When I type /invedit, it all works fine, and it knows that I am stored in the array. But when I try to move my items, it's like it doesn't check if I'm in the array.
    The command works fine. It knows when the player is stored and when the player isn't stored. The same can't be said for the event though.

    Here is the class.
    Code:java
    1. public class AdminEditTools implements Listener, CommandExecutor{
    2.  
    3. private ArrayList<String> admin = new ArrayList<String>();
    4.  
    5. public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
    6. Player p = (Player)sender;
    7. if (alias.equalsIgnoreCase("invedit")) {
    8. if (p.hasPermission("YangFu.invedit")) {
    9. if (!admin.contains(p.getName())) {
    10. p.sendMessage("§aInventory edit has been enabled");
    11. admin.add(p.getName());
    12. return true;
    13. }
    14. else if (admin.contains(p.getName()));
    15. p.sendMessage("§cInventory edit has been disabled");
    16. admin.remove(p.getName());
    17. return true;
    18. }
    19. }
    20. return true;
    21. }
    22.  
    23. @EventHandler
    24. public void onItemClick(InventoryClickEvent e) {
    25. Player p = (Player)e.getWhoClicked();
    26. if (!admin.contains(p.getName())) {
    27. e.setCancelled(true);
    28. return;
    29. }
    30. }
    31. }

    Thanks in advance. Dearly, TheLovelySloth.

    Edit:
    Okay I tweaked a few things, and did it the way Zombie_Striker told me. It got fixed! Woohoo! :3 You learn something new everyday :p
     
    Last edited: Oct 20, 2016
  2. Offline

    Zombie_Striker

    @TheLovelySlothMan
    Most likely, you are registering two instance of the class. Do tell me, do you have code that looks like this?:
    Code:
    getCommand("...").setExecutor(New AdminEdit...);
    ...
    Bukkit.getPluginManager.registerEvents(New AdminEdit...., this);
    
    Notice how you have to create two instance of AdminEdit'. What that means is the command will not interact with the events, because they are two separate objects.

    To fix this, make sure they are the same instance by using the following:
    Code:
    AdminEdit adminEdit = new AdminEdit;
    
    getCommand("...").setExecutor(adminEdit);
    ...
    Bukkit.getPluginManager.registerEvents(adminEdit, this);
    
     
    TheLovelySlothMan likes this.
Thread Status:
Not open for further replies.

Share This Page