Cannot define static initializer

Discussion in 'Plugin Development' started by BrushPainter, Feb 1, 2014.

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

    BrushPainter

    Hey guys, I am getting this error on line 30 of my code:
    "cannot define static initializer in inner type Main.myCustomInventory"

    Here is my code:
    Code:java
    1. package me.BrushPainter.ClearRealms;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.EventPriority;
    11. import org.bukkit.event.inventory.InventoryClickEvent;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Main extends JavaPlugin{
    18.  
    19. public void onEnable() {
    20. getLogger().info("ClearRealms Enabled");
    21. }
    22.  
    23. public void onDisable() {
    24. getLogger().info("ClearRealms Disabled");
    25. }
    26.  
    27. public class myCustomInventory {
    28. public static Inventory myInventory = Bukkit.createInventory(null, 9, "Choose a reward!");
    29.  
    30. static {
    31. myInventory.setItem(1, new ItemStack(Material.GOLDEN_APPLE, 1, (short) 1));
    32. myInventory.setItem(2, new ItemStack(Material.GOLD_BLOCK, 1));
    33. myInventory.setItem(3, new ItemStack(Material.GOLD_BLOCK, 1));
    34. myInventory.setItem(4, new ItemStack(Material.GOLD_BLOCK, 1));
    35. myInventory.setItem(5, new ItemStack(Material.GOLD_BLOCK, 1));
    36. myInventory.setItem(6, new ItemStack(Material.GOLD_BLOCK, 1));
    37. myInventory.setItem(7, new ItemStack(Material.GOLD_BLOCK, 1));
    38. myInventory.setItem(8, new ItemStack(Material.GOLD_BLOCK, 1));
    39. myInventory.setItem(9, new ItemStack(Material.GOLD_BLOCK, 1));
    40. }
    41. @EventHandler
    42. public void onInventoryClick(InventoryClickEvent event) {
    43. Player player = (Player) event.getWhoClicked();
    44. ItemStack clicked = event.getCurrentItem();
    45. Inventory inventory = event.getInventory();
    46. if (inventory.getName().equals(myInventory.getName())) {
    47. if (clicked.getType() == Material.DIRT) {
    48. event.setCancelled(true);
    49. player.closeInventory();
    50. player.getInventory().addItem(new ItemStack(Material.DIRT, 1));
    51. }
    52. }
    53. }
    54. }
    55.  
    56. @EventHandler(priority=EventPriority.HIGH)
    57. public void onPlayerUse(PlayerInteractEvent event){
    58. Player p = event.getPlayer();
    59.  
    60. if(p.getItemInHand().getType() == Material.MONSTER_EGG){
    61.  
    62. }
    63. else if(p.getItemInHand().getType() == Material.BLAZE_ROD){
    64.  
    65. }
    66. }
    67.  
    68. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    69. if(cmd.getName().equalsIgnoreCase("giveegg")){
    70. if (sender instanceof Player) {
    71. Player pl = (Player) sender;
    72. ItemStack claimEgg = new ItemStack(Material.MONSTER_EGG, 1);
    73. pl.getInventory().addItem(claimEgg);
    74. pl.sendMessage(ChatColor.YELLOW + "You have received a claim egg, right click to use it.");
    75. } else {
    76. sender.sendMessage(ChatColor.RED + "An error has occured! - BrushPainter");
    77. }
    78. }
    79.  
    80. return false;
    81.  
    82. }
    83.  
    84. }
     
  2. Offline

    xTrollxDudex

    BrushPainter
    Inner classes can't have static intializers. Make a new class.
    Why not add your items in the constructor though?
     
  3. Offline

    BrushPainter

    xTrollxDudex I'm not quite getting you, mind showing me a example of each thing?
     
  4. Offline

    xTrollxDudex

    Jake6177 and _Cookie_ like this.
  5. Offline

    Niknea

    BrushPainter He is saying, use multiple classes to make the static work. If you don't know how to make multiple classes check this out.
     
    BrushPainter likes this.
  6. Offline

    RawCode

    xTrollxDudex
    Wrong.

    Inner classes can have static initializers, but such classes also must be declared static.
     
    BrushPainter likes this.
  7. Offline

    xTrollxDudex

    RawCode
    I'm not on an IDE so I have no idea. I only assumed that it errors because of that, but thanks for pointing that out.
     
  8. Offline

    BrushPainter

    RawCode
    So what exactly do I have to do for it to work? I'm very confused now. By the way thanks for helping. :)

    Current code:
    Code:java
    1. package me.BrushPainter.ClearRealms;
    2.  
    3.  
    4.  
    5. import org.bukkit.Bukkit;
    6.  
    7. import org.bukkit.ChatColor;
    8.  
    9. import org.bukkit.Material;
    10.  
    11. import org.bukkit.command.Command;
    12.  
    13. import org.bukkit.command.CommandSender;
    14.  
    15. import org.bukkit.enchantments.Enchantment;
    16.  
    17. import org.bukkit.entity.Player;
    18.  
    19. import org.bukkit.event.EventHandler;
    20.  
    21. import org.bukkit.event.EventPriority;
    22.  
    23. import org.bukkit.event.inventory.InventoryClickEvent;
    24.  
    25. import org.bukkit.event.player.PlayerInteractEvent;
    26.  
    27. import org.bukkit.inventory.Inventory;
    28.  
    29. import org.bukkit.inventory.ItemStack;
    30.  
    31. import org.bukkit.plugin.java.JavaPlugin;
    32.  
    33.  
    34.  
    35. public class Main extends JavaPlugin{
    36.  
    37.  
    38.  
    39. public void onEnable() {
    40.  
    41. getLogger().info("ClearRealms Enabled");
    42.  
    43. }
    44.  
    45.  
    46.  
    47. public void onDisable() {
    48.  
    49. getLogger().info("ClearRealms Disabled");
    50.  
    51. }
    52.  
    53.  
    54.  
    55. public class myCustomInventory {
    56.  
    57. public static Inventory myInventory = Bukkit.createInventory(null, 9, "Choose a reward!");
    58.  
    59.  
    60.  
    61. static {
    62.  
    63. myInventory.setItem(1, new ItemStack(Material.GOLDEN_APPLE, 1, (short) 1));
    64.  
    65. myInventory.setItem(2, new ItemStack(Material.DIAMOND_SWORD, 1).addEnchantment(Enchantment.DAMAGE_ALL, 1));
    66.  
    67. myInventory.setItem(3, new ItemStack(Material.GOLD_BLOCK, 1));
    68.  
    69. myInventory.setItem(4, new ItemStack(Material.GOLD_BLOCK, 1));
    70.  
    71. myInventory.setItem(5, new ItemStack(Material.GOLD_BLOCK, 1));
    72.  
    73. myInventory.setItem(6, new ItemStack(Material.GOLD_BLOCK, 1));
    74.  
    75. myInventory.setItem(7, new ItemStack(Material.GOLD_BLOCK, 1));
    76.  
    77. myInventory.setItem(8, new ItemStack(Material.GOLD_BLOCK, 1));
    78.  
    79. myInventory.setItem(9, new ItemStack(Material.GOLD_BLOCK, 1));
    80.  
    81. }
    82.  
    83. @EventHandler
    84.  
    85. public void onInventoryClick(InventoryClickEvent event) {
    86.  
    87. Player player = (Player) event.getWhoClicked();
    88.  
    89. ItemStack clicked = event.getCurrentItem();
    90.  
    91. Inventory inventory = event.getInventory();
    92.  
    93. if (inventory.getName().equals(myInventory.getName())) {
    94.  
    95. if (clicked.getType() == Material.DIRT) {
    96.  
    97. event.setCancelled(true);
    98.  
    99. player.closeInventory();
    100.  
    101. player.getInventory().addItem(new ItemStack(Material.DIRT, 1));
    102.  
    103. }
    104.  
    105. }
    106.  
    107. }
    108.  
    109. }
    110.  
    111.  
    112.  
    113. @EventHandler(priority=EventPriority.HIGH)
    114.  
    115. public void onPlayerUse(PlayerInteractEvent event){
    116.  
    117. Player p = event.getPlayer();
    118.  
    119.  
    120.  
    121. if(p.getItemInHand().getType() == Material.MONSTER_EGG){
    122.  
    123.  
    124.  
    125. }
    126.  
    127. else if(p.getItemInHand().getType() == Material.MONSTER_EGG){
    128.  
    129.  
    130.  
    131. }
    132.  
    133. }
    134.  
    135.  
    136.  
    137. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    138.  
    139. if(cmd.getName().equalsIgnoreCase("giveegg")){
    140.  
    141. if (sender instanceof Player) {
    142.  
    143. Player pl = (Player) sender;
    144.  
    145. ItemStack claimEgg = new ItemStack(Material.MONSTER_EGG, 1);
    146.  
    147. pl.getInventory().addItem(claimEgg);
    148.  
    149. pl.sendMessage(ChatColor.YELLOW + "You have received a claim egg, right click to use it.");
    150.  
    151. } else {
    152.  
    153. sender.sendMessage(ChatColor.RED + "An error has occured! - BrushPainter");
    154.  
    155. }
    156.  
    157. }
    158.  
    159.  
    160.  
    161. return false;
    162.  
    163.  
    164.  
    165. }
    166.  
    167.  
    168.  
    169. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  9. Offline

    Sagacious_Zed Bukkit Docs

    static is a modifier, it has slightly different meaning when it comes to variables, methods, and classes. Although it only makes sense to have a static class if that class is nested. If you don't know where to put the modifier, you haven't been paying attention to Java syntax.
     
  10. Offline

    RawCode

    BrushPainter xTrollxDudex

    package rc.gpt;

    public class Debug
    {


    static public class test
    {

    static
    {
    }

    }

    }

    dont try to code plugins as long as you dont know java basics.
     
  11. Offline

    xTrollxDudex

    RawCode
    Wait why did you tahg me, I know how to do this....
     
  12. Offline

    RawCode

    are you sure?
     
  13. Offline

    BrushPainter

    RawCode

    Code:
    package me.BrushPainter.ClearRealms;
     
     
     
    import org.bukkit.Bukkit;
     
    import org.bukkit.ChatColor;
     
    import org.bukkit.Material;
     
    import org.bukkit.command.Command;
     
    import org.bukkit.command.CommandSender;
     
    import org.bukkit.enchantments.Enchantment;
     
    import org.bukkit.entity.Player;
     
    import org.bukkit.event.EventHandler;
     
    import org.bukkit.event.EventPriority;
     
    import org.bukkit.event.inventory.InventoryClickEvent;
     
    import org.bukkit.event.player.PlayerInteractEvent;
     
    import org.bukkit.inventory.Inventory;
     
    import org.bukkit.inventory.ItemStack;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
     
     
        public class Main extends JavaPlugin{
     
     
     
        public void onEnable() {
     
                getLogger().info("ClearRealms Enabled");
     
        }
     
     
     
        public void onDisable() {
     
                getLogger().info("ClearRealms Disabled");
     
        }
     
     
     
        public class myCustomInventory {
     
            public final Inventory myInventory = Bukkit.createInventory(null, 9, "Choose a reward!");
     
         
     
            public void Head() { {
     
            { 
                    myInventory.setItem(1, new ItemStack(Material.GOLDEN_APPLE, 1, (short) 1));
     
                    myInventory.setItem(2, new ItemStack(Material.DIAMOND_SWORD, 1).addEnchantment(Enchantment.DAMAGE_ALL, 1));
     
                    myInventory.setItem(3, new ItemStack(Material.GOLD_BLOCK, 1));
     
                    myInventory.setItem(4, new ItemStack(Material.GOLD_BLOCK, 1));
     
                    myInventory.setItem(5, new ItemStack(Material.GOLD_BLOCK, 1));
     
                    myInventory.setItem(6, new ItemStack(Material.GOLD_BLOCK, 1));
     
                    myInventory.setItem(7, new ItemStack(Material.GOLD_BLOCK, 1));
     
                    myInventory.setItem(8, new ItemStack(Material.GOLD_BLOCK, 1));
     
                    myInventory.setItem(9, new ItemStack(Material.GOLD_BLOCK, 1));
     
            }
            }
            }
     
            @EventHandler
     
            public void onInventoryClick(InventoryClickEvent event) {
     
            Player player = (Player) event.getWhoClicked();
     
            ItemStack clicked = event.getCurrentItem();
     
            Inventory inventory = event.getInventory();
     
            if (inventory.getName().equals(myInventory.getName())) {
     
            if (clicked.getType() == Material.DIRT) {
     
            event.setCancelled(true);
     
            player.closeInventory();
     
            player.getInventory().addItem(new ItemStack(Material.DIRT, 1));
     
            }
     
            }
     
            }
     
    }
     
     
     
        @EventHandler(priority=EventPriority.HIGH)
     
        public void onPlayerUse(PlayerInteractEvent event){
     
            Player p = event.getPlayer();
     
     
     
            if(p.getItemInHand().getType() == Material.MONSTER_EGG){
     
             
     
            }
     
            else if(p.getItemInHand().getType() == Material.MONSTER_EGG){
     
             
     
            }
     
        }
     
     
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
     
                if(cmd.getName().equalsIgnoreCase("giveegg")){
     
                        if (sender instanceof Player) {
     
                            Player pl = (Player) sender;
     
                            ItemStack claimEgg = new ItemStack(Material.MONSTER_EGG, 1);
     
                            pl.getInventory().addItem(claimEgg);
     
        pl.sendMessage(ChatColor.YELLOW + "You have received a claim egg, right click to use it.");
     
                } else {
     
        sender.sendMessage(ChatColor.RED + "An error has occured! - BrushPainter");
     
                    }
     
                }
     
             
     
                return false;
     
     
     
            }
     
     
     
    }
    I messed around with my previous code and came up with no errors, not sure if it works though. Anyway as I moved on I came up with another error.... I'm trying to add a enchanted item using setItem in a void.

    Line 66 is the error and it is:
    "The method setItem(int, ItemStack) in the type Inventory is not applicable for the arguments (int, void)"
     
  14. Offline

    rangersmash

    For the enchantment, create the itemstack outside the .setItem();

    for example,
    ItemStack ds = new ItemStack(Material.DIAMOND_SWORD, 1);
    ds.addEnchantment(Enchantment.DAMAGE_ALL, 1)
    myInventory.setItem(2,ds)

    BrushPainter ;)
     
    BrushPainter likes this.
  15. Offline

    BrushPainter

  16. Offline

    xTrollxDudex

    Yes. I didn't realize there was a way of DOING that, but once I knew the way, I know HOW to do it.
     
    swampshark19 likes this.
  17. Offline

    BrushPainter

    xTrollxDudex
    But if you didn't know there was a way, how did you know how to do it? Lol.
     
  18. Offline

    _Filip

    Jake6177 likes this.
  19. Offline

    Iaccidentally

    Better solution: don't use static, solves about 100 different problems.
     
  20. Offline

    BrushPainter

    Iaccidentally Lol yeah that's what I did to get my code to work. XD
     
  21. Offline

    Jake6177


    Static is useful in some situations.
     
  22. Offline

    Iaccidentally

    That was never in question, however, for this use case, it is not.
     
    Jake6177 likes this.
  23. Offline

    RawCode

    you must understand simple feature:

    each object have prototype - class instance
    anything declared static stored inside prototype
    anything not declared static stored inside instance

    as long as you dont understand it, you will have problems.
     
Thread Status:
Not open for further replies.

Share This Page