2 Classes

Discussion in 'Plugin Development' started by NoSpanMan, Nov 10, 2015.

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

    NoSpanMan

    I have two classes in the second to connect the config to the first:
    Code:
    
       private Main plugin;
    
       public Menu(Main main) {
         this.plugin = main;
         plugin.getConfig();
       }
    
    And in the first to connect with the second:
    Code:
     
       private Menu menu;
       
       public void onEnable() {
         menu = new Menu(this);
         getServer().getPluginManager().registerEvents(new Menu(this), this);
           saveConfig();
       }
    
    But i get an error?
    Code:
    [19:38:57 ERROR]: Could not pass event PlayerInteractEvent to MineAdventurePVP v1.0
    org.bukkit.event.EventException
      at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit.jar:git-Bukkit-f27339c]
      at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-f27339c]
      at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-f27339c]
      at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-f27339c]
      at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:210) [craftbukkit.jar:git-Bukkit-f27339c]
      at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:462) [craftbukkit.jar:git-Bukkit-f27339c]
      at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:705) [craftbukkit.jar:git-Bukkit-f27339c]
      at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [craftbukkit.jar:git-Bukkit-f27339c]
      at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [craftbukkit.jar:git-Bukkit-f27339c]
      at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-f27339c]
      at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_65]
      at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_65]
      at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [craftbukkit.jar:git-Bukkit-f27339c]
      at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:676) [craftbukkit.jar:git-Bukkit-f27339c]
      at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-f27339c]
      at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:632) [craftbukkit.jar:git-Bukkit-f27339c]
      at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:540) [craftbukkit.jar:git-Bukkit-f27339c]
      at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
    Caused by: java.lang.NullPointerException
      at org.bukkit.craftbukkit.v1_8_R3.entity.CraftHumanEntity.openInventory(CraftHumanEntity.java:176) ~[craftbukkit.jar:git-Bukkit-f27339c]
      at me.coding.mineadventurepvp.Menu.show(Menu.java:56) ~[?:?]
      at me.coding.mineadventurepvp.Main.onPlayerInteract(Main.java:102) ~[?:?]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_65]
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_65]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_65]
      at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_65]
      at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit.jar:git-Bukkit-f27339c]
      ... 17 more
    >
    
     
  2. Offline

    teej107

  3. Offline

    mcdorli

    Line 56 of tge Menu class bitte.
     
  4. Offline

    teej107

    That doesn't help if they can't understand stacktraces
     
  5. Offline

    mcdorli

    You're right.
     
  6. Offline

    NoSpanMan

    Code:
      p.openInventory(inv);
    
    More:
    Code:
      public void show(Player p) {
      p.openInventory(inv);
      }
    
     
  7. Offline

    mcdorli

    Is the inv variable null?
     
  8. Offline

    NoSpanMan

    No ofcourse not :)!
    Code:
      public Menu(Plugin p) {
      inv = Bukkit.getServer().createInventory(null, 9, "test");
       
      c = createItem(DyeColor.GREEN, ChatColor.GREEN + "1");
      s = createItem(DyeColor.YELLOW, ChatColor.YELLOW + "2");
      a = createItem(DyeColor.RED, ChatColor.RED +"3");
       
      inv.setItem(2, c);
      inv.setItem(4, s);
      inv.setItem(6, a);
      }
    
      private ItemStack createItem(DyeColor dc, String name) {
      ItemStack i = new Wool(dc).toItemStack(1);
      ItemMeta im = i.getItemMeta();
      im.setDisplayName(name);
      im.setLore(Arrays.asList("1", "to " + name.toLowerCase() + " 2"));
      i.setItemMeta(im);
      return i;
      }
    
     
  9. Offline

    teej107

    @NoSpanMan the player or the inventory is null. If the player isn't null then we need to see the whole entire class.
     
  10. Offline

    NoSpanMan

    When i remove this:
    Code:
      private Main plugin;
    
       public Menu(Main main) {
         this.plugin = main;
         plugin.getConfig();
       }
    
    Thats in the second class. Then it works.
     
  11. Offline

    Scimiguy

    Are you instantiating your plugin?

    There can only be one of your plugin..
     
  12. Offline

    teej107

    @NoSpanMan

    @Scimiguy No. A different error will be thrown if he is instantiating a JavaPlugin class
     
  13. Offline

    Scimiguy

    Ah I misread, he's getting an instance of the other class
     
  14. Offline

    NoSpanMan

    Code:
    package me.coding.mineadventurepvp;
    
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.material.Wool;
    import org.bukkit.plugin.Plugin;
    
    public class Menu implements Listener {
    
       private Main plugin;
    
       public Menu(Main main) {
         this.plugin = main;
         plugin.getConfig();
       }
       
      private Inventory inv;
      private ItemStack c, s, a;
       
      public Menu(Plugin p) {
      inv = Bukkit.getServer().createInventory(null, 9, "Gamemode Chooser");
       
      c = createItem(DyeColor.GREEN, ChatColor.GREEN + "1");
      s = createItem(DyeColor.YELLOW, ChatColor.YELLOW + "2");
      a = createItem(DyeColor.RED, ChatColor.RED + "3");
       
      inv.setItem(2, c);
      inv.setItem(4, s);
      inv.setItem(6, a);
      }
       
      private ItemStack createItem(DyeColor dc, String name) {
      ItemStack i = new Wool(dc).toItemStack(1);
      ItemMeta im = i.getItemMeta();
      im.setDisplayName(name);
      im.setLore(Arrays.asList("1", "to " + name.toLowerCase() + " 2"));
      i.setItemMeta(im);
      return i;
      }
       
      public void show(Player p) {
      p.openInventory(inv);
      }
       
      @EventHandler
      public void onInventoryClick(InventoryClickEvent e) {
      if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
      if (e.getCurrentItem().getItemMeta() == null) return;
      if (e.getCurrentItem().getItemMeta().getDisplayName().contains("1")) {
      e.setCancelled(true);
      e.getWhoClicked().closeInventory();
      }
      if (e.getCurrentItem().getItemMeta().getDisplayName().contains("2")) {
      e.setCancelled(true);
      e.getWhoClicked().closeInventory();
      }
      if (e.getCurrentItem().getItemMeta().getDisplayName().contains("3")) {
      e.setCancelled(true);
      e.getWhoClicked().closeInventory();
      }
      }
    
    }
    
     
  15. Offline

    teej107

    @NoSpanMan Do you know what a constructor is and what it does?
     
  16. Offline

    NoSpanMan

    Yeah?

    The problem is in this:
    Code:
      private Main plugin;
    
       public Menu(Main main) {
         this.plugin = main;
         plugin.getConfig();
       }
    
    Because when i remove this all is working fine.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 12, 2015
  17. Offline

    teej107

    Yes or no? Because if you knew about Constructors (which you should because it's part of basic Java) then this can be solved.
     
  18. Offline

    mcdorli

    Jesus, you use no identitations? That's even the problem in html. Press Ctrl+Shift+F to know what I mean by thag.
     
  19. Offline

    NoSpanMan

    It doens't change anything....
     
  20. Offline

    Scimiguy

    @NoSpanMan
    It does for us, repost your code now that you've fixed it
     
  21. Offline

    NoSpanMan

    Main:
    Code:
    package me.coding.mineadventurepvp;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
       // private Menu menu;
    
       public void onEnable() {
         // menu = new Menu(this);
         getServer().getPluginManager().registerEvents(this, this);
         getServer().getPluginManager().registerEvents(new Menu(this), this);
    
         try {
           saveConfig();
           setupConfig(getConfig());
           saveConfig();
         } catch (Exception e) {
           e.printStackTrace();
         }
       }
    
       @SuppressWarnings("deprecation")
       public boolean onCommand(CommandSender sender, Command command, String cmd, String[] args) {
         if (cmd.equalsIgnoreCase("kitb")) {
           if (sender instanceof Player) {
             Player p = (Player) sender;
             if (args.length == 0) {
               String[] classes = getConfig().getString("Kits.Names").split(",");
    
               for (String s : classes) {
                 if (s != null) {
                   p.sendMessage("[" + ChatColor.AQUA + s + ChatColor.WHITE + "] " + ChatColor.GRAY + ": "
                       + ChatColor.DARK_GRAY + "Adds you to the " + s + " class!");
                 }
               }
             } else {
               int i = 0;
    
               for (String s : getConfig().getConfigurationSection("Kits").getKeys(false)) {
                 i++;
                 if (args[0].equalsIgnoreCase(s)) {
                   if (p.hasPermission("minekits." + s)) {
                     p.getInventory().clear();
                     try {
                       String items = getConfig().getString("Kits." + s + ".Items");
    
                       String[] indiItems = items.split(",");
    
                       for (String s1 : indiItems) {
                         String[] itemAmounts = s1.split("-");
                         ItemStack item = new ItemStack(Integer.valueOf(itemAmounts[0]),
                             Integer.valueOf(itemAmounts[1]));
                         p.getInventory().addItem(item);
                       }
                       p.updateInventory();
                     } catch (Exception e) {
                       e.printStackTrace();
                     }
                   } else {
                     p.sendMessage(ChatColor.RED + "Je hebt hier geen permissies voor!");
                   }
                 }
               }
             }
           }
         }
         return false;
       }
    
       private void setupConfig(FileConfiguration config) throws IOException {
         if (!new File(getDataFolder(), "RESET.FILE").exists()) {
           config.set("Kits.Mage.Items", "50-64,278-1,277-4");
           config.set("Kits.Archer.Items", "64-64,1-1,5-4");
    
           config.set("Kits.Names", "Mage,Archer");
           config.set("GUI.Naam", "MineAdventure");
           config.set("GUI.Slot1.Naam", "Niks");
    
           new File(getDataFolder(), "RESET.FILE").createNewFile();
         }
       }
    }
    
    Second:
    Code:
    package me.coding.mineadventurepvp;
    
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    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.inventory.meta.ItemMeta;
    import org.bukkit.material.Wool;
    import org.bukkit.plugin.Plugin;
    
    public class Menu implements Listener {
    
       private Main plugin;
    
       public Menu(Main main) {
         this.plugin = main;
         plugin.getConfig();
       }
    
       private Inventory inv;
       private ItemStack c, s, a;
    
       public Menu(Plugin p) {
         inv = Bukkit.getServer().createInventory(null, 9, "1");
    
         c = createItem(DyeColor.GREEN, ChatColor.GREEN + "1");
         s = createItem(DyeColor.YELLOW, ChatColor.YELLOW + "2");
         a = createItem(DyeColor.RED, ChatColor.RED + "3");
    
         inv.setItem(2, c);
         inv.setItem(4, s);
         inv.setItem(6, a);
       }
    
       private ItemStack createItem(DyeColor dc, String name) {
         ItemStack i = new Wool(dc).toItemStack(1);
         ItemMeta im = i.getItemMeta();
         im.setDisplayName(name);
         im.setLore(Arrays.asList("1", "to " + name.toLowerCase() + " 2"));
         i.setItemMeta(im);
         return i;
       }
    
       public void show(Player p) {
         p.openInventory(inv);
       }
    
       @EventHandler
       public void onInventoryClick(InventoryClickEvent e) {
         if (!e.getInventory().getName().equalsIgnoreCase(inv.getName()))
           return;
         if (e.getCurrentItem().getItemMeta() == null)
           return;
         if (e.getCurrentItem().getItemMeta().getDisplayName().contains("1")) {
           e.setCancelled(true);
           e.getWhoClicked().closeInventory();
         }
         if (e.getCurrentItem().getItemMeta().getDisplayName().contains("2")) {
           e.setCancelled(true);
           e.getWhoClicked().closeInventory();
         }
         if (e.getCurrentItem().getItemMeta().getDisplayName().contains("3")) {
           e.setCancelled(true);
           e.getWhoClicked().closeInventory();
         }
       }
    
       @EventHandler
       public void onPlayerInteract(PlayerInteractEvent e) {
         if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
           show(e.getPlayer());
         }
       }
    }
    
     
  22. Offline

    teej107

    @NoSpanMan Since you don't know much about Constructors, I am just going to say to keep that removed. Hopefully you'll understand later.
     
  23. Offline

    mcdorli

    I would say him, to not do bukkit at least before he learn the very basic of java, constructors are one of the pillars of java, you can't do real OO code without them.
     
    teej107 likes this.
Thread Status:
Not open for further replies.

Share This Page