Thor Plugin Help

Discussion in 'Plugin Development' started by Dead_Skeleton, Sep 10, 2013.

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

    Dead_Skeleton

    Hey guys, I've just started learning to code Java and have gotten into bukkit. The console doesn't load the plugin with any errors but as soon as I right click with the thor axe, it gives an error. Here's the error:

    Code:
    2013-09-10 20:28:03 [SEVERE] Could not pass event PlayerInteractEvent to Thor v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredListener.java:30)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:478)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:463)
        at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:207)
        at net.minecraft.server.v1_6_R2.PlayerInteractManager.interact(PlayerInteractManager.java:374)
        at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:628)
        at net.minecraft.server.v1_6_R2.Packet15Place.handle(SourceFile:58)
        at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:230)
        at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
        at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:125)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:239)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:481)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:413)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.NullPointerException
        at me.Dead_Skeleton.thor.Thor.onPlayerInteract(Thor.java:53)
        at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 17 more
    I don't really know how to fix it.

    Here's my main and only class:
    Code:java
    1. package me.Dead_Skeleton.thor;
    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.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.Action;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class Thor extends JavaPlugin implements Listener {
    20.  
    21. public void onEnable() {
    22. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    23. }
    24.  
    25. public void onDisable() {
    26. Bukkit.getServer().getLogger().info(ChatColor.RED + "The thor plugin has been disabled!");
    27. }
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) {
    30. if(cmd.getName().equalsIgnoreCase("thor")){
    31. if(!(sender instanceof Player)) {
    32. sender.sendMessage(ChatColor.RED + "The console cannot receive kits!");
    33. return true;
    34. }
    35. else {
    36.  
    37. Player p = (Player) sender;
    38.  
    39. ItemStack thoraxe = new ItemStack(Material.WOOD_AXE);
    40. ItemMeta im = thoraxe.getItemMeta();
    41. im.setDisplayName("" + ChatColor.RED + ChatColor.BOLD + "Thor Axe");
    42. im.setLore(Arrays.asList("" + ChatColor.DARK_PURPLE + ChatColor.ITALIC + "The Thor axe of power!!!"));
    43. thoraxe.setItemMeta(im);
    44. p.getInventory().addItem(thoraxe);
    45. p.sendMessage("" + ChatColor.GOLD + "Here is your thor axe of power!");
    46. }
    47. return true;
    48. }
    49. return true;
    50. }
    51. @EventHandler
    52. public void onPlayerInteract(PlayerInteractEvent e) {
    53. if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName() == "" + ChatColor.RED + ChatColor.BOLD + "Thor Axe" && e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && e.getPlayer().getItemInHand().getType() == Material.WOOD_AXE)
    54. e.getClickedBlock().getWorld().strikeLightning(e.getClickedBlock().getLocation());
    55. else {
    56. return;
    57. }
    58. }
    59. }
    60.  


    Please remember that I am new to coding and this is one of my first real plugin that I am trying to make for fun. I have made a couple of other really basic ones.
     
  2. Offline

    tylersyme

    you should do a couple things

    first, instead of "if (blahblahblah.getDisplayer == "Thor Hammer")"

    do if "(blahblahblah.getDisplayer.equalsIgnoreCase(ChatColor.Strip("Thor Hammer"))"

    you also want to check to see if the item has item meta at all using another if statement and also make sure that the item in your hand is not null
    "if (e.getItemInHand() != null && e.getItemInHand().hasItemMeta())"

    if you still get a null pointer exception then it probably has something to do with .getDisplayName()
     
  3. Offline

    Dead_Skeleton

    Thanks for the help, I'll see if it works.
     
  4. you get a null pointer exception because not all items have item meta. The item meta is null, what you could do:
    check if it has item meta, then inside that if, check if it has display name, then check the name.
     
  5. Offline

    Dead_Skeleton

    Well, not sure if this is good or bad news: There are no errors, but it is not striking any lightning. There is no stack trace or null pointer exceptions or anything like that. The event just doesn't trigger. Here's my code:
    Code:java
    1. package me.Dead_Skeleton.thor;
    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.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.Action;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class Thor extends JavaPlugin implements Listener {
    20.  
    21. public void onEnable() {
    22. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    23. }
    24.  
    25. public void onDisable() {
    26. Bukkit.getServer().getLogger().info(ChatColor.RED + "The thor plugin has been disabled!");
    27. }
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) {
    30. if(cmd.getName().equalsIgnoreCase("thor")){
    31. if(!(sender instanceof Player)) {
    32. sender.sendMessage(ChatColor.RED + "The console cannot receive kits!");
    33. return true;
    34. }
    35. else {
    36.  
    37. Player p = (Player) sender;
    38.  
    39. ItemStack thoraxe = new ItemStack(Material.WOOD_AXE);
    40. ItemMeta im = thoraxe.getItemMeta();
    41. im.setDisplayName("" + ChatColor.RED + ChatColor.BOLD + "Thor Axe");
    42. im.setLore(Arrays.asList("" + ChatColor.DARK_PURPLE + ChatColor.ITALIC + "The Thor axe of power!!!"));
    43. thoraxe.setItemMeta(im);
    44. p.getInventory().addItem(thoraxe);
    45. p.sendMessage("" + ChatColor.GOLD + "Here is your thor axe of power!");
    46. }
    47. return true;
    48. }
    49. return true;
    50. }
    51. @EventHandler
    52. public void onPlayerInteract(PlayerInteractEvent e) {
    53. if (e.getPlayer().getItemInHand() != null) return;
    54. if(!(e.getAction() == Action.RIGHT_CLICK_AIR) || e.getAction() == Action.LEFT_CLICK_BLOCK) return;
    55. if(!(e.getPlayer().getItemInHand().equals(Material.WOOD_AXE))) return;
    56. if (!(e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.stripColor("" + ChatColor.RED + ChatColor.BOLD + "Thor Axe"))))
    57. if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName() == "" + ChatColor.RED + ChatColor.BOLD + "Thor Axe" && e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && e.getPlayer().getItemInHand().getType() == Material.WOOD_AXE) {
    58. e.getClickedBlock().getWorld().strikeLightning(e.getClickedBlock().getLocation());
    59. e.getPlayer().sendMessage(ChatColor.RED + "You activated the thor hammer lightning! Don't spam it or I will spam you!!");
    60. }
    61. else {
    62. return;
    63. }
    64. }
    65. }
    66.  


    Hope you can see what's wrong. Please remember that I'm new to this. As you can see, I even put in a message when they strike lightning. That message did NOT send. This proves that the event is not being triggered. As I said, there are no errors.
     
  6. you are doing it a lot more difficult than it needs to be.
    written out of memory:
    Code:java
    1. if(player.getItemInHand().hasItemMeta()){
    2. if(player.getItemInHand().getItemMeta().hasDisplayName()){
    3. if(player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.COLOR + "Axe name"){
    4. //do your stuff!
    5. }
    6. }
    7. }
     
  7. Offline

    Dead_Skeleton

    Thanks so much, it works! How would I get it so that when you right click where you're looking at, it would get that block and strike lightning? Thanks so much for your patience. I appreciate you working with me when I am so new to coding, hopefully I'm doing okay so far. I started a week ago, so yeah :/ .
     
  8. google is your friend ;)
     
    WauloK likes this.
  9. Also correct me if I'm wrong if you right click air it also returns null and ends up throwing a nullpointer exception and also if the item in the players hand does not have any meta information so i would add this to your code ;

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) { if(e.getClickedBlock() == null || e.getPlayer.getInventory().getItemMeta() == null){ return; }
    3. if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName() == "" + ChatColor.RED + ChatColor.BOLD + "Thor Axe" && e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && e.getPlayer().getItemInHand().getType() == Material.WOOD_AXE)
    4. e.getClickedBlock().getWorld().strikeLightning(e.getClickedBlock().getLocation());
    5. else {
    6. return;
    7. }
    8. }
    9. }


    please not i have not tested this!
    hope it helps :)
     
  10. Offline

    tylersyme

    I'm pretty sure it doesn't throw a null, and if it did, it wouldn't matter cause you're using an if statement
     
  11. Offline

    Dead_Skeleton

    Well, there was a null pointer exception when you right click in the air:
    Code:
    2013-09-11 18:06:41 [SEVERE] Could not pass event PlayerInteractEvent to Thor v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredListener.java:30)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:478)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:463)
        at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:207)
        at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:177)
        at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:1021)
        at net.minecraft.server.v1_6_R2.Packet18ArmAnimation.handle(SourceFile:41)
        at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:230)
        at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
        at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:125)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:239)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:481)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:413)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.NullPointerException
        at me.Dead_Skeleton.thor.Thor.onPlayerInteract(Thor.java:55)
        at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 17 more
    I modified my code a little bit, here it is:
    Code:java
    1. package me.Dead_Skeleton.thor;
    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.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class Thor extends JavaPlugin implements Listener {
    19.  
    20. public void onEnable() {
    21. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    22. }
    23.  
    24. public void onDisable() {
    25. Bukkit.getServer().getLogger().info(ChatColor.RED + "The thor plugin has been disabled!");
    26. }
    27.  
    28. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) {
    29. if(cmd.getName().equalsIgnoreCase("thor")){
    30. if(!(sender instanceof Player)) {
    31. sender.sendMessage(ChatColor.RED + "The console cannot receive kits!");
    32. return true;
    33. }
    34. else {
    35.  
    36. Player p = (Player) sender;
    37.  
    38. ItemStack thoraxe = new ItemStack(Material.WOOD_AXE);
    39. ItemMeta im = thoraxe.getItemMeta();
    40. im.setDisplayName("" + ChatColor.RED + ChatColor.BOLD + "Thor Axe");
    41. im.setLore(Arrays.asList("" + ChatColor.DARK_PURPLE + ChatColor.ITALIC + "The Thor axe of power!!!"));
    42. thoraxe.setItemMeta(im);
    43. p.getInventory().addItem(thoraxe);
    44. p.sendMessage("" + ChatColor.GOLD + "Here is your thor axe of power!");
    45. }
    46. return true;
    47. }
    48. return true;
    49. }
    50. @EventHandler
    51. public void onPlayerInteract(PlayerInteractEvent e) {
    52. if(e.getPlayer().getItemInHand().hasItemMeta()){
    53. if(e.getPlayer().getItemInHand().getItemMeta().hasDisplayName()){
    54. if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals("" + ChatColor.RED + ChatColor.BOLD + "Thor Axe")) {
    55. e.getClickedBlock().getWorld().strikeLightning(e.getClickedBlock().getLocation());
    56. e.getPlayer().sendMessage(ChatColor.RED + "You activated the thor lightning! Don't spam it, or I will spam you!!");
    57. }
    58. }
    59. }
    60. }
    61. }
    62.  
     
  12. Offline

    xCyanide

    Dead_Skeleton
    You need to add a null check like this if(e.getClickedBlock != null)
     
  13. Offline

    Dead_Skeleton

    Okay, thanks, I'll try it.
     
  14. it does I have had that problem before, doesn't matter if your using an if statement or not, clicking the air returns null so you have to check for it if your trying to get the block that was clicked. :p

    unless you got the action, left / right click block, this would remove the need for the null check then actually :)

    just as i said above in my earlier post, except if the clicked block is null it returns ;)

    Code:java
    1. if(e.getClickedBlock() == null){ return; }


    or you can remove the need for this check and get the action the player performed, you can also change this to right click block if you so wish;

    Code:java
    1. public void onPlayerInteract(PlayerInteractEvent e) {
    2. if(e.getPlayer().getItemInHand().hasItemMeta()&& e.getAction() == Action.LEFT_CLICK_BLOCK){
    3. if(e.getPlayer().getItemInHand().getItemMeta().hasDisplayName()){
    4. if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals("" + ChatColor.RED + ChatColor.BOLD + "Thor Axe")) {
    5. e.getClickedBlock().getWorld().strikeLightning(e.getClickedBlock().getLocation());
    6. e.getPlayer().sendMessage(ChatColor.RED + "You activated the thor lightning! Don't spam it, or I will spam you!!");
    7. }
    8. }
    9. }
    10. }


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

    Dead_Skeleton

    Thanks so much guys for your help! I'm advancing through this little plugin of mine fast with your help. One more thing, I want to make it so that when the player does the lightning effect with the thor axe then he takes no damage from the lightning. I've tried this:
    Code:java
    1. public void onPlayerDamage(EntityDamageEvent e) {
    2. if(e.getCause().equals(DamageCause.LIGHTNING)) {
    3. e.setDamage(0.0);
    4. }
    5. }


    But it's not working. I know that one of you will probably say, *Sigh* "It's so obvious... ", but again, I just want some help kicking off with the basic features. I'm not making you help me.


    -Thanks!
     
  16. e.seCancelled(true). but note, this will affect all players. btw, i've done this on a request by someone else, is there a popular game mode with this in or somethign?
     
  17. Offline

    MayoDwarf

    Code:
    e.setCancelled(true);
     
Thread Status:
Not open for further replies.

Share This Page