Saving items in a GUI?

Discussion in 'Plugin Development' started by lonemineah_, Sep 5, 2015.

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

    lonemineah_

    Hi! I am making a preferences plugin for my server and need some help. I have the GUI code down so that when you open it has the options. It looks like this:
    upload_2015-9-5_15-32-57.png
    I also have it so that when you click the lime dye, it changes it to gray dye and the name turns to a red Disabled. I need to make it so that when I open the GUI, it save the icon at position 19 and sets it there. Right now, it changes when I click the item, but when I reopen the GUI, it resets the icon to lime dye automatically.
    Code:
       
        public ItemStack visible = new ItemStack(Material.EYE_OF_ENDER, 1);
        public ItemMeta visiblem = visible.getItemMeta();
        public ItemStack enabled = new ItemStack(Material.INK_SACK, 1, (short) 10);
        public ItemMeta em = enabled.getItemMeta();
        public ItemStack disabled = new ItemStack(Material.INK_SACK, 1, (short) 8);
        public ItemMeta dm = disabled.getItemMeta();
        public ArrayList<String> el = new ArrayList<String>();
        public ArrayList<String> dl = new ArrayList<String>();
        public ArrayList<String> visiblel = new ArrayList<String>();
    public void openPREFS(Player p) {
            prefs = Bukkit.createInventory(null, 54, "Preferences");
        
            visiblem.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Player Visibility");
        
            em.setDisplayName(ChatColor.GREEN + "Enabled");
            enabled.setItemMeta(em);
       
            dm.setDisplayName(ChatColor.RED + "Disabled");
            disabled.setItemMeta(dm);
        
            prefs.setItem(40, back);
            prefs.setItem(10, visible);
            p.openInventory(prefs);
        }
    Thank you!

    PLEASE NOTE: Before you comment, please know that I DO have knowledge of Java for Bukkit. Remember, HDD (Help! Don't Hate.)
     
    Last edited: Sep 5, 2015
  2. Offline

    ShadowLAX

    @lonemineah_ I'm assuming (since you don't show what each of your variables are and do not show what happens when you click it) that you do not store the result of the click in a map for calling later when the inventory is reopened. If you don't have a check and change it depending on what the player has selected, it will obviously be the same every time they open it.
     
  3. Offline

    lonemineah_

    Sorry, the variables are in a public at the top. How would I make a map for calling later?
     
  4. Offline

    meguy26

    @lonemineah_
    One tiny problem with your last comment.... Bukkit runs on Java, not JavaScript.
     
  5. Offline

    lonemineah_

    LOL! I was typing fast. Thanks for reminding me I will edit. =D
     
  6. Offline

    ShadowLAX

    @meguy26 Yeah I noticed that too. Lol.

    @lonemineah_ Using a HashMap (UUID, Boolean) is what I would use. However, when the server is reloaded/stopped, whatever collection you do use to store your data will be lost. I also recommend that you store it externally in the plugins data folder using a YAML file as well.
     
    Last edited: Sep 5, 2015
  7. Offline

    lonemineah_

    One more question @ShadowLAX, how would I create a YML file to store data?

    Thank you for all your help! <3

    Sorry, also could you help me figure out so that when you click the dye again, it changes back to lime? Thank you!
     
    Last edited: Sep 6, 2015
  8. Offline

    ShadowLAX

    @lonemineah_ Create it in your project as you would your plugin.yml (though give it a different name of course). Then, you can use
    Code:
    YamlConfiguration.loadConfiguration(new File(MainClass.getDataFolder(), "your_file.yml"));
    to load and use it. And no problem :p

    EDIT: Almost forgot, it's a good idea to check and see if the file is already created or not in your plugins using the file's exists() method, and if it isn't use
    Code:
    MainClass.saveResource("your_file", false);
     
  9. Offline

    lonemineah_

    @ShadowLAX
    NICE! Thanks!

    My VERY last question for today is:

    Could you help me out so that when you click the dye again, it changes back to lime dye?

    Thank you so much!!
     
    Last edited: Sep 5, 2015
  10. Offline

    ShadowLAX

    @lonemineah_ Just check for the opposite of the value you use to tell if they have it enabled, which is the reason I used a boolean for the value in map suggestion. If enabled is true, then disabled is false and use an if/else statement to check for one.
     
  11. Offline

    lonemineah_

    @ShadowLAX
    I don't really know how to do that. Could you please post some code? Thanks!
     
  12. Offline

    ShadowLAX

    @lonemineah_ As simple as
    Code:
    if (enabled) {
    //Dye color green
    } else {
    //Dye color red
    }
    in your click code, with "enabled" being the value returned from where you store the booleans.
     
  13. Offline

    lonemineah_

    @ShadowLAX
    lol. I get confused with booleans and HashMaps. Those are my weak points. Could you help me with storing booleans?
     
  14. Offline

    Zombie_Striker

  15. Offline

    ShadowLAX

    @lonemineah_ What @Zombie_Striker said above is the basic outline for what a HashMap is. I linked to the official Oracle tutorial on Maps in an earlier post, you should learn how to use them there. A boolean, if you have taken the time to learn some basic Java, you should know.
     
  16. Offline

    lonemineah_

    I do know booleans
     
Thread Status:
Not open for further replies.

Share This Page