Solved Inventory Menu not changing

Discussion in 'Plugin Development' started by monkeymanboy, Jun 20, 2014.

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

    monkeymanboy

    So I am trying to create a score screen where it shows people least deaths and fastest time on parkour courses the first time I open the menu it's fine shows all the correct data but the menu won't update to the new data until I reload

    Here is what I am doing to make the menu
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("scores")){
    2. if(args.length == 1 && sender instanceof Player){
    3. Player player = (Player) sender;
    4. String PlayerUUID = UUIDApi.getUUIDAsString(args[0]);
    5. Inventory inv = Bukkit.createInventory(player, 27, ChatColor.BLACK + "Scores");
    6. inv.clear();
    7. if(PlayerUUID != null){
    8. for(String string : plugin.getConfig().getConfigurationSection("courses").getKeys(false)){
    9. ItemStack is = plugin.getConfig().getItemStack("courses." + string + ".item").clone();
    10. ItemMeta im = is.getItemMeta();
    11. List<String> lore = new ArrayList<String>();
    12. lore.add(" ");
    13. if(parkour.getPlayers().contains(PlayerUUID + ".courses." + string + ".normal")){
    14. lore.add(ChatColor.BLUE + "Best Normal Time: " + parkour.getPlayers().get(PlayerUUID + ".courses." + string + ".normal.time"));
    15. lore.add(" ");
    16. lore.add(ChatColor.BLUE + "Least Normal Deaths: " + parkour.getPlayers().get(PlayerUUID + ".courses." + string + ".normal.deaths"));
    17. lore.add(" ");
    18. } else {
    19. lore.add(ChatColor.DARK_BLUE + "Never completed this");
    20. lore.add(ChatColor.DARK_BLUE + "course in normal mode");
    21. lore.add(" ");
    22. }
    23. if(parkour.getPlayers().contains(PlayerUUID + ".courses." + string + ".hard")){
    24. lore.add(ChatColor.BLUE + "Best Hardcore Time: " + parkour.getPlayers().get(PlayerUUID + ".courses." + string + ".hard.time"));
    25. lore.add(" ");
    26. } else {
    27. lore.add(ChatColor.DARK_BLUE + "Never completed this");
    28. lore.add(ChatColor.DARK_BLUE + "course in hardcore mode");
    29. lore.add(" ");
    30. }
    31. im.setLore(lore);
    32. is.setItemMeta(im);
    33. inv.addItem(is);
    34. }
    35. player.openInventory(inv);
    36. } else
    37. player.sendMessage(prefix + ChatColor.DARK_RED + "That player does not exist");
    38. }


    But I think it might be a problem with my players.yml not giving the new information

    I have a class where I put all my methods and these are my players.yml methods
    Code:java
    1. File playerconfigfile = new File("plugins/Parkour", "players.yml");
    2. FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerconfigfile);
    3.  
    4. public void savePlayers(){
    5. try {
    6. playerConfig.save(playerconfigfile);
    7. } catch (IOException e) {
    8. e.printStackTrace();
    9. }
    10. }
    11. public FileConfiguration getPlayers(){
    12. return playerConfig;
    13. }


    I also have it do savePlayers(); in my onEnable
     
  2. Offline

    ElliottOlson

    This is just a guess, but try saving the players.yml when they open up the inventory. I believe this might fix the issue, because it would update the newest version for each player as they check! If this solution doesn't fix it, I may have other ways to fix it.
     
  3. Offline

    monkeymanboy

    I don't think that would work since I make a new inventory and have the player open it every time they do /scores <playername>

    @ElliotOlson Ok so I did some testing with it and it saves the data to the yml fine but it will send back the old data and not the new data until I reload
    So maybe if I reload the players.yml I just can't figure out how to do that

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

    teej107

    monkeymanboy Didn't you load the players.yml at the beginning of your plugin or at least at some point before? Just do the same thing you did there.
     
  5. Offline

    monkeymanboy

    I saved the plugin in the onEnable with savePlayers() function like this
    Code:java
    1. File playerconfigfile = new File("plugins/Parkour", "players.yml");
    2. FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerconfigfile);
    3.  
    4. public void savePlayers(){
    5. try {
    6. playerConfig.save(playerconfigfile);
    7. } catch (IOException e) {
    8. e.printStackTrace();
    9. }
    10. }
    11. public FileConfiguration getPlayers(){
    12. return playerConfig;
    13. }
     
  6. Offline

    teej107

    monkeymanboy That is not what I said. I said use the same code that you used to load the yml in the beginning. Not save.
     
  7. Offline

    monkeymanboy

    I just made the variable start as yamlconfiguration.load I never set it to that so in the onEnable I never loaded it all I did was save it
     
  8. Offline

    teej107

    monkeymanboy
    Code:java
    1. YamlConfiguration.loadConfiguration(playerconfigfile);
    I haven't read the JavaDoc for that but I can assume that method returns a FileConfiguration. So yes you have acutally loaded the config.
     
  9. Offline

    monkeymanboy

    teej107
    I tried making the function reloadPlayers where it does playerConfig = YamlConfiguration.loadConfiguration(playerconfigfile); and used that every time I saved the config but that didn't change anything
     
  10. Offline

    teej107

    monkeymanboy Current code please? Because the code you have given doesn't show that.
     
  11. Offline

    monkeymanboy

    teej107 Here is how I am saving the data
    Code:java
    1. if(!parkour.getPlayers().contains(PlayerUUID + ".courses." + parkour.getCurrentCourse(player) + ".normal") || parkour.getPlayers().getInt(PlayerUUID + ".courses." + parkour.getCurrentCourse(player) + ".normal.time") >= parkour.getCurrentTime(player)){
    2. player.sendMessage(prefix + "You got a new personal best time!");
    3. parkour.getPlayers().set(PlayerUUID + ".courses." + parkour.getCurrentCourse(player) + ".normal.time", parkour.getCurrentTime(player));
    4. parkour.savePlayers();
    5. parkour.reloadPlayers();
    6. }

    and here is the reloadPlayers function
    Code:java
    1. public void reloadPlayers(){
    2. playerConfig = YamlConfiguration.loadConfiguration(playerconfigfile);
    3. }


    EDIT: Sorry I needed the variables to be static so it works now
     
Thread Status:
Not open for further replies.

Share This Page