Solved Player Specific Scoreboard

Discussion in 'Plugin Development' started by robertcarr22, Jul 31, 2013.

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

    robertcarr22

    Im a very new to writing plugins and im writing one for an events server im making.
    My idea is that every player has a score and for it to show on the side bar but to only show their own score.
    I used the code from this code to try and implement it but im getting errors in the command executor I wrote to set/add/remove your score.

    Error:
    Code:
    31.07 12:57:10 [Server] INFO ... 15 more
    31.07 12:57:10 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    31.07 12:57:10 [Server] INFO at io.github.robertcarr22.incepticraft.iscoreCommandExecutor.onCommand(iscoreCommandExecutor.java:45)
    31.07 12:57:10 [Server] INFO Caused by: java.lang.NullPointerException
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:30)
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:116)
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296)
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:837)
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.java:880)
    31.07 12:57:10 [Server] INFO at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerConnection.java:962)
    31.07 12:57:10 [Server] INFO at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServer.java:523)
    31.07 12:57:10 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
    31.07 12:57:10 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    31.07 12:57:10 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'iscore' in plugin Incepticraft v0.1
    iscoreCommandExecutor.java
    Code:
    package io.github.robertcarr22.incepticraft;
     
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.scoreboard.DisplaySlot;
     
    public class iscoreCommandExecutor implements CommandExecutor {
       
        public iscoreCommandExecutor(Incepticraft plugin){
            //this.plugin = plugin;
        }
        public Incepticraft plugin;
        public scoreboard scoreboard;
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
           
            String player = sender.getName();
           
           
           
            if(!sender.hasPermission("incepticraft.score")){
                sender.sendMessage("You Do Not Have Permission!");
                return false;
            }
           
            if(args.length < 3){
                sender.sendMessage("Not Enough Arguments!");
                return false;
            }
           
            int value;
            try {
                value = Integer.parseInt(args[2]);
            } catch ( Exception e) {
                sender.sendMessage("Value Must Be a Number!");
                return false;
            }
           
            if (cmd.getName().equalsIgnoreCase("iscore")) {
                sender.sendMessage(args[0] +", "+ args[1] +", "+args[2]);
                if (args[0].equalsIgnoreCase("set")){
                    scoreboard.changeSingle(player, DisplaySlot.SIDEBAR,  Bukkit.getOfflinePlayer("Points:"), value);
                    return true;
                }else if (args[0].equalsIgnoreCase("add")){
                    int configValue = plugin.getConfig().getInt("Scores.players." + player);
                    value = configValue += value;
                    scoreboard.changeSingle(player, DisplaySlot.SIDEBAR,  Bukkit.getOfflinePlayer("Points:"), value);       
                    return true;
                }else if (args[0].equalsIgnoreCase("remove")){
                    int configValue = plugin.getConfig().getInt("Scores.players." + player);
                    value = configValue -= value;
                    scoreboard.changeSingle(player, DisplaySlot.SIDEBAR,  Bukkit.getOfflinePlayer("Points:"), value);
                    return true;
                }       
            }
           
            return false;
               
           
           
           
        }
     
       
     
    }
    
    scoreboard.java
    Code:
    package io.github.robertcarr22.incepticraft;
     
    import java.util.HashMap;
    import java.util.Map;
     
    import org.bukkit.Bukkit;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
     
    public class scoreboard{
        private Incepticraft plugin;
       
        private Map<String, Scoreboard> playerScoreboards = new HashMap<String, Scoreboard>();
       
       
        public void register(Player player) {
            Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
            Objective side = scoreboard.registerNewObjective("side", "dummy");
           
            int points = plugin.getConfig().getInt("Scores.players." + player.getName());
            Score score = side.getScore(Bukkit.getOfflinePlayer("Points:"));
            score.setScore(points);
           
            side.setDisplaySlot(DisplaySlot.SIDEBAR);
            side.setDisplayName("Your Score");
       
       
            playerScoreboards.put(player.getName(), scoreboard);
            player.setScoreboard(scoreboard);
        }
       
       
        public void unregister(String player) {
            playerScoreboards.remove(player);
        }
       
       
       
        public void changeSingle(String player, DisplaySlot slot, OfflinePlayer scoreName, int value ) {
            if(playerScoreboards.containsKey(player)) {
                Scoreboard scoreboard = playerScoreboards.get(player);
                scoreboard.getObjective(slot).getScore(scoreName).setScore(value);
                plugin.getConfig().set("Scores.players." + player, value);
        }
       
        }
       
     
    }
    
    Any help will be greatly appreciated! :)
     
  2. Offline

    xxMOxMOxx

    robertcarr22 "scoreboard" on line 15 of iscoreCommandExecutor.java should be "Scoreboard" I think
     
  3. Offline

    robertcarr22

    That is only referring to the scoreboard.java file,
    The error points to this line:
    Code:
     scoreboard.changeSingle(player, DisplaySlot.SIDEBAR,  Bukkit.getOfflinePlayer("Points:"), value);
    so i think it may be something wrong with this method in the scoreboard.java file but im not sure :/
     
  4. Offline

    rippin

    plugin is not initialized in scoreboard.java. Pass it in the constructor.
     
  5. Offline

    robertcarr22

    Is that this?:
    Code:
    private Incepticraft plugin;
     
  6. Offline

    rippin

    Yes.
     
  7. Offline

    robertcarr22

    rippin Already have that below
    Code:
    public class scoreboard {
    shameful bump :/

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

    xTrollxDudex

    robertcarr22
    You are not allowed to bump within 24 hours

    Furthermore, your scoreboard variable is not initialized
     
  9. Offline

    robertcarr22

    oh sorry i thought it was 12 hours :/
     
  10. Offline

    xTrollxDudex

    robertcarr22
    You still have not initialized the scoreboard
     
  11. Offline

    robertcarr22

    Sorry not sure how to do that :/ could i get an example?
     
  12. Offline

    xxMOxMOxx

    scoreboard = anything
     
    xTrollxDudex likes this.
  13. Offline

    robertcarr22

    xxMOxMOxx xTrollxDudex

    So if i want the scoreboard variable to point to the scoreboard class what do i do?
    Sorry for just asking but im fairly new to java
     
  14. Offline

    xTrollxDudex

    robertcarr22
    What "scoreboard class"? Like a scoreboard in another class?
     
  15. Offline

    robertcarr22

    the method in the scoreboard.class
     
Thread Status:
Not open for further replies.

Share This Page