Solved Disable Inventory movement [EASY?]

Discussion in 'Plugin Development' started by Dubehh, Feb 27, 2014.

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

    Dubehh

    Figured it out by myself! Thanks all!

    Final code
    Code:java
    1. if (!(event.getInventory().getType() == InventoryType.PLAYER ||
    2. event.getInventory().getType() == InventoryType.CRAFTING)) {
    3. if ((itemclicked.getType() == Material.//Material here!!! <-- {
    4. event.setCancelled(true);
    5. // do stuff
    6. }else {
    7. event.setCancelled(false);
    8. }
    9. }else {
    10. event.setCancelled(false);
    11.  
    12. }
     
  2. So you want players to be able to move items from a chest/furnace etc... into their inventory but not the other way round?
     
  3. Offline

    Dubehh

    Nope :p,
    Players should be able to move their specific 'Class items' around in their OWN inventory. But since I disabled item sharing of those 'Class items', they can still put their 'Class items' in a chest/furnace, and then they typ /kill, and then they enter that chest again and now they have 2x the 'Class items'.
    Short: InventoryClickEvent enabled in Player inventory, InventoryClickEvent disabled in other inventorys (furnace, chest, etc.)

    CODE:
    Code:java
    1. } else {
    2. if(!(event.getWhoClicked() instanceof Player)) return;
    3. if(event.getSlotType() != SlotType.CONTAINER) return;
    4. if(event.getInventory().getType() == InventoryType.PLAYER) {
    5. if ((itemclicked.getType() == //Classitems {
    6. event.setCancelled(false);
    7.  
    8. }else {
    9. event.setCancelled(true);
    10. player.playSound(player.getLocation(), Sound.NOTE_PIANO, 5, 2);
    11. }
     
  4. Offline

    Dubehh

    Anyone?
     
  5. Offline

    nuno1212sss

    Dubehh Could you put the full code of that class?
     
  6. Offline

    Dubehh

    Code:java
    1.  
    2. @EventHandler
    3. public void onChestEvent(InventoryClickEvent event) {
    4. Player player = (Player) event.getWhoClicked();
    5. ItemStack itemclicked = event.getCurrentItem();
    6. if (player.isOp()) {
    7. return;
    8. } else {
    9. if (!(event.getWhoClicked() instanceof Player))
    10. return;
    11. if (event.getSlotType() != SlotType.CONTAINER)
    12. return;
    13. if (event.getInventory().getType() == InventoryType.PLAYER) {
    14. if ((itemclicked.getType() == Material.WOOD_SPADE
    15. || itemclicked.getType() == Material.WOOD_SWORD
    16. || itemclicked.getType() == Material.WOOD_AXE
    17. || itemclicked.getType() == Material.NETHER_STAR
    18. || itemclicked.getType() == Material.GOLD_SWORD
    19. || itemclicked.getType() == Material.BOW
    20. || itemclicked.getType() == Material.ARROW
    21. || itemclicked.getType() == Material.WOOD_PICKAXE || itemclicked
    22. .getType() == Material.STONE_SWORD)
    23. && itemclicked.getItemMeta().getLore() != null) {
    24. event.setCancelled(false);
    25. } else {
    26. event.setCancelled(true);
    27. player.playSound(player.getLocation(), Sound.NOTE_PIANO, 5, 2);
    28. }
    29. }
    30. }
    31.  
    32.  
    33.  
    34. }
    35.  
    36. }


    This is it. ( its not in the main class, so all there is left is 'Public class PlayerListener implements Listener { )
     
  7. Offline

    nuno1212sss

    Dubehh NOT SURE, but I think that this code will get the Inventory that they are clicking on, not the inventory of the chest.
     
  8. Code:java
    1. @EventHandler
    2. public void onInv(InventoryClickEvent e) {
    3. Player p = (Player) e.getWhoClicked();
    4.  
    5. if(e.getCurrentItem().getType() == Material.What_Ever_You_Want() {
    6. e.setCancelled(true);
    7.  
    8. } else if(e.getCurrentItem().getType() == Material.What_Ever_You_Want_1() {
    9. e.setCancelled(true);
    10. }
    11. }


    Maybe works?
    Feel free to try it :D
     
  9. Offline

    Dubehh

    Minesuchtiiii
    This would cancel the movement in every inventory right?, I want to allow players to move the items in their OWN inventory, so like cancel everything BUT their own inventory :p Thanks for the reply though
     
  10. if (event.getInventory().getType() == InventoryType.PLAYER) {
     
  11. Guys, MAKE SURE to listen for InventoryDragEvent too!
     
    nuno1212sss likes this.
  12. Offline

    Dubehh

    bobacadodl

    What do you mean with InventoryDragEvent?

    Got it.
    I forgot that inventoryType.CRAFTING is ALSO in the players inventory, so I had to allow that too!
    Thanks, I figured it out.

    To look at the final code, scroll up!

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

Share This Page