Solved Use full item ID with itemstack ? (like 36:7)

Discussion in 'Plugin Development' started by ThunderWaffeMC, Dec 18, 2013.

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

    ThunderWaffeMC

    Hi. How can I use an items full ID with itemstack? For example: 36:7. Here's an example:

    Code:java
    1.  
    2. int firstId = 36;
    3. int secondId = 7;
    4. player.getInventory().addItem(new ItemStack(firstId + secondId, 64));
    5.  


    Thanks in advance!
     
  2. Offline

    adam753

    I think this would work?
    Code:java
    1. player.getInventory().addItem(new ItemStack(firstId, 64, secondId));
     
  3. Offline

    ThunderWaffeMC

    The itemstack constructor only allows 2 integer spots and no strings.
     
  4. Offline

    xTrollxDudex

    ThunderWaffeMC
    PHP:
    public ItemStack fromId(String idint amountthrows NullPointerExceptionNumberFormatException {
        
    String[] id.split(":"); // separate the id and data. Throws NPE
        
    int type Integer.parseInt(s[0]); // 
        
    int data Integer.parseInt(s[1]); // These two throw NFE

        
    return new ItemStack(typeamountdata);
    }
     
    ThunderWaffeMC likes this.
  5. Offline

    ThunderWaffeMC

    Thanks very much but I'm still getting the same error (even at "return new ItemStack(type, amount, data);")
     
  6. Offline

    xTrollxDudex

    ThunderWaffeMC
    Crap, what am I doing. Here's an updated code:
    PHP:
    @SupressWarnings("deprecation")
    public 
    ItemStack fromId(String idint amountthrows NullPointerExceptionNumberFormatException {
        
    String[] id.split(":"); // separate the id and data. Throws NPE
        
    int type Integer.parseInt(s[0]); // 
        
    int data Integer.parseInt(s[1]); // These two throw NFE
     
        
    ItemStack item = new ItemStack();
        
    item.setTypeId(type);
        
    item.setData(new MaterialData(type, (bytedata));
     
        return 
    item;
    }
     
    ThunderWaffeMC likes this.
  7. Offline

    ThunderWaffeMC

    Thanks once again but I'm getting an error saying that "new ItemStack()" is not visible.
     
  8. Offline

    The_Doctor_123

  9. Offline

    ThunderWaffeMC

    I'm not sure what you mean? Please explain how to use ItemStack(int).
     
  10. Offline

    The_Doctor_123

    ThunderWaffeMC
    The int in the constructor sets the ID of the ItemStack, if you looked at the documentation.
     
  11. Offline

    ThunderWaffeMC

    Oh, but I don't want to designate the ItemStack an ID because I'm using PlayerInteractEvent to get the clicked block and log it to a yml file.

    Edit: This is what I'm actually using:

    Code:java
    1.  
    2. int first = event.getClickedBlock().getTypeId();
    3. byte second = event.getClickedBlock().getData();
    4. plugin.getCustomConfig().set(player.getName() + ".equip.name", event.getClickedBlock().getType().toString().toLowerCase().replace("_", " "));
    5. plugin.getCustomConfig().set(player.getName() + ".equip.first", first);
    6. plugin.getCustomConfig().set(player.getName() + ".equip.second", second);
    7. plugin.saveCustomConfig();
    8. plugin.reloadCustomConfig();
    9. String name = plugin.getCustomConfig().getString(player.getName() + ".equip.name");
    10. int firstId = plugin.getCustomConfig().getInt(player.getName() + ".equip.first");
    11. int secondId = plugin.getCustomConfig().getInt(player.getName() + ".equip.second");
    12. player.getInventory().addItem(new ItemStack(firstId, 64, secondId));
    13.  
     
  12. Offline

    xTrollxDudex

    I think I need more sleep :p
    Just change that to
    PHP:
    new ItemStack(Material.getMaterial(type))
     
    ThunderWaffeMC likes this.
  13. Offline

    ThunderWaffeMC

    xTrollxDudex Haha. Well you fixed that problem but it hasn't changed the error from "player.getInventory().addItem(new ItemStack(firstId, 64, secondId));" where it doesn't allow three arguments?
     
  14. Offline

    The_Doctor_123

    ThunderWaffeMC
    Take a look at the types in this constructor. Really, you should be reading the JavaDocs on your own rather than us telling you ever step to make.
     
    ThunderWaffeMC likes this.
  15. Offline

    ThunderWaffeMC

    Thanks I've worked it out. I've just changed the 3rd argument to short. I understand where you're coming from.
     
  16. Offline

    xTrollxDudex

  17. Offline

    The_Doctor_123

    xTrollxDudex
    It's funny.. I don't think he used that method you made anywhere...
     
Thread Status:
Not open for further replies.

Share This Page