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.

This commit is contained in:
Olof Larsson 2013-04-23 17:01:43 +02:00
parent f802307056
commit 29e05fd54d
9 changed files with 119 additions and 77 deletions

View File

@ -32,6 +32,8 @@ import com.massivecraft.factions.listeners.FactionsListenerEcon;
import com.massivecraft.factions.listeners.FactionsListenerExploit; import com.massivecraft.factions.listeners.FactionsListenerExploit;
import com.massivecraft.factions.listeners.FactionsListenerMain; import com.massivecraft.factions.listeners.FactionsListenerMain;
import com.massivecraft.factions.listeners.TodoFactionsPlayerListener; 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.AutoLeaveTask;
import com.massivecraft.factions.task.EconRewardTask; import com.massivecraft.factions.task.EconRewardTask;
import com.massivecraft.factions.task.PowerUpdateTask; import com.massivecraft.factions.task.PowerUpdateTask;
@ -73,6 +75,11 @@ public class Factions extends MPlugin
// Database Initialized // Database Initialized
private boolean databaseInitialized; private boolean databaseInitialized;
public boolean isDatabaseInitialized() { return this.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 // OVERRIDE

View File

@ -1,20 +1,14 @@
package com.massivecraft.factions.entity; package com.massivecraft.factions.entity;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.permissions.PermissionDefault;
import com.massivecraft.factions.listeners.FactionsListenerChat; import com.massivecraft.factions.listeners.FactionsListenerChat;
import com.massivecraft.mcore.store.Entity; import com.massivecraft.mcore.store.Entity;
import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.util.PermUtil;
import com.massivecraft.mcore.util.TimeUnit; import com.massivecraft.mcore.util.TimeUnit;
public class MConf extends Entity<MConf> public class MConf extends Entity<MConf>
@ -35,7 +29,6 @@ public class MConf extends Entity<MConf>
{ {
super.load(that); super.load(that);
this.upsertPowerPerms();
FactionsListenerChat.get().setup(); FactionsListenerChat.get().setup();
return this; return this;
@ -47,55 +40,6 @@ public class MConf extends Entity<MConf>
public long powerTaskMillis = TimeUnit.MILLIS_PER_MINUTE; public long powerTaskMillis = TimeUnit.MILLIS_PER_MINUTE;
public Map<String, Double> 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<String, Double> 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<String, Double> 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<String, Double> 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<String> names = new ArrayList<String>();
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 // CHAT
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -51,6 +51,15 @@ public class UConf extends Entity<UConf>
public Map<FFlag, Boolean> defaultFactionFlags = FFlag.getDefaultDefaults(); public Map<FFlag, Boolean> defaultFactionFlags = FFlag.getDefaultDefaults();
public Map<FPerm, Set<Rel>> defaultFactionPerms = FPerm.getDefaultDefaults(); public Map<FPerm, Set<Rel>> 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 // DENY COMMANDS
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -289,6 +289,42 @@ public class UPlayer extends SenderEntity<UPlayer> implements EconomyParticipato
// FIELD: power // 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 // RAW
public double getDefaultPower() public double getDefaultPower()
@ -300,12 +336,14 @@ public class UPlayer extends SenderEntity<UPlayer> implements EconomyParticipato
{ {
Double ret = this.power; Double ret = this.power;
if (ret == null) ret = this.getDefaultPower(); if (ret == null) ret = this.getDefaultPower();
ret = this.getLimitedPower(ret);
return ret; return ret;
} }
public void setPower(Double power) public void setPower(Double power)
{ {
if (power == null || MUtil.equals(power, this.getDefaultPower())) power = null; if (power == null || MUtil.equals(power, this.getDefaultPower())) power = null;
power = this.getLimitedPower(power);
this.power = power; this.power = power;
this.changed(); this.changed();
} }

View File

@ -38,7 +38,7 @@ public class FactionsEventPowerChange extends FactionsEventAbstractSender
super(sender); super(sender);
this.uplayer = uplayer; this.uplayer = uplayer;
this.reason = reason; this.reason = reason;
this.newPower = newPower; this.newPower = uplayer.getLimitedPower(newPower);
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -60,7 +60,6 @@ import com.massivecraft.factions.event.FactionsEventPowerChange.PowerChangeReaso
import com.massivecraft.factions.util.VisualizeUtil; import com.massivecraft.factions.util.VisualizeUtil;
import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.util.MUtil; import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.util.PermUtil;
import com.massivecraft.mcore.util.Txt; import com.massivecraft.mcore.util.Txt;
public class FactionsListenerMain implements Listener public class FactionsListenerMain implements Listener
@ -109,14 +108,7 @@ public class FactionsListenerMain implements Listener
} }
// ... alter the power ... // ... alter the power ...
double maxPower = PermUtil.pickFirstVal(player, MConf.get().permToPowerMax); double newPower = uplayer.getPower() + uplayer.getPowerPerDeath();
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);
FactionsEventPowerChange powerChangeEvent = new FactionsEventPowerChange(null, uplayer, PowerChangeReason.DEATH, newPower); FactionsEventPowerChange powerChangeEvent = new FactionsEventPowerChange(null, uplayer, PowerChangeReason.DEATH, newPower);
powerChangeEvent.run(); powerChangeEvent.run();
@ -127,7 +119,7 @@ public class FactionsListenerMain implements Listener
// ... and inform the player. // ... and inform the player.
// TODO: A progress bar here would be epic :) // TODO: A progress bar here would be epic :)
uplayer.msg("<i>Your power is now <h>%.2f / %.2f", newPower, maxPower); uplayer.msg("<i>Your power is now <h>%.2f / %.2f", newPower, uplayer.getPowerMax());
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -8,7 +8,6 @@ import com.massivecraft.factions.entity.UPlayer;
import com.massivecraft.factions.event.FactionsEventPowerChange; import com.massivecraft.factions.event.FactionsEventPowerChange;
import com.massivecraft.factions.event.FactionsEventPowerChange.PowerChangeReason; import com.massivecraft.factions.event.FactionsEventPowerChange.PowerChangeReason;
import com.massivecraft.mcore.ModuloRepeatTask; import com.massivecraft.mcore.ModuloRepeatTask;
import com.massivecraft.mcore.util.PermUtil;
import com.massivecraft.mcore.util.TimeUnit; import com.massivecraft.mcore.util.TimeUnit;
public class PowerUpdateTask extends ModuloRepeatTask public class PowerUpdateTask extends ModuloRepeatTask
@ -45,16 +44,8 @@ public class PowerUpdateTask extends ModuloRepeatTask
{ {
if (player.isDead()) continue; 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); UPlayer uplayer = UPlayer.get(player);
double oldPower = uplayer.getPower(); double newPower = uplayer.getPower() + uplayer.getPowerPerHour() * millis / TimeUnit.MILLIS_PER_HOUR;
double newPower = oldPower + hourPower * millis / TimeUnit.MILLIS_PER_HOUR;
newPower = Math.max(newPower, minPower);
newPower = Math.min(newPower, maxPower);
FactionsEventPowerChange event = new FactionsEventPowerChange(null, uplayer, PowerChangeReason.TIME, newPower); FactionsEventPowerChange event = new FactionsEventPowerChange(null, uplayer, PowerChangeReason.TIME, newPower);
event.run(); event.run();