Small Inventory Problem

Discussion in 'Plugin Development' started by Danelli, Nov 30, 2013.

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

    Danelli

    So I'm developing a shield plugin. It's done nearly, but I have one task to complete.

    I decided to have the shield represented by an iron chestplate however I do not want the player to be able to put this iron chestplate on. So far I have removed the player's ability to right click the armor on and shift click the armor on:

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler (priority = EventPriority.HIGHEST)
    3. public void onPlayerInteract(PlayerInteractEvent e){
    4. Player p = e.getPlayer();
    5. Action action = e.getAction();
    6. if (action == Action.RIGHT_CLICK_BLOCK || action == Action.RIGHT_CLICK_AIR)
    7. if(p.getItemInHand().getType() == Material.IRON_CHESTPLATE)
    8. if ((p.getItemInHand().getItemMeta().equals(IronRecipes.getShield().getItemMeta()))) {
    9. e.setCancelled(true);
    10. p.updateInventory();
    11. }
    12. }


    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler()
    3. public void onClick(InventoryClickEvent e) {
    4. if(e.isShiftClick()) {
    5. Player p = (Player) e.getWhoClicked();
    6. if (e.getView().countSlots() != 41) return;
    7. if (e.getCurrentItem().getType() == Material.IRON_CHESTPLATE)
    8. if ((e.getCurrentItem().getItemMeta()).equals(IronRecipes.getShield().getItemMeta())) {
    9. e.setCancelled(true);
    10. p.updateInventory();
    11. p.sendMessage("Your shield is too heavy to put on your back.");
    12. }
    13. }
    14. }


    How would one write an event to determine if the item is being put in an armor slot?
     
  2. Offline

    willy00

    Code:java
    1. if (player.getInventory().getChestplate() == new ItemStack(Material.IRON_CHESTPLATE)); //if the player has an Iron Chestplate
    2. player.getInventory().setChestplate(null); //remove the chestplate
    3. player.getInventory().addItem(new ItemStack(Material.IRON_CHESTPLATE)); //give them back the chestplate
     
Thread Status:
Not open for further replies.

Share This Page