Money...

Discussion in 'Plugin Development' started by CraftCreeper6, May 30, 2014.

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

    CraftCreeper6

    Hello! I am trying to figure out how to handle with a money system. I know you must use a HashMap etc... but what are the separate things needed to finish it? Like how to load / save the HashMap, cross class support etc... Can anyone help? :)
     
  2. Offline

    WhatAaCow

    CraftCreeper6 "I know you must use a HashMap", you don't have to, if you don't want to. Money plugins usually have databases where they store the money per user, the prices for blocks etc.
     
  3. Offline

    CraftCreeper6

    WhatAaCow
    Shhh :p Plus, not really related.
     
  4. Offline

    WhatAaCow

    CraftCreeper6 Not really related? Wtf dude.
    and i wrote about database saving.

    And what do you mean with "cross class support"?
     
  5. Offline

    CraftCreeper6

    WhatAaCow
    I mean the HashMap is accessed in multiple classes.
     
  6. Offline

    WhatAaCow

    CraftCreeper6 Make the hashmap static and you're done. This is java basic so have a look at some tutorials ;)
     
  7. Offline

    JBoss925

    Well, to save you can loop through the keys and create a string list from them. Let's say it's UUIDs do:
    Code:java
    1. for(UUID id: hashmapname.keySet()){
    2. ArrayListName.add(id.toString());
    3. }
    4.  
    5. getConfig().set("path.name", ArrayListName);

    Do the same for the values in a different string list.
     
  8. Offline

    fireblast709

    WhatAaCow bad. Don't just tell him to 'make it static and you're done'. If you want to be on the safe side with static (since it can cause a number of issues when you don't know how to deal with it), I can recommend something like the following:
    Code:java
    1. public class Main extends JavaPlugin
    2. { // or however you want to call the class
    3.  
    4. private final EconomyUtil util;
    5.  
    6. public void onEnable()
    7. {
    8. this.util = new EconomyUtil();
    9. }
    10.  
    11. public EconomyUtil getUtil()
    12. {
    13. return this.util;
    14. }
    15. }
    16.  
    17. public class EconomyUtil
    18. {
    19.  
    20. private double _getBalance()
    21. {
    22. return balance;
    23. }
    24.  
    25. private static EconomyUtil getInstance()
    26. {
    27. return JavaPlugin.getPlugin(Main.class).getUtil();
    28. }
    29.  
    30. public static double getBalance()
    31. {
    32. return getInstance()._getBalance();
    33. }
    34. }
    Maybe a bit unfair towards Bukkit to let it handle all the static instances, but at least we can blame them for the memory leaks now :3.
     
  9. Offline

    JBoss925

    Then to retrieve them:
    Code:java
    1. ArrayList<UUID> ArrayListUserUUIDS = new ArrayList<UUID>();
    2. for(String string: getConfig().getStringList("path.name")){
    3. ArrayListUserUUIDS.add(UUID.valueOf(string));
    4. }
    5.  
    6. //do the same for the values then do
    7.  
    8. for(int i = 0; i < ArrayListUserUUIDS.size()){
    9. UUID[] uuidkeyset = ArrayListUserUUIDS.toArray();
    10. ValueType[] uuidvalueset = ValueArrayListName.toArray();
    11. hashmapname.put(UUID, uuidvalueset);
    12. }
     
  10. Offline

    WhatAaCow

    fireblast709 If you initialize a static hashmap in the onEnable you can't do anything wrong....

    I think were talking about 2 different things. I'm talking about cross class support and i think you talk about secure use of objects?
     
  11. Offline

    CraftCreeper6

    WhatAaCow
    Okay, I did some research and found nothing >_> :p Is there a site that you could send me? Preferably not StackOverFlow as they are very hard to understand. :)
     
  12. Offline

    WhatAaCow

  13. Offline

    CraftCreeper6

    WhatAaCow
    I do not understand it XD I really do not know why but it just does not make sense ;/

    WhatAaCow
    Nvm fixed it! :D

    WhatAaCow
    Fail :p
    Error in main class: The method getHashmap() is undefined for the type Currency

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

    WhatAaCow

  15. Offline

    CraftCreeper6

    WhatAaCow
    Main:
    Code:java
    1. public static void main(String args[]) {
    2. Currency ex = new Currency();
    3. HashMap<String, Integer> money = ex.getHashmap();
    4. }
    5.  


    Other:
    Code:java
    1. package me.CraftCreeper6.main;
    2.  
    3. import java.util.HashMap;
    4.  
    5. public class Currency {
    6.  
    7. public static HashMap<String, Integer> money = new HashMap<String, Integer>();
    8.  
    9. public void giveMoney(String p, int amount){
    10.  
    11. money.put(p, amount);
    12.  
    13. }
    14.  
    15. public void takeMoney(String p, int amount){
    16.  
    17. money.put(p, - amount);
    18.  
    19. }
    20.  
    21. public HashMap<String, Integer> getHashmap() {
    22. return money;
    23. }
    24.  
    25. }
    26.  
     
  16. Offline

    WhatAaCow

    CraftCreeper6 i think you imported the wrong "Currency" (java.util.Currency) object. Rename your currency class to something different and try again :)
     
  17. Offline

    CraftCreeper6

    WhatAaCow
    How do I access it? :p it wont let me use money.___ :(
     
  18. Offline

    WhatAaCow

    CraftCreeper6 RENAME your "Currency" CLASS and try it because you imported the wrong object in your main class
     
  19. Offline

    CraftCreeper6

  20. Offline

    WhatAaCow

Thread Status:
Not open for further replies.

Share This Page