GetItemInHand

Discussion in 'Plugin Development' started by noah8282, Mar 31, 2017.

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

    noah8282

    So i know the language Java, but im new to minecraft Scripting.

    Im trying to create a script, that logs a little message to the console everytime a person has a diamond sword in his/hers hand, but the console wont respond with anything.

    This is my code:

    Code:
    @EventHandler
        public void Hand(PlayerItemHeldEvent e){
            Player p = e.getPlayer();
            if (p.getItemInHand().getType().equals(Material.DIAMOND_SWORD)){
                getLogger().info("It Works!");
            }
     
  2. Offline

    Zombie_Striker

    @noah8282
    Are you sure you know Java?
    1. Java is not a scripting language, so neither is Bukkit.
    2. Method should start with a lower case letter.
    3. You are comparing enums using .equals instead of ==.
    Now, about your problem:
    1. Are you sure you are registering the listener?
    2. The item in the player's hand can be null. Null check it before getting the type.
     
  3. Offline

    mine-care

    ... Which is absolutely fine! :p especially in this case.
     
  4. Offline

    noah8282

    Well now my code looks like this, but is still not working. And i'm used to work in a C# environment, wich i suppose is a lot like Java, thus i know the basics of the language.

    Code:
    package me.chutney;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemHeldEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class SilkSpawner extends JavaPlugin implements Listener{
    
        @Override
        public void onEnable() {
            getLogger().info("SilkSpawner er aktiveret!");
        }
       
        @Override
        public void onDisable() {
            getLogger().info("SilkSpawner er deaktiveret!");
        }
       
        @EventHandler
        public void hand(PlayerItemHeldEvent e){
            Player p = e.getPlayer();
            if (p.getItemInHand().getType() == Material.DIAMOND_SWORD && p.getItemInHand().getType() != null) {
                getLogger().info("It Works!");
            } else {
                getLogger().info("null");
            }
     
    }
    }
    
     
  5. Offline

    Zombie_Striker

    @noah8282
    You still need to register the class in the onEnable.

    Also, you may want to review how Java works/ remember how order of operation works. If the itemstack is null, getting the type will throw a null pointer exception, stopping the rest of the method from being read. You have to null check the itemstack instanace, not its type. Not only that, but if the type is null (which it can't be) then checking if it is null after checking if it is a diamond sword is useless.

    BTW: Bukkit logs your plugins for you. You do not need those loggers.
     
Thread Status:
Not open for further replies.

Share This Page