From afe944d3c49f5243ed7bbc18b21a7afeacdea8fc Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 19 Apr 2013 14:39:44 +0200 Subject: [PATCH] For some reason I like ...Change events. The power loss event is now converted to a power change event. --- src/com/massivecraft/factions/FPlayer.java | 7 +++ .../event/FactionsEventPowerChange.java | 56 +++++++++++++++++++ .../event/FactionsEventPowerLoss.java | 27 --------- .../listeners/FactionsListenerMain.java | 15 +++-- 4 files changed, 72 insertions(+), 33 deletions(-) create mode 100644 src/com/massivecraft/factions/event/FactionsEventPowerChange.java delete mode 100644 src/com/massivecraft/factions/event/FactionsEventPowerLoss.java diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index 4648f238..966af9a5 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -9,6 +9,8 @@ import org.bukkit.entity.Player; import com.massivecraft.factions.event.FactionsEventLandClaim; import com.massivecraft.factions.event.FactionsEventMembershipChange; import com.massivecraft.factions.event.FactionsEventMembershipChange.MembershipChangeReason; +import com.massivecraft.factions.event.FactionsEventPowerChange; +import com.massivecraft.factions.event.FactionsEventPowerChange.PowerChangeReason; import com.massivecraft.factions.iface.EconomyParticipator; import com.massivecraft.factions.iface.RelationParticipator; import com.massivecraft.factions.integration.Econ; @@ -470,6 +472,11 @@ public class FPlayer extends SenderEntity implements EconomyParticipato } } + FactionsEventPowerChange event = new FactionsEventPowerChange(null, this, PowerChangeReason.TIME, powerTarget); + event.run(); + if (event.isCancelled()) return; + powerTarget = event.getNewPower(); + this.setPower(powerTarget, now); } diff --git a/src/com/massivecraft/factions/event/FactionsEventPowerChange.java b/src/com/massivecraft/factions/event/FactionsEventPowerChange.java new file mode 100644 index 00000000..cf3f46f9 --- /dev/null +++ b/src/com/massivecraft/factions/event/FactionsEventPowerChange.java @@ -0,0 +1,56 @@ +package com.massivecraft.factions.event; + +import org.bukkit.command.CommandSender; +import org.bukkit.event.HandlerList; + +import com.massivecraft.factions.FPlayer; + +public class FactionsEventPowerChange extends FactionsEventAbstractSender +{ + // -------------------------------------------- // + // REQUIRED EVENT CODE + // -------------------------------------------- // + + private static final HandlerList handlers = new HandlerList(); + @Override public HandlerList getHandlers() { return handlers; } + public static HandlerList getHandlerList() { return handlers; } + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private final FPlayer fplayer; + public FPlayer getFPlayer() { return this.fplayer; } + + private final PowerChangeReason reason; + public PowerChangeReason getReason() { return this.reason; } + + private double newPower; + public double getNewPower() { return this.newPower; } + public void setNewPower(double newPower) { this.newPower = newPower; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public FactionsEventPowerChange(CommandSender sender, FPlayer fplayer, PowerChangeReason reason, double newPower) + { + super(sender); + this.fplayer = fplayer; + this.reason = reason; + this.newPower = newPower; + } + + // -------------------------------------------- // + // REASON ENUM + // -------------------------------------------- // + + public enum PowerChangeReason + { + TIME, + DEATH, + UNDEFINED, + ; + } + +} diff --git a/src/com/massivecraft/factions/event/FactionsEventPowerLoss.java b/src/com/massivecraft/factions/event/FactionsEventPowerLoss.java deleted file mode 100644 index f817ca13..00000000 --- a/src/com/massivecraft/factions/event/FactionsEventPowerLoss.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.massivecraft.factions.event; - -import org.bukkit.command.CommandSender; -import org.bukkit.event.HandlerList; - -public class FactionsEventPowerLoss extends FactionsEventAbstractSender -{ - // -------------------------------------------- // - // REQUIRED EVENT CODE - // -------------------------------------------- // - - private static final HandlerList handlers = new HandlerList(); - @Override public HandlerList getHandlers() { return handlers; } - public static HandlerList getHandlerList() { return handlers; } - - // -------------------------------------------- // - // CONSTRUCT - // -------------------------------------------- // - - // TODO: Replace this event with a power change event? - - public FactionsEventPowerLoss(CommandSender sender) - { - super(sender); - } - -} diff --git a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java index 8ddc0c50..d94525ed 100644 --- a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java +++ b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java @@ -54,7 +54,8 @@ import com.massivecraft.factions.FPlayerColl; import com.massivecraft.factions.Faction; import com.massivecraft.factions.Factions; import com.massivecraft.factions.Rel; -import com.massivecraft.factions.event.FactionsEventPowerLoss; +import com.massivecraft.factions.event.FactionsEventPowerChange; +import com.massivecraft.factions.event.FactionsEventPowerChange.PowerChangeReason; import com.massivecraft.factions.util.VisualizeUtil; import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.util.MUtil; @@ -105,13 +106,15 @@ public class FactionsListenerMain implements Listener return; } - // ... and our special event doesn't get cancelled ... - FactionsEventPowerLoss powerLossEvent = new FactionsEventPowerLoss(player); - powerLossEvent.run(); - if (powerLossEvent.isCancelled()) return; + // ... Event ... + double newPower = fplayer.getPower() + ConfServer.powerPerDeath; + FactionsEventPowerChange powerChangeEvent = new FactionsEventPowerChange(null, fplayer, PowerChangeReason.DEATH, newPower); + powerChangeEvent.run(); + if (powerChangeEvent.isCancelled()) return; + newPower = powerChangeEvent.getNewPower(); // ... alter the power ... - fplayer.setPower(fplayer.getPower() + ConfServer.powerPerDeath); + fplayer.setPower(newPower); // ... and inform the player. fplayer.msg("Your power is now %d / %d", fplayer.getPowerRounded(), fplayer.getPowerMaxRounded());