How to change a item when player right clicks

Discussion in 'Plugin Development' started by RuthlessRage, Sep 29, 2014.

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

    RuthlessRage

    player has oh idk lets say a stone block. id like for if the player has the stone block in hand and they right click it, the stone block will change to bedrock. heres what i tried

    public void onBangeUse(PlayerInteractEntityEvent e) {
    Player player = e.getPlayer();
    Player clicked = (Player) e.getRightClicked();
    if (!(clicked instanceof Player))
    return;
    if (player.getItemInHand().getType().equals(Material.STONE)) {
    player.sendMessage(ChatColor.GREEN + "swag");
    player.getInventory().setItem(7, new ItemStack(Material.BEDROCK, 1));




    i know player.getInventory().setItem(7, new ItemStack(Material.BEDROCK, 1));} is making the bedrock go into the 7th slot.concluding the stone block is located in the 7th slot. might sound weird why i want to do this idk but help would be mush appreciated thanks
     
  2. Offline

    Zombieghost_391

    Code:java
    1. @EventHandler
    2. public void InteractEvent(PlayerInteractEvent e){
    3. Player p = e.getPlayer(); //Getting the player
    4. Material m = e.getPlayer().getItemInHand().getType(); //Getting the block type
    5. if (m.equals(Material.STONE)) { //Checking if block type is equal to stone block
    6. Action a = e.getAction(); //Getting the action
    7. if (a.equals(Action.RIGHT_CLICK_AIR) || a.equals(Action.RIGHT_CLICK_BLOCK)) { //Checking action
    8. int i = p.getItemInHand().getAmount(); //Saveing the amount so that the player gets the same amount as before (If you want to remove this just remove this line and remove the ",i" on the next line
    9. p.setItemInHand(new ItemStack(Material.BEDROCK, i)); //Setting the item in hand to bedrock
    10. p.sendMessage(ChatColor.GREEN + "swag"); //Sending players message that this is completed
    11. /**
    12.   * Now feel clever :D
    13.   */
    14. }
    15. }
    16. }
     
  3. Offline

    RuthlessRage

    Zombieghost_391 what about if a player opens their inventory and has stone in their inventory. if they were to click on the stone and try to drop it or move its slot place id like for it to be cancelled. heres what ive tried

    public void InventoryClickEvent(InventoryClickEvent event) {
    Player p = (Player) event.getWhoClicked();

    ItemStack itemclicked = event.getCurrentItem();
    if (!(p instanceof Player))
    return;
    if (event.getInventory().getType() == InventoryType.PLAYER) {
    if ((itemclicked.getType() == Material.STONE)){
    event.setCancelled(true);
    p.sendMessage("You arent allowed to change this slot");
    }
    }
    return;
    }
    }
     
  4. Offline

    MineStein

    To walk you through without spoon-feeding:
    - Listen for a PlayerInteractEvent.
    - Ensure the material is the equivalent of the desired one.
    - Make sure your action is setup how you want it.
    - Set the players item in their hand

    Done.

    EDIT:
    Event#setCancelled should work?
     
  5. Offline

    RuthlessRage

    that is irrelevant to this connatative this is what i asked about i have tried several code myself so honestly dont know what you mean by spoon fedding

    if a player opens their inventory and has stone in their inventory. if they were to click on the stone and try to drop it or move its slot place id like for it to be cancelled. heres what ive tried

    public void InventoryClickEvent(InventoryClickEvent event) {
    Player p = (Player) event.getWhoClicked();

    ItemStack itemclicked = event.getCurrentItem();
    if (!(p instanceof Player))
    return;
    if (event.getInventory().getType() == InventoryType.PLAYER) {
    if ((itemclicked.getType() == Material.STONE)){
    event.setCancelled(true);
    p.sendMessage("You arent allowed to change this slot");
    }
    }
    return;
    }
    }
     
  6. Offline

    MineStein

    Spoon feeding means that someone gives you the code for something without explaining it/making you write it yourself. You never learn from copy-pasting.

    I apologize for trying to actually help you. I won't try to again :)
     
  7. Offline

    RuthlessRage


    no need to apologize your ''help'' wasnt that great
     
  8. Offline

    MineStein

    RuthlessRage Apparently it wasn't. This really shouldn't be that hard for you.
     
Thread Status:
Not open for further replies.

Share This Page