use of com.elmakers.mine.bukkit.api.block.MaterialBrush in project MagicPlugin by elBukkit.
the class ConstructSpell method onCast.
@Override
public SpellResult onCast(ConfigurationSection parameters) {
Block target = null;
boolean isSelect = getTargetType() == TargetType.SELECT;
boolean finalCast = !isSelect || this.targetBlock != null;
if (finalCast && parameters.getBoolean("select_self", true) && isLookingDown()) {
target = mage.getLocation().getBlock().getRelative(BlockFace.DOWN);
} else {
Target t = getTarget();
target = t.getBlock();
}
if (target == null) {
return SpellResult.NO_TARGET;
}
MaterialBrush buildWith = getBrush();
boolean hasPermission = buildWith != null && buildWith.isErase() ? hasBreakPermission(target) : hasBuildPermission(target);
if (!hasPermission) {
return SpellResult.INSUFFICIENT_PERMISSION;
}
int radius = parameters.getInt("radius", DEFAULT_RADIUS);
radius = parameters.getInt("r", radius);
radius = parameters.getInt("size", radius);
boolean falling = parameters.getBoolean("falling", false);
boolean physics = parameters.getBoolean("physics", false);
boolean commit = parameters.getBoolean("commit", false);
boolean consume = parameters.getBoolean("consume", false);
double breakable = parameters.getDouble("breakable", 0);
double backfireChance = parameters.getDouble("reflect_chance", 0);
Vector orientTo = null;
Vector bounds = null;
if (parameters.getBoolean("use_brush_size", false)) {
if (!buildWith.isReady()) {
long timeout = System.currentTimeMillis() + 10000;
while (System.currentTimeMillis() < timeout) {
try {
Thread.sleep(500);
if (buildWith.isReady()) {
break;
}
} catch (InterruptedException ex) {
break;
}
}
if (!buildWith.isReady()) {
return SpellResult.NO_ACTION;
}
}
bounds = buildWith.getSize();
radius = (int) Math.max(Math.max(bounds.getX() / 2, bounds.getZ() / 2), bounds.getY());
} else if (isSelect) {
if (targetLocation2 != null) {
this.targetBlock = targetLocation2.getBlock();
}
if (targetBlock == null || !targetBlock.getWorld().equals(target.getWorld())) {
targetBlock = target;
activate();
return SpellResult.TARGET_SELECTED;
} else {
radius = (int) targetBlock.getLocation().distance(target.getLocation());
if (parameters.getBoolean("orient")) {
orientTo = target.getLocation().toVector().subtract(targetBlock.getLocation().toVector());
orientTo.setX(Math.abs(orientTo.getX()));
orientTo.setY(Math.abs(orientTo.getY()));
orientTo.setZ(Math.abs(orientTo.getZ()));
if (orientTo.getX() < orientTo.getZ() && orientTo.getX() < orientTo.getY()) {
orientTo = new Vector(1, 0, 0);
} else if (orientTo.getZ() < orientTo.getX() && orientTo.getZ() < orientTo.getY()) {
orientTo = new Vector(0, 0, 1);
} else {
orientTo = new Vector(0, 1, 0);
}
}
target = targetBlock;
targetBlock = null;
}
} else if (parameters.getBoolean("orient")) {
// orientTo = mage.getLocation().toVector().crossProduct(target.getLocation().toVector());
orientTo = mage.getLocation().toVector().subtract(target.getLocation().toVector());
orientTo.setX(Math.abs(orientTo.getX()));
orientTo.setY(Math.abs(orientTo.getY()));
orientTo.setZ(Math.abs(orientTo.getZ()));
if (orientTo.getX() > orientTo.getZ() && orientTo.getX() > orientTo.getY()) {
orientTo = new Vector(1, 0, 0);
} else if (orientTo.getZ() > orientTo.getX() && orientTo.getZ() > orientTo.getY()) {
orientTo = new Vector(0, 0, 1);
} else {
orientTo = new Vector(0, 1, 0);
}
}
if (!parameters.contains("radius")) {
int maxDimension = parameters.getInt("max_dimension", DEFAULT_MAX_DIMENSION);
maxDimension = parameters.getInt("md", maxDimension);
maxDimension = (int) (mage.getConstructionMultiplier() * maxDimension);
int diameter = radius * 2;
if (diameter > maxDimension) {
return SpellResult.FAIL;
}
}
// TODO : Is this needed? Or just use "ty"?
if (parameters.contains("y_offset")) {
target = target.getRelative(BlockFace.UP, parameters.getInt("y_offset", 0));
}
buildWith.setTarget(target.getLocation());
ConstructionType conType = DEFAULT_CONSTRUCTION_TYPE;
int thickness = parameters.getInt("thickness", 0);
String typeString = parameters.getString("type", "");
ConstructionType testType = ConstructionType.parseString(typeString, ConstructionType.UNKNOWN);
if (testType != ConstructionType.UNKNOWN) {
conType = testType;
}
ConstructBatch batch = new ConstructBatch(this, target.getLocation(), conType, radius, thickness, falling, orientTo);
batch.setCommit(commit);
batch.setConsume(consume);
UndoList undoList = getUndoList();
if (undoList != null && !currentCast.isConsumeFree()) {
undoList.setConsumed(consume);
}
if (parameters.getBoolean("replace", false)) {
List<com.elmakers.mine.bukkit.api.block.MaterialAndData> replaceMaterials = new ArrayList<>();
MaterialAndData wildReplace = new MaterialAndData(target);
if (!parameters.getBoolean("match_data", true)) {
wildReplace.setData(null);
}
// Hacky, but generally desired - maybe abstract to a parameterized list?
Material targetMaterial = target.getType();
if (targetMaterial == Material.STATIONARY_WATER || targetMaterial == Material.WATER || targetMaterial == Material.STATIONARY_LAVA || targetMaterial == Material.LAVA) {
wildReplace.setData(null);
}
replaceMaterials.add(wildReplace);
batch.setReplace(replaceMaterials);
}
// Check for command block overrides
if (parameters.contains("commands")) {
ConfigurationSection commandMap = parameters.getConfigurationSection("commands");
Set<String> keys = commandMap.getKeys(false);
for (String key : keys) {
batch.addCommandMapping(key, commandMap.getString(key));
}
}
if (falling) {
float force = (float) parameters.getDouble("speed", 0);
batch.setFallingDirection(ConfigurationUtils.getVector(parameters, "falling_direction"));
batch.setFallingBlockSpeed(force);
}
batch.setApplyPhysics(physics);
if (breakable > 0) {
batch.setBreakable(breakable);
}
if (backfireChance > 0) {
batch.setBackfireChance(backfireChance);
}
if (parameters.contains("orient_dimension_max")) {
batch.setOrientDimensionMax(parameters.getInt("orient_dimension_max"));
} else if (parameters.contains("odmax")) {
batch.setOrientDimensionMax(parameters.getInt("odmax"));
}
if (parameters.contains("orient_dimension_min")) {
batch.setOrientDimensionMin(parameters.getInt("orient_dimension_min"));
} else if (parameters.contains("odmin")) {
batch.setOrientDimensionMin(parameters.getInt("odmin"));
}
if (parameters.getBoolean("power", false)) {
batch.setPower(true);
}
if (bounds != null) {
batch.setBounds(bounds);
batch.setOrientDimensionMin(0);
}
boolean success = mage.addBatch(batch);
deactivate();
return success ? SpellResult.CAST : SpellResult.FAIL;
}
use of com.elmakers.mine.bukkit.api.block.MaterialBrush in project MagicPlugin by elBukkit.
the class LineAction method step.
@Override
public SpellResult step(CastContext context) {
MaterialBrush brush = context.getBrush();
Block currentTarget = actionContext.getTargetBlock();
if (incrementData) {
short data = current > 15 ? 15 : (short) current;
brush.setData(data);
}
if (requireBlock) {
Block lowerBlock = currentTarget.getRelative(BlockFace.DOWN);
if (lowerBlock.getType() == Material.AIR || lowerBlock.getType() == brush.getMaterial()) {
next(context);
skippedActions(context);
return SpellResult.NO_TARGET;
}
}
context.playEffects("iterate");
return startActions();
}
use of com.elmakers.mine.bukkit.api.block.MaterialBrush in project MagicPlugin by elBukkit.
the class VolumeAction method reset.
@Override
public void reset(CastContext context) {
super.reset(context);
MaterialBrush brush = context.getBrush();
brush.setTarget(context.getTargetLocation());
}
use of com.elmakers.mine.bukkit.api.block.MaterialBrush in project MagicPlugin by elBukkit.
the class ConstructBatch method constructBlock.
@SuppressWarnings("deprecation")
public boolean constructBlock(int dx, int dy, int dz) {
// Special-case hackiness..
if (limitYAxis && minOrientDimension != null && dy < -minOrientDimension)
return true;
if (limitYAxis && maxOrientDimension != null && dy > maxOrientDimension)
return true;
if (bounds != null) {
if (dx > bounds.getX() || dy > bounds.getY() || dz > bounds.getZ())
return true;
if (dx < -bounds.getX() || dy < -bounds.getY() || dz < -bounds.getZ())
return true;
}
// Initial range checks, we skip everything if this is not sane.
int x = center.getBlockX() + dx;
int y = center.getBlockY() + dy;
int z = center.getBlockZ() + dz;
if (y < 0 || y > center.getWorld().getMaxHeight())
return true;
// Make sure the block is loaded.
Block block = center.getWorld().getBlockAt(x, y, z);
if (!block.getChunk().isLoaded()) {
block.getChunk().load();
return false;
}
// Destructibility and permission checks
if (!spell.isDestructible(block)) {
return true;
}
if (replace != null && replace.size() > 0) {
com.elmakers.mine.bukkit.block.MaterialAndData check = new com.elmakers.mine.bukkit.block.MaterialAndData(block);
if (!replace.contains(check.getKey()) && !replace.contains(check.getWildDataKey())) {
return true;
}
}
// Prepare material brush, it may update
// given the current target (clone, replicate)
MaterialBrush brush = spell.getBrush();
brush.update(mage, block.getLocation());
if (brush.isErase()) {
if (!spell.hasBreakPermission(block)) {
return true;
}
} else {
if (!spell.hasBuildPermission(block)) {
return true;
}
}
// Check for power mode.
if (power) {
Material material = block.getType();
BlockState blockState = block.getState();
MaterialData data = blockState.getData();
boolean powerBlock = false;
if (data instanceof Button) {
Button powerData = (Button) data;
registerForUndo(block);
powerData.setPowered(!powerData.isPowered());
powerBlock = true;
} else if (data instanceof Lever) {
Lever powerData = (Lever) data;
registerForUndo(block);
powerData.setPowered(!powerData.isPowered());
powerBlock = true;
} else if (data instanceof PistonBaseMaterial) {
PistonBaseMaterial powerData = (PistonBaseMaterial) data;
registerForUndo(block);
powerData.setPowered(!powerData.isPowered());
powerBlock = true;
} else if (data instanceof PoweredRail) {
PoweredRail powerData = (PoweredRail) data;
registerForUndo(block);
powerData.setPowered(!powerData.isPowered());
powerBlock = true;
} else if (data instanceof RedstoneWire) {
RedstoneWire wireData = (RedstoneWire) data;
registerForUndo(block);
wireData.setData((byte) (15 - wireData.getData()));
powerBlock = true;
} else if (material == Material.REDSTONE_BLOCK) {
registerForUndo(block);
block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, material.getId());
controller.getRedstoneReplacement().modify(block, applyPhysics);
} else if (material == Material.REDSTONE_TORCH_OFF) {
registerForUndo(block);
block.setType(Material.REDSTONE_TORCH_ON);
} else if (material == Material.REDSTONE_TORCH_ON) {
registerForUndo(block);
block.setType(Material.REDSTONE_TORCH_OFF);
} else if (material == Material.TNT) {
registerForUndo(block);
block.setType(Material.AIR);
// Kaboomy time!
registerForUndo(block.getLocation().getWorld().spawnEntity(block.getLocation(), EntityType.PRIMED_TNT));
}
if (powerBlock) {
blockState.update();
}
return true;
}
// Make sure the brush is ready, it may need to load chunks.
if (!brush.isReady()) {
brush.prepare();
return false;
}
// Postpone attachable blocks to a second batch
if (attachables.testMaterialAndData(brush) || attachablesWall.testMaterialAndData(brush) || attachablesDouble.testMaterialAndData(brush)) {
BlockData attachBlock = new BlockData(block);
attachBlock.updateFrom(brush);
attachedBlockMap.put(attachBlock.getId(), attachBlock);
attachedBlockList.add(attachBlock);
return true;
}
if (delayed.testMaterialAndData(brush)) {
BlockData delayBlock = new BlockData(block);
delayBlock.updateFrom(brush);
delayedBlocks.add(delayBlock);
return true;
}
modifyWith(block, brush);
return true;
}
use of com.elmakers.mine.bukkit.api.block.MaterialBrush in project MagicPlugin by elBukkit.
the class ShapeBatch method createBlock.
public boolean createBlock(int dx, int dy, int dz) {
// Special-case hackiness..
if (limitYAxis && minOrientDimension != null && dy < -minOrientDimension)
return true;
if (limitYAxis && maxOrientDimension != null && dy > maxOrientDimension)
return true;
// Initial range checks, we skip everything if this is not sane.
int x = center.getBlockX() + dx;
int y = center.getBlockY() + dy;
int z = center.getBlockZ() + dz;
if (y < 0 || y > center.getWorld().getMaxHeight())
return true;
// Make sure the block is loaded.
Block block = center.getWorld().getBlockAt(x, y, z);
if (!block.getChunk().isLoaded()) {
block.getChunk().load();
return false;
}
// Prepare material brush, it may update
// given the current target (clone, replicate)
MaterialBrush brush = spell.getBrush();
brush.update(mage, block.getLocation());
// Make sure the brush is ready, it may need to load chunks.
if (!brush.isReady()) {
brush.prepare();
return false;
}
if (!spell.hasBuildPermission(block)) {
return true;
}
Location loc = center.clone();
Vector direction = block.getLocation().toVector().subtract(center.toVector()).normalize();
loc.setDirection(direction);
Minecart minecart = CompatibilityUtils.spawnCustomMinecart(loc, brush.getMaterial(), brush.getData(), radius * 16);
registerForUndo(minecart);
return true;
}
Aggregations