From a90299d32e403367e4f78fd743adf659c9acc4a9 Mon Sep 17 00:00:00 2001 From: Dariasc Date: Sun, 4 Mar 2018 19:13:32 -0300 Subject: [PATCH] Implement /f fly (#1023) --- .../com/massivecraft/factions/FPlayer.java | 10 +++ .../com/massivecraft/factions/cmd/CmdFly.java | 68 +++++++++++++++++++ .../massivecraft/factions/cmd/FCmdRoot.java | 2 + .../listeners/FactionsEntityListener.java | 13 ++++ .../listeners/FactionsPlayerListener.java | 6 ++ .../factions/struct/Permission.java | 3 +- .../factions/util/WarmUpUtil.java | 2 +- .../zcore/fperms/PermissableAction.java | 4 +- .../factions/zcore/persist/MemoryFPlayer.java | 40 +++++++++++ .../massivecraft/factions/zcore/util/TL.java | 8 +++ src/main/resources/config.yml | 6 ++ src/main/resources/plugin.yml | 4 +- 12 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/massivecraft/factions/cmd/CmdFly.java diff --git a/src/main/java/com/massivecraft/factions/FPlayer.java b/src/main/java/com/massivecraft/factions/FPlayer.java index b6a7d20b..66d951b4 100644 --- a/src/main/java/com/massivecraft/factions/FPlayer.java +++ b/src/main/java/com/massivecraft/factions/FPlayer.java @@ -261,6 +261,16 @@ public interface FPlayer extends EconomyParticipator { public void setId(String id); + public boolean isFlying(); + + public void setFlying(boolean fly); + + public void setFFlying(boolean fly, boolean damage); + + public boolean canFlyAtLocation(); + + public boolean canFlyAtLocation(FLocation location); + // ------------------------------- // Warmups // ------------------------------- diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdFly.java b/src/main/java/com/massivecraft/factions/cmd/CmdFly.java new file mode 100644 index 00000000..1b46db52 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/cmd/CmdFly.java @@ -0,0 +1,68 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.Board; +import com.massivecraft.factions.Faction; +import com.massivecraft.factions.P; +import com.massivecraft.factions.struct.Permission; +import com.massivecraft.factions.util.WarmUpUtil; +import com.massivecraft.factions.zcore.util.TL; + +public class CmdFly extends FCommand { + + public CmdFly() { + super(); + this.aliases.add("fly"); + + this.optionalArgs.put("on/off", "flip"); + + this.permission = Permission.FLIGHT.node; + this.senderMustBeMember = true; + this.senderMustBeModerator = false; + } + + @Override + public void perform() { + if (!P.p.getConfig().getBoolean("enable-faction-flight", false)) { + fme.msg(TL.COMMAND_FLY_DISABLED); + return; + } + + if (args.size() == 0) { + if (!fme.canFlyAtLocation() && !fme.isFlying()) { + Faction factionAtLocation = Board.getInstance().getFactionAt(fme.getLastStoodAt()); + fme.msg(TL.COMMAND_FLY_NO_ACCESS, factionAtLocation.getTag(fme)); + return; + } + + toggleFlight(!fme.isFlying()); + } else if (args.size() == 1) { + if (!fme.canFlyAtLocation() && argAsBool(0)) { + Faction factionAtLocation = Board.getInstance().getFactionAt(fme.getLastStoodAt()); + fme.msg(TL.COMMAND_FLY_NO_ACCESS, factionAtLocation.getTag(fme)); + return; + } + + toggleFlight(argAsBool(0)); + } + } + + private void toggleFlight(final boolean toggle) { + if (!toggle) { + fme.setFlying(false); + return; + } + + this.doWarmUp(WarmUpUtil.Warmup.FLIGHT, TL.WARMUPS_NOTIFY_FLIGHT, "Fly", new Runnable() { + @Override + public void run() { + fme.setFlying(true); + } + }, this.p.getConfig().getLong("warmups.f-fly", 0)); + } + + @Override + public TL getUsageTranslation() { + return TL.COMMAND_FLY_DESCRIPTION; + } + +} diff --git a/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java b/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java index b1bc8c2e..7c1ccb6b 100644 --- a/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java +++ b/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java @@ -20,6 +20,7 @@ public class FCmdRoot extends FCommand { public CmdDeinvite cmdDeinvite = new CmdDeinvite(); public CmdDescription cmdDescription = new CmdDescription(); public CmdDisband cmdDisband = new CmdDisband(); + public CmdFly cmdFly = new CmdFly(); public CmdHelp cmdHelp = new CmdHelp(); public CmdHome cmdHome = new CmdHome(); public CmdInvite cmdInvite = new CmdInvite(); @@ -114,6 +115,7 @@ public class FCmdRoot extends FCommand { this.addSubCommand(this.cmdDeinvite); this.addSubCommand(this.cmdDescription); this.addSubCommand(this.cmdDisband); + this.addSubCommand(this.cmdFly); this.addSubCommand(this.cmdHelp); this.addSubCommand(this.cmdHome); this.addSubCommand(this.cmdInvite); diff --git a/src/main/java/com/massivecraft/factions/listeners/FactionsEntityListener.java b/src/main/java/com/massivecraft/factions/listeners/FactionsEntityListener.java index 3ce02f35..a6ae99be 100644 --- a/src/main/java/com/massivecraft/factions/listeners/FactionsEntityListener.java +++ b/src/main/java/com/massivecraft/factions/listeners/FactionsEntityListener.java @@ -102,9 +102,11 @@ public class FactionsEntityListener implements Listener { if (damagee != null && damagee instanceof Player) { cancelFStuckTeleport((Player) damagee); + cancelFFly((Player) damagee); } if (damager instanceof Player) { cancelFStuckTeleport((Player) damager); + cancelFFly((Player) damager); } } else if (Conf.safeZonePreventAllDamageToPlayers && isPlayerInSafeZone(event.getEntity())) { // Players can not take any damage in a Safe Zone @@ -124,6 +126,17 @@ public class FactionsEntityListener implements Listener { } } + private void cancelFFly(Player player) { + if (player == null) { + return; + } + + FPlayer fPlayer = FPlayers.getInstance().getByPlayer(player); + if (fPlayer.isFlying()) { + fPlayer.setFFlying(false, true); + } + } + public void cancelFStuckTeleport(Player player) { if (player == null) { return; diff --git a/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java index 43537af2..377d6476 100644 --- a/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -177,6 +177,12 @@ public class FactionsPlayerListener implements Listener { Faction factionTo = Board.getInstance().getFactionAt(to); boolean changedFaction = (factionFrom != factionTo); + if (p.getConfig().getBoolean("enable-faction-flight", false) && changedFaction) { + if (!me.canFlyAtLocation() && me.isFlying()) { + me.setFlying(false); + } + } + if (me.isMapAutoUpdating()) { if (showTimes.containsKey(player.getUniqueId()) && (showTimes.get(player.getUniqueId()) > System.currentTimeMillis())) { if (P.p.getConfig().getBoolean("findfactionsexploit.log", false)) { diff --git a/src/main/java/com/massivecraft/factions/struct/Permission.java b/src/main/java/com/massivecraft/factions/struct/Permission.java index ee30e0fd..fb985d22 100644 --- a/src/main/java/com/massivecraft/factions/struct/Permission.java +++ b/src/main/java/com/massivecraft/factions/struct/Permission.java @@ -87,7 +87,8 @@ public enum Permission { TOP("top"), VAULT("vault"), SETMAXVAULTS("setmaxvaults"), - WARP("warp"); + WARP("warp"), + FLIGHT("flight"); public final String node; diff --git a/src/main/java/com/massivecraft/factions/util/WarmUpUtil.java b/src/main/java/com/massivecraft/factions/util/WarmUpUtil.java index 14862ab6..0d7b4c56 100644 --- a/src/main/java/com/massivecraft/factions/util/WarmUpUtil.java +++ b/src/main/java/com/massivecraft/factions/util/WarmUpUtil.java @@ -36,7 +36,7 @@ public class WarmUpUtil { } public enum Warmup { - HOME, WARP; + HOME, WARP, FLIGHT; } } diff --git a/src/main/java/com/massivecraft/factions/zcore/fperms/PermissableAction.java b/src/main/java/com/massivecraft/factions/zcore/fperms/PermissableAction.java index 79ac4a40..b16f5143 100644 --- a/src/main/java/com/massivecraft/factions/zcore/fperms/PermissableAction.java +++ b/src/main/java/com/massivecraft/factions/zcore/fperms/PermissableAction.java @@ -33,7 +33,9 @@ public enum PermissableAction { PROMOTE("promote"), PERMS("perms"), SETWARP("setwarp"), - WARP("warp"),; + WARP("warp"), + FLIGHT("fly"), + ; private String name; 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 75ae196c..5a3d1f13 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java @@ -16,6 +16,8 @@ import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.struct.Role; import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.factions.util.WarmUpUtil; +import com.massivecraft.factions.zcore.fperms.Access; +import com.massivecraft.factions.zcore.fperms.PermissableAction; import com.massivecraft.factions.zcore.util.TL; import mkremins.fanciful.FancyMessage; import org.bukkit.*; @@ -61,6 +63,7 @@ public abstract class MemoryFPlayer implements FPlayer { protected int kills, deaths; protected boolean willAutoLeave = true; protected int mapHeight = 8; // default to old value + protected boolean isFlying = false; protected transient FLocation lastStoodAt = new FLocation(); // Where did this player stand the last time we checked? protected transient boolean mapAutoUpdating; @@ -879,6 +882,43 @@ public abstract class MemoryFPlayer implements FPlayer { return !isOnline(); } + public boolean isFlying() { + return isFlying; + } + + public void setFlying(boolean fly) { + setFFlying(fly, false); + } + + public void setFFlying(boolean fly, boolean damage) { + getPlayer().setAllowFlight(fly); + getPlayer().setFlying(fly); + + if (!damage) { + msg(TL.COMMAND_FLY_CHANGE, fly ? "enabled" : "disabled"); + } else { + msg(TL.COMMAND_FLY_DAMAGE); + } + isFlying = fly; + } + + public boolean canFlyAtLocation() { + return canFlyAtLocation(lastStoodAt); + } + + public boolean canFlyAtLocation(FLocation location) { + Faction faction = Board.getInstance().getFactionAt(location); + if (faction.isWilderness() || faction.isSafeZone() || faction.isWarZone()) { + return false; + } + if (faction == getFaction() && getRole() == Role.ADMIN) { + return true; + } + + Access access = faction.getAccess(this, PermissableAction.FLIGHT); + return access != null && access == Access.ALLOW; + } + // -------------------------------------------- // // Message Sending Helpers // -------------------------------------------- // diff --git a/src/main/java/com/massivecraft/factions/zcore/util/TL.java b/src/main/java/com/massivecraft/factions/zcore/util/TL.java index 9b128e1b..b14aac24 100644 --- a/src/main/java/com/massivecraft/factions/zcore/util/TL.java +++ b/src/main/java/com/massivecraft/factions/zcore/util/TL.java @@ -187,6 +187,12 @@ public enum TL { COMMAND_DISBAND_HOLDINGS("You have been given the disbanded faction's bank, totaling %1$s."), COMMAND_DISBAND_DESCRIPTION("Disband a faction"), + COMMAND_FLY_DISABLED("&cSorry, Faction flight is disabled on this server"), + COMMAND_FLY_DESCRIPTION("Enter or leave Faction flight mode"), + COMMAND_FLY_CHANGE("&eFaction flight &d%1$s"), + COMMAND_FLY_DAMAGE("&eFaction flight &ddisabled&e due to entering combat"), + COMMAND_FLY_NO_ACCESS("&cCannot fly in territory of %1$s"), + COMMAND_FWARP_CLICKTOWARP("Click to warp!"), COMMAND_FWARP_COMMANDFORMAT("/f warp [password]"), COMMAND_FWARP_WARPED("Warped to %1$s"), @@ -579,6 +585,7 @@ public enum TL { COMMAND_WARUNCLAIMALL_SUCCESS("You unclaimed ALL war zone land."), COMMAND_WARUNCLAIMALL_LOG("%1$s unclaimed all war zones."), + /** * Leaving - This is accessed through a command, and so it MAY need a COMMAND_* slug :s */ @@ -779,6 +786,7 @@ public enum TL { /** * Warmups */ + WARMUPS_NOTIFY_FLIGHT("&eFlight will enable in &d%2$d &eseconds."), WARMUPS_NOTIFY_TELEPORT("&eYou will teleport to &d%1$s &ein &d%2$d &eseconds."), WARMUPS_ALREADY("&cYou are already warming up."), WARMUPS_CANCELLED("&cYou have cancelled your warmup."); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 948317d0..0dca0f9e 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -58,6 +58,10 @@ warp-cost: delwarp: 5 warp: 5 +# Faction Fly +# Enable Faction Fly: +enable-faction-flight: true + # Pistons # Should we disable pistons in Faction territory? This will prevent people from doing something like: # http://i.gyazo.com/6a1a31222e58a5d60ff341c13f6a8404.gif @@ -160,6 +164,8 @@ warmups: f-home: 0 # Delay for /f warp f-warp: 0 + # Delay for /f fly + f-fly: 0 ###################################################### #################### HCF Features #################### diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 9875ed9a..b36b9bc1 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -282,4 +282,6 @@ permissions: factions.mapheight: description: Set your /f map height. factions.ban: - description: Ban players from Factions \ No newline at end of file + description: Ban players from Factions + factions.flight: + description: Allow use of /f fly