Need help with user specific numbers/ints.

Discussion in 'Spigot Plugin Development' started by bradley13, May 18, 2016.

  1. Hey there, I am trying to make a plugin for my server that, when they kill someone, gives them 10 'Coins' (It is not currency). But, the problem I am having is that it gives everyone 10 'Coins' as when people do /coins it shows them the wrong amount not their amount.

    I was wondering how to do this. Any help would be appreciated!
     
  2. What is the 10 coins? Post your plugin
     
  3. Code (Text):

        @EventHandler

        public void onPlayerKill(PlayerDeathEvent e){

            Player player = e.getEntity();

            if(!(player.getKiller() instanceof Player)){

                return;

            }

            Player killer = player.getKiller();

            coins ++;

            coins ++;

            coins ++;

            coins ++;

            coins ++;

            coins ++;

            coins ++;

            coins ++;

            coins ++;

            coins ++;

            killer.sendMessage(ChatColor.GREEN + "+10 SpartanCoins");

            killer.playSound(killer.getLocation(), Sound.LEVEL_UP, 1, 10);

            return;

        }

       

        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){

            Player player = (Player) sender;

            if(cmd.getName().equalsIgnoreCase("coins")){

                player.sendMessage(ChatColor.GREEN + "You have: " + ChatColor.GOLD + "" + coins);

                returntrue;

            }

            if(cmd.getName().equalsIgnoreCase("spendcoins")){

                Inventory inv = Bukkit.createInventory(null, 9, ChatColor.AQUA + "" + ChatColor.BOLD + "Spartan Coin Shop");

               

                ItemStack basic = new ItemStack(Material.TRIPWIRE_HOOK, 1);

                ItemMeta basicMeta = basic.getItemMeta();

                basicMeta.setDisplayName(ChatColor.GRAY + "" + ChatColor.BOLD + "Basic Key");

                List<String> basicLore = new ArrayList<String>();

                basicLore.add(ChatColor.AQUA + "" + ChatColor.BOLD + "COSTS: " + ChatColor.GOLD + "30 Coins");

                basicMeta.setLore(basicLore);

                basic.setItemMeta(basicMeta);

                inv.setItem(0, basic);

               

               

                player.openInventory(inv);

            }

            returntrue;

        }



    }
    I know not the most effective way but it works.
     
  4. Looks like
    Code (Text):
    coins
    is declared as a global variable, not a per player variable. Try storing values in a file that the plugin can reference.
     
    • Useful Useful x 1
  5. How would I do that?
     
  6. Code (Text):
       coins ++;

            coins ++;

            coins ++;

            coins ++;

            coins ++;

            coins ++;

            coins ++;

            coins ++;

            coins ++;

            coins ++;
     
    replace this by
    Code (Text):
    coins +=10;
    Also as mention above you have to create a coin counter for every player, you coulf f.e. store the ammounts in a Hashmap
     
    • Like Like x 1
    • Useful Useful x 1
  7. Ok thanks for the simpler way, but, don't get mad, I have never done HashMaps.
     
  8. Not trying to be a cliché here and use the "learn java" remark, but if you're doing 'coins++' 10 times and don't know about global variables, you really should pick up a java book first.
     
    • Agree Agree x 2
  9. Well, I knew there was a way to do it quicker but I couldn't think so I did it like that. And I learnt basic Java before learning Bukkit but it seems I missed HashMaps so if anyone could help that would be great!
     
  10. Maps store data in a key/value format. Each value is paired with a key. Keys cannot be the same but values can be the same throughout.
    You can use one like so
    Code (Java):

    Map<UUID, Integer> coins = new HashMap<>(); // will need new HashMap<UUID, Integer> for java 7
     
    Then put data with
    Code (Java):

    coins.put(UUID, INTEGER);
     
    put() will add to the map or replace a value if it already exists.

    Because hashmaps are memory resident, the data won't persist through reloads/restarts so you'll need to store them in a file or database.
     
  11. So,
    Code (Text):

    Map<UUID, Integer> coins = new HashMap<>();
            coins.put(player.getUniqueId(), 10);
    Would work?
     
  12. coins.get(player.getUniqueId()); returns the integer.
     
  13. Ok, but where I have the HashMap, is in a event and where I want it to send them the message is in a command method. So how do I get it into my command method?
     
  14. We've linked you to the documents, and told you how to do everything already. I don't see the issue here. Just create a command, like any other, and when they execute the command, send the player the message with their balance. As @Lyxnx said though, you should really read up on how HashMaps work.

    Basically, you have a key and a value. Your key in this case is the UUID, and your value of the integer. Basically, if you want to get the value from a key, you use:

    Code (Text):
    Map.get(K);
    Where 'Map' is your map, and 'K' is your key. If you use this, it will then return the value of the key. So for example, let's say we have an item and a price for a store, and you need to list each item with a price. What you would use in this case is the HashMap. So here, I will use a String as the key (being the item name), and a double as the value (being the price).


    Code (Text):
    Map<String, Double) items = new HashMap<>();

    items.put("Soap", 1.50);
    items.put("Water", 1.00);
    items.put("Chips", 0.50);
    items.put("Milk", 2.50);
    items.put("Bread", 3.00);
    So these may not be exact prices obviously, but we are just using it for reference. Now you're wondering, "how do I get the price of bread now?". Well, that's simple! Basically, you can use the method I explained earlier:

    Code (Text):
    Map.get("Bread");
    And this would then return 3.00 (the value of bread). I hope I helped, and if you're still confused, feel free to message me, and I can try to explain further.
     
  15. He's asking how to get the HashMap reference.


    You can create a class that stores the HashMap within it privately and statically, so then you can just use get and set methods. This is called encapsulation. Example of the manager class:

    Code (Java):
    public class CoinManager
    {
      private static Map<String, Integer> coins = new HashMap<String, Integer>();

      public void setCoins(String uuid, int coins)
      {
        this.coins.put(uuid, coins);
      }

      public int getCoins(String uuid)
      {
        return coins.get(uuid);
      }
    }

    You can then instantiate it in each of your classes (ugly way, I don't really recommend it) or pass it through your plugin instance. This depends on whether or not you have a Singleton plugin instance or are passing it to your other classes through constructors. If you have no clue what I'm saying, then go teach yourself more Java. :p
     
  16. Learn java.
    I hate to be that "that person", but you seem to have no knowledge of very basic and important concepts. It would save you (and us) many hours of time in future if you paused and worked on learning the basics of OOP and scope.
     
    • Agree Agree x 3