Java Code Examples for org.bukkit.entity.Player#getTargetBlock()

The following examples show how to use org.bukkit.entity.Player#getTargetBlock() . 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: CommandCloudServer.java    From CloudNet with Apache License 2.0 6 votes vote down vote up
private boolean removeSign(CommandSender commandSender, Player player) {
    if (checkSignSelectorActive(commandSender)) {
        return true;
    }

    Block block = player.getTargetBlock((Set<Material>) null, 15);
    if (block.getState() instanceof org.bukkit.block.Sign) {
        if (SignSelector.getInstance().containsPosition(block.getLocation())) {
            Sign sign = SignSelector.getInstance().getSignByPosition(block.getLocation());

            if (sign != null) {
                CloudAPI.getInstance().getNetworkConnection().sendPacket(new PacketOutRemoveSign(sign));
                commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The sign has been removed");
            }
        }
    }
    return false;
}
 
Example 2
Source File: CommandCloudServer.java    From CloudNet with Apache License 2.0 6 votes vote down vote up
private boolean createSign(CommandSender commandSender, String[] args, Player player) {
    if (checkSignSelectorActive(commandSender)) {
        return false;
    }

    Block block = player.getTargetBlock((Set<Material>) null, 15);
    if (block.getState() instanceof org.bukkit.block.Sign) {
        if (!SignSelector.getInstance().containsPosition(block.getLocation())) {
            if (CloudAPI.getInstance().getServerGroupMap().containsKey(args[1])) {
                Sign sign = new Sign(args[1], SignSelector.getInstance().toPosition(block.getLocation()));
                CloudAPI.getInstance().getNetworkConnection().sendPacket(new PacketOutAddSign(sign));
                commandSender.sendMessage(CloudAPI.getInstance().getPrefix() + "The sign was successfully created!");
            } else {
                commandSender.sendMessage("The group doesn't exist");
            }
        } else {
            commandSender.sendMessage("The sign already exists!");
        }
    }
    return false;
}
 
Example 3
Source File: ExprTargetedBlock.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
	Block getTargetedBlock(final @Nullable Player p, final Event e) {
		if (p == null)
			return null;
		final long time = Bukkit.getWorlds().get(0).getFullTime();
		if (last != e || time != blocksValidForTick) {
			targetedBlocks.clear();
			blocksValidForTick = time;
			last = e;
		}
		if (!actualTargetedBlock && getTime() <= 0 && targetedBlocks.containsKey(p))
			return targetedBlocks.get(p);
//		if (e instanceof PlayerInteractEvent && p == ((PlayerInteractEvent) e).getPlayer() && (((PlayerInteractEvent) e).getAction() == Action.LEFT_CLICK_BLOCK || ((PlayerInteractEvent) e).getAction() == Action.RIGHT_CLICK_BLOCK)) {
//			targetedBlocks.put(((PlayerInteractEvent) e).getPlayer(), ((PlayerInteractEvent) e).getClickedBlock());
//			return ((PlayerInteractEvent) e).getClickedBlock();
//		}
		try {
			Block b = p.getTargetBlock((Set<Material>)null, SkriptConfig.maxTargetBlockDistance.value());
			if (b.getType() == Material.AIR)
				b = null;
			targetedBlocks.put(p, b);
			return b;
		} catch (final IllegalStateException ex) {// Bukkit my throw this (for no reason?)
			return null;
		}
	}
 
Example 4
Source File: ClickListener.java    From AnimatedFrames with MIT License 6 votes vote down vote up
void handleInteract(final Player player, Cancellable cancellable, final int action/* 0 = interact (right-click), 1 = attack (left-click) */) {

		Block targetBlock = player.getTargetBlock((Set<Material>) null, 16);
		if (targetBlock != null && targetBlock.getType() != Material.AIR) {
			Set<AnimatedFrame> frames = plugin.frameManager.getFramesInWorld(player.getWorld().getName());
			frames.removeIf(f -> !f.isClickable());

			final CursorPosition.CursorMapQueryResult queryResult = CursorPosition.findMenuByCursor(player, frames);

			if (queryResult != null && queryResult.isFound()) {
				Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
					@Override
					public void run() {
						queryResult.getClickable().handleClick(player, queryResult.getPosition(), action);
					}
				});

//				cancellable.setCancelled(true);
			}
		}

	}
 
