Solved Replace a command with left click

Discussion in 'Plugin Development' started by klofno1, Jul 11, 2013.

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

    klofno1

    Hello could anyone tell me how to replace the
    The onCommand() Method

    with a left click method?

    So, in stead of typing a command, if you left click the action will occur

    code :
    Code:java
    1. package me.JonathanNLD.Mesmer;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class Mesmer extends JavaPlugin {
    8. private static final Logger log = Logger.getLogger("Minecraft");
    9. public static Mesmer plugin;
    10.  
    11. @Override
    12. public void onEnable(){
    13. log.info("The Mesmer plugin has been enabled!");
    14. }
    15.  
    16. @Override
    17. public void onDisable(){
    18. log.info("The Mesmer plugin has been disabled!");
    19. }
    20.  
    21. public boolean
    22. }
    23.  
     
  2. Offline

    Sessional

    klofno1
    Need to create a PlayerInteractEvent listener, this way you can make it so any time a player does a specific action you can do whatever you please.
     
  3. Offline

    klofno1

    Could you tell me in detail how to do that? I am new to this.
     
  4. Offline

    xTrollxDudex

    klofno1
    Register your listener in the onEnable()
    Make a new class
    Name it the name you registered it as
    Have @EventHandler and have public void onInteract(PlayerInteractionEvent e){
    Put in if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK){
    Now you can do the actions a regular command has. Player player = e.getPlayer();
    Close with curly braces
     
  5. Offline

    klofno1

    Thank you, how do I register the listener in the onEnable?
     
  6. Offline

    Sessional

  7. Offline

    klofno1

  8. Offline

    Sessional

    klofno1
    Note the constructor public LoginListener
    Code:
    Registering Events in your Listener
     
    There are many ways to register your events. Here's an example where you register them in your listener class.
    import org.bukkit.event.Listener;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.player.PlayerLoginEvent;
     
    public class LoginListener implements Listener {
        public LoginListener(LoginPlugin plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
     
        @EventHandler
        public void normalLogin(PlayerLoginEvent event) {
            // Some code here
        }
     
        @EventHandler(priority = EventPriority.HIGH)
        public void highLogin(PlayerLoginEvent event) {
            // Some code here
        }
    }
     
  9. Offline

    xTrollxDudex

    In your main class you have onEnable(), onDisable(), onCommand(..) right?
    You do this:
    Code:java
    1. @Override
    2. public void onEnable(){
    3. getServer().getPluginManager().registerEvents(new <listener name>(), this);
    4. }
     
  10. Offline

    klofno1


    SO :

    Code:
    package me.JonathanNLD.Mesmer;
     
    import java.util.logging.Logger;
     
     
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Mesmer extends JavaPlugin {
        private static final Logger log = Logger.getLogger("Minecraft");
        public static Mesmer plugin;
     
        @Override
        public void onEnable(){
        getServer().getPluginManager().registerEvents(new Listener(), this);
        }
            log.info("The Mesmer plugin has been enabled!");
        }
     
        @Override
        public void onDisable(){
            log.info("The Mesmer plugin has been disabled!");
        }
     
    }
    
     
  11. Offline

    Sessional

    klofno1
    Your braces don't link up correctly. You seem to have log.info enabled message outside of a method.
     
  12. Offline

    xTrollxDudex

  13. Offline

    klofno1


    Now I have this :
    Code:java
    1. package me.JonathanNLD.Mesmer;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5.  
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Mesmer extends JavaPlugin {
    10. private static final Logger log = Logger.getLogger("Minecraft");
    11. public static Mesmer plugin;
    12.  
    13. @Override
    14. public void onEnable(){
    15. getServer().getPluginManager().registerEvents(new Listener(), this);
    16. log.info("The Mesmer plugin has been enabled!");
    17. }
    18.  
    19. @Override
    20. public void onDisable(){
    21. log.info("The Mesmer plugin has been disabled!");
    22. }
    23.  
    24. }


    but it gives me this error :
    cannot instantiate the type listener
     
  14. Offline

    xTrollxDudex

    klofno1
    No don't import it!

    EDIT: it might help to use a different listener name, just sayin
     
  15. Offline

    Sessional

    klofno1
    That's because Listener() isn't the name of your class. Is your event handling method in a new class?
    If so call that new YourClassName(); If not it will probably be this because they are in the same class you are calling the method from.
     
  16. Offline

    klofno1


    Ok, I have got my main class sorted out (it is called Mesmer)

    No errors or anything :
    Code:java
    1. package me.JonathanNLD.Mesmer;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5.  
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Mesmer extends JavaPlugin {
    10. private static final Logger log = Logger.getLogger("Minecraft");
    11. public static Mesmer plugin;
    12.  
    13. @Override
    14. public void onEnable(){
    15. getServer().getPluginManager().registerEvents((Listener) new PlayerInteractionEvent(), this);
    16. log.info("The Mesmer plugin has been enabled!");
    17. }
    18.  
    19. @Override
    20. public void onDisable(){
    21. log.info("The Mesmer plugin has been disabled!");
    22. }
    23.  
    24. }


    But what about my "PlayerInteractionEvent" class?

    Code:java
    1. package me.JonathanNLD.Mesmer;
    2.  
    3. import org.bukkit.event.block.Action;
    4.  
    5. public class PlayerInteractionEvent {
    6. @EventHanlder
    7. public void onInteract(PlayerInteractionEvent e){
    8. if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction().LEFT_CLICK_BLOCK){
    9. Player player = e.getPlayer()
    10. }
    11. }
    12.  
    13. }
    14.  
     
  17. Offline

    Firefly

    All listener classes need to implement "Listener"
     
  18. Offline

    Sessional

    @klofno
    public class PlayerInteractionEvent is a dangerous thing - it's also the name of a class inside bukkit, you may end up class masking with that. Try like MesmerListener instead? and make it implement Listener.
     
    xTrollxDudex likes this.
  19. Offline

    Firefly

    The Bukkit class is PlayerInteractEvent not Interaction. He needs to fix what the method receives as well, because he's currently passing in his class not the actual event.
     
    xTrollxDudex likes this.
  20. Offline

    xTrollxDudex

    Sessional klofno1
    Ahahahaha you forgot 1 in your tag. Next time click on the tahg button :)

    Well, now you can execute stuff, just put in what you would have inside your command....
     
  21. Offline

    klofno1


    Now I have got this in the MesmerListener class :

    Code:java
    1. package me.JonathanNLD.Mesmer;
    2.  
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.block.Action;
    6.  
    7. public class MesmerListener implements Listener {
    8. @EventHandler
    9. public void onInteract(PlayerInteractionEvent e){
    10. if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK){
    11.  
    12. }
    13. }
    14.  
    15. }
    16.  


    PlayerInteractionEvent gives an error :
    "cannot be resolved to a type"

    Once again, thank you for helping me I am a total beginner
     
  22. Offline

    Sessional

    Firefly klofno1
    Fair enough. I looked at the parameter and saw the same name as the class, didn't look to much into it. But yes - the method signature needs to be (PlayerInteractEvent e) not (PlayerInteractionEvent e)

    xTrollxDudex
    I did. I hit backspace to much when I typod right off the bat.
     
  23. Offline

    klofno1

    Sessional

    AH now it work thank you!
    So now I am I done with that part! Thank you so much.
    Now I need to learn how I make the person who left clicks invisible and spawn 2 clones that attack the opponent.... gosh....

    Could you help me with that? I'll post the code in a sec!

    ---------------------------------------------------------------------------------------------------------
    Mesmer.java

    Code:java
    1. package me.JonathanNLD.Mesmer;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5.  
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Mesmer extends JavaPlugin {
    10. private static final Logger log = Logger.getLogger("Minecraft");
    11. public static Mesmer plugin;
    12.  
    13. @Override
    14. public void onEnable(){
    15. getServer().getPluginManager().registerEvents((Listener) new MesmerListener(), this);
    16. log.info("The Mesmer plugin has been enabled!");
    17. }
    18.  
    19. @Override
    20. public void onDisable(){
    21. log.info("The Mesmer plugin has been disabled!");
    22. }
    23.  
    24. }


    MesmerListener.java

    Code:java
    1. package me.JonathanNLD.Mesmer;
    2.  
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.block.Action;
    6. import org.bukkit.event.player.PlayerInteractEvent;
    7.  
    8. public class MesmerListener implements Listener {
    9. @EventHandler
    10. public void onInteract(PlayerInteractEvent e){
    11. if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK){
    12.  
    13. }
    14. }
    15.  
    16. }
    17.  
     
  24. Offline

    Sessional

    klofno1 xTrollxDudex Firefly
    Tagged TrollDude and firefly - invisibility is not my cup of tea. As for clones - I don't even know where to start except for forcefully sending players packets to create the player in multiple spots.
     
  25. Offline

    xTrollxDudex

    klofno1
    What sessional said. The correct parameter is PlayerInteractEvent, change your code to public void onInteract(PlayerInteractEvent e)
    By the way, a VERY helpful link for developers: http://jd.bukkit.org/dev/apidocs/
     
  26. Offline

    klofno1

    Well my question for this thread has been answered, thank you all! All ask your help in my next thread with my next problem! I'll make a donation to your PayPal when the plugin is finished if you'd like to.
    @Firefly
    @Sessional
    @xTrollxDudex
     
    xTrollxDudex likes this.
  27. Offline

    xTrollxDudex

    klofno1
    I serve for free. By the way, in the if statement, do player.addPotionEffect(new PotionEffect(PotionType.INVISIBILITY, Integer.MAX_VALUE); then when you want to have him visible, do player.removePotionsEffects();
     
Thread Status:
Not open for further replies.

Share This Page