From 29e05fd54ddcb813e9ef8d2c802bee77951b92ee Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 23 Apr 2013 17:01:43 +0200 Subject: [PATCH] Introducing a power Mixin since Bukkit does not allow permissions for offline players and we want to know for offline players. Servers wanting individual power rules can implement and inject this mixin. --- src/com/massivecraft/factions/Factions.java | 7 +++ .../massivecraft/factions/entity/MConf.java | 56 ------------------- .../massivecraft/factions/entity/UConf.java | 9 +++ .../massivecraft/factions/entity/UPlayer.java | 38 +++++++++++++ .../event/FactionsEventPowerChange.java | 2 +- .../listeners/FactionsListenerMain.java | 12 +--- .../factions/mixin/PowerMixin.java | 12 ++++ .../factions/mixin/PowerMixinDefault.java | 49 ++++++++++++++++ .../factions/task/PowerUpdateTask.java | 11 +--- 9 files changed, 119 insertions(+), 77 deletions(-) create mode 100644 src/com/massivecraft/factions/mixin/PowerMixin.java create mode 100644 src/com/massivecraft/factions/mixin/PowerMixinDefault.java diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 258bfc74..1bcfc324 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -32,6 +32,8 @@ import com.massivecraft.factions.listeners.FactionsListenerEcon; import com.massivecraft.factions.listeners.FactionsListenerExploit; import com.massivecraft.factions.listeners.FactionsListenerMain; import com.massivecraft.factions.listeners.TodoFactionsPlayerListener; +import com.massivecraft.factions.mixin.PowerMixin; +import com.massivecraft.factions.mixin.PowerMixinDefault; import com.massivecraft.factions.task.AutoLeaveTask; import com.massivecraft.factions.task.EconRewardTask; import com.massivecraft.factions.task.PowerUpdateTask; @@ -73,6 +75,11 @@ public class Factions extends MPlugin // Database Initialized private boolean databaseInitialized; public boolean isDatabaseInitialized() { return this.databaseInitialized; } + + // Mixins + private PowerMixin powerMixin = null; + public PowerMixin getPowerMixin() { return this.powerMixin == null ? PowerMixinDefault.get() : this.powerMixin; } + public void setPowerMixin(PowerMixin powerMixin) { this.powerMixin = powerMixin; } // -------------------------------------------- // // OVERRIDE diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index d5bb7ac8..7fe72db0 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -1,20 +1,14 @@ package com.massivecraft.factions.entity; -import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; import java.util.Set; import org.bukkit.ChatColor; import org.bukkit.event.EventPriority; -import org.bukkit.permissions.PermissionDefault; import com.massivecraft.factions.listeners.FactionsListenerChat; import com.massivecraft.mcore.store.Entity; -import com.massivecraft.mcore.util.MUtil; -import com.massivecraft.mcore.util.PermUtil; import com.massivecraft.mcore.util.TimeUnit; public class MConf extends Entity @@ -35,7 +29,6 @@ public class MConf extends Entity { super.load(that); - this.upsertPowerPerms(); FactionsListenerChat.get().setup(); return this; @@ -47,55 +40,6 @@ public class MConf extends Entity public long powerTaskMillis = TimeUnit.MILLIS_PER_MINUTE; - public Map permToPowerMax = MUtil.map( - "factions.power.max.50", 50.0, - "factions.power.max.40", 40.0, - "factions.power.max.30", 30.0, - "factions.power.max.20", 20.0, - "factions.power.max.10", 10.0, - "factions.power.max.default", 10.0 - ); - - public Map permToPowerMin = MUtil.map( - "factions.power.min.50", 50.0, - "factions.power.min.40", 40.0, - "factions.power.min.30", 30.0, - "factions.power.min.20", 20.0, - "factions.power.min.10", 10.0, - "factions.power.min.default", 10.0 - ); - - public Map permToPowerHour = MUtil.map( - "factions.power.hour.50", 50.0, - "factions.power.hour.40", 40.0, - "factions.power.hour.30", 30.0, - "factions.power.hour.20", 20.0, - "factions.power.hour.10", 10.0, - "factions.power.hour.4", 4.0, - "factions.power.hour.2", 2.0, - "factions.power.hour.default", 2.0 - ); - - public Map permToPowerDeath = MUtil.map( - "factions.power.death.0", 0.0, - "factions.power.death.-2", -2.0, - "factions.power.death.default", -2.0 - ); - - public void upsertPowerPerms() - { - List names = new ArrayList(); - names.addAll(this.permToPowerMax.keySet()); - names.addAll(this.permToPowerMin.keySet()); - names.addAll(this.permToPowerHour.keySet()); - names.addAll(this.permToPowerDeath.keySet()); - - for (String name : names) - { - PermUtil.get(true, true, name, name, PermissionDefault.FALSE); - } - } - // -------------------------------------------- // // CHAT // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/UConf.java b/src/com/massivecraft/factions/entity/UConf.java index 7f36f606..54aa6495 100644 --- a/src/com/massivecraft/factions/entity/UConf.java +++ b/src/com/massivecraft/factions/entity/UConf.java @@ -51,6 +51,15 @@ public class UConf extends Entity public Map defaultFactionFlags = FFlag.getDefaultDefaults(); public Map> defaultFactionPerms = FPerm.getDefaultDefaults(); + // -------------------------------------------- // + // POWER + // -------------------------------------------- // + + public double powerMax = 10.0; + public double powerMin = 0.0; + public double powerPerHour = 2.0; + public double powerPerDeath = -2.0; + // -------------------------------------------- // // DENY COMMANDS // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/UPlayer.java b/src/com/massivecraft/factions/entity/UPlayer.java index 283e1620..b731824f 100644 --- a/src/com/massivecraft/factions/entity/UPlayer.java +++ b/src/com/massivecraft/factions/entity/UPlayer.java @@ -289,6 +289,42 @@ public class UPlayer extends SenderEntity implements EconomyParticipato // FIELD: power // -------------------------------------------- // + // MIXIN: RAW + + public double getPowerMaxUniversal() + { + return Factions.get().getPowerMixin().getMaxUniversal(this); + } + + public double getPowerMax() + { + return Factions.get().getPowerMixin().getMax(this); + } + + public double getPowerMin() + { + return Factions.get().getPowerMixin().getMin(this); + } + + public double getPowerPerHour() + { + return Factions.get().getPowerMixin().getPerHour(this); + } + + public double getPowerPerDeath() + { + return Factions.get().getPowerMixin().getPerDeath(this); + } + + // MIXIN: FINER + + public double getLimitedPower(double power) + { + power = Math.max(power, this.getPowerMin()); + power = Math.min(power, this.getPowerMax()); + return power; + } + // RAW public double getDefaultPower() @@ -300,12 +336,14 @@ public class UPlayer extends SenderEntity implements EconomyParticipato { Double ret = this.power; if (ret == null) ret = this.getDefaultPower(); + ret = this.getLimitedPower(ret); return ret; } public void setPower(Double power) { if (power == null || MUtil.equals(power, this.getDefaultPower())) power = null; + power = this.getLimitedPower(power); this.power = power; this.changed(); } diff --git a/src/com/massivecraft/factions/event/FactionsEventPowerChange.java b/src/com/massivecraft/factions/event/FactionsEventPowerChange.java index 7bc7e63f..59380a2e 100644 --- a/src/com/massivecraft/factions/event/FactionsEventPowerChange.java +++ b/src/com/massivecraft/factions/event/FactionsEventPowerChange.java @@ -38,7 +38,7 @@ public class FactionsEventPowerChange extends FactionsEventAbstractSender super(sender); this.uplayer = uplayer; this.reason = reason; - this.newPower = newPower; + this.newPower = uplayer.getLimitedPower(newPower); } // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java index 75cc683a..4891ecc9 100644 --- a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java +++ b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java @@ -60,7 +60,6 @@ import com.massivecraft.factions.event.FactionsEventPowerChange.PowerChangeReaso import com.massivecraft.factions.util.VisualizeUtil; import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.util.MUtil; -import com.massivecraft.mcore.util.PermUtil; import com.massivecraft.mcore.util.Txt; public class FactionsListenerMain implements Listener @@ -109,14 +108,7 @@ public class FactionsListenerMain implements Listener } // ... alter the power ... - double maxPower = PermUtil.pickFirstVal(player, MConf.get().permToPowerMax); - double minPower = PermUtil.pickFirstVal(player, MConf.get().permToPowerMin); - double deathPower = PermUtil.pickFirstVal(player, MConf.get().permToPowerDeath); - double oldPower = uplayer.getPower(); - - double newPower = oldPower + deathPower; - newPower = Math.max(newPower, minPower); - newPower = Math.min(newPower, maxPower); + double newPower = uplayer.getPower() + uplayer.getPowerPerDeath(); FactionsEventPowerChange powerChangeEvent = new FactionsEventPowerChange(null, uplayer, PowerChangeReason.DEATH, newPower); powerChangeEvent.run(); @@ -127,7 +119,7 @@ public class FactionsListenerMain implements Listener // ... and inform the player. // TODO: A progress bar here would be epic :) - uplayer.msg("Your power is now %.2f / %.2f", newPower, maxPower); + uplayer.msg("Your power is now %.2f / %.2f", newPower, uplayer.getPowerMax()); } // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/mixin/PowerMixin.java b/src/com/massivecraft/factions/mixin/PowerMixin.java new file mode 100644 index 00000000..4f8b96c0 --- /dev/null +++ b/src/com/massivecraft/factions/mixin/PowerMixin.java @@ -0,0 +1,12 @@ +package com.massivecraft.factions.mixin; + +import com.massivecraft.factions.entity.UPlayer; + +public interface PowerMixin +{ + public double getMaxUniversal(UPlayer uplayer); + public double getMax(UPlayer uplayer); + public double getMin(UPlayer uplayer); + public double getPerHour(UPlayer uplayer); + public double getPerDeath(UPlayer uplayer); +} diff --git a/src/com/massivecraft/factions/mixin/PowerMixinDefault.java b/src/com/massivecraft/factions/mixin/PowerMixinDefault.java new file mode 100644 index 00000000..03ab7ead --- /dev/null +++ b/src/com/massivecraft/factions/mixin/PowerMixinDefault.java @@ -0,0 +1,49 @@ +package com.massivecraft.factions.mixin; + +import com.massivecraft.factions.entity.UConf; +import com.massivecraft.factions.entity.UPlayer; + +public class PowerMixinDefault implements PowerMixin +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static PowerMixinDefault i = new PowerMixinDefault(); + public static PowerMixinDefault get() { return i; } + + // -------------------------------------------- // + // OVERRIDE: PowerMixin + // -------------------------------------------- // + + @Override + public double getMaxUniversal(UPlayer uplayer) + { + return this.getMax(uplayer); + } + + @Override + public double getMax(UPlayer uplayer) + { + return UConf.get(uplayer).powerMax; + } + + @Override + public double getMin(UPlayer uplayer) + { + return UConf.get(uplayer).powerMin; + } + + @Override + public double getPerHour(UPlayer uplayer) + { + return UConf.get(uplayer).powerPerHour; + } + + @Override + public double getPerDeath(UPlayer uplayer) + { + return UConf.get(uplayer).powerPerDeath; + } + +} diff --git a/src/com/massivecraft/factions/task/PowerUpdateTask.java b/src/com/massivecraft/factions/task/PowerUpdateTask.java index d8ac5357..279de0e8 100644 --- a/src/com/massivecraft/factions/task/PowerUpdateTask.java +++ b/src/com/massivecraft/factions/task/PowerUpdateTask.java @@ -8,7 +8,6 @@ import com.massivecraft.factions.entity.UPlayer; import com.massivecraft.factions.event.FactionsEventPowerChange; import com.massivecraft.factions.event.FactionsEventPowerChange.PowerChangeReason; import com.massivecraft.mcore.ModuloRepeatTask; -import com.massivecraft.mcore.util.PermUtil; import com.massivecraft.mcore.util.TimeUnit; public class PowerUpdateTask extends ModuloRepeatTask @@ -45,16 +44,8 @@ public class PowerUpdateTask extends ModuloRepeatTask { if (player.isDead()) continue; - double maxPower = PermUtil.pickFirstVal(player, MConf.get().permToPowerMax); - double minPower = PermUtil.pickFirstVal(player, MConf.get().permToPowerMin); - double hourPower = PermUtil.pickFirstVal(player, MConf.get().permToPowerHour); - UPlayer uplayer = UPlayer.get(player); - double oldPower = uplayer.getPower(); - - double newPower = oldPower + hourPower * millis / TimeUnit.MILLIS_PER_HOUR; - newPower = Math.max(newPower, minPower); - newPower = Math.min(newPower, maxPower); + double newPower = uplayer.getPower() + uplayer.getPowerPerHour() * millis / TimeUnit.MILLIS_PER_HOUR; FactionsEventPowerChange event = new FactionsEventPowerChange(null, uplayer, PowerChangeReason.TIME, newPower); event.run();