Skip to content

Commit

Permalink
Fixes bugs and implements features (#24)
Browse files Browse the repository at this point in the history
* Fixes bugs and implements features

* Deleting forgotten comment
  • Loading branch information
wellnesscookie authored and tastybento committed Nov 10, 2019
1 parent c964fb9 commit 87ece61
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- This allows to change between versions and snapshots. -->
<build.version>1.7.1</build.version>
<build.version>1.7.2</build.version>
<build.number>-LOCAL</build.number>
</properties>

Expand Down
20 changes: 18 additions & 2 deletions src/main/java/world/bentobox/islandfly/FlyToggleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;


Expand Down Expand Up @@ -38,10 +39,25 @@ public boolean canExecute(User user, String label, List<String> args) {
return false;
}

if (!user.hasPermission(this.getPermissionPrefix() + "island.flybypass") && !getIslands().userIsOnIsland(user.getWorld(), user)) {
user.sendMessage("islandfly.command.only-on-island");
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()) {
if (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;
}


return true;
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/world/bentobox/islandfly/IslandFlyAddon.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package world.bentobox.islandfly;

import org.bukkit.Material;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.islandfly.config.Settings;
import world.bentobox.islandfly.listeners.FlyDeathListener;
import world.bentobox.islandfly.listeners.FlyFlagListener;
import world.bentobox.islandfly.listeners.FlyListener;
import world.bentobox.islandfly.listeners.FlyLogoutListener;

Expand All @@ -17,6 +21,17 @@ public class IslandFlyAddon extends Addon {
*/
private Settings settings;

/**
* A flag to allow or disallow flight on island
* based on player's rank
*/
public static final Flag ISLAND_FLY_PROTECTION =
new Flag.Builder("ISLAND_FLY_PROTECTION", Material.ELYTRA)
.type(Flag.Type.PROTECTION)
.mode(Flag.Mode.ADVANCED)
.defaultRank(RanksManager.MEMBER_RANK)
.defaultSetting(true).build();

/**
* Boolean that indicate if addon is hooked into any gamemode.
*/
Expand Down Expand Up @@ -68,6 +83,8 @@ public void onEnable() {
new FlyToggleCommand(playerCommand);
hooked = true;
});

ISLAND_FLY_PROTECTION.addGameModeAddon(gameModeAddon);
}
});

Expand All @@ -77,6 +94,10 @@ public void onEnable() {
registerListener(new FlyListener(this));
registerListener(new FlyDeathListener(this));
registerListener(new FlyLogoutListener(this));
registerListener(new FlyFlagListener(this));

// Register a flag
registerFlag(ISLAND_FLY_PROTECTION);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package world.bentobox.islandfly.listeners;

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;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.islandfly.IslandFlyAddon;

public class FlyFlagListener implements Listener {

private BentoBox plugin;
private IslandFlyAddon addon;

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

@EventHandler
public void onFlagChange(FlagProtectionChangeEvent e) {

if (!e.getEditedFlag().equals(IslandFlyAddon.ISLAND_FLY_PROTECTION))
return;

Island island = e.getIsland();

// 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 -> {

startDisabling(p, island);
});
}

public void startDisabling(Player p, Island island) {

int flyTimeout = this.addon.getSettings().getFlyTimeout();
User user = User.getInstance(p);


// Alert player fly will be disabled
user.sendMessage("islandfly.fly-turning-off-alert", TextVariables.NUMBER, String.valueOf(flyTimeout));

// If timeout is 0 or less disable fly immediately
if (flyTimeout <= 0) {

p.setFlying(false);
p.setAllowFlight(false);
user.sendMessage("islandfly.disable-fly");

return;
}

// Else disable fly with a delay
addon.getServer().getScheduler().runTaskLater(this.addon.getPlugin(), () -> {

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

// Check if user was reallowed to fly in the meantime
if (!island.isAllowed(user,IslandFlyAddon.ISLAND_FLY_PROTECTION)) {

// Silent cancel fly if player changed island in the meantime
// It will be the job of Enter/Exit island event to turn fly off if required
if (!island.onIsland(p.getLocation()))
return;

p.setFlying(false);
p.setAllowFlight(false);
user.sendMessage("islandfly.disable-fly");
}
else {
user.sendMessage("islandfly.reallowed-fly");
}

}, 20L* flyTimeout);
}
}
53 changes: 36 additions & 17 deletions src/main/java/world/bentobox/islandfly/listeners/FlyListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.islandfly.IslandFlyAddon;

