1.15.2 World.Environment and TeleportCause

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

  1. I'm sure I'm making a simple mistake, but can't find it. That, or what I'm trying to do isn't the way to do it. I'm trying to send a message when player (p) is teleported via a cause (c) into an environment within a world (w). In simpler terms, for example, when someone enters a nether portal it should say either "Player entered the Nether," or "Player entered the Overworld." In my code, the nether messages work perfectly. However, when it comes to the end, you'd think it's going to be pretty much the same. That's not the case, unfortunately. Entering the end works fine, but exiting the end results in no message being sent.

    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)) {
                    Bukkit.broadcastMessage(Utils.chat(plugin.getConfig().getString("enter_normal").replace("<player>", p.getDisplayName().replace("&", "§"))));
                }else {
                    Bukkit.broadcastMessage(Utils.chat(plugin.getConfig().getString("enter_nether").replace("<player>", p.getDisplayName().replace("&", "§"))));
                }
            }
         
            if (c.equals(PlayerTeleportEvent.TeleportCause.END_PORTAL)) {
                if (w.getEnvironment().equals(Environment.THE_END)) {
                    Bukkit.broadcastMessage(Utils.chat(plugin.getConfig().getString("enter_normal").replace("<player>", p.getDisplayName().replace("&", "§"))));
                }else {
                    Bukkit.broadcastMessage(Utils.chat(plugin.getConfig().getString("enter_end").replace("<player>", p.getDisplayName().replace("&", "§"))));
                }
            }
             
        }
    If someone could help me pinpoint my mistake, that would be quite helpful.
     
    #1 Swordee, Feb 2, 2020
    Last edited: Feb 2, 2020
  2. Use PlayerChangedWorldEvent
    And check the new world.
    That's all.
     
    • Agree Agree x 1
  3. Code (Java):
    public void onWorldChange(PlayerChangedWorldEvent e) {
            Player p = e.getPlayer();
            World w = p.getWorld();
           
            if (w.getEnvironment().equals(Environment.THE_END)) {
                Bukkit.broadcastMessage(Utils.chat(plugin.getConfig().getString("enter_normal").replace("<player>", p.getDisplayName().replace("&", "§"))));
            }else {
                Bukkit.broadcastMessage(Utils.chat(plugin.getConfig().getString("enter_normal").replace("<player>", p.getDisplayName().replace("&", "§"))));
            }
        }
    Just did what you said, still doesn't work. It appears that leaving the end isn't working as intended, possibly. Any other ideas?
     
  4. You are sending the same message twice...
     
  5. I'm aware, I'm just trying to get it to spit out anything at this point when leaving the end. Need this to work before I can put the finishing touches.
     
  6. Well, the code works for me as expected... Did you forget to register your event? Did you forget to add the EventHandler annotation?

    This works just as it should...
    Code (Java):
    @EventHandler
    public void onWorldChange(@NotNull PlayerChangedWorldEvent event) {
            System.out.println(event.getFrom());
        System.out.println(event.getPlayer().getWorld());
    }
     
  7. Oh ffs, forgot the @EventHandler. Thanks, man.