Running some functions without an event (or what event to use)?

Discussion in 'Plugin Development' started by nicho96, Sep 22, 2012.

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

    nicho96

    Basically what I'm trying to do is give a player some potion effects when they are added to an array list, and keeping the effects as long as they are in the array. I can't find any event that would allow me to do this, so is there a way to do this without actually using an event? Or if not, what event should I use?

    Thanks for any help ^^
     
  2. Offline

    Kodfod

    just do a repeating task every x seconds using a for loop to add the effect to each person in the list.


    Edit: Just be sure to remove them when they logout, or the server stops.
     
  3. Offline

    JazzaG

    Unless I'm stupid, I'm not seeing a reason to use an event here. Where you add the player to the ArrayList, you give the player the potion effect, and where you remove them, you remove the potion effect.
     
  4. Offline

    nicho96

    Thanks Kodfod , I'll take a crack at that

    JazzaG the problem with that is that there is more than one way to put the player into the array. I want to effects to be given to the player once they are in the array for simplicities sake, and to make my life much easier in the future.
     
  5. Offline

    Kodfod

    Well, now that i have thought about it, you may need to just add it once if i'm not mistaken so @JazzaG's idea for addign when you add them to the list and remove when you remove from teh group is better =P
     
  6. Offline

    JazzaG

    Okay...check that the player is already in the ArrayList before you put them in it?
    Code:
    public void putPlayerInArrayList(Player player) {
        String name = player.getDisplayName();
        if(arraylist.contains(name))
            return; // Don't do anything if player is already in arraylist
       
        arraylist.add(name);
        
        // TODO: give player potion effects
    }
    
     
  7. Offline

    nicho96

    JazzaG I've already made the check to make sure Player's weren't added into the array twice for each scenario where a player could be added.

    I think I failed to explain myself properly. What I want the plugin to do is to check if the player is in the array list, and then add the respective potions. I could make it so that the PlayerJoinEvent checks if they are in the list, but it would be nice to have it so that the players don't need to log out and in to get the effects.

    My idea is rather complex, not going to lie, but needs to be done a specific way or it'll cause issues in the future.

    Alright, noticed a massive flaw in my planning. Going to have to reconstruct a large portion of my code xD Thanks to both of you for the help.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  8. I see that you don't really need this anymore, but I'm still saying my opinion, in case somebody finds this thread.

    My idea consists of two parts:
    1. Do not access the array directly (array.somefunction() ). Create functions like addToArray(), removeFromArray(), etc., and use them instead
    2. In the end of every function that somehow alters the array, add your code that checks for the players and adds/removes the corresponding effects.

    This is pretty much what JazzaG said. You said that there is more than one way to access the array. For every way, create a wrapper-function that updates the effects in the end
     
Thread Status:
Not open for further replies.

Share This Page