How to set custom player inventory to file? I have the save method.

Discussion in 'Plugin Development' started by kayc01, Nov 1, 2015.

Thread Status:
Not open for further replies.
  1. Hey, hoping this will be very quick but i have a few questions.

    I made a post a while ago but decided to totally scrap that and have come in from a new angle.

    So here is the code i have so far:


    Code:
    for (Player playerWinner : Bukkit.getServer().getOnlinePlayers()) {
                            //get online players, for use in args 2
                             Inventory myInventory = Bukkit.createInventory(null, 45, playerWinner + "'s Badges!");
                            //create an inventory for all online players and give display name of that player.
                             ItemStack gym1badge = new ItemStack(Material.getMaterial(getConfig().getInt("config.gym1badge")));
                            //get item ID from config and make a as an item stack
                             if ((args[0].equalsIgnoreCase("winner") || args[0].equalsIgnoreCase("win")  || args[0].equalsIgnoreCase("w"))) {
                             if (p.hasPermission("pixelgym.leader") {
                              //if a gym leader does /gym win
                                 if (playerWinner.getName().equalsIgnoreCase(args[2])) {
                                  //if args 2 = a player from all online players
                                     if (args[1].equalsIgnoreCase("gym1")) {
                                         //if args1 = gym1
                                         myInventory.setItem(0, gym1badge);
                                           //set the player that was in args2's custom inventory and give them the gym1badge item stack. 
    //now, here is where we have a problem. It looks like there is nothing to make the player in args[2] custom inventory get opened. It looks like it would open everyone's inventory.
    // so how i can i open the player from args[2]'s inventory (not open, set the item).
                                         settings.getBadge().set("Players." + playerWinner + ".Badges", playerWinner.getInventory());
    //then here i want to set the player from args[2]'s inventory to my file that is set up in my settings class.
    //but what would i need? surely not .getInventory but its so you guys know the sort of thing i want to save, the whole inventory/item stack.
                                     }
                                 }
                           }
                             }
                         }
    So, i hope my comments explained it enough.

    Also, on a side note how would i then load the custom inventory/item stack from the file when someone does a command to view the badges.

    Thanks!

    Please give me little parts of codes or thoroughly explain what i need.
    I can only imagine people are going to tell me i need to do stuff that i have never seen before or tried xD :p
     
  2. Offline

    Scimiguy

    Actually your comments make it far harder to read
     
  3. I realized that when posting it, here i remove the comments and put them here:
    //get online players, for use in args 2
    //create an inventory for all online players and give display name of that player.
    //get item ID from config and make it as an item stack
    //if a gym leader does /gym win, continue
    //if args 2 = a player from all online players, continue
    //if args1 = gym1, continue
    //set the player that was in args2's custom inventory and give them the gym1badge item stack.
    //now, here is where we have a problem. It looks like there is nothing to make the player in args[2] custom inventory get opened. It looks like it would open everyone's inventory.
    // so how i can i open the player from args[2]'s inventory (not open, set the item).
    //then here i want to set the player from args[2]'s inventory to my file that is set up in my settings class.
    //but what would i need? surely not .getInventory but its so you guys know the sort of thing i want to save, the whole inventory/item stack.

    Code:

    Code:
    for (Player playerWinner : Bukkit.getServer().getOnlinePlayers()) {
                         
                             Inventory myInventory = Bukkit.createInventory(null, 45, playerWinner + "'s Badges!");
                            players and give display name of that player.
                             ItemStack gym1badge = new ItemStack(Material.getMaterial(getConfig().getInt("config.gym1badge")));
    
                             if ((args[0].equalsIgnoreCase("winner") || args[0].equalsIgnoreCase("win")  || args[0].equalsIgnoreCase("w"))) {
                             if (p.hasPermission("pixelgym.leader") {
                            
                                 if (playerWinner.getName().equalsIgnoreCase(args[2])) {
                               
                                     if (args[1].equalsIgnoreCase("gym1")) {
                                      
                                         myInventory.setItem(0, gym1badge);
                                       
    
                                         settings.getBadge().set("Players." + playerWinner + ".Badges", playerWinner.getInventory());
    
                                     }
                                 }
                           }
                             }
                         }
    However i have been doing some thinking on a way i could complicate it less, well in my eye's something i know how to do.

    Instead of setting the item in the inventory when they win, just set a string in config:


    Code:
    for (Player playerWinner : Bukkit.getServer().getOnlinePlayers()) {
                            
                             if ((args[0].equalsIgnoreCase("winner") || args[0].equalsIgnoreCase("win")  || args[0].equalsIgnoreCase("w"))) {
                                 if (playerWinner.getName().equalsIgnoreCase(args[2])) {
                                    for (int i = 1; i <= 32; i++) {
                                     if (args[1].equalsIgnoreCase("gym"+i)) {
                                         settings.getBadge().set("Players." + playerWinner.getName() + ".Badges." +args[1], "Won");
                                     }
                                    }
                                         Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + getConfig().getString("config.title") + ChatColor.DARK_GRAY + "] " + ChatColor.YELLOW + playerWinner.getName() + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config."+args[1]+"colour")) +  " has won the " + getConfig().getString("config."+args[1]) + " Gym Badge!");
                                         //settings.getBadge().set("Players." + playerWinner.getName() + ".Badges", playerWinner.getInventory().getContents());
                                        
                                    
                                   
                                 }
                             }
                            
                           
                         }

    Then when you open to see the badges, if the strings are as they should be set. Then give the items to the inventory.


    Code:
    if ((args[0].equalsIgnoreCase("see") || args[0].equalsIgnoreCase("check")  || args[0].equalsIgnoreCase("s"))) {
                        if (playerBadges.getName().equalsIgnoreCase(args[2])) {
                                p.openInventory(myInventory);
                        //for (int i = 1; i <= 32; i++) {
                            if (settings.getBadge().get("Players." + playerBadges.getName() + ".Badges.Gym1").equals("Won")) {
                              if (myInventory.contains(gym1badge)) {
                                  //do not set gym1 badge.
                              }
                              else {
                                myInventory.setItem(0, gym1badge);
                              }
                               
                        }
                            else if (settings.getBadge().get("Players." + playerBadges.getName() + ".Badges.Gym2").equals("Won")) {
                                if (myInventory.contains(gym2badge)) {
                                      //do not set gym1 badge.
                                  }
                                  else {
                                    myInventory.setItem(1, gym2badge);
                                  }
                            }
                     //}
                        }
                }
    Is there any sort of checks i would need to do?
    And how could i prevent when they do /gym see that it does not duplicate the items.
    I have attempted it with the if (myInventory.contains(gym1badge) {

    Thanks!

    Ok, so i have it setting to the config like so:


    Code:
    Players:
      ABkayCkay:
        Badges:
          gym1: Won
          gym2: Won
    however when i do /gym see (player name) it open's the inventory but does not seem to set the item.
    I can only guess that it is the way i am setting the item. (I HAVE TO set it as an ItemID, because it will be using modded items)

    Here is the code:


    Code:
    for (Player playerBadges : Bukkit.getServer().getOnlinePlayers()) {
                          
                            Inventory myInventory = Bukkit.createInventory(null, 45, "Badges!");
                           
                             ItemStack gym1badge = new ItemStack(Material.getMaterial(getConfig().getInt("config.gym1badge")));
                             ItemStack gym2badge = new ItemStack(Material.getMaterial(getConfig().getInt("config.gym2badge")));
                           
                            if ((args[0].equalsIgnoreCase("see") || args[0].equalsIgnoreCase("check")  || args[0].equalsIgnoreCase("s"))) {
                                if (playerBadges.getName().equalsIgnoreCase(args[1])) {
                                        p.openInventory(myInventory);
                                        p.sendMessage(ChatColor.GREEN + "Opening" + playerBadges.getName() + "'s badge showcase!");
                                //for (int i = 1; i <= 32; i++) {
                                    if (settings.getBadge().get("Players." + playerBadges.getName() + ".Badges.gym1").equals("Won")) {
                                        //settings.getBadge().set("Players." + playerWinner.getName() + ".Badges." +args[1], "Won");
                                      if (myInventory.contains(gym1badge)) {
                                          //do not set gym1 badge.
                                      }
                                      else {
                                        myInventory.setItem(0, gym1badge);
                                      }
                                      
                                }
                                    else if (settings.getBadge().get("Players." + playerBadges.getName() + ".Badges.gym2").equals("Won")) {
                                        if (myInventory.contains(gym2badge)) {
                                              //do not set gym1 badge.
                                          }
                                          else {
                                            myInventory.setItem(1, gym2badge);
                                          }
                                    }
                             //}
                                }
                        }
                          
                        }
    Config area that it is getting the itemID from:

    Code:
    gym1badge: '1'
    Any idea's?

    bump :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 2, 2015
  4. ItemStacks and lists of itemstacks are ConfigurationSerializable. What this means is you can directly put items or item lists into a config or take them out and bukkit will do all of the serializing. Take the contents of an inventory and put them into your config. When you need them, just take them out again (Remember to cast what you're taking out of the config to an itemstack!).
     
  5. Offline

    Binner_Done

    @kayc01 Don't double post please.
     
  6. @Binner_Done that's not double posting... that's bumping.
     
  7. I know right, and so bump... xD

    Here is my code to date, left this.. for some reason it just does not set anything when you open up the inventory.
    It is all done in the onCommand (as well as the inventory creation).


    Code:
    Inventory myInventory = Bukkit.createInventory(null, 45, "Badges!");
                           
                             ItemStack gym1badge = new ItemStack(Material.getMaterial(getConfig().getInt("config.gym1badge")));
                             ItemStack gym2badge = new ItemStack(Material.getMaterial(getConfig().getInt("config.gym2badge")));
                           
                            if ((args[0].equalsIgnoreCase("see") || args[0].equalsIgnoreCase("check")  || args[0].equalsIgnoreCase("s"))) {
                             if(Bukkit.getPlayer(args[1]) != null){
                                 Player playerBadges = Bukkit.getPlayer(args[1]);
    
                                    if (settings.getBadge().getString("Players." + playerBadges.getUniqueId() + ".Badges.gym1").equalsIgnoreCase("Won")) {
                                        if ((settings.getBadge().get("Players." + p.getUniqueId() +".Badges.gym1") != null)) {
                                    
                                        myInventory.setItem(0, gym1badge);
                                        myInventory.addItem(new ItemStack(Material.getMaterial(getConfig().getInt("config.gym1badge"))));
                                    
                                        }
                                }
                                    if (settings.getBadge().getString("Players." + playerBadges.getUniqueId() + ".Badges.gym2").equalsIgnoreCase("Won")) {
                                        if ((settings.getBadge().get("Players." + p.getUniqueId() +".Badges.gym1") != null)) {
                                      
                                            (1, gym2badge);
                                            myInventory.addItem(new ItemStack(Material.getMaterial(getConfig().getInt("config.gym2badge"))));
                                        }
                                    }
                                    p.openInventory(myInventory);
                                    p.sendMessage(ChatColor.GREEN + "Opening " + playerBadges.getName() + "'s badge showcase!");
                                  }
                              
                          
                        }
    Any idea's why this would not work?

    NOTE: i used myInventory.setItem and myInventory.addItem to see if either would work.
    They don't :/

    Bump

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

    Scimiguy

    @kayc01
    Please don't double post (bump or otherwise) unless it's been more than 24h
     
  9. Well it means i still am having an issue with it.
    And i needed it bumping, fair enough for the 24hr thing but you could of also tried to help? xD

    Its about to of been 24 hours so bump. xD
    Got nowhere with this yet
     
  10. Offline

    mcdorli

    If you would actually manage to open the javadocs or check the return types, then this would been sokved before it was even created. I think Inventory#getContents() returns a list, go trough it, and simply save everything by saving the material (item.getType().toString()), the amount of the item (item.getAmount()), every enchantment (item.getEnchantments() or something), lores (item.getItemMeta().getLores()), and the displayname (item.getItemMeta.getDisplayname() !do a null check first!). It isn't that hard. Then simply loop trough it, and read every data.

    I did this a while ago in my backpack plugin. Everything was fine.
     
    teej107 likes this.
  11. Offline

    teej107

    It returns an array.
    If you would actually manage to open the javadocs or check the return types, you would know.
    [​IMG]
     
  12. Why would i save data if i cannot even get it to show in the inventory to start off with?
    That's the problem.

    Unless you are saying to save/check for things differently so i can use a different way to set items.
    I don't think that is the case though.

    Welp shots fired xD
     
    Last edited by a moderator: Nov 9, 2015
  13. Offline

    mcdorli

    Wait wait wait, you don't need specific information about items, nor save them, you just need to load them into an inventory if the player has won one? Then it is far more easier. I think the problem is, that the badges are null. If an item is null, and you try to put it in an inventory, then it doesn't throw any errors. Do you use UUIDs in the config file too?

    I hate you.

    Edit: please press Ctrl+Shift+F and post the code again.
     
    Last edited: Nov 9, 2015
    teej107 likes this.
  14. RePosted Code:

    Code:
    Inventory myInventory = Bukkit.createInventory(null, 45, "Badges!");
    
                    ItemStack gym1badge = new ItemStack(Material.getMaterial(getConfig().getInt("config.gym1badge")));
                    ItemStack gym2badge = new ItemStack(Material.getMaterial(getConfig().getInt("config.gym2badge")));
    
                    if ((args[0].equalsIgnoreCase("see") || args[0].equalsIgnoreCase("check")
                            || args[0].equalsIgnoreCase("s"))) {
                        if (Bukkit.getPlayer(args[1]) != null) {
                            Player playerBadges = Bukkit.getPlayer(args[1]);
    
                            if (settings.getBadge().getString("Players." + playerBadges.getUniqueId() + ".Badges.gym1")
                                    .equalsIgnoreCase("Won")) {
                                if ((settings.getBadge().get("Players." + p.getUniqueId() + ".Badges.gym1") != null)) {
    
                                    myInventory.setItem(0, gym1badge);
                                    myInventory.addItem(
                                            new ItemStack(Material.getMaterial(getConfig().getInt("config.gym1badge"))));
    
                                }
                            }
                            if (settings.getBadge().getString("Players." + playerBadges.getUniqueId() + ".Badges.gym2")
                                    .equalsIgnoreCase("Won")) {
                                if ((settings.getBadge().get("Players." + p.getUniqueId() + ".Badges.gym1") != null)) {
    
                                    myInventory.setItem(1, gym2badge);
                                    myInventory.addItem(
                                            new ItemStack(Material.getMaterial(getConfig().getInt("config.gym2badge"))));
                                }
                            }
                            p.openInventory(myInventory);
                            p.sendMessage(ChatColor.GREEN + "Opening " + playerBadges.getName() + "'s badge showcase!");
                        }
    
                    }
    And yeah it just needs to load the items into the inventory from an item ID in the config.
    No there is no UUID's in that config, however the file that it gets from (checking if they have won) is stored as a UUID yes.

    Example:
    Code:
    Players:
      4d2405ee-084a-4de4-ae72-85b54a50562d:
        Badges:
          gym1: Won
          gym2: Won
    


    This is what the string looks like in my config (This is supposed to be the ItemID):

    Code:
    gym1badge: '1'
     
  15. Offline

    mcdorli

    You should use Material#toString() instead of using the item id (it is deprecated) and then Material#fromString(String s)

    You didn't post the whole section from the config file? Because you use "config.gym1badge" for getting the item id instead of simply "gym1badge"

    I don't see where you defined "p"

    Why do you do null checks after using .equalsignorecase?

    If you set the 0th item to a badge then add a another one and then set the 1st item to a badge too, then you delete what you placed on the secobd slot
     
    Last edited: Nov 9, 2015
  16. Could you give me the code for the new ItemStack that you are suggesting i use?
    I am getting error's in eclipse.

    Material.toString() does not want to work.
    Just to double check you are on about this line right?
    Code:
    ItemStack gym1badge = new ItemStack(Material.getMaterial(getConfig().getInt("config.gym1badge")));
    Or one of the setting/adding item line's?
    (Whichever it is, let me know and post the code that i should use instead).
    Not sure if i am just tired or being stupid.

    p is the command sender

    That's true, the null check is after the equals xD I will sort that now.

    Do not entirely understand the last part.
    Give me an example or something?

    I want it so that if they have gym1 = Won, then show the item in slot 0 and if they have gym2 = Won then show the other item in slot 1.
    If that makes sense.
     
  17. Offline

    mcdorli

    Do you store the badge materials in the config file like

    configs:
    gym1badge: 1

    Or simply

    gym1badge: 1

    2.: Material yourMaterial = Material.DIRT
    yourMaterial.toString() (returns DIRT)

    I don't want to give you code, 1.: it would eb spoonfeeding and it isn't the best thing to do 2.: I'm on my phone, writing down normal sentences is frustratong enough.
     
  18. Problem is, it needs to be itemID. It is for modded items (Pixelmon Gym Badges)
    As far as i know there is no reachable item name, it is just by ItemID.

    And yes the config has :

    Config:
    gym1badge: '1'
     
  19. Offline

    mcdorli

    But you reference it as configs.gym1badge

    Please put a nullcheck for the item id ypi got from the config and print a message if it is null.
     
  20. its not configs? Its config.
    I will do the null check now, ill re-post in a few mins. :)
    Or ill edit this post.

    EDIT:

    Did the null check:

    Code:
    if ((settings.getBadge().get("Players." + p.getUniqueId() + ".Badges.gym1") != null)) {
                                    if (settings.getBadge().getString("Players." + playerBadges.getUniqueId() + ".Badges.gym1")
                                            .equalsIgnoreCase("Won")) {
                                       
                                        if (getConfig().getString("config.gym1badge") != null) {
                                    myInventory.setItem(0, gym1badge);
                                    myInventory.addItem(
                                            new ItemStack(Material.getMaterial(getConfig().getInt("config.gym1badge"))));
                                        }
                                        else {
                                            System.out.println("gym1badge = null");
                                        }
    
                                }
                            }
    Did not give me anything in console so it is not null, however the inventory does not set any items:
    [​IMG]
     
    Last edited: Nov 9, 2015
  21. Offline

    mcdorli

    Yeah, but uppercase matters too. I'm still guessing what the problem is. I can't really figure out anything.

    settings.getBadge().get("Players."+ p.getUniqueId()+".Badges.gym1"
    Where did yoh specify "p"

    Edit: My last suggestion would be to create a debug message in every possible space, and check where it fails to cobtinue. I don't have any more ideas.
     
    Last edited: Nov 9, 2015
  22. Just noticed that should be playerBadges, but p is the sender:
    Player p = (Player)sender;

    Not sure if that change will of fixed it.
    I don't think so though.

    EDIT:

    Yeah it did not fix it.
     
  23. Offline

    mcdorli

    My last suggestion would be to create a debug message in every possible space, and check where it fails to cobtinue. I don't have any more ideas.
     
  24. Hmm ok.
    I think it is maybe how the item is setting or something?

    Code:
    myInventory.setItem(0, gym1badge);
                                    myInventory.addItem(
                                            new ItemStack(Material.getMaterial(getConfig().getInt("config.gym1badge"))));
    :s

    Or how the ItemID is being retrieved with:
    Code:
    ItemStack gym1badge = new ItemStack(Material.getMaterial(getConfig().getInt("config.gym1badge")));
     
  25. Offline

    mcdorli

    Delete the lines with the addItems, they aren't needed. Did you do the debug message thing? Where does it stopped?
     
  26. Yep,

    Here is the console output:


    Code:
    [22:05:42 INFO]: ABkayCkay issued server command: /gym see ABkayCkay
    [22:05:42 INFO]: Check command reached
    [22:05:42 INFO]: player is not null
    [22:05:42 INFO]: badges.gym1 is not null
    [22:05:42 INFO]: player has gym1badge set to won
    [22:05:42 INFO]: config gym1 badge is not null
    This means nothing is null and its just struggling on actually setting the item to the inventory.
    Am i creating the inventory correctly? It is something to do with that, or the ItemStack, not the config.

    However it could be to do with getting the ItemID from config.

    Code:


    Code:
                Inventory myInventory = Bukkit.createInventory(null, 45, "Badges!");
    
                    ItemStack gym1badge = new ItemStack(Material.getMaterial(getConfig().getInt("config.gym1badge")));
                   
                    //Material gym1badge1 = Material.toString().getConfig().getInt("config.gym1badge")));
                   
                    ItemStack gym2badge = new ItemStack(Material.getMaterial(getConfig().getInt("config.gym2badge")));
    
                    if ((args[0].equalsIgnoreCase("see") || args[0].equalsIgnoreCase("check")
                            || args[0].equalsIgnoreCase("s"))) {
                        System.out.println("Check command reached");
                        if (Bukkit.getPlayer(args[1]) != null) {
                            System.out.println("player is not null");
                            Player playerBadges = Bukkit.getPlayer(args[1]);
    
                                if ((settings.getBadge().get("Players." + playerBadges.getUniqueId() + ".Badges.gym1") != null)) {
                                    System.out.println("badges.gym1 is not null");
                                    if (settings.getBadge().getString("Players." + playerBadges.getUniqueId() + ".Badges.gym1")
                                            .equalsIgnoreCase("Won")) {
                                        System.out.println("player has gym1badge set to won");
                                       
                                        if (getConfig().getString("config.gym1badge") != null) {
                                            System.out.println("config gym1 badge is not null");
                                    myInventory.setItem(0, gym1badge);
                                        }
                                        else {
                                            System.out.println("gym1badge = null");
                                        }
    
                                }
                            }
    
                                if ((settings.getBadge().get("Players." + playerBadges.getUniqueId() + ".Badges.gym1") != null)) {
                                    if (settings.getBadge().getString("Players." + playerBadges.getUniqueId() + ".Badges.gym2")
                                            .equalsIgnoreCase("Won")) {
                                       
                                        if (getConfig().getString("config.gym1badge") != null) {
    
                                    myInventory.setItem(1, gym2badge);
                                        }
                                        else {
                                            System.out.println("gym2badge = null");
                                          }
                                        }
                                }
                           
                            p.openInventory(myInventory);
                            p.sendMessage(ChatColor.GREEN + "Opening " + playerBadges.getName() + "'s badge showcase!");
                       
                        }
    
                    }
     
  27. Offline

    mcdorli

    Then do two thing, prin tout what you get from the config as item id and print out, what item is in your inventorys 1st ppace after you placed the badge in it.
     
  28. Ok ill do the first part, but i do not quite understand your 2nd question?
    It does not set anything.

    As a test i was using:
    gym1badge: '1'

    Which would be stone or something.
    But nothing is set in the inventory.

    EDIT:

    Here is what it said, ItemStack = Air? :/
    At least we know thats what the issue is now, but the int in the config is 1, not 0.

    Code:
    [13:54:14 INFO]: gym1badge:ItemStack{AIR x 1}
    [13:54:14 INFO]: Check command reached
    [13:54:14 INFO]: player is not null
    [13:54:14 INFO]: badges.gym1 is not null
    [13:54:14 INFO]: player has gym1badge set to won
    [13:54:14 INFO]: config gym1 badge is not null
     
  29. Offline

    mcdorli

    @kayc01 Please post me the section of the config with the gym1badge and at least 5 - 5 lines before and after it.
     
    Last edited: Nov 10, 2015
Thread Status:
Not open for further replies.

Share This Page