Java Code Examples for org.bukkit.event.inventory.InventoryClickEvent#getView()

The following examples show how to use org.bukkit.event.inventory.InventoryClickEvent#getView() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MasonryPane.java    From IF with The Unlicense 5 votes vote down vote up
@Override
public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int paneOffsetX, int paneOffsetY,
                     int maxLength, int maxHeight) {
    int length = Math.min(this.length, maxLength);
    int height = Math.min(this.height, maxHeight);

    int slot = event.getSlot();
    InventoryView view = event.getView();
    Inventory inventory = view.getInventory(event.getRawSlot());

    int x, y;

    if (inventory != null && inventory.equals(view.getBottomInventory())) {
        x = (slot % 9) - getX() - paneOffsetX;
        y = ((slot / 9) + gui.getRows() - 1) - getY() - paneOffsetY;

        if (slot / 9 == 0) {
            y = (gui.getRows() + 3) - getY() - paneOffsetY;
        }
    } else {
        x = (slot % 9) - getX() - paneOffsetX;
        y = (slot / 9) - getY() - paneOffsetY;
    }

    if (x < 0 || x >= length || y < 0 || y >= height)
        return false;

    callOnClick(event);

    boolean success = false;

    for (Pane pane : panes) {
        success = success || pane.click(gui, event, paneOffsetX + getX(),
            paneOffsetY + getY(), length, height);
    }

    return success;
}
 
Example 2
Source File: PercentageBar.java    From IF with The Unlicense 5 votes vote down vote up
@Override
public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int paneOffsetX, int paneOffsetY,
                     int maxLength, int maxHeight) {
    int length = Math.min(this.length, maxLength);
    int height = Math.min(this.height, maxHeight);

    int slot = event.getSlot();
    InventoryView view = event.getView();
    Inventory inventory = view.getInventory(event.getRawSlot());

    int x, y;

    if (inventory != null && inventory.equals(view.getBottomInventory())) {
        x = (slot % 9) - getX() - paneOffsetX;
        y = ((slot / 9) + gui.getRows() - 1) - getY() - paneOffsetY;

        if (slot / 9 == 0) {
            y = (gui.getRows() + 3) - getY() - paneOffsetY;
        }
    } else {
        x = (slot % 9) - getX() - paneOffsetX;
        y = (slot / 9) - getY() - paneOffsetY;
    }

    if (x < 0 || x >= length || y < 0 || y >= height) {
        return false;
    }

    callOnClick(event);

    event.setCancelled(true);

    int newPaneOffsetX = paneOffsetX + getX();
    int newPaneOffsetY = paneOffsetY + getY();

    return this.fillPane.click(gui, event, newPaneOffsetX, newPaneOffsetY, length, height) ||
        this.backgroundPane.click(gui, event, newPaneOffsetX, newPaneOffsetY, length, height);
}
 
Example 3
Source File: CompatibilityUtils.java    From NovaGuilds with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets clicked inventory
 * For API older than 1.8
 *
 * @param event inventory click event
 * @return inventory
 */
public static Inventory getClickedInventory(InventoryClickEvent event) {
	int slot = event.getRawSlot();
	InventoryView view = event.getView();

	if(slot < 0) {
		return null;
	}
	else if(view.getTopInventory() != null && slot < view.getTopInventory().getSize()) {
		return view.getTopInventory();
	}
	else {
		return view.getBottomInventory();
	}
}
 
Example 4
Source File: PaginatedPane.java    From IF with The Unlicense 4 votes vote down vote up
@Override
  public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int paneOffsetX, int paneOffsetY, int maxLength,
                       int maxHeight) {
      int length = Math.min(this.length, maxLength);
      int height = Math.min(this.height, maxHeight);

      int slot = event.getSlot();
      InventoryView view = event.getView();
      Inventory inventory = view.getInventory(event.getRawSlot());

      int x, y;

      if (inventory != null && inventory.equals(view.getBottomInventory())) {
          x = (slot % 9) - getX() - paneOffsetX;
          y = ((slot / 9) + gui.getRows() - 1) - getY() - paneOffsetY;

          if (slot / 9 == 0) {
              y = (gui.getRows() + 3) - getY() - paneOffsetY;
          }
      } else {
          x = (slot % 9) - getX() - paneOffsetX;
          y = (slot / 9) - getY() - paneOffsetY;
      }

      //this isn't our item
      if (x < 0 || x >= length || y < 0 || y >= height) {
          return false;
      }

callOnClick(event);

      List<Pane> panes = this.panes.get(page);

      if (panes == null) {
          return false;
      }

      boolean success = false;

      for (Pane pane : panes) {
          success = success || pane.click(gui, event, paneOffsetX + getX(),
              paneOffsetY + getY(), length, height);
      }

      return success;
  }
 
