Selecting Teammate Method

Discussion in 'Spigot Plugin Help' started by ThatBraxGuy, May 15, 2017.

  1. So I'm in the process of writing a plugin that changes a compass' direction to point toward their teammate (there will only be two on the team). I understand the idea of using `setCompassTarget(Location loc)` but what I'm a little fuzzy on is how to get `Location` to equal the teammate's location. I'm thinking the best way is to use a method, maybe `getDisplayName()` and then locate their location using that? Again, I'm not sure what the best plan of attack is. If anyone has any insight I'd appreciate it. Haven't developed a plugin in a few years so I'm really fuzzy on the code.
     
  2. You can either store the Player instance for the teammates and just call p.getLocation() and update it periodically or store their names and do Bukkit.getPlayer("player1") and get the location from that. I do suggest checking null on the player instances if you do save them or the names because the player could leave etc.
     
  3. Would it be best to do that on a per player basis? I need every player to have a compass so would it be easier to store all the variables inside of a call like when they right/left click the compass and have it do it's thing then?
     
  4. I would put in in a runnable or in a move listener so that the compass gets updated frequently as if you just set the location once and the player moves it will not be updated to the players location.
     
  5. Thanks man. Any guides you could point me to? I'm still really rusty in that department.
     
  6. I would start with a map or something alike
    Code (Text):
    Map<Player, Player> teammates = new HashMap<Player, Player>();
    then i would make a move listener
    Code (Text):
    public void PlayerMoveListener(PlayerMoveEvent event){
        Player player1 = event.getPlayer(); //the player moving
    }
    from there I would grab the teammate and update his compass
    Code (Text):
    Player player2 = teammates.get(player1); //get the teammate
    now since you have both players what you would do is update the compass of the teammate and not the player moving
    Code (Text):
    player2.setCompassTarget(player1.getLocation());
    this now would update the teammates compass to point toward the player whenever he moves

    this is how you would add and remove players from the list
    Code (Text):
    teammates.put(player1, player2);
    teammates.remove(player1 or player2);
    here is the code put together
    Code (Text):
    Map<Player, Player> teammates = new HashMap<Player, Player>();

    @EventHandler
    public void PlayerMoveListener(PlayerMoveEvent event){
        Player player1 = event.getPlayer(); //the player moving
        Player player2 = teammates.get(player1); //the teammate
        player2.setCompassTarget(player1.getLocation()); //setting the compass
    }
     
    Like I said before though: use proper error handling and always check null ;)
     
    #6 proferabg, May 15, 2017
    Last edited: May 15, 2017
  7. This is fantastic! Thank you so much. I'll definitely keep the advice in mind. Thanks for the well written tutorial! So now all that would have to happen is get the teammates into the hashmap together?
     
  8. Yes and you only need to store one entry set for each team so you dont need to add (player1, player2) then (player2, player1) just one set for each team.