Why is it reading from integrated config.yml not the folder one?

Discussion in 'Plugin Development' started by MGlolenstine, May 24, 2017.

Thread Status:
Not open for further replies.
  1. So as you can see in the following picture[​IMG]
    It's reading Example from integrated config.yml not from the config file I have opened in the background.

    Default config file:
    Code:
    enabled:
        rightclick: true
    rightclick_items:
        amount_of_items: 1
        '0':
            id: COMPASS
            command: gui Example
    guis:
        number_of_guis: 1
        '0':
            size: 9
            name: Example
            '0':
                id: DIAMOND_SWORD
                name: Factions
                amount: '1'
                action: server factions
            '1':
                id: GRASS
                name: SkyBlock
                amount: '1'
                action: server skyblock
            '2':
                id: WOOL
                name: Vegova
                amount: '1'
                action: server vegova
            '3':
                id: SPONGE
                name: Vaaraiant
                amount: '1'
                action: server vaaraiant
            '4':
                id: AIR
                name:
                amount: '1'
            '5':
                id: AIR
                name:
                amount: '1'
            '6':
                id: AIR
                name:
                amount: '1'
            '7':
                id: AIR
                name:
                amount: '1'
            '8':
                id: AIR
                name:
                amount: '1'

    I did this at the beginning of the Main.java
    Code:
        File f = new File(getDataFolder(), "config.yml");
            if(!f.exists()){
                getLogger().info("config.yml not found, creating!");
                this.saveDefaultConfig();
                this.saveConfig();
            }
    and then I'm reading with
    Code:
    Main.getPlugin(Main.class).getConfig().get("guis.0.name");
    I know it's not in the config, but why it doesn't throw an error, but instead it reads from integrated config.yml?

    Thanks for the help!

    FYI: There are no errors in the console.
     
  2. Online

    timtower Administrator Administrator Moderator

    @MGlolenstine 1. Pass that main instance around using constructors please, not with what it is that you have.
    2. Please post your full main class and whatever the second class is.
     
  3. MAIN:
    Code:
    public class Main extends JavaPlugin{
        public void onEnable(){
            File f = new File(getDataFolder(), "config.yml");
            if(!f.exists()){
                getLogger().info("config.yml not found, creating!");
                this.saveDefaultConfig();
                this.saveConfig();
            }
            Bukkit.getPluginManager().registerEvents(new MyListener(), this);
            this.getCommand("gui").setExecutor(new CommandGui());
        }
    }
    
    MyListener (open)

    Code:
    public class MyListener implements Listener{
        Main plugin = Main.getPlugin(Main.class);
        @EventHandler
        public void onItemGrab(InventoryClickEvent e){
            Inventory inv = e.getInventory();
            String name = inv.getName();
            boolean exists = false;
            int num = plugin.getConfig().getInt("guis.number_of_guis");
            e.getWhoClicked().sendMessage("Hello? num: "+num);
            for(int i = 0; i < num; i++){
                e.getWhoClicked().sendMessage(plugin.getConfig().getString("guis."+i+".name") + " == " + name);
                if(plugin.getConfig().getString("guis."+i+".name").equalsIgnoreCase(name)){
                    exists = true;
                }
            }
            if(exists){
            //e.getWhoClicked().sendMessage("The name of inventory is "+name);
            //if(plugin.getConfig().get("guis."+num) != null){
                ItemStack is1 = new ItemStack(Material.WOOL, 1, (byte)5);
                ItemStack is2 = new ItemStack(Material.WOOL, 1, (byte)14);
                ItemMeta im1 = is1.getItemMeta();
                im1.setDisplayName("Submit your new GUI!");
                is1.setItemMeta(im1);
                ItemMeta im2 = is2.getItemMeta();
                im2.setDisplayName("Delete your new GUI!");
                is2.setItemMeta(im2);
                if(e.getCurrentItem() == is1){
                    //e.getWhoClicked().sendMessage("It was is1!");
                    e.setCancelled(true);
                    int k = 0;
                    //e.getWhoClicked().sendMessage("name: "+name);
                    int index2 = plugin.getConfig().getInt("guis.number_of_guis");
                    plugin.getConfig().set("guis."+index2+".name", name);
                    plugin.getConfig().set("guis.number_of_guis", index2+1);
                    plugin.getConfig().set("guis."+index2+".size", inv.getSize()-9);
                    //e.getWhoClicked().sendMessage("Inv.size(): "+inv.getSize());
                    for(int i = 0; i < inv.getSize()-9; i++){
                        ItemStack ist = inv.getItem(i);
                        if(ist == null || ist.getType() == Material.AIR){
                            plugin.getConfig().set("guis."+index2+"."+i+".id", "AIR");
                            plugin.getConfig().set("guis."+index2+"."+i+".name", "");
                            plugin.getConfig().set("guis."+index2+"."+i+".amount", 1);
                            plugin.getConfig().set("guis."+index2+"."+i+".action", "");
                        }else{
                            plugin.getConfig().set("guis."+index2+"."+i+".id", ist.getType().toString());
                            if(ist.getItemMeta().hasDisplayName()){
                                plugin.getConfig().set("guis."+index2+"."+i+".name", ist.getItemMeta().getDisplayName());
                            }else{
                                plugin.getConfig().set("guis."+index2+"."+i+".name", "");
                            }
                            plugin.getConfig().set("guis."+index2+"."+i+".amount", ist.getAmount());
                            if(ist.getItemMeta().getLore() != null){
                                plugin.getConfig().set("guis."+index2+"."+i+".action", ist.getItemMeta().getLore().get(ist.getItemMeta().getLore().size()-1));
                            }else{
                                plugin.getConfig().set("guis."+index2+"."+i+".action", "");
                            }
                        }
                    }
                    plugin.saveConfig();
                    //e.getWhoClicked().sendMessage("Your gui has been submitted!");
                    e.getWhoClicked().closeInventory();
                }else if(e.getCurrentItem() == is2){
                    //e.getWhoClicked().sendMessage("It was is2!");
                    e.setCancelled(true);
                    e.getWhoClicked().closeInventory();
                }else{
                    e.setCancelled(true);
                    //e.getWhoClicked().sendMessage("It was something else!");
                    //e.getWhoClicked().sendMessage("HELLU!");
                    for(int i = 0; i < num; i++){
                        if(plugin.getConfig().getString("guis."+i+".name").equalsIgnoreCase(name)){
                            num = i;
                            break;
                        }
                    }
                    for(int i = 0; i < plugin.getConfig().getInt("guis."+num+".size"); i++){
                        //e.getWhoClicked().sendMessage("Num = "+num+"; i = "+i);
                        ItemStack is = new ItemStack(Material.getMaterial(plugin.getConfig().getString("guis."+num+"."+i+".id")), plugin.getConfig().getInt("guis."+num+"."+i+".amount"));
                        if(!plugin.getConfig().getString("guis."+num+"."+i+".name").isEmpty()){
                            String itemName = plugin.getConfig().getString("guis."+num+"."+i+".name");
                            ItemMeta m = is.getItemMeta();
                            m.setDisplayName(itemName);
                            is.setItemMeta(m);
                        }
                        //e.getWhoClicked().sendMessage("Yello?!");
                        //if(e.getCurrentItem().getItemMeta().getDisplayName() == is.getItemMeta().getDisplayName()){
                        if(e.getCurrentItem().hasItemMeta() == is.hasItemMeta()){
                            //e.getWhoClicked().sendMessage("They are the same!");
                            if(e.getCurrentItem().getType() == is.getType()){
                                //e.getWhoClicked().sendMessage("They are the same 2x!");
                                e.setCancelled(true);
                                String command = plugin.getConfig().getString("guis."+num+"."+i+".action");
                                //e.getWhoClicked().sendMessage("Command is /"+command);
                                if(command != null){
                                    //e.getWhoClicked().sendMessage("[MGGuiManager] Command /"+command+" has been dispatched!");
                                    Bukkit.dispatchCommand(Bukkit.getPlayer(e.getWhoClicked().getName()), command);
                                }else{
                                    e.getWhoClicked().sendMessage("[MGGuiManager] There is no command connected to this item!");
                                }
                                break;
                            }
                        }
                    }
                }
            }
            //}
        }
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onRightClick(PlayerInteractEvent e){
            Player p = e.getPlayer();
            if(plugin.getConfig().getBoolean("enabled.rightclick")){
                if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
                    for(int i = 0; i < plugin.getConfig().getInt("rightclick_items.amount_of_items"); i++){
                        if(p.getItemInHand().getType() == Material.getMaterial(plugin.getConfig().getString("rightclick_items."+i+".id"))){
                            String command = plugin.getConfig().getString("rightclick_items."+i+".command");
                            Bukkit.dispatchCommand(p, command);
                        }
                    }
                }
            }
        }
    }


    I hope you're gonna be able to understand at least some of this code...
     
  4. Online

    timtower Administrator Administrator Moderator

    @MGlolenstine If you re-open the config in the server, which one do you see then?
     
  5. I see this:
    Code:
    enabled:
      rightclick: true
    rightclick_items:
      amount_of_items: 1
      '0':
        id: COMPASS
        command: gui Example
    guis:
      number_of_guis: 2
      '1':
        name: skyblock
      '0': {}
      '2':
        name: skyblock
    
    @timtower ?
     
    Last edited: May 25, 2017
  6. @timtower *bump*

    Sent from my E2303 using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page