Solved Get Item ID and Data, alternatives

Discussion in 'Plugin Development' started by chikenlitle99, May 24, 2014.

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

    chikenlitle99

    How I get the ID of an Item?

    Like 153:3

    any help?

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

    tommyhoogstra

    @chickenlittle99 Wait 24 hours before bumping.
     
  3. Offline

    mythbusterma

    Using the method getTypeId () in both Block and ItemStack. Then concat ":" and getData () why do you need it if I might ask.
     
  4. Code:
    int id = itemstack.getTypeId();
    int data = itemstack.getData();
     
  5. Offline

    chikenlitle99

    mythbusterma

    I need for this, could you help me?

    Code:java
    1. item = getConfig().getInt("Item_Join.Item_Id");
    2.  
    3. itemonjoin = getConfig().getBoolean("Item_Join.Item_On_Join");
    4. if (itemonjoin){
    5.  
    6. itemname = getConfig().getString("Item_Join.Item_Name").replaceAll("&", "§");
    7. itemlore = getConfig().getString("Item_Join.Item_lore").replaceAll("&", "§");
    8. }
    9. }
    10.  
    11. ArrayList<Player> cooldown = new ArrayList<Player>();
    12.  
    13. @EventHandler
    14. public void onPlayerInteract(PlayerInteractEvent e) {
    15.  
    16. final Player p = e.getPlayer();
    17.  
    18. if(!cooldown.contains(p)) {
    19. if (e.getItem() == null) return;
    20. if (e.getItem().getTypeId() != item) return;
    21. if (p.hasPermission("fa.launch")){
    22. if (p.getItemInHand().getDurability() != 1 && (e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_BLOCK)) {
    23. if (e.getItem().getTypeId() == item) {
    24. e.getItem().setDurability((short) (e.getItem().getDurability() +1));
    25.  
    26. Bukkit.dispatchCommand(p, "fw");
    27.  
    28. p.sendMessage(prefix + getConfig().getString("Messages.MessageLaunch").replaceAll("&", "§"));
    29. cooldown.add(p);
    30. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    31. public void run() {
    32.  
    33. cooldown.remove(p);
    34. p.sendMessage(prefix + getConfig().getString("Item_Join.Item_Ready").replaceAll("&", "§"));}
    35. }, getConfig().getInt("Item_Join.Item_delay"));
    36. }
    37. }
    38. }else{e.setCancelled(true);
    39. p.sendMessage(prefix + getConfig().getString("Messages.MessageLaunchNoPer").replaceAll("&", "§"));}
    40. }
    41. else {p.sendMessage(prefix + getConfig().getString("Item_Join.Wait_To_Use").replaceAll("&", "§"));}
    42. }
     
  6. Offline

    mythbusterma

    I'm not sure what this code is trying to do...are you trying to read the block id from config? In which case it's String[] block = ...getString("blockid").split(":") which will give you an array length one or two. The first one would be the type and the second would be the data. Make sure to check that the array is long enough and the second one isn't null or "".

    P.S. String.valueOf(Integer) is your friend
     
  7. Offline

    Smerfa

    String.valueOf(Integer) ?
    no Integer.valueOf(String) ?
    :p
     
  8. Offline

    chikenlitle99

    @mythbusterma
    "are you trying to read the block id from config?"
    Yes I try this
     
  9. Offline

    TheOatBaron

    chikenlitle99
    Looking at line 22, why are you checking the durability to be 1?

    Also (sorry, I'm not reading completely through your code, you'll need to replace path with the actual path), to get the item id of the config you would do:

    Code:java
    1. ItemStack is = new ItemStack(Material.AIR);
    2. int id = getConfig().getInt(path);
    3. short dmg = (short) getConfig().getInt(path);
    4. is = new ItemStack(Material.getMaterial(id));
    5. if(dmg!=0){
    6. is.setDurability(dmg);
    7. }


    That is assuming that the path on the config is an int, you might have to do some idiot-proofing
     
  10. Offline

    chikenlitle99

    TheOatBaron

    What I want is to get items with ":" as for example 154:2
    With the code I have, I can, but not can get with ":"
     
  11. Offline

    mythbusterma

    See:

     
  12. Offline

    chikenlitle99

    @mythbusterma
    I do not know English, and do not quite understand what they write, could you give me an example of code?
     
  13. Offline

    chikenlitle99

  14. Offline

    Smerfa

    If you have string "35:14" (red wool) or "46" (TnT?) you can split it by ":" (by string.split(":")
    And then you get:
    for "35:14" - String[] {"35", "14"} (array of String with 2 elements - ID and data)
    for "46" - String[] {"46"} (array of String with 1 element - ID)

    So you must check if length of that String[] is 1 (without data) or 2 (with data) and if is 1, then use 0 as data.
    You can change that String to Integer by:
    Integer.valueOf(String)


    I hope you understand now...
     
  15. Offline

    chikenlitle99

Thread Status:
Not open for further replies.

Share This Page