下面列出了org.bukkit.entity.Player#getTargetBlock ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
@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;
}
}
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);
}
}
}
@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;
}
@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;
}
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));
}
/**
* 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);
}
}