Example 5
Source File: StaticPane.java    From IF with The Unlicense 4 votes vote down vote up
@Override
public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int paneOffsetX, int paneOffsetY,
                        int maxLength, int maxHeight) {
	int length = Math.min(this.length, maxLength);
	int height = Math.min(this.height, maxHeight);

	int slot = event.getSlot();
       InventoryView view = event.getView();
       Inventory inventory = view.getInventory(event.getRawSlot());

       int x, y;

       if (inventory != null && inventory.equals(view.getBottomInventory())) {
           x = (slot % 9) - getX() - paneOffsetX;
           y = ((slot / 9) + gui.getRows() - 1) - getY() - paneOffsetY;

           if (slot / 9 == 0) {
               y = (gui.getRows() + 3) - getY() - paneOffsetY;
           }
       } else {
           x = (slot % 9) - getX() - paneOffsetX;
           y = (slot / 9) - getY() - paneOffsetY;
       }

	//this isn't our item
	if (x < 0 || x >= length || y < 0 || y >= height)
		return false;

	callOnClick(event);

       ItemStack itemStack = event.getCurrentItem();

       if (itemStack == null) {
           return false;
       }

       GuiItem clickedItem = findMatchingItem(items.values(), itemStack);

       if (clickedItem == null) {
           return false;
       }

       clickedItem.callAction(event);

       return true;
}
 
Example 6
Source File: OutlinePane.java    From IF with The Unlicense 4 votes vote down vote up
@Override
public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int paneOffsetX, int paneOffsetY,
                     int maxLength, int maxHeight) {
    int length = Math.min(this.length, maxLength);
    int height = Math.min(this.height, maxHeight);

    int slot = event.getSlot();
    InventoryView inventoryView = event.getView();
    Inventory inventory = inventoryView.getInventory(event.getRawSlot());

    int x, y;

    if (inventory != null && inventory.equals(inventoryView.getBottomInventory())) {
        x = (slot % 9) - getX() - paneOffsetX;
        y = ((slot / 9) + gui.getRows() - 1) - getY() - paneOffsetY;

        if (slot / 9 == 0) {
            y = (gui.getRows() + 3) - getY() - paneOffsetY;
        }
    } else {
        x = (slot % 9) - getX() - paneOffsetX;
        y = (slot / 9) - getY() - paneOffsetY;
    }

    //this isn't our item
    if (x < 0 || x >= length || y < 0 || y >= height)
        return false;

    callOnClick(event);

    ItemStack itemStack = event.getCurrentItem();

    if (itemStack == null) {
        return false;
    }

    GuiItem item = findMatchingItem(items, itemStack);

    if (item == null) {
        return false;
    }

    item.callAction(event);

    return true;
}
 
Example 7
Source File: Slider.java    From IF with The Unlicense 4 votes vote down vote up
@Override
public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int paneOffsetX, int paneOffsetY,
                     int maxLength, int maxHeight) {
    int length = Math.min(this.length, maxLength);
    int height = Math.min(this.height, maxHeight);

    int slot = event.getSlot();
    InventoryView view = event.getView();
    Inventory inventory = view.getInventory(event.getRawSlot());

    int x, y;

    if (inventory != null && inventory.equals(view.getBottomInventory())) {
        x = (slot % 9) - getX() - paneOffsetX;
        y = ((slot / 9) + gui.getRows() - 1) - getY() - paneOffsetY;

        if (slot / 9 == 0) {
            y = (gui.getRows() + 3) - getY() - paneOffsetY;
        }
    } else {
        x = (slot % 9) - getX() - paneOffsetX;
        y = (slot / 9) - getY() - paneOffsetY;
    }

    if (x < 0 || x >= length || y < 0 || y >= height) {
        return false;
    }

    callOnClick(event);

    int newPaneOffsetX = paneOffsetX + getX();
    int newPaneOffsetY = paneOffsetY + getY();

    boolean success = this.fillPane.click(gui, event, newPaneOffsetX, newPaneOffsetY, length, height) ||
        this.backgroundPane.click(gui, event, newPaneOffsetX, newPaneOffsetY, length, height);

    if (orientation == Orientation.HORIZONTAL) {
        setValue((float) (x + 1) / length);
    } else if (orientation == Orientation.VERTICAL) {
        setValue((float) (y + 1) / height);
    } else {
        throw new UnsupportedOperationException("Unknown orientation");
    }

    gui.update();

    return success;
}
 
