AiseyFox
本帖最后由 人诺华延 于 2021-11-18 21:42 编辑

在bukkit插件开发的过程中,我已经学会通过枚举常量Item egg = new ItemStack(Material.EGG)获取物品堆
但是我想知道 如何才能用字符串,或者物品id的形式获取这个物品 比如Material.“EGG”或Material.123
除了Material.EGG还有其他的用法吗


已经折腾一天了 求大佬们帮忙看看




redfish
本帖最后由 redfish 于 2021-11-18 22:21 编辑

要学会看文档

你的需求应该是根据表示材料的字符串来获取对应的itemstack
Item egg = new ItemStack(Material.EGG)

https://hub.spigotmc.org/javadoc ... tory/ItemStack.html
ItemStack​(Material type)
这个是这个构造方法的签名,可以看到参数是Material类型的,

然后你可以看看怎么构造Material
https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html
查看它提供的一些方法
static Material
getMaterial​(String name)
Attempts to get the Material with the given name.

static Material
getMaterial​(String name, boolean legacyName)
Attempts to get the Material with the given name.

static Material
matchMaterial​(String name)
Attempts to match the Material with the given name.

static Material
matchMaterial​(String name, boolean legacyName)
Attempts to match the Material with the given name.

static Material
valueOf​(String name)
Returns the enum constant of this type with the specified name.

static Material[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.


看到没有,入参是String,返回类型是Material,不就是你想要的根据字符串返回Material的需求吗。






2660759456
nbnb,可是这个怎么打开

AiseyFox
redfish 发表于 2021-11-18 22:17
要学会看文档

你的需求应该是根据表示材料的字符串来获取对应的itemstack

!非常感谢解答 我曾翻阅过很多遍文档 没有深入理解找不到解决方案
经过您的解答,我成功使用valueOF方法解决了这个问题
再次感谢!

AiseyFox
2660759456 发表于 2021-11-19 11:39
nbnb,可是这个怎么打开



第一页 上一页 下一页 最后一页