PlayerInteractEvent issues

Discussion in 'Plugin Development' started by ColaCraft, Jul 28, 2013.

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

    ColaCraft

    Hey guys, I am having issues getting this to work- it doesn't seem to be listening at all.

    Code:java
    1. package me.PorterK.RPG;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Arrow;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.plugin.Plugin;
    12.  
    13. public class ClassListener implements Listener{
    14.  
    15. private main plugin;
    16.  
    17. public ClassListener(main plugin) {
    18. this.plugin = plugin;
    19. }
    20.  
    21. public void onEnable(){
    22.  
    23.  
    24.  
    25. Bukkit.getServer().getPluginManager().registerEvents(this, (Plugin) this);
    26. plugin.loadConfiguration();
    27.  
    28. }
    29.  
    30. public void onDisable(){
    31.  
    32.  
    33. }
    34.  
    35.  
    36. @EventHandler
    37. public void onPlayerInteractEvent(PlayerInteractEvent event){
    38.  
    39.  
    40. if((event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) && event.getPlayer().getInventory().getItemInHand().getType() == Material.BOW){
    41. Player p = event.getPlayer();
    42.  
    43. if(plugin.getConfig().getString("users." + p + ".class").equalsIgnoreCase("Archer")){
    44. Arrow ar = (Arrow) p.getWorld().spawn(p.getEyeLocation(), Arrow.class);
    45. ar.setShooter(p);
    46. ar.setVelocity(p.getLocation().getDirection().multiply(2));
    47. }
    48.  
    49. }
    50.  
    51.  
    52. }
    53.  
    54.  
    55. }


    I have several classes for this, because it is a custom RPG plugin I am making for my server.

    If you could help, that'd be awesome. Thanks!
     
  2. Offline

    StevenMANGOS

    Remove your onEnable() and onDisable() from that class and put it into your 'main' class. Inside your onEnable place:
    Code:
    Bukkit.getPluginManager().registerEvents(new ClassListener(this), this);
    Then on your 'ClassListener' class change
    Code:
    private main plugin;
     
    public ClassListener(main plugin) {
        this.plugin = plugin;
    }
    to

    Code:
    private static main plugin;
     
    public ClassListener(main instance) {
        plugin = instance;
    }
     
    ColaCraft likes this.
  3. Offline

    flaaghara

    Why do you have the onEnable() and onDisable() methods in a class that isn't a subclass of JavaPlugin? They aren't automatically invoked and need to go in your class which (it seems) is named "main". It's not listening because the registerEvents() method isn't being invoked due to its encapsulation by onEnable(). Also in that method there seems to be a cast which doesn't make sense - (Plugin). Seems like you have some fundamental errors in this class. I suggest you brush up on Java and flow of control and review the Plugin Tutorial.

    P.S. It's bad convention to have a class name which doesn't start with a capital letter - main.
     
  4. Offline

    ColaCraft

    Thanks bro!

    This was the main class at one point, forgot to remove while converting. :/

    Everything works as suggested.
     
Thread Status:
Not open for further replies.

Share This Page