Solved Event/ArrayList, what is it!?

Discussion in 'Plugin Development' started by Synapz, Aug 10, 2015.

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

    Synapz

    I am updating my plugin, and when I got to the /god command I encountered a strange error. Not really an error but something I have no idea why isn't working.

    How it works:
    When a player types in /god, they are added/removed from the godPlayers array list based. When they are in the godPlayers array list their damage event will be canceled so they don't die and are invincible. However, whats happening is they are still getting damage when they toggled /god and it's enabled.

    Here is the code:
    Code:
    public class CommandGod extends AdminEssentialsCommand implements ConsoleCommand, Listener {
    
        private static ArrayList<String> godPlayers = new ArrayList();
    
        private void god(CommandSender sender, Player target) {
            String action;
            if (godPlayers.contains(target.getName())) {
                godPlayers.remove(target.getName());
                action = RED + "disabled";
            } else {
                godPlayers.add(target.getName());
                action = RED + "enabled";
            }
            target.sendMessage(GOLD + "God mode " + action);
            Utils.sendSenderMessage(sender, target, GOLD + "God mode " + action + GOLD + " for " + RED + target.getName());
        }
    
        public void onCommand(Player player, String[] args) {
            Player target = args.length == 0 ? player : player.getServer().getPlayer(args[0]);
            if (args.length == 1 && !Utils.isPlayerOnline(player, args[0])) {
                return;
            }
            god(player, target);
        }
    
        public void onConsoleCommand(CommandSender sender, String[] args) {
            Player target = sender.getServer().getPlayer(args[0]);
            if (Utils.isPlayerOnline(sender, args[0])) {
                god(sender, target);
            }
        }
    
        public String getName() {
            return "god";
        }
    
        public ArrayList<String> getPermissions() {
            ArrayList<String> permissions = new ArrayList<>();
            permissions.add("adminessentials.god 0");
            permissions.add("adminessentials.god.others 1");
            return permissions;
        }
    
        public ArrayList<Integer> handledArgs() {
            return Utils.makeArgs(0, 1);
        }
    
        public ArrayList<Integer> consoleHandledArgs() {
            return Utils.makeArgs(1);
        }
    
        public String[] getArguments() {
            return new String[] {"<player>"};
        }
    
        @EventHandler
        public void onPlayerMove(EntityDamageEvent event) {
            if ((event.getEntity() instanceof Player)) {
                System.out.println("Test one");
                Player player = (Player)event.getEntity();
                if (godPlayers.contains(player.getName())) {
                    System.out.println("Test two");
                    event.setCancelled(true);
                }
            }
        }
    }
    Pay the most attention to the god method and the EnetityDamageEvent listener.

    "Hey, maybe you haven't registered your listener!"
    > Yes, I have. To prove this in console it prints out "Test 1" ("Test 2" isn't printing out at all)

    "Try to remove the static in ArrayList"
    > I tried, still hasn't working.

    "Use player#getUniqueID"
    > I already did that, I also tried to .toString() it. Still is not working... (I'm going to put it back to store UUIDs once I figure out why this isn't working)

    I feel like this is a simple fix... But I've tried everything and it still... isn't working.
     
    Last edited: Aug 11, 2015
  2. "Hey, you probably used a reeeeeeeaaaaaaaaally old version of bukkit because the 'new' method is"
    > public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {}

    "Hey, why do you implement so many things?"
    > Just implement CommandExecutor (and Listener if you use events) and register the command in onEnable with getCommand("god").setExecutor(yourCommandExecutorClassInstance);
     
  3. Offline

    Synapz

    It's called Object-Oriented programming.

    It's not a old version of bukkit... It's a different method I made..I know how to use and implement CommandExecutor classes and there is a reason why I didn't. Either way the issue isn't solved.
     
    Last edited: Aug 10, 2015
  4. Offline

    CoolDude53

    Instead of outputting "test1", output the ArrayList to see if anything is actually in it. If there is nothing in it, try relocating your declaration of the ArrayList to another class like your Main one for posterity's sake. Post back with what you find from that.
     
  5. Offline

    mythbusterma

    Laughed heartily, great post, my friend.

    Post your main class and the contents of the ArrayList when you move.
     
    FisheyLP, Synapz and teej107 like this.
  6. Offline

    Synapz

    @mythbusterma Of course there are exceptions to that. I actually went through everything I can think of and it made sense because I don't want each instance of the class to have a different godPlayers list. I also just resolved the issue, so should I remove the static? I always see people posting code with statics everywhere just so they can access methods and variables, with people telling them not to use static. I read how to use them/when, I'm still not 100% sure, should I remove static or keep it?

    @CoolDude53 and @mythbusterma, thanks a lot because as I was moving it to another class, I realized this is what I had...
    Code:
    private static ArrayList<String> godPlayers = new ArrayList();
    Just an empty ArrayList with no type specified and when I changed it to this...
    Code:
    private static ArrayList<String> godPlayers = new ArrayList<>();
    It worked.

    Thanks everyone
     
  7. Offline

    CoolDude53

    The principle behind static objects is that they are loaded and associated with the class, not an object of that class, allowing them to be accessed just by referencing the class. Since you are attempting to go full object oriented, using statics is not encouraged. They have their uses, especially in larger projects and in constants, but I will let you determine what you think is best.
     
    FisheyLP and Synapz like this.
Thread Status:
Not open for further replies.

Share This Page