Activate something upon armor remove/place?

Discussion in 'Plugin Development' started by theViTALiTY, Jun 8, 2014.

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

    theViTALiTY

    Hi,

    So im making this simple plugin which allows u to fly if u are wearing the specific piece of armor.

    Now, what i dont know how to achieve is, the way to check when the player placed the that specific piece of armor on himself or removed it.

    Lets say we are using chestplate. As soon as he puts the chestplate on himself, he is granted flying, and as soon as he removes it, he cant fly anymore. So all suggestions are welcome.
     
  2. Offline

    MineCrashKoen

    You can use an InventoryDragEvent. Then check if the player is wearing, for example, a diamondchestplate. If this is true, set his flying to true. If it is not, set it so false.

    For example:
    Code:
    if(player.getInventory().getChestPlate().getType() == Material.DIAMOND_CHESTPLATE)
      player.setFlying(true);
    else player.setFlying(false)
     
  3. Offline

    theViTALiTY

    MineCrashKoen

    How would i get the player? Since in the DragEvent it seems i cant get the player via

    Code:java
    1. @EventHandler
    2. public void onInventoryDrag(InventoryDragEvent e) {
    3. Player p = e.getPlayer();
    4.  
    5. }
    6.  
     
  4. Offline

    MineCrashKoen

    Use this:
    Code:
    if(e.getWhoClicked() instanceof Player)
    {
      Player p = (Player) e.getWhoClicked();
    }
     
  5. Offline

    Drkmaster83

    Err - I'm not sure InventoryDragEvent is universal for something like this... Why not InventoryClickEvent?
    When he clicks, check if the cursor has an item, if it does, check if it's a chestplate. If it is, then let the event pass and grant the player fly mode. Else, let the event pass and don't grant the player fly mode. You could also check for the specific slots of a PlayerInventory object if you want to make sure he's clicking the chestplate in the chestplate armor slot:
    [​IMG]
    Edit: As an afterthought, you might also want to check the Player's gamemode and any other data involving MetaData, ItemMeta, or whatnot of the item.
     
  6. Offline

    theViTALiTY

    Drkmaster83

    Wait, i get it to the part when u say i can check specific slot. I need it to grant fly when he places chestplace in the chestplate slot, and when he removes it, fly is removed. I dont want to check it right away as he click the chestplate and starts dragin it across inventory. So, click a chestplate, once placed in the chestplate slot, can fly, when removed, flying removed.

    And for the other part, ive got a custom recipe for that armor, and the way im checking for it is if the ItemMeta displayname matches the one of the chestplate :D
     
  7. Offline

    MrSnare

    Hi. This is a bit of code I made a few years ago for boots that make you fly if you put them on and stop you flying if you take them off. Hopefully you can adapt it to your needs. I have commented it so you will be able to understand
    Code:java
    1. @EventHandler
    2. public void onCloseInventory(InventoryCloseEvent ev){
    3. //if the inventory is a player's inventory
    4. if(ev.getView().getType() == InventoryType.CRAFTING || ev.getView().getType() == InventoryType.CREATIVE){
    5. Player player = (Player) ev.getPlayer();
    6. //get the players armor
    7. ItemStack[] armor = player.getInventory().getArmorContents();
    8. //cycle through the armor
    9. for(int i = 0; i < armor.length; i++){
    10. if(armor[i].hasItemMeta()){
    11. String itemName = getItemName(armor[i]);
    12. //if the item doesnt have a special name e.g. "fly boots" return.
    13. if(itemName != null){
    14. //let the player fly
    15. }
    16. }
    17. }
    18. }
    19. }
    20.  
    21. @EventHandler
    22. public void onInventoryClick(final InventoryClickEvent ev){
    23. final Player player = (Player) ev.getWhoClicked();
    24. //if the clicked slot is an armor slot or the hovered item is boots
    25. if(ev.getSlotType() == InventoryType.SlotType.ARMOR || ev.getCursor().getType().equals(Material.LEATHER_BOOTS)){
    26. //get the players armor
    27. final ItemStack[] armor = player.getInventory().getArmorContents();
    28. //wait one tick and check the players armor again
    29. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    30. public void run() {
    31. //get the players armor
    32. ItemStack[] armor2 = player.getInventory().getArmorContents();
    33. for(int i = 0; i < armor.length; i++){
    34. //compare the armor before and after the event
    35. if(armor[i] != armor2[i] && armor[i].hasItemMeta()){
    36. String itemName = getItemName(armor[i]);
    37. if(itemName != null){
    38. //stop flying the player
    39. }
    40. }
    41. }
    42. }
    43. }, 1L);
    44. }
    45. }[/i][/i][/i][/i][/i][/i]


    I forgot this method
    private String getItemName(ItemStack item) {
    return ChatColor.stripColor(item.getItemMeta().getDisplayName());
    }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page