Util ScoreboardManager - Sidebar handling made easier!

Discussion in 'Resources' started by megamichiel, Jul 4, 2015.

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

    megamichiel

    Hello everyone, a while back I made a small Scoreboard utility that gives everyone a unique sidebar to show their stats on while staying on the same scoreboard.

    How does it work?
    When enabled, ScoreboardManager registers a new bukkit Scoreboard instance and saves it in a field. Using custom packet handling ScoreboardManager shows different sidebar scores for everyone while having the players use the same Scoreboard. Using this library, you don't have to register each team for the scoreboard of every player online, saving you a bit of memory when a lot of players are online and lots of teams are modified.

    Usage
    Put Scoreboard.java from below somewhere in your package. You can access the ScoreboardManager class by calling Scoreboard.getManager(). There are 3 things you need to do to get the scoreboard up and running:
    1. When your plugin enables, call ScoreboardManager#start() to setup the scoreboard
    2. When a player joins, call ScoreboardManager#setupBoard(player) to give the player a scoreboard instance
    3. By calling ScoreboardManager#getScoreboard(player) you get a Scoreboard instance of the player, where you can edit stuff. When a player quits call Scoreboard#unregister() so it gets removed from the memory.
    To modify a player's sidebar, there are a couple of methods:
    • setScores(List<String>) OR setScores(String[] scores)
      • Sets a player's scores to the specified scores, it will show up in descending order
    • setHeader(String)
      • Set a player's header (the sidebar's displayname on top)
    • update()
      • Call this when you are done with modifying the stuff and are ready to show the updates to the player. If the new scores are the same size of the old scores, the method changes only the score names that have changed, to prevent flickering.
    Source Code
    For the util to work, you need to create a class called Scoreboard, and put this code in:
    http://pastebin.com/tbaxsq8F

    Example

    Show Spoiler
    Code:
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
      Player p = event.getPlayer();
      final Scoreboard board = Scoreboard.getManager().setupBoard(p);
      board.setHeader(ChatColor.GOLD + p.getName()).update();
      new BukkitRunnable() {
        int time = 4;
        @Override
        public void run() {
          time--;
          String[] scores;
          if(time == 0) {
            cancel();
            scores = {ChatColor.RED + "Welcome to the server!"};
          } else {
            scores = {ChatColor.GRAY + "Counting down...", ChatColor.GREEN + "  " + time};
          }
          board.setScores(scores).update();
        }
      }.runTaskTimer(<main plugin>, 5L, 20L);
    }


    Notes
    • On player join you might need to put a little delay on join before setting the scores for them to show up.
    • Should be compitable with most versions, if not report the stacktrace.
     
    Last edited: Jul 11, 2015
  2. Offline

    Krumb069

    Good but can you write an example with List<> and put a screenshot?
     
    Last edited: Jul 4, 2015
  3. Offline

    KingFaris11

    How do you remove a player's scoreboard? I haven't implemented this into my plugin but I'd like to, but my plugin has arenas and only players in arenas have scoreboards along with nametags.

    @megamichiel Are you even following this post...? -_-

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  4. Offline

    megamichiel

    @KingFaris11
    Oh sorry, I don't always have time. You can enable the scoreboard by calling ScoreboardManager#setupBoard(player), and disable it by calling unregister() on the player's scoreboard. That should do what you want.
    @Krumb069
    List<String> isn't so hard to do. You can just create an arraylist, add some elements to it and call setScores(list)
    I'll post some screenshots
     
  5. Offline

    KingFaris11

    Thanks =) I was just wondering because I see you helping around on the forums a lot, so I found it a bit weird not to reply to this thread...
     
  6. Offline

    Krumb069

    Last edited: Jul 9, 2015
  7. Offline

    megamichiel

    @Krumb069
    I tried to make it version compitable, but not all classes have the same name. Does it throw any errors in console?
     
  8. Offline

    Krumb069

    Edit:1.7 supported fixed on new code
     
    Last edited: Jul 9, 2015
  9. Offline

    DeathZonePT

    i have a error, i try 1.8_R1 and 1.8_R2

    1.8_R3
    Code:
    [09:10:07 INFO]: [DeathZone] Enabling DeathZone v2.1.3
    [09:10:07 INFO]: [DeathZone] DeathZone is running on NMS version v1_8_R2
    [09:10:07 INFO]: [DeathZone] Actionbar setup was successful!
    [09:10:07 INFO]: [DeathZone] Tab list setup was successful!
    [09:10:07 INFO]: [DeathZone] Title's setup was successful!
    [09:10:07 INFO]: [DeathZone] The plugin setup process is complete!
    [09:10:07 INFO]: Server permissions file permissions.yml is empty, ignoring it
    [09:10:07 INFO]: Done (3,625s)! For help, type "help" or "?"
    [09:10:18 INFO]: UUID of player jack_Gaming_PT is f8126972-e983-3da5-873b-ce35c7
    8e5c7b
    [09:10:19 ERROR]: Could not pass event PlayerJoinEvent to DeathZone v2.1.3
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:305) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.PlayerList.onPlayerJoin(PlayerList.java:
    296) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.PlayerList.a(PlayerList.java:156) [spigo
    t.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.LoginListener.b(LoginListener.java:144)
    [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.LoginListener.c(LoginListener.java:54) [
    spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.NetworkManager.a(NetworkManager.java:231
    ) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.ServerConnection.c(ServerConnection.java
    :148) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:8
    09) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.IllegalArgumentException: Scoreboard cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.
    jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.craftbukkit.v1_8_R2.entity.CraftPlayer.setScoreboard(Craft
    Player.java:1240) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at com.DeathZone.Scoreboard.<init>(Scoreboard.java:44) ~[?:?]
            at com.DeathZone.Scoreboard.<init>(Scoreboard.java:41) ~[?:?]
            at com.DeathZone.Scoreboard$ScoreboardManager.setupBoard(Scoreboard.java
    :173) ~[?:?]
            at com.DeathZone.Listener.ScoreBoardListener.onJoin(ScoreBoardListener.j
    ava:49) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _45]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _45]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_45]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            ... 14 more
    [09:10:19 INFO]: jack_Gaming_PT[/127.0.0.1:49190] logged in with entity id 0 at
    ([world]-1214.5, 3.0, 158.5)
    1.8_R1
    Code:
    [09:03:50] [Server thread/INFO]: [DeathZone] Enabling DeathZone v2.1.3
    [09:03:51] [Server thread/INFO]: [DeathZone] DeathZone is running on NMS version v1_8_R1
    [09:03:51] [Server thread/INFO]: [DeathZone] Actionbar setup was successful!
    [09:03:51] [Server thread/INFO]: [DeathZone] Tab list setup was successful!
    [09:03:51] [Server thread/INFO]: [DeathZone] Title's setup was successful!
    [09:03:51] [Server thread/INFO]: [DeathZone] The plugin setup process is complete!
    [09:03:51] [Server thread/INFO]: Server permissions file permissions.yml is empty, ignoring it
    [09:03:51] [Server thread/INFO]: CONSOLE: [0;32;1mReload complete.[m
    [09:03:54] [Server thread/INFO]: jack_Gaming_PT lost connection: Disconnected
    [09:03:55] [Server thread/INFO]:  jack_Gaming_PT has leave the server
    [09:04:02] [User Authenticator #2/INFO]: UUID of player jack_Gaming_PT is f8126972-e983-3da5-873b-ce35c78e5c7b
    [09:04:02] [Server thread/ERROR]: Could not pass event PlayerJoinEvent to DeathZone v2.1.3
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot.jar:git-Spigot-550ebac-7019900]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-550ebac-7019900]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-550ebac-7019900]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-550ebac-7019900]
        at net.minecraft.server.v1_8_R1.PlayerList.onPlayerJoin(PlayerList.java:272) [spigot.jar:git-Spigot-550ebac-7019900]
        at net.minecraft.server.v1_8_R1.PlayerList.a(PlayerList.java:156) [spigot.jar:git-Spigot-550ebac-7019900]
        at net.minecraft.server.v1_8_R1.LoginListener.b(LoginListener.java:109) [spigot.jar:git-Spigot-550ebac-7019900]
        at net.minecraft.server.v1_8_R1.LoginListener.c(LoginListener.java:41) [spigot.jar:git-Spigot-550ebac-7019900]
        at net.minecraft.server.v1_8_R1.NetworkManager.a(NetworkManager.java:159) [spigot.jar:git-Spigot-550ebac-7019900]
        at net.minecraft.server.v1_8_R1.ServerConnection.c(ServerConnection.java:82) [spigot.jar:git-Spigot-550ebac-7019900]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:800) [spigot.jar:git-Spigot-550ebac-7019900]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-550ebac-7019900]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot.jar:git-Spigot-550ebac-7019900]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot.jar:git-Spigot-550ebac-7019900]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.IllegalArgumentException: Scoreboard cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.jar:git-Spigot-550ebac-7019900]
        at org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer.setScoreboard(CraftPlayer.java:1240) ~[spigot.jar:git-Spigot-550ebac-7019900]
        at com.DeathZone.Scoreboard.<init>(Scoreboard.java:44) ~[?:?]
        at com.DeathZone.Scoreboard.<init>(Scoreboard.java:41) ~[?:?]
        at com.DeathZone.Scoreboard$ScoreboardManager.setupBoard(Scoreboard.java:173) ~[?:?]
        at com.DeathZone.Listener.ScoreBoardListener.onJoin(ScoreBoardListener.java:49) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot.jar:git-Spigot-550ebac-7019900]
        ... 14 more
    [09:04:02] [Server thread/INFO]: jack_Gaming_PT[/127.0.0.1:49179] logged in with entity id 1 at ([world] -1214.5, 3.0, 158.5)
    class
    Code:
    package com.DeathZone.Listener;
    
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import com.DeathZone.Core;
    import com.DeathZone.Scoreboard;
    
    public class ScoreBoardListener implements Listener {
      
        private Core plugin;
      
        public ScoreBoardListener(Core plugin) {
            this.plugin = plugin;
        }
      
      
        private static final List<String> frames = Arrays.asList(
                "§4§lDeathZone",
                "§c§lD§4§leathZone",
                "§f§lD§c§le§4§lathZone",
                "§c§lD§f§le§c§la§4§lthZone",
                "§4§lD§c§le§f§la§c§lt§4§lhZone",
                "§4§lDe§c§la§f§lt§c§lh§4§lZone",
                "§4§lDea§c§lt§f§lh§c§lZ§4§lone",
                "§4§lDeat§c§lh§f§lZ§c§lo§4§lne",
                "§4§lDeath§c§lZ§f§lo§c§ln§4§le",
                "§4§lDeathZ§c§lo§f§ln§c§le",
                "§4§lDeathZo§c§ln§f§le",
                "§4§lDeathZon§c§le",
                "§4§lDeathZone");
    
        private static final Map<String, Integer> taskIds = new HashMap<>();
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = (Player) e.getPlayer();
          
            final Scoreboard board = Scoreboard.getManager().setupBoard(p);
          
            if (board != null) {
              
                board.setHeader(frames.get(0)).update();
                board.setScores(new String[] {"", "§7Player:§a", p.getName(), "", "§7Online:§a" + Bukkit.getOnlineMode(), "", "§7Website:", "§ahttp://deathzone.ml"}).update();
              
              
                taskIds.put(p.getName(), new BukkitRunnable() {
                    int frame = 0;
    
                    public void run() {
                        if (frame == frames.size()) {
                            frame = 0;
                        }
                        frame++;
                        board.setHeader(frames.get(frame)).update();
                    }
                }.runTaskTimer(plugin, 0L, 10L).getTaskId());
            }
        }
    
        @EventHandler
        public void onQuit(PlayerQuitEvent e) {
            Integer id = taskIds.remove(e.getPlayer().getName());
            if (id != null) {
                plugin.getServer().getScheduler().cancelTask(id);
            }
        }
    }
    EDIT:

    i add Scoreboard#getManager()#start(); in onEnable()

    this is now the error

    Code:
    [09:25:58 INFO]: [DeathZone] Enabling DeathZone v2.1.3
    [09:25:58 INFO]: [DeathZone] DeathZone is running on NMS version v1_8_R2
    [09:25:58 INFO]: [DeathZone] Actionbar setup was successful!
    [09:25:58 INFO]: [DeathZone] Tab list setup was successful!
    [09:25:58 INFO]: [DeathZone] Title's setup was successful!
    [09:25:58 INFO]: [DeathZone] The plugin setup process is complete!
    [09:25:58 INFO]: Server permissions file permissions.yml is empty, ignoring it
    [09:25:59 INFO]: Done (3,434s)! For help, type "help" or "?"
    [09:26:06 INFO]: UUID of player jack_Gaming_PT is f8126972-e983-3da5-873b-ce35c7
    8e5c7b
    [09:26:07 ERROR]: Could not pass event PlayerJoinEvent to DeathZone v2.1.3
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:305) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.PlayerList.onPlayerJoin(PlayerList.java:
    296) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.PlayerList.a(PlayerList.java:156) [spigo
    t.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.LoginListener.b(LoginListener.java:144)
    [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.LoginListener.c(LoginListener.java:54) [
    spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.NetworkManager.a(NetworkManager.java:231
    ) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.ServerConnection.c(ServerConnection.java
    :148) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:8
    09) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.ExceptionInInitializerError
            at com.DeathZone.Scoreboard.update(Scoreboard.java:132) ~[?:?]
            at com.DeathZone.Listener.ScoreBoardListener.onJoin(ScoreBoardListener.j
    ava:53) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _45]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _45]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_45]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            ... 14 more
    Caused by: java.lang.NumberFormatException: For input string: "raftServer"
            at java.lang.NumberFormatException.forInputString(Unknown Source) ~[?:1.
    8.0_45]
            at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_45]
            at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_45]
            at com.DeathZone.Scoreboard$ReflectUtil.<clinit>(Scoreboard.java:252) ~[
    ?:?]
            at com.DeathZone.Scoreboard.update(Scoreboard.java:132) ~[?:?]
            at com.DeathZone.Listener.ScoreBoardListener.onJoin(ScoreBoardListener.j
    ava:53) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _45]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _45]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_45]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            ... 14 more
    [09:26:07 INFO]: jack_Gaming_PT[/127.0.0.1:49588] logged in with entity id 0 at
    ([world]-1214.5, 3.0, 158.5)
     
    Last edited: Jul 7, 2015
  10. Offline

    megamichiel

    @DeathZonePT
    Ah, thank you. I found what the issue was. Updated the pastebin link which fixed the issue.
    @Krumb069
    Try using the updated link's source
     
    Krumb069 and DeathZonePT like this.
  11. Offline

    DeathZonePT

    now i have this erro help me pleae :D

    Code:
    [14:57:50 INFO]: [DeathZone] Enabling DeathZone v2.1.3
    [14:57:50 INFO]: [DeathZone] DeathZone is running on NMS version v1_8_R2
    [14:57:50 INFO]: [DeathZone] Actionbar setup was successful!
    [14:57:50 INFO]: [DeathZone] Tab list setup was successful!
    [14:57:50 INFO]: [DeathZone] Title's setup was successful!
    [14:57:50 INFO]: [DeathZone] The plugin setup process is complete!
    [14:57:50 INFO]: Server permissions file permissions.yml is empty, ignoring it
    [14:57:50 INFO]: CONSOLE: Reload complete.
    [14:57:55 INFO]: UUID of player jack_Gaming_PT is f8126972-e983-3da5-873b-ce35c7
    8e5c7b
    [14:57:55 INFO]: jack_Gaming_PT[/127.0.0.1:49310] logged in with entity id 2 at
    ([world]-1214.5, 3.0, 158.5)
    [14:57:57 WARN]: [DeathZone] Task #6 for DeathZone v2.1.3 generated an exception
    
    java.lang.ArrayIndexOutOfBoundsException: 13
            at java.util.Arrays$ArrayList.get(Unknown Source) ~[?:1.8.0_45]
            at com.DeathZone.Listener.ScoreBoardListener$1.run(ScoreBoardListener.ja
    va:65) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7
    18) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    [14:57:58 WARN]: [DeathZone] Task #6 for DeathZone v2.1.3 generated an exception
    
    java.lang.ArrayIndexOutOfBoundsException: 13
            at java.util.Arrays$ArrayList.get(Unknown Source) ~[?:1.8.0_45]
            at com.DeathZone.Listener.ScoreBoardListener$1.run(ScoreBoardListener.ja
    va:65) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7
    18) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    [14:57:59 WARN]: [DeathZone] Task #6 for DeathZone v2.1.3 generated an exception
    
    java.lang.ArrayIndexOutOfBoundsException: 13
            at java.util.Arrays$ArrayList.get(Unknown Source) ~[?:1.8.0_45]
            at com.DeathZone.Listener.ScoreBoardListener$1.run(ScoreBoardListener.ja
    va:65) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7
    18) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    [14:58:01 WARN]: [DeathZone] Task #6 for DeathZone v2.1.3 generated an exception
    
    java.lang.ArrayIndexOutOfBoundsException: 13
            at java.util.Arrays$ArrayList.get(Unknown Source) ~[?:1.8.0_45]
            at com.DeathZone.Listener.ScoreBoardListener$1.run(ScoreBoardListener.ja
    va:65) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7
    18) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    [14:58:02 WARN]: [DeathZone] Task #6 for DeathZone v2.1.3 generated an exception
    
    java.lang.ArrayIndexOutOfBoundsException: 13
            at java.util.Arrays$ArrayList.get(Unknown Source) ~[?:1.8.0_45]
            at com.DeathZone.Listener.ScoreBoardListener$1.run(ScoreBoardListener.ja
    va:65) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7
    18) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    [14:58:03 WARN]: [DeathZone] Task #6 for DeathZone v2.1.3 generated an exception
    
    java.lang.ArrayIndexOutOfBoundsException: 13
            at java.util.Arrays$ArrayList.get(Unknown Source) ~[?:1.8.0_45]
            at com.DeathZone.Listener.ScoreBoardListener$1.run(ScoreBoardListener.ja
    va:65) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7
    18) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    [14:58:05 WARN]: [DeathZone] Task #6 for DeathZone v2.1.3 generated an exception
    
    java.lang.ArrayIndexOutOfBoundsException: 13
            at java.util.Arrays$ArrayList.get(Unknown Source) ~[?:1.8.0_45]
            at com.DeathZone.Listener.ScoreBoardListener$1.run(ScoreBoardListener.ja
    va:65) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7
    18) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot.jar:git-Spigot-1d14d5f-7aaa833]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    [14:58:05 INFO]: jack_Gaming_PT lost connection: Disconnected
    [14:58:05 INFO]:  jack_Gaming_PT has leave the server
    >
    OBS: can you a option to add a blank line please.
     
    Last edited: Jul 7, 2015
  12. Offline

    megamichiel

    @DeathZonePT
    This appears to be an error on your side. Could you show me your ScoreBoardListener class?
     
  13. Offline

    DeathZonePT

    @megamichiel

    Code:
    package com.DeathZone.Listener;
    
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import com.DeathZone.Core;
    import com.DeathZone.Scoreboard;
    
    public class ScoreBoardListener implements Listener {
      
        private Core plugin;
      
        public ScoreBoardListener(Core plugin) {
            this.plugin = plugin;
        }
      
      
        private static final List<String> frames = Arrays.asList(
                "§4§lDeathZone",
                "§c§lD§4§leathZone",
                "§f§lD§c§le§4§lathZone",
                "§c§lD§f§le§c§la§4§lthZone",
                "§4§lD§c§le§f§la§c§lt§4§lhZone",
                "§4§lDe§c§la§f§lt§c§lh§4§lZone",
                "§4§lDea§c§lt§f§lh§c§lZ§4§lone",
                "§4§lDeat§c§lh§f§lZ§c§lo§4§lne",
                "§4§lDeath§c§lZ§f§lo§c§ln§4§le",
                "§4§lDeathZ§c§lo§f§ln§c§le",
                "§4§lDeathZo§c§ln§f§le",
                "§4§lDeathZon§c§le",
                "§4§lDeathZone");
    
        private static final Map<String, Integer> taskIds = new HashMap<>();
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
          
            Player p = (Player) e.getPlayer();
          
            List<String> score = Arrays.asList(
                    " ",
                    "§7Player:",
                    "§a" + p.getName(),
                    " ",
                    "§7Online:",
                    "§a" + Bukkit.getOnlinePlayers().size() + "/300",
                    " ",
                    "§7Website:",
                    "§ahttp://deathzone.ml");
          
            final Scoreboard board = Scoreboard.getManager().setupBoard(p);
          
            if (board != null) {
              
                board.setHeader(frames.get(0)).update();
                board.setScores(score).update();
              
              
                taskIds.put(p.getName(), new BukkitRunnable() {
                    int frame = 0;
    
                    public void run() {
                        if (frame == frames.size()) {
                            frame = 0;
                        }
                        frame++;
                        board.setHeader(frames.get(frame)).update();
                    }
                }.runTaskTimer(plugin, 0L, 2L).getTaskId());
            }
        }
    
        @EventHandler
        public void onQuit(PlayerQuitEvent e) {
          
            Integer id = taskIds.remove(e.getPlayer().getName());
            if (id != null) {
                plugin.getServer().getScheduler().cancelTask(id);
            }
        }
    }
     
  14. Offline

    megamichiel

    @DeathZonePT
    I see what's wrong. I think you made some changes in your code because the stacktrace doesn't match the place. In your run() method you're detecting if the frame is equal to the frames size, and after that increasing the frame. But if the frame is 1 below the size it doesn't set the frame to 0 and still increases so it throws an IndexOutOfBoundsException. put the frame++; line before you check if the frame is equal to the frames size, that should fix it.
     
  15. Offline

    DeathZonePT

    @megamichiel
    thx bro gonna test now. tell me how can i add a blank line?
     
  16. Offline

    megamichiel

    @DeathZonePT
    You can create blank lines by placing simply a chatcolor in the line, e.g.:
    Code:
    board.setScores(Arrays.asList("Line", "§a", "Another line", "§b"));
    Note that you can't have identical scores, I will make a workaround for it soon
     
  17. Offline

    DeathZonePT

    final work :D
    [​IMG]
    thx bro your the best :D
     
  18. Offline

    Krumb069

    Edit:1.7 supported fixed on new code

    Edit:1.7 supported fixed on new code

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  19. Offline

    DeathZonePT

    @Krumb069

    Try now
    String pkg = Bukkit.getServer().getClass().getPackage().getName();
     
  20. Offline

    Krumb069

    Edit:1.7 supported fixed on new code
     
    Last edited: Jul 9, 2015
  21. Offline

    DeathZonePT

    this is my class
    Code:
    package com.DeathZone;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.UUID;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Team;
    public class Scoreboard {
            static {
                    manager = new ScoreboardManager();
            }
          
            private static final ScoreboardManager manager;
            private static org.bukkit.scoreboard.Scoreboard board;
            private static Objective obj;
          
            /**
             * Returns the scoreboard manager
             * @return the scoreboard manager
             */
            public static ScoreboardManager getManager() {
                    return manager;
            }
          
            private final String name;
            private List<String> lastUpdate = new ArrayList<>();
            private List<String> scores = new ArrayList<>();
            private String header;
          
            private Scoreboard(Player p) {
                    name = p == null ? null : p.getName();
                    if(p != null) {
                            p.setScoreboard(board);
                            header = p.getName();
                    }
            }
          
            /**
             * Returns the scoreboard header of the player
             * @return the scoreboard header
             */
            public String getHeader() {
                    return header;
            }
          
            /**
             * Sets the header
             * @param header the new header
             * @return the scoreboard instance, useful for chaining actions
             */
            public Scoreboard setHeader(String header) {
                    this.header = header;
                    return this;
            }
          
            /**
             * Returns the scoreboard's player
             * @return the scoreboard's player
             */
            public Player getPlayer() {
                    return Bukkit.getPlayer(name);
            }
          
            /**
             * Returns the scores
             * @return the scores, where the keys are the score names and the values their score value
             */
            public List<String> getScores() {
                    return scores;
            }
          
            /**
             * Sets the scoreboard scores in descending order
             * @param scores the score names
             * @return the scoreboard instance
             */
            public Scoreboard setScores(List<String> scores) {
                    this.scores = scores;
                    return this;
            }
          
            /**
             * Sets the scoreboard scores in descending order, this method simply calls setScores(Arrays.asList(scores))
             * @param scores the score names
             * @return the scoreboard instance
             */
            public Scoreboard setScores(String... scores) {
                    return setScores(Arrays.asList(scores));
            }
          
            /**
             * Updates the scoreboard with the current scores and header
             * @return the scoreboard instance
             */
            public Scoreboard update() {
                    Player p = getPlayer();
                    if(p == null) return this;
                    if(scores.size() == lastUpdate.size()) {
                            int size = scores.size() - 1;
                            for(int i=0;i<=size;i++) {
                                    String entry = scores.get(i);
                                    String last = lastUpdate.get(i);
                                    if(!entry.equals(last)) {
                                            Object remove = manager.getRemovePacket(obj, last);
                                            Object add = manager.getChangePacket(obj, entry, size - i);
                                            ReflectUtil.sendPacket(p, remove, add);
                                    }
                            }
                    } else {
                            for(int i=0;i<lastUpdate.size();i++) {
                                    Object packet = manager.getRemovePacket(obj, lastUpdate.get(i));
                                    ReflectUtil.sendPacket(p, packet);
                            }
                            int size = scores.size() - 1;
                            for(int i=0;i<=size;i++) {
                                    Object packet = manager.getChangePacket(obj, scores.get(i), size-i);
                                    ReflectUtil.sendPacket(p, packet);
                            }
                    }
                    try {
                            Object packet = ReflectUtil.getNMSClass("PacketPlayOutScoreboardObjective").newInstance();
                            String[] keys = "a, b, c, d".split(", ");
                            Object[] values = {
                                    "Board", header, ReflectUtil.getField(ReflectUtil.getNMSClass("EnumScoreboardHealthDisplay"),
                                                    null, "INTEGER"), 2
                            };
                            for(int i=0;i<keys.length;i++)
                                    ReflectUtil.setField(packet.getClass(), packet, keys[i], values[i]);
                            ReflectUtil.sendPacket(p, packet);
                    } catch (Exception ex) {
                            ex.printStackTrace();
                    }
                    this.lastUpdate = scores;
                    return this;
            }
          
            /**
             * Unregisters the scoreboard from the system, this is called in a quit listener, don't call it urself :P
             */
            public void unregister() {
                    Player p = getPlayer();
                    if(p != null) {
                            manager.boards.remove(p.getUniqueId());
                            p.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
                    }
            }
          
            public static class ScoreboardManager {
                  
                    private final Map<UUID, Scoreboard> boards;
                  
                    private ScoreboardManager() {
                            boards = new HashMap<>();
                    }
                  
                    /**
                     * Sets a scoreboard up for the player, this is called in a join listener, use getScoreboard(Player) to get a player's board
                     * @param p the player to set the board for
                     * @return the newly created scoreboard instance
                     */
                    public Scoreboard setupBoard(Player p) {
                            Scoreboard board = new Scoreboard(p);
                            boards.put(p.getUniqueId(), board);
                            return board;
                    }
                  
                    /**
                     * Returns the player's scoreboard
                     * @param p the player's scoreboard
                     * @return the scoreboard of the player
                     */
                    public Scoreboard getScoreboard(Player p) {
                            return boards.get(p.getUniqueId());
                    }
                  
                    /**
                     * Gets the main scoreboard, use this to register/unregister/manipulate teams
                     * @return
                     */
                    public org.bukkit.scoreboard.Scoreboard getScoreboard() {
                            return board;
                    }
                  
                    /**
                     * Resets all teams
                     */
                    public void resetTeams() {
                            //Converting it to an array because when unregistering a team, it gets removed from the board's teams list,
                            //and even by calling Iterator#remove() it will throw a ConcurrentModificationException
                            Team[] teams = board.getTeams().toArray(new Team[0]);
                            for(Team team : teams) {
                                    team.unregister();
                            }
                    }
                  
                    private Object getChangePacket(Objective obj, String entry, int score) {
                            try {
                                    Object packet = ReflectUtil.getNMSClass("PacketPlayOutScoreboardScore").newInstance();
                                    String[] keys = "a, b, c, d".split(", ");
                                    Object[] values = {entry, obj.getName(), score,
                                                    ReflectUtil.getField(ReflectUtil.getNMSClass("EnumScoreboardAction"),
                                                                    null, "CHANGE")};
                                    for(int i=0;i<keys.length;i++)
                                            ReflectUtil.setField(packet.getClass(), packet, keys[i], values[i]);
                                    return packet;
                            } catch (Exception ex) {
                                    ex.printStackTrace();
                                    return null;
                            }
                    }
                  
                    private Object getRemovePacket(Objective obj, String entry) {
                            Object handle = ReflectUtil.getHandle(obj);
                            try {
                                    return ReflectUtil.getNMSClass("PacketPlayOutScoreboardScore")
                                                    .getConstructor(String.class, ReflectUtil.getNMSClass("ScoreboardObjective"))
                                                    .newInstance(entry, handle);
                            } catch (Exception ex) {
                                    ex.printStackTrace();
                                    return null;
                            }
                    }
                  
                    public void start() {
                            board = Bukkit.getScoreboardManager().getNewScoreboard();
                            obj = board.registerNewObjective("Board", "dummy");
                            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
                    }
            }
          
            private static class ReflectUtil {
                  
                    static final String CBK;
                    static final boolean subclasses;
                  
                    static {
                            String pkg = Bukkit.getServer().getClass().getPackage().getName();
                            CBK = pkg.substring(pkg.lastIndexOf('.') + 1);
                            String[] split = CBK.split("_");
                          
                            int i = Integer.parseInt(split[0].substring(1));
                            int j = Integer.parseInt(split[1]);
                            int k = Integer.parseInt(split[2].substring(1));
                            subclasses = i > 1 ? true : i == 1 ? (j > 8 ? true : j == 8 ? k >= 2 : false) : false;
                    }
                  
                    static Class<?> getNMSClass(String name) {
                            if(subclasses){
                                    //From v1_8_R2, some classes have become subclasses
                                    if(name.equals("EnumScoreboardAction"))
                                            name = "PacketPlayOutScoreboardScore$" + name;
                                    else if(name.equals("EnumScoreboardHealthDisplay"))
                                            name = "IScoreboardCriteria$" + name;
                            }
                            try {
                                    return Class.forName("net.minecraft.server." + CBK + "." + name);
                            } catch (ClassNotFoundException ex) {
                                    ex.printStackTrace();
                                    return null;
                            }
                    }
                  
                    static Object getField(Class<?> clazz, Object instance, String field) {
                            try {
                                    Field f = clazz.getDeclaredField(field);
                                    f.setAccessible(true);
                                    return f.get(instance);
                            } catch (Exception ex) {
                                    ex.printStackTrace();
                                    return null;
                            }
                    }
                  
                    static void setField(Class<?> clazz, Object instance, String field, Object value) {
                            try {
                                    Field f = clazz.getDeclaredField(field);
                                    f.setAccessible(true);
                                    f.set(instance, value);
                            } catch (Exception ex) {
                                    ex.printStackTrace();
                            }
                    }
                  
                    static Object getHandle(Object obj) {
                            try {
                                    return obj.getClass().getMethod("getHandle").invoke(obj);
                            } catch (Exception ex) {
                                    ex.printStackTrace();
                                    return null;
                            }
                    }
                  
                    static void sendPacket(Player p, Object... packets) {
                            try {
                                    Object handle = getHandle(p);
                                    Object connection = handle.getClass().getDeclaredField("playerConnection").get(handle);
                                    Method send = connection.getClass().getDeclaredMethod("sendPacket", getNMSClass("Packet"));
                                    for(Object packet : packets)
                                            send.invoke(connection, packet);
                            } catch (Exception ex) {
                                    ex.printStackTrace();
                            }
                    }
            }
    }
     
  22. Offline

    Krumb069

    Edit:1.7 supported fixed on new code
     
    Last edited: Jul 9, 2015
  23. Offline

    megamichiel

    @Krumb069
    You can create scores below a player's name by going to Scoreboard.getManager().getScoreboard(). This returns the bukkit version of the scoreboard. From there you can create a new objective, and set the display slot to below name.
     
    DeathZonePT and Krumb069 like this.
  24. Offline

    DeathZonePT

    @megamichiel
    Hey can you help me here. How can disable the scoreboard in a specific world like if the player is in the work the scoreboard will work but if they go to a minigames the scoreboard will disable!
     
  25. Offline

    megamichiel

    @DeathZonePT
    You can either:
    1. Set their scores to an empty list/array
    2. Call unregister() on their scoreboard (make sure you call ScoreboardManager#setupBoard(player) when you want to enable it again.
     
  26. Offline

    DeathZonePT

  27. Offline

    megamichiel

    @DeathZonePT
    Code:
    //Listen for when a player changes world
    public void onWorldChanged(PlayerChangedWorldEvent event) {
      Player p = event.getPlayer();
      Scoreboard board = Scoreboard.getManager().getScoreboard(p);
      if(event.getPlayer().getWorld().getName().equals("someWorldName")) { //Check if the player went to a certain world
        board.setScores(new String[0]).update(); //Set their scores to an empty array and update
      } else { //Set their scores to some totally random ones
        board.setScores(new String[] {"One!", "Two!", "Three!"}).update();
      }
    }
     
  28. Offline

    Krumb069

    Edit:1.7 supported fixed on new code
     
    Last edited: Jul 9, 2015
  29. Offline

    DeathZonePT

    Can you tell if this will work?
    Code:
    package com.DeathZone.Listener;
    
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChangedWorldEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import com.DeathZone.Core;
    import com.DeathZone.Scoreboard;
    
    public class ScoreBoardListener implements Listener {
       
        private Core plugin;
       
        public ScoreBoardListener(Core plugin) {
            this.plugin = plugin;
        }
       
       
        private static final List<String> frames = Arrays.asList(
                "§4§lDeathZone",
                "§c§lD§4§leathZone",
                "§f§lD§c§le§4§lathZone",
                "§c§lD§f§le§c§la§4§lthZone",
                "§4§lD§c§le§f§la§c§lt§4§lhZone",
                "§4§lDe§c§la§f§lt§c§lh§4§lZone",
                "§4§lDea§c§lt§f§lh§c§lZ§4§lone",
                "§4§lDeat§c§lh§f§lZ§c§lo§4§lne",
                "§4§lDeath§c§lZ§f§lo§c§ln§4§le",
                "§4§lDeathZ§c§lo§f§ln§c§le",
                "§4§lDeathZo§c§ln§f§le",
                "§4§lDeathZon§c§le",
                "§4§lDeathZone");
    
        private static final Map<String, Integer> taskIds = new HashMap<>();
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
           
            Player p = (Player) e.getPlayer();
               
            @SuppressWarnings("deprecation")
            final
            List<String> score = Arrays.asList(
                    "§1",
                    "§7Player",
                    "§a" + p.getName(),
                    "§a",
                    "§7Rank",
                    "§a" + plugin.perm.getPrimaryGroup(p),
                    "§2",
                    "§7Money",
                    "§a$" + plugin.econ.getBalance(p.getName()),
                    "§b",
                    "§7Online",
                    "§a" + Bukkit.getOnlinePlayers().size() + "/300",
                    "§c",
                    "§7Website",
                    "§ahttp://deathzone.ml");
           
            final Scoreboard board = Scoreboard.getManager().setupBoard(p);
           
            if (board != null) {
               
                board.setHeader(frames.get(0)).update();
                board.setScores(score).update();
                   
                   
                taskIds.put(p.getName(), new BukkitRunnable() {
                    int frame = 0;
    
                    public void run() {
                        frame++;
                        if (frame == frames.size()) {
                            frame = 0;
                        }
                        board.setHeader(frames.get(frame)).update();
                        board.setScores(score).update();
                    }
                }.runTaskTimer(plugin, 0L, 2L).getTaskId());
            }
        }
       
        @EventHandler
        public void onWorldChanged(PlayerChangedWorldEvent e) {
            Player p = (Player) e.getPlayer();
           
            final Scoreboard board = Scoreboard.getManager().getScoreboard(p);
           
            if (!e.getPlayer().getWorld().getName().equals("world")) {
                board.setScores(new String[0]);
                board.unregister();
                Integer id = taskIds.remove(e.getPlayer().getName());
                if (id != null) {
                    plugin.getServer().getScheduler().cancelTask(id);
                }
                return;
            }
        }
       
    
        @EventHandler
        public void onQuit(PlayerQuitEvent e) {
           
            Integer id = taskIds.remove(e.getPlayer().getName());
            if (id != null) {
                plugin.getServer().getScheduler().cancelTask(id);
            }
        }
    }
     
  30. Offline

    Krumb069

    Edit:1.7 supported fixed on new code
     
    Last edited: Jul 9, 2015
Thread Status:
Not open for further replies.

Share This Page