Solved Toggling booleans for another player

Discussion in 'Plugin Development' started by jetp250, Mar 23, 2016.

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

    jetp250

    Hey! I've been working on a simple nopvp plugin lately. It has gone well until I got a problem; how do I toggle a boolean (from false to true) for another player? Been googling around for a hour now, I'd say.
    Here's the piece of code that I have problems with:
    Code:
    Player target = Bukkit.getPlayerExact(args[0]);
    target.toggle = false;
    ^ the 'toggle' gets underlined.

    and here's the boolean:
    Code:
    public boolean toggle = false;
    As you know, by doing string.variable calls the 'string' part from another class, right? like, now it'd try to get a variable 'toggle' from a class called 'target'. I don't want that. How can I set the 'toggle' variable for 'target' to 'true'? :l

    EDIT: didn't tell what's this for.. well, I want an option to toggle the nopvp for other players, like /nopvp <player>. Sorry for that :p


    EDIT 2: Ok, I got it solved, thanks everyone! I'm using now arrayLists, and checking if the arraylist contains the player. Got some really helpful replies down there, if someone else got the same problem, do what they said below, it worked perfectly.
     
    Last edited: Mar 27, 2016
  2. Offline

    Zombie_Striker

    @jetp250
    The player field does not contain a "toggle" field. You are most likely referring to another object that has the "toggle" field. Get the object associated with the player that also has your custom "toggle" field, and set toggle equal to !toggle (which means the reverse of the toggle field)
     
  3. Offline

    jetp250

    Hm? How would I do it in code? Confused :l
     
  4. Offline

    Zombie_Striker

    @jetp250
    Well, since there is no "toggle" field for the Player class, I'm assuming you created your own custom class that stores this "toggle" field. Is this correct? If so, you need to change what toggle is equal to for that class, not for the player object.
     
  5. Offline

    clapynick

    The toggle method is not part of the player class. Meaning that you cannot use .toggle unless you created that method.
    I would suggest looking up how to do a toggle command. Essentially what you have to do is create a list of all the players that you want pvp turned off for. This means that whenever a player is put into nopvp mode using the /nopvp <player> you would add them into the list. And when they are taken out of pvp mode I assume you do /pvp <player> or maybe even /nopvp player again (You didn't state how the player turns pvp back on), then you would take them out of the list.

    To do this the first thing you have to do is create a list in your Main class. (If you only have one class put it at the top of your code, also your main class is the class with the onEnable method).
    Creating a list
    Code:java
    1.  
    2. List<String> nopvp = new ArrayList<String>();
    3.  


    If you have multiple classes such as an your event class a class to handle your commands then you would want to create the list by doing this:
    Code:java
    1.  
    2. public static List<String> nopvp = new ArrayList<String>();
    3.  

    Then whenever you want to access this list you can do
    Code:java
    1.  
    2. // Main is the name of the class that contains the List we created above. Yours may be something different depending on what you named it.
    3. Main.nopvp
    4.  

    Then whenever you do the command of /nopvp <player> you do this code. (This is what you currently trying to do player.toggle = false):
    Code:java
    1.  
    2. // Remember if you are using multiple class files you would have to do Main.nopvp.add(player.getName());
    3. nopvp.add(player.getName());
    4.  

    or you could do this if you have no further use for the player object. (This means you wouldn't need Player target = Bukkit.getPlayerExact(args[0]);)
    Code:java
    1.  
    2. nopvp.add(args[0]);
    3.  


    Then whenever you run the event that handles pvp (you probably are using the EntityDamageByEntityEvent event) you would check if the player activating the event is in pvp mode, or if the other player that is being attacked is in pvp mode.
    You can use this using the contains method on our list we made.
    Code:java
    1.  
    2. if(nopvp.contains(/*<player name of event activator or person being attacked goes here>*/)){
    3. // Do code... (You would probably have to do something like event.setCancelled(true); To stop the pvp event)
    4. }
    5.  


    Then if you want to take a player out of pvp mode you can do the following:
    Code:java
    1.  
    2. nopvp.remove(player.getName());
    3.  

    This will remove the player out of the list.

    Hope this helps :)
     
    Last edited: Mar 24, 2016
    jetp250 likes this.
  6. Offline

    Lordloss

    @clapynick If he doesnt know how to use an ArrayList, hes not in the right place here anyway. Better link to a good documentation instead of explaining the very basics of java. Maybe he asks this question and tries to access an non existent field in the Player object, because he got something totally wrong with OOP. If so, your explanation of an ArrayList is like a drop in the bucket.
     
  7. Offline

    jetp250

    Sorry, haven't been able to read the posts, but now I will :D
    @clapynick definitely trying that out right now, and yea, I'm not used to arraylists and those, kinda new to java.

    EDIT: I got the toggle working, but I'll have to re-write the previous toggle to get it working :) I'll be back if it doesn't work, but thanks!
     
    Last edited: Mar 24, 2016
  8. Offline

    mcdorli

    Please, learn java before diving into bukkit
     
    Zombie_Striker likes this.
  9. Offline

    jetp250

    Okay, the bukkit forums finally let me to log in, sorry, solved it ages ago :D Thanks, the ArrayList worked, now marking thread as solved :)
     
  10. Offline

    clapynick

    Ok, thanks for the advice.
     
Thread Status:
Not open for further replies.

Share This Page