Gems automatically crafting into blocks

Discussion in 'Plugin Development' started by diamondcodes, Aug 24, 2014.

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

    diamondcodes

    How would I go about creating a plugin that when gems (Emeralds, Diamond, Gold, Etc...) Go into the players inventory they automatically craft into blocks?
     
  2. Offline

    unon1100

    Listen to PlayerItemPickupEvent or something like that. Loop through their inventory, and if they have 8 emeralds, cancel the event to prevent them from picking up the next emerald, and clear out the 8 emeralds from their inventory. Then add a block to their inventory.
     
  3. Offline

    JWhy

    On PlayerPickupItemEvent check whether getItem() equals one of the gem items, if so, cancel the event and add the corresponding block item to the player's inventory
     
  4. Offline

    diamondcodes

    Can you show me a Example of how I would do that?
     
  5. Offline

    GameplayJDK

    diamondcodes
    Code:java
    1. //remember to implement Listener and register your listener
    2. @EventHandler public void onItemPickup(PlayerItemPickupEvent event) {
    3. if (event.getItem().getMaterial() == Material.EMERALD) {
    4. if (event.getPlayer().getInventory().contains(Material.EMERALD, 9/* or whatever you want */)) {
    5. event.getPlayer().getInventory().remove(new ItemStack(Material.EMERALD, 9));
    6. event.getPlayer().getInventory().add(new ItemStack(Material.EMERALD_BLOCK, 1));
    7. event.getPlayer().updateInventory();
    8. }
    9. }
    10. }
     
  6. Offline

    diamondcodes

    GameplayJDK Thank you, but "Cannot resolve method getMaterial()'
     
  7. Offline

    JWhy

    diamondcodes: It's getType() instead of getMaterial(). I always found that confusing...
     
  8. Offline

    diamondcodes

    JWhy So it would look like this
    Code:
     if (event.getItem().getType() == Material.EMERALD) { 
    But then I get an error on the whole line, "Operator '==' cannot be applied to 'org.bukkit.entity.EntityType', 'org.Material'"
     
  9. Offline

    PandazNWafflez

    diamondcodes this is because the item in getItem refers to the item entity which was picked up. You actually want getItemStack() or getStack or something (a method which returns ItemStack, idk the name) and then getType on that.
     
  10. Offline

    GameplayJDK

    diamondcodes Ou, sorry, I wrote that piece of code without testing it ^^
     
Thread Status:
Not open for further replies.

Share This Page