Why doesn't this simple thing work?

Discussion in 'Plugin Development' started by bloodless2010, Sep 23, 2013.

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

    bloodless2010

    So I'm trying to make a simple settings manager but I'm getting this error
    "Cannot make a static reference to the non-static method getConfig() from the type JavaPlugin"
    Here is my custom inventory I'm trying to do;
    Code:java
    1. public static Inventory settingsMenu = Bukkit.createInventory(null, 18, ChatColor.RED+"Server settings manager");
    2. static {
    3. FileConfiguration config = getConfig();
    4. boolean allowChat = config.getBoolean("ezcontrol.allowChat");
    5.  
    6. ItemStack allowChatItem;
    7. ChatColor allowChatColour;
    8. String allowChatStatus;
    9.  
    10. if (allowChat) {
    11. allowChatColour = ChatColor.GREEN;
    12. allowChatItem = new ItemStack(351, 10);
    13. allowChatStatus = "On";
    14. } else {
    15. allowChatColour = ChatColor.RED;
    16. allowChatItem = new ItemStack(351, 8);
    17. allowChatStatus = "Off";
    18. }
    19.  
    20. ItemMeta allowChatItemMeta = allowChatItem.getItemMeta();
    21.  
    22. allowChatItemMeta.setDisplayName(ChatColor.AQUA+"Chat");
    23.  
    24. String allowChatItemLore[] = { allowChatColour + allowChatStatus };
    25. allowChatItemMeta.setLore(Arrays.asList(allowChatItemLore));
    26.  
    27. settingsMenu.setItem(0, allowChatItem);
    28. }

    The error is on this line:
    FileConfiguration config = getConfig();

    on the getConfig(); thank you very much :D
     
  2. Offline

    The_Doctor_123

    getConfig() is not a static method.
     
  3. Offline

    bloodless2010

    The_Doctor_123 Okay, how can I accomplish what I want to do then? I am quite new to Java still, and unfamiliar how to work around this.
     
  4. Offline

    Mitsugaru

    Where is this code? More importantly, why is it in a static block rather than a method?

    Assuming this is all in the same class, move the contents within your static block into a method.

    Here's an untested minor rewrite to start you off:
    Code:
    public class YourClass extends JavaPlugin{
        private Inventory settingsMenu = Bukkit.createInventory(null, 18, ChatColor.RED+"Server settings manager");
        private ItemStack allowChatItem;
        private ChatColor allowChatColour;
        private String allowChatStatus;
       
        @Override
        public void onEnable() {
            FileConfiguration config = getConfig();
            boolean allowChat = config.getBoolean("ezcontrol.allowChat");
     
            if (allowChat) {
                allowChatColour = ChatColor.GREEN;
                allowChatItem = new ItemStack(351, 10);
                allowChatStatus = "On";
            } else {
                allowChatColour = ChatColor.RED;
                allowChatItem = new ItemStack(351, 8);
                allowChatStatus = "Off";
            }
     
            ItemMeta allowChatItemMeta = allowChatItem.getItemMeta();
     
            allowChatItemMeta.setDisplayName(ChatColor.AQUA+"Chat");
     
            String allowChatItemLore[] = { allowChatColour + allowChatStatus };
            allowChatItemMeta.setLore(Arrays.asList(allowChatItemLore));
     
            settingsMenu.setItem(0, allowChatItem);
        }
    }
    Since your compiler said JavaPlugin, I take it you were trying to run this from your main plugin class.
    Also, you should reconsider changing allowChatStatus to a boolean if its just going to be an on / off variable.

    Good luck with the rest of it.
     
Thread Status:
Not open for further replies.

Share This Page