Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java (BUKKIT)getTargetBlock方法不是';行不通_Java_Eclipse_Bukkit - Fatal编程技术网

Java (BUKKIT)getTargetBlock方法不是';行不通

Java (BUKKIT)getTargetBlock方法不是';行不通,java,eclipse,bukkit,Java,Eclipse,Bukkit,我认为(希望)我使用的论点是正确的播放器。getTargetBlock在bukkit中是很多东西所需要的,但它对我从来都不起作用!它唯一的工作时间是与闪电产卵器 以下是不起作用的部分: @EventHandler public void onPlayerInteractBlockTeleport(PlayerInteractEvent event) { Player player = event.getPlayer(); if (player.getItemInHand().ge

我认为(希望)我使用的论点是正确的<代码>播放器。getTargetBlock在bukkit中是很多东西所需要的,但它对我从来都不起作用!它唯一的工作时间是与闪电产卵器

以下是不起作用的部分:

@EventHandler
public void onPlayerInteractBlockTeleport(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    if (player.getItemInHand().getType() == Material.BONE) {
        player.getWorld().teleport(player.getTargetBlock(null, 200));
        player.playSound(player.getLocation(), Sound.ENDERMAN_TELEPORT, 10, 1);
    }
}

有人帮忙吗?

有三个原因可以解释为什么您的代码很可能无法工作:

  • 您正在尝试传送玩家的“世界”(
    player.getWorld().teleport
    ),而不是玩家本身。从Bukkit 1.8.3 API中可以看出,世界上没有
    teleport()
    方法。我假设您正在尝试传送玩家,在这种情况下,
    player.teleport(位置/实体)
    将起作用

  • teleport()方法将位置或实体作为参数,而不是块。您需要使用
    block.getLocation()
    将目标块的位置传递给它

  • 根据您使用的Bukkit版本以及是否也在使用Craftbukkit,您可能应该将getTargetBlock()方法的第一个参数强制转换为一个集合,以避免歧义(还有另一个方法(已弃用)将HashSet作为第一个参数)

  • 在你的事件方法中,我也会将玩家传送到区块上方的某个位置,这样就不会将玩家传送到区块内。最后但并非最不重要的一点是,确保事件侦听器类已注册。下面是我测试的一些示例代码:

    @EventHandler
    public void onPlayerInteractBlockTeleport(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        //Null check isn't necessary anymore, an empty item will have Material.AIR and getTargetBlock() now also works if no target block is found
        if (player.getItemInHand().getType() == Material.BONE) {
            Location playerLoc = player.getLocation(); //Get the player's location
            Location target = player.getTargetBlock((Set) null, 200).getLocation().clone().add(0, 1, 0); //Get the block location + 1 y
            target.setYaw(playerLoc.getYaw()); //Set the yaw of the target location to the player's yaw
            target.setPitch(playerLoc.getPitch()); //Set the pitch of the target location to the player's pitch
            player.teleport(target); //Teleport player
            player.playSound(player.getLocation(), Sound.ENDERMAN_TELEPORT, 10, 1); //Play sound
        }
    }
    

    你遇到了什么样的问题?是否没有发生任何事情或正在抛出错误?您是否注册了听众等?请在提问之前自己多做些努力。例如,测试您的侦听器是否工作。如果没有,问一个关于如何设置侦听器的问题。如果其他问题不起作用,只问这个问题。请不要实质性地改变你现有的问题。你已经收到了原始版本的答案,你不能再重复使用帖子来问另一个问题。嗯,为什么投否决票?我尝试了这个代码,它成功了,似乎回答了孢子的问题,为什么他的方法不起作用。关于如何改进答案,有什么建设性的反馈或建议吗?对不起,实际错误不清楚。我得到的实际错误是模糊性错误。它不喜欢参数,在getTargetBlock位中,它有一行代码:/:D您的代码工作正常!我试图删除player.getWorld,但同样的事情发生了。它一定是克隆位。你能澄清一下它到底是做什么的吗?这样我就可以知道它的未来了。很高兴它成功了!克隆位只提供对象的精确副本,允许您编辑新副本,而不是更改原始引用(=或“赋值”运算符仅将引用复制到对象)。在本例中,这可能无关紧要,尽管有时有必要避免编辑原始位置。对块的第一个参数(消除歧义)和getLocation()方法的强制转换可能成功了。如果你能接受/支持我的回答,我将不胜感激,因为它有帮助:)我遇到了一个新问题。每当我传送时,它总是使我面向北方(正Z)。如何操纵玩家的视线方向?