org.spongepowered.api.util.blockray.BlockRayHit Java Examples

The following examples show how to use org.spongepowered.api.util.blockray.BlockRayHit. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PlayerEventHandler.java    From GriefPrevention with MIT License 5 votes vote down vote up
private GPClaim findNearbyClaim(Player player) {
    int maxDistance = GriefPreventionPlugin.instance.maxInspectionDistance;
    BlockRay<World> blockRay = BlockRay.from(player).distanceLimit(maxDistance).build();
    GPPlayerData playerData = GriefPreventionPlugin.instance.dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId());
    GPClaim claim = null;
    int count = 0;
    while (blockRay.hasNext()) {
        BlockRayHit<World> blockRayHit = blockRay.next();
        Location<World> location = blockRayHit.getLocation();
        claim = this.dataStore.getClaimAt(location);
        if (claim != null && !claim.isWilderness() && (playerData.visualBlocks == null || (claim.id != playerData.visualClaimId))) {
            playerData.lastValidInspectLocation = location;
            return claim;
        }

        BlockType blockType = location.getBlockType();
        if (blockType != BlockTypes.AIR && blockType != BlockTypes.TALLGRASS) {
            break;
        }
        count++;
    }

    if (count == maxDistance) {
        GriefPreventionPlugin.sendMessage(player, GriefPreventionPlugin.instance.messageData.claimTooFar.toText());
    } else if (claim != null && claim.isWilderness()){
        GriefPreventionPlugin.sendMessage(player, GriefPreventionPlugin.instance.messageData.blockNotClaimed.toText());
    }

    return claim;
}
 
Example #2
Source File: BlockUtils.java    From GriefPrevention with MIT License 5 votes vote down vote up
public static Optional<Location<World>> getTargetBlock(Player player, GPPlayerData playerData, int maxDistance, boolean ignoreAir) throws IllegalStateException {
    BlockRay<World> blockRay = BlockRay.from(player).distanceLimit(maxDistance).build();
    GPClaim claim = null;
    if (playerData.visualClaimId != null) {
        claim = (GPClaim) GriefPreventionPlugin.instance.dataStore.getClaim(player.getWorld().getProperties(), playerData.visualClaimId);
    }

    while (blockRay.hasNext()) {
        BlockRayHit<World> blockRayHit = blockRay.next();
        if (claim != null) {
            for (Vector3i corner : claim.getVisualizer().getVisualCorners()) {
                if (corner.equals(blockRayHit.getBlockPosition())) {
                    return Optional.of(blockRayHit.getLocation());
                }
            }
        }
        if (ignoreAir) {
            if (blockRayHit.getLocation().getBlockType() != BlockTypes.TALLGRASS) {
                return Optional.of(blockRayHit.getLocation());
            }
        } else { 
            if (blockRayHit.getLocation().getBlockType() != BlockTypes.AIR &&
                    blockRayHit.getLocation().getBlockType() != BlockTypes.TALLGRASS) {
                return Optional.of(blockRayHit.getLocation());
            }
        }
    }

    return Optional.empty();
}
 
Example #3
Source File: BlockInfoExecutor.java    From EssentialCmds with MIT License 5 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (src instanceof Player)
	{
		Player player = (Player) src;

		BlockRay<World> playerBlockRay = BlockRay.from(player).blockLimit(5).build();
		BlockRayHit<World> finalHitRay = null;

		while (playerBlockRay.hasNext())
		{
			BlockRayHit<World> currentHitRay = playerBlockRay.next();

			if (!player.getWorld().getBlockType(currentHitRay.getBlockPosition()).equals(BlockTypes.AIR))
			{
				finalHitRay = currentHitRay;
				break;
			}
		}

		if (finalHitRay != null)
		{
			player.sendMessage(Text.of(TextColors.GOLD, "The name of the block you're looking at is: ", TextColors.GRAY, finalHitRay.getLocation().getBlock().getType().getTranslation().get()));
			player.sendMessage(Text.of(TextColors.GOLD, "The ID of the block you're looking at is: ", TextColors.GRAY, finalHitRay.getLocation().getBlock().getName()));
			Optional<Object> metaDataQuery = finalHitRay.getLocation().getBlock().toContainer().get(DataQuery.of("UnsafeMeta"));
			player.sendMessage(Text.of(TextColors.GOLD, "The meta of the block you're looking at is: ", TextColors.GRAY, metaDataQuery.isPresent() ? metaDataQuery.get().toString() : 0));
		}
		else
		{
			player.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "You're not looking at any block within range."));
		}
	}
	else
	{
		src.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "You must be an in-game player to use this command."));
	}

	return CommandResult.success();
}
 
