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

View File

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