Example 5
Source File: LookingAtCondition.java    From BetonQuest with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Boolean execute(String playerID) throws QuestRuntimeException {
    Player p = PlayerConverter.getPlayer(playerID);
    Block lookingAt = p.getTargetBlock(null, 6);
    if (loc != null) {
        Location location = loc.getLocation(playerID);
        Location to = lookingAt.getLocation();
        if (location.getBlockX() != to.getBlockX()
                || location.getBlockY() != to.getBlockY()
                || location.getBlockZ() != to.getBlockZ()) return false;
    }
    if (selector != null) {
        return selector.match(lookingAt);
    }
    return true;
}
 
Example 6
Source File: Ammo.java    From QualityArmory with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onRMB(Player e, ItemStack usedItem) {
	QAMain.DEBUG("The item being click is ammo!");
	Block b = e.getTargetBlock(null,6);
	if (usedItem.getType() == Material.DIAMOND_HOE
			&& (b.getType() == Material.DIRT
					||b.getType() == Material.GRASS
					|| b.getType() == Material.GRASS_PATH
					|| b.getType() == MultiVersionLookup.getMycil()))
		return true;
	return false;
}
 
Example 7
Source File: SetSubCommand.java    From MineableSpawners with MIT License 4 votes vote down vote up
public void execute(MineableSpawners plugin, CommandSender sender, String type) {
    if (!(sender instanceof Player)) {
        System.out.println("[MineableSpawners] Only players can run this command!");
        return;
    }

    Player player = (Player) sender;

    if (plugin.getConfigurationHandler().getList("set", "blacklisted-worlds").contains(player.getWorld().getName())) {
        player.sendMessage(plugin.getConfigurationHandler().getMessage("set", "blacklisted"));
        return;
    }

    EntityType entityType;
    try {
        entityType = EntityType.valueOf(type.toUpperCase());
    } catch (IllegalArgumentException e) {
        player.sendMessage(plugin.getConfigurationHandler().getMessage("set", "invalid-type"));
        return;
    }

    if (plugin.getConfigurationHandler().getBoolean("set", "require-individual-permission")) {
        if (!player.hasPermission("mineablespawners.set." + type.toLowerCase())) {
            player.sendMessage(plugin.getConfigurationHandler().getMessage("set", "no-individual-permission"));
            return;
        }
    }

    Block target = player.getTargetBlock(invisibleBlocks, 5);

    if (target.getState().getBlock().getType() != XMaterial.SPAWNER.parseMaterial()) {
        player.sendMessage(plugin.getConfigurationHandler().getMessage("set", "not-looking-at"));
        return;
    }

    CreatureSpawner spawner = (CreatureSpawner) target.getState();

    String from = Chat.uppercaseStartingLetters(spawner.getSpawnedType().name());
    String to = Chat.uppercaseStartingLetters(type);
    if (from.equals(to)) {
        player.sendMessage(plugin.getConfigurationHandler().getMessage("set", "already-type"));
        return;
    }

    spawner.setSpawnedType(entityType);
    spawner.update();

    player.sendMessage(plugin.getConfigurationHandler().getMessage("set", "success").replace("%from%", from).replace("%to%", to));
}
 
Example 8
Source File: CompatibilityUtils.java    From NovaGuilds with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Wrapper for Player#getTargetBlock
 * The "HashSet" method has been removed in 1.12.1
 * The "Set" method has been added in 1.8-R1 (0fcdca4beac)
 *
 * @param player   	  player
 * @param transparent transparent of transparent blocks
 * @param maxDistance maxDistance
 * @return target block
 */
public static Block getTargetBlock(Player player, Set<Material> transparent, int maxDistance) {
	if(ConfigManager.getServerVersion().isNewerThan(ConfigManager.ServerVersion.MINECRAFT_1_8_R1)) {
		return player.getTargetBlock(transparent, maxDistance);
	}
	else {
		return getTargetBlockMethod.invoke(player, transparent, maxDistance);
	}
}