插口-将字符串转换为材质-Java

插口-将字符串转换为材质-Java,java,string,minecraft,bukkit,Java,String,Minecraft,Bukkit,我尝试通过以下操作将字符串转换为材质: for (Material ma : Material.values()) { if (String.valueOf(ma.getId()).equals(args[0]) || ma.name().equalsIgnoreCase(args[0])) { } } 如果args[0]是一个类似2或grass的字符串,它工作得很好,但是如何将示例41:2转换为材质 谢谢你的帮助,很抱歉我的英语不好;) 如果您描述的符号使用冒号分隔的两个魔术

我尝试通过以下操作将字符串转换为
材质

for (Material ma : Material.values()) {
    if (String.valueOf(ma.getId()).equals(args[0]) || ma.name().equalsIgnoreCase(args[0])) {
    }
}
如果
args[0]
是一个类似
2
grass
的字符串,它工作得很好,但是如何将示例
41:2
转换为
材质


谢谢你的帮助,很抱歉我的英语不好;)

如果您描述的符号使用冒号分隔的两个魔术值(类型ID和数据值)来指定块的特定“类型”,则需要拆分字符串并分别设置这两个值。使用
MaterialData
类可能有更好的方法来转换魔术值数据字节,但是使用
block.setData(byte data)
的直接和不推荐的方法可能更容易。因此,如果
args[0]
包含冒号,则将其拆分并解析这两个数字。类似这样的东西可能对你有用:

if (arguments[0].contains(":")) { // If the first argument contains colons
    String[] parts = arguments[0].split(":"); // Split the string at all colon characters
    int typeId; // The type ID
    try {
        typeId = Integer.parseInt(parts[0]); // Parse from the first string part
    } catch (NumberFormatException nfe) { // If the string is not an integer
        sender.sendMessage("The type ID has to be a number!"); // Tell the CommandSender
        return false;
    }
    byte data; // The data value
    try {
        data = Byte.parseByte(parts[1]); // Parse from the second string part
    } catch (NumberFormatException nfe) {
        sender.sendMessage("The data value has to be a byte!");
        return false;
    }

    Material material = Material.getMaterial(typeId); // Material will be null if the typeId is invalid!

    // Get the block whose type ID and data value you want to change

    if (material != null) {
        block.setType(material);
        block.setData(data); // Deprecated method
    } else {
        sender.sendMessage("Invalid material ID!");
    }

}

我不知道插口中的
材料是什么,但我认为第一个问题是您希望字符串
“41:2”
变成什么。这是一个值范围吗?“2”是材质上的修饰符吗?在Spigot/Minecraft中,材质具有ID和名称。格拉斯有ID
2
和名字
Grass
minecraft:Grass
。当我有
材料时
,我想更改给定
位置处块的类型
。如下所示:
Location.getBlock().setType(Material)
@soongOkay,那么
41
是什么意思呢?
41:2
的具体含义是什么?41或2?41:x是石板,所以是半块,我认为41:2是石板。但41:2只是一个例子。也可能是89:5或112:4@因此,听起来您需要投入一些时间来创建一个函数,该函数接受其中一个字符串输入,并只返回其中的
部分。如果
x:y
是除了您已经检查的两种模式之外唯一可能的模式,那么涉及
String.split()
的内容可能会起作用。