From 9a2066efccd43dc53500af50d67e6730f4badcb2 Mon Sep 17 00:00:00 2001 From: ulumulu1510 Date: Wed, 15 Mar 2017 18:22:17 +0100 Subject: [PATCH] Implement interface Powerboosted. --- plugin.yml | 13 +++- .../factions/FactionsParticipator.java | 7 ++ src/com/massivecraft/factions/Perm.java | 3 + .../massivecraft/factions/PowerBoosted.java | 7 ++ .../factions/cmd/CmdFactionsPowerBoost.java | 66 ++-------------- .../cmd/CmdFactionsPowerBoostAbstract.java | 76 +++++++++++++++++++ .../cmd/CmdFactionsPowerBoostFaction.java | 16 ++++ .../cmd/CmdFactionsPowerBoostPlayer.java | 16 ++++ .../massivecraft/factions/entity/Faction.java | 30 ++++---- .../massivecraft/factions/entity/MPlayer.java | 8 +- 10 files changed, 160 insertions(+), 82 deletions(-) create mode 100644 src/com/massivecraft/factions/FactionsParticipator.java create mode 100644 src/com/massivecraft/factions/PowerBoosted.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsPowerBoostAbstract.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsPowerBoostFaction.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsPowerBoostPlayer.java diff --git a/plugin.yml b/plugin.yml index d0bc69fa..d0deca9a 100644 --- a/plugin.yml +++ b/plugin.yml @@ -59,7 +59,10 @@ permissions: factions.perm.set: {description: set perms, default: false} factions.perm.show: {description: show perms, default: false} factions.player: {description: show player information} - factions.powerboost: {description: set powerboost, default: false} + factions.powerboost: {description: manage powerboost, default: false} + factions.powerboost.faction: {description: show faction powerboost, default: false} + factions.powerboost.player: {description: show player powerboost, default: false} + factions.powerboost.set: {description: set powerboost, default: false} factions.rank: {description: manage/show ranks, default: false} factions.rank.show: {description: show rank, default: false} factions.rank.action: {description: change rank, default: false} @@ -147,6 +150,9 @@ permissions: factions.perm.show: true factions.player: true factions.powerboost: true + factions.powerboost.faction: true + factions.powerboost.player: true + factions.powerboost.set: true factions.promote: true factions.relation: true factions.relation.list: true @@ -188,7 +194,7 @@ permissions: default: false children: factions.kit.rank1: true - factions.powerboost: true + factions.powerboost.set: true factions.join.others: true factions.leader.any: true factions.officer.any: true @@ -253,6 +259,9 @@ permissions: factions.perm.show: true factions.player: true factions.promote: true + factions.powerboost: true + factions.powerboost.faction: true + factions.powerboost.player: true factions.rank: true factions.rank.show: true factions.rank.action: true diff --git a/src/com/massivecraft/factions/FactionsParticipator.java b/src/com/massivecraft/factions/FactionsParticipator.java new file mode 100644 index 00000000..90998e51 --- /dev/null +++ b/src/com/massivecraft/factions/FactionsParticipator.java @@ -0,0 +1,7 @@ +package com.massivecraft.factions; + +import com.massivecraft.massivecore.Named; + +public interface FactionsParticipator extends Named, EconomyParticipator, PowerBoosted +{ +} diff --git a/src/com/massivecraft/factions/Perm.java b/src/com/massivecraft/factions/Perm.java index edc336aa..372aca93 100644 --- a/src/com/massivecraft/factions/Perm.java +++ b/src/com/massivecraft/factions/Perm.java @@ -61,6 +61,9 @@ public enum Perm implements Identified PERM_SHOW, PLAYER, POWERBOOST, + POWERBOOST_PLAYER, + POWERBOOST_FACTION, + POWERBOOST_SET, RANK, RANK_SHOW, RANK_ACTION, diff --git a/src/com/massivecraft/factions/PowerBoosted.java b/src/com/massivecraft/factions/PowerBoosted.java new file mode 100644 index 00000000..58aaf1b1 --- /dev/null +++ b/src/com/massivecraft/factions/PowerBoosted.java @@ -0,0 +1,7 @@ +package com.massivecraft.factions; + +public interface PowerBoosted +{ + public double getPowerBoost(); + public void setPowerBoost(Double powerBoost); +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoost.java b/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoost.java index ba0c418e..1f057632 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoost.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoost.java @@ -1,23 +1,13 @@ package com.massivecraft.factions.cmd; -import com.massivecraft.factions.Factions; -import com.massivecraft.factions.cmd.type.TypeFaction; -import com.massivecraft.factions.cmd.type.TypeMPlayer; -import com.massivecraft.factions.entity.Faction; -import com.massivecraft.factions.entity.MPlayer; -import com.massivecraft.massivecore.MassiveException; -import com.massivecraft.massivecore.command.Parameter; -import com.massivecraft.massivecore.command.type.primitive.TypeDouble; -import com.massivecraft.massivecore.command.type.primitive.TypeString; - public class CmdFactionsPowerBoost extends FactionsCommand { // -------------------------------------------- // // FIELDS // -------------------------------------------- // - private Parameter parameterMplayer = new Parameter(TypeMPlayer.get(), "name"); - private Parameter parameterFaction = new Parameter(TypeFaction.get(), "name"); + public CmdFactionsPowerBoostPlayer cmdFactionsPowerBoostPlayer = new CmdFactionsPowerBoostPlayer(); + public CmdFactionsPowerBoostFaction cmdFactionsPowerBoostFaction = new CmdFactionsPowerBoostFaction(); // -------------------------------------------- // // CONSTRUCT @@ -25,55 +15,9 @@ public class CmdFactionsPowerBoost extends FactionsCommand public CmdFactionsPowerBoost() { - // Parameters - this.addParameter(TypeString.get(), "p|f|player|faction"); - this.addParameter(parameterMplayer); - this.addParameter(TypeDouble.get(), "#"); - } - - // -------------------------------------------- // - // OVERRIDE - // -------------------------------------------- // - - @Override - public void perform() throws MassiveException - { - String type = this.readArg().toLowerCase(); - boolean doPlayer = true; - if (type.equals("f") || type.equals("faction")) - { - doPlayer = false; - } - else if (!type.equals("p") && !type.equals("player")) - { - msg("You must specify \"p\" or \"player\" to target a player or \"f\" or \"faction\" to target a faction."); - msg("ex. /f powerboost p SomePlayer 0.5 -or- /f powerboost f SomeFaction -5"); - return; - } - - double targetPower = this.readArgAt(2); - - String target; - - if (doPlayer) - { - this.getParameters().set(1, parameterMplayer); - MPlayer targetPlayer = this.readArgAt(1); - - targetPlayer.setPowerBoost(targetPower); - target = "Player \""+targetPlayer.getName()+"\""; - } - else - { - this.getParameters().set(1, parameterFaction); - Faction targetFaction = this.readArgAt(1); - - targetFaction.setPowerBoost(targetPower); - target = "Faction \""+targetFaction.getName()+"\""; - } - - msg(""+target+" now has a power bonus/penalty of "+targetPower+" to min and max power levels."); - Factions.get().log(msender.getName()+" has set the power bonus/penalty for "+target+" to "+targetPower+"."); + // Child + this.addChild(this.cmdFactionsPowerBoostPlayer); + this.addChild(this.cmdFactionsPowerBoostFaction); } } diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoostAbstract.java b/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoostAbstract.java new file mode 100644 index 00000000..df536599 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoostAbstract.java @@ -0,0 +1,76 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.Factions; +import com.massivecraft.factions.FactionsParticipator; +import com.massivecraft.factions.Perm; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.command.type.Type; +import com.massivecraft.massivecore.command.type.TypeNullable; +import com.massivecraft.massivecore.command.type.primitive.TypeDouble; +import com.massivecraft.massivecore.util.Txt; + +public abstract class CmdFactionsPowerBoostAbstract extends FactionsCommand +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + protected CmdFactionsPowerBoostAbstract(Type parameterType, String parameterName) + { + // Parameters + this.addParameter(parameterType, parameterName); + this.addParameter(TypeNullable.get(TypeDouble.get()), "amount", "show"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Parameters + FactionsParticipator factionsParticipator = this.readArg(); + Double powerBoost = this.readArg(factionsParticipator.getPowerBoost()); + + // Try set the powerBoost + boolean updated = this.trySet(factionsParticipator, powerBoost); + + // Inform + this.informPowerBoost(factionsParticipator, powerBoost, updated); + } + + private boolean trySet(FactionsParticipator factionsParticipator, Double powerBoost) throws MassiveException + { + // Trying to set? + if (!this.argIsSet(1)) return false; + + // Check set permissions + if (!Perm.POWERBOOST_SET.has(sender, true)) throw new MassiveException(); + + // Set + factionsParticipator.setPowerBoost(powerBoost); + + // Return + return true; + } + + private void informPowerBoost(FactionsParticipator factionsParticipator, Double powerBoost, boolean updated) + { + // Prepare + String participatorDescribe = factionsParticipator.describeTo(msender, true); + powerBoost = powerBoost == null ? factionsParticipator.getPowerBoost() : powerBoost; + String powerDescription = Txt.parse(Double.compare(powerBoost, 0D) >= 0 ? "bonus" : "penalty"); + String when = updated ? "now " : ""; + String verb = factionsParticipator.equals(msender) ? "have" : "has"; + + // Create message + String messagePlayer = Txt.parse("%s %s%s a power %s of %.2f to min and max power levels.", participatorDescribe, when, verb, powerDescription, powerBoost); + String messageLog = Txt.parse("%s %s set the power %s for %s to %.2f.", msender.getName(), verb, powerDescription, factionsParticipator.getName(), powerBoost); + + // Inform + msender.message(messagePlayer); + if (updated) Factions.get().log(messageLog); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoostFaction.java b/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoostFaction.java new file mode 100644 index 00000000..623f1bf0 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoostFaction.java @@ -0,0 +1,16 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.cmd.type.TypeFaction; + +public class CmdFactionsPowerBoostFaction extends CmdFactionsPowerBoostAbstract +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsPowerBoostFaction() + { + super(TypeFaction.get(), "faction"); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoostPlayer.java b/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoostPlayer.java new file mode 100644 index 00000000..b12073a5 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsPowerBoostPlayer.java @@ -0,0 +1,16 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.cmd.type.TypeMPlayer; + +public class CmdFactionsPowerBoostPlayer extends CmdFactionsPowerBoostAbstract +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsPowerBoostPlayer() + { + super(TypeMPlayer.get(), "player"); + } + +} diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index d7586e10..8e7ba15d 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -17,16 +17,15 @@ import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import com.massivecraft.factions.EconomyParticipator; import com.massivecraft.factions.FactionEqualsPredicate; import com.massivecraft.factions.Factions; +import com.massivecraft.factions.FactionsParticipator; import com.massivecraft.factions.Lang; import com.massivecraft.factions.PredicateRole; import com.massivecraft.factions.Rel; import com.massivecraft.factions.RelationParticipator; import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.util.RelationUtil; -import com.massivecraft.massivecore.Named; import com.massivecraft.massivecore.collections.MassiveList; import com.massivecraft.massivecore.collections.MassiveMapDef; import com.massivecraft.massivecore.collections.MassiveSet; @@ -44,7 +43,7 @@ import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.Txt; -public class Faction extends Entity implements EconomyParticipator, Named +public class Faction extends Entity implements FactionsParticipator { // -------------------------------------------- // // META @@ -401,7 +400,7 @@ public class Faction extends Entity implements EconomyParticipator, Nam // -------------------------------------------- // // RAW - + @Override public double getPowerBoost() { Double ret = this.powerBoost; @@ -409,6 +408,7 @@ public class Faction extends Entity implements EconomyParticipator, Nam return ret; } + @Override public void setPowerBoost(Double powerBoost) { // Clean input @@ -942,12 +942,7 @@ public class Faction extends Entity implements EconomyParticipator, Nam ret += mplayer.getPower(); } - double factionPowerMax = MConf.get().factionPowerMax; - if (factionPowerMax > 0 && ret > factionPowerMax) - { - ret = factionPowerMax; - } - + ret = this.limitWithPowerMax(ret); ret += this.getPowerBoost(); return ret; @@ -963,17 +958,20 @@ public class Faction extends Entity implements EconomyParticipator, Nam ret += mplayer.getPowerMax(); } - double factionPowerMax = MConf.get().factionPowerMax; - if (factionPowerMax > 0 && ret > factionPowerMax) - { - ret = factionPowerMax; - } - + ret = this.limitWithPowerMax(ret); ret += this.getPowerBoost(); return ret; } + private double limitWithPowerMax(double power) + { + // NOTE: 0.0 powerMax means there is no max power + double powerMax = MConf.get().factionPowerMax; + + return powerMax <= 0 || power < powerMax ? power : powerMax; + } + public int getPowerRounded() { return (int) Math.round(this.getPower()); diff --git a/src/com/massivecraft/factions/entity/MPlayer.java b/src/com/massivecraft/factions/entity/MPlayer.java index b5b0b438..d08c7467 100644 --- a/src/com/massivecraft/factions/entity/MPlayer.java +++ b/src/com/massivecraft/factions/entity/MPlayer.java @@ -10,8 +10,8 @@ import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import com.massivecraft.factions.EconomyParticipator; import com.massivecraft.factions.Factions; +import com.massivecraft.factions.FactionsParticipator; import com.massivecraft.factions.Lang; import com.massivecraft.factions.Perm; import com.massivecraft.factions.Rel; @@ -20,8 +20,8 @@ import com.massivecraft.factions.event.EventFactionsChunkChangeType; import com.massivecraft.factions.event.EventFactionsChunksChange; import com.massivecraft.factions.event.EventFactionsDisband; import com.massivecraft.factions.event.EventFactionsMembershipChange; -import com.massivecraft.factions.event.EventFactionsRemovePlayerMillis; import com.massivecraft.factions.event.EventFactionsMembershipChange.MembershipChangeReason; +import com.massivecraft.factions.event.EventFactionsRemovePlayerMillis; import com.massivecraft.factions.mixin.PowerMixin; import com.massivecraft.factions.util.RelationUtil; import com.massivecraft.massivecore.mixin.MixinSenderPs; @@ -34,7 +34,7 @@ import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName; -public class MPlayer extends SenderEntity implements EconomyParticipator +public class MPlayer extends SenderEntity implements FactionsParticipator { // -------------------------------------------- // // META @@ -393,6 +393,7 @@ public class MPlayer extends SenderEntity implements EconomyParticipato // FIELD: powerBoost // -------------------------------------------- // + @Override public double getPowerBoost() { Double ret = this.powerBoost; @@ -400,6 +401,7 @@ public class MPlayer extends SenderEntity implements EconomyParticipato return ret; } + @Override public void setPowerBoost(Double powerBoost) { // Clean input