11package world .bentobox .islandfly .listeners ;
22
3+ import java .util .HashMap ;
4+ import java .util .Map ;
5+
36import org .bukkit .Bukkit ;
47import org .bukkit .GameMode ;
58import org .bukkit .entity .Player ;
69import org .bukkit .event .EventHandler ;
710import org .bukkit .event .EventPriority ;
811import org .bukkit .event .Listener ;
912import org .bukkit .event .player .PlayerToggleFlightEvent ;
13+ import org .eclipse .jdt .annotation .NonNull ;
14+
1015import world .bentobox .bentobox .api .events .island .IslandEnterEvent ;
1116import world .bentobox .bentobox .api .events .island .IslandExitEvent ;
1217import world .bentobox .bentobox .api .localization .TextVariables ;
18+ import world .bentobox .bentobox .api .metadata .MetaDataValue ;
1319import world .bentobox .bentobox .api .user .User ;
1420import world .bentobox .bentobox .database .objects .Island ;
1521import world .bentobox .islandfly .IslandFlyAddon ;
1925 */
2026public class FlyListener implements Listener {
2127
28+ private static final @ NonNull String ISLANDFLY = "IslandFly-" ;
2229 /**
2330 * Addon instance object.
2431 */
25- private final IslandFlyAddon islandFlyAddon ;
32+ private final IslandFlyAddon addon ;
2633
2734
2835 /**
2936 * Default constructor.
3037 * @param islandFlyAddon instance of IslandFlyAddon
3138 */
3239 public FlyListener (final IslandFlyAddon islandFlyAddon ) {
33- this .islandFlyAddon = islandFlyAddon ;
40+ this .addon = islandFlyAddon ;
3441 }
3542
3643 @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
3744 public void onToggleFlight (final PlayerToggleFlightEvent event ) {
3845 final User user = User .getInstance (event .getPlayer ());
3946 if (checkUser (user )) {
4047 user .sendMessage ("islandfly.not-allowed" );
48+ } else {
49+ addon .getIslands ().getIslandAt (user .getLocation ())
50+ .filter (i -> i .getMemberSet ().contains (user .getUniqueId ())).ifPresent (is -> {
51+ Map <String , MetaDataValue > metaData = new HashMap <>();
52+ metaData .put ("IslandFly-" + is .getUniqueId (), new MetaDataValue (event .isFlying ()));
53+ user .setMetaData (metaData ); // Record the fly state for this island
54+ });
55+
4156 }
4257 }
4358
@@ -46,7 +61,7 @@ public void onToggleFlight(final PlayerToggleFlightEvent event) {
4661 * @return true if fly was blocked
4762 */
4863 private boolean checkUser (User user ) {
49- String permPrefix = islandFlyAddon .getPlugin ().getIWM ().getPermissionPrefix (user .getWorld ());
64+ String permPrefix = addon .getPlugin ().getIWM ().getPermissionPrefix (user .getWorld ());
5065 // Ignore ops
5166 if (user .isOp () || user .getPlayer ().getGameMode ().equals (GameMode .CREATIVE )
5267 || user .getPlayer ().getGameMode ().equals (GameMode .SPECTATOR )
@@ -57,8 +72,13 @@ private boolean checkUser(User user) {
5772 @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
5873 public void onEnterIsland (final IslandEnterEvent event ) {
5974 final User user = User .getInstance (event .getPlayerUUID ());
75+ user .getMetaData (ISLANDFLY + event .getIsland ().getUniqueId ())
76+ .ifPresent (mdv -> {
77+ user .getPlayer ().setAllowFlight (true );
78+ user .getPlayer ().setFlying (mdv .asBoolean ());
79+ });
6080 // Wait until after arriving at the island
61- Bukkit .getScheduler ().runTask (this .islandFlyAddon .getPlugin (), () -> checkUser (user ));
81+ Bukkit .getScheduler ().runTask (this .addon .getPlugin (), () -> checkUser (user ));
6282 }
6383
6484 /**
@@ -67,17 +87,16 @@ public void onEnterIsland(final IslandEnterEvent event) {
6787 */
6888 @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
6989 public void onExitIsland (final IslandExitEvent event ) {
70-
7190 final User user = User .getInstance (event .getPlayerUUID ());
72- String permPrefix = islandFlyAddon .getPlugin ().getIWM ().getPermissionPrefix (user .getWorld ());
91+ String permPrefix = addon .getPlugin ().getIWM ().getPermissionPrefix (user .getWorld ());
7392 // Ignore ops
7493 if (user .isOp () || user .getPlayer ().getGameMode ().equals (GameMode .CREATIVE )
7594 || user .getPlayer ().getGameMode ().equals (GameMode .SPECTATOR )
7695 || user .hasPermission (permPrefix + "island.flybypass" )
7796 || (!user .hasPermission (permPrefix + "island.fly" )
7897 && !user .hasPermission (permPrefix + "island.flyspawn" ))) return ;
7998 // Alert player fly will be disabled
80- final int flyTimeout = this .islandFlyAddon .getSettings ().getFlyTimeout ();
99+ final int flyTimeout = this .addon .getSettings ().getFlyTimeout ();
81100
82101 // If timeout is 0 or less disable fly immediately
83102 if (flyTimeout <= 0 ) {
@@ -90,7 +109,7 @@ public void onExitIsland(final IslandExitEvent event) {
90109 user .sendMessage ("islandfly.fly-outside-alert" , TextVariables .NUMBER , String .valueOf (flyTimeout ));
91110 }
92111
93- Bukkit .getScheduler ().runTaskLater (this .islandFlyAddon .getPlugin (), () -> removeFly (user ), 20L * flyTimeout );
112+ Bukkit .getScheduler ().runTaskLater (this .addon .getPlugin (), () -> removeFly (user ), 20L * flyTimeout );
94113 }
95114
96115
@@ -103,7 +122,7 @@ boolean removeFly(User user) {
103122 // Verify player is still online
104123 if (!user .isOnline ()) return false ;
105124
106- Island island = islandFlyAddon .getIslands ().getProtectedIslandAt (user .getLocation ()).orElse (null );
125+ Island island = addon .getIslands ().getProtectedIslandAt (user .getLocation ()).orElse (null );
107126
108127 if (island == null ) {
109128 disableFly (user );
@@ -112,7 +131,7 @@ boolean removeFly(User user) {
112131
113132 // Check if player is back on a spawn island
114133 if (island .isSpawn ()) {
115- if (this .islandFlyAddon .getPlugin ().getIWM ().getAddon (user .getWorld ())
134+ if (this .addon .getPlugin ().getIWM ().getAddon (user .getWorld ())
116135 .map (a -> !user .hasPermission (a .getPermissionPrefix () + "island.flyspawn" )).orElse (false )) {
117136
118137 disableFly (user );
@@ -121,8 +140,9 @@ boolean removeFly(User user) {
121140 return false ;
122141 }
123142
124- if (islandFlyAddon .getSettings ().getFlyMinLevel () > 1 && islandFlyAddon .getLevelAddon () != null ) {
125- if (islandFlyAddon .getLevelAddon ().getIslandLevel (island .getWorld (), island .getOwner ()) < islandFlyAddon .getSettings ().getFlyMinLevel ()) {
143+ if (addon .getSettings ().getFlyMinLevel () > 1 && addon .getLevelAddon () != null ) {
144+ if (addon .getLevelAddon ().getIslandLevel (island .getWorld (), island .getOwner ()) < addon .getSettings ()
145+ .getFlyMinLevel ()) {
126146 disableFly (user );
127147 return false ;
128148 }
0 commit comments