Saving to config

Discussion in 'Plugin Development' started by tylerthecreeper1, Feb 11, 2016.

Thread Status:
Not open for further replies.
  1. Hi, I'm trying to save player name and an enum to my config, but it is not saving anything.

    Here's the code, it's for gadgets for a plugin I'm making, handling when a player buys them.
    When they click the shop item:

    Code:
                if(e.getCurrentItem().getType() == Material.GOLD_BARDING) {
                    ShopAPI.buyGadget((Player) e.getWhoClicked(), Gadget.PaintballGun, 50);
                    e.getWhoClicked().closeInventory();
                }
    The buyGadget() method:
    Code:
        public static void buyGadget(Player p, Gadget gadget, int price) {
            FileConfiguration config = CloudyHub.getInstance().getConfig();
            if(!config.getStringList("Users." + p.getName()).contains(gadget.toString())) {
                if(Tokens.getTokens(p) >= price) {
                    Tokens.removeTokens(p, price);
                    config.getStringList("Users." + p.getName()).add(gadget.toString());
                    CloudyHub.getInstance().saveConfig();
                    CloudyHub.getInstance().reloadConfig();
                    p.playSound(p.getLocation(), Sound.NOTE_PLING, 5, 5);
                    Utils.sendPlayerMsg(p, ChatColor.GREEN + "You purchased " + ChatColor.YELLOW + gadget +
                            ChatColor.GREEN + " for " + ChatColor.YELLOW + price + ChatColor.GREEN + " tokens!");
                } else {
                    Utils.sendPlayerMsg(p, ChatColor.RED + "You don't have enough tokens!");
                }
            } else {
                Utils.sendPlayerMsg(p, ChatColor.RED + "You already own that gadget!");
            }
        }
    It works fine telling me I don't have enough tokens so I know the rest of the code is working, it's just when I "buy" it, it won't save it to the config like it's suppose to. There's no errors in console, just not saving to file. Any help would be greatly appreciated, been stuck with this all day. Thanks
     
  2. Offline

    pie_flavor

    @tylerthecreeper1 The list is a copy. You need to get it, add the item, then set it back again.
     
  3. would this work?


    Code:
                    List<String> list = config.getStringList("Users." + p.getName());
                    list.add(gadget.toString());
                    config.set("Users." + p.getName(), list);
     
  4. Offline

    mcdorli

    T
    try it out
     
  5. Offline

    DoggyCode™

    Store the players UUID, not the name.
     
  6. Offline

    pie_flavor

  7. oh yeah, haha. gonna do that.
    it did, thanks.

    I have another question maybe one of you could answer, how can I get the player that is viewing an inventory? So if I have a player head in the inventory I can set the owner as the player? I know there's getViewers() but I don't think that is the same as what I want to do.
     
  8. Offline

    pie_flavor

  9. I was having an issue getting the player.


    Code:
        public static void getInvViewer(Inventory inv) {
            for(HumanEntity p : inv.getViewers()) {
                if(p instanceof Player) {
                    p.getName();
                }
            }
        }
    I try to use Player viewer = getInvViewer(GUIManager.trails); but there is an error because it can't return Player.
     
    Last edited: Feb 12, 2016
  10. Offline

    teej107

  11. Offline

    Gerov

    In Java if you want a method to return something, it cannot be void, it has to be a method of the object, or primitive you want to return.
    @tylerthecreeper1
     
  12. I figured it out, thanks for the help guys.
     
Thread Status:
Not open for further replies.

Share This Page