How to create a listener?

Hello, I’m trying to create a plugin for the first time and I’m trying to make a listener but it dosen’t trigger
Code:
package com.icube56.cityx;

import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class OpenStaffDoors implements Listener {

@EventHandler
public void PlayerInteractEvent(@NotNull Player who, @NotNull Action action, @Nullable ItemStack item, @Nullable Block clickedBlock, @NotNull BlockFace clickedFace, @Nullable EquipmentSlot hand) {
    Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), "say Event Works");
    if (action.isRightClick()) {
        if (clickedBlock.toString().contains("Obsidian")) {
            if (who.hasPermission("CityX.UseDoors")) {

} else {
who.sendMessage(“No permission”);
}
}
}
}

spigot has a good tutorial on this i suggedt you follow since you’re doing it completely wrong

https://www.spigotmc.org/wiki/using-the-event-api/

Did not work, I tested it on the class and in another class

if it doesn’t work, you’re doing it wrong.

make sure you register your event in onEnable() in your main class, your listener class implements Listener from org.bukkit.event and that you have a method definition like public void openStaffDoor(PlayerInteractEvent event) {}

because i can assure you, this works

In the main class:
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new OpenStaffDoors(), this);
The OpenStaffDoors Class:
package com.icube56.cityx;

import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class OpenStaffDoors implements Listener {

@EventHandler
public void PlayerInteractEvent(PlayerInteractEvent event, @NotNull Player who, @NotNull Action action, @Nullable ItemStack item, @Nullable Block clickedBlock, @NotNull BlockFace clickedFace, @Nullable EquipmentSlot hand) {
    Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), "say Event Works");
    if (action.isRightClick()) {
        if (clickedBlock.toString().contains("Obsidian")) {
            if (who.hasPermission("CityX.UseDoors")) {
                Location clickedBlockLoc = clickedBlock.getLocation();
                double CLX = clickedBlockLoc.getX();
                double CLZ = clickedBlockLoc.getZ();
                Location PlayerLoc = who.getLocation();
                double PLX = PlayerLoc.getX();
                double PLZ = PlayerLoc.getZ();
                double PLY = PlayerLoc.getY();
                String air = "Air";
                World worldWorld = PlayerLoc.getWorld();
                String world = "world";

                if (CLX == -111 && CLZ == -46) {
                    if (CLX == -111 && CLZ == -46) {


                        if (worldWorld.toString() == world) {
                            Location teleportLoc = null;
                            teleportLoc.set(PLX - 2, PLY + 0, PLZ - 2);
                            if (teleportLoc.getBlock().toString() == air) {
                                who.teleport(teleportLoc);
                            } else {
                                who.sendMessage("Error: Teleport Pad at coordinates " + CLX + " " + CLZ + " Encountered an error: No Air block at selected Coordinates");
                            }

                        }
                    } else {
                        who.sendMessage("The block you right-clicked was not a Teleport Pad");
                    }
                }
            } else {
                who.sendMessage("No permission");
            }
        }
    }
}

}

i dont believe that’s how you’re supposed to register an event

Edit: nvm

this: getServer().getPluginManager().registerEvents(new MyListener(), this);
is the same as this: PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new MyListener(), this);

I also created a class just like the tutorial one, and it dosen’t work

try pulling stuff from the event object itself instead e.g: event.getPlayer()

now it’s just referring to an object that isnt assigned to any value

The event dosen’t trigger because if you look at the start there is a “Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), “say Event Works”);”

nvm Now it works, it’s only the playerinteractevent that does not work

  1. “Doesn’t work” gives nobody anything to go off
  2. provide the latest code
  3. If your event handler still has more than 1 param, that’s wrong and ofc won’t work, see the event API documentation on the bukkit wiki

It’s the fifth reply

No error message, simply does nothing

if that’s your latest code, that won’t work, and I already explained why

ik i’m fixing it now

The event did not trigger

Latest code:
package com.icube56.cityx;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;

public class OpenDoors implements Listener {

@EventHandler (priority = EventPriority.HIGHEST)
public void PlayerInteractEvent(PlayerInteractEvent e) {
    Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), "say Event Works");
    Player who = e.getPlayer();
    Action action = e.getAction();
    Block clickedBlock = e.getClickedBlock();
    if (action.isRightClick()) {
        if (clickedBlock.toString().contains("Obsidian")) {
            if (who.hasPermission("CityX.UseDoors")) {
                Location clickedBlockLoc = clickedBlock.getLocation();
                double CLX = clickedBlockLoc.getX();
                double CLZ = clickedBlockLoc.getZ();
                Location PlayerLoc = who.getLocation();
                double PLX = PlayerLoc.getX();
                double PLZ = PlayerLoc.getZ();
                double PLY = PlayerLoc.getY();
                String air = "Air";
                World worldWorld = PlayerLoc.getWorld();
                String world = "world";

                if (CLX == -111 && CLZ == -46) {
                    if (CLX == -111 && CLZ == -46) {


                        if (worldWorld.toString() == world) {
                            Location teleportLoc = null;
                            teleportLoc.set(CLX + 1, PLY + 0, CLZ + 1);
                            if (teleportLoc.getBlock().toString() == air) {
                                who.teleport(teleportLoc);
                            } else {
                                teleportLoc.set(CLX - 1, PLY + 0, CLZ - 1);
                                if (teleportLoc.getBlock().toString() == air) {
                                    who.teleport(teleportLoc);
                                } else {
                                    who.sendMessage("Error: Teleport Pad at coordinates " + CLX + " " + CLZ + " Encountered an error: No Air block at selected Coordinates");
                                }

                            }

                        }
                    } else {
                        who.sendMessage("The block you right-clicked was not a Teleport Pad");
                    }
                }
            } else {
                who.sendMessage("No permission");
            }
        }
    }
}

}