getScore() of everyone ? HOW ?!

Discussion in 'Plugin Development' started by Sw_aG, Dec 29, 2019.

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

    Sw_aG

    Hello fellow developers !

    I'm trying to make a calculator for the score of all the players in a list.
    The list itself is a team, (BlueTeam) and the scores are in a HashMap<Player, Integer>.
    I've tried this:
    PHP:
    for(Player allblue blueteam) {
                
    int blueteamscore score.get(allblue);
    msg(sender"blue teams score: " blueteamscore);
    }
    But this returns me null, I've searched everywhere online and didn't find anything.

    I think I need to calculate everybody's own score in that list and + them all together, but can't find a way to.
    Thanks in advance for all the helpers !
     
    Last edited: Dec 29, 2019
  2. Offline

    KarimAKL

    @Sw_aG Could you show us the output?
    An int can't be null.
    Also, if you want to add them all together do something like this:
    Code:Java
    1. int blueteamscore = 0;
    2. for (Player allblue : blueteam) blueteamscore += score.get(allblue);
    3. msg(sender, "blue teams score: " + blueteamscore);
     
  3. Offline

    Sw_aG

    @KarimAKL Gonna try that right now !
    Thank you for the reply

    The message sends me itself the amount of times theres players on that team and it sends every player's score separately instead of combinding them.
    Lets say there's 3 people on blue team, it will send me this:
    I want it to send only 1 message which says how much is the score of all the blueteam combined
     
    Last edited: Dec 30, 2019
  4. Offline

    travja

    @Sw_aG Did you get @KarimAKL's code to work? If so, you can mark the thread as Solved!
     
  5. Offline

    KarimAKL

    That's what your original code does.

    The code i sent you does that, try it out, and then try to understand it.
     
  6. Offline

    Sw_aG

    That's what I said,I've tried it and it doesn't work the way it should.
    as I said, it sends me each player in that team's score separately, if there are 10 players on that team, it will send me 10 messages with each of their score.
    Lets say we have 3 players on that team.
    first player score is 10
    second player score is 7
    third player score is 9
    I'll get this messages:

    Code:
    blue teams score: 10
    blue teams score: 7
    blue teams score: 9
    Imagine having a 100 players on that team - that would mean 100 messages.

    What I'm trying to do is to get this message:
    Code:
    blue teams score: 26
    @KarimAKL I did try it before sending that message, that what I refer to.
    The code you sent sends X messages, X is the amount of players on that team with each message a player's own score, not everyone's combined.
     
    Last edited: Dec 31, 2019
  7. Offline

    KarimAKL

    @Sw_aG Could you show us your current code?
     
    Sw_aG likes this.
  8. Offline

    Sw_aG

    PHP:
    private int getTeamsScore(List team) {
       
            
    int score 0;

            if(
    team.equals(redteam)) {
                for(
    Player allred redteam) {
                    
    score += score.get(allred);
                    }
                }
       
            if(
    team.equals(blueteam)) {
                for(
    Player allblue blueteam) {
                    
    score += score.get(allblue);
                    }
                }
            return 
    score;
        }
     
    Last edited: Dec 31, 2019
  9. Offline

    KarimAKL

    @Sw_aG That code should be causing compilation errors and warnings, is that your actual code?

    1. Using the raw type "java.util.List" should be giving you a warning.
    2. You are calling a method called "get" on an int; primitive types don't have any methods, that should cause an error.
     
  10. Offline

    Sw_aG

    Thanks for replying!
    This works perfectly with everything else I need, be it a string, a double, everything.

    I just changed the method to this:
    PHP:
        private int getWholeTeamBlueScore() {
          
            
    int score 0;
          
              
            for(
    Player allblue blueteam) {
                
    score += scores.get(allblue);
              
                }
          
            return 
    score;
        }
    and it still does the same, multiple messages.
    Besides that, what do you suggest ?
     
  11. Offline

    KarimAKL

    @Sw_aG That code never sends any messages, show us the code where you are calling the method.
     
    Sw_aG likes this.
  12. Offline

    Sw_aG


    Thanks for replying !

    Code:
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender instanceof Player) {
       
          Player p = (Player)sender;
    
    
       
          if (p.getName().equalsIgnoreCase("scores")) {
            if (p.isOp())
            {
    p.sendMessage(ChatColor.BLUE +"Blue team's score: " + getWholeTeamBlueScore());
    p.sendMessage(ChatColor.RED + "Red team's score: " + getWholeTeamRedScore());
    }
    }
    No offense, but it seems like you don't know
     
    Last edited: Dec 31, 2019
  13. Offline

    KarimAKL

    @Sw_aG You're only sending the message once, are you sure you updated the jar in the server's plugins folder?
    The only explanation for the messages getting sent more than once is if you're sending messages inside of a loop.
     
    Sw_aG likes this.
  14. Offline

    Sw_aG

    @KarimAKL Yeah I did, I don't have it called from any loop.
    I actually tested it, and if I do have even 9 players it sends it exactly 9 times.
    My previous code didn't send me anything, I understand now it was all over the place, but the "+" seems to not do the purpose either.
     
  15. Offline

    KarimAKL

    @Sw_aG You must have a loop somewhere, i can't think of any other explanation. Look through your own code, or post it all here, otherwise we can't really help.
     
    Sw_aG likes this.
  16. Offline

    Sw_aG

    @KarimAKL If there would be a loop, it would just send it repeatedly.
    I've tested it with a different amount of player in that team to confirm its the amount of messages.
    That's the only code related to it.
    But thanks for trying, I really appreciate it !
     
    Last edited: Dec 31, 2019
  17. Offline

    timtower Moderator Moderator

    @Sw_aG Please post all related classes in full.
     
  18. Offline

    Sw_aG

    I don't know what new information you'll get out of this, but here:

    PHP:
    package MainPackage;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class 
    MainClass extends JavaPlugin implements Listener {
       
       
          private List<
    Playerredteam = new ArrayList<>();
          private List<
    Playerblueteam = new ArrayList<>();
       
          private 
    HashMap<PlayerIntegerscores = new HashMap<PlayerInteger>();
       
       
       
          public 
    void onEnable() {
           
              
    Bukkit.getServer().getPluginManager().registerEvents(thisthis);
              
    System.out.print("this plugin is on ! Yaayy       ");
           
          }
       
       
          public 
    void onDisable() {
           
              
    System.out.print("Plugin is disabled *GOODBYE* !");
          }
       
       
       
       
       
       
       
       
          public 
    boolean onCommand(CommandSender senderCommand cmdString labelString[] args) {
                if (
    sender instanceof Player) {
            
                  
    Player p = (Playersender;

               
               
                  if (
    cmd.getName().equalsIgnoreCase("setscore")) {
                   
                        if (
    p.isOp()) {
                         
                        if(
    args.length == 0) {
                         
                            
    msg(p"Select how much score you want to have !");
                            
    msg(p"Usage: /setscore [Integer]");
                         
                        }else if (
    args.length 0)
                            {
                         
                            if(
    isInt(args[0])) {
                             
                                
    int newscore Integer.parseInt(args[0]);
                             
                                
    scores.put(pnewscore);
                                
    msg(p"Your score is now: " ChatColor.GREEN args[0]);
                                
    msg(p"Double check, your new score is: " scores.get(p));
                             
                            }else {
                                
    msg(p"Use a number !");
                            }
                         
                         
                            }
                         
                         
                            }
                     
                     
                          }else
               
               
                  if (
    cmd.getName().equalsIgnoreCase("scores")) {
                   
                    if (
    p.isOp()) {
                     
                        if(!
    blueteam.isEmpty() && !redteam.isEmpty() && !scores.isEmpty()) {
                     
            
    p.sendMessage(ChatColor.BLUE +"Blue team's score: " getWholeTeamBlueScore());
            
    p.sendMessage(ChatColor.RED "Red team's score: " getWholeTeamRedScore());
         
                        }
                      }
                  }
               
                  else
                       
                       
                        if (
    cmd.getName().equalsIgnoreCase("chooseteam")) {
                         
                            if (!
    p.isOp())
                            {
                              
    msg(pChatColor.DARK_RED "BananaJoe says: NO!");


                         
                            }
                            else if (
    args.length == 0)
                            {
                              
    msg(p"Usage:" ChatColor.AQUA " /chooseteam [red/blue]");
                              
    msg(p"Pick a team !");
                         
                            }
                            else if (
    args.length 0)
                            {
                             
                              if (
    args[0].equalsIgnoreCase("red")) {
                             
                                if (!
    redteam.contains(p)) {
                                 
                                  
    blueteam.remove(p);
                                  
    redteam.add(p);
                               
                                  
    msg(p"You're now in" ChatColor.RED " red " "team");
                                } else {
                                  
    msg(p"You're already in red team");
                                }
                           
                              } else if (
    args[0].equalsIgnoreCase("blue")) {
                             
                                if (!
    blueteam.contains(p)) {
                                 
                                  
    redteam.remove(p);
                                  
    blueteam.add(p);
                                  
    msg(p"You're now in" ChatColor.BLUE " blue " "team");
                                } else {
                                  
    msg(p"You're already in blue team");
                                }
                           
                              }


                         
                            }

                       
                          }
     
               
               
             
                }
                return 
    false;
          }
       
       
       
       
       
          private 
    void msg(Player pString msg) {
           
              
    p.sendMessage(msg);
        }



          private 
    int getWholeTeamBlueScore() {
           
                
    int score 0;
           
               
                for(
    Player allblue blueteam) {
                    
    score += scores.get(allblue);
               
                    }
           
                return 
    score;
            }
       
       
       
          private 
    int getWholeTeamRedScore() {
           
                
    int score 0;
           
               
                for(
    Player allred redteam) {
                    
    score += scores.get(allred);
               
                    }
           
                return 
    score;
            }
       
       
       
          public 
    void ConsoleMessage(String ConsoleMessage) {
       
          
    System.out.print(ConsoleMessage);
       
        }
       
       
          private 
    boolean isInt(String s) {
                try {
                  
    Integer.parseInt(s);
                } catch (
    NumberFormatException nfe) {
                  return 
    false;
                }
                return 
    true;
              }
       
       
       
       
    }
    I did register everything in the plugin.yml
    I've also tried to make a constructor for the score, but got the same result.
    Can you please help me figure out the problem ?
     
    Last edited: Dec 31, 2019
  19. Offline

    bwfcwalshy Retired Staff

    There are multiple improvements which can be made to the code so I'm going to list everything. This may also solve your problem so just fix them all.

    1.
    You should use a HashSet and also UUID. You don't ever want dupes and the lookup is quicker in a hashtable.
    private final Set<UUID> blueTeam = new HashSet<>();
    You should also make this final as it is a constant.

    2.
    Should also be final and use UUID rather than Player.

    3. You shouldn't use one command handler for multiple commands. Use different classes. This creates much cleaner and easier to maintain code.

    4.
    You should use permissions rather than just op.

    5. You run isInt which does parsing but then you do it again. Just do
    Code:
    int i = getInt(args[0]);
    if (i == -1) {
        msg(p, "Invalid input");
        return true;
    }
    In the getInt, if the try works return that otherwise in the catch return -1.

    6.
    You don't need to do contains. Can do like if (redteams.add(p.getUniqueId()) { msg(p, "you're now on red") }

    7.
    Naming conventions! camelCase not PascalCase.

    Also,
    use println not print.

    8. The biggest thing of all. Please format your code. The formatting is kinda all over. Your IDE should have an auto-formatting thing. Just press the keybind! In IntelliJ it's Ctrl+Alt+L
     
  20. Offline

    travja

    @Sw_aG Just looking over your code, nothing should be sending that many messages. Could you check the jar that you are using and make sure that you are using a version of the jar with the updated code. If you're having compiling errors that you aren't seeing, it may just still be the same old jar as before.
     
    Sw_aG likes this.
  21. Offline

    Sw_aG




    Really ?
    all of this and not even a single hint about whats causing the problem, you only negged about things that do work !
    If you don't believe these things work either way, test them, I'm not causing issues for myself on purpose obviously..


    Thank you for replying, I know you don't see a reason why it would, I don't see either.
    I did tested it on a different test server I have, deleted everything, changed the messages to differ from the last jar version.
    I'm using the correct Bukkit version and everything, trust me.
    Even try the code for yourself


    I gave up on this post, no one but Karim actually tried to help me, just neg about my current code
    You can close it, I don't want it anymore.
    So many waitings for nothing, if you don't like to help me, don't reply.
    See it as UNSOLVED, but there's no prefix for that XD
    For those who'll think I wanted to be spoon fed, LOL !
     
    Last edited: Dec 31, 2019
  22. Offline

    bwfcwalshy Retired Staff

    @Sw_aG Never once it doesn't work. These are inefficient, bad practices or just generally bad (readability wise, etc). Fix these, then I'll help with the actual problem. Right now it's hard to even properly read the code considering it isn't formatting correctly.

    Again, fix all these (which will take 2 mins) then get back to me with the updated code.

    Edit: Also, the double-post, the bot will fix it.
     
  23. Offline

    timtower Moderator Moderator

    Because everything is unsolved. That is the way this forum works.

    For the actual problem:
    The UUID change might fix the issue though. Player objects are not really steady objects.
    Same reason why you don't use them for long term storage, memory leaks.
    Please just try it.
    And otherwise: print the values in the map, print the members in the list, check if the members in the list also exist in the map at all.
     
Thread Status:
Not open for further replies.

Share This Page