Solved Give player Lapiz Lazuli

Discussion in 'Plugin Development' started by Nutowen, Sep 18, 2016.

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

    Nutowen

    Theres no material.lapiz lazuli or something.

    EventHandler
    public void onBlockBreak(BlockBreakEvent e){

    int drops = 1;

    Player p = e.getPlayer();
    Material block = e.getBlock().getType();
    Material blockToGive = Material.AIR;


    if(p.getGameMode().equals(GameMode.CREATIVE)){
    return;
    }



    switch (block){
    case STONE:
    blockToGive = Material.COBBLESTONE;
    break;
    case IRON_ORE:
    blockToGive = Material.IRON_INGOT;
    break;
    case GOLD_ORE:
    blockToGive = Material.GOLD_INGOT;
    break;
    case LAPIS_ORE:

    break;
    case EMERALD_ORE:
    blockToGive = Material.EMERALD;
    break;
    case DIAMOND_ORE:
    blockToGive = Material.DIAMOND;
    break;
    case COAL_ORE:
    blockToGive = Material.COAL;
    break;
    default:
    blockToGive = block;
    break;


    }

    if(p.getInventory().getItemInMainHand().containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)){


    drops = drops + getFortuneEffect(p.getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS));

    }else if( p.getInventory().getItemInMainHand().containsEnchantment(Enchantment.DIG_SPEED)
    && p.getInventory().getItemInMainHand().containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)){
    drops = drops + getFortuneEffect(p.getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS));
    }

    ItemStack is = new ItemStack(blockToGive); //We create a new item stack to give to the player, using the Material we want to give them
    is.setAmount(drops); //Here we set the ammount. If fortune wasnt applied, then will be 1
    p.getInventory().addItem(is); //Here we actually add the item to the players inventory

    e.getBlock().setType(Material.AIR); //Here we set the block they broke to air
    e.setCancelled(true); //Here we cancle the event



    }
     
  2. Offline

    frostalf

    its called LAPIS_ORE and LAPIS_BLOCK. Lapis ore is the item, where as, lapis block, is a block of lapis
     
  3. Offline

    JanTuck

    @frostalf @Nutowen
    To give Lapis Lazuli it is an ink sack
    Code:java
    1.  
    2. e.getPlayer().getInventory().addItem(new ItemStack(Material.INK_SACK, 1, (short) 4));
    3.  
     
    frostalf likes this.
  4. Offline

    Nutowen

    Code:java
    1.  
    2. It doesn't work with the setup I got. with the blockToGive
     
  5. Offline

    JanTuck

    @Nutowen
    Well I'm sry you have to make it work.

    ItemStack is = blockToGive.equals(Material.INK_SACK) ? new ItemStack(blockToGive,1,(short) 4) : new ItemStack(blockToGive);
     
    Last edited: Sep 18, 2016
  6. Offline

    I Al Istannen

    @Nutowen
    Then change your way ;) There is no other way to obtain lapis lazuli (okay, maybe DyeColor, but it ends up being the same).
     
  7. Offline

    frostalf

    You are right @JanTuck , you either need to use the method you said, or use itemstack and the dyecolor method.
     
  8. Offline

    Nutowen

    blockToGive = Material.INK_SACK((short) 4);
    tried that but it says (The method INK_SACK(short) is undefined for the type Material)
     
  9. Offline

    frostalf

    try
    ItemStack inkSack = new ItemStack(Material.INK_SACK, 1, (short) 4);

    just to note, @JanTuck had included a code snippet of what I told you to try too.
     
  10. Offline

    Nutowen

    I will then have to be change the itemstack intto a material but I don't know how
     
    Last edited: Sep 18, 2016
  11. Offline

    frostalf

    ItemStack has getType() methods. And since there will only be 1 item in the ItemStack you shouldn't have too much of an issue.

    ItemStack inkSack = new ItemStack(Material.INK_SACK, 1, (short) 4);
    Material matInkSack = inkSack.getType();

    Do know, when you get the material, you only get the type of material, which is INK_SACK and not the material data which you will need to call getData() method as such so you can preserve the data for later use if needed.

    MaterialData matData = inkSack.getData();
     
    Last edited: Sep 18, 2016
  12. Offline

    Nutowen

    Still just giving me and ink sack
     
  13. Offline

    frostalf

    give this a try then.

    Code:
    ItemStack inkSack = new ItemStack(Material.INK_SACK);
    Dye dye = new Dye();
    dye.setColor(DyeColor.BLUE); //or what ever color you want.
    stack.setData(dye);
    
     
  14. Offline

    Nutowen

    case LAPIS_ORE:
    ItemStack inkSack = new ItemStack(Material.INK_SACK);
    Dye dye = new Dye();
    dye.setColor(DyeColor.BLUE); //or what ever color you want.
    inkSack.setData(dye);

    blockToGive = inkSack.getType();
    break;

    Still and ink sac
     
  15. Offline

    frostalf

    according to your code above, you give it to them in their inventory, so instead of trying to store it as a material in code, which is not needed to place items in an inventory, unless you want to check the type of the item. Just use the itemstack and place said itemstack in the inventory. No need to convert the ItemStack into a Material.

    Code:
    
    if (blockToGive == Material.INK_SACK) {
        ItemStack is = new ItemStack(Material.INK_SACK);
        Dye dye = new Dye();
        dye.setColor(DyeColor.BLUE);
        is.setData(dye);
    } else {
        ItemStack is = new ItemStack(blockToGive);
    }
     
    Last edited: Sep 18, 2016
Thread Status:
Not open for further replies.

Share This Page