diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index dd20b280..f2a25c44 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -7,8 +7,9 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.entity.Player; -import com.massivecraft.factions.event.FactionsEventLeave; import com.massivecraft.factions.event.FactionsEventLandClaim; +import com.massivecraft.factions.event.FactionsEventMembershipChange; +import com.massivecraft.factions.event.FactionsEventMembershipChange.MembershipChangeReason; import com.massivecraft.factions.iface.EconomyParticipator; import com.massivecraft.factions.iface.RelationParticipator; import com.massivecraft.factions.integration.Econ; @@ -674,17 +675,10 @@ public class FPlayer extends SenderEntity implements EconomyParticipato public void leave(boolean makePay) { Faction myFaction = this.getFaction(); - makePay = makePay && Econ.isEnabled() && ! this.isUsingAdminMode(); - if (myFaction == null) - { - resetFactionData(); - return; - } - - boolean perm = myFaction.getFlag(FFlag.PERMANENT); + boolean permanent = myFaction.getFlag(FFlag.PERMANENT); - if (!perm && this.getRole() == Rel.LEADER && myFaction.getFPlayers().size() > 1) + if (!permanent && this.getRole() == Rel.LEADER && myFaction.getFPlayers().size() > 1) { msg("You must give the admin role to someone else first."); return; @@ -696,15 +690,10 @@ public class FPlayer extends SenderEntity implements EconomyParticipato return; } - // if economy is enabled and they're not on the bypass list, make sure they can pay - if (makePay && ! Econ.hasAtLeast(this, ConfServer.econCostLeave, "to leave your faction.")) return; - - FactionsEventLeave leaveEvent = new FactionsEventLeave(sender, this, myFaction, FactionsEventLeave.PlayerLeaveReason.LEAVE); - leaveEvent.run(); - if (leaveEvent.isCancelled()) return; - - // then make 'em pay (if applicable) - if (makePay && ! Econ.modifyMoney(this, -ConfServer.econCostLeave, "leave your faction")) return; + // Event + FactionsEventMembershipChange membershipChangeEvent = new FactionsEventMembershipChange(sender, this, myFaction, MembershipChangeReason.LEAVE); + membershipChangeEvent.run(); + if (membershipChangeEvent.isCancelled()) return; // Am I the last one in the faction? if (myFaction.getFPlayers().size() == 1) @@ -727,7 +716,7 @@ public class FPlayer extends SenderEntity implements EconomyParticipato this.resetFactionData(); - if (myFaction.isNormal() && !perm && myFaction.getFPlayers().isEmpty()) + if (myFaction.isNormal() && !permanent && myFaction.getFPlayers().isEmpty()) { // Remove this faction for (FPlayer fplayer : FPlayerColl.get().getAllOnline()) diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsCreate.java b/src/com/massivecraft/factions/cmd/CmdFactionsCreate.java index 9018b296..ce8d1726 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsCreate.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsCreate.java @@ -10,8 +10,9 @@ import com.massivecraft.factions.FactionColl; import com.massivecraft.factions.Factions; import com.massivecraft.factions.Perm; import com.massivecraft.factions.Rel; -import com.massivecraft.factions.event.FactionsEventJoin; import com.massivecraft.factions.event.FactionsEventCreate; +import com.massivecraft.factions.event.FactionsEventMembershipChange; +import com.massivecraft.factions.event.FactionsEventMembershipChange.MembershipChangeReason; import com.massivecraft.mcore.cmd.req.ReqHasPerm; public class CmdFactionsCreate extends FCommand @@ -66,7 +67,7 @@ public class CmdFactionsCreate extends FCommand fme.setRole(Rel.LEADER); fme.setFaction(faction); - FactionsEventJoin joinEvent = new FactionsEventJoin(sender, fme, faction, FactionsEventJoin.PlayerJoinReason.CREATE); + FactionsEventMembershipChange joinEvent = new FactionsEventMembershipChange(sender, fme, faction, MembershipChangeReason.CREATE); joinEvent.run(); // NOTE: join event cannot be cancelled or you'll have an empty faction diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsDisband.java b/src/com/massivecraft/factions/cmd/CmdFactionsDisband.java index 6a45bd03..518b98fb 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsDisband.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsDisband.java @@ -2,13 +2,15 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.ConfServer; import com.massivecraft.factions.cmd.arg.ARFaction; -import com.massivecraft.factions.event.FactionsEventLeave; import com.massivecraft.factions.event.FactionsEventDisband; +import com.massivecraft.factions.event.FactionsEventMembershipChange; +import com.massivecraft.factions.event.FactionsEventMembershipChange.MembershipChangeReason; import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.FFlag; import com.massivecraft.factions.FPerm; import com.massivecraft.factions.FPlayerColl; import com.massivecraft.factions.Faction; +import com.massivecraft.factions.FactionColl; import com.massivecraft.factions.Factions; import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.Perm; @@ -51,10 +53,10 @@ public class CmdFactionsDisband extends FCommand // Merged Apply and Inform // Send FPlayerLeaveEvent for each player in the faction - for ( FPlayer fplayer : faction.getFPlayers() ) + for (FPlayer fplayer : faction.getFPlayers()) { - FactionsEventLeave leaveEvent = new FactionsEventLeave(sender, fplayer, faction, FactionsEventLeave.PlayerLeaveReason.DISBAND); - leaveEvent.run(); + FactionsEventMembershipChange membershipChangeEvent = new FactionsEventMembershipChange(sender, fplayer, FactionColl.get().getNone(), MembershipChangeReason.DISBAND); + membershipChangeEvent.run(); } // Inform all players diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsJoin.java b/src/com/massivecraft/factions/cmd/CmdFactionsJoin.java index dfc77775..ebf2ab83 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsJoin.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsJoin.java @@ -1,16 +1,14 @@ package com.massivecraft.factions.cmd; -import org.bukkit.Bukkit; - import com.massivecraft.factions.ConfServer; import com.massivecraft.factions.FPlayer; -import com.massivecraft.factions.FPlayerColl; import com.massivecraft.factions.Faction; import com.massivecraft.factions.Factions; import com.massivecraft.factions.Perm; import com.massivecraft.factions.cmd.arg.ARFPlayer; import com.massivecraft.factions.cmd.arg.ARFaction; -import com.massivecraft.factions.event.FactionsEventJoin; +import com.massivecraft.factions.event.FactionsEventMembershipChange; +import com.massivecraft.factions.event.FactionsEventMembershipChange.MembershipChangeReason; import com.massivecraft.mcore.cmd.req.ReqHasPerm; public class CmdFactionsJoin extends FCommand @@ -28,6 +26,7 @@ public class CmdFactionsJoin extends FCommand @Override public void perform() { + // Args Faction faction = this.arg(0, ARFaction.get()); if (faction == null) return; @@ -35,7 +34,8 @@ public class CmdFactionsJoin extends FCommand if (fplayer == null) return; boolean samePlayer = fplayer == fme; - + + // Validate if (!samePlayer && ! Perm.JOIN_OTHERS.has(sender, false)) { msg("You do not have permission to move other players into a faction."); @@ -70,30 +70,33 @@ public class CmdFactionsJoin extends FCommand { msg("This faction requires invitation."); if (samePlayer) + { faction.msg("%s tried to join your faction.", fplayer.describeTo(faction, true)); + } return; } - // trigger the join event (cancellable) - FactionsEventJoin joinEvent = new FactionsEventJoin(FPlayerColl.get().get(me),faction,FactionsEventJoin.PlayerJoinReason.JOIN); - Bukkit.getServer().getPluginManager().callEvent(joinEvent); - if (joinEvent.isCancelled()) return; - - // then make 'em pay (if applicable) - if (samePlayer && ! payForCommand(ConfServer.econCostJoin)) return; - + // Event + FactionsEventMembershipChange membershipChangeEvent = new FactionsEventMembershipChange(sender, fme, faction, MembershipChangeReason.JOIN); + membershipChangeEvent.run(); + if (membershipChangeEvent.isCancelled()) return; + + // Inform if (!samePlayer) + { fplayer.msg("%s moved you into the faction %s.", fme.describeTo(fplayer, true), faction.getTag(fplayer)); + } faction.msg("%s joined your faction.", fplayer.describeTo(faction, true)); fme.msg("%s successfully joined %s.", fplayer.describeTo(fme, true), faction.getTag(fme)); + // Apply fplayer.resetFactionData(); fplayer.setFaction(faction); fme.setRole(ConfServer.factionRankDefault); // They have just joined a faction, start them out on the lowest rank (default config). - faction.deinvite(fplayer); - + faction.setInvited(fplayer, false); + // Derplog if (ConfServer.logFactionJoin) { if (samePlayer) diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsKick.java b/src/com/massivecraft/factions/cmd/CmdFactionsKick.java index 3ff09743..0c5a7be7 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsKick.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsKick.java @@ -1,16 +1,16 @@ package com.massivecraft.factions.cmd; -import org.bukkit.Bukkit; - import com.massivecraft.factions.ConfServer; import com.massivecraft.factions.FPerm; import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.Faction; +import com.massivecraft.factions.FactionColl; import com.massivecraft.factions.Factions; import com.massivecraft.factions.Perm; import com.massivecraft.factions.Rel; import com.massivecraft.factions.cmd.arg.ARFPlayer; -import com.massivecraft.factions.event.FactionsEventLeave; +import com.massivecraft.factions.event.FactionsEventMembershipChange; +import com.massivecraft.factions.event.FactionsEventMembershipChange.MembershipChangeReason; import com.massivecraft.mcore.cmd.req.ReqHasPerm; public class CmdFactionsKick extends FCommand @@ -27,56 +27,60 @@ public class CmdFactionsKick extends FCommand @Override public void perform() - { - FPlayer you = this.arg(1, ARFPlayer.getStartAny()); - if (you == null) return; + { + // Arg + FPlayer fplayer = this.arg(1, ARFPlayer.getStartAny()); + if (fplayer == null) return; - if (fme == you) + // Validate + if (fme == fplayer) { msg("You cannot kick yourself."); msg("You might want to: %s", Factions.get().getOuterCmdFactions().cmdFactionsLeave.getUseageTemplate(false)); return; } - if (you.getRole() == Rel.LEADER && !(this.senderIsConsole || fme.isUsingAdminMode())) + if (fplayer.getRole() == Rel.LEADER && !(this.senderIsConsole || fme.isUsingAdminMode())) { msg("The leader can not be kicked."); return; } - if ( ! ConfServer.canLeaveWithNegativePower && you.getPower() < 0) + if ( ! ConfServer.canLeaveWithNegativePower && fplayer.getPower() < 0) { msg("You cannot kick that member until their power is positive."); return; } - Faction yourFaction = you.getFaction(); + // FPerm + Faction fplayerFaction = fplayer.getFaction(); + if (!FPerm.KICK.has(sender, fplayerFaction)) return; - if (fme != null && ! FPerm.KICK.has(fme, yourFaction)) return; - - // trigger the leave event (cancellable) [reason:kicked] - FactionsEventLeave event = new FactionsEventLeave(you, you.getFaction(), FactionsEventLeave.PlayerLeaveReason.KICKED); - Bukkit.getServer().getPluginManager().callEvent(event); + // Event + FactionsEventMembershipChange event = new FactionsEventMembershipChange(sender, fplayer, FactionColl.get().getNone(), MembershipChangeReason.KICK); + event.run(); if (event.isCancelled()) return; - // then make 'em pay (if applicable) - if (!payForCommand(ConfServer.econCostKick)) return; - - yourFaction.msg("%s kicked %s from the faction! :O", fme.describeTo(yourFaction, true), you.describeTo(yourFaction, true)); - you.msg("%s kicked you from %s! :O", fme.describeTo(you, true), yourFaction.describeTo(you)); - if (yourFaction != myFaction) + // Inform + fplayerFaction.msg("%s kicked %s from the faction! :O", fme.describeTo(fplayerFaction, true), fplayer.describeTo(fplayerFaction, true)); + fplayer.msg("%s kicked you from %s! :O", fme.describeTo(fplayer, true), fplayerFaction.describeTo(fplayer)); + if (fplayerFaction != myFaction) { - fme.msg("You kicked %s from the faction %s!", you.describeTo(fme), yourFaction.describeTo(fme)); + fme.msg("You kicked %s from the faction %s!", fplayer.describeTo(fme), fplayerFaction.describeTo(fme)); } if (ConfServer.logFactionKick) - Factions.get().log((senderIsConsole ? "A console command" : fme.getName())+" kicked "+you.getName()+" from the faction: "+yourFaction.getTag()); + { + Factions.get().log((senderIsConsole ? "A console command" : fme.getName())+" kicked "+fplayer.getName()+" from the faction: "+fplayerFaction.getTag()); + } - if (you.getRole() == Rel.LEADER) - yourFaction.promoteNewLeader(); - - yourFaction.deinvite(you); - you.resetFactionData(); + // Apply + if (fplayer.getRole() == Rel.LEADER) + { + fplayerFaction.promoteNewLeader(); + } + fplayerFaction.setInvited(fplayer, false); + fplayer.resetFactionData(); } } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsLeader.java b/src/com/massivecraft/factions/cmd/CmdFactionsLeader.java index bc6b43ef..3fa6e416 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsLeader.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsLeader.java @@ -7,7 +7,8 @@ import com.massivecraft.factions.Perm; import com.massivecraft.factions.Rel; import com.massivecraft.factions.cmd.arg.ARFPlayer; import com.massivecraft.factions.cmd.arg.ARFaction; -import com.massivecraft.factions.event.FactionsEventJoin; +import com.massivecraft.factions.event.FactionsEventMembershipChange; +import com.massivecraft.factions.event.FactionsEventMembershipChange.MembershipChangeReason; import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.mcore.cmd.req.ReqHasPerm; import com.massivecraft.mcore.util.Txt; @@ -65,7 +66,7 @@ public class CmdFactionsLeader extends FCommand // only perform a FPlayerJoinEvent when newLeader isn't actually in the faction if (newLeader.getFaction() != targetFaction) { - FactionsEventJoin event = new FactionsEventJoin(sender, newLeader, targetFaction, FactionsEventJoin.PlayerJoinReason.LEADER); + FactionsEventMembershipChange event = new FactionsEventMembershipChange(sender, newLeader, targetFaction, MembershipChangeReason.LEADER); event.run(); if (event.isCancelled()) return; } diff --git a/src/com/massivecraft/factions/cmd/FCommand.java b/src/com/massivecraft/factions/cmd/FCommand.java index 47506b60..4f21b107 100644 --- a/src/com/massivecraft/factions/cmd/FCommand.java +++ b/src/com/massivecraft/factions/cmd/FCommand.java @@ -58,10 +58,4 @@ public abstract class FCommand extends MCommand return false; } - - // if economy is enabled and they're not on the bypass list, make 'em pay; returns true unless person can't afford the cost - /*public boolean payForCommand(double cost) - { - return Econ.payForAction(cost, sender, this.getDesc()); - }*/ } diff --git a/src/com/massivecraft/factions/event/FactionsEventJoin.java b/src/com/massivecraft/factions/event/FactionsEventJoin.java deleted file mode 100644 index fe29e17b..00000000 --- a/src/com/massivecraft/factions/event/FactionsEventJoin.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.massivecraft.factions.event; - -import org.bukkit.command.CommandSender; -import org.bukkit.event.HandlerList; - -import com.massivecraft.factions.FPlayer; -import com.massivecraft.factions.Faction; - -public class FactionsEventJoin extends FactionsEventAbstractSender -{ - // -------------------------------------------- // - // REQUIRED EVENT CODE - // -------------------------------------------- // - - private static final HandlerList handlers = new HandlerList(); - @Override public HandlerList getHandlers() { return handlers; } - public static HandlerList getHandlerList() { return handlers; } - - // -------------------------------------------- // - // FIELDS - // -------------------------------------------- // - - private final FPlayer fplayer; - public FPlayer getFPlayer() { return this.fplayer; } - - private final Faction faction; - public Faction getFaction() { return this.faction; } - - private final PlayerJoinReason reason; - public PlayerJoinReason getReason() { return reason; } - - // -------------------------------------------- // - // CONSTRUCT - // -------------------------------------------- // - - public FactionsEventJoin(CommandSender sender, FPlayer fplayer, Faction faction, PlayerJoinReason reason) - { - super(sender); - this.fplayer = fplayer; - this.faction = faction; - this.reason = reason; - } - - // -------------------------------------------- // - // INTERNAL ENUM - // -------------------------------------------- // - - public enum PlayerJoinReason - { - CREATE, LEADER, JOIN - } - -} \ No newline at end of file diff --git a/src/com/massivecraft/factions/event/FactionsEventLeave.java b/src/com/massivecraft/factions/event/FactionsEventMembershipChange.java similarity index 55% rename from src/com/massivecraft/factions/event/FactionsEventLeave.java rename to src/com/massivecraft/factions/event/FactionsEventMembershipChange.java index e345f3db..96e88f74 100644 --- a/src/com/massivecraft/factions/event/FactionsEventLeave.java +++ b/src/com/massivecraft/factions/event/FactionsEventMembershipChange.java @@ -6,7 +6,7 @@ import org.bukkit.event.HandlerList; import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.Faction; -public class FactionsEventLeave extends FactionsEventAbstractSender +public class FactionsEventMembershipChange extends FactionsEventAbstractSender { // -------------------------------------------- // // REQUIRED EVENT CODE @@ -23,31 +23,28 @@ public class FactionsEventLeave extends FactionsEventAbstractSender @Override public void setCancelled(boolean cancelled) { - if (this.reason == PlayerLeaveReason.DISBAND || this.reason == PlayerLeaveReason.RESET) - { - cancelled = false; - } + if (!this.reason.isCancellable()) cancelled = false; super.setCancelled(cancelled); } private final FPlayer fplayer; public FPlayer getFPlayer() { return this.fplayer; } - private final Faction faction; - public Faction getFaction() { return this.faction; } + private final Faction newFaction; + public Faction getNewFaction() { return this.newFaction; } - private final PlayerLeaveReason reason; - public PlayerLeaveReason getReason() { return this.reason; } + private final MembershipChangeReason reason; + public MembershipChangeReason getReason() { return this.reason; } // -------------------------------------------- // // CONSTRUCT // -------------------------------------------- // - public FactionsEventLeave(CommandSender sender, FPlayer fplayer, Faction faction, PlayerLeaveReason reason) + public FactionsEventMembershipChange(CommandSender sender, FPlayer fplayer, Faction newFaction, MembershipChangeReason reason) { super(sender); this.fplayer = fplayer; - this.faction = faction; + this.newFaction = newFaction; this.reason = reason; } @@ -55,9 +52,28 @@ public class FactionsEventLeave extends FactionsEventAbstractSender // INTERNAL ENUM // -------------------------------------------- // - public enum PlayerLeaveReason + public enum MembershipChangeReason { - KICKED, DISBAND, RESET, JOINOTHER, LEAVE + // Join + JOIN (true), + CREATE (false), + LEADER (true), + + // Leave + LEAVE (true), + //JOINOTHER (true), + KICK (true), + DISBAND (false), + //RESET (false), + ; + + private final boolean cancellable; + public boolean isCancellable() { return this.cancellable; } + + private MembershipChangeReason(boolean cancellable) + { + this.cancellable = cancellable; + } } } \ No newline at end of file diff --git a/src/com/massivecraft/factions/listeners/FactionsListenerEcon.java b/src/com/massivecraft/factions/listeners/FactionsListenerEcon.java index f778fe51..b8f49265 100644 --- a/src/com/massivecraft/factions/listeners/FactionsListenerEcon.java +++ b/src/com/massivecraft/factions/listeners/FactionsListenerEcon.java @@ -13,6 +13,8 @@ import com.massivecraft.factions.event.FactionsEventDescriptionChange; import com.massivecraft.factions.event.FactionsEventHomeChange; import com.massivecraft.factions.event.FactionsEventHomeTeleport; import com.massivecraft.factions.event.FactionsEventInvitedChange; +import com.massivecraft.factions.event.FactionsEventMembershipChange; +import com.massivecraft.factions.event.FactionsEventMembershipChange.MembershipChangeReason; import com.massivecraft.factions.event.FactionsEventOpenChange; import com.massivecraft.factions.event.FactionsEventRelationChange; import com.massivecraft.factions.event.FactionsEventTagChange; @@ -110,4 +112,32 @@ public class FactionsListenerEcon implements Listener payForCommand(event, ConfServer.econCostHome, Factions.get().getOuterCmdFactions().cmdFactionsHome); } + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void payForCommand(FactionsEventMembershipChange event) + { + Double cost = null; + MCommand command = null; + + if (event.getReason() == MembershipChangeReason.JOIN) + { + cost = ConfServer.econCostJoin; + command = Factions.get().getOuterCmdFactions().cmdFactionsJoin; + } + else if (event.getReason() == MembershipChangeReason.LEAVE) + { + cost = ConfServer.econCostLeave; + command = Factions.get().getOuterCmdFactions().cmdFactionsLeave; + } + else if (event.getReason() == MembershipChangeReason.KICK) + { + cost = ConfServer.econCostKick; + command = Factions.get().getOuterCmdFactions().cmdFactionsKick; + } + else + { + return; + } + + payForCommand(event, cost, command); + } }