Allow invincibility flag to disable god mode (when set to deny) unless the player has the worldguard.god.override-regions permission.

This commit is contained in:
Wizjany 2011-08-10 17:41:54 -04:00
parent 3757056e46
commit 17741822c1
3 changed files with 47 additions and 12 deletions

View File

@ -87,7 +87,8 @@ public void run() {
ApplicableRegionSet applicable = regionManager
.getApplicableRegions(playerLocation);
if (!RegionQueryUtil.isInvincible(plugin, player, applicable)) {
if (!RegionQueryUtil.isInvincible(plugin, player, applicable)
&& !plugin.getGlobalStateManager().hasGodMode(player)) {
processHeal(applicable, player, state);
}
}

View File

@ -26,6 +26,7 @@
import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.managers.RegionManager;
public class RegionQueryUtil {
@ -63,4 +64,21 @@ public static boolean isInvincible(WorldGuardPlugin plugin, Player player,
return state.wasInvincible;
}
public static Boolean isAllowedInvinciblity(WorldGuardPlugin plugin, Player player) {
Location loc = player.getLocation();
World world = player.getWorld();
FlagStateManager.PlayerFlagState state = plugin.getFlagStateManager().getState(player);
Vector vec = new Vector(state.lastInvincibleX, state.lastInvincibleY, state.lastInvincibleZ);
StateFlag.State regionState = plugin.getGlobalRegionManager().get(world).
getApplicableRegions(vec).getFlag(DefaultFlag.INVINCIBILITY);
if (regionState == StateFlag.State.ALLOW) {
return true;
} else if (regionState == StateFlag.State.DENY) {
return false;
} else {
return null;
}
}
}

View File

@ -67,6 +67,7 @@
import com.sk89q.worldguard.blacklist.events.ItemUseBlacklistEvent;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.managers.RegionManager;
/**
@ -164,8 +165,7 @@ private void onEntityDamageByBlock(EntityDamageByBlockEvent event) {
} else if (defender instanceof Player) {
Player player = (Player) defender;
if (cfg.hasGodMode(player)
|| (wcfg.useRegions && RegionQueryUtil.isInvincible(plugin, player))) {
if (isInvincible(player)) {
event.setCancelled(true);
return;
}
@ -238,13 +238,8 @@ private void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
ConfigurationManager cfg = plugin.getGlobalStateManager();
WorldConfiguration wcfg = cfg.get(player.getWorld());
if (cfg.hasGodMode(player)) {
event.setCancelled(true);
return;
}
if (wcfg.useRegions && RegionQueryUtil.isInvincible(plugin, player)) {
if (isInvincible(player)) {
if (wcfg.regionInvinciblityRemovesMobs
&& attacker instanceof LivingEntity && !(attacker instanceof Player)
&& !(attacker instanceof Tameable && ((Tameable) attacker).isTamed())) {
@ -359,7 +354,7 @@ private void onEntityDamageByProjectile(EntityDamageByEntityEvent event) {
ConfigurationManager cfg = plugin.getGlobalStateManager();
WorldConfiguration wcfg = cfg.get(player.getWorld());
if (cfg.hasGodMode(player) || (wcfg.useRegions && RegionQueryUtil.isInvincible(plugin, player))) {
if (isInvincible(player)) {
event.setCancelled(true);
return;
}
@ -426,8 +421,7 @@ public void onEntityDamage(EntityDamageEvent event) {
} else if (defender instanceof Player) {
Player player = (Player) defender;
if (cfg.hasGodMode(player)
|| (wcfg.useRegions && RegionQueryUtil.isInvincible(plugin, player))) {
if (isInvincible(player)) {
event.setCancelled(true);
player.setFireTicks(0);
return;
@ -772,4 +766,26 @@ public void onEntityRegainHealth(EntityRegainHealthEvent event) {
}
}
/**
* Check if a player is invincible, via either god mode or region flag. If
* the region denies invincibility, the player must have the another
* permission to override it.
*
* @param player
* @return
*/
private boolean isInvincible(Player player) {
ConfigurationManager cfg = plugin.getGlobalStateManager();
WorldConfiguration wcfg = cfg.get(player.getWorld());
boolean god = cfg.hasGodMode(player);
Boolean allowed = wcfg.useRegions && RegionQueryUtil.isAllowedInvinciblity(plugin, player);
if (allowed == false && wcfg.useRegions) {
return god && plugin.hasPermission(player, "worldguard.god.override-regions");
} else if (allowed == true || !wcfg.useRegions) {
return god;
} else {
return RegionQueryUtil.isInvincible(plugin, player);
}
}
}