Scoreboard problem

Discussion in 'Plugin Development' started by Konkz, Jan 24, 2014.

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

    Konkz

    I been trying for past hour or two to work the scoreboards out and I am unable.

    I looked on tutorials on YouTube, on here etc. with no sucess so I decided to ask here for support; what I want is a scoreboard that will basically say:

    -- Test --
    Konkz: 20

    (The 20 is amount of Material.SUGAR in inventory)

    Could someone please write me the code for that so I can see how it works? Please and thank you.

    If anyone is having a bad day and feels like being negative, please just don't reply.
     
  2. Offline

    beastman3226

    Please don't ask for code so blatantly especially without supplying any to see where you are at. However, you need to create a scoreboard and mess around with objectives and score. If you haven't already, read the documentation or go find the wikipedia on Scoreboard programming to learn what each piece means/does.
     
  3. Offline

    Konkz


    Hmm okay. I have managed to get the Scoreboard to work to somewhat what I want, to see if I can get it to work I checked how many users are online and now I have the message

    -- Test --
    Online Players: 1

    Which is what I excepted to get, but now I would like to know how to change the 'Online Players' to names and the number of players to amount of sugar found in inventory.

    This is the code I have used:
    Code:java
    1. public void scoreboard() {
    2. ScoreboardManager manager = Bukkit.getScoreboardManager();
    3. board = manager.getNewScoreboard();
    4.  
    5. Objective objective = board.registerNewObjective("Test", "Test2");
    6. objective.setDisplayName(ChatColor.AQUA + "-- Test --");
    7. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    8.  
    9. Team team = board.registerNewTeam("Team");
    10. team.setDisplayName(ChatColor.RED + "t");
    11.  
    12. int a = Bukkit.getOnlinePlayers().length;
    13.  
    14. Score score = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Online Players"));
    15. score.setScore(a);
    16. }
     
  4. Offline

    Maurdekye

    You would have to add a couple event listeners, like InventoryClickEvent and PlayerDropItemEvent, and update the count each time they;'re called.
     
  5. Offline

    beastman3226

    Iterate through each player on the server, creating a new score object and setting the score to the amount of sugar said player has the inventory. Note that this may cause lag on lower end servers and on larger servers will flood the screen.

    Maurdekye
    To add on to what he said instead of doing that you could have a repeating runnable that checks every minute or so.

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

    Maurdekye

    I'm sure that a couple event listeners would be much more accurate, and reduce overhead when its not necessary.
     
  7. Offline

    beastman3226

    Maurdekye
    True and iterating every minute would be a lot more inefficient than just iterating whenever the value you want to display changes. This in mind I would create handlers for events: PlayerDropItem, PlayerQuitEvent, InventoryChangeEvent and PlayerJoin.
     
  8. Offline

    Maurdekye

    I'm pretty sure that InventoryChangeEvent would handle everything by itself, I didn't know that event existed before.
     
  9. Offline

    beastman3226

    Maurdekye
    Yes, I would only change the value of the person who moved the sugar so I wouldn't have to iterate again, I picked those events so that you would never have to iterate through every player on the server.
     
  10. Offline

    Konkz


    Instead of checking for Material Sugar what I got is when as an example cobblestone block is destroyed it gives you sugar but it then can also add to the score. I'm not sure how to do that though.
     
  11. Offline

    Maurdekye

    Konkz You mean when a cobblestone block is broken, it drops sugar as well? That's pretty easy;
    Code:java
    1. @EventHandler
    2. public void Break(BlockBreakEvent event) {
    3. ItemStack sugar = new ItemStack(Material.SUGAR, (int) (Math.random()*3 + 1));
    4. if (event.getBlock().getType() == Material.COBBLESTONE) event.getBlock().getWorld().dropItem(event.getBlock().getLocation(), sugar);
    5. }

    As for adding the sugar to the player's count, you should just leave that to the inventory event listener.
     
  12. Offline

    Konkz


    No I mean when cobblestone is broken then it adds 1 to the scoreboard

    Example:

    -- Test --
    Konkz: 0
    Martin: 0

    Martin Destroys 1 Cobblestone

    -- Test --
    Martin: 1
    Konkz: 0
     
  13. Offline

    Maurdekye

    I'm not sure how the scoreboard api works, really.
     
Thread Status:
Not open for further replies.

Share This Page