Example #4
Source File: MobSpawnExecutor.java    From EssentialCmds with MIT License 5 votes vote down vote up
public Location<World> getSpawnLocFromPlayerLoc(Player player)
{
	BlockRay<World> playerBlockRay = BlockRay.from(player).blockLimit(350).build();
	BlockRayHit<World> finalHitRay = null;

	while (playerBlockRay.hasNext())
	{
		BlockRayHit<World> currentHitRay = playerBlockRay.next();

		if (!player.getWorld().getBlockType(currentHitRay.getBlockPosition()).equals(BlockTypes.AIR))
		{
			finalHitRay = currentHitRay;
			break;
		}
	}

	Location<World> spawnLocation = null;

	if (finalHitRay == null)
	{
		spawnLocation = player.getLocation();
	}
	else
	{
		spawnLocation = finalHitRay.getLocation();
	}

	return spawnLocation;
}
 
Example #5
Source File: BlockSelectionTask.java    From UltimateCore with MIT License 5 votes vote down vote up
@Override
public void select(Player p, Function<Location<World>, Boolean> test, Consumer<Location<World>> callable) {
    //Check if player is looking at block
    BlockRay<World> blockray = BlockRay.from(p).distanceLimit(5).stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1)).build();
    Optional<BlockRayHit<World>> hit = blockray.end();
    //If player is looking at block & block is correct type
    if (hit.isPresent() && test.apply(hit.get().getLocation())) {
        callable.accept(hit.get().getLocation());
        return;
    }

    //If not looking at block
    Messages.send(p, "core.selection.block");
    consumers.put(p.getUniqueId(), callable);
}
 
Example #6
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 4 votes vote down vote up
private GDClaim findNearbyClaim(Player player, GDPlayerData playerData, int maxDistance, boolean hidingVisuals) {
    if (maxDistance <= 20) {
        maxDistance = 100;
    }
    BlockRay<World> blockRay = BlockRay.from(player).distanceLimit(maxDistance).build();
    GDClaim playerClaim = GriefDefenderPlugin.getInstance().dataStore.getClaimAtPlayer(playerData, player.getLocation());
    GDClaim firstClaim = null;
    GDClaim claim = null;
    playerData.lastNonAirInspectLocation = null;
    playerData.lastValidInspectLocation = null;
    while (blockRay.hasNext()) {
        BlockRayHit<World> blockRayHit = blockRay.next();
        Location<World> location = blockRayHit.getLocation();
        claim = this.dataStore.getClaimAt(location);
        if (firstClaim == null && !claim.isWilderness()) {
            if (hidingVisuals) {
                if (claim.hasActiveVisual(player)) {
                    firstClaim = claim;
                }
            } else {
                firstClaim = claim;
            }
        }

        if (playerData.lastNonAirInspectLocation == null && !location.getBlockType().equals(BlockTypes.AIR)) {
            playerData.lastNonAirInspectLocation = location;
        }
        if (claim != null && !claim.isWilderness() && !playerClaim.getUniqueId().equals(claim.getUniqueId())) {
            playerData.lastValidInspectLocation = location;
        }

        if (!location.getBlockType().equals(BlockTypes.AIR) && !NMSUtil.getInstance().isBlockTransparent(location.getBlock())) {
            break;
        }
    }

    if (claim == null || claim.isWilderness()) {
        if (firstClaim == null) {
            return GriefDefenderPlugin.getInstance().dataStore.getClaimWorldManager(player.getWorld().getUniqueId()).getWildernessClaim();
        }
        return firstClaim;
    }

    return claim;
}
 
Example #7
Source File: DataHandler.java    From Nations with MIT License 4 votes vote down vote up
public static void toggleMarkJob(Player player)
{
	if (markJobs.containsKey(player.getUniqueId()))
	{
		Sponge.getScheduler().getTaskById(markJobs.get(player.getUniqueId())).ifPresent(task -> {task.cancel();});
		markJobs.remove(player.getUniqueId());
		return;
	}
	ParticleEffect nationParticule = ParticleEffect.builder().type(ParticleTypes.DRAGON_BREATH).quantity(1).build();
	ParticleEffect zoneParticule = ParticleEffect.builder().type(ParticleTypes.HAPPY_VILLAGER).quantity(1).build();
	Task t = Sponge.getScheduler()
			.createTaskBuilder()
			.execute(task -> {
				if (!player.isOnline())
				{
					task.cancel();
					markJobs.remove(player.getUniqueId());
					return;
				}
				Location<World> loc = player.getLocation().add(0, 2, 0);
				loc = loc.sub(8, 0, 8);
				for (int x = 0; x < 16; ++x)
				{
					for (int y = 0; y < 16; ++y)
					{
						Nation nation = DataHandler.getNation(loc);
						if (nation != null)
						{
							BlockRay<World> blockRay = BlockRay.from(loc).direction(new Vector3d(0, -1, 0)).distanceLimit(50).stopFilter(BlockRay.blockTypeFilter(BlockTypes.AIR)).build();
							Optional<BlockRayHit<World>> block = blockRay.end();
							if (block.isPresent())
							{
								if (nation.getZone(loc) != null)
								{
									player.spawnParticles(zoneParticule, block.get().getPosition(), 60);
								}
								else
								{
									player.spawnParticles(nationParticule, block.get().getPosition(), 60);
								}
							}
						}
						loc = loc.add(0,0,1);
					}
					loc = loc.add(1,0,0);
					loc = loc.sub(0,0,16);
				}
			})
			.delay(1, TimeUnit.SECONDS)
			.interval(1, TimeUnit.SECONDS)
			.async()
			.submit(NationsPlugin.getInstance());
	markJobs.put(player.getUniqueId(), t.getUniqueId());
}
 
