Get player in onEnable?

Discussion in 'Plugin Development' started by Condolent, Jul 21, 2014.

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

    Condolent

    So I'm making a scoreboard and one text should frequently update to a value that's written down in a log-file.
    This is the code:
    Code:java
    1. @EventHandler
    2. public void onMove(PlayerJoinEvent e) {
    3. String UUID = e.getPlayer().getUniqueId().toString();
    4.  
    5. p.setScoreboard(board);
    6.  
    7. balanceInt = o.getScore(Bukkit.getServer().getOfflinePlayer(getPlayerCurrency().getInt(UUID) + ""));
    8.  
    9. space.setScore(12);
    10. Online.setScore(11);
    11. onlineInt.setScore(10);
    12. space2.setScore(9);
    13. faction.setScore(8);
    14. factionName.setScore(7);
    15. space3.setScore(6);
    16. balance.setScore(5);
    17. balanceInt.setScore(4);
    18. space4.setScore(3);
    19. }


    Note that I had to put this in the PlayerJoinEvent, so it won't update unless I rejoin the server. Is it possible to have this in onEnable? Like is it possible to define the player somehow? Or will I just have to live with it?

    Note that it's not the actual score that changes, it's the text!
    How it looks like: [​IMG]
    So it's the 0 below "balance:" that's meant to be updating.
     
  2. Offline

    Flamedek

    Condolent You could use a BukkitRunnable to schedule a repeating task, checking players balances every few seconds or so.
    During the onEnable there are usually no players online, as you can't join then. Only in case of a reload there can be and in that case I think it might work to check players there.

    I think your best bet here is a repeating task though, for the best result. To keep it semi lightweight just check for the values that change and update them, like the balance
     
  3. Offline

    Dubehh

    Flamedek
    A repeating task would cause lagg right?
    Condolent
    Why don't update the board once a player's balance changes?
    For example, when a player gains money (not sure what kind of currency you use) update the scoreboard right after it.
     
  4. Offline

    Flamedek

    Dubehh A repeating task can theoretically cause lagg yes, just becaust it's 'work' for the server. But if it's run every few seconds (say 100 ticks), and only a few values are checked/updated, the lagg of the task is totaly negligible.

    If vault fires an event for every balance change that would be the best option yes, for the currency. But depending on which values you want to update that is not always possible.

    So if you know when a value is changed, update the scoreboard with it, if you can't find out you will need a repeating task to check
     
  5. Offline

    Condolent

    I was trying to accomplish something like this, but the event of when someone gains gold, is in a event-class, not the main class where the actual scoreboard code is in.. :/
     
  6. Offline

    dsouzamatt

    Just call a method in your main class from the event class.
     
  7. Offline

    Dubehh

    Condolent
    Code:java
    1.  
    2. final MainClass m;
    3. public ListenerClass(MainClass instance){
    4. m = instance;
    5. }
    6.  

    if you add that into your Listener class.

    Make the
    Scoreboard thing Public,
    then inside your Listener class, you can
    Code:java
    1. m.//public methods in your main
     
Thread Status:
Not open for further replies.

Share This Page