Example 8
Source File: CycleButton.java    From IF with The Unlicense 4 votes vote down vote up
@Override
public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int paneOffsetX, int paneOffsetY,
                     int maxLength, int maxHeight) {
    int length = Math.min(this.length, maxLength);
    int height = Math.min(this.height, maxHeight);

    int slot = event.getSlot();
    InventoryView view = event.getView();
    Inventory inventory = view.getInventory(event.getRawSlot());

    int x, y;

    if (inventory != null && inventory.equals(view.getBottomInventory())) {
        x = (slot % 9) - getX() - paneOffsetX;
        y = ((slot / 9) + gui.getRows() - 1) - getY() - paneOffsetY;

        if (slot / 9 == 0) {
            y = (gui.getRows() + 3) - getY() - paneOffsetY;
        }
    } else {
        x = (slot % 9) - getX() - paneOffsetX;
        y = (slot / 9) - getY() - paneOffsetY;
    }

    //this isn't our item
    if (x < 0 || x >= length || y < 0 || y >= height) {
        return false;
    }

    callOnClick(event);

    panes.get(position).click(gui, event, paneOffsetX + x, paneOffsetY + y, length, height);

    position++;

    if (position == panes.size()) {
        position = 0;
    }

    gui.update();

    return true;
}
 
Example 9
Source File: ToggleButton.java    From IF with The Unlicense 4 votes vote down vote up
@Override
public boolean click(@NotNull Gui gui, @NotNull InventoryClickEvent event, int paneOffsetX, int paneOffsetY,
                     int maxLength, int maxHeight) {
    int length = Math.min(this.length, maxLength);
    int height = Math.min(this.height, maxHeight);

    int slot = event.getSlot();
    InventoryView view = event.getView();
    Inventory inventory = view.getInventory(event.getRawSlot());

    int x, y;

    if (inventory != null && inventory.equals(view.getBottomInventory())) {
        x = (slot % 9) - getX() - paneOffsetX;
        y = ((slot / 9) + gui.getRows() - 1) - getY() - paneOffsetY;

        if (slot / 9 == 0) {
            y = (gui.getRows() + 3) - getY() - paneOffsetY;
        }
    } else {
        x = (slot % 9) - getX() - paneOffsetX;
        y = (slot / 9) - getY() - paneOffsetY;
    }

    //this isn't our item
    if (x < 0 || x >= length || y < 0 || y >= height) {
        return false;
    }

    callOnClick(event);

    int newX = paneOffsetX + x;
    int newY = paneOffsetY + y;

    if (enabled) {
        enabledPane.click(gui, event, newX, newY, length, height);
    } else {
        disabledPane.click(gui, event, newX, newY, length, height);
    }

    toggle();

    gui.update();

    return true;
}
 