Example #8
Source File: EntityInfoExecutor.java    From EssentialCmds with MIT License 4 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (src instanceof Player)
	{
		Player player = (Player) src;

		BlockRay<World> playerBlockRay = BlockRay.from(player).blockLimit(5).build();
		BlockRayHit<World> finalHitRay = null;

		while (playerBlockRay.hasNext())
		{
			BlockRayHit<World> currentHitRay = playerBlockRay.next();

			if (!player.getWorld().getBlockType(currentHitRay.getBlockPosition()).equals(BlockTypes.AIR))
			{
				finalHitRay = currentHitRay;
				break;
			}
		}

		if (finalHitRay != null)
		{
			Entity entityFound = null;

			for (Entity entity : player.getWorld().getEntities())
			{
				if (entity.getLocation().getBlockPosition().equals(finalHitRay.getBlockPosition().add(new Vector3i(0, 1, 0))))
				{
					entityFound = entity;
					break;
				}
			}

			if (entityFound != null)
			{
				player.sendMessage(Text.of(TextColors.GOLD, "The name of the entity you're looking at is: ", TextColors.GRAY, entityFound.getTranslation().get()));
				player.sendMessage(Text.of(TextColors.GOLD, "The ID of the entity you're looking at is: ", TextColors.GRAY, entityFound.getType().getId()));
			}
			else
			{
				player.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "No entity found!"));
			}
		}
		else
		{
			player.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "You're not looking at any block within range."));
		}
	}
	else
	{
		src.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "You must be an in-game player to use this command."));
	}

	return CommandResult.success();
}
 
Example #9
Source File: ThruExecutor.java    From EssentialCmds with MIT License 4 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (src instanceof Player)
	{
		Player player = (Player) src;

		BlockRay<World> playerBlockRay = BlockRay.from(player).blockLimit(25).build();
		BlockRayHit<World> finalHitRay = null;
		Location<World> location = null;

		while (playerBlockRay.hasNext())
		{
			BlockRayHit<World> currentHitRay = playerBlockRay.next();

			if (finalHitRay != null && player.getWorld().getBlockType(currentHitRay.getBlockPosition()).equals(BlockTypes.AIR))
			{
				location = currentHitRay.getLocation();
				break;
			}
			else if (player.getWorld().getBlockType(currentHitRay.getBlockPosition()).equals(BlockTypes.AIR))
			{
				continue;
			}
			else
			{
				finalHitRay = currentHitRay;
			}
		}

		if (finalHitRay != null && location != null)
		{
			if (player.setLocationSafely(location))
			{
				player.sendMessage(Text.of(TextColors.LIGHT_PURPLE, "Whoosh!"));
			}
			else
			{
				player.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "No free spot ahead of you found."));
			}
		}
		else if (finalHitRay == null)
		{
			player.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "You are not facing a wall."));
		}
		else
		{
			player.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "No free spot ahead of you found."));
		}
	}
	else
	{
		src.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Must be an in-game player to use /thru!"));
	}

	return CommandResult.success();
}
 
Example #10
Source File: JumpExecutor.java    From EssentialCmds with MIT License 4 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (src instanceof Player)
	{
		Player player = (Player) src;

		BlockRay<World> playerBlockRay = BlockRay.from(player).blockLimit(350).build();

		BlockRayHit<World> finalHitRay = null;

		while (playerBlockRay.hasNext())
		{
			BlockRayHit<World> currentHitRay = playerBlockRay.next();

			if (!player.getWorld().getBlockType(currentHitRay.getBlockPosition()).equals(BlockTypes.AIR))
			{
				finalHitRay = currentHitRay;
				break;
			}
		}

		if (finalHitRay == null)
		{
			player.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Could not find the block you're looking at within range!"));
		}
		else
		{
			boolean safely = false;
			// If not passable, then it is a solid block
			if (!finalHitRay.getLocation().getProperty(PassableProperty.class).get().getValue())
			{
				safely = player.setLocationSafely(finalHitRay.getLocation().add(0, 1, 0));
			}
			else
			{
				// If the block below this is tall grass or a tall flower, then teleport down to that
				if (finalHitRay.getLocation().getRelative(Direction.DOWN).getProperty(PassableProperty.class).get().getValue())
				{
					safely = player.setLocationSafely(finalHitRay.getLocation().sub(0, 1, 0));
				}
				else
				{
					// If not then we found our location
					safely = player.setLocationSafely(finalHitRay.getLocation());
				}
			}
			if (safely)
			{
				player.sendMessage(Text.of(TextColors.GREEN, "Success! ", TextColors.YELLOW, "Jumped to the block you were looking at."));
			}
			else
			{
				player.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Couldn't safely put you where you are looking."));
			}
		}
	}
	else
	{
		src.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Must be an in-game player to use /jump!"));
	}

	return CommandResult.success();
}