1
1
package world .bentobox .islandfly .listeners ;
2
2
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
+
3
6
import org .bukkit .Bukkit ;
4
7
import org .bukkit .GameMode ;
5
8
import org .bukkit .entity .Player ;
6
9
import org .bukkit .event .EventHandler ;
7
10
import org .bukkit .event .EventPriority ;
8
11
import org .bukkit .event .Listener ;
9
12
import org .bukkit .event .player .PlayerToggleFlightEvent ;
13
+ import org .eclipse .jdt .annotation .NonNull ;
14
+
10
15
import world .bentobox .bentobox .api .events .island .IslandEnterEvent ;
11
16
import world .bentobox .bentobox .api .events .island .IslandExitEvent ;
12
17
import world .bentobox .bentobox .api .localization .TextVariables ;
18
+ import world .bentobox .bentobox .api .metadata .MetaDataValue ;
13
19
import world .bentobox .bentobox .api .user .User ;
14
20
import world .bentobox .bentobox .database .objects .Island ;
15
21
import world .bentobox .islandfly .IslandFlyAddon ;
19
25
*/
20
26
public class FlyListener implements Listener {
21
27
28
+ private static final @ NonNull String ISLANDFLY = "IslandFly-" ;
22
29
/**
23
30
* Addon instance object.
24
31
*/
25
- private final IslandFlyAddon islandFlyAddon ;
32
+ private final IslandFlyAddon addon ;
26
33
27
34
28
35
/**
29
36
* Default constructor.
30
37
* @param islandFlyAddon instance of IslandFlyAddon
31
38
*/
32
39
public FlyListener (final IslandFlyAddon islandFlyAddon ) {
33
- this .islandFlyAddon = islandFlyAddon ;
40
+ this .addon = islandFlyAddon ;
34
41
}
35
42
36
43
@ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
37
44
public void onToggleFlight (final PlayerToggleFlightEvent event ) {
38
45
final User user = User .getInstance (event .getPlayer ());
39
46
if (checkUser (user )) {
40
47
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
+
41
56
}
42
57
}
43
58
@@ -46,7 +61,7 @@ public void onToggleFlight(final PlayerToggleFlightEvent event) {
46
61
* @return true if fly was blocked
47
62
*/
48
63
private boolean checkUser (User user ) {
49
- String permPrefix = islandFlyAddon .getPlugin ().getIWM ().getPermissionPrefix (user .getWorld ());
64
+ String permPrefix = addon .getPlugin ().getIWM ().getPermissionPrefix (user .getWorld ());
50
65
// Ignore ops
51
66
if (user .isOp () || user .getPlayer ().getGameMode ().equals (GameMode .CREATIVE )
52
67
|| user .getPlayer ().getGameMode ().equals (GameMode .SPECTATOR )
@@ -57,8 +72,13 @@ private boolean checkUser(User user) {
57
72
@ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
58
73
public void onEnterIsland (final IslandEnterEvent event ) {
59
74
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
+ });
60
80
// 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 ));
62
82
}
63
83
64
84
/**
@@ -67,17 +87,16 @@ public void onEnterIsland(final IslandEnterEvent event) {
67
87
*/
68
88
@ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
69
89
public void onExitIsland (final IslandExitEvent event ) {
70
-
71
90
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 ());
73
92
// Ignore ops
74
93
if (user .isOp () || user .getPlayer ().getGameMode ().equals (GameMode .CREATIVE )
75
94
|| user .getPlayer ().getGameMode ().equals (GameMode .SPECTATOR )
76
95
|| user .hasPermission (permPrefix + "island.flybypass" )
77
96
|| (!user .hasPermission (permPrefix + "island.fly" )
78
97
&& !user .hasPermission (permPrefix + "island.flyspawn" ))) return ;
79
98
// Alert player fly will be disabled
80
- final int flyTimeout = this .islandFlyAddon .getSettings ().getFlyTimeout ();
99
+ final int flyTimeout = this .addon .getSettings ().getFlyTimeout ();
81
100
82
101
// If timeout is 0 or less disable fly immediately
83
102
if (flyTimeout <= 0 ) {
@@ -90,7 +109,7 @@ public void onExitIsland(final IslandExitEvent event) {
90
109
user .sendMessage ("islandfly.fly-outside-alert" , TextVariables .NUMBER , String .valueOf (flyTimeout ));
91
110
}
92
111
93
- Bukkit .getScheduler ().runTaskLater (this .islandFlyAddon .getPlugin (), () -> removeFly (user ), 20L * flyTimeout );
112
+ Bukkit .getScheduler ().runTaskLater (this .addon .getPlugin (), () -> removeFly (user ), 20L * flyTimeout );
94
113
}
95
114
96
115
@@ -103,7 +122,7 @@ boolean removeFly(User user) {
103
122
// Verify player is still online
104
123
if (!user .isOnline ()) return false ;
105
124
106
- Island island = islandFlyAddon .getIslands ().getProtectedIslandAt (user .getLocation ()).orElse (null );
125
+ Island island = addon .getIslands ().getProtectedIslandAt (user .getLocation ()).orElse (null );
107
126
108
127
if (island == null ) {
109
128
disableFly (user );
@@ -112,7 +131,7 @@ boolean removeFly(User user) {
112
131
113
132
// Check if player is back on a spawn island
114
133
if (island .isSpawn ()) {
115
- if (this .islandFlyAddon .getPlugin ().getIWM ().getAddon (user .getWorld ())
134
+ if (this .addon .getPlugin ().getIWM ().getAddon (user .getWorld ())
116
135
.map (a -> !user .hasPermission (a .getPermissionPrefix () + "island.flyspawn" )).orElse (false )) {
117
136
118
137
disableFly (user );
@@ -121,8 +140,9 @@ boolean removeFly(User user) {
121
140
return false ;
122
141
}
123
142
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 ()) {
126
146
disableFly (user );
127
147
return false ;
128
148
}
0 commit comments