Skip to content

Commit

Permalink
Remember fly state for member islands so on return it is resumed.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Aug 25, 2024
1 parent 547dedb commit e776df7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
7 changes: 3 additions & 4 deletions src/main/java/world/bentobox/islandfly/FlyToggleCommand.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package world.bentobox.islandfly;

import java.util.List;

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.
Expand Down Expand Up @@ -88,13 +89,11 @@ 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");
Expand Down
40 changes: 28 additions & 12 deletions src/main/java/world/bentobox/islandfly/listeners/FlyListener.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package world.bentobox.islandfly.listeners;

import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerToggleFlightEvent;
import org.eclipse.jdt.annotation.NonNull;

import world.bentobox.bentobox.api.events.island.IslandEnterEvent;
import world.bentobox.bentobox.api.events.island.IslandExitEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.metadata.MetaDataValue;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.islandfly.IslandFlyAddon;
Expand All @@ -19,25 +24,31 @@
*/
public class FlyListener implements Listener {

private static final @NonNull String ISLANDFLY = "IslandFly-";
/**
* Addon instance object.
*/
private final IslandFlyAddon islandFlyAddon;
private final IslandFlyAddon addon;


/**
* Default constructor.
* @param islandFlyAddon instance of IslandFlyAddon
*/
public FlyListener(final IslandFlyAddon islandFlyAddon) {
this.islandFlyAddon = islandFlyAddon;
this.addon = islandFlyAddon;
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onToggleFlight(final PlayerToggleFlightEvent event) {
final User user = User.getInstance(event.getPlayer());
if (checkUser(user)) {
user.sendMessage("islandfly.not-allowed");
} else {
addon.getIslands().getIslandAt(user.getLocation())
.filter(i -> i.getMemberSet().contains(user.getUniqueId())).ifPresent(is -> user
.setMetaData(Map.of("IslandFly-" + is.getUniqueId(), new MetaDataValue(event.isFlying())))); // Record the fly state for this island

}
}

Expand All @@ -46,7 +57,7 @@ public void onToggleFlight(final PlayerToggleFlightEvent event) {
* @return true if fly was blocked
*/
private boolean checkUser(User user) {
String permPrefix = islandFlyAddon.getPlugin().getIWM().getPermissionPrefix(user.getWorld());
String permPrefix = addon.getPlugin().getIWM().getPermissionPrefix(user.getWorld());
// Ignore ops
if (user.isOp() || user.getPlayer().getGameMode().equals(GameMode.CREATIVE)
|| user.getPlayer().getGameMode().equals(GameMode.SPECTATOR)
Expand All @@ -57,8 +68,13 @@ private boolean checkUser(User user) {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onEnterIsland(final IslandEnterEvent event) {
final User user = User.getInstance(event.getPlayerUUID());
user.getMetaData(ISLANDFLY + event.getIsland().getUniqueId())
.ifPresent(mdv -> {
user.getPlayer().setAllowFlight(true);
user.getPlayer().setFlying(mdv.asBoolean());
});
// Wait until after arriving at the island
Bukkit.getScheduler().runTask(this.islandFlyAddon.getPlugin(), () -> checkUser(user));
Bukkit.getScheduler().runTask(this.addon.getPlugin(), () -> checkUser(user));
}

/**
Expand All @@ -67,17 +83,16 @@ public void onEnterIsland(final IslandEnterEvent event) {
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onExitIsland(final IslandExitEvent event) {

final User user = User.getInstance(event.getPlayerUUID());
String permPrefix = islandFlyAddon.getPlugin().getIWM().getPermissionPrefix(user.getWorld());
String permPrefix = addon.getPlugin().getIWM().getPermissionPrefix(user.getWorld());
// Ignore ops
if (user.isOp() || user.getPlayer().getGameMode().equals(GameMode.CREATIVE)
|| user.getPlayer().getGameMode().equals(GameMode.SPECTATOR)
|| user.hasPermission(permPrefix + "island.flybypass")
|| (!user.hasPermission(permPrefix + "island.fly")
&& !user.hasPermission(permPrefix + "island.flyspawn"))) return;
// Alert player fly will be disabled
final int flyTimeout = this.islandFlyAddon.getSettings().getFlyTimeout();
final int flyTimeout = this.addon.getSettings().getFlyTimeout();

// If timeout is 0 or less disable fly immediately
if (flyTimeout <= 0) {
Expand All @@ -90,7 +105,7 @@ public void onExitIsland(final IslandExitEvent event) {
user.sendMessage("islandfly.fly-outside-alert", TextVariables.NUMBER, String.valueOf(flyTimeout));
}

Bukkit.getScheduler().runTaskLater(this.islandFlyAddon.getPlugin(), () -> removeFly(user), 20L* flyTimeout);
Bukkit.getScheduler().runTaskLater(this.addon.getPlugin(), () -> removeFly(user), 20L * flyTimeout);
}


Expand All @@ -103,7 +118,7 @@ boolean removeFly(User user) {
// Verify player is still online
if (!user.isOnline()) return false;

Island island = islandFlyAddon.getIslands().getProtectedIslandAt(user.getLocation()).orElse(null);
Island island = addon.getIslands().getProtectedIslandAt(user.getLocation()).orElse(null);

if (island == null) {
disableFly(user);
Expand All @@ -112,7 +127,7 @@ boolean removeFly(User user) {

// Check if player is back on a spawn island
if (island.isSpawn()) {
if (this.islandFlyAddon.getPlugin().getIWM().getAddon(user.getWorld())
if (this.addon.getPlugin().getIWM().getAddon(user.getWorld())
.map(a -> !user.hasPermission(a.getPermissionPrefix() + "island.flyspawn")).orElse(false)) {

disableFly(user);
Expand All @@ -121,8 +136,9 @@ boolean removeFly(User user) {
return false;
}

if(islandFlyAddon.getSettings().getFlyMinLevel() > 1 && islandFlyAddon.getLevelAddon() != null) {
if (islandFlyAddon.getLevelAddon().getIslandLevel(island.getWorld(), island.getOwner()) < islandFlyAddon.getSettings().getFlyMinLevel()) {
if (addon.getSettings().getFlyMinLevel() > 1 && addon.getLevelAddon() != null) {
if (addon.getLevelAddon().getIslandLevel(island.getWorld(), island.getOwner()) < addon.getSettings()
.getFlyMinLevel()) {
disableFly(user);
return false;
}
Expand Down

0 comments on commit e776df7

Please sign in to comment.