String to Item ID?

Discussion in 'Plugin Development' started by Rufus5, Jan 4, 2015.

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

    Rufus5

    Trying to make my own version of shops with signs. Here is the desired layout for a sign:
    [ShopSign] <- Identifies as a shop
    264 <- Name of item
    200 <- Buy price
    100 <- Sell price

    Code:
    @EventHandler
        public void onSignClick(PlayerInteractEvent e){
            Player p = e.getPlayer();
            Block b = e.getClickedBlock();
            if(b.getState() instanceof Sign){
                BlockState s = b.getState();
                Sign sign = (Sign) s;
                if(sign.getLine(0).equalsIgnoreCase("[ShopSign]")){
                    String ID = sign.getLine(1).toUpperCase();
                    String price = sign.getLine(2);
                    String resale = sign.getLine(3);
                    Item i = new Item();
                }
            }
        }
    How do I tell bukkit that line 1 will be the item ID?
     
  2. Offline

    Windy Day

    @Rufus5
    Integer.valueOf(string); or Integer.parseInt(string); Then make a new itemstack with the id.
     
    Last edited: Jan 4, 2015
  3. Offline

    _Cookie_

    Could just use
    Material.getString(ID);
    Then you could use items names. Make sure you do a null check though.
     
  4. Offline

    Konato_K

    @_Cookie_ Material#matchMaterial is a bit better for that
     
    _Cookie_ likes this.
  5. Offline

    _Cookie_

    Yeah but if you really just want to use a item id,

    Code:
    String itemId = s.getLine(0);
    int id;
    try{
       id = Integer.parInteger(itemId);
    }catch(Exception e){
       p.sendMessage("Invalid item id");
    }
    ItemStack customItem = new ItemStack(id);
    
     
  6. Offline

    Konato_K

    @_Cookie_ matchMaterial(String) will first try to parse it as an integer and getting it by the ID, if it fails then it will try by name, I think it would be better to use it because IDs can chance and are less human-readable, but whatever.

    If you use Material.matchMaterial("345") you should get the material or not depending on the ID you pass
     
  7. Offline

    RingOfStorms

    While that method is cool, it is deprecated for using it with "345." which means that functionality may get removed in the future, so it would probably best to just do that part yourself to ensure compatibility across future versions.
     
  8. Offline

    Konato_K

    @RingOfStorms matchMaterial is not deprecated, getMateria(int) is, and that is because he wants to use a number ID (called magic number in the docs) instead of using a name, this is his decision and has nothing to do with matchMaterial, deprecation in magic number methods it's because IDs may change from version to version, the solution for this is simple, use the material name instead of the ID, which is also what matchMaterial does.
     
    _Cookie_ likes this.
  9. Offline

    _Cookie_

    The only reason I suggested using itemID's instead of Item names, is since he asked for it. Or I would of stuck with using item names.
    So lets just let him decide on what he wants to use since he has the facts and how he wants to future proof his plugins for, which he can base his decision on thanks to you two.
     
Thread Status:
Not open for further replies.

Share This Page