Skip to content

Instantly share code, notes, and snippets.

@xZise
Created August 3, 2011 16:10
Show Gist options
  • Save xZise/1123025 to your computer and use it in GitHub Desktop.
Save xZise/1123025 to your computer and use it in GitHub Desktop.
Location safety test
/**
* Returns if the location is save.
* @return if the location is save. Is false if invalid.
*/
public boolean isSave() {
//TODO: Check if the player can fall through: Check below if there is a torch (not on ground), wall sign
if (this.location.isValid()) {
Location location = this.getLocation().toLocation();
Material lower = location.getBlock().getType();
LocationWrapper.moveX(location, 1.0D);
Material higher = location.getBlock().getType();
LocationWrapper.moveX(location, 1.0D);
Material top = location.getBlock().getType();
Boolean save = null;
double comma = MinecraftUtil.getDecimalPlaces(location.getY());
if (save == null) {
if (comma <= 0.05D) {
save = checkOpaqueMaterials(lower, higher);
} else {
if (comma > 0.5D) {
save = checkMaterials(lower, Material.STEP);
}
save = ((comma > 0.5D && checkMaterials(lower, Material.STEP)) || checkOpaqueMaterials(lower)) && checkOpaqueMaterials(higher, top);
}
}
return save;
} else {
return false;
}
}
private static boolean checkOpaqueMaterials(Material... materials) {
//TODO: Move to Minecraft Util?
// @formatter:off
return checkMaterials(materials,
// "Solids" blocks
Material.AIR, Material.WATER, Material.STATIONARY_WATER, Material.SNOW,
// Plants
Material.SAPLING, Material.YELLOW_FLOWER, Material.RED_ROSE, Material.BROWN_MUSHROOM, Material.RED_MUSHROOM, Material.SUGAR_CANE_BLOCK, Material.CROPS, Material.LONG_GRASS,
// Torches/Redstone
Material.TORCH, Material.REDSTONE_TORCH_ON, Material.REDSTONE_TORCH_OFF, Material.REDSTONE_WIRE,
// Signs
Material.SIGN_POST, Material.WALL_SIGN,
// Rails
Material.RAILS, Material.POWERED_RAIL, Material.DETECTOR_RAIL,
// Switches
Material.LEVER, Material.STONE_PLATE, Material.WOOD_PLATE, Material.STONE_BUTTON,
// Doors
Material.WOODEN_DOOR, Material.IRON_DOOR,
Material.LADDER,
Material.PORTAL,
Material.CAKE_BLOCK);
// @formatter:on
}
private static boolean checkMaterials(Material material, Material... allowed) {
return MinecraftUtil.contains(material, allowed);
}
private static boolean checkMaterials(Material[] materials, Material... allowed) {
for (Material material : materials) {
if (!MinecraftUtil.contains(material, allowed)) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment