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!"); }
@noah8282 Are you sure you know Java? Java is not a scripting language, so neither is Bukkit. Method should start with a lower case letter. You are comparing enums using .equals instead of ==. Now, about your problem: Are you sure you are registering the listener? The item in the player's hand can be null. Null check it before getting the type.
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"); } } }
@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.