Clean up Heal and Feed flags.

Allow the flags to increase values for players who are invincible (or in creative mode),
but not decrease them.
Fixes WORLDGUARD-3492.
This commit is contained in:
wizjany 2015-06-04 01:57:29 -04:00
parent fbd8206038
commit ea75549a29
2 changed files with 73 additions and 65 deletions

View File

@ -19,6 +19,7 @@
package com.sk89q.worldguard.session.handler;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.session.Session;
@ -35,42 +36,47 @@ public FeedFlag(Session session) {
@Override
public void tick(Player player, ApplicableRegionSet set) {
if (!getSession().isInvincible(player) && player.getGameMode() != GameMode.CREATIVE) {
long now = System.currentTimeMillis();
long now = System.currentTimeMillis();
LocalPlayer localPlayer = getSession().getPlugin().wrapPlayer(player);
Integer feedAmount = set.getFlag(DefaultFlag.FEED_AMOUNT);
Integer feedDelay = set.getFlag(DefaultFlag.FEED_DELAY);
Integer minHunger = set.getFlag(DefaultFlag.MIN_FOOD);
Integer maxHunger = set.getFlag(DefaultFlag.MAX_FOOD);
Integer feedAmount = set.queryValue(localPlayer, DefaultFlag.FEED_AMOUNT);
Integer feedDelay = set.queryValue(localPlayer, DefaultFlag.FEED_DELAY);
Integer minHunger = set.queryValue(localPlayer, DefaultFlag.MIN_FOOD);
Integer maxHunger = set.queryValue(localPlayer, DefaultFlag.MAX_FOOD);
if (feedAmount == null || feedDelay == null || feedAmount == 0 || feedDelay < 0) {
return;
}
if (minHunger == null) {
minHunger = 0;
}
if (maxHunger == null) {
maxHunger = 20;
}
if (feedAmount == null || feedDelay == null || feedAmount == 0 || feedDelay < 0) {
return;
}
if (feedAmount < 0
&& (getSession().isInvincible(player)
|| (player.getGameMode() != GameMode.SURVIVAL && player.getGameMode() != GameMode.ADVENTURE))) {
// don't starve invincible players
return;
}
if (minHunger == null) {
minHunger = 0;
}
if (maxHunger == null) {
maxHunger = 20;
}
// Apply a cap to prevent possible exceptions
minHunger = Math.min(20, minHunger);
maxHunger = Math.min(20, maxHunger);
// Apply a cap to prevent possible exceptions
minHunger = Math.min(20, minHunger);
maxHunger = Math.min(20, maxHunger);
if (player.getFoodLevel() >= maxHunger && feedAmount > 0) {
return;
}
if (player.getFoodLevel() >= maxHunger && feedAmount > 0) {
return;
}
if (feedDelay <= 0) {
player.setFoodLevel(feedAmount > 0 ? maxHunger : minHunger);
player.setSaturation(player.getFoodLevel());
lastFeed = now;
} else if (now - lastFeed > feedDelay * 1000) {
// clamp health between minimum and maximum
player.setFoodLevel(Math.min(maxHunger, Math.max(minHunger, player.getFoodLevel() + feedAmount)));
player.setSaturation(player.getFoodLevel());
lastFeed = now;
}
if (feedDelay <= 0) {
player.setFoodLevel(feedAmount > 0 ? maxHunger : minHunger);
player.setSaturation(player.getFoodLevel());
lastFeed = now;
} else if (now - lastFeed > feedDelay * 1000) {
// clamp health between minimum and maximum
player.setFoodLevel(Math.min(maxHunger, Math.max(minHunger, player.getFoodLevel() + feedAmount)));
player.setSaturation(player.getFoodLevel());
lastFeed = now;
}
}

View File

@ -38,46 +38,48 @@ public HealFlag(Session session) {
public void tick(Player player, ApplicableRegionSet set) {
LocalPlayer localPlayer = getPlugin().wrapPlayer(player);
if (!getSession().isInvincible(player) && player.getGameMode() != GameMode.CREATIVE) {
if (player.getHealth() <= 0) {
return;
}
if (player.getHealth() <= 0) {
return;
}
long now = System.currentTimeMillis();
long now = System.currentTimeMillis();
Integer healAmount = set.queryValue(localPlayer, DefaultFlag.HEAL_AMOUNT);
Integer healDelay = set.queryValue(localPlayer, DefaultFlag.HEAL_DELAY);
Double minHealth = set.queryValue(localPlayer, DefaultFlag.MIN_HEAL);
Double maxHealth = set.queryValue(localPlayer, DefaultFlag.MAX_HEAL);
Integer healAmount = set.queryValue(localPlayer, DefaultFlag.HEAL_AMOUNT);
Integer healDelay = set.queryValue(localPlayer, DefaultFlag.HEAL_DELAY);
Double minHealth = set.queryValue(localPlayer, DefaultFlag.MIN_HEAL);
Double maxHealth = set.queryValue(localPlayer, DefaultFlag.MAX_HEAL);
if (healAmount == null || healDelay == null || healAmount == 0 || healDelay < 0) {
return;
}
if (healAmount == null || healDelay == null || healAmount == 0 || healDelay < 0) {
return;
}
if (healAmount < 0
&& (getSession().isInvincible(player)
|| (player.getGameMode() != GameMode.SURVIVAL && player.getGameMode() != GameMode.ADVENTURE))) {
// don't damage invincible players
return;
}
if (minHealth == null) {
minHealth = 0.0;
}
if (maxHealth == null) {
maxHealth = player.getMaxHealth();
}
if (minHealth == null) {
minHealth = 0.0;
}
// Apply a cap to prevent possible exceptions
minHealth = Math.min(player.getMaxHealth(), minHealth);
maxHealth = Math.min(player.getMaxHealth(), maxHealth);
if (maxHealth == null) {
maxHealth = player.getMaxHealth();
}
if (player.getHealth() >= maxHealth && healAmount > 0) {
return;
}
// Apply a cap to prevent possible exceptions
minHealth = Math.min(player.getMaxHealth(), minHealth);
maxHealth = Math.min(player.getMaxHealth(), maxHealth);
if (player.getHealth() >= maxHealth && healAmount > 0) {
return;
}
if (healDelay <= 0) {
player.setHealth(healAmount > 0 ? maxHealth : minHealth); // this will insta-kill if the flag is unset
lastHeal = now;
} else if (now - lastHeal > healDelay * 1000) {
// clamp health between minimum and maximum
player.setHealth(Math.min(maxHealth, Math.max(minHealth, player.getHealth() + healAmount)));
lastHeal = now;
}
if (healDelay <= 0) {
player.setHealth(healAmount > 0 ? maxHealth : minHealth); // this will insta-kill if the flag is unset
lastHeal = now;
} else if (now - lastHeal > healDelay * 1000) {
// clamp health between minimum and maximum
player.setHealth(Math.min(maxHealth, Math.max(minHealth, player.getHealth() + healAmount)));
lastHeal = now;
}
}