Solved Config from other package

Discussion in 'Spigot Plugin Development' started by Legomix13, Jun 27, 2016.

  1. Hi,how can I access the config from other package?
    Thanks in advance
     
  2. Grab an instance of your main class (you can use a singleton for this) then call something like mainClassVariable.getConfig()
     
  3. It returns null
     
    • Optimistic Optimistic x 1
  4. Learn Java
    Develop plugins
    ...
    Profit
     
    • Agree Agree x 1
    • Funny Funny x 1
  5. @stonar96 come on, he's not gonna learn by saying that

    for the OP -
    make an instance of your main class by doing this
    Code (Text):
    X mainclass;

    public Y(X mainclass) {
    this.mainclass = mainclass;
    }
    (replace X with your main class's name, and replace y with your current class's name)
    and from there on do mainclass#getConfig()#etc

    EDIT - if you have
    new Y()
    written anywhere in your main class replace it with
    new Y(this)
     
    • Agree Agree x 1
    • Winner Winner x 1
  6. Its not working... look my code:
    MAIN:
    Code (Text):
    package com.gmail.Legomix13.Magia;

    import com.gmail.Legomix13.Magia.Commands.SelectClass;
    import com.gmail.Legomix13.Magia.Eventos.Join;
    import com.gmail.Legomix13.Magia.FirstJoin.FJInventory;
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener{
        FJInventory fjinv = FJInventory.getInstance();
        @Override
        public void onEnable(){
            getConfig().options().copyDefaults(true);
            saveConfig();
            Bukkit.getPluginManager().registerEvents(this, this);
            Bukkit.getPluginManager().registerEvents(new Join(), this);
            saveDefaultConfig();
            getCommand("sclass").setExecutor(new SelectClass());
            Bukkit.broadcastMessage(getConfig().getString("first-join-menu-mage-class-name"));
        }
        @Override
        public void onDisable(){
            saveConfig();
        }
    }
    JFInventory
    Code (Text):


    package com.gmail.Legomix13.Magia.FirstJoin;

    import com.gmail.Legomix13.Magia.Main;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;


    public class FJInventory {
        Main main;

        public FJInventory(Main main) {
        this.main = main;
        }
        private FJInventory() { }static FJInventory instance = new FJInventory();public static FJInventory getInstance() {return instance;}
        private Inventory inv;
        private void createItems(){
            ItemStack stick = new ItemStack(Material.STICK,1);
            ItemMeta stickMeta = (ItemMeta)stick.getItemMeta();
            stickMeta.setDisplayName(main.getConfig().getString("first-join-menu-mage-class-name"));
            stick.setItemMeta(stickMeta);
            inv.setItem(1, stick);
        }
        private void createInv(Player p){
            inv = Bukkit.createInventory(p, 9, main.getConfig().getString("first-join-menu-name"));
        }
        private void openInv(Player p){
            p.openInventory(inv);
        }
        public void prepareAll(Player p){
            p.sendMessage(main.getConfig().getString("first-join-menu-mage-class-name"));
            createInv(p);
            createItems();
            openInv(p);
           
        }
    }
     
    SELECT CLASS
    Code (Text):


    package com.gmail.Legomix13.Magia.Commands;

    import com.gmail.Legomix13.Magia.FirstJoin.FJInventory;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class SelectClass implements CommandExecutor{
        FJInventory fjinv = FJInventory.getInstance();
        @Override
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          Player player = (Player)sender;
          if(cmd.getName().equalsIgnoreCase("sclass")){
              fjinv.prepareAll(player);
              return true;
          }
            return true;
       }
    }
     
    Thanks
     
  7. I did it but stay not working
     
  8. move:

    private FJInventory() { }static FJInventory instance = new FJInventory();public static FJInventory getInstance() {return instance;}


    to your command class and do the following:
    FJInventory instance = new FJInventory(this); (so remove the rest)
    and remove FJInventory fjinv = FJInventory.getInstance();
    and just do instance.prepareAll(player)
     
    • Optimistic Optimistic x 1
  9. Select class cannot be converted to main...
     
  10. ah fail, you got it in an executor i was brain afk ^^, so mb the trick is to create a static Variable of your main class, and init it in onEnable, that was the plan, but cause you started with the constructor i wanted to hold this sheme that u wont get confused
     
  11. Easiest way to do it:

    Code (Text):
    package com.gmail.Legomix13.Magia;

    import com.gmail.Legomix13.Magia.Commands.SelectClass;
    import com.gmail.Legomix13.Magia.Eventos.Join;
    import com.gmail.Legomix13.Magia.FirstJoin.FJInventory;
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener{
        FJInventory fjinv = FJInventory.getInstance();

        //add this first
        public static Main instance;

        @Override
        public void onEnable() {
            //very important
            instance = this;
            getConfig().options().copyDefaults(true);
            saveConfig();
            Bukkit.getPluginManager().registerEvents(this, this);
            Bukkit.getPluginManager().registerEvents(new Join(), this);
            saveDefaultConfig();
            getCommand("sclass").setExecutor(new SelectClass());
            Bukkit.broadcastMessage(getConfig().getString("first-join-menu-mage-class-name"));
        }
     
        @Override
        public void onDisable(){
            saveConfig();
        }

        public static Main getInstance() {
            return instance;
        }

    }
    And then use it as this:
    Code (Text):
    Main.getInstance().getConfig()...
     
    • Agree Agree x 1
    • Creative Creative x 1
  12. Thanks so much!!!!
     
    • Friendly Friendly x 1
  13. Ew. Ugly.
    don't forget to mark the thread swolved :3