Right Click Block to Edit Inventory

Discussion in 'Plugin Development' started by CrashBandiMan, Jan 25, 2015.

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

    CrashBandiMan

    hi im wondering how to do this:

    when a player right clicks a STONE block, it will fill up there hot bar with 1 stone block in each box, but it will also get rid of Stone block the right clicded
     
  2. Offline

    Ambamore2000

    PlayerInteractEvent, if right click, set everything in the hotbar to stone (For loop 8 times to set x number in players inventory 1 stone) and then get the player and set his hand to null or air.
     
  3. Offline

    CrashBandiMan

    would something like this work for getting the block in your inventory:
    Code:
        @EventHandler
            public void onPlayerInteract(PlayerInteractEvent event) {
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    event.getPlayer().getInventory().addItem(new ItemStack(Material.STONE, 1));
                    event.getPlayer().getInventory().addItem(new ItemStack(Material.STONE, 1));
                    event.getPlayer().getInventory().addItem(new ItemStack(Material.STONE, 1));
                    event.getPlayer().getInventory().addItem(new ItemStack(Material.STONE, 1));
                    event.getPlayer().getInventory().addItem(new ItemStack(Material.STONE, 1));
                    event.getPlayer().getInventory().addItem(new ItemStack(Material.STONE, 1));
     
  4. Offline

    ReadySetPawn

    That'll work but it's not good code. Like @Ambamore2000 said, use a for loop to get number from 0 - 8 and then add stone to the current slot using .setItem(x, new ItemStack(Material.STONE));
     
  5. Offline

    TehHypnoz

    use setItem() instead, because addItem basically adds the item to the first available slot, which might not be the hotbar. Keep in mind that setItem can replace other items though.
     
  6. Offline

    InfamousSheep

    Instead of adding all of the items individually, you could use a for loop to make it more dynamic.
    Code:
    for(int i = 0; i <= 8; i ++)
        player.getInventory().setItem(i, new ItemStack(Material.STONE, 1));
    
    This would fill up the hotbar.
     
  7. Offline

    CrashBandiMan

    ok changed code to this. but got a squiggly under "player":

    Code:
            @EventHandler
            public void onPlayerInteract(PlayerInteractEvent event) {
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    for(int i = 0; i <= 8; i ++)
                        player.getInventory().setItem(i, new ItemStack(Material.STONE, 1));
     
  8. Offline

    WinX64

    That's most likely because you haven't declared the variable player anywhere within your method.
     
    ReadySetPawn likes this.
  9. Offline

    Infuzion

    @CrashBandiMan
    You haven't declared player yet.
    Add:
    Player player = event.getPlayer();

    EDIT:
    Ninja'd
     
  10. Offline

    CrashBandiMan

    Ah i see. it worked! thanks!
     
  11. Offline

    InfamousSheep

    My bad, I should have specified player. Glad you got it working though!
     
  12. Offline

    CrashBandiMan

    so i got the event handler to work, now how to i implement that to the plugin. I went to test my plugin on my server, and when i right clicked a block, nothing happened. I am prolly missing a giant step in my code.
     
  13. Offline

    InfamousSheep

    Make sure you're registering your listeners in the onEnable() method.
     
  14. Offline

    CrashBandiMan

    @InfamousSheep
    Oh thats right. i completely forgot. let me add that in the code and test it out!

    @InfamousSheep Alright, i tested it and it registered the event. only problem is, when i clicked the block, it only gave me one block, and it placed a block directly in front of it. here is my Event:
    Code:
    @EventHandler
            public boolean onPlayerInteract(PlayerInteractEvent event) {
                Player player = event.getPlayer();
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    for(int i = 0; i <= 8; i ++)
                        player.getInventory().setItem(i, new ItemStack(Material.STONE, 1));
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  15. Offline

    Burnett

    @CrashBandiMan It should give you a full hot bar of them. To stop it placing a block you will want to cancel the event and break the block you wanted broken naturally using Block#breakNaturally. Also going by your original post you wanted it to only execute if it was a stone block that was clicked, don't forget to check that.
     
  16. Offline

    CrashBandiMan

    @Burnett
    So i changed the code to this:
    Code:
    @EventHandler
            public boolean onPlayerInteract(PlayerInteractEvent event) {
                Player player = event.getPlayer();
                if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    Block block = event.getClickedBlock();
                    if (block.getType() == Material.STONE)
                    for(int i = 0; i <= 8; i ++)
                        player.getInventory().setItem(i, new ItemStack(Material.STONE, 1));
                    event.setCancelled(true);
    it seems that any block i click it will give me a stone block. i only want to be able to click STONE blocks. The good news is now u cant place block :D
     
    Last edited: Jan 26, 2015
Thread Status:
Not open for further replies.

Share This Page