Solved Change block in inventory on click

Discussion in 'Plugin Development' started by ProMCKingz, Jul 13, 2014.

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

    ProMCKingz

    Hey!
    I was wondering how I could change the block in my hand (PISTON_BASE) and when i right click it, it will change to (PISTON_STICKY_BASE) in my inventory.
     
  2. Offline

    SimplyCoded

    I think the event is PlayerInteractEvent, do an event and check if the player right clicks and check if the item == PISTON_BLOCK then set it to the other block reply if you need an example
     
  3. Offline

    ProMCKingz

    Hey!
    Thanks for the reply!
    However this is my current code
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. Action a = event.getAction();
    4. ItemStack is = event.getItem();
    5. Player player = event.getPlayer();
    6. if (a == Action.PHYSICAL || is == null || is.getType() == Material.PISTON_BASE)
    7. return;
    8. if (is.getType() == Material.PISTON_BASE)
    9. event.setCancelled(true);
    10. player.sendMessage("You have clicked a piston");

    However this is just an identification method, which does not seem to work.
    IF you could correct me that would be great!
    Thanks,
     
  4. Offline

    SimplyCoded

    Try this Im not entirely sure this is gonan work but its a 80% chance it will xD
    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e){
            ItemStack is = e.getItem();
            if (e.getPlayer().getItemInHand().getType() == Material.PISTON_BASE &&
                    e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                is.setType(Material.PISTON_STICKY_BASE);
            }
        }
     
    ProMCKingz likes this.
  5. Offline

    ProMCKingz

    Hey!
    Thanks for the reply!
    It seems to be working :)

    However it only switches when I place the block down,
    Is there anyway to solve that, so when i right click it changes, without having to place it down?

    Thanks for your contribution, you shall be given full credit in the fully functional uploaded plugin!
     
  6. Offline

    Sgt_Tailor

    ProMCKingz e.getAction() == Action.right_clock_block || e.getAction() == Action.right_click_air.
     
    ProMCKingz likes this.
  7. Offline

    ProMCKingz

    Thank you SOOO much!
    I shall give both of you full credit on the finalized product!
    [SOLVED]
    However I have one more question.
    How can I make it so that if you hold the PISTON_STICKY_BASE, your damage will be reduced?
    I think you know what I am about to make now.. So if you have an answer that would be FANTASTIC!
    If not, thank you very very much!
    This will be my first plugin,
    And I will make a new thread,
    and you will be given full credit.
     
  8. ProMCKingz Check for both Air click and block click. Action.Physical is for other types of interaction ( pressure plate etc... )

    To reduce the damage, Check form entitydamageevent check for player and check if the player is holding the item, then use the getdamage method to get the original damage and the setdamage method to change it to something else.
     
  9. Offline

    ProMCKingz

    Another question is that how might I be able to make it NOT be placed?
    Thanks,!
     
  10. Offline

    Sgt_Tailor

    Register the EntityDamageEvent, check if they are holding the item. If so, use the setDamage method to reduce the damage.
     
  11. Offline

    MomsKnife

    This should work...
    Code:java
    1. @EventHandler
    2. public void onClick(InventoryClickEvent e) {
    3. Player p = (Player) e.getWhoClicked();
    4. if ((p.hasPermission("permission.permission") && (!(e.isRightClick())) && e.getCurrentItem().equals(Material.PISTON_BASE))) {
    5. e.getCurrentItem().setType(Material.PISTON_STICKY_BASE);
    6. }
    7. if ((p.hasPermission("permission.permission") && (!(e.isRightClick())) && e.getCurrentItem().equals(Material.PISTON_STICKY_BASE))) {
    8. e.getCurrentItem().setType(Material.PISTON_BASE);
    9. }
    10. }


    Also, this will stop it from being placed
    Code:java
    1. @EventHandler
    2.  
    3. // when a player breaks a block
    4. public void onBreak(BlockBreakEvent e) {
    5.  
    6. // get the player from the event
    7. Player p = e.getPlayer();
    8.  
    9. // if the player is holding a sticky piston
    10. if (e.getBlock().equals(Material.PISTON_STICKY_BASE)) {
    11.  
    12. // if the player has the correct permission
    13. if (p.hasPermission("permission.permission")) {
    14.  
    15. // cancel the event
    16. e.setCancelled(true);
    17. }
    18. }
    19.  
    20. // if the player is holding a regular piston
    21. if (e.getBlock().equals(Material.PISTON_BASE)) {
    22.  
    23. // if the player has the correct permission
    24. if (p.hasPermission("permission.permission")) {
    25.  
    26. // cancel the event
    27. e.setCancelled(true);
    28. }
    29. }
    30. }
    31. }

    Code:java
    1. @EventHandler
    2. // when an entity hits an entity
    3. public void onDamage(EntityDamageByEntityEvent e){
    4.  
    5. // gets the damager
    6. Player d = (Player) e.getDamager();
    7.  
    8. // if the damager is holding a regular piston
    9. if (d.getItemInHand().getType().equals((Material.PISTON_BASE))){
    10.  
    11. // set the damage that the damager is dealing to the target
    12. e.setDamage(1);
    13. }
    14. // if the damager is holding a sticky piston
    15. if (d.getItemInHand().getType().equals(Material.PISTON_STICKY_BASE)){
    16.  
    17. // set the damage that the damager is dealing to the target
    18. e.setDamage(1);
    19. }
    20. }


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

    Necrodoom

    MomsKnife you are trying to compare an itemstack to material.
    And you are trying to check if an item is two materials at the same time.
     
  13. Offline

    MomsKnife

    lolol didn't look at the code much xD
    Should be fixed now
     
  14. Offline

    Necrodoom

    MomsKnife then please at least make sure your code is correct before spoonfeeding without even explaining how and why you did what you did in the code.
     
  15. Offline

    MomsKnife

    Hope it's to your content now, haven't really been on these forums long :p
     
  16. Offline

    ProMCKingz

    Thanks so much!
    I am new to coding and have learnt alot and have thoroughly looked at why this should work!
    All of you will be given credit once again, and I will announce my plugin as of when it is out!
    Thanks again"

    Unfortunately
    I do not think that the damage one is working :(
    But thank you very much for your time ;)

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

    MomsKnife

    Pretty sure the damage has to be a double, I was just using 1 as a placeholder
     
  18. Offline

    ProMCKingz

    I believe that it does not work. I did change the values of 1 to 5 and 1. However it doesn't seem to be having any effect
     
Thread Status:
Not open for further replies.

Share This Page