use of org.bukkit.entity.FallingBlock in project MagicPlugin by elBukkit.
the class ModifyBlockAction method perform.
@SuppressWarnings("deprecation")
@Override
public SpellResult perform(CastContext context) {
MaterialBrush brush = context.getBrush();
if (brush == null) {
return SpellResult.FAIL;
}
if (checkChunk && !CompatibilityLib.getCompatibilityUtils().checkChunk(context.getTargetLocation())) {
context.addWork(100);
return SpellResult.PENDING;
}
Block block = context.getTargetBlock();
if (brush.isErase()) {
if (!context.hasBreakPermission(block)) {
return SpellResult.INSUFFICIENT_PERMISSION;
}
} else {
if (!context.hasBuildPermission(block)) {
return SpellResult.INSUFFICIENT_PERMISSION;
}
}
if (commit) {
if (!context.areAnyDestructible(block)) {
return SpellResult.NO_TARGET;
}
} else if (!context.isDestructible(block)) {
return SpellResult.NO_TARGET;
}
Material fallingMaterial = block.getType();
String fallingData = CompatibilityLib.getCompatibilityUtils().getBlockData(block);
byte fallingLegacyData = block.getData();
Mage mage = context.getMage();
brush.update(mage, context.getTargetSourceLocation());
if (!brush.isReady()) {
brush.prepare();
return SpellResult.PENDING;
}
if (!brush.isValid()) {
return SpellResult.FAIL;
}
if (!brush.isTargetValid()) {
return SpellResult.NO_TARGET;
}
if (!replaceSame && !brush.isDifferent(block)) {
return SpellResult.NO_TARGET;
}
if (consumeBlocks && !context.isConsumeFree() && !brush.isErase()) {
UndoList undoList = context.getUndoList();
if (undoList != null) {
undoList.setConsumed(true);
}
if (!mage.consumeBlock(brush, consumeVariants)) {
String requiresMessage = context.getMessage("insufficient_resources");
context.sendMessageKey("insufficient_resources", requiresMessage.replace("$cost", brush.getName()));
return SpellResult.INSUFFICIENT_RESOURCES;
}
}
boolean spawnFalling = spawnFallingBlocks;
if (spawnFalling && fallingProbability < 1) {
spawnFalling = context.getRandom().nextDouble() < fallingProbability;
}
if (spawnFalling && !brush.isErase()) {
fallingMaterial = brush.getMaterial();
fallingData = brush.getModernBlockData();
Byte data = brush.getBlockData();
fallingLegacyData = data == null ? 0 : data;
} else {
if (!commit) {
context.registerForUndo(block);
if (brush.isErase() && !DefaultMaterials.isAir(block.getType())) {
context.clearAttachables(block);
}
}
UndoList undoList = context.getUndoList();
if (undoList != null) {
undoList.setApplyPhysics(applyPhysics);
}
BlockState prior = block.getState();
brush.modify(block, applyPhysics);
if (undoList != null && !undoList.isScheduled()) {
context.getController().logBlockChange(context.getMage(), prior, block.getState());
}
if (autoBlockState) {
Location targetLocation = context.getTargetLocation();
Block hitBlock = targetLocation.getBlock();
BlockFace direction = hitBlock.getFace(block);
if (direction == BlockFace.SELF) {
direction = BlockFace.UP;
}
CompatibilityLib.getCompatibilityUtils().setAutoBlockState(block, targetLocation, direction, applyPhysics, context.getMage().getPlayer());
/*
BlockFace[] neighbors = {BlockFace.WEST, BlockFace.EAST, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.UP, BlockFace.DOWN};
for (BlockFace blockFace : neighbors) {
Block neighbor = block.getRelative(blockFace);
CompatibilityUtils.forceUpdate(neighbor, applyPhysics);
}
*/
}
}
spawnFalling = spawnFalling && !DefaultMaterials.isAir(fallingMaterial);
if (spawnFalling) {
Location blockLocation = block.getLocation();
Location blockCenter = new Location(blockLocation.getWorld(), blockLocation.getX() + 0.5, blockLocation.getY() + 0.5, blockLocation.getZ() + 0.5);
Vector fallingBlockVelocity = null;
if (fallingBlockSpeed > 0) {
Location source = context.getTargetCenterLocation();
fallingBlockVelocity = blockCenter.clone().subtract(source).toVector();
fallingBlockVelocity.normalize();
if (fallingBlockDirection != null) {
fallingBlockVelocity.add(fallingBlockDirection).normalize();
}
fallingBlockVelocity.multiply(fallingBlockSpeed);
}
if (fallingBlockVelocity != null && (Double.isNaN(fallingBlockVelocity.getX()) || Double.isNaN(fallingBlockVelocity.getY()) || Double.isNaN(fallingBlockVelocity.getZ()) || Double.isInfinite(fallingBlockVelocity.getX()) || Double.isInfinite(fallingBlockVelocity.getY()) || Double.isInfinite(fallingBlockVelocity.getZ()))) {
fallingBlockVelocity = null;
}
// If not using erase, spawn falling block instead of placing a block
FallingBlock falling;
if (fallingData != null) {
falling = CompatibilityLib.getCompatibilityUtils().spawnFallingBlock(blockCenter, fallingMaterial, fallingData);
} else {
falling = CompatibilityLib.getDeprecatedUtils().spawnFallingBlock(blockCenter, fallingMaterial, fallingLegacyData);
}
falling.setDropItem(false);
if (fallingBlockVelocity != null) {
SafetyUtils.setVelocity(falling, fallingBlockVelocity);
}
if (fallingBlockMaxDamage > 0 && fallingBlockFallDamage > 0) {
CompatibilityLib.getCompatibilityUtils().setFallingBlockDamage(falling, fallingBlockFallDamage, fallingBlockMaxDamage);
} else {
falling.setHurtEntities(fallingBlocksHurt);
}
context.registerForUndo(falling);
}
if (breakable > 0) {
context.registerBreakable(block, breakable);
}
if (backfireChance > 0) {
context.registerReflective(block, backfireChance);
}
if (commit) {
com.elmakers.mine.bukkit.api.block.BlockData blockData = context.getUndoList().get(block);
;
blockData.commit();
}
return SpellResult.CAST;
}
use of org.bukkit.entity.FallingBlock in project MagicPlugin by elBukkit.
the class ConstructBatch method modifyWith.
protected void modifyWith(Block block, MaterialAndData brush) {
Material previousMaterial = block.getType();
byte previousData = CompatibilityLib.getDeprecatedUtils().getData(block);
touch(block);
boolean isDifferent = false;
MaterialAndData replacement = null;
if (replaceMaterials != null) {
replacement = replaceMaterials.get(brush.getMaterial());
}
if (replacement != null) {
isDifferent = replacement.isDifferent(block);
} else {
isDifferent = brush.isDifferent(block);
}
if (brush.isValid() && (isDifferent || commit)) {
if (consume && !context.isConsumeFree() && brush.getMaterial() != Material.AIR) {
if (!mage.consumeBlock(brush, consumeVariants)) {
String requiresMessage = context.getMessage("insufficient_resources");
context.addResult(SpellResult.INSUFFICIENT_RESOURCES);
context.sendMessageKey("insufficient_resources", requiresMessage.replace("$cost", brush.getName()));
finish();
return;
}
}
if (!commit) {
registerForUndo(block);
}
BlockState prior = block.getState();
brush.modify(block, applyPhysics);
if (replacement != null) {
replacement.modify(block, applyPhysics);
}
if (!undoList.isScheduled()) {
controller.logBlockChange(spell.getMage(), prior, block.getState());
}
if (breakable > 0) {
context.registerBreakable(block, breakable);
}
if (backfireChance > 0) {
context.registerReflective(block, backfireChance);
}
if (spawnFallingBlocks) {
FallingBlock falling = CompatibilityLib.getDeprecatedUtils().spawnFallingBlock(block.getLocation(), previousMaterial, previousData);
falling.setDropItem(false);
if (fallingBlockSpeed != 0) {
Vector direction = this.fallingDirection != null ? this.fallingDirection : falling.getLocation().subtract(center).toVector();
direction = direction.normalize().multiply(fallingBlockSpeed);
SafetyUtils.setVelocity(falling, direction);
}
registerForUndo(falling);
}
if (commit) {
com.elmakers.mine.bukkit.api.block.BlockData blockData = UndoList.register(block);
blockData.commit();
}
context.addResult(SpellResult.CAST);
}
}
use of org.bukkit.entity.FallingBlock in project MagicPlugin by elBukkit.
the class FillBatch method process.
@Override
@SuppressWarnings("deprecation")
public int process(int maxWork) {
int workPerformed = 0;
while (workPerformed <= maxWork && ix < absx) {
Location location = new Location(world, x + ix * dx, y + iy * dy, z + iz * dz);
if (!CompatibilityLib.getCompatibilityUtils().checkChunk(location)) {
return workPerformed + 20;
}
Block block = location.getBlock();
brush.update(mage, block.getLocation());
if (!brush.isReady()) {
brush.prepare();
return workPerformed + 10;
}
workPerformed += 10;
touch(block);
boolean hasPermission = brush.isErase() ? spell.hasBreakPermission(block) : spell.hasBuildPermission(block);
if (hasPermission && !spell.isIndestructible(block) && spell.isDestructible(block)) {
Material previousMaterial = block.getType();
byte previousData = block.getData();
if (brush.isDifferent(block)) {
if (consume && !context.isConsumeFree() && brush.getMaterial() != Material.AIR) {
if (!mage.consumeBlock(brush, consumeVariants)) {
String requiresMessage = context.getMessage("insufficient_resources");
context.addResult(SpellResult.INSUFFICIENT_RESOURCES);
context.sendMessageKey("insufficient_resources", requiresMessage.replace("$cost", brush.getName()));
finish();
return workPerformed;
}
}
BlockState prior = block.getState();
registerForUndo(block);
brush.modify(block);
if (!undoList.isScheduled()) {
controller.logBlockChange(spell.getMage(), prior, block.getState());
}
if (spawnFallingBlocks) {
FallingBlock falling = block.getWorld().spawnFallingBlock(block.getLocation(), previousMaterial, previousData);
falling.setDropItem(false);
}
context.addResult(SpellResult.CAST);
}
}
iy++;
if (iy >= absy) {
iy = 0;
iz++;
if (iz >= absz) {
iz = 0;
ix++;
}
}
}
if (ix >= absx) {
finish();
}
return workPerformed;
}
Aggregations