即使 UUID 在 HashMap 中,为什么 containsKey() 返回 false?

问题描述 投票:0回答:1

嗨,我正在为我和一些朋友制作一个小型的 Minecraft 服务器,我已经有一段时间没有使用 java 了,顺便说一句,我使用的是 spigot 1.14.1。 遗憾的是,即使我运行了第一个命令并且知道我的 UUID 在 HashMap 中,我的 containsKey() 返回 false。 如果有人可以解释那就太好了,如果可能的话,除了解释之外,我的代码的修改版本:) 非常感谢。

我尝试过制作自己的包含布尔值,但遗憾的是没有成功

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import me.melorn.com.pineapplegang.mc.chatutility.Prefix;
import me.melorn.com.pineapplegang.mc.programutility.HashMapUtility;

public class TempYannick implements CommandExecutor{

    private static Location joejoespawn;

    private HashMap<UUID, Location> OLDLOCATIONS = new HashMap<UUID, Location>();

    public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
        if(sender instanceof Player) {
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("joejoe")) {
                this.OLDLOCATIONS.put(player.getUniqueId(), player.getLocation());
                player.sendMessage(Prefix.getPineappleCore() + Prefix.getChatPrefix() + Prefix.getChatColor() + " You have ben teleported to " + Prefix.getPlayerNameColor() + "JoeJoe's" + Prefix.getChatColor() + " Creative Universe");
            }else if(cmd.getName().equalsIgnoreCase("joejoeback")) {
                if(this.OLDLOCATIONS.containsKey(player.getUniqueId())) {
                    this.OLDLOCATIONS.remove(player.getUniqueId());
                    player.sendMessage(Prefix.getPineappleCore() + Prefix.getChatPrefix() + Prefix.getChatColor() + " You teleported back to your old location away from " + Prefix.getPlayerNameColor() + "JoeJoe's " + Prefix.getChatColor() + " Creative Universe");
                }else {
                    player.sendMessage(Prefix.getPineappleCore() + Prefix.getChatPrefix() + Prefix.getChatColor() + " You cant teleport back from " + Prefix.getPlayerNameColor() + "JoeJoe's" + Prefix.getChatColor() + " Creative Universe if you have not ben there!");
                }
            }
        }
        return false;
    }
}

我希望 containsKey() 返回 true,以便将“您传送回原来的位置”消息发送给玩家。

java hashmap bukkit
1个回答
0
投票

您可以使用以下代码:

if (commandSender instanceof Player) {
    Player player = (Player) commandSender;
    Location from = player.getLocation();
    Location to = new Location(player.getWorld(), player.getLocation().getX() + 4, player.getLocation().getY(), player.getLocation().getZ() + 4);

    player.setVelocity(new Vector(from.getX() - to.getX(), from.getY() - to.getY(), from.getZ() - to.getZ()));
}

只需将

Location to
变量替换为您需要扔给玩家的坐标即可。此代码会将玩家推至 X+4、Y、Z+4。

© www.soinside.com 2019 - 2024. All rights reserved.