Solved "Ghost" Players Not Working

Discussion in 'Plugin Development' started by Stigosaurus, Jun 14, 2013.

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

    Stigosaurus

    I am making a private ghost squadron plugin and have encountered a problem. "Ghost" players aren't working (meaning that players on the same team don't show up as a holographic version of the player). Here is my code:
    Code:java
    1. Scoreboard board = manager.getNewScoreboard();
    2. Objective objective = board.registerNewObjective("stats", "dummy");
    3. Team red = board.registerNewTeam("red");
    4. Team blue = board.registerNewTeam("blue");
    5.  
    6. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    7. objective.setDisplayName(ChatColor.GREEN + currentMap + ":");
    8.  
    9. red.setAllowFriendlyFire(false);
    10. red.setCanSeeFriendlyInvisibles(true);
    11.  
    12. blue.setAllowFriendlyFire(false);
    13. blue.setCanSeeFriendlyInvisibles(true);
    14.  
    15. for(Player player : getPlayersOnRed()){
    16. red.addPlayer(player);
    17. }
    18.  
    19. for(Player player : getPlayersOnBlue()){
    20. blue.addPlayer(player);
    21. }
    22.  
    23. Score redTeamScore = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_RED + "Red Team Size"));
    24. Score blueTeamScore = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_BLUE + "Blue Team Size"));
    25.  
    26. redTeamScore.setScore(red.getPlayers().size());
    27. blueTeamScore.setScore(blue.getPlayers().size());
    28.  
    29. setScoreboard(board);

    I have added debug messages. The scoreboard on the sidebar shows up, and I have added debug messages and found out that players successfully get added to their respective teams, but they don't show up as ghosts.
     
  2. Offline

    TheTinySpider

    If I recall correctly, the Ghost only shows up when the guy you are looking at has invisibility and is in the same team with setCanSeeFriendlyInvisibles(true), so try adding invisibility to your ally
     
  3. Offline

    Stigosaurus

    Players should also be able to see themselves as transparent when in F5 or in the survival inventory. Anyways, I tried adding two players into the same team, but they still appear invisible to each other. If this helps, when I start the game, I call the code I posted above (except it is inside an update() method) and then give them the invisibility effect in that order. Anyone know how I can fix this?

    Anyone? I have looked over my code several times and can't find any errors...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  4. Offline

    Quidam

    Could you post your getPlayersOnRed and getPlayersOnBlue method?
     
  5. Offline

    Stigosaurus

    I have added debug messages and know they are getting added properly.
    Code:java
    1. public List<Player> getPlayersOnRed(){
    2. List<Player> playersOnTeam = new ArrayList<Player>();
    3.  
    4. for(String playerName : onRed){
    5. Player player = Bukkit.getPlayerExact(playerName);
    6. playersOnTeam.add(player);
    7. }
    8.  
    9. return playersOnTeam;
    10. }


    Anyone know what I'm doing wrong? Comphenix you may know because you did the GhostManager thing with the Bukkit API.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  6. Offline

    nitrousspark

    you should try clearing the list when your done with it

    Code:
    public List<Player> getPlayersOnRed(){
            List<Player> playersOnTeam = new ArrayList<Player>();
     
                for(String playerName : onRed){
                    Player player = Bukkit.getPlayerExact(playerName);
                    playersOnTeam.add(player);
                }
     
            return playersOnTeam;
            playersOnTeam.clear();
        }
     
  7. Offline

    chasechocolate

    nitrousspark you can't do anything after a return statement.
     
    ZachBora and Stigosaurus like this.
  8. Offline

    Stigosaurus

    Anyone? I have no idea what I am doing wrong lol. I looked over chasechocolate's tutorial and still can't find anything wrong.

    Bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  9. Offline

    Stigosaurus

    Bump.

    Anyone? Comphenix chasechocolate

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  10. Offline

    Jake0oo0

    Stigosaurus Don't store Player values in a list or hashmap, use their name then getPlayerExact(name);
     
  11. Offline

    Stigosaurus

    I'm not.
    My "onRed" variable is a List<String>.

    Bump. What am I doing wrong lol

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  12. Offline

    lycano

    Stigosaurus you store the player object. You should never store the Player object in any object. If you need to know what player is in a team you most likely only want to know the players name (String).

    Whenever you need additional information from that player like the location you should create a getPlayer(String playerName) method that will call Bukkit.getPlayerExact(playerName) .. check if that player is still online and return the player object.

    And before you say again "Im not storing player object" you do :D Checkout your code .. line 2,3,6,7.

    Your code
    Code:java
    1.  
    2. public List<Player> getPlayersOnRed(){
    3. List<Player> playersOnTeam = new ArrayList<Player>();
    4.  
    5. for(String playerName : onRed) {
    6. Player player = Bukkit.getPlayerExact(playerName);
    7. playersOnTeam.add(player);
    8. }
    9.  
    10. return playersOnTeam;
    11. }
    12.  


    how it should be (example for team red)
    Code:java
    1.  
    2. protected String List<String> playersOnRed = new ArrayList<String>();
    3.  
    4. public List<String> getPlayersOnRed(){
    5. return this.playersOnRed;
    6. }
    7.  
    8. public boolean isPlayerOnRed(Player player) {
    9. return this.isPlayerOnRed(player.getName());
    10. }
    11.  
    12. public boolean isPlayerOnRed(String playerName) {
    13. return this.playersOnRed.contains(playerName);
    14. }
    15.  
    16. public void addPlayerToRed(Player player) {
    17. this.addPlayerToRed(player.getName());
    18. }
    19.  
    20. public void addPlayerToRed(String playerName) {
    21. this.playersOnRed.add(playerName);
    22. }
    23.  
    24. public void removePlayerFromRed(Player player) {
    25. this.removePlayerFromRed(player.getName());
    26. }
    27.  
    28. public void removePlayerFromRed(String playerName) {
    29. this.playersOnRed.remove(playerName);
    30. }
    31.  


    If you dont want to expose your playerOnRed variable to other member classes (force availability via getPlayersOnRed()) then set the object to private instead of protected.

    Dublicate the above code for blue team and adapt your code.

    Note: I overloaded add and remove methods so that you could use the player object for convenience as you can now include your "isOnline checking and stuff" into your class instead using it over and over when you need to add or remove players.

    Also make sure that you dont call the manager them from different threads as you will run into a ConcurrentModificationException (short CME) when you try to remove an object form an array.

    If you want to store additional values to the player object i would rewrite the class to make use of hashmaps.

    Something like
    Code:java
    1.  
    2. Map<String, TeamAssignment> playerData = New HashMap<String, TeamAssignment>();
    3.  
    4. /**
    5. The Key as String is the playersName.
    6. TeamAssignment is interface that describes basic data for a team.
    7.  
    8. You would need an "assignTeam" and "removeFromTeam" method that can put a player to the correct team.
    9.  
    10. TeamRed extends TeamAssignment ...
    11. TeamBlue extends TeamAssignment...
    12. **/
    13.  
    14. public void assignToRed(String playerName) {
    15. this.playerData.put(playerName, new TeamRed());
    16. }
    17.  


    Code:java
    1.  
    2. public abstract class TeamAssignment {
    3. protected String teamName;
    4.  
    5. public TeamAssignment(String teamName) {
    6. this.teamName = teamName;
    7. }
    8.  
    9. public String getTeamName()
    10. return this.teamName;
    11. }
    12. }
    13.  
    14. public class TeamRed extends TeamAssignment {
    15. public TeamRed() {
    16. super("Red");
    17. }
    18. }
    19.  


    After that you can use public TeamAssignment getTeam(String playerName); and do yourTeamManagerClass.getTeam("SomePlayer").getTeamName(); to output "Red".

    To code it super clean you could use the Decorator Design Pattern if its applicable to your problem.

    Written from mind (untested) But i think this should work. If someone else see a problem here please correct me.
     
    Stigosaurus likes this.
  13. Offline

    AmShaegar

    Could you list all plugins currently enabled on your server please? I had the same case some days ago and it was another plugin that caused the issue.
     
  14. Offline

    Stigosaurus

    Essentials, essentials spawn, WorldEdit and this one.
     
  15. Offline

    Jake0oo0

    Stigosaurus likes this.
  16. Offline

    Stigosaurus

Thread Status:
Not open for further replies.

Share This Page