From 486f8ac957a4a6917058fe799fef9cb5b46a2057 Mon Sep 17 00:00:00 2001 From: Benjamin Heusser Date: Tue, 13 Jan 2015 13:50:38 +0100 Subject: [PATCH] Rework the /f invite command to be a parentcommand. --- plugin.yml | 14 ++- src/com/massivecraft/factions/Perm.java | 6 +- .../factions/cmd/CmdFactionsInvite.java | 75 +++-------- .../factions/cmd/CmdFactionsInviteAdd.java | 82 ++++++++++++ .../factions/cmd/CmdFactionsInviteList.java | 85 +++++++++++++ .../factions/cmd/CmdFactionsInviteRemove.java | 117 ++++++++++++++++++ .../massivecraft/factions/entity/Faction.java | 12 ++ 7 files changed, 328 insertions(+), 63 deletions(-) create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsInviteAdd.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsInviteList.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsInviteRemove.java diff --git a/plugin.yml b/plugin.yml index 3f97d410..0a8773f3 100644 --- a/plugin.yml +++ b/plugin.yml @@ -29,7 +29,11 @@ permissions: factions.faction: {description: show faction information, default: false} factions.flag: {description: manage faction flags, default: false} factions.home: {description: teleport to faction home, default: false} - factions.invite: {description: set if player is invited, default: false} + factions.invite: {description: manage invites, default: false} + factions.invite.list: {description: list invited players, default: false} + factions.invite.list.other: {description: list invited players of another factions, default: false} + factions.invite.add: {description: invite player, default: false} + factions.invite.remove: {description: revoke an invite, default: false} factions.join: {description: join faction, default: false} factions.join.any: {description: join closed faction, default: false} factions.join.others: {description: have another player join faction, default: false} @@ -95,6 +99,10 @@ permissions: factions.flag: true factions.home: true factions.invite: true + factions.invite.list: true + factions.invite.list.other: true + factions.invite.add: true + factions.invite.remove: true factions.join: true factions.join.any: true factions.join.others: true @@ -162,6 +170,7 @@ permissions: children: factions.kit.rank0: true factions.admin: true + factions.invite.list.other: true factions.kit.rank0: default: false children: @@ -185,6 +194,9 @@ permissions: factions.flag: true factions.home: true factions.invite: true + factions.invite.list: true + factions.invite.add: true + factions.invite.remove: true factions.join: true factions.kick: true factions.leader: true diff --git a/src/com/massivecraft/factions/Perm.java b/src/com/massivecraft/factions/Perm.java index d32bce41..6c76d72f 100644 --- a/src/com/massivecraft/factions/Perm.java +++ b/src/com/massivecraft/factions/Perm.java @@ -30,6 +30,10 @@ public enum Perm FLAG("flag"), HOME("home"), INVITE("invite"), + INVITE_LIST("invite.list"), + INVITE_LIST_OTHER("invite.list.other"), + INVITE_ADD("invite.add"), + INVITE_REMOVE("invite.remove"), JOIN("join"), JOIN_ANY("join.any"), JOIN_OTHERS("join.others"), @@ -52,7 +56,7 @@ public enum Perm POWERBOOST("powerboost"), RANK("rank"), RANK_SHOW("rank.show"), - RANK_ACTION("rank.action"), + RANK_ACTION("rank.action"), RELATION("relation"), SEECHUNK("seechunk"), SEECHUNKOLD("seechunkold"), diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsInvite.java b/src/com/massivecraft/factions/cmd/CmdFactionsInvite.java index 9ab9990d..25255ef5 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsInvite.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsInvite.java @@ -1,18 +1,18 @@ package com.massivecraft.factions.cmd; -import com.massivecraft.factions.Factions; import com.massivecraft.factions.Perm; -import com.massivecraft.factions.cmd.arg.ARMPlayer; -import com.massivecraft.factions.cmd.req.ReqHasFaction; -import com.massivecraft.factions.entity.MPerm; -import com.massivecraft.factions.entity.MPlayer; -import com.massivecraft.factions.event.EventFactionsInvitedChange; -import com.massivecraft.massivecore.cmd.arg.ARBoolean; import com.massivecraft.massivecore.cmd.req.ReqHasPerm; -import com.massivecraft.massivecore.cmd.req.ReqIsPlayer; public class CmdFactionsInvite extends FactionsCommand { + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + public CmdFactionsInviteList cmdFactionsInviteList = new CmdFactionsInviteList(); + public CmdFactionsInviteAdd cmdFactionsInviteAdd = new CmdFactionsInviteAdd(); + public CmdFactionsInviteRemove cmdFactionsInviteRemove = new CmdFactionsInviteRemove(); + // -------------------------------------------- // // CONSTRUCT // -------------------------------------------- // @@ -20,63 +20,16 @@ public class CmdFactionsInvite extends FactionsCommand public CmdFactionsInvite() { // Aliases - this.addAliases("inv", "invite"); + this.addAliases("i", "inv", "invite"); + + // Add SubCommands + this.addSubCommand(this.cmdFactionsInviteAdd); + this.addSubCommand(this.cmdFactionsInviteRemove); + this.addSubCommand(this.cmdFactionsInviteList); - // Args - this.addRequiredArg("player"); - this.addOptionalArg("yes/no", "toggle"); // Requirements this.addRequirements(ReqHasPerm.get(Perm.INVITE.node)); - this.addRequirements(ReqHasFaction.get()); - this.addRequirements(ReqIsPlayer.get()); - } - - // -------------------------------------------- // - // OVERRIDE - // -------------------------------------------- // - - @Override - public void perform() - { - // Args - MPlayer mplayer = this.arg(0, ARMPlayer.getAny()); - if (mplayer == null) return; - - Boolean newInvited = this.arg(1, ARBoolean.get(), !msenderFaction.isInvited(mplayer)); - if (newInvited == null) return; - - // Allready member? - if (mplayer.getFaction() == msenderFaction) - { - msg("%s is already a member of %s", mplayer.getName(), msenderFaction.getName()); - msg("You might want to: " + Factions.get().getOuterCmdFactions().cmdFactionsKick.getUseageTemplate(false)); - return; - } - - // MPerm - if ( ! MPerm.getPermInvite().has(msender, msenderFaction, true)) return; - - // Event - EventFactionsInvitedChange event = new EventFactionsInvitedChange(sender, mplayer, msenderFaction, newInvited); - event.run(); - if (event.isCancelled()) return; - newInvited = event.isNewInvited(); - - // Apply - msenderFaction.setInvited(mplayer, newInvited); - - // Inform - if (newInvited) - { - mplayer.msg("%s invited you to %s", msender.describeTo(mplayer, true), msenderFaction.describeTo(mplayer)); - msenderFaction.msg("%s invited %s to your faction.", msender.describeTo(msenderFaction, true), mplayer.describeTo(msenderFaction)); - } - else - { - mplayer.msg("%s revoked your invitation to %s.", msender.describeTo(mplayer), msenderFaction.describeTo(mplayer)); - msenderFaction.msg("%s revoked %s's invitation.", msender.describeTo(msenderFaction), mplayer.describeTo(msenderFaction)); - } } } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsInviteAdd.java b/src/com/massivecraft/factions/cmd/CmdFactionsInviteAdd.java new file mode 100644 index 00000000..f9e9c26c --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsInviteAdd.java @@ -0,0 +1,82 @@ +package com.massivecraft.factions.cmd; + +import java.util.Set; + +import com.massivecraft.factions.Factions; +import com.massivecraft.factions.Perm; +import com.massivecraft.factions.cmd.arg.ARMPlayer; +import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.factions.event.EventFactionsInvitedChange; +import com.massivecraft.massivecore.cmd.arg.ARSet; +import com.massivecraft.massivecore.cmd.req.ReqHasPerm; + +public class CmdFactionsInviteAdd extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + public CmdFactionsInviteAdd() + { + // Aliases + this.addAliases("a", "add"); + + // Args + this.addRequiredArg("players"); + this.setErrorOnToManyArgs(false); + + // Requirements + this.addRequirements(ReqHasPerm.get(Perm.INVITE_ADD.node)); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() + { + // Args + Set mplayers = this.argConcatFrom(0, ARSet.get(ARMPlayer.getAny(), true)); + if (mplayers == null) return; + + // MPerm + if ( ! MPerm.getPermInvite().has(msender, msenderFaction, true)) return; + + for (MPlayer mplayer : mplayers) + { + // Already member? + if (mplayer.getFaction() == msenderFaction) + { + msg("%s is already a member of %s.", mplayer.getName(), msenderFaction.getName()); + continue; + } + + // Already invited? + boolean isInvited = msenderFaction.isInvited(mplayer); + + if ( ! isInvited) + { + // Event + EventFactionsInvitedChange event = new EventFactionsInvitedChange(sender, mplayer, msenderFaction, isInvited); + event.run(); + if (event.isCancelled()) continue; + isInvited = event.isNewInvited(); + + // Inform + mplayer.msg("%s invited you to %s.", msender.describeTo(mplayer, true), msenderFaction.describeTo(mplayer)); + msenderFaction.msg("%s invited %s to your faction.", msender.describeTo(msenderFaction, true), mplayer.describeTo(msenderFaction)); + + // Apply + msenderFaction.setInvited(mplayer, true); + } + else + { + // Inform + msg("%s is already invited to %s.", mplayer.getName(), msenderFaction.getName()); + msg("You might want to: " + Factions.get().getOuterCmdFactions().cmdFactionsInvite.cmdFactionsInviteRemove.getUseageTemplate(false)); + } + } + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsInviteList.java b/src/com/massivecraft/factions/cmd/CmdFactionsInviteList.java new file mode 100644 index 00000000..1021f2df --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsInviteList.java @@ -0,0 +1,85 @@ +package com.massivecraft.factions.cmd; + +import java.util.List; + +import com.massivecraft.factions.Perm; +import com.massivecraft.factions.Rel; +import com.massivecraft.factions.cmd.arg.ARFaction; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.massivecore.cmd.arg.ARInteger; +import com.massivecraft.massivecore.cmd.req.ReqHasPerm; +import com.massivecraft.massivecore.pager.PagerSimple; +import com.massivecraft.massivecore.pager.Stringifier; +import com.massivecraft.massivecore.util.Txt; + +public class CmdFactionsInviteList extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsInviteList() + { + // Aliases + this.addAliases("l", "list"); + + // Args + this.addOptionalArg("page", "1"); + this.addOptionalArg("faction", "you"); + + // Requirements + this.addRequirements(ReqHasPerm.get(Perm.INVITE_LIST.node)); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() + { + // Args + Integer pageHumanBased = this.arg(0, ARInteger.get(), 1); + if (pageHumanBased == null) return; + + Faction faction = this.arg(1, ARFaction.get(), msenderFaction); + if (faction == null) return; + + if ( faction != msenderFaction && ! Perm.INVITE_LIST_OTHER.has(sender, true)) return; + + // MPerm + if ( ! MPerm.getPermInvite().has(msender, msenderFaction, true)) return; + + // Create Pager + final List mplayers = faction.getInvitedMPlayers(); + final PagerSimple pager = new PagerSimple(mplayers, sender); + + // Use Pager + List messages = pager.getPageTxt(pageHumanBased, "Invited Players List", new Stringifier(){ + + public String toString(MPlayer target) + { + // TODO: Madus would like to implement this in MPlayer + String targetName = target.getDisplayName(msender); + String isAre = target == msender ? "are" : "is"; + Rel targetRank = target.getRole(); + Faction targetFaction = target.getFaction(); + String theAan = targetRank == Rel.LEADER ? "the" : Txt.aan(targetRank.name()); + String rankName = Txt.getNicedEnum(targetRank).toLowerCase(); + String ofIn = targetRank == Rel.LEADER ? "of" : "in"; + String factionName = targetFaction.describeTo(msender, true); + if (targetFaction == msenderFaction) + { + factionName = factionName.toLowerCase(); + } + return Txt.parse("%s %s %s %s %s %s.", targetName, isAre, theAan, rankName, ofIn, factionName); + } + }); + + // Send message + sendMessage(messages); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsInviteRemove.java b/src/com/massivecraft/factions/cmd/CmdFactionsInviteRemove.java new file mode 100644 index 00000000..a27450f7 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsInviteRemove.java @@ -0,0 +1,117 @@ +package com.massivecraft.factions.cmd; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.massivecraft.factions.Factions; +import com.massivecraft.factions.Perm; +import com.massivecraft.factions.cmd.arg.ARMPlayer; +import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.factions.event.EventFactionsInvitedChange; +import com.massivecraft.massivecore.cmd.arg.ARSet; +import com.massivecraft.massivecore.cmd.arg.ARString; +import com.massivecraft.massivecore.cmd.req.ReqHasPerm; + +public class CmdFactionsInviteRemove extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + public CmdFactionsInviteRemove() + { + // Aliases + this.addAliases("r", "remove"); + + // Args + this.addRequiredArg("players/all"); + this.setErrorOnToManyArgs(false); + + // Requirements + this.addRequirements(ReqHasPerm.get(Perm.INVITE_REMOVE.node)); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() + { + Set mplayers = new HashSet(); + boolean all = false; + + // Args + if (this.arg(0, ARString.get()).equalsIgnoreCase("all")) + { + List invitedPlayers = msenderFaction.getInvitedMPlayers(); + // Doesn't show up if list is empty. Test at home if it worked. + if (invitedPlayers == null || invitedPlayers.isEmpty()) + { + msg("Your faction has not invited anyone."); + return; + } + all = true; + mplayers.addAll(invitedPlayers); + } + else + { + Set senderInput = this.argConcatFrom(0, ARSet.get(ARMPlayer.getAny(), true)); + if (senderInput == null) return; + + mplayers.addAll(senderInput); + } + + // MPerm + if ( ! MPerm.getPermInvite().has(msender, msenderFaction, true)) return; + + for (MPlayer mplayer : mplayers) + { + // Already member? + if (mplayer.getFaction() == msenderFaction) + { + msg("%s is already a member of %s.", mplayer.getName(), msenderFaction.getName()); + msg("You might want to: " + Factions.get().getOuterCmdFactions().cmdFactionsKick.getUseageTemplate(false)); + continue; + } + + // Already invited? + boolean isInvited = msenderFaction.isInvited(mplayer); + + if (isInvited) + { + // Event + EventFactionsInvitedChange event = new EventFactionsInvitedChange(sender, mplayer, msenderFaction, isInvited); + event.run(); + if (event.isCancelled()) continue; + isInvited = event.isNewInvited(); + + // Inform Player + mplayer.msg("%s revoked your invitation to %s.", msender.describeTo(mplayer, true), msenderFaction.describeTo(mplayer)); + + // Inform Faction + if ( ! all) + { + msenderFaction.msg("%s revoked %s's invitation.", msender.describeTo(msenderFaction), mplayer.describeTo(msenderFaction)); + } + + // Apply + msenderFaction.setInvited(mplayer, false); + } + else + { + // Inform + msg("%s is not invited to %s.", mplayer.describeTo(msender, true), msenderFaction.describeTo(mplayer)); + msg("You might want to: " + Factions.get().getOuterCmdFactions().cmdFactionsInvite.cmdFactionsInviteAdd.getUseageTemplate(false)); + } + } + + // Inform Faction if all + if (all) + { + msenderFaction.msg("%s revoked all %s pending invitations from your faction.", msender.describeTo(msenderFaction), mplayers.size()); + } + } + +} diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index cf3a66d0..b0207c22 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -482,6 +482,18 @@ public class Faction extends Entity implements EconomyParticipator this.setInvited(mplayer.getId(), invited); } + public List getInvitedMPlayers() + { + List mplayers = new ArrayList(); + + for (String id : this.getInvitedPlayerIds()) + { + MPlayer mplayer = MPlayer.get(id); + mplayers.add(mplayer); + } + return mplayers; + } + // -------------------------------------------- // // FIELD: relationWish // -------------------------------------------- //