Get material to string and string to material

Discussion in 'Plugin Development' started by jaimetruman, Nov 29, 2019.

  1. Offline

    jaimetruman

    Hello, i have been trying this but for some reason is not working really well. I need to get the name of the item in hand to save it in a data base, and i need to get the item by the name on the data base to put it on the inventory. I have tried this:

    ItemStack i = p.getItemInHand();
    String material = i.getType().toString();
    p.sendMessage("name of the material on your hand: " + material);

    p.getInventory().addItem(new ItemStack(Material.getMaterial(material)));

    it works in some items but it doesnt work on items such as Conduit, all kind of trapdoors, stripped wook etc. The "String material" created on the code, on these blocks it returns "AIR" when these blocks are registered in MATERIAL.CONDUIT, MATERIAL.STRIPPED_ACACIA_LOG etc. I dont know if thats a bug but if anyone can help me to find other way to do that that would be nice.

    Thanks for reading.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 29, 2019
  2. Offline

    Strahan

    You should be doing p.getInventory().getItemInMainHand() - getItemInHand() is deprecated. That said, I tested with stripped acacia log as you listed it as a failing item. Works fine for me. I wrote it to config as:
    Code:
    getConfig().set("someItem", p.getInventory().getItemInMainHand().getType().name());
    saveConfig();
    ..then I read it in via
    Code:
    Material m = Material.matchMaterial(getConfig().getString("someItem"));
    p.getInventory().addItem(new ItemStack(m));
    and it worked fine. Are you writing the value to config from an existing ItemStack as you indicated or are you getting the name of the material from an argument input? If so, validate the input before saving it.
     
  3. Offline

    knokko

    I once also had the problem you are describing. For me, it was because I was compiling against craftbukkit 1.12.2 and was using the plug-in on a craftbukkit 1.13 server. Any materials changed between those versions will somehow look like AIR in your plug-in.
    If you are compiling against the same version as your server, I wouldn't know what's causing it.
     
  4. Offline

    jaimetruman

    @knokko Im compiling in spigot-1.14.4.jar on the server and java (i guess thats the library that you add as an externar jar) anyways thanks for helping
     
  5. Offline

    jaimetruman

    Ok it works apparentely i forgot to put in plugin.yml: api-version: "1.14". Thanks for helping anyways <333
     

Share This Page