Portal event only fires one time

Discussion in 'Spigot Plugin Development' started by mlgcraftnetwork, May 27, 2016.

  1. I want to make it so when a player walks inside a netherportal (custom build portal for the hub) it opens the travel gui and they are pushed back. I have this code (down below) and the pushing works but when I try to walk in the portal a second time nothing happens. I have not tried to fix the gui not opening yet but that should probably be fixed by adding a delay to it. So the problem is the portal event only seems to fire the first time a player walks inside a portal, does anyone know how to make it fire every time? Thanks

    Code:
    Code (Text):
        @EventHandler
        public void onPlayerPortal(PlayerPortalEvent e) {
        if (e.getCause() == TeleportCause.NETHER_PORTAL) {
        Player p = e.getPlayer();
        e.setCancelled(true);
        p.setVelocity(p.getLocation().getDirection().multiply(-2));
        Main.guimanager.openTravel(p);
        }
        }
     
  2. Choco

    Moderator

    1. Your spacing is horrific, how do you program like that xD Ctrl + A, Ctrl + I
    2. Don't abuse static (Main.guimanager). Create a getter and pass an instance of your main class to your listener class.
    3. Try to avoid nested if statements if you're able to
    Code (Java):
    if (e.getCause != TeleportCause.NETHER_PORTAL) return;
    // Continue the rest of your code
    There's no reason as to why that shouldn't fire more than once. If it's firing the first time, it will fire the second time I'm sure
     
  3. First off I didn't knew about ctrl + i, ITS AWESOME xD. I use this (in my main class) to access classes and it seems to work very good:
    Code (Text):
    static Menu guimanager = new Menu();
    I changed my code to this:
    Code (Text):
        @EventHandler
        public void onPlayerPortal(PlayerPortalEvent e) {
            if (e.getCause() != TeleportCause.NETHER_PORTAL) return;
            final Player p = e.getPlayer();
            e.setCancelled(true);
            p.setVelocity(p.getLocation().getDirection().multiply(-2));
            Bukkit.getScheduler().scheduleSyncDelayedTask(Main.plugin, new Runnable() {
                public void run() {
                    Main.guimanager.openTravel(p);
                }
            }
            , 10L);
        }
    and everything works BUT, it still only works the first time :confused:
     
  4. I just did some tests and the event works every time because the e.setCancelled works every time, but for some reason it only executes my code once. (I also used ctrl + I for all my classes xD)
     
    • Funny Funny x 1