Solved PlayerTeleportEvent Causes Help

Discussion in 'Spigot Plugin Development' started by Swordee, Feb 2, 2020.

Thread Status:
Not open for further replies.
  1. Hey, so I'm trying to make a message that spits out when a player enters the nether, end, and overworld. However, due to this being my first day making plugins, even this is difficult for me. Currently, I'm trying to use a PlayerTeleportEvent and then checking the cause for it, but I have no idea how to check the cause.

    Code (Java):
    @EventHandler
        public void onPlayerTeleport(PlayerTeleportEvent e) {
            if (event.getCause() == TeleportCause.NETHER_PORTAL) {
                return;
           
            }

        }
    That is what I was able to come up with after researching for over an hour—pathetic, I know—but the event.getCause() isn't being recognized. If someone could help me out with determining causes, that would be very helpful!
     
    #1 Swordee, Feb 2, 2020
    Last edited: Feb 2, 2020
  2. https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/player/PlayerChangedWorldEvent.html
    If you get the player location you will be able to get his new world. You can also get his old world if you want using event.getFrom() :)

    Also, did you properly register your event? Is your plugin active on the server?
     
  3. I believe I registered the event correctly, and no it's not active on my server currently. I got some help and was able to get this:
    Code (Java):
    public class EventsClass implements Listener{

        @EventHandler
        public void onPlayerTeleport(PlayerTeleportEvent e) {
            if (event.getCause().equals(PlayerTeleportEvent.TeleportCause.NETHER_PORTAL)) {
                event.getPlayer().sendMessage("Nether portal");
            }
               
        }
    }
    But, the event.getCause/Player() is what's causing errors. I'm not sure what I'm doing wrong in relation to that.
     
  4. Update: I've updated it even more and now nothing is happening. It's not very much code to look though, so I'll post in code form the two files I have.

    EventsClass.java
    Code (Java):
    package me.swordee.esdimensionenter;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerTeleportEvent;
    import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

    public class EventsClass implements Listener{

        @EventHandler
        public void onPlayerTeleport(PlayerTeleportEvent e) {
            Player p = e.getPlayer();
            TeleportCause c = e.getCause();
            if (c.equals(PlayerTeleportEvent.TeleportCause.NETHER_PORTAL)) {
                p.sendMessage("Nether portal");
            }
             
        }
    }
     
    Main.java
    Code (Java):
    package me.swordee.esdimensionenter;

    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {

        @Override
        public void onEnable() {
            saveDefaultConfig();
            getServer().getPluginManager().registerEvents(new EventsClass(), this);
        }
    }
     
    If there's anything wrong with this, please lemme know!
     
  5. This is simple to debug. just Bukkit.broadcastMessage(c.getCause().name()) and see what it outputs when you go through a nether portal.

    If the event is running period, you likely have an error outside the specification of your plugin. Check console for errors.
     
  6. Was able to figure it out, thanks guys! If anyone needs it—I doubt it— here's what works:
    Code (Java):
    public void onPlayerTeleport(PlayerTeleportEvent e) {
            Player p = e.getPlayer();
            World w = p.getWorld();
            TeleportCause c = e.getCause();
           
            if (c.equals(PlayerTeleportEvent.TeleportCause.NETHER_PORTAL)) {
                if (w.getEnvironment().equals(Environment.NETHER)) {
     
Thread Status:
Not open for further replies.