diff --git a/src/main/java/com/massivecraft/factions/FPlayer.java b/src/main/java/com/massivecraft/factions/FPlayer.java index 377c9ee2..75c32f49 100644 --- a/src/main/java/com/massivecraft/factions/FPlayer.java +++ b/src/main/java/com/massivecraft/factions/FPlayer.java @@ -31,6 +31,19 @@ public interface FPlayer extends EconomyParticipator { boolean hasNotificationsEnabled(); + /** + * Determine if a player has enemies nearby based on the enemy check task in CmdFly + * NOTE: THIS VALUE IS ONLY UPDATED WHEN A USER IS USING FLY + * @return enemiesNearby as a boolean + */ + boolean hasEnemiesNearby(); + + /** + * Set if this FPlayer has an enemy nearby + * @param b enemiesNearby + */ + void setEnemiesNearby(Boolean b); + /** * Get if a player has setup their Discord before * @return if the player setup Discord as a boolean diff --git a/src/main/java/com/massivecraft/factions/FactionsPlugin.java b/src/main/java/com/massivecraft/factions/FactionsPlugin.java index ed3eb66a..853fb633 100755 --- a/src/main/java/com/massivecraft/factions/FactionsPlugin.java +++ b/src/main/java/com/massivecraft/factions/FactionsPlugin.java @@ -210,7 +210,6 @@ public class FactionsPlugin extends MPlugin { if (fPlayer.isAlt()) faction.addAltPlayer(fPlayer); else faction.addFPlayer(fPlayer); } - if (getConfig().getBoolean("enable-faction-flight", true)) UtilFly.run(); Board.getInstance().load(); diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdFly.java b/src/main/java/com/massivecraft/factions/cmd/CmdFly.java index 43631d52..eec4de47 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdFly.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdFly.java @@ -9,6 +9,7 @@ import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.PermissableAction; import com.massivecraft.factions.zcore.util.TL; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -34,7 +35,7 @@ public class CmdFly extends FCommand { this.aliases.addAll(Aliases.fly); this.optionalArgs.put("on/off", "flip"); - this.requirements = new CommandRequirements.Builder(Permission.FLY) + this.requirements = new CommandRequirements.Builder(Permission.FLY_FLY) .playerOnly() .memberOnly() .build(); @@ -78,19 +79,15 @@ public class CmdFly extends FCommand { } FPlayer fPlayer = FPlayers.getInstance().getByPlayer(player); Faction myFaction = fPlayer.getFaction(); - if (myFaction.isWilderness()) { - fPlayer.setFlying(false); - flyMap.remove(name); - continue; - } - if (player.hasPermission("factions.fly.bypassnearbyenemycheck") || fPlayer.checkIfNearbyEnemies()) { + if (!player.hasPermission("factions.fly.bypassnearbyenemycheck") && !fPlayer.isAdminBypassing()) { + if (fPlayer.hasEnemiesNearby()) disableFlightSync(fPlayer); + checkEnemiesSync(fPlayer); continue; } FLocation myFloc = new FLocation(player.getLocation()); if (Board.getInstance().getFactionAt(myFloc) != myFaction) { - if (!checkBypassPerms(fPlayer, player, Board.getInstance().getFactionAt(myFloc))) { - Bukkit.getScheduler().runTask(FactionsPlugin.instance, () -> fPlayer.setFFlying(false, false)); - flyMap.remove(name); + if (!checkFly(fPlayer, player, Board.getInstance().getFactionAt(myFloc))) { + disableFlightSync(fPlayer); } } @@ -100,36 +97,15 @@ public class CmdFly extends FCommand { }, 20L, 20L); } - public static boolean checkBypassPerms(FPlayer fme, Player me, Faction toFac) { + public static boolean checkFly(FPlayer fme, Player me, Faction toFac) { if (Conf.denyFlightIfInNoClaimingWorld && !Conf.worldsNoClaiming.isEmpty() && Conf.worldsNoClaiming.stream().anyMatch(me.getWorld().getName()::equalsIgnoreCase)) return false; - - if (toFac != fme.getFaction()) { - if (!me.hasPermission(Permission.FLY_WILD.node) && toFac.isWilderness() || !me.hasPermission(Permission.FLY_SAFEZONE.node) && toFac.isSafeZone() || !me.hasPermission(Permission.FLY_WARZONE.node) && toFac.isWarZone()) { - fme.msg(TL.COMMAND_FLY_NO_ACCESS, toFac.getTag(fme)); - return false; - } - Access access = toFac.getAccess(fme, PermissableAction.FLY); - if ((!(me.hasPermission(Permission.FLY_ENEMY.node) || access == Access.ALLOW)) && toFac.getRelationTo(fme.getFaction()) == Relation.ENEMY) { - fme.msg(TL.COMMAND_FLY_NO_ACCESS, toFac.getTag(fme)); - return false; - } - if (!(me.hasPermission(Permission.FLY_ALLY.node) || access == Access.ALLOW) && toFac.getRelationTo(fme.getFaction()) == Relation.ALLY) { - fme.msg(TL.COMMAND_FLY_NO_ACCESS, toFac.getTag(fme)); - return false; - } - if (!(me.hasPermission(Permission.FLY_TRUCE.node) || access == Access.ALLOW) && toFac.getRelationTo(fme.getFaction()) == Relation.TRUCE) { - fme.msg(TL.COMMAND_FLY_NO_ACCESS, toFac.getTag(fme)); - return false; - } - - if (!(me.hasPermission(Permission.FLY_NEUTRAL.node) || access == Access.ALLOW) && toFac.getRelationTo(fme.getFaction()) == Relation.NEUTRAL && !toFac.isSystemFaction()) { - fme.msg(TL.COMMAND_FLY_NO_ACCESS, toFac.getTag(fme)); - return false; - } - return me.hasPermission(Permission.FLY.node) && (access != Access.DENY || toFac.isSystemFaction()); - } - return true; + if (toFac.getAccess(fme, PermissableAction.FLY) == Access.ALLOW) return true; + if (fme.getFaction().isWilderness()) return false; + if (toFac.isSystemFaction()) return me.hasPermission(Permission.valueOf("FLY_" + ChatColor.stripColor(toFac.getTag()).toUpperCase()).node); + Relation relationTo = toFac.getRelationTo(fme.getFaction()); + if (!relationTo.isEnemy() && !relationTo.isMember()) return me.hasPermission(Permission.valueOf("FLY_" + relationTo.name()).node); + return false; } @@ -145,34 +121,43 @@ public class CmdFly extends FCommand { flyMap.remove(fme.getPlayer().getName()); } + private static void disableFlightSync(FPlayer fme) { + Bukkit.getScheduler().runTask(FactionsPlugin.instance, () -> fme.setFFlying(false, false)); + flyMap.remove(fme.getName()); + } + public boolean isInFlightChecker(Player player) { return flyMap.containsKey(player.getName()); } + private static void checkEnemiesSync(FPlayer fp) { + Bukkit.getScheduler().runTask(FactionsPlugin.instance, fp::checkIfNearbyEnemies); + } + @Override public void perform(CommandContext context) { - // Disabled by default. - if (!FactionsPlugin.getInstance().getConfig().getBoolean("enable-faction-flight", false)) { - context.fPlayer.msg(TL.COMMAND_FLY_DISABLED); - return; - } + if (!context.fPlayer.isAdminBypassing()) { + List entities = context.player.getNearbyEntities(16.0D, 256.0D, 16.0D); - FLocation myfloc = new FLocation(context.player.getLocation()); - Faction toFac = Board.getInstance().getFactionAt(myfloc); - if (!checkBypassPerms(context.fPlayer, context.player, toFac)) return; - List entities = context.player.getNearbyEntities(16.0D, 256.0D, 16.0D); - - for (int i = 0; i <= entities.size() - 1; ++i) { - if (entities.get(i) instanceof Player) { - Player eplayer = (Player) entities.get(i); - FPlayer efplayer = FPlayers.getInstance().getByPlayer(eplayer); - if (efplayer.getRelationTo(context.fPlayer) == Relation.ENEMY && !efplayer.isStealthEnabled()) { - context.msg(TL.COMMAND_FLY_CHECK_ENEMY); - return; + for (int i = 0; i <= entities.size() - 1; ++i) { + if (entities.get(i) instanceof Player) { + Player eplayer = (Player) entities.get(i); + FPlayer efplayer = FPlayers.getInstance().getByPlayer(eplayer); + if (efplayer.getRelationTo(context.fPlayer) == Relation.ENEMY && !efplayer.isStealthEnabled()) { + context.msg(TL.COMMAND_FLY_CHECK_ENEMY); + return; + } + context.fPlayer.setEnemiesNearby(false); } } - } + FLocation myfloc = new FLocation(context.player.getLocation()); + Faction toFac = Board.getInstance().getFactionAt(myfloc); + if (!checkFly(context.fPlayer, context.player, toFac)) { + context.fPlayer.sendMessage(TL.COMMAND_FLY_NO_ACCESS.format(toFac.getTag())); + return; + } + } if (context.args.size() == 0) { toggleFlight(context.fPlayer.isFlying(), context.fPlayer, context); @@ -188,8 +173,6 @@ public class CmdFly extends FCommand { return; } - - if (fme.canFlyAtLocation()) { context.doWarmUp(WarmUpUtil.Warmup.FLIGHT, TL.WARMUPS_NOTIFY_FLIGHT, "Fly", () -> { fme.setFlying(true); flyMap.put(fme.getPlayer().getName(), true); @@ -201,9 +184,6 @@ public class CmdFly extends FCommand { startFlyCheck(); } }, FactionsPlugin.getInstance().getConfig().getLong("warmups.f-fly", 0)); - } else { - fme.msg(TL.COMMAND_FLY_NO_ACCESS, Board.getInstance().getFactionAt(fme.getLastStoodAt()).getTag()); - } } @Override diff --git a/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 06718769..c113a93b 100644 --- a/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -668,7 +668,7 @@ public class FactionsPlayerListener implements Listener { if (!fPlayer.checkIfNearbyEnemies()) { FLocation myFloc = new FLocation(player.getLocation()); if (Board.getInstance().getFactionAt(myFloc) != myFaction) { - if (!CmdFly.checkBypassPerms(fPlayer, player, Board.getInstance().getFactionAt(myFloc))) { + if (!CmdFly.checkFly(fPlayer, player, Board.getInstance().getFactionAt(myFloc))) { fPlayer.setFFlying(false, false); CmdFly.flyMap.remove(name); } diff --git a/src/main/java/com/massivecraft/factions/struct/Permission.java b/src/main/java/com/massivecraft/factions/struct/Permission.java index 725c98d9..a9462ece 100755 --- a/src/main/java/com/massivecraft/factions/struct/Permission.java +++ b/src/main/java/com/massivecraft/factions/struct/Permission.java @@ -44,8 +44,8 @@ public enum Permission { DISBAND_ANY("disband.any"), DISCORD("discord"), DRAIN("drain"), - FLY("fly"), - FLY_WILD("fly.wilderness"), + FLY_FLY("fly"), + FLY_WILDERNESS("fly.wilderness"), FLY_SAFEZONE("fly.safezone"), FLY_WARZONE("fly.warzone"), FLY_ENEMY("fly.enemy"), diff --git a/src/main/java/com/massivecraft/factions/util/UtilFly.java b/src/main/java/com/massivecraft/factions/util/UtilFly.java index 5422ab77..782d28c1 100644 --- a/src/main/java/com/massivecraft/factions/util/UtilFly.java +++ b/src/main/java/com/massivecraft/factions/util/UtilFly.java @@ -10,7 +10,10 @@ import org.bukkit.Bukkit; public class UtilFly { - + /** + * UtilFly is being removed very soon as all of its functionality has been updated and moved to CmdFly + */ + @Deprecated public static void run() { if (!FactionsPlugin.getInstance().getConfig().getBoolean("enable-faction-flight")) return; @@ -22,7 +25,7 @@ public class UtilFly { } }, 0, FactionsPlugin.getInstance().getConfig().getInt("fly-task-interval", 10)); } - + @Deprecated public static void setFly(FPlayer fp, boolean fly, boolean silent, boolean damage) { if (!FactionsPlugin.getInstance().getConfig().getBoolean("enable-faction-flight")) return; @@ -42,7 +45,7 @@ public class UtilFly { setFallDamage(fp, fly, damage); } - + @Deprecated public static void checkFly(FPlayer me, Faction factionTo) { if (!FactionsPlugin.getInstance().getConfig().getBoolean("enable-faction-flight")) return; diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java index bc2b9a68..f45f2dc0 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java @@ -47,6 +47,7 @@ import java.util.*; */ public abstract class MemoryFPlayer implements FPlayer { + public boolean enemiesNearby = false; public boolean inChest = false; public boolean discordSetup = false; public String discordUserID = ""; @@ -221,6 +222,10 @@ public abstract class MemoryFPlayer implements FPlayer { return this.notificationsEnabled; } + public boolean hasEnemiesNearby() {return this.enemiesNearby;} + + public void setEnemiesNearby(Boolean b) {this.enemiesNearby = b;} + public boolean discordSetup() {return this.discordSetup;} public String discordUserID() {return this.discordUserID;} @@ -972,7 +977,7 @@ public abstract class MemoryFPlayer implements FPlayer { public boolean canFlyAtLocation(FLocation location) { Faction faction = Board.getInstance().getFactionAt(location); if ((faction == getFaction() && getRole() == Role.LEADER) || isAdminBypassing) return true; - if (faction.isSystemFaction()) return CmdFly.checkBypassPerms(this, getPlayer(), faction); + if (faction.isSystemFaction()) return CmdFly.checkFly(this, getPlayer(), faction); Access access = faction.getAccess(this, PermissableAction.FLY); return access == null || access == Access.UNDEFINED || access == Access.ALLOW; } @@ -1108,16 +1113,18 @@ public abstract class MemoryFPlayer implements FPlayer { setFlying(false); msg(TL.COMMAND_FLY_ENEMY_NEAR); Bukkit.getServer().getPluginManager().callEvent(new FPlayerStoppedFlying(this)); + this.enemiesNearby = true; return true; } } } + this.enemiesNearby = false; return false; } @Override public Boolean canflyinWilderness() { - return getPlayer().hasPermission(Permission.FLY_WILD.node); + return getPlayer().hasPermission(Permission.FLY_WILDERNESS.node); } @Override