Solved Creating new instances of class for no reason

Discussion in 'Plugin Development' started by The Gaming Grunts, May 24, 2014.

Thread Status:
Not open for further replies.
  1. Hey all. So, I'm making an SQL balance storage system. On startup, the plugin loads the info from the database & creating a new class instance so that there is a cached version of the data to prevent needless DB calls. Now, when a player joins a new "account" (basically a new instance of the Balance class) will be created if they don't have one already.

    The problem that I'm getting is that a new instance of the Balance class is always created whether or not the player already has an "account". This is causing problems getting info, like the balance due to the fact that there are multiple instances of the player's account. Thanks in advance for the help :)

    Here's all of the relevant code:

    Balance:
    Code:java
    1. public class Balance {
    2.  
    3. private double balance;
    4. private UUID uuid;
    5.  
    6. /**
    7.   * Create a new Balance instance for a player
    8.   *
    9.   * @param uuid : The UUID of a player
    10.   */
    11. public Balance(UUID uuid){
    12. this.uuid = uuid;
    13. }
    14.  
    15. /**
    16.   * Get the balance of the player
    17.   *
    18.   * @return The player's balance
    19.   */
    20. public double getBalance(){
    21. return balance;
    22. }
    23.  
    24. /**
    25.   * Get the UUID of the player
    26.   *
    27.   * @return The UUID of the player
    28.   */
    29. public UUID getId(){
    30. return uuid;
    31. }
    32.  
    33. /**
    34.   * Set the balance of the player
    35.   *
    36.   * @param amount : The amount to add on to the balance
    37.   */
    38. public void setBalance(double amount){
    39. this.balance = amount;
    40. }
    41.  
    42. /**
    43.   * Get the player from the UUID
    44.   *
    45.   * @return The player associated with the UUID
    46.   */
    47. public Player getPlayer(){
    48. return Bukkit.getPlayer(uuid);
    49. }
    50. }


    BalanceManager:
    Code:java
    1. public class BalanceManager extends Thread {
    2.  
    3. private ArrayList<Balance> instances = new ArrayList<Balance>();
    4. private static BalanceManager bm = new BalanceManager();
    5.  
    6. public static BalanceManager getManager(){
    7. return bm;
    8. }
    9.  
    10. /**
    11.   * Get the Balance class instance of a given player to allow access to various methods
    12.   *
    13.   * @param player : The player
    14.   * @return The Balance class instance of the player
    15.   */
    16. public Balance getAccount(Player player){
    17. for (Balance b : instances){
    18. if (b.getId() == player.getUniqueId()){
    19. return b;
    20. }
    21. }
    22. return null;
    23. }
    24.  
    25. public void createBalance(Player player){
    26. Balance b = new Balance(player.getUniqueId());
    27. b.setBalance(0);
    28. instances.add(b);
    29.  
    30. try {
    31. DatabaseUtils.queryOut("INSERT INTO balance(player, balance)"
    32. + "VALUES ('" + player.getUniqueId().toString() + "'," + b.getBalance() + ");");
    33. } catch(SQLException e) {
    34. e.printStackTrace();
    35. }
    36. }
    37.  
    38. /**
    39.   * Load all the balances from the database. Call this method in onEnable()
    40.   */
    41. public void loadBalancesFromDB(){
    42. new Thread() {
    43. @Override
    44. public void run() {
    45. ResultSet result = null;
    46. try {
    47. result = DatabaseUtils.queryIn("SELECT * FROM balance;");
    48. while (result.next()){
    49. Balance b = new Balance(UUID.fromString(result.getString("player")));
    50. b.setBalance(result.getDouble("balance"));
    51. instances.add(b);
    52. System.out.println("Loaded: " + b);
    53. for (Balance bal : instances){
    54. System.out.println("UUID: " + bal.getId());
    55. }
    56. }
    57. System.out.println("Instances: " + instances);
    58. for (Balance bal : instances){
    59. System.out.println("UUID: " + bal.getId() + " Bal: " + bal.getBalance());
    60. }
    61. } catch(SQLException e) {e.printStackTrace();}
    62. }
    63. }.start();
    64. }


    PlayerJoin:
    Code:java
    1. public class PlayerJoin implements Listener {
    2.  
    3. @EventHandler
    4. public void onJoin(PlayerJoinEvent e){
    5. if (BalanceManager.getManager().getAccount(e.getPlayer()) == null){
    6. BalanceManager.getManager().createBalance(e.getPlayer());
    7. }else{
    8. System.out.println("not null");
    9. }
    10. }
    11. }


    I don't want to edit the previous post as to not mess to the code format, but I'm sorry if something doesn't make any sense. I'm kinda going on only a few hours of sleep (which might explain why I can't figure this out :p ).

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

    jthort

    The Gaming Grunts If I understood your question correctly, you need to just create a new value for the players who don't already have one. Take a look at the example below..

    I have the following code on the OnEnable
    Code:
    for(Player player: Bukkit.getOnlinePlayers()){
                if(!(hasAccount(player.getName()))){
                    createAccount(player.getName());
                    System.out.println("[Towns] Generated a new account for " + player.getName());
                }
            }
    The hasAccount method..
    Code:
    public boolean hasAccount(String player){
              for(String name: accountsConfig.getKeys(false)){
                  if(name.equals(player)){
                      return true;
                  }
              }
              return false;
          }
    Create Account method
    Code:
    public void createAccount(String player){
              Account account = new Account(player, accountsConfig, newF);
          }
    Then I have the following event to keep track of the players leaving and joining, checking to make sure they have a account
    Code:
    @EventHandler
          public void onPlayerJoin(PlayerJoinEvent event){
              if(!(hasAccount(event.getPlayer().getName()))){
                  createAccount(event.getPlayer().getName());
                  System.out.println("[Towns] Generated a new Account for " + event.getPlayer().getName());
              }
          }
    With all of these methods it only creates an account for the players who don't already have one. Of course this isn't SQL, but you get the idea

    Best of luck
     
  3. jthort
    I already have that ;) Adding a null check with my getAccount() method is the same thing as your hasAccount() method :p
     
  4. Offline

    jthort

    The Gaming Grunts Well then I assume something is wrong with one of your methods that checks if that player has an account. I'm going to be honest and say I didn't look at any of your code :).
     
  5. jthort
    Looking at the code would probably help ;)
     
  6. Offline

    L33m4n123

    Server running in online mode? If not player#getUniqueID Will return random uuids every time
     
  7. L33m4n123
    Yes it's in online mode ;)

    Well, I figured it out... had to change the == to .equals() in getAccount(). Gotta love these little oversights...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page