Problem With Materials

Discussion in 'Plugin Development' started by Molten, Jan 10, 2014.

Thread Status:
Not open for further replies.
  1. Offline


    I'm having a problem with materials, I have a check to see if the block is valid and I'm getting problems on blocks that have more than 1, as in wood, or wool etc.

    I'm not finding the actual names of those if I try and do Material.getMaterial(Block Name)
    I can only find the first one of regular wool, or wood (which is oak).

    I set my variables based on what the user entered, if they enter 17:1 then it's spruce wood etc. I then set another variable to that name so Block Name = spruce wood if the player entered 17:1.

    But as far as I know, there isn't anything named spruce wood, or magenta wool that I can use so it gives me an error when I use for example, 17:1.

    Here's my list for "int's" to strings, such as 17:1 -> spruce wood. I do this elsewhere in the code but the String arrays are basically 1-1 as far as location goes. I used this site to get the base names expecting them to be the same but they are not.

    Is there anyone that can help me get the actual names that I can use this way? By this way I mean Material.getMaterial(Name)

    Here are the arrays I have with the id's and names:

    1. private String[] validBlockIDs = {"1", "2", "3","4", "5", "5:1","5:2", "5:3", "17","17:1", "17:2", "17:3",
    2. "20", "24", "24:1","24:2", "35", "35:1","35:2", "35:3", "35:4", "35:5",
    3. "35:6", "35:7", "35:8","35:9", "35:10", "35:11","35:12", "35:13", "35:14",
    4. "35:15", "43", "43:1","43:2", "43:3", "43:4","43:5", "43:6", "43:7","45",
    5. "48", "49", "60", "78", "79","80", "82", "87","88", "89", "98", "98:1",
    6. "98:2", "98:3", "110","112", "121", "125", "125:1", "125:2",
    7. "125:3", "155", "159", "159:1","159:2", "159:3", "159:4", "159:5","159:6",
    8. "159:7", "159:8","159:9", "159:10", "159:11","159:12", "159:13", "159:14",
    9. "159:15", "170", "172"};
    10. private String[] validBlockNames = {"STONE", "GRASS", "DIRT","COBBLESTONE",
    34. "HARDENED_CLAY"};

    Sorry about the formatting, it looks a lot better in my file.
  2. Offline


    For wools I know you have to declare the ItemStacks like this:
    1. Wool w = new Wool(DyeColor.GREEN); // declares a green wool object
    2. ItemStack i = w.toItemStack(); // converts the wool to an item stack

    Also, I think for woods you declare the ItemStack by using a third parameter after Quantity, like so:
    1. ItemStack i = new ItemStack(Material.WOOD, 1, (short)3); // should get a jungle wood block

    Let me know if this doesn't work, as I am not entirely positive about the wood itemstack. But I know the wool code works :)
  3. Offline

    evilmidget38 Retired Staff

    Molten Material only represents the "regular item". Every other item is specified as either the durability for that ItemStack, or the data for that Block. For example, 5:1 would represent an ItemStack with the Material WOOD and a durability of 1. If you just want a "regular item", then you should keep the default durability of 0.
  4. Offline


    So how do I use itemstacks to place a block though? Im trying to setType for a block and not sure how I can with itemstacks.
  5. Offline

    evilmidget38 Retired Staff

    Molten You'd want to get the block, and then set the type to the material of the ItemStack, and the data to the durability of the ItemStack. Keep in mind though that this isn't going to work for Materials that only represent an item and not a block, such as tools or records. I believe beds also require special treatment as they're made up of two blocks instead of one.
  6. Offline


    evilmidget38 so the durability means which one it is, i.e oak would be 0, spruce 1, etc.

    Im still not sure how to set a block using itemstacks vs material. Are you able to explain that a little bit?

    Also, thanks for the reply!!

    Ok that makes sense, and i dont need item types other than blocks. Im making a basic drawbridge plugin that uses most regular blocks without ores etc.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 6, 2016
  7. Offline

    evilmidget38 Retired Staff

    // Get whatever Block you need.
    Block block =someWorld.getBlockAt(someX, someY, someZ);
    // Set the type of the block to whatever the type of the ItemStack is.
    // Also apply the durability of the item to the block, that way you keep the data value.
    setData is deprecated as it relies upon magic numbers, but I figured it was best to keep referencing magic numbers since you were using them in your original post.
    lukegb likes this.
  8. Offline


    Thanks that helps a lot, and i thought setType wasnt deprecated and was in use? Thats what ive been using, is there something else i should be?
  9. Offline


    How would I achieve this without using .setData()?
Thread Status:
Not open for further replies.

Share This Page