Java Code Examples for org.bukkit.inventory.InventoryView#convertSlot()

The following examples show how to use org.bukkit.inventory.InventoryView#convertSlot() . 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: InventoryClickEvent.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public InventoryClickEvent(InventoryView view, SlotType type, int slot, ClickType click, InventoryAction action) {
    super(view);
    this.slot_type = type;
    this.rawSlot = slot;
    if (slot < 0) {
        this.clickedInventory = null;
    } else if (view.getTopInventory() != null && slot < view.getTopInventory().getSize()) {
        this.clickedInventory = view.getTopInventory();
    } else {
        this.clickedInventory = view.getBottomInventory();
    }
    this.whichSlot = view.convertSlot(slot);
    this.click = click;
    this.action = action;
}
 
Example 2
Source File: InventorySlot.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static InventorySlot<?> fromViewIndex(InventoryView view, int rawIndex) {
    final Slot slot = Slot.forViewIndex(view, rawIndex);
    if(slot == null) {
        throw new IllegalArgumentException("Could not determine slot at index " + rawIndex + " in view " + view);
    }
    final Inventory inventory = rawIndex == view.convertSlot(rawIndex) ? view.getTopInventory()
                                                                       : view.getBottomInventory();
    return new InventorySlot<>(inventory, slot);
}
 
Example 3
Source File: Slot.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static @Nullable Slot<?, ?> forViewIndex(InventoryView view, int rawIndex) {
    final int cookedIndex = view.convertSlot(rawIndex);
    return forInventoryIndex((rawIndex == cookedIndex ? view.getTopInventory()
                                                      : view.getBottomInventory()).getClass(),
                             cookedIndex);
}
 
Example 4
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 5
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());
			}
			
		}
	}
}