import java.util.Optional;


/**
* This class manages players fly ability.
Expand Down Expand Up @@ -39,17 +40,14 @@ public FlyListener(final IslandFlyAddon addon) {
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onExitIsland(final IslandEvent.IslandExitEvent event) {
// Check only when player exit is own island
// Player already flying

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

if (!addon.getIslands().userIsOnIsland(Util.getWorld(event.getLocation().getWorld()), user)) {
return;
}

if (!user.getPlayer().getAllowFlight()) {
return;
}
final Optional<Island> i = addon.getIslands().getIslandAt(user.getLocation());

if (!i.isPresent()) return;

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

// Bypass permission
if (this.addon.getPlugin().getIWM().getAddon(user.getWorld())
Expand All @@ -60,27 +58,48 @@ public void onExitIsland(final IslandEvent.IslandExitEvent event) {
// Alert player fly will be disabled
final int flyTimeout = this.addon.getSettings().getFlyTimeout();

user.sendMessage("islandfly.fly-outside-alert", TextVariables.NUMBER, String.valueOf(flyTimeout));
// If timeout is 0 or less disable fly immediately
if (flyTimeout <= 0) {
disableFly(user);
return;
}

// If this is not true, player's fly will silently be turned off
if (user.getPlayer().isFlying())
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(), () -> {

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

final IslandsManager islands = this.addon.getIslands();
Island is = addon.getIslands().getIslandAt(user.getLocation()).orElse(null);

if (is == null) 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 player is not on his own island
if (!(islands.userIsOnIsland(Util.getWorld(user.getWorld()), user))) {
disableFly(user);
return;
}
if (user.getPlayer().isFlying())
user.sendMessage("islandfly.cancel-disable");
return;
}

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

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

Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/addon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,39 @@ description: Allow players to fly on their island


permissions:
acidisland.island.flyspawn:
description: Allows to use fly on spawn
default: op
acidisland.island.fly:
description: Allows access to fly command.
default: true
acidisland.island.flybypass:
description: Allows to keep fly mode on player death.
default: op

bskyblock.island.flyspawn:
description: Allows to use fly on spawn
default: op
bskyblock.island.fly:
description: Allows access to fly command.
default: true
bskyblock.island.flybypass:
description: Allows to keep fly mode on player death.
default: op

caveblock.island.flyspawn:
description: Allows to use fly on spawn
default: op
caveblock.island.fly:
description: Allows access to fly command.
default: true
caveblock.island.flybypass:
description: Allows to keep fly mode on player death.
default: op

skygrid.island.flyspawn:
description: Allows to use fly on spawn
default: op
skygrid.island.fly:
description: Allows access to fly command.
default: true
Expand Down
10 changes: 9 additions & 1 deletion src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
islandfly:
fly-outside-alert: "&cYou are outside your island so fly mode will be disabled in &e[number]&c seconds."
fly-turning-off-alert: "&cYou are not permitted to fly here anymore. Turning fly off in &e[number]&c seconds."
disable-fly: "&cYour fly mode has been disabled."
reallowed-fly: "&aYour fly has been reallowed"
enable-fly: "&aYour fly mode has been enabled."
cancel-disable: "&aYou are back, huh! Fly fuel successfully refilled!"
wrong-world: "&cYou are not in the right gamemode world"
command:
description: "allows you to fly on your island"
only-on-island: "&cYou can only fly on your island."
not-allowed-fly: "&cYou are not allowed to fly on this island"

protection:
flags:
ISLAND_FLY_PROTECTION:
description: "&aToggle who can fly on your island"
name: "&aFly prevention"

0 comments on commit 87ece61

Please sign in to comment.