Skip to content

Commit

Permalink
Adds test class for FlyListener and fixes bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Nov 30, 2019
1 parent 8f964a0 commit 40a9d06
Show file tree
Hide file tree
Showing 4 changed files with 322 additions and 52 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<powermock.version>1.7.4</powermock.version>
<powermock.version>2.0.2</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.14.4-R0.1-SNAPSHOT</spigot.version>
<spigot.version>1.13.2-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.7.0</bentobox.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import world.bentobox.bentobox.BentoBox;

import world.bentobox.bentobox.api.events.flags.FlagProtectionChangeEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
Expand All @@ -12,11 +12,9 @@

public class FlyFlagListener implements Listener {

private BentoBox plugin;
private IslandFlyAddon addon;

public FlyFlagListener(IslandFlyAddon addon) {
this.plugin = addon.getPlugin();
this.addon = addon;
}

Expand All @@ -31,12 +29,12 @@ public void onFlagChange(FlagProtectionChangeEvent e) {
// Stream through all of the flying and not allowed users at
// the moment and warn them that their fly is about to turn off
e.getIsland().getPlayersOnIsland().parallelStream()
.filter(Player::isFlying)
.filter(p -> !(island.isAllowed(User.getInstance(p), IslandFlyAddon.ISLAND_FLY_PROTECTION) || p.isOp()))
.forEach(p -> {
.filter(Player::isFlying)
.filter(p -> !(island.isAllowed(User.getInstance(p), IslandFlyAddon.ISLAND_FLY_PROTECTION) || p.isOp()))
.forEach(p -> {

startDisabling(p, island);
});
startDisabling(p, island);
});
}

public void startDisabling(Player p, Island island) {
Expand Down
81 changes: 39 additions & 42 deletions src/main/java/world/bentobox/islandfly/listeners/FlyListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package world.bentobox.islandfly.listeners;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -11,9 +12,6 @@
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.islandfly.IslandFlyAddon;

import java.util.Optional;


/**
* This class manages players fly ability.
*/
Expand All @@ -35,75 +33,74 @@ public FlyListener(final IslandFlyAddon addon) {


/**
* This method is triggered when player leaves its island.
* This method is triggered when player leaves their island.
* @param event instance of IslandEvent.IslandExitEvent
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onExitIsland(final IslandEvent.IslandExitEvent event) {

final User user = User.getInstance(event.getPlayerUUID());

final Island i = addon.getIslands().getIslandAt(user.getLocation()).orElse(null);

if (i == null) return;

if (!user.getPlayer().getAllowFlight()) return;

// Bypass permission
if (this.addon.getPlugin().getIWM().getAddon(user.getWorld())
.map(a -> user.hasPermission(a.getPermissionPrefix() + "island.flybypass")).orElse(false)) {
return;
}

// Alert player fly will be disabled
final int flyTimeout = this.addon.getSettings().getFlyTimeout();

// If timeout is 0 or less disable fly immediately
if (flyTimeout <= 0) {
disableFly(user);
removeFly(user);
return;
}

// If this is not true, player's fly will silently be turned off
// Else disable fly with a delay
if (user.getPlayer().isFlying())
user.sendMessage("islandfly.fly-outside-alert", TextVariables.NUMBER, String.valueOf(flyTimeout));
user.sendMessage("islandfly.fly-outside-alert", TextVariables.NUMBER, String.valueOf(flyTimeout));

// Else disable fly with a delay
this.addon.getServer().getScheduler().runTaskLater(this.addon.getPlugin(), () -> {
Bukkit.getScheduler().runTaskLater(this.addon.getPlugin(), () -> removeFly(user), 20L* flyTimeout);
}

// Verify player is still online
if (!user.isOnline()) return;

Island is = addon.getIslands().getIslandAt(user.getLocation()).orElse(null);
/**
* Remove fly from a player if required
* @param user - user to check
* @return true if fly is removed, otherwise false
*/
boolean removeFly(User user) {
// Verify player is still online
if (!user.isOnline()) return false;

if (is == null) return;
Island is = addon.getIslands().getProtectedIslandAt(user.getLocation()).orElse(null);

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

disableFly(user);
return;
}
if (user.getPlayer().isFlying())
user.sendMessage("islandfly.cancel-disable");
return;
}
// Check if player is back on a spawn island
if (is.isSpawn()) {
if (this.addon.getPlugin().getIWM().getAddon(user.getWorld())
.map(a -> !user.hasPermission(a.getPermissionPrefix() + "island.flyspawn")).orElse(false)) {

// Check if player was reallowed to fly on the island he is at that moment
if (!is.isAllowed(user, IslandFlyAddon.ISLAND_FLY_PROTECTION) || !is.onIsland(user.getLocation())) {
disableFly(user);
return;
return true;
}

// If false, will stay silent
if (user.getPlayer().isFlying())
user.sendMessage("islandfly.cancel-disable");
}, 20L* flyTimeout);
return false;
}

// Check if player is allowed to fly on the island he is at that moment
if (!is.isAllowed(user, IslandFlyAddon.ISLAND_FLY_PROTECTION)) {
disableFly(user);
return true;
}

// If false, will stay silent
if (user.getPlayer().isFlying())
user.sendMessage("islandfly.cancel-disable");
return false;
}



/**
* Disable player fly and alert it
* @param user
Expand Down

0 comments on commit 40a9d06

Please sign in to comment.