Solved How to give players items?

Discussion in 'Plugin Development' started by ThePandaPlayer, Aug 3, 2017.

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

    ThePandaPlayer

    Hello all! I need some help again. I am trying to give players an item specified, but my solution would be creating an if() statement for every item. But there are thousands of items in Minecraft, so that would be near impossible. So, I tried grabbing the argument for what item to give, created a String out of it and plopped it into the addItem() method. It still gave me an error. Here is my code:

    Code:
    Player player = (Player) sender;
    StringBuilder itemGrabber = new StringBuilder();
    itemGrabber.append(args[0]);
    String item = itemGrabber.toString().trim().toUpperCase();
    player.getInventory().addItem(new ItemStack(Material.item));
     
  2. Offline

    Omer H.

  3. Offline

    ThePandaPlayer

    I'm not compiling the code (just a side note) Eclipse is telling me "item cannot be resolved or is not a field"
     
  4. Offline

    Zombie_Striker

    @ThePandaPlayer
    You can't convert a string directly to an itemstack. However, you can match strings to materials, and create new Itemstacks with that material. Here is the code for that:
    Code:
    ItemStack item = new ItemStack(Material.matchMaterial(MATERIAL NAME));
     
    FullyCanadian likes this.
  5. Offline

    ThePandaPlayer

    @Zombie_Striker
    Could you provide an example of that? I don't really understand what the method does.
     
  6. Offline

    FullyCanadian

    Code:
    Player player = (Player) sender;
            Material item = Material.getMaterial(args[0].toUpperCase());
            if (item == null) {
                //if material isn't a valid material
            } else {
                player.getInventory().addItem(new ItemStack(item));
            }
    EDIT: Looks like your question was already answered but this will work as well!
     
    Zombie_Striker likes this.
  7. Offline

    Zombie_Striker

    @ThePandaPlayer
    What it does is convert a string to a material, and then create a new itemstack with that material. So, If I wanted to get stone, i would use:
    Code:
    ItemStack item = new ItemStack(Material.matchMaterial("STONE"));
    For a diamonds sword, I would use:
    Code:
    ItemStack item = new ItemStack(Material.matchMaterial("DIAMOND_SWORD"));
    and so on, where the string is the name of the material object which is all uppercase.
     
    FullyCanadian likes this.
  8. Offline

    ThePandaPlayer

    Thank you all! All of these methods work perfectly!
     
Thread Status:
Not open for further replies.

Share This Page