From d6d5cb325dfbe3736fde5a55954609e10cae35eb Mon Sep 17 00:00:00 2001 From: Benjamin Heusser Date: Fri, 23 Jan 2015 08:16:31 +0100 Subject: [PATCH] Implement the f setpower command --- plugin.yml | 3 + src/com/massivecraft/factions/Perm.java | 219 +++++++++--------- .../factions/cmd/CmdFactions.java | 2 + .../factions/cmd/CmdFactionsSetpower.java | 72 ++++++ .../event/EventFactionsPowerChange.java | 1 + 5 files changed, 188 insertions(+), 109 deletions(-) create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsSetpower.java diff --git a/plugin.yml b/plugin.yml index 0a8773f3..a47ae238 100644 --- a/plugin.yml +++ b/plugin.yml @@ -61,6 +61,7 @@ permissions: factions.seechunk: {description: see the chunk you stand in, default: false} factions.seechunkold: {description: see the chunk you stand in, default: false} factions.sethome: {description: set the faction home, default: false} + factions.setpower: {description: set power, default: false} factions.name: {description: set faction name, default: false} factions.title: {description: set player title, default: false} factions.title.color: {description: set player title with color, default: false} @@ -132,6 +133,7 @@ permissions: factions.seechunk: true factions.seechunkold: true factions.sethome: true + factions.setpower: true factions.name: true factions.title: true factions.title.color: true @@ -165,6 +167,7 @@ permissions: factions.leader.any: true factions.officer.any: true factions.access.any: true + factions.setpower: true factions.kit.rank1: default: false children: diff --git a/src/com/massivecraft/factions/Perm.java b/src/com/massivecraft/factions/Perm.java index 6c76d72f..f23d92d4 100644 --- a/src/com/massivecraft/factions/Perm.java +++ b/src/com/massivecraft/factions/Perm.java @@ -1,109 +1,110 @@ -package com.massivecraft.factions; - -import org.bukkit.permissions.Permissible; - -import com.massivecraft.massivecore.util.PermUtil; - -public enum Perm -{ - // -------------------------------------------- // - // ENUM - // -------------------------------------------- // - - ACCESS("access"), - ACCESS_VIEW("access.view"), - ACCESS_PLAYER("access.player"), - ACCESS_FACTION("access.faction"), - ADMIN("admin"), - CLAIM("claim"), - CLAIM_ONE("claim.one"), - CLAIM_AUTO("claim.auto"), - CLAIM_FILL("claim.fill"), - CLAIM_SQUARE("claim.square"), - CLAIM_CIRCLE("claim.circle"), - CLAIM_ALL("claim.all"), - CREATE("create"), - DESCRIPTION("description"), - DISBAND("disband"), - EXPANSIONS("expansions"), - FACTION("faction"), - 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"), - KICK("kick"), - LEAVE("leave"), - LIST("list"), - MAP("map"), - MONEY("money"), - MONEY_BALANCE("money.balance"), - MONEY_BALANCE_ANY("money.balance.any"), - MONEY_DEPOSIT("money.deposit"), - MONEY_F2F("money.f2f"), - MONEY_F2P("money.f2p"), - MONEY_P2F("money.p2f"), - MONEY_WITHDRAW("money.withdraw"), - MOTD("motd"), - OPEN("open"), - PERM("perm"), - PLAYER("player"), - POWERBOOST("powerboost"), - RANK("rank"), - RANK_SHOW("rank.show"), - RANK_ACTION("rank.action"), - RELATION("relation"), - SEECHUNK("seechunk"), - SEECHUNKOLD("seechunkold"), - SETHOME("sethome"), - NAME("name"), - TITLE("title"), - TITLE_COLOR("title.color"), - UNCLAIM("unclaim"), - UNCLAIM_ONE("unclaim.one"), - UNCLAIM_AUTO("unclaim.auto"), - UNCLAIM_FILL("unclaim.fill"), - UNCLAIM_SQUARE("unclaim.square"), - UNCLAIM_CIRCLE("unclaim.circle"), - UNCLAIM_ALL("unclaim.all"), - UNSETHOME("unsethome"), - VERSION("version"), - - // END OF LIST - ; - - // -------------------------------------------- // - // FIELDS - // -------------------------------------------- // - - public final String node; - - // -------------------------------------------- // - // CONSTRUCT - // -------------------------------------------- // - - Perm(final String node) - { - this.node = "factions."+node; - } - - // -------------------------------------------- // - // HAS - // -------------------------------------------- // - - public boolean has(Permissible permissible, boolean informSenderIfNot) - { - return PermUtil.has(permissible, this.node, informSenderIfNot); - } - - public boolean has(Permissible permissible) - { - return has(permissible, false); - } - -} +package com.massivecraft.factions; + +import org.bukkit.permissions.Permissible; + +import com.massivecraft.massivecore.util.PermUtil; + +public enum Perm +{ + // -------------------------------------------- // + // ENUM + // -------------------------------------------- // + + ACCESS("access"), + ACCESS_VIEW("access.view"), + ACCESS_PLAYER("access.player"), + ACCESS_FACTION("access.faction"), + ADMIN("admin"), + CLAIM("claim"), + CLAIM_ONE("claim.one"), + CLAIM_AUTO("claim.auto"), + CLAIM_FILL("claim.fill"), + CLAIM_SQUARE("claim.square"), + CLAIM_CIRCLE("claim.circle"), + CLAIM_ALL("claim.all"), + CREATE("create"), + DESCRIPTION("description"), + DISBAND("disband"), + EXPANSIONS("expansions"), + FACTION("faction"), + 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"), + KICK("kick"), + LEAVE("leave"), + LIST("list"), + MAP("map"), + MONEY("money"), + MONEY_BALANCE("money.balance"), + MONEY_BALANCE_ANY("money.balance.any"), + MONEY_DEPOSIT("money.deposit"), + MONEY_F2F("money.f2f"), + MONEY_F2P("money.f2p"), + MONEY_P2F("money.p2f"), + MONEY_WITHDRAW("money.withdraw"), + MOTD("motd"), + OPEN("open"), + PERM("perm"), + PLAYER("player"), + POWERBOOST("powerboost"), + RANK("rank"), + RANK_SHOW("rank.show"), + RANK_ACTION("rank.action"), + RELATION("relation"), + SEECHUNK("seechunk"), + SEECHUNKOLD("seechunkold"), + SETHOME("sethome"), + SETPOWER("setpower"), + NAME("name"), + TITLE("title"), + TITLE_COLOR("title.color"), + UNCLAIM("unclaim"), + UNCLAIM_ONE("unclaim.one"), + UNCLAIM_AUTO("unclaim.auto"), + UNCLAIM_FILL("unclaim.fill"), + UNCLAIM_SQUARE("unclaim.square"), + UNCLAIM_CIRCLE("unclaim.circle"), + UNCLAIM_ALL("unclaim.all"), + UNSETHOME("unsethome"), + VERSION("version"), + + // END OF LIST + ; + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + public final String node; + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + Perm(final String node) + { + this.node = "factions."+node; + } + + // -------------------------------------------- // + // HAS + // -------------------------------------------- // + + public boolean has(Permissible permissible, boolean informSenderIfNot) + { + return PermUtil.has(permissible, this.node, informSenderIfNot); + } + + public boolean has(Permissible permissible) + { + return has(permissible, false); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactions.java b/src/com/massivecraft/factions/cmd/CmdFactions.java index de6e7291..2394273c 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactions.java +++ b/src/com/massivecraft/factions/cmd/CmdFactions.java @@ -53,6 +53,7 @@ public class CmdFactions extends FactionsCommand public CmdFactionsAdmin cmdFactionsAdmin = new CmdFactionsAdmin(); public CmdFactionsDisband cmdFactionsDisband = new CmdFactionsDisband(); public CmdFactionsPowerBoost cmdFactionsPowerBoost = new CmdFactionsPowerBoost(); + public CmdFactionsSetpower cmdFactionsSetpower = new CmdFactionsSetpower(); public VersionCommand cmdFactionsVersion = new VersionCommand(Factions.get(), Perm.VERSION.node, "v", "version"); // -------------------------------------------- // @@ -102,6 +103,7 @@ public class CmdFactions extends FactionsCommand this.addSubCommand(this.cmdFactionsAdmin); this.addSubCommand(this.cmdFactionsDisband); this.addSubCommand(this.cmdFactionsPowerBoost); + this.addSubCommand(this.cmdFactionsSetpower); this.addSubCommand(this.cmdFactionsVersion); // Deprecated Commands diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsSetpower.java b/src/com/massivecraft/factions/cmd/CmdFactionsSetpower.java new file mode 100644 index 00000000..b5991754 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsSetpower.java @@ -0,0 +1,72 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.Perm; +import com.massivecraft.factions.cmd.arg.ARMPlayer; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.factions.event.EventFactionsPowerChange; +import com.massivecraft.factions.event.EventFactionsPowerChange.PowerChangeReason; +import com.massivecraft.massivecore.cmd.arg.ARDouble; +import com.massivecraft.massivecore.cmd.req.ReqHasPerm; + +public class CmdFactionsSetpower extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsSetpower() + { + // Aliases + this.addAliases("sp", "setpower"); + + // Args + this.addRequiredArg("player"); + this.addRequiredArg("power"); + + // Requirements + this.addRequirements(ReqHasPerm.get(Perm.SETPOWER.node)); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() + { + // Args + MPlayer mplayer = this.arg(0, ARMPlayer.getAny()); + if (mplayer == null) return; + Double power = this.arg(1, ARDouble.get()); + if (power == null) return; + + // Power + double oldPower = mplayer.getPower(); + double newPower = mplayer.getLimitedPower(power); + + // Detect "no change" + double difference = Math.abs(newPower - oldPower); + double maxDifference = 0.1d; + if (difference < maxDifference) + { + msender.msg("%s's power is already %.2f.", mplayer.getDisplayName(msender), newPower); + return; + } + + // Event + EventFactionsPowerChange event = new EventFactionsPowerChange(sender, mplayer, PowerChangeReason.COMMAND, newPower); + event.run(); + if (event.isCancelled()) return; + + // Inform + msender.msg("You changed %s's power from %.2f to %.2f.", mplayer.getDisplayName(msender), oldPower, newPower); + if (msender != mplayer) + { + mplayer.msg("%s changed your power from %.2f to %.2f.", msender.getDisplayName(mplayer), oldPower, newPower); + } + + // Apply + mplayer.setPower(newPower); + } + +} diff --git a/src/com/massivecraft/factions/event/EventFactionsPowerChange.java b/src/com/massivecraft/factions/event/EventFactionsPowerChange.java index deb8e93d..c790ede5 100644 --- a/src/com/massivecraft/factions/event/EventFactionsPowerChange.java +++ b/src/com/massivecraft/factions/event/EventFactionsPowerChange.java @@ -49,6 +49,7 @@ public class EventFactionsPowerChange extends EventFactionsAbstractSender { TIME, DEATH, + COMMAND, UNDEFINED, ; }