1
votes

I tried to cast a String to a Material by doing this:

for (Material ma : Material.values()) {
    if (String.valueOf(ma.getId()).equals(args[0]) || ma.name().equalsIgnoreCase(args[0])) {
    }
}

If args[0] is a String like 2 or grass it works very well, but how can I cast for example 41:2 to Material?

Thank you for your help and sorry for my bad English ;)

1
I don't know what a Material is in Spigot, but I think the first question is what you expect the String "41:2" to become. Is that a range of values? Is the '2' a modifier on the material?childofsoong
In Spigot/Minecraft a Material has an ID and a Name. Grass has the ID 2 and the Name grass or minecraft:grass. When I have the Material, I want to change the Type of a Block at a given Location. Like this: Location.getBlock().setType(Material); @soongLeWimbes
Okay, so what does the 41 mean in that? What specifically should 41:2 come out to mean? Either 41 or 2?childofsoong
41:x are Slab's, so that are half Blocks and I think 41:2 is a Stone Slab. But 41:2 is just a example. It could also be 89:5 or 112:4. @soongLeWimbes
It sounds like you need to invest some time into making a function that takes one of those string inputs and returns just the Material part of that. If x:y is the only possible pattern other than the two you already check for, then something involving String.split() should probably do the trick.childofsoong

1 Answers

1
votes

In the case of the notation you're describing which uses two magic values (the type ID and data value) separated by a colon to specify the certain "type" of a block, you'll need to split the string and set the two values separately. There might be a nicer way to convert the magic value data byte using the MaterialData class, but it's probably still easier to use the direct and deprecated method of block.setData(byte data). So if args[0] contains a colon, split it and parse the two numbers. Something along the lines of this could work for you:

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!");
    }

}