Help with illegal plugin access exception?

Discussion in 'Plugin Help/Development/Requests' started by Caprei, Oct 5, 2014.

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

    Caprei

    Hey all.
    I tried to create a plugin which:
    - When the player joins, it checks if they have a chest with a certain display name
    - If they don't, gives them one.
    - When this chest is clicked, it opens an icon menu -- Found here: https://forums.bukkit.org/threads/icon-menu.108342/

    - Basically converts materials into higher tier materials, and higher tier materials into lower.

    I have 2 classes. Here they are:

    Code:java
    1. package me.Caprei.ChestPvP;
    2.  
    3. import me.Caprei.ChestPvP.IconMenu.OptionClickEvent;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.inventory.PlayerInventory;
    13. import org.bukkit.plugin.Plugin;
    14. import org.bukkit.plugin.PluginManager;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class ChestPvP extends JavaPlugin{
    18.  
    19. public void onEnable(){
    20.  
    21. PluginManager pm = Bukkit.getPluginManager();
    22. pm.registerEvents(im, this);
    23. Bukkit.getServer().getLogger().info("ChestPVP is now enabled. All seems to be working.");
    24.  
    25.  
    26.  
    27. }
    28.  
    29. public void onDisable(){
    30.  
    31. }
    32.  
    33. public static IconMenu im;
    34. IconMenu menu = new IconMenu("Shop", 27, new IconMenu.OptionClickEventHandler(){
    35.  
    36. @Override
    37. public void onOptionClick(OptionClickEvent event) {
    38. String name = event.getName();
    39. Player p = event.getPlayer();
    40. event.setWillClose(false);
    41. event.setWillDestroy(true);
    42. PlayerInventory pi = p.getInventory();
    43. if(name.equalsIgnoreCase(ChatColor.GREEN + ChatColor.BOLD.toString() + "+ NETHER STAR")){
    44. if(pi.contains(new ItemStack(Material.DIAMOND, 64))){
    45. pi.addItem(new ItemStack(Material.NETHER_STAR, 1));
    46. pi.removeItem(new ItemStack(Material.DIAMOND, 64));
    47. return;
    48. }else{
    49. p.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have enough diamonds to do that.");
    50. }
    51.  
    52. }else if(name.equalsIgnoreCase(ChatColor.GREEN + ChatColor.BOLD.toString() + "+ DIAMOND")){
    53. if(pi.contains(new ItemStack(Material.GOLD_INGOT, 64))){
    54. pi.addItem(new ItemStack(Material.DIAMOND, 1));
    55. pi.removeItem(new ItemStack(Material.GOLD_INGOT, 64));
    56. return;
    57. }else{
    58. p.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have enough gold ingots to do that.");
    59. }
    60. }else if(name.equalsIgnoreCase(ChatColor.GREEN + ChatColor.BOLD.toString() + "+ GOLD INGOT")){
    61. if(pi.contains(new ItemStack(Material.IRON_INGOT, 64))){
    62. pi.addItem(new ItemStack(Material.GOLD_INGOT, 1));
    63. pi.removeItem(new ItemStack(Material.IRON_INGOT, 64));
    64. return;
    65. }else{
    66. p.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have enough iron ingots to do that.");
    67. }
    68. }else if(name.equalsIgnoreCase(ChatColor.GREEN + ChatColor.BOLD.toString() + "+ IRON INGOT")){
    69. if(pi.contains(new ItemStack(Material.COAL, 64))){
    70. pi.addItem(new ItemStack(Material.IRON_INGOT, 1));
    71. pi.removeItem(new ItemStack(Material.COAL, 64));
    72. return;
    73. }else{
    74. p.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have enough coal to do that.");
    75. }
    76. }else if(name.equalsIgnoreCase(ChatColor.RED + ChatColor.BOLD.toString() + "- NETHER STAR")){
    77. if(pi.containsAtLeast(new ItemStack(Material.NETHER_STAR), 1)){
    78. pi.addItem(new ItemStack(Material.DIAMOND, 64));
    79. pi.removeItem(new ItemStack(Material.NETHER_STAR, 1));
    80. return;
    81. }else{
    82. p.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have enough nether stars to do that.");
    83. }
    84. }
    85.  
    86. else if(name.equalsIgnoreCase(ChatColor.RED + ChatColor.BOLD.toString() + "- DIAMOND")){
    87. if(pi.containsAtLeast(new ItemStack(Material.DIAMOND), 1)){
    88. pi.addItem(new ItemStack(Material.GOLD_INGOT, 64));
    89. pi.removeItem(new ItemStack(Material.DIAMOND, 1));
    90. return;
    91. }else{
    92. p.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have enough diamonds to do that.");
    93. }
    94. }
    95. else if(name.equalsIgnoreCase(ChatColor.RED + ChatColor.BOLD.toString() + "- GOLD INGOT")){
    96. if(pi.containsAtLeast(new ItemStack(Material.GOLD_INGOT), 1)){
    97. pi.addItem(new ItemStack(Material.IRON_INGOT, 64));
    98. pi.removeItem(new ItemStack(Material.GOLD_INGOT, 1));
    99. return;
    100. }else{
    101. p.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have enough gold ingots to do that.");
    102. }
    103. }
    104. else if(name.equalsIgnoreCase(ChatColor.RED + ChatColor.BOLD.toString() + "- IRON INGOT")){
    105. if(pi.containsAtLeast(new ItemStack(Material.IRON_INGOT), 1)){
    106. pi.addItem(new ItemStack(Material.COAL, 64));
    107. pi.removeItem(new ItemStack(Material.IRON_INGOT, 1));
    108. return;
    109. }else{
    110. p.sendMessage(ChatColor.RED + ChatColor.ITALIC.toString() + "You don't have enough iron ingots to do that.");
    111. }
    112. }
    113.  
    114. }
    115.  
    116.  
    117. }, this).setOption(2, new ItemStack(Material.IRON_INGOT, 1), ChatColor.GREEN + ChatColor.BOLD.toString() + "+ IRON INGOT", "You need 64 coal to purchase this.")
    118. .setOption(3, new ItemStack(Material.GOLD_INGOT, 1),ChatColor.GREEN + ChatColor.BOLD.toString() + "+ GOLD INGOT", "You need 64 iron ingots to purchase this.")
    119. .setOption(4, new ItemStack(Material.DIAMOND, 1),ChatColor.GREEN + ChatColor.BOLD.toString() + "+ DIAMOND", "You need 64 gold ingots to purchase this.")
    120. .setOption(5, new ItemStack(Material.NETHER_STAR, 1),ChatColor.GREEN + ChatColor.BOLD.toString() + "+ NETHER STAR", "You need 64 diamonds to purchase this.")
    121. .setOption(20, new ItemStack(Material.IRON_INGOT, 1),ChatColor.RED + ChatColor.BOLD.toString() + "- IRON INGOT", "You need 1 iron ingot to downgrade.")
    122. .setOption(21, new ItemStack(Material.GOLD_INGOT, 1),ChatColor.RED + ChatColor.BOLD.toString() + "- GOLD INGOT", "You need 1 gold ingot to downgrade.")
    123. .setOption(22, new ItemStack(Material.DIAMOND, 1),ChatColor.RED + ChatColor.BOLD.toString() + "- DIAMOND", "You need 1 diamond to downgrade.")
    124. .setOption(23, new ItemStack(Material.NETHER_STAR, 1),ChatColor.RED + ChatColor.BOLD.toString() + "- NETHER STAR", "You need 1 nether star to downgrade.");
    125.  
    126.  
    127.  
    128.  
    129. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    130. if(cmd.getName().equalsIgnoreCase("shop")){
    131. if(!(sender instanceof Player)){
    132. return true;
    133. }
    134.  
    135. menu.open((Player) sender);
    136. }
    137. return true;
    138. }
    139.  
    140. public IconMenu getIconMenu(){
    141. return menu;
    142. }
    143.  
    144.  
    145.  
    146. }
    147.  


    Class 2:
    Code:java
    1. package me.Caprei.ChestPvP;
    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.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.EventPriority;
    11. import org.bukkit.event.HandlerList;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.Action;
    14. import org.bukkit.event.inventory.InventoryClickEvent;
    15. import org.bukkit.event.player.PlayerInteractEvent;
    16. import org.bukkit.event.player.PlayerJoinEvent;
    17. import org.bukkit.inventory.Inventory;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.inventory.PlayerInventory;
    20. import org.bukkit.inventory.meta.ItemMeta;
    21. import org.bukkit.plugin.Plugin;
    22.  
    23. public class IconMenu implements Listener {
    24.  
    25. private String name;
    26. private int size;
    27. private OptionClickEventHandler handler;
    28. private Plugin plugin;
    29.  
    30. private String[] optionNames;
    31. private ItemStack[] optionIcons;
    32.  
    33. boolean chest = false;
    34. Player player;
    35.  
    36. ChestPvP pvp;
    37.  
    38. public IconMenu(String name, int size, OptionClickEventHandler handler, Plugin plugin) {
    39. this.name = name;
    40. this.size = size;
    41. this.handler = handler;
    42. this.plugin = plugin;
    43. this.optionNames = new String[size];
    44. this.optionIcons = new ItemStack[size];
    45. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    46. }
    47.  
    48. public IconMenu setOption(int position, ItemStack icon, String name, String... info) {
    49. optionNames[position] = name;
    50. optionIcons[position] = setItemNameAndLore(icon, name, info);
    51. return this;
    52. }
    53.  
    54. public void open(Player player) {
    55. Inventory inventory = Bukkit.createInventory(player, size, name);
    56. for (int i = 0; i < optionIcons.length; i++) {
    57. if (optionIcons[i] != null) {
    58. inventory.setItem(i, optionIcons[i]);
    59. }
    60. }
    61. player.openInventory(inventory);
    62. }
    63.  
    64. public void destroy() {
    65. HandlerList.unregisterAll(this);
    66. handler = null;
    67. plugin = null;
    68. optionNames = null;
    69. optionIcons = null;
    70. }
    71.  
    72. @EventHandler(priority=EventPriority.MONITOR)
    73. void onInventoryClick(InventoryClickEvent event) {
    74. if (event.getInventory().getTitle().equals(name)) {
    75. event.setCancelled(true);
    76. int slot = event.getRawSlot();
    77. if (slot >= 0 && slot < size && optionNames[slot] != null) {
    78. Plugin plugin = this.plugin;
    79. OptionClickEvent e = new OptionClickEvent((Player)event.getWhoClicked(), slot, optionNames[slot]);
    80. handler.onOptionClick(e);
    81. if (e.willClose()) {
    82. final Player p = (Player)event.getWhoClicked();
    83. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    84. public void run() {
    85. p.closeInventory();
    86. }
    87. }, 1);
    88. }
    89. if (e.willDestroy()) {
    90. destroy();
    91. }
    92. }
    93. }
    94. }
    95.  
    96.  
    97. public void onPlayerJoin(PlayerJoinEvent event){
    98. Player p = event.getPlayer();
    99. PlayerInventory pi = p.getInventory();
    100. if(pi.containsAtLeast(new ItemStack(Material.CHEST), 1)){
    101. for(int i = 36; --i >= 0;){
    102. ItemStack is = pi.getItem(i);
    103. if(is == null){
    104. i--;
    105. return;
    106. }else if(is == new ItemStack(Material.CHEST, 1)){
    107. if(is.hasItemMeta()){
    108. String name = is.getItemMeta().getDisplayName().toString();
    109. if(name.equalsIgnoreCase(ChatColor.GOLD + ChatColor.ITALIC.toString() + "SHOP"));
    110. break;
    111. }
    112. }
    113. ItemStack iss = new ItemStack(Material.CHEST, 1);
    114. ItemMeta im = iss.getItemMeta();
    115. im.setDisplayName(ChatColor.GOLD + ChatColor.ITALIC.toString() + "SHOP");
    116. iss.setItemMeta(im);
    117. ItemStack its = pi.getItem(8);
    118. int num = pi.firstEmpty();
    119. pi.setItem(num, its);
    120. pi.setItem(8, iss);
    121. return;
    122. }
    123. }
    124. }
    125.  
    126. public void onAction(PlayerInteractEvent event){
    127. if(event.getPlayer().getItemInHand() == new ItemStack(Material.CHEST, 1)){
    128. if(event.getPlayer().getItemInHand().hasItemMeta()){
    129. if(event.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GOLD + ChatColor.ITALIC.toString() + "SHOP")){
    130. if(event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    131. IconMenu m = pvp.getIconMenu();
    132. m.open(event.getPlayer());
    133. }
    134. }
    135. }
    136. }
    137. }
    138.  
    139. public boolean getChest(){
    140. return chest;
    141. }
    142.  
    143. public Player getOpener(){
    144. return player;
    145. }
    146.  
    147.  
    148.  
    149.  
    150.  
    151.  
    152.  
    153.  
    154.  
    155. public interface OptionClickEventHandler {
    156. public void onOptionClick(OptionClickEvent event);
    157. }
    158.  
    159. public class OptionClickEvent {
    160. private Player player;
    161. private int position;
    162. private String name;
    163. private boolean close;
    164. private boolean destroy;
    165.  
    166. public OptionClickEvent(Player player, int position, String name) {
    167. this.player = player;
    168. this.position = position;
    169. this.name = name;
    170. this.close = true;
    171. this.destroy = false;
    172. }
    173.  
    174. public Player getPlayer() {
    175. return player;
    176. }
    177.  
    178. public int getPosition() {
    179. return position;
    180. }
    181.  
    182. public String getName() {
    183. return name;
    184. }
    185.  
    186. public boolean willClose() {
    187. return close;
    188. }
    189.  
    190. public boolean willDestroy() {
    191. return destroy;
    192. }
    193.  
    194. public void setWillClose(boolean close) {
    195. this.close = close;
    196. }
    197.  
    198. public void setWillDestroy(boolean destroy) {
    199. this.destroy = destroy;
    200. }
    201. }
    202.  
    203. private ItemStack setItemNameAndLore(ItemStack item, String name, String[] lore) {
    204. ItemMeta im = item.getItemMeta();
    205. im.setDisplayName(name);
    206. im.setLore(Arrays.asList(lore));
    207. item.setItemMeta(im);
    208. return item;
    209. }
    210.  
    211. }
    212. [/i][/i]


    Here is the error:


    Code:
    [20:35:14 ERROR]: Could not load 'plugins\ChestPvP.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:133) ~[craftbukkit.jar:git-Spigot-1649]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:341) ~[craftbukkit.jar:git-Spigot-1649]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:263) [craftbukkit.jar:git-Spigot-1649]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugins(CraftServer.ja
    va:369) [craftbukkit.jar:git-Spigot-1649]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.reload(CraftServer.java:86
    4) [craftbukkit.jar:git-Spigot-1649]
            at org.bukkit.Bukkit.reload(Bukkit.java:301) [craftbukkit.jar:git-Spigot
    -1649]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23) [craftbukkit.jar:git-Spigot-1649]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    1) [craftbukkit.jar:git-Spigot-1649]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServe
    r.java:767) [craftbukkit.jar:git-Spigot-1649]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchServerCommand(Craf
    tServer.java:753) [craftbukkit.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.DedicatedServer.aB(DedicatedServer.java:
    326) [craftbukkit.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    90) [craftbukkit.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [craftbukkit.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [craftbukkit.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Spigot-1649]
    Caused by: java.lang.NullPointerException
            at me.Caprei.ChestPvP.IconMenu.<init>(IconMenu.java:45) ~[?:?]
            at me.Caprei.ChestPvP.ChestPvP.<init>(ChestPvP.java:119) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    ~[?:1.7.0_40]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    ~[?:1.7.0_40]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce) ~[?:1.7.0_40]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.7.0_4
    0]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.7.0_40]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:76) ~[craftbukkit.jar:git-Spigot-1649]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:129) ~[craftbukkit.jar:git-Spigot-1649]
            ... 14 more
    Please inform me how I can fix this.

    Thank you very very much!
    Caprei
     
  2. Offline

    Zettelkasten

    I think the problem is in IconMenu line 45, you want to register the events only after the plugin is enabled. So move this line to onEnable():
    Code:java
    1. plugin.getServer().getPluginManager().registerEvents(this, plugin);
     
  3. Offline

    Caprei

    Zettelkasten
    Thank you very much. That did fix it, the /shop command now opens the IconMenu. But when I log in, I still don't receive a chest. Any suggestions?

    Thanks for your help,
    Caprei!
     
  4. Offline

    Zettelkasten

    Caprei You are missing an @EventHandler.
     
  5. Offline

    Caprei

    Zettelkasten
    Actually, I had found this error earlier, and I had fixed it, but I still do not receive a chest. Do you have any ideas?
    Thanks!
     
Thread Status:
Not open for further replies.

Share This Page