Adding Items By Clicking Blocks!

Discussion in 'Plugin Development' started by Chibbey, Jan 4, 2014.

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

    Chibbey

    Hello, So I'm making a plugin which when someone with permission tried to break a block it will be like their in creative. So for example if they try breaking a Diamond ore with a wood pickaxe it will remove the block and give them the block in their inventory. I'm just making this for fun. But for adding Items I want to know how to add items to their inventory depending on the item clicked. So I have it so it checks for permissions, and check to see if its one of the ores, and I check to see if their holding a pickaxe type. Now I just cancel the event set the block to AIR and for adding items this is what I tried doing but it doesn't work
    Code:java
    1. p.getInventory().addItem(e.getClickedBlock().getType());

    So the addItem is underlined in red and it says "

    The method addItem(ItemStack...) in the type Inventory is not applicable for the arguments (Material)

    "

    Please help me!
     
  2. Offline

    GaaTavares

    Create a new itemstack based on the block type.
    ItemStack is = new ItemStack(e.getClickedBlock().getType());
    p.getInventory().addItem(is);
     
  3. Offline

    Chibbey

    GaaTavares
    Thanks! It worked!

    GaaTavares
    Nevermind... I thought it worked :( it didn't. Could someone help me please!
    here is my code!
    Code:java
    1. package me.chibbey.orebreaker;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class OreBreaker extends JavaPlugin implements Listener {
    15.  
    16. public void onEnable() {
    17. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    18. }
    19.  
    20. @EventHandler
    21. public void onPlayerInteract(PlayerInteractEvent e) {
    22. Player p = (Player) e.getPlayer();
    23. if (p.hasPermission("orebreaker.use")) {
    24. if (!(e.getAction() == Action.LEFT_CLICK_BLOCK)) return;
    25. if (e.getClickedBlock().getType() == Material.IRON_ORE || e.getClickedBlock().getType() == Material.DIAMOND_ORE || e.getClickedBlock().getType() == Material.GOLD_ORE || e.getClickedBlock().getType() == Material.COAL_ORE) {
    26. if (e.getItem().getType() == Material.WOOD_PICKAXE || e.getItem().getType() == Material.STONE_PICKAXE || e.getItem().getType() == Material.IRON_PICKAXE || e.getItem().getType() == Material.GOLD_PICKAXE || e.getItem().getType() == Material.DIAMOND_PICKAXE) {
    27. e.setCancelled(true);
    28. e.getClickedBlock().setType(Material.AIR);
    29. ItemStack clickedblock = new ItemStack(e.getClickedBlock().getType());
    30. p.getInventory().addItem(clickedblock);
    31. p.sendMessage(ChatColor.DARK_AQUA + "Got block!");
    32. }
    33. }
    34. }
    35. }
    36. }

    it sends the message "Got Block!". Just it wont add the item :(. it sets it to air after I hit it with the item. So EVERYTHING works besides it giving me the block. :( please help!

    bump

    woops, sorry I forgot you have to bump after 24 hours :(

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

    xTigerRebornx

    Chibbey Update the player's inventory using p.updateInventory() (I believe thats the method), it will be deprecated, but it should fix the problem
     
  5. Offline

    Chibbey

  6. Offline

    xTigerRebornx

  7. Offline

    Chibbey

    xTigerRebornx Ok
    Code:java
    1. package me.chibbey.orebreaker;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class OreBreaker extends JavaPlugin implements Listener {
    15.  
    16. public void onEnable() {
    17. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    18. }
    19.  
    20. @SuppressWarnings("deprecation")
    21. @EventHandler
    22. public void onPlayerInteract(PlayerInteractEvent e) {
    23. Player p = (Player) e.getPlayer();
    24. if (p.hasPermission("orebreaker.use")) {
    25. if (!(e.getAction() == Action.LEFT_CLICK_BLOCK)) return;
    26. if (e.getClickedBlock().getType() == Material.IRON_ORE || e.getClickedBlock().getType() == Material.DIAMOND_ORE || e.getClickedBlock().getType() == Material.GOLD_ORE || e.getClickedBlock().getType() == Material.COAL_ORE) {
    27. if (e.getItem().getType() == Material.WOOD_PICKAXE || e.getItem().getType() == Material.STONE_PICKAXE || e.getItem().getType() == Material.IRON_PICKAXE || e.getItem().getType() == Material.GOLD_PICKAXE || e.getItem().getType() == Material.DIAMOND_PICKAXE) {
    28. e.setCancelled(true);
    29. e.getClickedBlock().setType(Material.AIR);
    30. ItemStack clickedblock = new ItemStack(e.getClickedBlock().getType());
    31. p.getInventory().addItem(clickedblock);
    32. p.updateInventory();
    33. p.sendMessage(ChatColor.DARK_AQUA + "Got block!");
    34. }
    35. }
    36. }
    37. }
    38. }


    Help please.. It cant be that hard..

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

    xTigerRebornx

    Chibbey Try creating the ItemStack before you set the type of the block to air, since you are creating it after, you are just creating an ItemStack of AIR
     
  9. Offline

    Chibbey

    xTigerRebornx
    Good idea ill test it right now!

    xTigerRebornx xTigerRebornx xTigerRebornx xTigerRebornx xTigerRebornx xTigerRebornx
    THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!I LOVE U!!!!!!!!!!!!!!!

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

    GaaTavares

    Sorry I couldn't reply in time .-.
     
  11. Offline

    Chibbey

Thread Status:
Not open for further replies.

Share This Page