Solved InventoryClickEvent

Discussion in 'Plugin Development' started by YoFuzzy3, Jan 24, 2013.

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

    YoFuzzy3

    This is really getting to me, so simple what I want to achieve yet it's not working, I just want to stop clicking of all items other than redstone. Except it just cancels all item clicks. What am I doing wrong? >_<

    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent event){
    3. HumanEntity humanEntity = event.getWhoClicked();
    4. if(humanEntity instanceof Player){
    5. Player player = (Player) humanEntity;
    6. if(plugin.isPoweringBoots.contains(player.getName())){
    7. if(event.getCursor().getType() != Material.REDSTONE){
    8. event.setCancelled(true);
    9. }
    10. }
    11. }
    12. }
     
  2. Offline

    KeybordPiano459

    YoFuzzy3
    You're checking if the item clicked isn't redstone :p Look at your code:
    Code:java
    1. if (event.getCursor().getType() != Material.REDSTONE) {
     
  3. Offline

    YoFuzzy3

    That's not a mistake, I want to stop clicking of all items other than redstone. Except it blocks every single item.
     
  4. Offline

    KeybordPiano459

    Oh, sorry didn't read too closely. Try checking for Material.REDSTONE_WIRE or just the data value 331 (redstone item)
     
  5. Offline

    puyttre

    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent event) {
    3. if (event.getWhoClicked() instanceof Player) {
    4. Player player = (Player) event.getWhoClicked();
    5. if (plugin.isPoweringBoots.contains(player.getName())) {
    6. if (player.getInventory().getItem(event.getSlot()).getType() != Material.REDSTONE) {
    7. event.setCancelled(true);
    8. }
    9. }
    10. }
    11. }
    In your code, you were checking to see what item was already on the cursor which is default air. You have to check the slot clicked and check their inventory to see what item is in that slot number. If the item in that slot IS redstone (331), then the event should be cancelled.

    Also, make sure not to initialize variables that are only used once like the HumanEntity variable you had in your code. It makes your code a bit faster :D

    Have a good day :)

    Edit: Updated code!
     
    YoFuzzy3 likes this.
  6. Offline

    YoFuzzy3

    Ah I see, thank you very much.
     
Thread Status:
Not open for further replies.

Share This Page