Clean up docs / code for utility classes.

This commit is contained in:
sk89q 2014-08-11 17:54:32 -07:00
parent 05369b69b6
commit 110aa2e56b
3 changed files with 40 additions and 14 deletions

View File

@ -27,11 +27,21 @@
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
/**
* Utility methods to deal with blocks.
*/
public final class Blocks { public final class Blocks {
private Blocks() { private Blocks() {
} }
/**
* Get a list of connected blocks to the given block, not including
* the given block.
*
* @param block the block
* @return a list of connected blocks, not including the given block
*/
public static List<Block> getConnected(Block block) { public static List<Block> getConnected(Block block) {
MaterialData data = block.getState().getData(); MaterialData data = block.getState().getData();

View File

@ -23,26 +23,49 @@
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
public final class DamageCauses { /**
* Utility methods to deal with event-related enums in Bukkit.
*/
public final class EventEnums {
private DamageCauses() { private EventEnums() {
} }
public static boolean isFire(DamageCause cause) { /**
* Return whether the given damage cause is fire-reltaed.
*
* @param cause the cause
* @return true if fire related
*/
public static boolean isFireCause(DamageCause cause) {
return cause == DamageCause.FIRE || cause == DamageCause.FIRE_TICK; return cause == DamageCause.FIRE || cause == DamageCause.FIRE_TICK;
} }
public static boolean isExplosion(DamageCause cause) { /**
* Return whether the given cause is an explosion.
*
* @param cause the cause
* @return true if it is an explosion cuase
*/
public static boolean isExplosionCause(DamageCause cause) {
return cause == DamageCause.BLOCK_EXPLOSION || cause == DamageCause.ENTITY_EXPLOSION; return cause == DamageCause.BLOCK_EXPLOSION || cause == DamageCause.ENTITY_EXPLOSION;
} }
/**
* Restore the statistic associated with the given cause. For example,
* for the {@link DamageCause#DROWNING} cause, the entity would have its
* air level set to its maximum.
*
* @param entity the entity
* @param cause the cuase
*/
public static void restoreStatistic(Entity entity, DamageCause cause) { public static void restoreStatistic(Entity entity, DamageCause cause) {
if (cause == DamageCause.DROWNING && entity instanceof LivingEntity) { if (cause == DamageCause.DROWNING && entity instanceof LivingEntity) {
LivingEntity living = (LivingEntity) entity; LivingEntity living = (LivingEntity) entity;
living.setRemainingAir(living.getMaximumAir()); living.setRemainingAir(living.getMaximumAir());
} }
if (isFire(cause)) { if (isFireCause(cause)) {
entity.setFireTicks(0); entity.setFireTicks(0);
} }

View File

@ -29,7 +29,6 @@
import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.managers.RegionManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -38,23 +37,17 @@
public class RegionQuery { public class RegionQuery {
private final WorldGuardPlugin plugin;
private final ConfigurationManager config; private final ConfigurationManager config;
private final GlobalRegionManager globalManager; private final GlobalRegionManager globalManager;
@Nullable @Nullable
private final LocalPlayer localPlayer; private final LocalPlayer localPlayer;
public RegionQuery(WorldGuardPlugin plugin, @Nullable Player player) { public RegionQuery(WorldGuardPlugin plugin, @Nullable Player player) {
this.plugin = plugin;
this.config = plugin.getGlobalStateManager(); this.config = plugin.getGlobalStateManager();
this.globalManager = plugin.getGlobalRegionManager(); this.globalManager = plugin.getGlobalRegionManager();
this.localPlayer = player != null ? plugin.wrapPlayer(player) : null; this.localPlayer = player != null ? plugin.wrapPlayer(player) : null;
} }
public boolean canBuild(Block block) {
return canBuild(block.getLocation());
}
public boolean canBuild(Location location) { public boolean canBuild(Location location) {
World world = location.getWorld(); World world = location.getWorld();
WorldConfiguration worldConfig = config.get(world); WorldConfiguration worldConfig = config.get(world);
@ -63,7 +56,7 @@ public boolean canBuild(Location location) {
return true; return true;
} }
if (globalManager.hasBypass(localPlayer, world)) { if (localPlayer != null && globalManager.hasBypass(localPlayer, world)) {
return true; return true;
} else { } else {
RegionManager manager = globalManager.get(location.getWorld()); RegionManager manager = globalManager.get(location.getWorld());
@ -79,7 +72,7 @@ public boolean canConstruct(Location location) {
return true; return true;
} }
if (globalManager.hasBypass(localPlayer, world)) { if (localPlayer != null && globalManager.hasBypass(localPlayer, world)) {
return true; return true;
} else { } else {
RegionManager manager = globalManager.get(location.getWorld()); RegionManager manager = globalManager.get(location.getWorld());