@@ -788,6 +788,23 @@ public void onPlayerMove(PlayerMoveEvent event) {
788788 handleCellChange (event .getPlayer (), event .getFrom (), event .getTo (), event );
789789 }
790790
791+ //TODO: Remove when we support 1.21.5 and upwards.
792+ @ SuppressWarnings ("removal" )
793+ private TeleportCause chorusFruitTeleport = MinecraftVersion .CURRENT_VERSION .isOlderThan (MinecraftVersion .MINECRAFT_1_21_5 ) ? TeleportCause .CHORUS_FRUIT : TeleportCause .CONSUMABLE_EFFECT ;
794+
795+ @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
796+ public void onPlayerTeleport (PlayerTeleportEvent event ) {
797+ Player player = event .getPlayer ();
798+ handleTeleportCellChange (player , event .getCause (), event .getFrom (), event .getTo (), event );
799+ }
800+
801+ /**
802+ * Calls the proper events to handle a player changing townblocks.
803+ * @param player The player who is moving.
804+ * @param from The location the player is moving from.
805+ * @param to The location the player is moving to.
806+ * @param event The cancellable event that triggered this cell change. This will usually be a PlayerMoveEvent, but it could be a PlayerTeleportEvent if the teleport caused a cell change.
807+ */
791808 public void handleCellChange (Player player , Location from , Location to , Cancellable event ) {
792809 /*
793810 * Abort if we haven't really moved, or if the event.getTo() is null (which is allowed...)
@@ -819,62 +836,66 @@ public void handleCellChange(Player player, Location from, Location to, Cancella
819836 }
820837 }
821838
822- //TODO: Remove when we support 1.21.5 and upwards.
823- @ SuppressWarnings ("removal" )
824- private TeleportCause chorusFruitTeleport = MinecraftVersion .CURRENT_VERSION .isOlderThan (MinecraftVersion .MINECRAFT_1_21_5 ) ? TeleportCause .CHORUS_FRUIT : TeleportCause .CONSUMABLE_EFFECT ;
825-
826- @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
827- public void onPlayerTeleport (PlayerTeleportEvent event ) {
839+ /**
840+ * Handles a player moving from one TownBlock to another, including firing the PlayerChangePlotEvent and handling plot notifications.
841+ * This should only be called from teleportation events.
842+ * @param player The player who is moving.
843+ * @param cause The teleport cause.
844+ * @param from The location the player is moving from.
845+ * @param to The location the player is moving to.
846+ * @param event The cancellable event that triggered this cell change. This will usually be a PlayerMoveEvent, but it could be a PlayerTeleportEvent if the teleport caused a cell change.
847+ */
848+ public void handleTeleportCellChange (Player player , TeleportCause cause , Location from , Location to , Cancellable event ) {
828849 // Let's ignore Citizens NPCs. This must come before the safemode check, as Citizens stores their NPCs
829850 // at the world spawn until a player loads a chunk, to which the NPC is then teleported. Towny would
830851 // prevent them teleporting, leaving them at spawn even after Safe Mode is cleaned up.
831- if (PluginIntegrations .getInstance ().isNPC (event . getPlayer () ))
852+ if (PluginIntegrations .getInstance ().isNPC (player ))
832853 return ;
833854
834855 if (plugin .isError ()) {
835856 event .setCancelled (true );
836857 return ;
837858 }
838859
839- Player player = event . getPlayer ();
860+
840861 Resident resident = TownyUniverse .getInstance ().getResident (player .getUniqueId ());
841862 if (resident == null )
842863 return ;
843864
844865 boolean isAdmin = !Towny .getPlugin ().hasPlayerMode (player , "adminbypass" ) && (resident .isAdmin () || resident .hasPermissionNode (PermissionNodes .TOWNY_ADMIN_OUTLAW_TELEPORT_BYPASS .getNode ()));
845866 if (isAdmin ) {
846867 // Admins don't get restricted further but they do need to fire the PlayerChangePlotEvent.
847- onPlayerMove ( event );
868+ handleCellChange ( player , from , to , event );
848869 return ;
849870 }
850871
851872 // Cancel teleport if Jailed by Towny.
852873 if (resident .isJailed ()) {
853- if (event . getCause () == TeleportCause .COMMAND ) {
854- TownyMessaging .sendErrorMsg (event . getPlayer () , Translatable .of ("msg_err_jailed_players_no_teleport" ));
874+ if (cause == TeleportCause .COMMAND ) {
875+ TownyMessaging .sendErrorMsg (player , Translatable .of ("msg_err_jailed_players_no_teleport" ));
855876 event .setCancelled (true );
856877 return ;
857878 }
858- if (!TownySettings .JailAllowsTeleportItems () && (event . getCause () == TeleportCause .ENDER_PEARL || event . getCause () == chorusFruitTeleport )) {
859- TownyMessaging .sendErrorMsg (event . getPlayer () , Translatable .of ("msg_err_jailed_players_no_teleport" ));
879+ if (!TownySettings .JailAllowsTeleportItems () && (cause == TeleportCause .ENDER_PEARL || cause == chorusFruitTeleport )) {
880+ TownyMessaging .sendErrorMsg (player , Translatable .of ("msg_err_jailed_players_no_teleport" ));
860881 event .setCancelled (true );
861882 return ;
862883 }
863884 }
864885
865886 // Cancel teleport if resident is outlawed in Town.
866887 if (!TownySettings .canOutlawsTeleportOutOfTowns ()) {
867- TownBlock tb = TownyAPI .getInstance ().getTownBlock (event . getFrom () );
888+ TownBlock tb = TownyAPI .getInstance ().getTownBlock (from );
868889 if (tb != null && tb .hasTown ()) {
869890 Town town = tb .getTownOrNull ();
870891 if (town != null && town .hasOutlaw (resident )) {
871- if (event . getCause () == TeleportCause .COMMAND ) {
872- TownyMessaging .sendErrorMsg (event . getPlayer () , Translatable .of ("msg_err_outlawed_players_no_teleport" ));
892+ if (cause == TeleportCause .COMMAND ) {
893+ TownyMessaging .sendErrorMsg (player , Translatable .of ("msg_err_outlawed_players_no_teleport" ));
873894 event .setCancelled (true );
874895 return ;
875896 }
876- if (!TownySettings .canOutlawsUseTeleportItems () && (event . getCause () == TeleportCause .ENDER_PEARL || event . getCause () == chorusFruitTeleport )) {
877- TownyMessaging .sendErrorMsg (event . getPlayer () , Translatable .of ("msg_err_outlawed_players_no_teleport" ));
897+ if (!TownySettings .canOutlawsUseTeleportItems () && (cause == TeleportCause .ENDER_PEARL || cause == chorusFruitTeleport )) {
898+ TownyMessaging .sendErrorMsg (player , Translatable .of ("msg_err_outlawed_players_no_teleport" ));
878899 event .setCancelled (true );
879900 return ;
880901 }
@@ -883,18 +904,18 @@ public void onPlayerTeleport(PlayerTeleportEvent event) {
883904 }
884905
885906 // Test to see if CHORUS_FRUIT is in the item_use list.
886- if (event . getCause () == chorusFruitTeleport && TownySettings .isItemUseMaterial (Material .CHORUS_FRUIT , event . getTo () )) {
907+ if (cause == chorusFruitTeleport && TownySettings .isItemUseMaterial (Material .CHORUS_FRUIT , to )) {
887908 //Make decision on whether this is allowed using the PlayerCache and then a cancellable event.
888- if (!TownyActionEventExecutor .canItemuse (event . getPlayer (), event . getTo () , Material .CHORUS_FRUIT )) {
909+ if (!TownyActionEventExecutor .canItemuse (player , to , Material .CHORUS_FRUIT )) {
889910 event .setCancelled (true );
890911 return ;
891912 }
892913 }
893914
894915 // Test to see if Ender pearls is in the item_use list.
895- if (event . getCause () == TeleportCause .ENDER_PEARL && TownySettings .isItemUseMaterial (Material .ENDER_PEARL , event . getTo () )) {
916+ if (cause == TeleportCause .ENDER_PEARL && TownySettings .isItemUseMaterial (Material .ENDER_PEARL , to )) {
896917 //Make decision on whether this is allowed using the PlayerCache and then a cancellable event.
897- if (!TownyActionEventExecutor .canItemuse (event . getPlayer (), event . getTo () , Material .ENDER_PEARL )) {
918+ if (!TownyActionEventExecutor .canItemuse (player , to , Material .ENDER_PEARL )) {
898919 event .setCancelled (true );
899920 return ;
900921 }
@@ -905,7 +926,7 @@ public void onPlayerTeleport(PlayerTeleportEvent event) {
905926 resident .removeRespawnProtection ();
906927
907928 // Send the event to the onPlayerMove so Towny can fire the PlayerChangePlotEvent.
908- onPlayerMove ( event );
929+ handleCellChange ( player , from , to , event );
909930 }
910931
911932 @ EventHandler (priority = EventPriority .LOWEST )
0 commit comments