Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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 传送方法使我的坐标加倍(插口)_Java_Minecraft_Bukkit - Fatal编程技术网

Java 传送方法使我的坐标加倍(插口)

Java 传送方法使我的坐标加倍(插口),java,minecraft,bukkit,Java,Minecraft,Bukkit,我正在尝试制作一个传送棒,它可以传送你向前一个街区,爆炸,给你1秒的再生,但每次我传送它都会因为某种原因使我的坐标加倍。例如,我将从坐标383,43,256转到坐标767,86,512。有人知道这是什么原因吗 这是我的班级代码: package com.jason.sbitems.swords; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; import org.

我正在尝试制作一个传送棒,它可以传送你向前一个街区,爆炸,给你1秒的再生,但每次我传送它都会因为某种原因使我的坐标加倍。例如,我将从坐标383,43,256转到坐标767,86,512。有人知道这是什么原因吗

这是我的班级代码:

package com.jason.sbitems.swords;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.potion.PotionEffectType;

public class AOTE implements Listener {
    @EventHandler
    public static void onPlayerInteract(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        Material mat = e.getPlayer().getInventory().getItemInMainHand().getType();

        int x = p.getLocation().getBlockX();
        int y = p.getLocation().getBlockY();
        int z = p.getLocation().getBlockZ();
        Location a = p.getLocation().add(x + 1, y, z);

        if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if (mat == Material.STICK) {
                p.getWorld().createExplosion(p.getLocation(), 0f);
                p.teleport(a);
                p.addPotionEffect(PotionEffectType.REGENERATION.createEffect(20, 1));
            }
        }
    }
}

您的问题是您误解了该方法如何处理位置。您正在将提供的双精度点添加到当前位置,因此无需再次指定坐标

因此,要将1添加到X坐标,您可以这样做

Location a=p.getLocation().add(1,0,0);
通过再次添加它们,可以将它们的坐标加倍。除了X坐标之外,在X坐标中,再次添加值加1

另请注意,如果你的目标是将玩家向前传送一个街区,添加到X并不总是导致“向前”,这取决于玩家看的地方。您可以通过以下类似的方式实现:

Vector teleportTo=player.getLocation().getDirection().normalize().multiply(1);
player.teleport(player.getLocation().add(teleportTo));

这将允许它们被传送到一个块中,因此您当然需要执行自己的检查和修改。

谢谢您,但是是否有任何文档或东西可以让我查看每个方法的详细功能以及我可以添加到这些方法中的内容?是的,我在回答中链接了它(
add()
是一个超链接)。但这里是主页:。