-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
FlyToggleCommand.java
104 lines (77 loc) · 3.32 KB
/
FlyToggleCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package world.bentobox.islandfly;
import org.bukkit.entity.Player;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
import world.bentobox.islandfly.config.Settings;
import java.util.List;
/**
* This command allows to enable and disable fly mode.
*/
public class FlyToggleCommand extends CompositeCommand {
private final Settings settings;
private final IslandFlyAddon islandFlyAddon;
/**
* Default constructor
* @param parent Instance of CompositeCommand
*/
public FlyToggleCommand(CompositeCommand parent, IslandFlyAddon addon) {
super(parent, "fly");
this.settings = addon.getSettings();
this.islandFlyAddon = addon;
}
@Override
public void setup() {
this.setPermission("island.fly");
this.setOnlyPlayer(true);
this.setDescription("islandfly.command.description");
}
@Override
public boolean canExecute(User user, String label, List<String> args) {
// Checks world from corresponding gamemode command with the world player is executing in
if (this.getWorld() != Util.getWorld(user.getWorld())) {
user.sendMessage("islandfly.wrong-world");
return false;
}
Island island = getIslands().getIslandAt(user.getLocation()).orElse(null);
if (island == null) return false;
// Gets the island at User's location
// Enable fly if island is a spawn and user has permission for it
if (island.isSpawn() && user.hasPermission(this.getPermissionPrefix() + "island.flyspawn")) {
return true;
}
if (!island.isAllowed(user, IslandFlyAddon.ISLAND_FLY_PROTECTION) && !user.hasPermission(this.getPermissionPrefix() + "island.flybypass")) {
user.sendMessage("islandfly.command.not-allowed-fly");
return false;
}
if (!this.settings.isAllowCommandOutsideProtectionRange()
&& !island.getProtectionBoundingBox().contains(user.getLocation().toVector())) {
user.sendMessage("islandfly.outside-protection-range");
return false;
}
if(islandFlyAddon.getSettings().getFlyMinLevel() > 1 && islandFlyAddon.getLevelAddon() != null) {
if (islandFlyAddon.getLevelAddon().getIslandLevel(island.getWorld(), island.getOwner()) < islandFlyAddon.getSettings().getFlyMinLevel()) {
user.sendMessage("islandfly.fly-min-level-alert", TextVariables.NUMBER, String.valueOf(islandFlyAddon.getSettings().getFlyMinLevel()));
return false;
}
}
return true;
}
@Override
public boolean execute(User user, String label, List<String> args) {
final Player player = user.getPlayer();
if (player.getAllowFlight()) {
// Disable fly and notify player
player.setFlying(false);
player.setAllowFlight(false);
user.sendMessage("islandfly.disable-fly");
} else {
// Enable fly and notify player
player.setAllowFlight(true);
user.sendMessage("islandfly.enable-fly");
}
return true;
}
}