Vault problem

Discussion in 'Plugin Development' started by therandy123, Jul 26, 2014.

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

    therandy123

    So im making a GUI and in this gui an item will display the players balance. Im using Vault and Essentials econ to do this. this is my code
    Code:
    ItemStack Armor1 = new ItemStack(Material.GOLD_INGOT);
        ItemMeta Armorm1 = Armor1.getItemMeta();
        Armorm1.setDisplayName(ChatColor.GREEN + "Money Balance");
        ArrayList<String> ArmorA1 = new ArrayList<String>();
        ArmorA1.add(ChatColor.GRAY + "Your money balance is: " + ChatColor.DARK_AQUA + iKits.econ.getBalance(player));
        Armorm1.setLore(ArmorA1);
        Armor1.setItemMeta(Armorm1);
        bShop.setItem(8, Armor1);
    So when they open the gui the item displays the players balance but if someone else lets say player 2 opens the gui both players will see players2s balance so is there any way to make this not happen so only the player that open see his own balance and not any one else? BTW player is the sender. Thanks!
     
  2. Offline

    Iroh

    Moved to plugin dev.
     
  3. Offline

    therandy123

  4. Offline

    Niknea

    therandy123 Firstly, only bump every 12 hours, secondly, you can store their value and name in a hashmap, then retrieve it for when they open the GUI. Therefore everyone has their own data.
     
  5. Offline

    therandy123

    Niknea So should I use UUID for this?
     
  6. Offline

    Niknea

    therandy123 I don't believe that's required using the Vault API, as vault will do that part for you, just store their username, and their balance.
     
  7. Offline

    therandy123

    Niknea How do i store the balance?
     
  8. Offline

    Niknea

    therandy123 In a HashMap, such as this one:

    PHP:
        HashMap<StringDoublebalances = new HashMap<StringDouble>();
     
  9. Offline

    therandy123

    Niknea So i did bal.put(player.getName(), econ.getBalance(player));
    and then what do i need to set the balance on the item to their balance using this?
     
  10. Offline

    Niknea

    therandy123 You'd get the player from the HashMap, like in the following code:

    PHP:
    meta.setDisplayName(balances.get(p.getName()));
     
  11. Offline

    therandy123

    Niknea It works but the problem is still there when another person opens the inventory the balance changes for both tha players my code:
    Code:java
    1. ItemStack bala = new ItemStack(Material.GOLD_INGOT);
    2. ItemMeta balmeta = bala.getItemMeta();
    3. balmeta.setDisplayName(ChatColor.GREEN + "Money balance");
    4. bala.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
    5. HashMap<String, Double> balances = new HashMap<String, Double>();
    6. balances.put(player.getName(), iKits.econ.getBalance(player));
    7. ArrayList<String> ballore = new ArrayList<String>();
    8. ballore.add(ChatColor.GRAY + "Balance: " + balances.get(player.getName()));
    9. balmeta.setLore(ballore);
    10. bala.setItemMeta(balmeta);
    11.  
    12. bShop.setItem(8, bala);

    Do i have to check if the player cntains the map?
     
  12. Offline

    xTigerRebornx

    therandy123 Show us the creation of the Inventory and where you open it (full code would be preferred)
     
  13. Offline

    Niknea

    therandy123 I know the reason why it's happening, the ItemMeta displayname keeps changing as players open the Inventory, changing to different balances. However I currently don't know a fix to it, let me think on it, and I'll get back to you. Maybe xTigerRebornx knows?
     
  14. Offline

    xTigerRebornx

    Niknea Problem is that he uses the same Inventory for everyone (I believe, unable to tell without full code), so that when someone else opens it, the change occurs for that Inventory, which all Players view. Solution would be to create the Inventory dynamically (such that each Player has their own and the Inventory could be stored in a Map<UUID, Inventory>), and use those individual Inventories rather then a global Inventory
     
    Niknea likes this.
  15. Offline

    therandy123

    xTigerRebornx
    Code:java
    1. public class bShop implements Listener, CommandExecutor{
    2. public static Inventory bShop = Bukkit.createInventory(null, 9, ChatColor.GREEN.toString() + ChatColor.BOLD + "bShop");
    3. public static Inventory bArmor = Bukkit.createInventory(null, 9, ChatColor.GREEN.toString() + ChatColor.BOLD + "Armor");
    4. public static Inventory bHG = Bukkit.createInventory(null, 9, ChatColor.GREEN.toString() + ChatColor.BOLD + "bShop HG");
    5. public static Inventory bArmorHG = Bukkit.createInventory(null, 9, ChatColor.GREEN.toString() + ChatColor.BOLD + "Armor HG");
    6. public iKits plugin;
    7. public bShop(iKits instance){
    8. plugin = instance;
    9. }
    10. public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String[] args) {
    11.  
    12. Player player = (Player) sender;
    13. if(!(plugin.hg.contains(player.getName()))) {
    14. player.sendMessage("tfrc");
    15. player.openInventory(bShop);
    16.  
    17.  
    18.  
    19. ItemStack bala = new ItemStack(Material.GOLD_INGOT);
    20. ItemMeta balmeta = bala.getItemMeta();
    21. balmeta.setDisplayName(ChatColor.GREEN + "Money balance");
    22. bala.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
    23. HashMap<String, Double> balances = new HashMap<String, Double>();
    24. balances.put(player.getName(), iKits.econ.getBalance(player));
    25. ArrayList<String> ballore = new ArrayList<String>();
    26. ballore.add(ChatColor.GRAY + "Balance: " + balances.get(player.getName()));
    27. balmeta.setLore(ballore);
    28. bala.setItemMeta(balmeta);
    29.  
    30. bShop.setItem(8, bala);

    This is my code right now
     
  16. Offline

    xTigerRebornx

    therandy123 See my comment above yours on how to fix your problem. You use one Inventory, meaning once someone else open's it, the items are shown to everyone.
     
  17. Offline

    therandy123

    xTigerRebornx I did like this not sure if it right
    Code:java
    1. static Player p;
    2. HashMap<UUID, Inventory> inv = new HashMap<UUID, Inventory>();
    3. public static Inventory bShop = Bukkit.createInventory(p, 9, ChatColor.GREEN.toString() + ChatColor.BOLD + "bShop");
    4.  
    5. public static Inventory bArmor = Bukkit.createInventory(p, 9, ChatColor.GREEN.toString() + ChatColor.BOLD + "Armor");
    6. public static Inventory bHG = Bukkit.createInventory(p, 9, ChatColor.GREEN.toString() + ChatColor.BOLD + "bShop HG");
    7. public static Inventory bArmorHG = Bukkit.createInventory(p, 9, ChatColor.GREEN.toString() + ChatColor.BOLD + "Armor HG");
    8.  
    9. public iKits plugin;
    10. public bShop(iKits instance){
    11.  
    12. plugin = instance;
    13.  
    14. }
    15. public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String[] args) {
    16.  
    17. Player player = (Player) sender;
    18. p = (Player) sender;
    19. if(!(plugin.hg.contains(player.getName()))) {
    20. player.sendMessage("tfrc");
    21. player.openInventory(bShop);
    22. inv.put(p.getUniqueId(), bShop);
    23.  

    Im not the best at this
     
  18. Offline

    xTigerRebornx

    therandy123 You are still using the same Inventory for everyone. The idea is that each Player gets their own Inventory, so that the changes are only shown to them.
     
  19. Offline

    therandy123

    xTigerRebornx So should i use the hashmap to set the inventory for the player
     
  20. Offline

    xTigerRebornx

    therandy123 The Map would be used as a way to pair the Inventory with the Player. Something like:
    Code:
    Map<UUID, Inventory> inventories = (...);
     
    // When accessing
    Player p = (...);
    if(inventory.get(p.getUniqueId()) != null){
    // Inventory exists, you can grab it and use it
    } else {
    // Inventory doesn't exist, you need to make a new one and put it in.
    }
    Please note, this is pseudo-code, may/may not work. Its to show a concept, not to be taken word-by-word and copy pasted.
    The concept with this is that each Player has their own Inventory that you set their individual balances in.
     
Thread Status:
Not open for further replies.

Share This Page