There are a couple things that could be wrong.
1) Make sure that the class that this is in implements Listener
, and that in your Main
class, (the one that extends JavaPlugin
) in the onEnable()
method, you have:
this.getServer().getPluginManager().registerEvents(new <class that implements Listener>(), this);
so if the class that your code with @EventHandler
is in is called Handler
, then you would use:
this.getServer().getPluginManager().registerEvents(new Handler(), this);
2) Try using .equals()
instead of ==
:
if(e.getAction().equals(Action.RIGHT_CLICK_AIR)){
if(e.getItem().equals(SpellBook)){
3) Make sure that SpellBook
is actually an ItemStack
. If it is, then you may want to try doing this if it has no ItemMeta
(display name, lore, etc.)
if(e.getItem().getType().equals(SpellBook.getType())){
Otherwise, if it does have ItemMeta
, you could use this:
if(e.getItem().getType().equals(SpellBook.getType()) && e.getItem().hasItemMeta()){
if(e.getItem().getItemMeta().getDisplayName().equals(SpellBook.getItemMeta().getDisplayName(){
if(e.getItem().getItemMeta().getLore().equals(SpellBook.getItemMeta().getLore(){
So, your final code should probably look something like this:
@EventHandler
public void onBookInteract(PlayerInteractEvent e){
if(e.getAction().equals(Action.RIGHT_CLICK_AIR)){
if(e.getItem().getType().equals(SpellBook.getType()) && e.getItem().hasItemMeta()){
if(e.getItem().getItemMeta().getDisplayName().equals(SpellBook.getItemMeta().getDisplayName()){
if(e.getItem().getItemMeta().getLore().equals(SpellBook.getItemMeta().getLore()){
openGUI(e.getPlayer());
}
}
}
}
}