Multiple events

Discussion in 'Plugin Development' started by MinecraftDorado, Oct 2, 2016.

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

    MinecraftDorado

    As I can make the two events are detected?
    Code:
        @EventHandler
        public void event(PlayerToggleSneakEvent e, PlayerInteractEvent b){
            Player player = e.getPlayer();
            ItemStack item = new ItemStack(Material.NAME_TAG);
            if(e.isSneaking() == true){
                if(b.getAction() == Action.LEFT_CLICK_AIR){
                    if(player.getInventory().getItem(8).equals(item)){
                        player.sendMessage("Item detect");
                    }
                }
            }
        }
     
  2. Offline

    mehboss

  3. Offline

    MinecraftDorado

    @mehboss
    I want you to run the two events
     
  4. Offline

    mehboss

  5. Offline

    MinecraftDorado

    @mehboss
    Code does not work, I can not detect the second event
     
  6. Offline

    mehboss

  7. Offline

    Zombie_Striker

    @MinecraftDorado
    You cannot have two events in one method. You can only have one event per method .Listen to PlayerInteractEvent. If the player is sneaking (Player# isSneaking), do stuff.
     
  8. Offline

    mehboss

    @Zombie_Striker @MinecraftDorado
    Yeah, I did this but console gives an error :)
    Code:
    package me.mehboss.testttt;
    
    import org.bukkit.Bukkit;
    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.plugin.java.JavaPlugin;
    
    public class Test extends JavaPlugin implements Listener {
      
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
    
        }
    
        @EventHandler
        public void event(PlayerInteractEvent e) {
            Player player = e.getPlayer();
            if (e.getAction() == Action.LEFT_CLICK_AIR) {
                    if (player.getInventory().getItem(8).getType().equals(Material.NAME_TAG)) {
                        player.sendMessage("Item detect");
                    }
                }
            }
        }
    
    Caused by: java.lang.NullPointerException
     
  9. Offline

    Zombie_Striker

    @mehboss
    What is the error?
    1. The item can be null
    2. Use == to compare enums values.
     
  10. Offline

    mehboss

    Want exact errors? @Zombie_Striker

    I thought the caused by would be enough :)
     
  11. Offline

    fireboyev

    Are you sure you want it to detect the item in the 8th slot in the hotbar and not the item in hand?

    -EDIT-
    getType returns a null pointer exception if the getItem(8) is null
    try this before you check for the nametag:
    Code:
    if (player.getInventory().getItem(8) != null) {
     
    Last edited: Oct 2, 2016
  12. Offline

    mehboss

    @fireboyev so
    Code:
    if (player.getInventory().getItem(8) != null) {
    player.sendmessage("No name tag detected!");
    }
    if (player.getInventory().getItem(8).getType().equals(Material.NAME_TAG)) {
    player.sendMessage("Name tag detected");
    
     
  13. Offline

    Zombie_Striker

    Nope! We (I) need to know what line is throwing the NPE.

    Yes.
     
  14. Offline

    fireboyev

    @mehboss
    Almost. The code you have there will still not work because you have it that it is detecting if the item is not null then say no name tag detected.
    you want to have it so it is detecting if it IS null, so basically just remove the ! and it should just work fine
     
  15. @fireboyev *replace the ! with an equals to make it == and you should be fine



    @mehboss And has Zombie has already said, use == for the enum comparision not .equals
     
  16. Offline

    fireboyev

  17. Offline

    mehboss

    thanks, this thread was for @MinecraftDorado, I just got curious :)
     
Thread Status:
Not open for further replies.

Share This Page