How do I check if an Inventory is the same as another?

Discussion in 'Plugin Development' started by mykoxaxoxa, Jul 12, 2014.

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

    mykoxaxoxa

    I tried with this but it didn't work:
    Code:java
    1. this.inv = Bukkit.createInventory(null, InventoryType.CHEST);

    Code:java
    1. if (ev.getInventory().equals(this.getInv()))


    This is the whole class:
    Code:java
    1. /*
    2. * To change this license header, choose License Headers in Project Properties.
    3. * To change this template file, choose Tools | Templates
    4. * and open the template in the editor.
    5. */
    6.  
    7. package tk.mina47.mina47juegos;
    8.  
    9. import java.util.HashMap;
    10. import java.util.logging.Logger;
    11. import org.bukkit.Bukkit;
    12. import org.bukkit.Material;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.inventory.InventoryClickEvent;
    18. import org.bukkit.event.inventory.InventoryType;
    19. import org.bukkit.inventory.Inventory;
    20. import org.bukkit.inventory.InventoryView;
    21. import org.bukkit.inventory.ItemStack;
    22.  
    23. /**
    24. *
    25. * @author Admin
    26. */
    27. public class InvMenu implements Listener {
    28. private static final Logger LOG = Logger.getLogger(InvMenu.class.getName());
    29. private final Inventory inv;
    30. @SuppressWarnings("unchecked")
    31. private final HashMap<Integer,String> action;
    32.  
    33. public InvMenu()
    34. {
    35. this.action = new HashMap();
    36. Bukkit.getPluginManager().registerEvents(this, Bukkit.getPluginManager().getPlugin("Mina47Core"));
    37. this.inv = Bukkit.createInventory(null, InventoryType.CHEST);
    38. }
    39.  
    40. public Inventory getInv()
    41. {
    42. return this.inv;
    43. }
    44.  
    45. public void open(Player player)
    46. {
    47. InventoryView opInv = player.openInventory(this.inv);
    48. }
    49.  
    50. public void setItem(int Pos, ItemStack i, String action)
    51. {
    52. this.action.put(Pos, action);
    53. this.inv.setItem(Pos, i);
    54.  
    55. }
    56.  
    57. @EventHandler
    58. public void invClick(InventoryClickEvent ev)
    59. {
    60. LOG.info("InvClick!");
    61. ItemStack i = ev.getCurrentItem();
    62. if (ev.getInventory().equals(this.getInv()))
    63. {
    64. LOG.info("InvMenuClick");
    65. //ev.setCancelled(true);
    66. int slot = ev.getSlot();
    67. Bukkit.dispatchCommand((CommandSender) ev.getWhoClicked(),this.action.get(slot));
    68. ItemStack ii = new ItemStack(Material.AIR);
    69. ev.getView().setCursor(ii);
    70. }
    71. }
    72.  
    73. }
    74.  
     
  2. Offline

    Aengo

    mykoxaxoxa likes this.
  3. Offline

    mykoxaxoxa

    The title should be the same in both because it does not change but LOL I forgot about the contents! xDxDxD Obviously it will not be the same inventory lol.

    Edit: Nope, It should be the same because is the same inventory that is opened.
     
  4. Offline

    Pik0

    mykoxaxoxa
    Code:java
    1. Player player1 = ....
    2. Player player2 = ....
    3. boolean equal = true;
    4. for(int i = 0; i < player1.getInventory().getContents().length; i++){
    5. /*
    6. * You can instead of checking if a itemstack is
    7. * equal to other, check the Type and Data.
    8. * Keep in mind that everything should be equal in
    9. * case of checking a itemstack(durability, meta, * etc)
    10. */
    11. if(player1.getInventory().getItem(i) != player2.getInventory().getItem(i){
    12. equal = false;
    13. break;
    14. }
    15. }
     
  5. Offline

    mykoxaxoxa

    But this would check the contents, I want to make an unique inventory. I have opened the inventory and I want to check if the inventory that the player clicked is the same that was opened.

    Well, I compared the InventoryView instead of the inventory. This way works.

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

Share This Page