use of org.spongepowered.api.util.blockray.BlockRayHit in project Nucleus by NucleusPowered.
the class LightningCommand method executeCommand.
@Override
public CommandResult executeCommand(final CommandSource src, CommandContext args) throws Exception {
Collection<Living> playerCollection = args.getAll(player);
// No argument, let's not smite the subject.
if (playerCollection.isEmpty()) {
if (!(src instanceof Player)) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.playeronly"));
return CommandResult.empty();
}
Player pl = (Player) src;
// 100 is a good limit here.
BlockRay<World> playerBlockRay = BlockRay.from(pl).distanceLimit(100).stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1)).build();
Optional<BlockRayHit<World>> obh = playerBlockRay.end();
Location<World> lightningLocation;
// Smite above, but not on.
lightningLocation = obh.map(BlockRayHit::getLocation).orElseGet(() -> pl.getLocation().add(0, 3, 0));
return this.spawnLightning(lightningLocation, src, null);
}
int successCount = 0;
for (Living pl : playerCollection) {
CommandResult cr = this.spawnLightning(pl.getLocation(), src, pl instanceof Player ? (Player) pl : null);
successCount += cr.getSuccessCount().orElse(0);
}
return CommandResult.builder().successCount(successCount).build();
}
use of org.spongepowered.api.util.blockray.BlockRayHit in project guardian by ichorpowered.
the class RayUtil method getFirstBlock.
private static Optional<BlockRayHit> getFirstBlock(final Extent extent, final Vector3d from, final Vector3d direction, final Predicate<BlockType> skipFilter, final int distanceLimit) {
final BlockRay<Extent> blockRay = BlockRay.from(extent, from).direction(direction).distanceLimit(distanceLimit).build();
Optional<BlockRayHit> blockRayHit = Optional.empty();
while (blockRay.hasNext()) {
final BlockRayHit nextBlockRayHit = blockRay.next();
final BlockType blockType = extent.getBlockType(nextBlockRayHit.getBlockPosition());
if (!skipFilter.test(blockType)) {
blockRayHit = Optional.of(nextBlockRayHit);
break;
}
}
return blockRayHit;
}
use of org.spongepowered.api.util.blockray.BlockRayHit in project modules-extra by CubeEngine.
the class Writer method editSignInSight.
/**
* Edits the sign the user is looking at
*
* @param user the user
* @param line1 the 1st line
* @param line2 the 2nd line
* @param line3 the 3rd line
* @param line4 the 4th line
*
* @return false if there is no sign to edit in sight
*/
public boolean editSignInSight(Player user, String line1, String line2, String line3, String line4) {
Iterator<BlockRayHit<World>> it = BlockRay.from(user).distanceLimit(10).iterator();
BlockRayHit<World> result = null;
while (it.hasNext()) {
BlockRayHit<World> next = it.next();
BlockType type = next.getLocation().getBlockType();
if (type == WALL_SIGN || type == STANDING_SIGN) {
result = next;
break;
}
}
if (result == null) {
return false;
}
Location<World> block = result.getLocation();
BlockType type = block.getBlockType();
if (type != WALL_SIGN && type != STANDING_SIGN) {
return false;
}
SignData signData = block.get(SignData.class).get();
ListValue<Text> lines = signData.lines();
lines.set(0, line1 == null ? lines.get(0) : Text.of(line1));
lines.set(1, line2 == null ? lines.get(1) : Text.of(line2));
lines.set(2, line3 == null ? lines.get(2) : Text.of(line3));
lines.set(3, line4 == null ? lines.get(3) : Text.of(line4));
System.out.println(block.offer(signData).isSuccessful());
i18n.send(user, POSITIVE, "The sign has been changed!");
return true;
}
use of org.spongepowered.api.util.blockray.BlockRayHit in project modules-extra by CubeEngine.
the class PlayerCommands method explosion.
@Command(desc = "Creates an explosion")
public void explosion(CommandSource context, @Optional Integer damage, @Named({ "player", "p" }) Player player, @Flag boolean unsafe, @Flag boolean fire, @Flag boolean blockDamage, @Flag boolean playerDamage) {
damage = damage == null ? 1 : damage;
if (damage > this.module.getConfig().command.explosion.power) {
i18n.send(context, NEGATIVE, "The power of the explosion shouldn't be greater than {integer}", this.module.getConfig().command.explosion.power);
return;
}
Location<World> loc;
if (player != null) {
if (!context.equals(player)) {
if (!context.hasPermission(module.perms().COMMAND_EXPLOSION_OTHER.getId())) {
i18n.send(context, NEGATIVE, "You are not allowed to specify a player.");
return;
}
}
loc = player.getLocation();
} else {
if (!(context instanceof Player)) {
i18n.send(context, NEGATIVE, "This command can only be used by a player!");
return;
}
java.util.Optional<BlockRayHit<World>> end = BlockRay.from(((Player) context)).distanceLimit(module.getConfig().command.explosion.distance).stopFilter(BlockRay.onlyAirFilter()).build().end();
if (end.isPresent()) {
loc = end.get().getLocation();
} else {
throw new IllegalStateException();
}
}
if (!context.hasPermission(module.perms().COMMAND_EXPLOSION_BLOCK_DAMAGE.getId()) && (blockDamage || unsafe)) {
i18n.send(context, NEGATIVE, "You are not allowed to break blocks");
return;
}
if (!context.hasPermission(module.perms().COMMAND_EXPLOSION_FIRE.getId()) && (fire || unsafe)) {
i18n.send(context, NEGATIVE, "You are not allowed to set fireticks");
return;
}
if (!context.hasPermission(module.perms().COMMAND_EXPLOSION_PLAYER_DAMAGE.getId()) && (playerDamage || unsafe)) {
i18n.send(context, NEGATIVE, "You are not allowed to damage another player");
return;
}
Explosion explosion = Explosion.builder().location(loc).canCauseFire(fire || unsafe).shouldDamageEntities(playerDamage || unsafe).shouldBreakBlocks(blockDamage || unsafe).build();
Sponge.getCauseStackManager().pushCause(context);
loc.getExtent().triggerExplosion(explosion);
}
use of org.spongepowered.api.util.blockray.BlockRayHit in project modules-extra by CubeEngine.
the class ElevatorListener method findNextSign.
private Vector3i findNextSign(Location<World> loc, Vector3i previous, Vector3i startPos, boolean up) {
startPos = previous == null ? startPos : previous;
// Search for next Elevator sign
BlockRay<World> ray = BlockRay.from(loc.getExtent(), startPos.toDouble()).direction(new Vector3d(0, up ? 1 : -1, 0)).narrowPhase(false).stopFilter(b -> b.getBlockY() <= loc.getExtent().getBlockMax().getY()).stopFilter(b -> b.getBlockY() >= loc.getExtent().getBlockMin().getY()).build();
while (ray.hasNext()) {
BlockRayHit<World> next = ray.next();
if (next.getBlockPosition().equals(startPos)) {
continue;
}
Optional<ElevatorData> targetData = next.getLocation().get(ElevatorData.class);
if (targetData.isPresent() && !next.getBlockPosition().equals(loc.getBlockPosition())) {
return next.getBlockPosition();
}
}
// nothing found? Return same location as before when it is valid
Optional<ElevatorData> targetData = loc.getExtent().get(startPos, ElevatorData.class);
return targetData.isPresent() ? previous : null;
}
Aggregations