From 07eb3756e47c8549ddcc5e338e1e4a0690ce5bcc Mon Sep 17 00:00:00 2001 From: Trent Hensler Date: Sun, 4 Mar 2018 17:27:48 -0800 Subject: [PATCH] Don't take fall damage after flying --- .../com/massivecraft/factions/FPlayer.java | 4 +++ .../listeners/FactionsEntityListener.java | 6 ++++ .../factions/zcore/persist/MemoryFPlayer.java | 35 +++++++++++++++++-- src/main/resources/config.yml | 5 +++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/FPlayer.java b/src/main/java/com/massivecraft/factions/FPlayer.java index 66d951b4..3cfcd12d 100644 --- a/src/main/java/com/massivecraft/factions/FPlayer.java +++ b/src/main/java/com/massivecraft/factions/FPlayer.java @@ -55,6 +55,10 @@ public interface FPlayer extends EconomyParticipator { public void setRole(Role role); + public boolean shouldTakeFallDamage(); + + public void setTakeFallDamage(boolean fallDamage); + public double getPowerBoost(); public void setPowerBoost(double powerBoost); diff --git a/src/main/java/com/massivecraft/factions/listeners/FactionsEntityListener.java b/src/main/java/com/massivecraft/factions/listeners/FactionsEntityListener.java index a6ae99be..cd0254d9 100644 --- a/src/main/java/com/massivecraft/factions/listeners/FactionsEntityListener.java +++ b/src/main/java/com/massivecraft/factions/listeners/FactionsEntityListener.java @@ -111,6 +111,12 @@ public class FactionsEntityListener implements Listener { } else if (Conf.safeZonePreventAllDamageToPlayers && isPlayerInSafeZone(event.getEntity())) { // Players can not take any damage in a Safe Zone event.setCancelled(true); + } else if (event.getCause() == EntityDamageEvent.DamageCause.FALL && event.getEntity() instanceof Player) { + Player player = (Player) event.getEntity(); + FPlayer fPlayer = FPlayers.getInstance().getByPlayer(player); + if (fPlayer != null && !fPlayer.shouldTakeFallDamage()) { + event.setCancelled(true); // Falling after /f fly + } } // entity took generic damage? 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 5a3d1f13..dde53459 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java @@ -72,6 +72,7 @@ public abstract class MemoryFPlayer implements FPlayer { protected transient boolean autoWarZoneEnabled; protected transient boolean loginPvpDisabled; protected transient long lastFrostwalkerMessage; + protected transient boolean shouldTakeFallDamage = true; public void login() { this.kills = getPlayer().getStatistic(Statistic.PLAYER_KILLS); @@ -891,14 +892,36 @@ public abstract class MemoryFPlayer implements FPlayer { } public void setFFlying(boolean fly, boolean damage) { - getPlayer().setAllowFlight(fly); - getPlayer().setFlying(fly); + Player player = getPlayer(); + if (player != null) { + player.setAllowFlight(fly); + player.setFlying(fly); + } if (!damage) { msg(TL.COMMAND_FLY_CHANGE, fly ? "enabled" : "disabled"); } else { msg(TL.COMMAND_FLY_DAMAGE); } + + // If leaving fly mode, don't let them take fall damage for x seconds. + if (!fly) { + int cooldown = P.p.getConfig().getInt("fly-falldamage-cooldown", 3); + + // If the value is 0 or lower, make them take fall damage. + // Otherwise, start a timer and have this cancel after a few seconds. + // Short task so we're just doing it in method. Not clean but eh. + if (cooldown > 0) { + this.shouldTakeFallDamage = false; + Bukkit.getScheduler().runTaskLater(P.p, new Runnable() { + @Override + public void run() { + shouldTakeFallDamage = true; + } + }, 20L * cooldown); + } + } + isFlying = fly; } @@ -919,6 +942,14 @@ public abstract class MemoryFPlayer implements FPlayer { return access != null && access == Access.ALLOW; } + public boolean shouldTakeFallDamage() { + return this.shouldTakeFallDamage; + } + + public void setTakeFallDamage(boolean fallDamage) { + this.shouldTakeFallDamage = fallDamage; + } + // -------------------------------------------- // // Message Sending Helpers // -------------------------------------------- // diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 0dca0f9e..560fa525 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -62,6 +62,11 @@ warp-cost: # Enable Faction Fly: enable-faction-flight: true +# If a player leaves fly (out of territory or took damage) +# how long should they not take fall damage for? +# Set to 0 to have them always take fall damage. +fly-falldamage-cooldown: 3 + # Pistons # Should we disable pistons in Faction territory? This will prevent people from doing something like: # http://i.gyazo.com/6a1a31222e58a5d60ff341c13f6a8404.gif