Example 10
Source File: ItemRenameListener.java    From EnchantmentsEnhance with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
    public void onInventoryClick(InventoryClickEvent e) {
// check if the event has been cancelled by another plugin
        if (!e.isCancelled()) {
            HumanEntity ent = e.getWhoClicked();

// not really necessary
            if (ent instanceof Player) {
                Player player = (Player) ent;
                Inventory inv = e.getInventory();

// see if the event is about an anvil
                if (inv instanceof AnvilInventory) {
                    InventoryView view = e.getView();
                    int rawSlot = e.getRawSlot();

// compare the raw slot with the inventory view to make sure we are talking about the upper inventory
                    if (rawSlot == view.convertSlot(rawSlot)) {
/*
slot 0 = left item slot
slot 1 = right item slot
slot 2 = result item slot

see if the player clicked in the result item slot of the anvil inventory
*/
                        if (rawSlot == 2) {
/*
get the current item in the result slot
I think inv.getItem(rawSlot) would be possible too
*/
                            ItemStack item = e.getCurrentItem();

// check if there is an item in the result slot
                            if (item != null) {
                                ItemMeta meta = item.getItemMeta();

// it is possible that the item does not have meta data
                                if (meta != null) {
// see whether the item is beeing renamed
                                    if (meta.hasDisplayName()) {
                                        String displayName = meta.getDisplayName();
                                        ItemManager.setName(item, displayName);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
 
Example 11
Source File: CustomItemManager.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST) 
public void OnInventoryClick(InventoryClickEvent event) {
	boolean currentEmpty = (event.getCurrentItem() == null) || (ItemManager.getId(event.getCurrentItem()) == CivData.AIR);
	boolean cursorEmpty = (event.getCursor() == null) || (ItemManager.getId(event.getCursor()) == CivData.AIR);
	
	if (currentEmpty && cursorEmpty) {
		return;
	}
	
	convertLegacyItem(event);
	
	if (event.getRawSlot() < 0) {
		//I guess this means "drop the item?"
		
		//CivLog.debug("GET RAW WAS NEGATIVE, cancel this event it should be invalid.");
		//event.setResult(Result.DENY);
		//event.setCancelled(true);
		
		//try {
		//	Player player = CivGlobal.getPlayer(event.getWhoClicked().getName());
		//	player.updateInventory();
		//} catch (CivException e) {
		//}
		
		return;
	}
	
	InventoryView view = event.getView();
	Inventory clickedInv;
	Inventory otherInv;
	
	if (view.getType().equals(InventoryType.CRAFTING)) {
		//This is the player's own inventory. For some reason it requires
		//special treatment. The 'top' inventory is the 2x2 crafting
		//area plus the output. During shift click, items do not go there
		//so the otherInv should always be the player's inventory aka the bottom.
		if (event.getRawSlot() <= 4) {
			clickedInv = view.getTopInventory();
			otherInv = view.getBottomInventory();
		} else {
			clickedInv = view.getBottomInventory();
			otherInv = view.getBottomInventory();
		}
	} else {
		if (event.getRawSlot() == view.convertSlot(event.getRawSlot())) {
			//Clicked in the top holder
			clickedInv = view.getTopInventory();
			otherInv = view.getBottomInventory();
		} else {
			clickedInv = view.getBottomInventory();
			otherInv = view.getTopInventory();
		}
	}
	
	LoreMaterial current = LoreMaterial.getMaterial(event.getCurrentItem());
	LoreMaterial cursor = LoreMaterial.getMaterial(event.getCursor());
	
	if (event.isShiftClick()) {
		// Shift click is _always_ current item.
	//	CustomItemStack is = new CustomItemStack(event.getCurrentItem());
		if (current != null) {
		//if (is.isCustomItem() && (is.getMaterial() instanceof CustomMaterialExtended)) {
			// Calling onInvShiftClick Event.
			//((CustomMaterialExtended)is.getMaterial()).onInvShiftClick(event, clickedInv, otherInv, is.getItem());
			current.onInvShiftClick(event, clickedInv, otherInv, event.getCurrentItem());
		//}
		}
		
	} else {
		
		if (!currentEmpty && !cursorEmpty) {
			//CustomItemStack currentIs = new CustomItemStack(event.getCurrentItem());
			//CustomItemStack cursorIs = new CustomItemStack(event.getCursor());
			
			if (current != null) {
				current.onInvItemSwap(event, clickedInv, event.getCursor(), event.getCurrentItem());
			}
			
			if (cursor != null) {
				cursor.onInvItemSwap(event, clickedInv, event.getCursor(), event.getCurrentItem());
			}
		} else if (!currentEmpty) {
			// This is a pickup event.
			//CustomItemStack is = new CustomItemStack(event.getCurrentItem());
			if (current != null) {
				// Calling onInvItemPickup Event.
				current.onInvItemPickup(event, clickedInv, event.getCurrentItem());
			}
		} else {
			// Implied !cursorEmpty
			if (cursor != null) {
				// Calling onInvItemDrop Event.
				cursor.onInvItemDrop(event, clickedInv, event.getCursor());
			}
			
		}
	}
}