Solved Help! - The EventHandler doesn't work!

Discussion in 'Plugin Development' started by Charliechumbuck, May 3, 2014.

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

    Charliechumbuck

    The title explains it all. Here's my coding (it really is only one class):

    Code:java
    1. package minecraft.Charliechumbuck.me;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    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.Listener;
    12. import org.bukkit.event.block.Action;
    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 PortalGunMain extends JavaPlugin implements Listener{
    19. boolean firstportalfired = false;
    20. boolean secondportalfired = false;
    21. ItemStack pgn = new ItemStack(Material.IRON_BARDING);
    22. ItemStack pgo = new ItemStack(Material.GOLD_BARDING);
    23. ItemStack pgb = new ItemStack(Material.DIAMOND_BARDING);
    24. Location blockLocOrange;
    25. Location blockLocBlue;
    26. public void onDisable(){
    27.  
    28. }
    29.  
    30. public void onEnable(){
    31. getServer().getPluginManager().registerEvents(this, this);
    32. }
    33.  
    34. @SuppressWarnings("deprecation")
    35. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    36. if(cmd.getLabel().equalsIgnoreCase("portalgun")){
    37. Player player = (Player) sender;
    38. ItemMeta pgnmeta = pgn.getItemMeta();
    39. pgnmeta.setDisplayName(ChatColor.GRAY+""+ChatColor.BOLD+"Portal Gun");
    40. pgn.setItemMeta(pgnmeta);
    41. player.getInventory().setItem(0, pgn);
    42. player.updateInventory();
    43. return true;
    44. }
    45. return false;
    46. }
    47.  
    48. @EventHandler(priority = EventPriority.MONITOR)
    49. public void onPlayerInteract(PlayerInteractEvent e){
    50. Player p = e.getPlayer();
    51. ItemMeta pgometa = pgo.getItemMeta();
    52. pgometa.setDisplayName(ChatColor.GOLD+""+ChatColor.BOLD+"Portal Gun");
    53. pgo.setItemMeta(pgometa);
    54. ItemMeta pgbmeta = pgb.getItemMeta();
    55. pgbmeta.setDisplayName(ChatColor.BLUE+""+ChatColor.BOLD+"Portal Gun");
    56. pgb.setItemMeta(pgbmeta);
    57. if(e.getItem()==pgn||e.getItem()==pgo||e.getItem()==pgb){
    58. if(e.getAction()==Action.RIGHT_CLICK_AIR||e.getAction()==Action.RIGHT_CLICK_BLOCK){
    59. @SuppressWarnings("deprecation")
    60. Location blockLocOrange = p.getTargetBlock(null, 100).getLocation();
    61. p.sendMessage(ChatColor.GOLD+"You created an orange portal!");
    62. firstportalfired = true;
    63. p.setItemInHand(pgo);
    64. portalTravel(p,blockLocOrange,blockLocBlue);
    65. }else{
    66. firstportalfired = false;
    67. }
    68. }else if(e.getItem()==pgn||e.getItem()==pgo||e.getItem()==pgb){
    69. if(e.getAction()==Action.LEFT_CLICK_AIR||e.getAction()==Action.LEFT_CLICK_BLOCK){
    70. @SuppressWarnings("deprecation")
    71. Location blockLocBlue = p.getTargetBlock(null, 100).getLocation();
    72. p.sendMessage(ChatColor.BLUE+"You created a blue portal!");
    73. secondportalfired = true;
    74. p.setItemInHand(pgb);
    75. portalTravel(p,blockLocOrange,blockLocBlue);
    76. }else{
    77. firstportalfired = false;
    78. }
    79. }
    80. }
    81.  
    82. public void portalTravel(Player player, Location blockLocOrange,Location blockLocBlue){
    83. if(player.getLocation()==blockLocOrange){
    84. if(secondportalfired){
    85. player.teleport(blockLocBlue);
    86. }else if(!secondportalfired){
    87. player.sendMessage(ChatColor.RED+"Say, did a glitch happen or what? Because I can't find the "+ChatColor.BLUE+"blue "
    88. +ChatColor.RED+"portal!");
    89. }
    90. }
    91. }
    92. }
     
  2. Offline

    Slikey

    1. What should this actually do?
    2. What is this doing?
    3. Why do you use deprecated methods?
    4. Is there any stacktrace?
    5. Is the Event fired? (Debugging tested?)

    Why do people just hand in code and hope, that they get help.. :/
     
  3. Offline

    bdubz4552

    Despite the inner me saying not to help because all you did here was give us your source and say "fix it", here's some basic things that you should consider:
    • You mashed every possible thing you could into one class. You should probably look into making separate classes for handling events, and executing commands.
    • The deprecated methods could be partially responsible.
    • You don't need to specify an event priority; perhaps try with no priority given
    • Solely because of the clutter in this class it is likely that things are conflicting and causing errors.
    • Rule number one of debugging: Read Stack Traces
    EDIT: Player.getTargetBlock() is deprecated because it returns a list of block IDs instead of materials. Again, because there is no stack trace I don't know what the issue is, but that is potentially the issue.
     
  4. Offline

    ZekeMo

  5. Offline

    akabarblake

    @EventHandler
    You should see that ... >.<
     
  6. Offline

    Charliechumbuck

    I saw it and did everything it said (for what I needed), but it didn't work (I also tried to test other events to see if they would work(Example: PlayerJoinEvent, send message), but it didn't work.
    There has to be something I missed, ZekeMo

    I also tried to do stack trace and print the stack trace, but it wouldn't do so.

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

    AoH_Ruthless

    What does that even mean?
     
  8. Offline

    ZekeMo

    @Charliechumbuck
    You need two things
    1. The class that will wrap around the method, that does what you want.
    Code:
        public final class runMyCode implements Listener {
            @EventHandler
            public void doTheThingIWant(PlayerInteractEvent e) {
                Player p = e.getPlayer();
                // your code here...
            }
        }
    2. You need to register the listener.
    Add this line to your onEnable() method
    Code:
    getServer().getPluginManager().registerEvents(new runMyCode(), this);
     
  9. Offline

    Charliechumbuck

    I tried that, but it still ain't workin'!

    Here's meh codin':

    Main class:

    Code:
    package minecraft.Charliechumbuck.me;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class PortalGunMain extends JavaPlugin{
        boolean firstportalfired = false;
        boolean secondportalfired = false;
        ItemStack pgn = new ItemStack(Material.IRON_BARDING);
        ItemStack pgo = new ItemStack(Material.GOLD_BARDING);
        ItemStack pgb = new ItemStack(Material.DIAMOND_BARDING);
        Location blockLocOrange;
        Location blockLocBlue;
        public void onDisable(){
           
        }
       
        public void onEnable(){
            getServer().getPluginManager().registerEvents(new PortalShootListener(this), this);
        }
       
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getLabel().equalsIgnoreCase("portalgun")){
                Player player = (Player) sender;
                ItemMeta pgnmeta = pgn.getItemMeta();
                pgnmeta.setDisplayName(ChatColor.GRAY+""+ChatColor.BOLD+"Portal Gun");
                pgn.setItemMeta(pgnmeta);
                player.getInventory().setItem(0, pgn);
                player.updateInventory();
                return true;
            }
            return false;
        }
       
        public void portalTravel(Player player, Location blockLocOrange,Location blockLocBlue){
            if(player.getLocation()==blockLocOrange){
                if(secondportalfired){
                    player.teleport(blockLocBlue);
                }else if(!secondportalfired){
                    player.sendMessage(ChatColor.RED+"Say, did a glitch happen or what? Because I can't find the "+ChatColor.BLUE+"blue "
                            +ChatColor.RED+"portal!");
                }
            }
        }
    }
    SubClass:

    Code:
    package minecraft.Charliechumbuck.me;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    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.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
     
    public class PortalShootListener extends PortalGunMain implements Listener{
       
        public PortalShootListener(PortalGunMain plugin){
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onPlayerInteract(final PlayerInteractEvent e){
            Player p = e.getPlayer();
            if(e.getItem()==new ItemStack(Material.ARROW)){
                p.sendMessage("hey");
            }
            ItemMeta pgometa = pgo.getItemMeta();
            pgometa.setDisplayName(ChatColor.GOLD+""+ChatColor.BOLD+"Portal Gun");
            pgo.setItemMeta(pgometa);
            ItemMeta pgbmeta = pgb.getItemMeta();
            pgbmeta.setDisplayName(ChatColor.BLUE+""+ChatColor.BOLD+"Portal Gun");
            pgb.setItemMeta(pgbmeta);
            if(e.getItem()==pgn||e.getItem()==pgo||e.getItem()==pgb){
                if(e.getAction()==Action.RIGHT_CLICK_AIR||e.getAction()==Action.RIGHT_CLICK_BLOCK){
                    @SuppressWarnings("deprecation")
                    Location blockLocOrange = p.getTargetBlock(null, 100).getLocation();
                    p.sendMessage(ChatColor.GOLD+"You created an orange portal!");
                    firstportalfired = true;
                    p.setItemInHand(pgo);
                    portalTravel(p,blockLocOrange,blockLocBlue);
                }else{
                    firstportalfired = false;
                }
            }else if(e.getItem()==pgn||e.getItem()==pgo||e.getItem()==pgb){
                if(e.getAction()==Action.LEFT_CLICK_AIR||e.getAction()==Action.LEFT_CLICK_BLOCK){
                    @SuppressWarnings("deprecation")
                    Location blockLocBlue = p.getTargetBlock(null, 100).getLocation();
                    p.sendMessage(ChatColor.BLUE+"You created a blue portal!");
                    secondportalfired = true;
                    p.setItemInHand(pgb);
                    portalTravel(p,blockLocOrange,blockLocBlue);
                }else{
                    firstportalfired = false;
                }   
            }
        }
    }
    ZekeMo
    just thought I'd tag ya.

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

    ZekeMo

    @Charliechumbuck
    ummm what I meant was
    Code:
    public class PortalGunMain {
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            // your code here doesn't affect the listener
        }
     
        class PortalShootListener implements Listener {
            @EventHandler
            public void onPlayerInteract(PlayerInteractEvent event) {
                Player p = event.getPlayer();
                // your code here...
            }
        }
     
        public void onEnable() {
            getServer().getPluginManager().registerEvents(new runMyCode(), this);
        }
    }
     
  11. Offline

    Charliechumbuck

    ZekeMo
    IT STILL DOESN'T WORK!!!!!!

    Here's meh codin':

    Code:java
    1. package minecraft.Charliechumbuck.me;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    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.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class PortalGunMain extends JavaPlugin{
    18. boolean firstportalfired = false;
    19. boolean secondportalfired = false;
    20. ItemStack pgn = new ItemStack(Material.IRON_BARDING);
    21. ItemStack pgo = new ItemStack(Material.GOLD_BARDING);
    22. ItemStack pgb = new ItemStack(Material.DIAMOND_BARDING);
    23. Location blockLocOrange;
    24. Location blockLocBlue;
    25. public void onDisable(){
    26.  
    27. }
    28.  
    29.  
    30. @SuppressWarnings("deprecation")
    31. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    32. if(cmd.getLabel().equalsIgnoreCase("portalgun")){
    33. Player player = (Player) sender;
    34. ItemMeta pgnmeta = pgn.getItemMeta();
    35. pgnmeta.setDisplayName(ChatColor.GRAY+""+ChatColor.BOLD+"Portal Gun");
    36. pgn.setItemMeta(pgnmeta);
    37. player.getInventory().setItem(0, pgn);
    38. player.updateInventory();
    39. return true;
    40. }
    41. return false;
    42. }
    43.  
    44. public class PortalShootListener implements Listener{
    45. @EventHandler
    46. public void onPlayerInteract(final PlayerInteractEvent e){
    47. Player p = e.getPlayer();
    48. if(e.getItem()==new ItemStack(Material.ARROW)){
    49. p.sendMessage("hey");
    50. }
    51. ItemMeta pgometa = pgo.getItemMeta();
    52. pgometa.setDisplayName(ChatColor.GOLD+""+ChatColor.BOLD+"Portal Gun");
    53. pgo.setItemMeta(pgometa);
    54. ItemMeta pgbmeta = pgb.getItemMeta();
    55. pgbmeta.setDisplayName(ChatColor.BLUE+""+ChatColor.BOLD+"Portal Gun");
    56. pgb.setItemMeta(pgbmeta);
    57. if(e.getItem()==pgn||e.getItem()==pgo||e.getItem()==pgb){
    58. if(e.getAction()==Action.RIGHT_CLICK_AIR||e.getAction()==Action.RIGHT_CLICK_BLOCK){
    59. @SuppressWarnings("deprecation")
    60. Location blockLocOrange = p.getTargetBlock(null, 100).getLocation();
    61. p.sendMessage(ChatColor.GOLD+"You created an orange portal!");
    62. firstportalfired = true;
    63. p.setItemInHand(pgo);
    64. portalTravel(p,blockLocOrange,blockLocBlue);
    65. }else{
    66. firstportalfired = false;
    67. }
    68. }else if(e.getItem()==pgn||e.getItem()==pgo||e.getItem()==pgb){
    69. if(e.getAction()==Action.LEFT_CLICK_AIR||e.getAction()==Action.LEFT_CLICK_BLOCK){
    70. @SuppressWarnings("deprecation")
    71. Location blockLocBlue = p.getTargetBlock(null, 100).getLocation();
    72. p.sendMessage(ChatColor.BLUE+"You created a blue portal!");
    73. secondportalfired = true;
    74. p.setItemInHand(pgb);
    75. portalTravel(p,blockLocOrange,blockLocBlue);
    76. }else{
    77. firstportalfired = false;
    78. }
    79. }
    80. }
    81. }
    82.  
    83.  
    84. public void onEnable(){
    85. getServer().getPluginManager().registerEvents(new PortalShootListener(), this);
    86. }
    87.  
    88. public void portalTravel(Player player, Location blockLocOrange,Location blockLocBlue){
    89. if(player.getLocation()==blockLocOrange){
    90. if(secondportalfired){
    91. player.teleport(blockLocBlue);
    92. }else if(!secondportalfired){
    93. player.sendMessage(ChatColor.RED+"Say, did a glitch happen or what? Because I can't find the "+ChatColor.BLUE+"blue "
    94. +ChatColor.RED+"portal!");
    95. }
    96. }
    97. }
    98. }


    (the whole arrow thing was just a test to see if it was just the "e.getAction()" thing or if it was something to do with the eventhandler thing)

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

    Aqua

  13. Offline

    Mayoz

    Your onEnable() should be in the class that extends JavaPlugin
     
  14. Offline

    Charliechumbuck

    Aqua Mayoz
    well, the console does say that the plugin is out of date, but I don't know how to fix that. Everything else in the plugin is working okay except the event handler. ZekeMo
     
  15. Offline

    Europia79

    Charliechumbuck

    Post the error.

    http://dl.bukkit.org/downloads/craftbukkit/

    Also, you could try with a different version of craftbukkit.

    Also, try a decompiler or archive program (like 7zip) to look inside your JAR file to make sure it's packaged correctly (all your classes and YMLs actually made it into the JAR).
     
  16. Offline

    Mayoz

  17. Offline

    Charliechumbuck

    Mayoz Europia79
    Okay, the gist thing didn't work (but thanks for helping anyway :)), but I did get an error in the console saying:
    "Server permissions file is not a valid yml", but I don't use permissions!
     
  18. Offline

    ZekeMo

    @Charliechumbuck
    Your problem is with your if statement on line 48 it should be this
    Code:
    if(e.getItem().getType().equals(Material.ARROW)){
    you should only use == if you are comparing ints doubles floats longs shorts bytes and chars. If you want to compare two Objects use .equals
     
  19. Offline

    Charliechumbuck

    THANK YOU SO MUCH!!!!! I know I should've known better about that "==" thing (it might be because I'm more used to regular java as in "non-bukkit" stuff, but who am I to brag?). :) :) :)

    ZekeMo
    Just thought I'd tag ya to see that last message, and sorry to bug ya. :)

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

    ZekeMo

    It's my pleasure I joined the forums so I could use my knowledge to help other people. If you have any more questions I'd be happy to help in the future.
     
  21. Offline

    Charliechumbuck

Thread Status:
Not open for further replies.

Share This Page