Solved Inventory name from config

Discussion in 'Plugin Development' started by xepisolonxx, Oct 24, 2014.

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

    xepisolonxx

    I cannot set the name of the inventory to the one in the config. I would like a solution to this promblem.

    Code
    Code:
        public Main plugin;
       
        public Gui(Main instance){
        this.plugin = instance;
            }
     
            public String getInvname(){
                FileConfiguration file = plugin.getConfig();
               
                return ChatColor.translateAlternateColorCodes('&', file.getString("main.invname"));
     
                }
           
              public static  Inventory inv = Bukkit.createInventory(null, 18 , getInvname());
     
     
           
     
     
           
       
      public static void createinv(){
     
        for(int i = 0; i < Duty.OnDuty.size(); i++){ //Where players is an array of the players in-game
          String playerName = Duty.OnDuty.get(i).toString();
          ItemStack item = new ItemStack(Material.SKULL_ITEM);
          ItemMeta meta = item.getItemMeta();
          item.setDurability((short) 3);
     
          meta.setDisplayName(playerName);
          item.setItemMeta(meta);
          inv.setItem(i, item);
                }
            }
        }
    

    Main class
    Code:
    package jr.net.me;
     
    import jr.net.me.cmd.Duty;
    import jr.net.me.cmd.Gui;
     
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener {
       
       
        @Override
        public void onEnable(){
           
     
        PluginManager pm = Bukkit.getPluginManager();
        pm.registerEvents(this, this);
        pm.registerEvents(new Gui(this), this);
        getCommand("duty").setExecutor(new Duty(this));
        saveDefaultConfig();
        }
    }
        
     
  2. Offline

    acer5999

    Do you have any errors?
     
  3. Offline

    mine-care

    Again let me point that excessive use of static should be avoided :3
     
  4. Offline

    xepisolonxx

    acer5999 Red line next to getInvname becuase its not static and if i add static to
    Code:java
    1. public String getInvname(){

    then the file variable turns red and says to remove invalid modifiers
    Code:
      public String getInvname(){
                FileConfiguration file = plugin.getConfig();
             
                return ChatColor.translateAlternateColorCodes('&', file.getString("main.invname"));
     
                }
         
              public static  Inventory inv = Bukkit.createInventory(null, 18 , getInvname());
     
     
         
    
     
  5. Offline

    acer5999

    remove file variable and just write plugin.getConfig().getString()
     
  6. Offline

    xepisolonxx

    acer5999 Errors onlines 28 23 null pointer exception , i also tried declaring main plugin in main class and getting it from there but still no
    Code:java
    1. package jr.net.me.cmd;
    2.  
    3. import jr.net.me.Main;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.inventory.Inventory;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.inventory.meta.ItemMeta;
    12.  
    13. public class Gui implements Listener {
    14.  
    15. public Main plugin;
    16.  
    17. public Gui(Main instance){
    18. this.plugin = instance;
    19. }
    20.  
    21. public static String getInvname(){
    22.  
    23. return ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("main.invname"));
    24.  
    25. }
    26.  
    27.  
    28. public static Inventory inv = Bukkit.createInventory(null, 18 , getInvname());
    29.  
    30.  
     
  7. Offline

    xepisolonxx

  8. Offline

    mine-care

    Okay you are accessing main.plugin but in your main class I cannot see a variable called plugin in it. Please repost da code of main.
     
  9. Offline

    xepisolonxx

    mine-care An attempt to try and register plugin so it wont be null idk how to get it without it being ull or static also happy haloween
    Code:
       
    public static Main plugin;
           
        @Override
        public void onEnable(){
     
        PluginManager pm = Bukkit.getPluginManager();
        pm.registerEvents(this, this);
        pm.registerEvents(new Gui(), this);
        getCommand("duty").setExecutor(new Duty(this));
        saveDefaultConfig();
        this.plugin = plugin;
     
        }
    @Override
    public void onDisable(){
        this.plugin = null
        }
    }
    Solved on my own code for people with same issue
    Code:
    package jr.net.me;
     
    import java.util.logging.Logger;
     
    import jr.net.me.cmd.Duty;
    import jr.net.me.cmd.Gui;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener {
       
        private Logger log = Logger.getLogger("Minecraft");
        public static Inventory inv;
        public static int invtotal;
        public static String invname;
           
        @Override
        public void onEnable(){
     
        PluginManager pm = Bukkit.getPluginManager();
        pm.registerEvents(this, this);
        pm.registerEvents(new Gui(), this);
        getCommand("duty").setExecutor(new Duty(this));
        log.info("\u001B[34m[OnDuty] Has Been Enabled!\u001B[37m");
        saveDefaultConfig();
        invtotal = getConfig().getInt("main.invtotal");
        invname = ChatColor.translateAlternateColorCodes('&', getConfig().getString("main.invname"));
        inv = Bukkit.createInventory(null, invtotal, invname);
        System.out.println("Iventory Name: " + invname + " Total: " + invtotal);
        }
       
    @Override
    public void onDisable(){
        log.info("\u001B[34m[OnDuty] Has Been Disabled!\u001B[37m");
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page