Player right click event help

Discussion in 'Plugin Development' started by Shikishima, Apr 7, 2017.

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

    Shikishima

    i am trying to make a plugin where if you right click with a dead bush renamed to "Parasite" it will replace the item for an iron sword renamed "Parasite Blade", Then when you right click with the iron sword renamed "Parasite Blade" it will replace the "Parasite Blade" with a dead bush renamed "Parasite"

    i also want to make it so when you left click with the dead bush renamed to "Parasite" it will fire an arrow in the direction of the players crosshair with a 1 second cooldown. does anyone know how i can do this? this is the code i have so far, i am not very good at bukkit plugin making so if anyone can help me i would greatly appreciate it.
    Code:
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    
    public class event implements Listener {
      
        @EventHandler
        public void on
    
    }
     
    Last edited by a moderator: Apr 7, 2017
  2. Offline

    Ragnarok_

    For this you would want to use 'PlayerInteractEvent' and use and if statement to the item, with an itemstack. There are a bunch different event# options(e.i event.setItem, event.whatever)
    For firing an arrow you would want do to:
    Code:
    Arrow a = e.getPlayer().launchProjectile(Arrow.class);
    This is the only code I'm giving you. And for the cooldown, create a runnable and an integer for the time. Set the runnable time as long as you want and make a public void run() or whatever and inside that run an if statement to check whether the time != 0. If so, set the event cancelled.
     
  3. Offline

    ClassifiedDev

    To make the right click event:
    1. Make a PlayerInteractEvent
    2. Check the action the player is doing.
    3. Check the name of the item they right clicked.
    4. If its the correct action and name do whatever.
     
  4. Offline

    Shikishima

    @Ragnarok_

    this is the code i have done so far but there are some yellow line errors in it, i will underline and bold the error parts.

    the plugin loads and shows in /plugins but right clicking with the dead bush doesn't do anything



    public class Event {

    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
    Player player = event.getPlayer();

    if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    if(player.getItemInHand().getType() == Material.DEAD_BUSH){
    Arrow a = event.getPlayer().launchProjectile(Arrow.class);

    }
    }}}
     
    Last edited: Apr 7, 2017
  5. Offline

    Zombie_Striker

    @Shikishima
    1. Since 1.9, you can get the main hand and the off-hand. Because of this, getItemInHand is no longer recommended, as you should account for both hands. Instead of that, use Player#getInventory().getItemInMainHand()
    2. You never reference the a variable. Because of this, you can just call the launch method without creating the variable.
     
  6. Offline

    Shikishima

    @Zombie_Striker

    ah thank you for the help but now it has a red error which i will underline and bold now, and what do you mean by you dont reference the a variable, what do i put there instead

    import org.bukkit.Material;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;

    public class Event {

    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
    Player player = event.getPlayer();

    if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    if(Player#getInventory().getItemInMainHand().getType() == Material.DEAD_BUSH){
    Arrow a = event.getPlayer().launchProjectile(Arrow.class);

    }
    }
    }}
     
  7. Offline

    Zombie_Striker

    @Shikishima
    The # means that you have to call the getInventory method on a Player instance, not the Player class. Replace Player with the player's instance, and replace the # with a period.

    Just remove the "Arrow a =" bit. If you're not going to call a in any way, then there is no need to store it as a variable.
     
  8. Offline

    Shikishima

    @Zombie_Striker

    what is a players instance, i am not very good with bukkit so im not familiar with all these things
     
  9. Offline

    Ragnarok_

    He means, you'll have to get the player that is doing the event. So you'll want to make a variable. Like I did with the arrow, because I thought you'd want to add velocity and such, (Arrow a =...) So, let's get the player. You'd use 'Player p = (Player) e.getPlayer(). So, you're getting the player, and you'll use p.getInventory() or whatever. You're getting that player that did the event.
     
  10. Offline

    Shikishima

    @Ragnarok_

    ok i think i have got it right but i would like to add velocity to the arrow and there is 1 little error i will show you with bold and underlined, its that 1 half of the bracket at the end of getType

    public class Event {

    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
    Player player = (Player) event.getPlayer();

    if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    if(Player) event.getPlayer().GetInventory().getItemInMainHand().getType() == Material.DEAD_BUSH){
    Arrow a = event.getPlayer().launchProjectile(Arrow.class);

    }
    }
    }}
     
  11. Offline

    timtower Administrator Administrator Moderator

  12. Offline

    Shikishima

    @timtower

    what do you mean? do you mean Player player = (Player)
     
  13. Offline

    timtower Administrator Administrator Moderator

  14. Offline

    Shikishima

    @timtower

    so i should change everything that is "(Player)" to what
     
  15. Offline

    timtower Administrator Administrator Moderator

    @Shikishima To nothing, you are casting a player to a player.
     
  16. Offline

    Shikishima

    @timtower

    i have 1 error now and its underlined in yellow

    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
    Player player = event.getPlayer();

    if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    if (event.getPlayer().getInventory().getItemInMainHand().getType() == Material.DEAD_BUSH);{
    event.getPlayer().launchProjectile(Arrow.class);

    }
    }
    }}
     
  17. Offline

    timtower Administrator Administrator Moderator

    @Shikishima When coding: hover your mouse over it. Then it shows what the error is.
     
  18. Offline

    Shikishima

    @timtower

    the value of the local variable player is not used
     
  19. Offline

    timtower Administrator Administrator Moderator

    @Shikishima Then start using it.
    As you do use event.getPlayer multiple times.
     
  20. Offline

    Shikishima

    @timtower

    so use player insdead of event.getPlayer?
     
  21. Offline

    timtower Administrator Administrator Moderator

  22. Offline

    Shikishima

    ok i have fixed it up and there are no errors now


    public class Event {

    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
    Player player = event.getPlayer();

    if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    if (player.getInventory().getItemInMainHand().getType() == Material.DEAD_BUSH);{
    player.launchProjectile(Arrow.class);

    }
    }
    }}


    but i exported it and put it into my plugins and it doesnt work. it loads the plugin and shows it is in the /plugins but when i right click with a dead bush it doesnt fire an arrow
     
  23. Offline

    timtower Administrator Administrator Moderator

    @Shikishima Your class isn't implementing Listener
    Which means that you also didn't register the event in the onEnable.
     
  24. Offline

    Shikishima

    @timtower
    so how do i implement listener and how do i register the event in onEnable
     
  25. Offline

    timtower Administrator Administrator Moderator

  26. Offline

    Shikishima

    @timtower is this right because its not working

    Code:
    import java.util.logging.Logger;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.Listener;
    
    public class Parasyte extends JavaPlugin implements Listener{
    
        public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = Logger.getLogger("Minecraft");
    
            logger.info(pdfFile.getName() + " has been enabled (V. " + pdfFile.getVersion() + ")");
        }   
        @EventHandler(priority=EventPriority.HIGH)
        public void onPlayerUse(PlayerInteractEvent event){
           Player player = event.getPlayer();
         
           if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
               if (player.getInventory().getItemInMainHand().getType() == Material.DEAD_BUSH);{
              player.launchProjectile(Arrow.class);}}
    
    }
        public void onDisable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = Logger.getLogger("Minecraft");
    
            logger.info(pdfFile.getName() + " has been disabled (V. " + pdfFile.getVersion() + ")");
           
        }}
     
  27. Offline

    timtower Administrator Administrator Moderator

    @Shikishima You still don't register the event.
    Don't log your own plugins.
     
  28. Offline

    Shikishima

    @timtower what do i put for the bolded underlined part

    public class Parasyte extends JavaPlugin implements Listener{

    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
    PluginDescriptionFile pdfFile = getDescription();
    Logger logger = Logger.getLogger("Minecraft");

    logger.info(pdfFile.getName() + " has been enabled (V. " + pdfFile.getVersion() + ")");
    }
    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
    Player player = event.getPlayer();

    if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    if (player.getInventory().getItemInMainHand().getType() == Material.DEAD_BUSH);{
    player.launchProjectile(Arrow.class);}}

    }
    public void onDisable() {
    PluginDescriptionFile pdfFile = getDescription();
    Logger logger = Logger.getLogger("Minecraft");

    logger.info(pdfFile.getName() + " has been disabled (V. " + pdfFile.getVersion() + ")");

    }}
     
  29. Offline

    timtower Administrator Administrator Moderator

    @Shikishima You leave it that way.
    Do you have any Java knowledge?
     
  30. Offline

    Shikishima

    @timtower not very much, only very basic stuff but i figured i would try and make my own plugins and learn along the way as i dont think anyone would take my requests.
     
Thread Status:
Not open for further replies.

Share This Page