Add new vehicle checks

This commit is contained in:
Eric Stokes 2011-07-30 21:12:16 -06:00
parent fbcadd9b21
commit c78e11bf52
5 changed files with 76 additions and 6 deletions

@ -1 +1 @@
Subproject commit 69b0753a195089471f10e5ff1adc7cfb16918824
Subproject commit f91fbc6dd1030d2576cae84b46d41bd0ff9f5694

View File

@ -4,7 +4,9 @@ import java.util.logging.Level;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Minecart;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import com.onarandombox.utils.BlockSafety;
@ -179,6 +181,18 @@ public class MVTeleport {
// System.out.print("The first location you gave me was safe!");
return true;
}
if(e instanceof Minecart) {
Minecart m = (Minecart)e;
if(!this.bs.canSpawnCartSafely(m)) {
return false;
}
}
else if(e instanceof Vehicle) {
Vehicle v = (Vehicle)e;
if(!this.bs.canSpawnVehicleSafely(v)) {
return false;
}
}
Location safeLocation = this.getSafeLocation(l);
if (safeLocation != null) {
// Add offset to account for a vehicle on dry land!

View File

@ -7,7 +7,7 @@ import org.bukkit.command.CommandSender;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.pneumaticraft.commandhandler.Command;
public class MultiverseCommand extends Command {
public abstract class MultiverseCommand extends Command {
protected MultiverseCore plugin;
public MultiverseCommand(MultiverseCore plugin) {

View File

@ -4,6 +4,8 @@ import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Minecart;
import org.bukkit.entity.Vehicle;
public class BlockSafety {
/**
@ -15,7 +17,7 @@ public class BlockSafety {
* @param z
* @return
*/
private boolean blockIsAboveAir(Location l) {
public boolean isBlockAboveAir(Location l) {
Location downOne = new Location(l.getWorld(), l.getX(), l.getY(), l.getZ());
downOne.setY(downOne.getY() - 1);
return (downOne.getBlock().getType() == Material.AIR);
@ -54,8 +56,8 @@ public class BlockSafety {
return false;
}
if (blockIsAboveAir(actual)) {
return false;
if (isBlockAboveAir(actual)) {
return this.hasTwoBlocksofWaterBelow(actual);
}
return true;
}
@ -143,5 +145,44 @@ public class BlockSafety {
System.out.print("Location Down: " + downOne.getBlock().getType());
System.out.print(" " + downOne);
}
/**
* Checks recursively below location L for 2 blocks of water
* @param l
* @return
*/
public boolean hasTwoBlocksofWaterBelow(Location l) {
if(l.getBlockY() < 0) {
return false;
}
Location oneBelow = l.clone();
oneBelow.subtract(0, 1, 0);
if(oneBelow.getBlock().getType() == Material.WATER || oneBelow.getBlock().getType() == Material.STATIONARY_WATER) {
Location twoBelow = oneBelow.clone();
twoBelow.subtract(0, 1, 0);
return (oneBelow.getBlock().getType() == Material.WATER || oneBelow.getBlock().getType() == Material.STATIONARY_WATER);
}
if(oneBelow.getBlock().getType() != Material.AIR) {
return false;
}
return hasTwoBlocksofWaterBelow(oneBelow);
}
public boolean canSpawnCartSafely(Minecart cart) {
if(this.isBlockAboveAir(cart.getLocation())) {
return true;
}
if(this.isEntitiyOnTrack(cart, LocationManipulation.getNextBlock(cart))) {
return true;
}
return false;
}
public boolean canSpawnVehicleSafely(Vehicle vehicle) {
if(this.isBlockAboveAir(vehicle.getLocation())) {
return true;
}
return false;
}
}

View File

@ -5,6 +5,7 @@ import java.util.Map;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Vehicle;
import org.bukkit.util.Vector;
public class LocationManipulation {
@ -117,7 +118,7 @@ public class LocationManipulation {
// X, Y, Z
// -W/+E,0, -N/+S
public static Vector getTranslatedVector(Vector v, String direction) {
if(direction == null) {
if (direction == null) {
return v;
}
float speed = getSpeed(v);
@ -142,4 +143,18 @@ public class LocationManipulation {
}
return v;
}
/**
* Returns the next Location that an entity is traveling at
*
* @param v
* @return
*/
public static Location getNextBlock(Vehicle v) {
Vector vector = v.getVelocity();
Location location = v.getLocation();
int x = vector.getX() < 0 ? vector.getX() == 0 ? 0 : -1 : 1;
int z = vector.getZ() < 0 ? vector.getZ() == 0 ? 0 : -1 : 1;
return location.add(x, 0, z);
}
}