javapluginsminecraftbukkit

Bukkit Plugin - End-Portal Teleport Problem


I'm trying to code my own plugins for my server but i came across a problem. I tried that a player enters end portal in a specific world, event would be cancelled and performs another command but event is not working at all.

part of event class

private static Main main;
    
public void SpawnPortal(Main main) {
    this.main = main;
}


public void onPortalEnter(PlayerTeleportEvent event) {
    
    Player p = event.getPlayer();

    if(event.getCause() == PlayerPortalEvent.TeleportCause.END_PORTAL) {
        event.setCancelled(true);
        p.performCommand("is home");
        p.sendMessage("okay");
    
    }

Main class plugin manager

PluginManager pm = Bukkit.getPluginManager();
        
SpawnPortal sp = new SpawnPortal();
        
pm.registerEvents(sp, this);

Solution

  • You should read over the Spigot Wiki, specifically Using the Event API. Your listener will not work because the method isn't annotated by @EventHandler and the class doesn't implement Listener.

    Your code should look something like this:

    import org.bukkit.event.Listener;
    
    public class MyPortalListener implements Listener {
        public void onPortalEnter(PlayerTeleportEvent event) {
            Player p = event.getPlayer();
            if(event.getCause() == PlayerPortalEvent.TeleportCause.END_PORTAL) {
                event.setCancelled(true);
                p.performCommand("is home");
                p.sendMessage("okay");
            }
        }
    }
    

    And your main class:

    public class MyMain extends JavaPlugin {
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(new MyPortalListener(), this);
        }
    }
    

    You can create the listener from within the main class if you wish. You will need to make the main class implement Listener. The @EventHandler annotation is always required for listeners.