[Solved] How to set a player variable?

Discussion in 'Plugin Development' started by spy_1134, Jun 24, 2012.

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

    spy_1134

    I'm currently working on making my first plugin and I'm not very experienced with Bukkit, although I know a bit of Java.

    I'm trying to make a command listener set a variable that is checked by another listener.
    The variable will be set on a per-player basis, similar to how some players can have god mode while others do not.

    So my question is this:
    Is there any way to set a variable on a player? If so, how is it done?
    Or am I trying to approach this problem from the wrong direction?

    The plugin I am making will listen for a command, check permissions, and if the player has permission to use the command, it will set a variable on the sender or specified target of the command.
    Then, when the player triggers another listener, it will check the variable to see whether or not it should do anything.

    Thanks in advance,
    spy_1134
     
  2. Offline

    Darq

    A HashMap is probably what you'd want to use. A HashMap is a set of keys and values, so you can use Player as a key, and whichever you want as a value, depends what you'd like to do.

    Example of using a hashmap:
    Code:
    HashMap<Player, Boolean> hm = new HashMap<Player, Boolean>();
     
    public boolean someMethod(Player p) {
      if (hm.containsKey(p)) {
          return hm.get(p);
      }
      return false;
    }
    
    Though, in that case since I'm only checking a boolean, you could use a List object to do the same thing. Having a list of players that have a special condition, and when they lose that condition, you can just remove them from the list.
     
  3. Offline

    spy_1134

    Thanks for the reply. I also found a page in the API documentation for setting metadata on an object. It looks like that could be used to store this data as well.

    Maybe something like this?
    Code:
    player.setMetadata(myvariable, true)
     
  4. Offline

    Darq

    Give it a try, never used the method. :)
     
  5. Offline

    x3chaos

    What exactly do you plan on doing with this variable? Depending on the circumstances I would use a HashMap like Darq said.
    For example, if the variable is to broadcast a specific message for elevated users on login, I would do this:

    Code:
    public class GreetUsers extends JavaPlugin implements Listener {
     
        Server server = Bukkit.getServer();
        HashMap<String, Boolean> greet;
     
        public void onEnable() {
            this.greet = new HashMap<String, Boolean>();
            server.getPluginManager().registerEvents(this, this);
        }
     
        @EventHandler
        public void onPlayerLogin(PlayerLoginEvent event) {
            if (greet.containsKey(event.getPlayer().getName())) {
                if (greet.get(event.getPlayer().getName())) {
                    server.broadcastMessage("THIS IS A MESSAGE");
                }
            }
        }
    }
    Or something like that. You can label the HashMap as the variable, modifying it as
    Code:
    HashMap<String, Boolean> var; 
    I suggest not using the Player object itself in whatever you use because the player's name would serve a better purpose. Just use
    Code:
    Bukkit.getServer().getPlayer(string name)
    to get that player.

    EDITS: Derp, fixing my pseudo-code like a BAWSS
     
    ferrybig likes this.
  6. Offline

    spy_1134

    The data being stored would be these two things:
    A Boolean: if true then the player listener acts, if false then nothing happens
    A string: type of action to preform when certain events are fired

    EDIT: I forgot to mention that the string would be set per player as well, so that when two players with different values fire the same event, two different things happen.
     
  7. A HashMap<String, String> would work just fine, player name and player's event thing, if you want nothing to happen, just remove the key or set the value to null instead of making another one with boolean.
     
Thread Status:
Not open for further replies.

Share This Page