From 9e81d6d865370270ff1eff95481c580459fe1887 Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Thu, 31 Jan 2019 10:38:48 +0100 Subject: [PATCH] Improve faction fly --- .../factions/cmd/CmdFactionsFlag.java | 6 +- .../factions/cmd/CmdFactionsFly.java | 12 +-- .../factions/cmd/FactionsCommand.java | 2 +- .../factions/engine/EngineFly.java | 99 ++++++++++++++++++- 4 files changed, 104 insertions(+), 15 deletions(-) diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsFlag.java b/src/com/massivecraft/factions/cmd/CmdFactionsFlag.java index a48a1650..1fd88599 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsFlag.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsFlag.java @@ -6,8 +6,8 @@ public class CmdFactionsFlag extends FactionsCommand // FIELDS // -------------------------------------------- // - CmdFactionsFlagList cmdFactionsFlagList = new CmdFactionsFlagList(); - CmdFactionsFlagShow cmdFactionsFlagShow = new CmdFactionsFlagShow(); - CmdFactionsFlagSet cmdFactionsFlagSet = new CmdFactionsFlagSet(); + public CmdFactionsFlagList cmdFactionsFlagList = new CmdFactionsFlagList(); + public CmdFactionsFlagShow cmdFactionsFlagShow = new CmdFactionsFlagShow(); + public CmdFactionsFlagSet cmdFactionsFlagSet = new CmdFactionsFlagSet(); } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsFly.java b/src/com/massivecraft/factions/cmd/CmdFactionsFly.java index c5fc2c71..ec5334a9 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsFly.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsFly.java @@ -4,9 +4,9 @@ import com.massivecraft.factions.engine.EngineFly; import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.command.MassiveCommandToggle; +import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer; import com.massivecraft.massivecore.engine.EngineMassiveCorePlayerUpdate; import com.massivecraft.massivecore.ps.PS; -import com.massivecraft.massivecore.util.IdUtil; import org.bukkit.entity.Player; public class CmdFactionsFly extends MassiveCommandToggle @@ -24,8 +24,7 @@ public class CmdFactionsFly extends MassiveCommandToggle public CmdFactionsFly() { - super(); - this.setAliases("fly"); + this.addRequirements(RequirementIsPlayer.get()); } // -------------------------------------------- // @@ -47,14 +46,11 @@ public class CmdFactionsFly extends MassiveCommandToggle public void setValue(boolean value) throws MassiveException { MPlayer mplayer = MPlayer.get(sender); - Player player = IdUtil.getPlayer(sender); + Player player = me; if (player == null) throw new MassiveException().addMsg("Could not find player."); PS ps = PS.valueOf(player); - if (value && !EngineFly.canFlyInTerritory(mplayer, ps)) - { - throw new MassiveException().addMsg("You can't fly where you are."); - } + if (value) EngineFly.canFlyInTerritoryOrThrow(mplayer, ps); mplayer.setFlying(value); EngineMassiveCorePlayerUpdate.update(player, false); diff --git a/src/com/massivecraft/factions/cmd/FactionsCommand.java b/src/com/massivecraft/factions/cmd/FactionsCommand.java index a89e7c9b..a3530d07 100644 --- a/src/com/massivecraft/factions/cmd/FactionsCommand.java +++ b/src/com/massivecraft/factions/cmd/FactionsCommand.java @@ -19,7 +19,7 @@ public class FactionsCommand extends MassiveCommand public FactionsCommand() { - this.setSetupEnabled(true); + } // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/engine/EngineFly.java b/src/com/massivecraft/factions/engine/EngineFly.java index 335c3db4..07e89e63 100644 --- a/src/com/massivecraft/factions/engine/EngineFly.java +++ b/src/com/massivecraft/factions/engine/EngineFly.java @@ -1,19 +1,28 @@ package com.massivecraft.factions.engine; +import com.massivecraft.factions.Perm; +import com.massivecraft.factions.cmd.CmdFactions; import com.massivecraft.factions.entity.BoardColl; import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.MConf; import com.massivecraft.factions.entity.MFlag; +import com.massivecraft.factions.entity.MFlagColl; +import com.massivecraft.factions.entity.MPerm; import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.factions.event.EventFactionsFlagChange; import com.massivecraft.massivecore.Engine; +import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.event.EventMassiveCorePlayerUpdate; import com.massivecraft.massivecore.ps.PS; +import com.massivecraft.massivecore.store.DriverFlatfile; import com.massivecraft.massivecore.util.MUtil; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerMoveEvent; +import java.io.File; + public class EngineFly extends Engine { // -------------------------------------------- // @@ -78,15 +87,99 @@ public class EngineFly extends Engine deactivateForPlayer(player); } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void flagUpdate(EventFactionsFlagChange event) + { + if (event.getFlag() != MFlag.getFlagFly()) return; + if (event.isNewValue() == true) return; + + // Disable for all players when disabled + event.getFaction().getOnlinePlayers().forEach(EngineFly::deactivateForPlayer); + } + public static boolean canFlyInTerritory(MPlayer mplayer, PS ps) { + try + { + canFlyInTerritoryOrThrow(mplayer, ps); + return true; + } + catch (MassiveException ex) + { + return false; + } + } + + public static void canFlyInTerritoryOrThrow(MPlayer mplayer, PS ps) throws MassiveException + { + if (!mplayer.isPlayer()) + { + throw new MassiveException().addMsg("Only players can fly"); + } + Faction faction = mplayer.getFaction(); Faction locationFaction = BoardColl.get().getFactionAt(ps.getChunk(true)); - if (faction != locationFaction) return false; - if (!faction.getFlag(MFlag.getFlagFly())) return false; + if (faction != locationFaction) + { + throw new MassiveException().addMsg("You can only fly within your own faction."); + } - return true; + // If the faction does not have the flag ... + if (!faction.getFlag(MFlag.getFlagFly())) + { + MFlag flag = MFlag.getFlagFly(); + MassiveException ex = new MassiveException() + .addMsg("Flying requires that the %s flag is enabled for your faction.", flag.getName()); + + // ... but they can change ... + if (flag.isEditable()) { + boolean canEdit = MPerm.getPermFlags().has(mplayer, faction, false); + // ... and the player can edit it themselves ... + if (canEdit) { + // ... tell them to edit. + ex.addMsg("You can edit the flag with: "); + ex.addMessage(CmdFactions.get().cmdFactionsFlag.cmdFactionsFlagSet.getTemplate(false, true, mplayer.getSender())); + } + // ... otherwise ... + else { + // .. tell them to have someone else edit it ... + ex.addMsg("You can ask a faction admin to change the flag"); + } + } + // ... or only server admins can change it ... + else + { + boolean isAdmin = Perm.OVERRIDE.has(mplayer.getSender()); + boolean isDefault = flag.isDefault(); + if (isAdmin) + { + boolean overriding = mplayer.isOverriding(); + ex.addMsg("You can change the flag if you are overriding."); + if (overriding) ex.addMsg("You are already overriding."); + else + { + ex.addMsg("You can enable override with:"); + ex.addMessage(CmdFactions.get().cmdFactionsOverride.getTemplate(false, true, mplayer.getSender())); + } + + if (!isDefault) + { + ex.addMsg("You can also ask someone with access to the configuration files to make flying enabled by default."); + if (MFlagColl.get().getDb().getDriver() instanceof DriverFlatfile) + { + File file = DriverFlatfile.getDirectory(MFlagColl.get()); + ex.addMsg("Configuring the flags can be done by editing the files in %s.", file.getAbsoluteFile()); + } + } + } + else + { + ex.addMsg("Only server admins can change the flag. Per default flying is %s.", isDefault ? "enabled" : "disabled"); + } + } + throw ex; + } } public static void deactivateForPlayer(Player player)