Java HashMap - 字符串到 bukkit vector ?如何?

标签 java vector bukkit

我真的没有看到任何可以回答我的问题的东西,现在我陷入了困境。其要点是我将 Vector 作为字符串存储在 HashMap 中,如下所示:

notes.put(notes.size()+1),player.getLocation().getDirection().toString());

notes 是我的 HashMap 名称。由于 HashMap 似乎只存储字符串,因此我需要一种将其转换回 Vector 的方法。

在我的代码中,我稍后会像这样实现 vector :

`player.getLocation().setDirection(vector);`

当我想不出解决转换的方法时,我尝试了这种数学方法来计算面向的方向,如下所示:

`double pit = ((parsed[4]+ 90) * Math.PI) / 180;
double ya = ((parsed[3]+ 90) * Math.PI) / 180;
double newX = Math.sin(pit) * Math.cos(ya);
double newY = Math.sin(pit) * Math.sin(ya);
double newZ = Math.cos(pit);
Vector vector = new Vector(newX, newZ, newY);`

pit 是俯仰,ya 是偏航。 parsed[3]parsed[4] 只是我原始的播放器的 Pitch 和 Yaw。这再次不起作用并将此错误发送到服务器控制台。 [错误]:空。简而言之,我只是想要一种将字符串转换为 vector 的方法。我不想用数学的方式来做,但如果我别无选择,那就这样吧。感谢任何帮助和建设性批评;提前致谢!

顺便说一句:我对 Java 还很陌生,但我确实有 C 和 JavaScript 经验,所以很多东西我都很熟悉。

最佳答案

HashMap 不限于存储字符串。它们可以存储任何对象,包括 vector

Map<Integer, Vector> myMap = new HashMap<Integer, Vector>();

因此,您不必担心将字符串转换为 vector ,只需将 vector 存储在 HashMap 中即可

Map<Integer, Vector> notes = new HashMap<Integer, Vector>();

//add a vector to the map
notes.put(notes.size() + 1, player.getLocation().getDirection());

//get a vector out of the map
Vector playerVector = notes.get(notes.size());

此外,按照您当前编写的方式,您可以简单地使用 ArrayList

List<Vector> notes = new ArrayList<Vector>();

//add a vector to the array
notes.add(player.getLocation().getDirection());

//get a vector out of the map
Vector playerVector = notes.get(notes.size());

如果您确实想出于某种原因将字符串更改为 vector ,您可以使用

//get the x, y, and z values of the vector as an Array
String[] components = vectorString.split(",");

//components[0] will be the x value, [1] will be y, and [2] will be z.
double x = Double.valueOf(components[0]);
double y = Double.valueOf(components[1]);
double z = Double.valueOf(components[2]);

//construct the vector using the components
Vector myVector = new Vector(x, y, z);

关于Java HashMap - 字符串到 bukkit vector ?如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30836387/

相关文章:

java - 使用流 Java 8 转换 Map<K,V> 中的 List<V>

c++ - 如何检查数字序列是否在C++中具有增加/减少趋势

java - Bukkit - 如何从 YML 文件读取并在 if 语句中使用

我的世界 bukkit 插件的 Java 编程

java - Android:角色显示为一个盒子

java - 发电机 : Getting only most recent items of all unique hash keys

python - 使用 numba 计算向量和矩阵行之间的余弦相似度

java - 当我使用 break 时从 "for loop"获取字符串

java - 自定义方程求解器错误

java - J2ME 从 Vector 获取特定对象