Solved Toggle Scoreboard by command

Discussion in 'Plugin Development' started by Cro007, May 11, 2014.

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

    Cro007

    Hello people. I have an issue here.
    I tried to make toggleable scoreboard with command, but every time I get error out.

    I put in player.setScoreboard(board) and player.setScoreboard(manager.getNewScoreboard())
    but didn't worked. Am I missing something?
    here is bit of code that is making issues:

    Code:java
    1. public void togglePluginState(Player player){
    2.  
    3. if(ee.containsKey(player)){
    4. if(ee.get(player)){
    5. ee.put(player, false);
    6. player.sendMessage(prefix + "Scoreboard disabled");
    7. player.setScoreboard(null);
    8. } else {
    9. ee.put(player, true);
    10. player.sendMessage(prefix + "Scoreboard enabled");
    11. player.setScoreboard(board);
    12.  
    13. }
    14. } else {
    15. ee.put(player, true);
    16. player.sendMessage(prefix + "Scoreboard enabled");
    17. player.setScoreboard(board);
    18. }
    19.  
    20. }
    21.  
    22.  
    23.  
    24. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    25. {
    26. Player player1 = (Player)sender;
    27.  
    28. if (commandLabel.equalsIgnoreCase("ecotoggleon"))
    29. {
    30. Player player = (Player) sender;
    31. togglePluginState(player);
    32. return true;
    33. }
    34. }


    bump

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

    Drkmaster83

    The way you're doing this is unnecessary. You don't need to use a HashMap for something with an on and off state.
    Code:
    ArrayList<UUID> enabled = new ArrayList<UUID>();
     
    public void togglePluginState(Player p) {
        if(enabled.contains(p.getUniqueId()) {
            enabled.remove(p.getUniqueId());
            p.setScoreboard(getServer().getScoreboardManager().getNewScoreboard());
            p.sendMessage("\u00A7cScoreboard disabled!");
        }
        else {
            enabled.add(p.getUniqueId());
            p.setScoreboard(board);
            p.sendMessage("\u00A7aScoreboard enabled!");
        }
    }
    
    Edit: Also, don't force-cast a player to the CommandSender object in your onCommand:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        if(!(sender instanceof Player)) return false;
        Player player = (Player) sender;
     
        if(commandLabel.equalsIgnoreCase("ecotoggle") {
            togglePluginState(player);
        }
    }
     
    Cro007 likes this.
  3. Offline

    JoeyDevs

    Why do u use '\u00A7c' and not:
    Code:java
    1. p.sendMessage(ChatColor.RED + "...");
     
  4. Offline

    Drkmaster83

    I prefer it over concatenating the ChatColor for it, you can use either one, but that's just the unicode value for it.
     
  5. Offline

    JoeyDevs

    I never use the UniCode one only in the Motde from server.properties but oke it's fine
     
  6. Offline

    Cro007

    I put the code but I get this error:
    Code:
    [10:34:01 INFO]: Cro007 issued server command: /ecotoggle
    [10:34:01 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ecot
    oggle' in plugin EcoRepairPlus v0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:17
    5) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServe
    r.java:683) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:952) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.IllegalArgumentException: Scoreboard cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbu
    kkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer.setScoreboard(Craft
    Player.java:1181) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at me.Cro007.item.main.togglePluginState(main.java:148) ~[?:?]
            at me.Cro007.item.main.onCommand(main.java:163) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            ... 13 more
    [10:34:03 INFO]: Cro007 issued server command: /ecotoggle
    Line 46 is:
    Code:java
    1. public static main plugin;



    Code:
    [10:36:22 ERROR]: Could not pass event PlayerJoinEvent to EcoRepairPlus v0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:320) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:471) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerList.c(PlayerList.java:225) [craft
    bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerList.a(PlayerList.java:116) [craft
    bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.LoginListener.c(LoginListener.java:78) [
    craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.LoginListener.a(LoginListener.java:42) [
    craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:149
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.IllegalArgumentException: Scoreboard cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbu
    kkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer.setScoreboard(Craft
    Player.java:1181) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at me.Cro007.item.main.onPlayerJoin(main.java:118) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:318) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            ... 14 more
    and code that is messing this up:
    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent e) {
    3. Player p = e.getPlayer();
    4.  
    5. p.setScoreboard(board);
    6.  
    7. if (scores.get(p) == null) scores.put(p, o.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.GREEN + "Hits Left:")));
    8. }


    Thanks for great help btw
     
  7. Offline

    Drkmaster83

    Is 'board' declared or made? You need to make the Scoreboard object to set it.
     
  8. Offline

    AdamQpzm

    Drkmaster83 It has to be declared, he wouldn't get so far otherwise.

    Cro007 You need to initialise the board variable, because it's null when you try to do p.setScoreboard(board);
     
  9. Offline

    viper_monster

    Cro007 You have to initialize the plugin variable

    Code:java
    1. private static Main instance;
    2.  
    3. @Override
    4. public void onEnable() {
    5. instance = this;
    6. }
    7.  
    8. @Override
    9. public void onDisable() {
    10. instance = null;
    11. }
    12.  
    13. public static Main getInstance() {
    14. return instance;
    15. }


    ps. Izgleda da ima dosta Hrvata na ovim forumima :p
     
  10. Offline

    AdamQpzm

  11. Offline

    viper_monster

    AdamQpzm are you sure, cause he put this in on of his posts:
    [​IMG]
    it looks like he didn't initialize the plugin variable
     
  12. Offline

    AdamQpzm

    spoljo666 You can't tell that from the one line he posted. And yes, check the stack trace. Line 46 refers to line 46 in a different class. Don't trust people to read stack traces correctly :p
     
    spoljo666 likes this.
  13. Offline

    Drkmaster83

    You'd be surprised, some people will put un-declared variable names into their code and expect it to work. I was simply generalizing my question so that the answer could give me a general idea.
     
  14. Offline

    AdamQpzm

    Drkmaster83 Yes, but very few of them convince the compiler to let it slide.
     
  15. Offline

    Cro007

    Thanks for all your help. I got it fixed.
    I rewritten all of scoreboard code, implemented toggle command and now it works.
    I had some loops in code and errors, but finally fixed it.. Thanks Drkmaster83, and others, who tried to help, but issue was on my end
     
Thread Status:
Not open for further replies.

Share This Page