Skip to content

Commit

Permalink
Add min level required /is fly usage (#115)
Browse files Browse the repository at this point in the history
* Add min level required /is fly usage

* Fix missing methods and inspect errors

---------

Co-authored-by: Dieu <[email protected]>
  • Loading branch information
IAISI and L4BORG authored Jan 20, 2024
1 parent 7541d30 commit 23029d1
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 53 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
<!-- More visible way how to change dependency versions -->
<spigot.version>1.20.4-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>2.0.0-SNAPSHOT</bentobox.version>
<!-- Level addon version -->
<level.version>2.9.0</level.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- This allows to change between versions and snapshots. -->
Expand Down Expand Up @@ -123,6 +125,12 @@
<version>${bentobox.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>world.bentobox</groupId>
<artifactId>level</artifactId>
<version>${level.version}</version>
<scope>provided</scope>
</dependency>
<!-- Mockito (Unit testing) -->
<dependency>
<groupId>org.mockito</groupId>
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/world/bentobox/islandfly/FlyToggleCommand.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
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.
*/
public class FlyToggleCommand extends CompositeCommand {


private Settings settings;
private final Settings settings;
private final IslandFlyAddon islandFlyAddon;

/**
* Default constructor
Expand All @@ -26,6 +27,7 @@ public class FlyToggleCommand extends CompositeCommand {
public FlyToggleCommand(CompositeCommand parent, IslandFlyAddon addon) {
super(parent, "fly");
this.settings = addon.getSettings();
this.islandFlyAddon = addon;
}


Expand Down Expand Up @@ -71,6 +73,12 @@ public boolean canExecute(User user, String label, List<String> args) {

}

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;
}
Expand Down
39 changes: 33 additions & 6 deletions src/main/java/world/bentobox/islandfly/IslandFlyAddon.java
Original file line number Diff line number Diff line change
@@ -1,17 +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.FlyLoginListener;
import world.bentobox.islandfly.listeners.FlyLogoutListener;
import world.bentobox.islandfly.listeners.*;
import world.bentobox.level.Level;


/**
Expand All @@ -23,6 +19,11 @@ public class IslandFlyAddon extends Addon {
*/
private Settings settings;

/**
* Level addon instance.
*/
private Level levelAddon;

/**
* A flag to allow or disallow flight on island
* based on player's rank
Expand Down Expand Up @@ -115,6 +116,21 @@ public void onDisable() {
//Nothing to do here
}

/**
* Check addon hooks.
*/
public void allLoaded()
{
// Try to find Level addon and if it does not exist, display a warning
this.getAddonByName("Level").ifPresentOrElse(addon ->
{
this.levelAddon = (Level) addon;
this.log("Level Addon hooked into Level addon.");
}, () ->
{
this.levelAddon = null;
});
}

/**
* This method loads addon configuration settings in memory.
Expand All @@ -136,4 +152,15 @@ private void loadSettings() {
public Settings getSettings() {
return settings;
}


/**
* Gets level addon.
*
* @return the level addon
*/
public Level getLevelAddon()
{
return levelAddon;
}
}
23 changes: 18 additions & 5 deletions src/main/java/world/bentobox/islandfly/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
package world.bentobox.islandfly.config;


import java.util.HashSet;
import java.util.Set;

import world.bentobox.bentobox.api.configuration.ConfigComment;
import world.bentobox.bentobox.api.configuration.ConfigEntry;
import world.bentobox.bentobox.api.configuration.ConfigObject;
import world.bentobox.bentobox.api.configuration.StoreAt;

import java.util.HashSet;
import java.util.Set;


/**
* Settings that implements ConfigObject is powerful and dynamic Config Objects that
Expand Down Expand Up @@ -45,6 +45,13 @@ public int getFlyTimeout()
return flyTimeout;
}

public long getFlyMinLevel() {
return flyMinLevel;
}

public void setFlyMinLevel(long flyMinLevel) {
this.flyMinLevel = flyMinLevel;
}

/**
* Method Settings#setFlyTimeout sets new value for the flyTimeout of this object.
Expand Down Expand Up @@ -79,6 +86,7 @@ public void setFlyDisableOnLogout(boolean flyDisableOnLogout)
}



/**
* This method returns the disabledGameModes value.
*
Expand Down Expand Up @@ -115,8 +123,8 @@ public boolean isAllowCommandOutsideProtectionRange()


/**
* Method Settings#setFlyDisableOnLogout sets new value for the flyDisableOnLogout of this object.
* @param flyDisableOnLogout new value for this object.
* Method Settings#setAllowCommandOutsideProtectionRange sets new value for the commandAllowed of this object.
* @param commandAllowed new value for this object.
*
*/
public void setAllowCommandOutsideProtectionRange(boolean commandAllowed)
Expand All @@ -142,6 +150,11 @@ public void setAllowCommandOutsideProtectionRange(boolean commandAllowed)
@ConfigEntry(path = "logout-disable-fly")
private boolean flyDisableOnLogout = false;

@ConfigComment("")
@ConfigComment("Minimum required level to toggle fly.")
@ConfigEntry(path = "fly-min-level")
private long flyMinLevel = 0;

@ConfigComment("")
@ConfigComment("This list stores GameModes in which islandFly addon should not work.")
@ConfigComment("To disable addon it is necessary to write its name in new line that starts with -. Example:")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package world.bentobox.islandfly.listeners;

import java.util.Optional;
import java.util.UUID;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerRespawnEvent;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.islandfly.IslandFlyAddon;

import java.util.Optional;
import java.util.UUID;


/**
* This class manages Death and Respawn options.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import world.bentobox.bentobox.api.events.flags.FlagProtectionChangeEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
Expand Down
36 changes: 21 additions & 15 deletions src/main/java/world/bentobox/islandfly/listeners/FlyListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerToggleFlightEvent;

import world.bentobox.bentobox.api.events.island.IslandEnterEvent;
import world.bentobox.bentobox.api.events.island.IslandExitEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
Expand All @@ -23,15 +22,15 @@ public class FlyListener implements Listener {
/**
* Addon instance object.
*/
private final IslandFlyAddon addon;
private final IslandFlyAddon islandFlyAddon;


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

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

/**
Expand All @@ -70,15 +69,15 @@ public void onEnterIsland(final IslandEnterEvent event) {
public void onExitIsland(final IslandExitEvent event) {

final User user = User.getInstance(event.getPlayerUUID());
String permPrefix = addon.getPlugin().getIWM().getPermissionPrefix(user.getWorld());
String permPrefix = islandFlyAddon.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.addon.getSettings().getFlyTimeout();
final int flyTimeout = this.islandFlyAddon.getSettings().getFlyTimeout();

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

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


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

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

if (is == null) {
if (island == null) {
disableFly(user);
return true;
}

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

disableFly(user);
Expand All @@ -122,8 +121,15 @@ 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()) {
disableFly(user);
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)) {
if (!island.isAllowed(user, IslandFlyAddon.ISLAND_FLY_PROTECTION)) {
disableFly(user);
return true;
}
Expand Down
Loading

0 comments on commit 23029d1

Please sign in to comment.