Creating an inventory which can read Config.

Discussion in 'Plugin Development' started by Tamir, Jul 30, 2014.

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

    Tamir

    Hey guys.
    SO, I know how to create an inventory with the static method, But it wont work.
    I need to create an inventory which will be able to read the config, Like if i do
    Code:java
    1. Inventory.setItem(1, getConfig().getString("Example");
    2.  

    Nothing will bug it.
    I really need it asap :p
    Thanks in advance,
    Tamir.
     
  2. Offline

    Archelaus

    I am unsure on the purpose of the code, but I can immediately tell you that you're missing a closing bracket. You open setItem with a bracket, but never close it. You code should change to the below to be correct syntax wise.

    Code:
    Inventory.setItem(1, getConfig().getString("Example"));
     
  3. Offline

    FabeGabeMC

    Tamir Archelaus
    Why can't you just create an ItemStack with an integer?
    Although it is deprecated, it still works.
    Code:java
    1. @SuppressWarnings("deprecation")
    2. ItemStack yourItem = new ItemStack(
    3. getConfig().getInt("item.id"),
    4. getConfig().getInt("item.amount"),
    5. getConfig().getInt("item.damage"),
    6. getConfig().getInt("item.data"));
    7. inventory.setItem(slot, yourItem);

    Source: http://jd.bukkit.org/beta/apidocs/org/bukkit/inventory/ItemStack.html
     
  4. Offline

    Tamir

    Archelaus
    That was just an example.
    FabeGabeMC
    You got me worng. I mean the STATIC method itself wont let me read the config :l
     
  5. Offline

    FabeGabeMC

    Tamir
    in the parameters for the static method, add your main class:
    Code:java
    1. public static void yourMethod(YourPlugin plugin, other parameters....) {
    2. }

    and in your other classes make sure you instantiate the main class:
    Code:java
    1. YourPlugin plugin;

    It should look something like:
    Code:JAVA
    1. //example code
    2. @EventHandler
    3. public void onJoin(PlayerJoinEvent e) {
    4. YourPlugin.yourMethod(plugin, asd, asjf, dfga);
    5. }
    6. //Of course asd, asjf, and dfga mean nothing.
     
  6. Offline

    mythbusterma

    FabeGabeMC

    Or you use a non-static method for something that shouldn't be static.
     
  7. Offline

    Tamir

    FabeGabeMC
    Already tried, It wont let the inventory open, It wont run the command.
    mythbusterma
    How do i create an inventory with a non static method? :p
     
  8. Offline

    mythbusterma

    Tamir

    What is causing this inventory to be opened? Just call a method in your main class that will build an array of ItemStacks and call it wherever you need it.
     
  9. Offline

    Tamir

    mythbusterma
    I made the inventory on my main class,
    I used
    Code:java
    1. player.openInventory(Inventory);

    When he uses a command
    I made it like
    player.sendMessage("Debug");
    It sents the msg Debug
    But then It just wont open the inventory like red chat with an error
     
  10. Offline

    mythbusterma

    Tamir

    Do you mean it generated an exception? You should post the stack trace in that case.
     
  11. Offline

    MoeMix

    Just make sure that the classes are instantiated. In your main class put YourPlugin plugin; and in the constructor make sure you set plugin = this;

    In your second class do the same thing but in the constructor set plugin to whatever variable you passed into the constructor.

    Edit: Not sure if that solved your problem but I was readin up on this in the previous messages and wasn't sure if you solved the issue regarding using static methods or not.
     
  12. Offline

    Tamir

    mythbusterma
    That's my main class, I didnt add the command yet:
    Code:java
    1. package me.Tamir.Sgamble;
    2.  
    3. import java.util.Arrays;
    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. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Main extends JavaPlugin implements Listener{
    15.  
    16. private static final Main instance = new Main();
    17. public static final Main getPlugin() {
    18. return instance;
    19. }
    20.  
    21. public static Inventory GambleMenu = Bukkit.createInventory(null, 18, "Gambling Menu");
    22. static {
    23. //ItemStacks.
    24. //FirstPot
    25. ItemStack Pot1 = new ItemStack(Material.POTION, 1);
    26. ItemMeta Potion1 = Pot1.getItemMeta();
    27. Potion1.setDisplayName(instance.getConfig().getString("FirstPotionName"));
    28. Potion1.setLore(Arrays.asList(instance.getConfig().getString("FirstPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    29. Pot1.setItemMeta(Potion1);
    30.  
    31. //SecondPot
    32. ItemStack Pot2 = new ItemStack(Material.POTION, 1);
    33. ItemMeta Potion2 = Pot2.getItemMeta();
    34. Potion2.setDisplayName(instance.getConfig().getString("SecondPotionName"));
    35. Potion2.setLore(Arrays.asList(instance.getConfig().getString("SecondPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    36. Pot2.setItemMeta(Potion2);
    37.  
    38. //ThirdPot
    39. ItemStack Pot3 = new ItemStack(Material.POTION, 1);
    40. ItemMeta Potion3 = Pot3.getItemMeta();
    41. Potion3.setDisplayName(instance.getConfig().getString("ThirdPotionName"));
    42. Potion3.setLore(Arrays.asList(instance.getConfig().getString("ThirdPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    43. Pot3.setItemMeta(Potion3);
    44.  
    45. //4'thPot
    46. ItemStack Pot4 = new ItemStack(Material.POTION, 1);
    47. ItemMeta Potion4 = Pot4.getItemMeta();
    48. Potion4.setDisplayName(instance.getConfig().getString("FourthPotionName"));
    49. Potion4.setLore(Arrays.asList(instance.getConfig().getString("FourthPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    50. Pot4.setItemMeta(Potion4);
    51.  
    52. //5'thPot
    53. ItemStack Pot5 = new ItemStack(Material.POTION, 1);
    54. ItemMeta Potion5 = Pot5.getItemMeta();
    55. Potion5.setDisplayName(instance.getConfig().getString("FifthPotionName"));
    56. Potion5.setLore(Arrays.asList(instance.getConfig().getString("FifthPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    57. Pot5.setItemMeta(Potion5);
    58. //6'thPot
    59. ItemStack Pot6 = new ItemStack(Material.POTION, 1);
    60. ItemMeta Potion6 = Pot6.getItemMeta();
    61. Potion6.setDisplayName(instance.getConfig().getString("SixPotionName"));
    62. Potion6.setLore(Arrays.asList(instance.getConfig().getString("SixPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    63. Pot6.setItemMeta(Potion6);
    64. //7'thPot
    65. ItemStack Pot7 = new ItemStack(Material.POTION, 1);
    66. ItemMeta Potion7 = Pot7.getItemMeta();
    67. Potion7.setDisplayName(instance.getConfig().getString("SevenPotionName"));
    68. Potion7.setLore(Arrays.asList(instance.getConfig().getString("SevenPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    69. Pot7.setItemMeta(Potion7);
    70. //8'thPot
    71. ItemStack Pot8 = new ItemStack(Material.POTION, 1);
    72. ItemMeta Potion8 = Pot8.getItemMeta();
    73. Potion8.setDisplayName(instance.getConfig().getString("EightPotionName"));
    74. Potion8.setLore(Arrays.asList(instance.getConfig().getString("EightPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    75. Pot8.setItemMeta(Potion8);
    76. //9Th Pot
    77. ItemStack Pot9 = new ItemStack(Material.POTION, 1);
    78. ItemMeta Potion9 = Pot9.getItemMeta();
    79. Potion9.setDisplayName(instance.getConfig().getString("NinePotionName"));
    80. Potion9.setLore(Arrays.asList(instance.getConfig().getString("NinePotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    81. Pot9.setItemMeta(Potion9);
    82. //PICK1
    83. ItemStack Pickaxe15 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    84. ItemMeta Pick15 = Pickaxe15.getItemMeta();
    85. Pick15.setDisplayName(instance.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "15" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    86. Pick15.setLore(Arrays.asList(instance.getConfig().getString("PickaxePrice15"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    87. Pickaxe15.setItemMeta(Pick15);
    88.  
    89. //PICK2
    90. ItemStack Pickaxe20 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    91. ItemMeta Pick20 = Pickaxe20.getItemMeta();
    92. Pick20.setDisplayName(instance.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "20" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    93. Pick20.setLore(Arrays.asList(instance.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    94. Pickaxe20.setItemMeta(Pick20);
    95.  
    96. //PICK3
    97. ItemStack Pickaxe25 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    98. ItemMeta Pick25 = Pickaxe25.getItemMeta();
    99. Pick25.setDisplayName(instance.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "25" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    100. Pick25.setLore(Arrays.asList(instance.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    101. Pickaxe25.setItemMeta(Pick25);
    102.  
    103. //PICK4
    104. ItemStack Pickaxe30 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    105. ItemMeta Pick30 = Pickaxe30.getItemMeta();
    106. Pick30.setDisplayName(instance.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "30" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    107. Pick30.setLore(Arrays.asList(instance.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    108. Pickaxe30.setItemMeta(Pick30);
    109.  
    110. //PICK5
    111. ItemStack Pickaxe35 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    112. ItemMeta Pick35 = Pickaxe35.getItemMeta();
    113. Pick35.setDisplayName(instance.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "35" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    114. Pick35.setLore(Arrays.asList(instance.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    115. Pickaxe35.setItemMeta(Pick35);
    116.  
    117. //PICK6
    118. ItemStack Pickaxe40 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    119. ItemMeta Pick40 = Pickaxe40.getItemMeta();
    120. Pick40.setDisplayName(instance.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "40" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    121. Pick40.setLore(Arrays.asList(instance.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    122. Pickaxe40.setItemMeta(Pick40);
    123. //PICK7
    124. ItemStack Pickaxe45 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    125. ItemMeta Pick45 = Pickaxe45.getItemMeta();
    126. Pick45.setDisplayName(instance.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "45" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    127. Pick45.setLore(Arrays.asList(instance.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    128. Pickaxe45.setItemMeta(Pick45);
    129. //PICK8
    130. ItemStack Pickaxe50 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    131. ItemMeta Pick50 = Pickaxe50.getItemMeta();
    132. Pick50.setDisplayName(instance.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "50" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    133. Pick50.setLore(Arrays.asList(instance.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    134. Pickaxe50.setItemMeta(Pick50);
    135. //PICK9
    136. ItemStack Pickaxe60 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    137. ItemMeta Pick60 = Pickaxe60.getItemMeta();
    138. Pick60.setDisplayName(instance.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "50" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    139.  
    140. Pick60.setLore(Arrays.asList(instance.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    141. Pickaxe60.setItemMeta(Pick60);
    142. //EndOfItemstacks
    143. GambleMenu.setItem(0, Pot1);
    144. GambleMenu.setItem(1, Pot2);
    145. GambleMenu.setItem(2, Pot3);
    146. GambleMenu.setItem(3, Pot4);
    147. GambleMenu.setItem(4, Pot5);
    148. GambleMenu.setItem(5, Pot6);
    149. GambleMenu.setItem(6, Pot7);
    150. GambleMenu.setItem(7, Pot8);
    151. GambleMenu.setItem(8, Pot9);
    152. GambleMenu.setItem(9, Pickaxe15);
    153. GambleMenu.setItem(10, Pickaxe20);
    154. GambleMenu.setItem(11, Pickaxe25);
    155. GambleMenu.setItem(12, Pickaxe30);
    156. GambleMenu.setItem(13, Pickaxe35);
    157. GambleMenu.setItem(14, Pickaxe40);
    158. GambleMenu.setItem(15, Pickaxe45);
    159. GambleMenu.setItem(16, Pickaxe50);
    160. GambleMenu.setItem(17, Pickaxe60);
    161.  
    162.  
    163. }
    164.  
    165. }
     
  13. Offline

    mythbusterma

    Tamir

    Doesn't this generate a stack trace when you start the server? It shouldn't let you instantiate the plugin.
     
  14. Offline

    Tamir

  15. Offline

    mythbusterma

    Tamir

    Well don't do "new Main()" for starters.

    Furthermore, I believe doing Bukkit.createInventory(null, ...) could cause an NPE. Post your stack traces.
     
  16. Offline

    Tamir

    mythbusterma
    The stacktrace comes ONLY when I actully add the line that opens the inventory.
    Thoes are my stacktraces:
    [​IMG]
     
  17. Offline

    JaguarJo

    Locked. Please seek support from the makers of your server build. Craftbukkit is not affiliated with Spigot, so when that is the software your server runs on, they are the ones who are better equipped to help you with compatibility for their software.
     
Thread Status:
Not open for further replies.

Share This Page