From dd02853934c6469db2ae566ca9ff557f99159236 Mon Sep 17 00:00:00 2001 From: DroppingAnvil Date: Fri, 1 Nov 2019 13:15:18 -0500 Subject: [PATCH] Much needed additions to events. --- .../factions/event/PowerLossEvent.java | 29 +++++++++++++++++-- .../factions/event/PowerRegenEvent.java | 24 +++++++++++++++ .../listeners/FactionsEntityListener.java | 6 ++-- .../factions/zcore/persist/MemoryFPlayer.java | 6 +++- 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/event/PowerLossEvent.java b/src/main/java/com/massivecraft/factions/event/PowerLossEvent.java index df277804..ccffe43c 100644 --- a/src/main/java/com/massivecraft/factions/event/PowerLossEvent.java +++ b/src/main/java/com/massivecraft/factions/event/PowerLossEvent.java @@ -13,6 +13,7 @@ public class PowerLossEvent extends FactionPlayerEvent implements Cancellable { private boolean cancelled = false; private String message; + private double modified = 0; public PowerLossEvent(Faction f, FPlayer p) { super(f, p); @@ -70,11 +71,33 @@ public class PowerLossEvent extends FactionPlayerEvent implements Cancellable { } /** - * Gets the damage to a players individual power - * @return power lost as a Double. + * Gets the configured damage to a players individual power on death + * @return power to be lost as a Double. */ - public Double getPowerLost() {return Conf.powerPerDeath;} + public Double getDefaultPowerLost() {return Conf.powerPerDeath;} + /** + * Gets the variable power lost. Custom power ignored when less than or equal to zero. + * @return custom power to be lost as a Double. + */ + public Double getCustomPowerLost() {return this.modified;} + + /** + * Sets the variable power lost. Custom power ignored when less than or equal to zero. + * @param loss Double amount for the custom power loss to be set to. + */ + public void setCustomPowerLost(Double loss) {modified = loss;} + + /** + * Determines if custom power is to be used. + * @return If custom power is to be used as a boolean. + */ + public boolean usingCustomPower() { + if (modified > 0) { + return true; + } + return false; + } @Override public boolean isCancelled() { return cancelled; diff --git a/src/main/java/com/massivecraft/factions/event/PowerRegenEvent.java b/src/main/java/com/massivecraft/factions/event/PowerRegenEvent.java index 9d3291d5..f321c02c 100644 --- a/src/main/java/com/massivecraft/factions/event/PowerRegenEvent.java +++ b/src/main/java/com/massivecraft/factions/event/PowerRegenEvent.java @@ -13,6 +13,7 @@ import org.bukkit.event.Cancellable; public class PowerRegenEvent extends FactionPlayerEvent implements Cancellable { private boolean cancelled = false; + private double modified = 0; public PowerRegenEvent(Faction f, FPlayer p) { super(f, p); @@ -26,6 +27,29 @@ public class PowerRegenEvent extends FactionPlayerEvent implements Cancellable { return fPlayer.getMillisPassed() * Conf.powerPerMinute / 60000; } + /** + * Get the amount of custom power this player will gain. Ignored if less than or equal to 0. + * @return Custom power as a double + */ + public double getCustomPower() {return modified;} + + /** + * Get if we will be using the custom power gain instead of default. + * @return If we will process the event custom returned as a Boolean. + */ + public boolean usingCustomPower() { + if (modified > 0) { + return true; + } + return false; + } + + /** + * Set the custom power gain for this event. + * @param gain Amount of power to be added to player. + */ + public void setCustomPower(Double gain) {modified = gain;} + @Override public boolean isCancelled() { return cancelled; diff --git a/src/main/java/com/massivecraft/factions/listeners/FactionsEntityListener.java b/src/main/java/com/massivecraft/factions/listeners/FactionsEntityListener.java index ed016459..ca98b4a7 100644 --- a/src/main/java/com/massivecraft/factions/listeners/FactionsEntityListener.java +++ b/src/main/java/com/massivecraft/factions/listeners/FactionsEntityListener.java @@ -80,9 +80,11 @@ public class FactionsEntityListener implements Listener { // call Event Bukkit.getPluginManager().callEvent(powerLossEvent); - // Call player onDeath if the event is not cancelled - if (!powerLossEvent.isCancelled()) { + // Call player onDeath if the event is not cancelled and not using custom power + if (!powerLossEvent.isCancelled() && !powerLossEvent.usingCustomPower()) { fplayer.onDeath(); + } else if (powerLossEvent.usingCustomPower() && !powerLossEvent.isCancelled()) { + fplayer.alterPower(-powerLossEvent.getCustomPowerLost()); } // Send the message from the powerLossEvent final String msg = powerLossEvent.getMessage(); diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java index 1c6b01ea..80dacc66 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java @@ -630,7 +630,11 @@ public abstract class MemoryFPlayer implements FPlayer { Bukkit.getScheduler().runTask(FactionsPlugin.getInstance(), () -> Bukkit.getServer().getPluginManager().callEvent(powerRegenEvent)); if (!powerRegenEvent.isCancelled()) - this.alterPower(millisPassed * Conf.powerPerMinute / 60000); // millisPerMinute : 60 * 1000 + if (!powerRegenEvent.usingCustomPower()) { + this.alterPower(millisPassed * Conf.powerPerMinute / 60000); // millisPerMinute : 60 * 1000 + } else { + this.alterPower(+powerRegenEvent.getCustomPower()); + } } public void losePowerFromBeingOffline() {