Skip to content

Muqsit/InvMenuUtils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

InvMenuUtils

A utility virion for the InvMenu virion implementing some commonly used procedures.

Assigning multiple listeners to one InvMenu

Multiple listeners have different behaviour for readonly and non-readonly InvMenu instances. For non-readonly InvMenu instances, listeners are prioritised in the order they were passed to the InvMenuListenerUtils::multiple() method. A listener will not be executed if the previous listener cancelled the transaction (i.e returned false).

$menu = InvMenu::create(InvMenu::TYPE_CHEST);
$menu->setListener(InvMenuListenerUtils::multiple(
	$menu,
	function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : bool{
		echo "This listener is called first" . PHP_EOL;
		return true;
	},
	function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : bool{
		echo "This listener is called second." . PHP_EOL;
		return false;
	},
	function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : bool{
		// This listener is not called as the previous listener cancelled the transaction
		// by returning false.
		return true;
	}
));

For readonly InvMenu instances, all listeners will be executed as the transaction is anyway forcefully cancelled.

$menu = InvMenu::create(InvMenu::TYPE_CHEST);
$menu->readonly();
$menu->setListener(InvMenuListenerUtils::multiple(
	$menu,
	function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : void{
		echo "This listener is called first" . PHP_EOL;
	},
	function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : void{
		echo "This listener is called second." . PHP_EOL;
	},
	function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : void{
		echo "This listener is called third." . PHP_EOL;
	}
));

Assigning slot-specific listeners to an InvMenu

Listen or handle specific slots, or handle each slot separately.
Index your listeners to the slot you'd like the listener to handle/listen.
TIP: Use index -1 to "catch-all" (fallback).

$menu->setListener(InvMenuListenerUtils::slotSpecific($menu, [
	8 => function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : bool{
		$player->sendMessage("You clicked slot #8");
		return true;
	},
	16 => function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : bool{
		$player->sendMessage("You clicked slot #16");
		return true;
	},
	-1 => function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : bool{
		$player->sendMessage("Fallback: You clicked slot #" . $action->getSlot());
		return true;
	}
]));

Blacklisting specific slots

NOTE: This method is applicable ONLY to non-readonly InvMenu instances.
Blacklisting an array of slots disallows players from modify those slots.

$menu = InvMenu::create(InvMenu::TYPE_CHEST);
$menu->setListener(InvMenuListenerUtils::blacklistSlots([0, 4, 8]));

You can even use this in combination with InvMenuListenerUtils::multiple().

$menu = InvMenu::create(InvMenu::TYPE_CHEST);
$menu->setListener(InvMenuListenerUtils::multiple(
	$menu,
	InvMenuListenerUtils::blacklistSlots([0, 1, 2]),
	function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : bool{
		$player->sendMessage("You didn't click any of these slots: 0, 1, 2");
		return true;
	}
));

Whitelisting specific slots

NOTE: This method is applicable ONLY to non-readonly InvMenu instances.
Whitelisting an array of slots allows players to modify ONLY those slots.

$menu = InvMenu::create(InvMenu::TYPE_CHEST);
$menu->setListener(InvMenuListenerUtils::whitelistSlots([0, 4, 8]));

Similar to InvMenuListenerUtils::blacklistSlots(), you can use this in combination with InvMenuListenerUtils::multiple().

$menu = InvMenu::create(InvMenu::TYPE_CHEST);
$menu->setListener(InvMenuListenerUtils::multiple(
	$menu,
	InvMenuListenerUtils::whitelistSlots([0, 1, 2]),
	function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : bool{
		$player->sendMessage("You click one of these slots: 0, 1, 2");
		return true;
	}
));

Filtering items with specific NBT tags

NOTE: This method is applicable ONLY to non-readonly InvMenu instances.
Filter items only with a specific NBT tag on them to allow players to take those items out of the inventory.

$menu->setListener(InvMenuListenerUtils::onlyItemsWithTag("CustomItem", StringTag::class));

Filter items only without a specific NBT tag on them to allow players to take those items out of the inventory.

$menu->setListener(InvMenuListenerUtils::onlyItemsWithoutTag("Button", ByteTag::class));

Similar to InvMenuListenerUtils::whitelistSlots() and InvMenuListenerUtils::blacklistSlots(), this can be used in combination with InvMenuListenerUtils::multiple().

$menu->setListener(InvMenuListenerUtils::multiple(
	$menu,
	InvMenuListenerUtils::onlyItemsWithTag("Button", ByteTag::class),
	function(Player $player, Item $itemClicked, Item $itemClickedWith, SlotChangeAction $action) : bool{
		$player->sendMessage("You clicked a button!");
		return false;
	}
));

About

A utility virion for InvMenu implementing some commonly used procedures

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages