use of org.bukkit.block.BlockFace in project MagicPlugin by elBukkit.
the class SimulateBatch method getDiagonalNeighborCount.
protected int getDiagonalNeighborCount(Block block, MaterialAndData liveMaterial) {
int liveCount = 0;
for (BlockFace face : DIAGONAL_FACES) {
if (liveMaterial.is(block.getRelative(face))) {
liveCount++;
}
}
if (yRadius > 0) {
Block upBlock = block.getRelative(BlockFace.UP);
for (BlockFace face : NEIGHBOR_FACES) {
if (liveMaterial.is(upBlock.getRelative(face))) {
liveCount++;
}
}
Block downBlock = block.getRelative(BlockFace.DOWN);
for (BlockFace face : NEIGHBOR_FACES) {
if (liveMaterial.is(downBlock.getRelative(face))) {
liveCount++;
}
}
}
return liveCount;
}
use of org.bukkit.block.BlockFace in project MagicPlugin by elBukkit.
the class BaseSpell method getFacing.
public static BlockFace getFacing(Location location) {
float playerRot = location.getYaw();
while (playerRot < 0) playerRot += 360;
while (playerRot > 360) playerRot -= 360;
BlockFace direction = BlockFace.NORTH;
if (playerRot <= 45 || playerRot > 315) {
direction = BlockFace.SOUTH;
} else if (playerRot > 45 && playerRot <= 135) {
direction = BlockFace.WEST;
} else if (playerRot > 135 && playerRot <= 225) {
direction = BlockFace.NORTH;
} else if (playerRot > 225 && playerRot <= 315) {
direction = BlockFace.EAST;
}
return direction;
}
use of org.bukkit.block.BlockFace in project MagicPlugin by elBukkit.
the class BridgeSpell method onCast.
@Override
public SpellResult onCast(ConfigurationSection parameters) {
Block playerBlock = getPlayerBlock();
if (playerBlock == null) {
// no spot found to bridge
return SpellResult.NO_TARGET;
}
if (!hasBuildPermission(playerBlock)) {
return SpellResult.INSUFFICIENT_PERMISSION;
}
BlockFace direction = getPlayerFacing();
Block attachBlock = playerBlock;
Block targetBlock = attachBlock;
int distance = 0;
while (isTargetable(targetBlock) && distance <= MAX_SEARCH_DISTANCE) {
distance++;
attachBlock = targetBlock;
targetBlock = attachBlock.getRelative(direction);
}
if (isTargetable(targetBlock)) {
return SpellResult.NO_TARGET;
}
if (!hasBuildPermission(targetBlock)) {
return SpellResult.INSUFFICIENT_PERMISSION;
}
MaterialBrush buildWith = getBrush();
buildWith.setTarget(attachBlock.getLocation(), targetBlock.getLocation());
buildWith.update(mage, targetBlock.getLocation());
registerForUndo(targetBlock);
buildWith.modify(targetBlock);
registerForUndo();
return SpellResult.CAST;
}
use of org.bukkit.block.BlockFace in project acidisland by tastybento.
the class LavaCheck method onCobbleGen.
/**
* Magic Cobble Generator
* @param e - event
*/
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onCobbleGen(BlockFromToEvent e) {
// If magic cobble gen isnt used
if (!Settings.useMagicCobbleGen) {
// plugin.getLogger().info("DEBUG: no magic cobble gen");
return;
}
// Only do this in ASkyBlock world
if (!e.getBlock().getWorld().equals(ASkyBlock.getIslandWorld())) {
// plugin.getLogger().info("DEBUG: wrong world");
return;
}
// Do nothing if a new island is being created
if (plugin.isNewIsland()) {
// plugin.getLogger().info("DEBUG: new island in creation");
return;
}
// If only at spawn, do nothing if we're not at spawn
if (Settings.magicCobbleGenOnlyAtSpawn && (!ASkyBlockAPI.getInstance().isAtSpawn(e.getBlock().getLocation()))) {
return;
}
final Block b = e.getBlock();
if (b.getType().equals(Material.WATER) || b.getType().equals(Material.STATIONARY_WATER) || b.getType().equals(Material.LAVA) || b.getType().equals(Material.STATIONARY_LAVA)) {
// plugin.getLogger().info("DEBUG: From block is water or lava. To = " + e.getToBlock().getType());
final Block toBlock = e.getToBlock();
if (toBlock.getType().equals(Material.AIR) && generatesCobble(b, toBlock)) {
// plugin.getLogger().info("DEBUG: potential cobble gen");
// Get island level or use default
long l = Long.MIN_VALUE;
Island island = plugin.getGrid().getIslandAt(b.getLocation());
if (island != null) {
if (island.getOwner() != null) {
l = plugin.getPlayers().getIslandLevel(island.getOwner());
// plugin.getLogger().info("DEBUG: level " + level);
}
}
final long level = l;
// Check if cobble was generated next tick
// Store surrounding blocks and their current material types
final List<Block> prevBlock = new ArrayList<Block>();
final List<Material> prevMat = new ArrayList<Material>();
for (BlockFace face : FACES) {
Block r = toBlock.getRelative(face);
prevBlock.add(r);
prevMat.add(r.getType());
// r = toBlock.getRelative(face,2);
// prevBlock.add(r);
// prevMat.add(r.getType());
}
// Check if they became cobblestone next tick
plugin.getServer().getScheduler().runTask(plugin, new Runnable() {
@Override
public void run() {
Iterator<Block> blockIt = prevBlock.iterator();
Iterator<Material> matIt = prevMat.iterator();
while (blockIt.hasNext() && matIt.hasNext()) {
Block block = blockIt.next();
Material material = matIt.next();
if (block.getType().equals(Material.COBBLESTONE) && !block.getType().equals(material)) {
// plugin.getLogger().info("DEBUG: Cobble generated. Island level = " + level);
if (!Settings.magicCobbleGenChances.isEmpty()) {
Entry<Long, TreeMap<Double, Material>> entry = Settings.magicCobbleGenChances.floorEntry(level);
double maxValue = entry.getValue().lastKey();
double rnd = Util.randomDouble() * maxValue;
Entry<Double, Material> en = entry.getValue().ceilingEntry(rnd);
// plugin.getLogger().info("DEBUG: material = " + en.getValue());
if (en != null) {
block.setType(en.getValue());
// Record stats, per level
if (stats.containsKey(entry.getKey())) {
stats.get(entry.getKey()).add(en.getValue());
} else {
Multiset<Material> set = HashMultiset.create();
set.add(en.getValue());
stats.put(entry.getKey(), set);
}
}
}
}
}
}
});
}
}
}
use of org.bukkit.block.BlockFace in project BKCommonLib by bergerhealer.
the class ItemFrameInfo method findLookPosition.
/**
* Follows an eye ray to see if it lands on this item frame. If it does,
* returns the exact coordinates on the display shown on this item frame.
* If there is no map display being displayed, or the eye isn't looking at
* this item frame, null is returned.<br>
* <br>
* Use {@link MapLookPosition#isWithinBounds()} to check whether the player is
* looking within bounds of this item frame, or not.
*
* @param startPosition Start position of the eye ray
* @param lookDirection Normalized direction vector of the eye ray
* @return Map Look Position, or null if the eye ray doesn't land on this
* item frame, or this item frame isn't displaying a map display.
*/
public MapLookPosition findLookPosition(Vector startPosition, Vector lookDirection) {
// If it shows no display, don't bother checking
if (this.lastMapUUID == null) {
return null;
}
// Check whether the item frame is invisible. If so, a different offset is used.
boolean invisible = this.itemFrameHandle.getDataWatcher().getFlag(EntityHandle.DATA_FLAGS, EntityHandle.DATA_FLAG_INVISIBLE);
// Offset from block face to canvas
double FRAME_OFFSET = invisible ? 0.00625 : 0.0625;
// Compare facing with the eye ray to calculate the eye distance to the item frame
final double distance;
boolean withinBounds = true;
IntVector3 frameBlock = this.coordinates;
BlockFace facing = this.itemFrameHandle.getFacing();
switch(facing) {
case NORTH:
if (lookDirection.getZ() > 1e-10) {
distance = (frameBlock.z + 1.0 - FRAME_OFFSET - startPosition.getZ()) / lookDirection.getZ();
} else {
withinBounds = false;
distance = MathUtil.distance(frameBlock.x, frameBlock.y, frameBlock.z, startPosition.getX(), startPosition.getY(), startPosition.getZ());
}
break;
case SOUTH:
if (lookDirection.getZ() < -1e-10) {
distance = (frameBlock.z + FRAME_OFFSET - startPosition.getZ()) / lookDirection.getZ();
} else {
withinBounds = false;
distance = MathUtil.distance(frameBlock.x, frameBlock.y, frameBlock.z, startPosition.getX(), startPosition.getY(), startPosition.getZ());
}
break;
case WEST:
if (lookDirection.getX() > 1e-10) {
distance = (frameBlock.x + 1.0 - FRAME_OFFSET - startPosition.getX()) / lookDirection.getX();
} else {
withinBounds = false;
distance = MathUtil.distance(frameBlock.x, frameBlock.y, frameBlock.z, startPosition.getX(), startPosition.getY(), startPosition.getZ());
}
break;
case EAST:
if (lookDirection.getX() < -1e-10) {
distance = (frameBlock.x + FRAME_OFFSET - startPosition.getX()) / lookDirection.getX();
} else {
withinBounds = false;
distance = MathUtil.distance(frameBlock.x, frameBlock.y, frameBlock.z, startPosition.getX(), startPosition.getY(), startPosition.getZ());
}
break;
case DOWN:
if (lookDirection.getY() > 1e-10) {
distance = (frameBlock.y + 1.0 - FRAME_OFFSET - startPosition.getY()) / lookDirection.getY();
} else {
withinBounds = false;
distance = MathUtil.distance(frameBlock.x, frameBlock.y, frameBlock.z, startPosition.getX(), startPosition.getY(), startPosition.getZ());
}
break;
case UP:
if (lookDirection.getY() < -1e-10) {
distance = (frameBlock.y + FRAME_OFFSET - startPosition.getY()) / lookDirection.getY();
} else {
withinBounds = false;
distance = MathUtil.distance(frameBlock.x, frameBlock.y, frameBlock.z, startPosition.getX(), startPosition.getY(), startPosition.getZ());
}
break;
default:
throw new IllegalArgumentException("Invalid facing: " + facing);
}
// Add distance * lookDirection to startPosition and subtract item frame coordinates
// to find the coordinates relative to the middle of the block that are looked at
final double at_x = distance * lookDirection.getX() + startPosition.getX() - frameBlock.x - 0.5;
final double at_y = distance * lookDirection.getY() + startPosition.getY() - frameBlock.y - 0.5;
final double at_z = distance * lookDirection.getZ() + startPosition.getZ() - frameBlock.z - 0.5;
// If outside range [-0.5 .. 0.5] then this item frame was not looked at
double edgeDistance = Double.MAX_VALUE;
if (withinBounds) {
// Get distance from the edge of each coordinate space
final Vector edge = new Vector(Math.max(0.0, Math.abs(at_x) - 0.5), Math.max(0.0, Math.abs(at_y) - 0.5), Math.max(0.0, Math.abs(at_z) - 0.5));
edgeDistance = edge.length();
}
// Convert x/y/z into x/y using facing information
double map_x, map_y;
switch(facing) {
case NORTH:
map_x = 0.5 - at_x;
map_y = 0.5 - at_y;
break;
case SOUTH:
map_x = 0.5 + at_x;
map_y = 0.5 - at_y;
break;
case WEST:
map_x = 0.5 + at_z;
map_y = 0.5 - at_y;
break;
case EAST:
map_x = 0.5 - at_z;
map_y = 0.5 - at_y;
break;
case DOWN:
map_x = 0.5 + at_x;
map_y = 0.5 - at_z;
break;
case UP:
map_x = 0.5 + at_x;
map_y = 0.5 + at_z;
break;
default:
throw new IllegalArgumentException("Invalid facing: " + facing);
}
// Adjust the coordinates if a non-zero rotation is set
switch(this.itemFrameHandle.getRotationOrdinal() & 0x3) {
case 1:
{
double tmp = map_x;
map_x = map_y;
map_y = 1.0 - tmp;
break;
}
case 2:
{
map_x = 1.0 - map_x;
map_y = 1.0 - map_y;
break;
}
case 3:
{
double tmp = map_x;
map_x = 1.0 - map_y;
map_y = tmp;
break;
}
default:
break;
}
// Change to pixel coordinates based on resolution and done!
return new MapLookPosition(this, MapDisplayTile.RESOLUTION * (map_x + this.lastMapUUID.getTileX()), MapDisplayTile.RESOLUTION * (map_y + this.lastMapUUID.getTileY()), distance, edgeDistance);
}
Aggregations