Simple CointSystem.

Discussion in 'Plugin Development' started by BlazeEyezz, Nov 19, 2013.

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

    BlazeEyezz

    How do i make a simple per player Coinsystem?
    When you get a zombiekill, you get 1 coin.
    And how do i remove coins of a player?
     
  2. Offline

    adam753

    Hashmap.
     
    Dr_Bunsen likes this.
  3. Offline

    Sarrabiscos

    Try to learn how to use vault, its a very good api
     
  4. Offline

    BlazeEyezz

    Can someone give me a simple example ?
     
  5. Offline

    adam753

    A HashMap is a list that associates two values together like a dictionary. In a dictionary, you have words and their associated definitions. In a HashMap, those are called keys and values, respectively. You'd want the player name to be the key and the number of coins they have to be the value. So you would make that hashmap like this:
    Code:
    HashMap<String, Integer> mymap = new HashMap<String, Integer>();
    That's more than enough info, I'm sure you can find the rest out using google.
     
  6. Offline

    BlazeEyezz

    I know, i can do it with hashmaps. but how..?
     
  7. Offline

    xTrollxDudex

  8. Offline

    jusjus112

  9. Offline

    BlazeEyezz

    I don´t understand it exactly...
    Do you use Hashmap or Config ?

    Anyone ?

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

    Panjab

    BlazeEyezz

    Just an example:

    Classes:

    CoinHandler
    TestCommand

    CoinHandler

    Code:
    private static HashMap<String, Integer> coins = new HashMap<String, Integer>();
     
    public static int getCoins(String player) {
        return coins.get(player);
    }
     
    public static void addCoins(String player, int amount) {
        coins.put(coins.get(player) + amount);
    }
     
    public static void removeCoins(String player, int amount) {
        coins.put(coins.get(player) - amount);
    }
    
    Just use those methods in your TestCommand (usually in all your classes) to use the CoinSystem.
     
    TheTrixsta likes this.
  11. Offline

    Desle

    If you want to keep it stored, even after restarting the server or reloading, use a configuration file;



    Code:
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            if (!getConfig().contains(e.getPlayer().getName())) {
                getConfig().set(e.getPlayer().getName() + ".coins", 0);
                saveConfig();
            }
        }
     
    #This code will set the coins of a user to 0 if they haven't joined before.
    Code:
        @EventHandler
        public void o(final EntityDamageByEntityEvent e) {
                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new BukkitRunnable() {
                    @Override
                    public void run() {
                        if (e.getEntity().isDead() && !(e.getEntity() instanceof Player)) {
                        Player p = (Player) e.getDamager();
                        getConfig().set(p.getName() + ".coins", getConfig().getInt(p.getName() + ".coins") + 1);
                        saveConfig();
                    }
                }
            }, 1L);
        }
     
    #This code will add 1 coin when a player kills an entity that's not a player (A mob) and stores & updates the player's coins.
     
  12. Offline

    BlazeEyezz

    Great! Thank you!
    I did choose the config option, but how can i show someone his coins with a command like /voxelcoins ?

    Can i do it with:

    Code:java
    1. if (commandLabel.equalsIgnorecase("Voxelcoins"){
    2. player p = (Player) sender;
    3. p.sendMessage(ChatColor.GRAY + "Coins: " + (coins.get(p));


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

    BlazeEyezz

    anyone /?

    ;(

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

    Aperx

    Test your command, see if it works. If not, paste your errors here
     
  15. Offline

    Dr_Bunsen

    quit the bumping already.

    But I assume the coins is a hashmap? you should use p.getName(), or else you are using a player object as a string (which will result in a pointer as the given string to check in the hashmap)
     
  16. Offline

    Zarko

    Do you want to use sql db to store the coins?
     
  17. Offline

    BlazeEyezz

  18. Offline

    Dr_Bunsen

Thread Status:
Not open for further replies.

Share This Page