get itemstack from globalname

Discussion in 'Plugin Development' started by atesin, Jul 27, 2017.

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

    atesin

    hi... i know i can get the globalname of an item with

    Code:
    String globalName = CraftItemStack.asNMSCopy(myItemStack).a();
    but i want the opposite method for a "search item" plugin i am writing ... i like to pass a globalname and get the item stack that matches that globalname ... something like

    Code:
    ItemStack matchedItem = someClass.getItemStack("tile.cloth.blue"); // got a blue wool block
    also could be material + variant (materialdata?) ... does anybody know how to achieve this?

    please answer my question and don't distract with nms, reflection, coding style or other discussion
     
  2. Offline

    Zombie_Striker

    @atesin
    The easiest way to do this would be to create a HashMap, the keys will be Strings and the values will be ItemStacks. From there, just add all the itemstacks and their keys in the onEnable.
     
  3. Offline

    atesin

    i thought that before ... but i find very difficult to iterate all existing blocks and items including its variants .. who knows how to do it that also?
     
  4. Offline

    Zombie_Striker

    @atesin
    I was assuming you meant for a few custom items. If you just want default materials, use Material.matchMaterial for non-data specific values and just include the select few by hand that do require specific data values.
     
  5. Offline

    atesin

    i wrote above, for "all existing blocks and items including its variants" ... i can't think any way on how can i iterate all blocks and items with each variant, in an easy, efficient and not resource hog way ....

    it will be easier if were static vanilla methods like its inverse "
    CraftItemStack.asNMSCopy(myItemStack).a()"

    .. but if not, i know i could do a little effort and use it to collect data if could iterate all of them .. but if already exists that methods i would not like to reinvent the wheel .. i was looking decompiled CB code but my head will gonna explode
     
    Last edited: Jul 27, 2017
  6. Offline

    Zombie_Striker

    @atesin
    Here's a simple way to do it:
    Code:
    public getItemStackByName(String name){
    if(Material.matchMaterial(name) != null){
       //The item is a base-game item, so return just the material
       return new ItemStack(Material.matchMaterial(name));
    }
      //This will only be triggered for Wool, Dyes, Stained Clay and Glass. For these, just set up a small look-up table for all the names
      return [Pre-created itemstack with the assigned data];
    As for it being not-resource-heavy, unless you run on a computer from the 90's, look up tables will cause negligible amounts of lag.
     
  7. Offline

    atesin

    i will be thinking something like this (pseudocode) ... tell me how do you think about it.. if it would run and if need some correction, please

    Code:
    Map<String, String> itemIds = new HashMap<String, String>();
    ItemStack item;
    String itemId;
    
    for (Material mat : Material.values())
    {
      item = new ItemStack(mat);
      itemId = ""+mat;
    
      if (item.hasMaterialData())
        for (MaterialData data : item.getMaterialData.values())
          itemId += ":"+data;
    
      itemIds.put(CraftItemStack.asNMSCopy(item).a(), itemId);
    }
    
    return itemIds;
    how can i for example, when i reach doors or wools, know there is multiple types? .. then how can i cycle throught different wood doors or different colors of wool?
     
    Last edited: Jul 27, 2017
  8. Offline

    Zombie_Striker

    @atesin
    Since the hasMaterialData methods do not exist, I recommend using the following for the data:
    Code:
    for (Material mat : Material.values())
    {
    item = new ItemStack(mat);
    itemId = ""+mat;
    
    int maxDataValues = 1;
    if(mat.name().contains("WOOL")||mat.name().contains("STAINED")||mat.name().contains("INK_SACK")){
    maxDataValues = 16;
    }else if(mat== Material.STONE){
    maxDataValues = 8;
    }else if(mat== Material.LOG){
    maxDataValues = 4;
    }else if(mat== Material.LOG2){
    maxDataValues = 2;
    }
    
    for(int i = 0; i < maxDataValues;i++){
    itemIds.put(itemId+":"+i, item);
    }
    }
    If you just copy and paste this in the onEnable, this should work. I just changed the max data to correctly add the correct data to the map, changed it so the map stores the itemstack an Itemstack object and changed the 'value' to be the key (since you want to be able to get the itemstack by providing a name, not turn the item into a name)
     
Thread Status:
Not open for further replies.

Share This Page