Solved InventoryClickEvent

Discussion in 'Plugin Development' started by Buizelfan2, Aug 22, 2013.

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

    Buizelfan2

    Im trying to make it so when you click on a item you cant move i almost have it

    Code:java
    1. @EventHandler
    2. public void onPlayerMoveItem(InventoryClickEvent event) {
    3. int fixedLowOffset = -54;
    4. int fixedHighOffest = -9;
    5. int numRawSlots = event.getView().countSlots();
    6. int clickedSlot = event.getRawSlot();
    7. if ((clickedSlot >= numRawSlots + fixedLowOffset) && (clickedSlot <= numRawSlots + fixedHighOffest)) {
    8. event.setCancelled(true);
    9. }
    10. if (event.getSlotType() == SlotType.QUICKBAR) {
    11. event.setCancelled(false);
    12. }
    13. }


    but when i double click on one the slots thats set to event.setCancelled(true); it allows me to pick it up but if i single click its fine
     
  2. Offline

    Dippoakabob

    Try
    Code:
    @EventHandler
        public void onPlayerMoveItem(InventoryClickEvent event) {
            if (event.getSlot() == 0){
                event.setCancelled(true);
            }
        }
    You can also add things like permission checks and stuff for instance:
    Code:
    @EventHandler
        public void onPlayerMoveItem(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            if (event.getSlot() == 0 && !player.hasPermission("test.canClick")){
                event.setCancelled(true);
            }
        }
     
  3. Offline

    Buizelfan2

    that didn't work.

    I have this
    Code:java
    1. @EventHandler
    2. public void onPlayerMoveItem(InventoryClickEvent event) {
    3. if (event.getRawSlot() < 36) {
    4. event.setCancelled(true);
    5. }
    6. }

    it all works but i still cant modify chests

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

    Buizelfan2

  5. Offline

    dunbaratu

    I see a few problems here:

    Problem 1: You are trapping any and all InventoryClickEvents, not all of which are with chests, so your code needs to verify what it is that's being interacted with. The way it's written now, you're also going to have your code effect furnaces, hoppers, brewing stands, etc, all of which can generate InventoryClickEvents. ANY time you have a GUI screen containing the player's inventory and possibly the inventory of another object up above it, the InventoryClickEvent is used.

    Problem 2: You are hardcoding the assumption that there are exactly 36 slots no matter what kind of container is being interacted with. If it's a single-chest, it's 27 slots. If it's a furnace, its only 3 (source stack, fuel stack, output stack).

    Problem 3: I suspect because of Problem #2 above that maybe what you were trying to to was freeze the PLAYER inventory not the chest inventory (you never explained that very clearly). If so then you have it backward. The low numbered raw slots are the REMOTE inventory, and the higher numbered raw slots are the PLAYER inventory. The player inventory always comes last on the screen, with the highest numbered slots. So what you need to do is this:
    fixedLowOffset = -45;
    fixedHighOffest = -9;
    numRawSlots = event.getView().countSlots();
    clickedSlot = event.getRawSlot();
    if( ( clickedSlot >= numRawSlots + fixedLowOffset )&&( clickedSlot <= numRawSlots + fixedHighOffset ) )
    {
    event.setCancelled(true);
    }
    That example would protect the slots that are in the range between 9 and 45 slots counting BACKWARD from the max slot, allowing manipulations of any slots within 9 slots of the highest slot (i.e. the hotkey row on the bottom of the player inventory), and allowing manipulations of any slots more low-numbered than 45 less than the max slot. (Note the negative signs on the offsets). It sounds like you want something roughly like this. By fixing the range of slots to the END of the slot list like this, you refer to the same slots in the player half of the inventory screen regardless of how many slots come before it in the remote container side of the inventory screen.

    The numbers -9 and -45 might not be the exact range you wanted. experiment and see, trying other numbers.

    Experiment by simply printing out a message like so every time there's a click:

    if( event.getPlayer() != null ) {
    event.getPlayer().sendMessage( "debug: clicked on slot="+event.getSlot()+", rawSlot="+event.getRawSlot()+", and numRawSlots="+event.getView().countSlots() );
    }

    To work out exactly which slot numbers you want. Be sure to try it on more than one inventory screen (try it with chest, double chest, hopper, furnace, etc) to see the difference between them.

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

    Buizelfan2

    event.player doesnt show up

    never mind i figured out a way to do it

    Well i got what i wanted to work

    Code:java
    1. @EventHandler
    2. public void onPlayerMoveItem(InventoryClickEvent event) {
    3. int fixedLowOffset = -54;
    4. int fixedHighOffest = -9;
    5. int numRawSlots = event.getView().countSlots();
    6. int clickedSlot = event.getRawSlot();
    7. if ((clickedSlot >= numRawSlots + fixedLowOffset) && (clickedSlot <= numRawSlots + fixedHighOffest)) {
    8. event.setCancelled(true);
    9. }
    10. if (event.getSlotType() == SlotType.QUICKBAR) {
    11. event.setCancelled(false);
    12. }
    13. }


    But for some reason when i double click one of the inventory slots it lets me move the item

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

Share This Page