• Stars
    star
    174
  • Rank 219,104 (Top 5 %)
  • Language
    PHP
  • License
    GNU General Publi...
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A PocketMine-MP virion to create and manage virtual inventories!

InvMenu

InvMenu is a PocketMine-MP virion that eases creating and managing fake inventories!

Installation

You can get the compiled .phar file on poggit by clicking here.

Usage

InvMenu supports creating a GUI out of any kind of Inventory.

NOTE: You MUST register InvMenuHandler during plugin enable before you can begin creating InvMenu instances.

if(!InvMenuHandler::isRegistered()){
	InvMenuHandler::register($this);
}

Creating an InvMenu instance

InvMenu::create($identifier) creates a new instance of InvMenu. $identifier must be an identifier of a registered InvMenuType object. InvMenu comes with 3 pre-registered InvMenuType identifiers: InvMenu::TYPE_CHEST, InvMenu::TYPE_DOUBLE_CHEST and InvMenu::TYPE_HOPPER.

$menu = InvMenu::create(InvMenu::TYPE_CHEST);

To access this menu's inventory, you can use:

$inventory = $menu->getInventory();

The $inventory implements pocketmine's Inventory interface, so you can access all the fancy pocketmine inventory methods.

$menu->getInventory()->setContents([
	VanillaItems::DIAMOND_SWORD(),
	VanillaItems::DIAMOND_PICKAXE()
]);
$menu->getInventory()->addItem(VanillaItems::DIAMOND_AXE());
$menu->getInventory()->setItem(3, VanillaItems::GOLD_INGOT());

To send the menu to a player, use:

/** @var Player $player */
$menu->send($player);

Yup, that's it. It's that simple.

Specifying a custom name to the menu

To set a custom name to a menu, use

$menu->setName("Custom Name");

You can also specify a different menu name for each player separately during InvMenu::send().

/** @var Player $player */
$menu->send($player, "Greetings, " . $player->getName());

Verifying whether the menu was sent to the player

Not a common occurrence but it's possible for plugins to disallow players from opening inventories. This can also occur as an attempt to drop garbage InvMenu::send() requests (if you send two menus simultaneously without any delay in betweeen, the first menu request may be regarded as garbage).

/** @var string|null $name */
$menu->send($player, $name, function(bool $sent) : void{
	if($sent){
		// do something
	}
});

Handling menu item transactions

To handle item transactions happening to and from the menu's inventory, you may specify a Closure handler that gets triggered by InvMenu every time a transaction occurs. You may allow, cancel and do other things within this handler. To register a transaction handler to a menu, use:

/** @var Closure $listener */
$menu->setListener($listener);

What's $listener?

/**
 * @param InvMenuTransaction $transaction
 *
 * Must return an InvMenuTransactionResult instance.
 * Return $transaction->continue() to continue the transaction.
 * Return $transaction->discard() to cancel the transaction.
 * @return InvMenuTransactionResult
 */
Closure(InvMenuTransaction $transaction) : InvMenuTransactionResult;

InvMenuTransaction holds all the item transction data.
InvMenuTransaction::getPlayer() returns the Player that triggered the transaction.
InvMenuTransaction::getItemClicked() returns the Item the player clicked in the menu.
InvMenuTransaction::getItemClickedWith() returns the Item the player had in their hand when clicking an item.
InvMenuTransaction::getAction() returns a SlotChangeAction instance, to get the slot index of the item clicked from the menu's inventory.
InvMenuTransaction::getTransaction() returns the complete InventoryTransaction instance.

$menu->setListener(function(InvMenuTransaction $transaction) : InvMenuTransactionResult{
	$player = $transaction->getPlayer();
	$itemClicked = $transaction->getItemClicked();
	$itemClickedWith = $transaction->getItemClickedWith();
	$action = $transaction->getAction();
	$invTransaction = $transaction->getTransaction();
	return $transaction->continue();
});

A handler that doesn't allow players to take out apples from the menu's inventory:

$menu->setListener(function(InvMenuTransaction $transaction) : InvMenuTransactionResult{
	if($transaction->getItemClicked()->getId() === ItemIds::APPLE){
		$player->sendMessage("You cannot take apples out of that inventory.");
		return $transaction->discard();
	}
	return $transaction->continue();
});

Preventing inventory from being changed by players

There are two ways you can go with to prevent players from modifying the inventory contents of a menu.

Method #1: Calling InvMenuTransaction::discard()

$menu->setListener(function(InvMenuTransaction $transaction) : InvMenuTransactionResult{
	// do something
	return $transaction->discard();
});

Method #2: Using InvMenu::readonly()

$menu->setListener(InvMenu::readonly());
$menu->setListener(InvMenu::readonly(function(DeterministicInvMenuTransaction $transaction) : void{
	// do something
}));

Based on your use-case, you may find one better than the other. While Method #1 gives you full control over a transaction (you can conditionally cancel a transaction, f.e based on whether player has permission, or player is in a specific area etc), Method #2 reduces boilerplate InvMenuTransactionResult imports and calls to InvMenutransaction::discard().

Executing a task post-transaction

A few actions are impossible to be done at the time a player is viewing an inventory, such as sending a form β€” a player won't be able to view a form while viewing an inventory. To do this, you will need to close the menu inventory and make sure they've closed it by waiting for a response from their side. You can do this by supplying a callback to InvMenuTransactionResult::then().

$menu->setListener(function(InvMenuTransaction $transaction) : InvMenuTransactionResult{
	$transaction->getPlayer()->removeCurrentWindow();
	return $transaction->discard()->then(function(Player $player) : void{ // $player === $transaction->getPlayer()
		// assert($player->isOnline());
		$player->sendForm(new Form());
	});
});
$menu->setListener(InvMenu::readonly(function(DeterministicInvMenuTransaction $transaction) : void{
	$transaction->getPlayer()->removeCurrentWindow();
	$transaction->then(function(Player $player) : void{
		$player->sendForm(new Form());
	});
}));

Listening players closing or no longer viewing the inventory

To listen inventory close triggers, specify the inventory close Closure using:

/** @var Closure $listener */
$menu->setInventoryCloseListener($listener);

What's $listener?

/**
 * @param Player $player the player who closed the inventory.
 *
 * @param Inventory $inventory the inventory instance closed by the player.
 */
Closure(Player $player, Inventory $inventory) : void;

To forcefully close or remove the menu from a player:

/** @var Player $player */
$player->removeCurrentWindow();

Registering a custom InvMenu type

So let's say you'd like to send players a dispenser inventory. While InvMenu doesn't ship with a InvMenu::TYPE_DISPENSER, you can still create a dispenser InvMenu by registering an InvMenuType object with the information about what a dispenser inventory looks like.

public const TYPE_DISPENSER = "myplugin:dispenser";

protected function onEnable() : void{
	InvMenuHandler::getTypeRegistry()->register(self::TYPE_DISPENSER, InvMenuTypeBuilders::BLOCK_ACTOR_FIXED()
		->setBlock(BlockFactory::getInstance()->get(BlockLegacyIds::DISPENSER, 0))
		->setBlockActorId("Dispenser")
		->setSize(9)
		->setNetworkWindowType(WindowTypes::DISPENSER)
	->build());
}

Sweet! Now you can create a dispenser menu using

$menu = InvMenu::create(self::TYPE_DISPENSER);

InvMenu Wiki

Applications, examples, tutorials and featured projects using InvMenu can be found on the InvMenu Wiki.

More Repositories

1

VanillaGenerator

GlowstoneMC's generator ported to PocketMine-MP
PHP
86
star
2

ChestShop

ChestShop for PocketMine-MP (pmmp)
PHP
64
star
3

FormImagesFix

PocketMine-MP plugin that fixes form images taking too long to load
PHP
52
star
4

FakePlayer

Specter but targeting PocketMine-MP API 4.0.0
PHP
47
star
5

PlayerVaults

Per-player GUI-based vaults plugin for PocketMine-MP
PHP
31
star
6

SimplePacketHandler

Handle specific data packets (virion for PMMP API 4.0.0)
PHP
31
star
7

WorldStyler

Yet another World Editor plugin for PocketMine... except it's actually fast!
PHP
31
star
8

DimensionPortals

Adds nether and end portals (without nether and end generator) to PocketMine-MP
PHP
29
star
9

Tebex

Tebex plugin for PocketMine-MP servers
PHP
26
star
10

InvCrashFix

Attempt to fix inventory crashes caused by ContainerClosePacket on MCPE v1.16
PHP
26
star
11

PreProcessor

A PHP library to optimize code for production
PHP
23
star
12

FaceLogin

Show a picture of your player's head in chat when they join!
PHP
23
star
13

mcMMO

Port of mcMMO-Dev's mcMMO for PocketMine servers
PHP
21
star
14

PMHopper

Hopper behaviour implementation for PocketMine-MP API 4.0.0
PHP
21
star
15

Sell

A QuickSell plugin for PocketMine-MP
PHP
18
star
16

arithmexp

A powerful mathematical expression parser and evaluator for PHP featuring variable substitution, user-defined constants, functions, deterministic functions, macros, operators, and compiler optimizations.
PHP
18
star
17

ProxyBlocker

Block players using proxies.
PHP
16
star
18

SkyWars

Yet another SkyWars plugin for PocketMine
PHP
15
star
19

AsyncIterator

A virion that simplifies writing tasks that traverse iterators
PHP
14
star
20

XenForoPM

Connect your server with your XenForo hosted forum.
PHP
13
star
21

AggressiveOptz

Tradeoffs to reduce CPU load of your PocketMine-MP server
PHP
13
star
22

Help

A PocketMine plugin that modifies /help
PHP
12
star
23

PortableCrafting

/workbench plugin for PocketMine-MP API 4
PHP
11
star
24

InvMenuUtils

A utility virion for InvMenu implementing some commonly used procedures
PHP
11
star
25

WorldHeightHack

Infinite world height in PocketMine!
PHP
10
star
26

Hats

PocketMine-MP plugin that allows wearing items using /hat
PHP
10
star
27

BlockTypeCorrector

PC to PE maps has a lot of mis-matched blocks
PHP
9
star
28

MiningMoney

PocketMine: MiningMoney allows you to get paid for mining defined blocks.
PHP
9
star
29

php-thinkit

Think-Kit is a library that offers a generic machine learning implementation.
PHP
9
star
30

Wilderness

A PocketMine-MP plugin that teleports players into the wilderness!
PHP
8
star
31

DimensionFix

Hacks to fix dimension crashes since some recent mcpe version
PHP
8
star
32

OfflinePlayerTP

A PocketMine-MP plugin to teleport among offline players
PHP
8
star
33

XPBottle

For PocketMine-MP (Genisys). Title says it all.
PHP
8
star
34

AssaultXP

PocketMine: Reward players with a random amount of XP while they're PvPing.
PHP
7
star
35

ChunkGenerator

PocketMine-MP plugin that autogenerates world terrain
PHP
7
star
36

CurlWithSSL

An example PocketMine-MP plugin demonstrating execution of cURL with CURLOPT_SSL_VERIFYPEER=true
PHP
7
star
37

FakeBlood

PocketMine-MP: A plugin that adds fake-realistic blood effects.
PHP
6
star
38

MiningPlus

PocketMine-MP plugin (Genisys)
PHP
6
star
39

AntiHack

Must-Have AntiHack plugin for Genisys (PocketMine-MP)
PHP
6
star
40

2FA

[In-dev] Two-factor authentication for your PocketMine-MP (PMMP) server. Currently there are no documentations or doc comments.
PHP
6
star
41

MonsterSpawnerGenerator

An example PocketMine-MP plugin that generates monster spawner rooms
PHP
6
star
42

DeathInventoryLog

A PocketMine-MP plugin that logs inventory contents of players right before their death (PM4)
PHP
6
star
43

PerWorldViewDistance

Configure view-distance cap per-world
PHP
6
star
44

Holograms

Create, delete and modify floating texts on your PocketMine server
PHP
5
star
45

AutoLapis

PocketMine-MP Plugin: Inserts Lapis Lazuli into enchantment tables when they are opened.
PHP
5
star
46

ObsidianBreaker

PHP
5
star
47

TeleportationBow

PHP
5
star
48

Spiral

Spiral plugin for PocketMine-MP
PHP
5
star
49

ArenaGenerator

Arena generation library for PocketMine-MP (API 4)
PHP
4
star
50

AntiReach

A @PocketMine plugin
PHP
4
star
51

Beacon

Event-driven beacon behaviour implementation for PocketMine-MP API 4.0.0
PHP
4
star
52

CosmicHUD

A simple and smooth Heads Up Display plugin.
PHP
4
star
53

TebexAPI

Tebex Plugin API as a PHP library
PHP
4
star
54

QueryManipulator

Manipulate server query info
PHP
4
star
55

Statscraft

PocketMine-MP plugin that integrates your server with Statscraft
PHP
4
star
56

noVoid

noVoid plugin!
PHP
3
star
57

SoundDiscovery

Discover new sounds.
PHP
3
star
58

AutoFeed

PocketMine-MP plugin. Amazing shit in the README.
PHP
3
star
59

muqsit.github.io

HTML
3
star
60

Trashbook

Better than pastebin lol
3
star
61

CustomVanillaEnchants

A PocketMine-MP plugin that lets you customize vanilla enchantment values
PHP
3
star
62

NetworkQuery

A plugin that combines query data of PocketMine-MP servers. Currently being used by CosmicPE.
PHP
3
star
63

PMArmorStand

Armor Stands for PocketMine-MP API 4
PHP
3
star
64

Walker

Aliuly's Toybox
PHP
2
star
65

XPBottle-Test

Testing
Java
2
star
66

TestPlugin

A test plugin.
PHP
2
star
67

Balance

Balance PHP Test
PHP
2
star
68

FabulousDeath

*-* Kawaii! Lightning strikes on player's death!
PHP
2
star
69

SlashHelp

A hacky PocketMine-MP plugin that redirects /help to ./help
PHP
2
star
70

SimpleBlindAuth

PocketMine's modified SimpleAuth plugin that gives players blindness effect when on register or login screen! NOTE: THIS DOES NOT SUPPORT authenticatebylastuniqueid.
PHP
2
star
71

RPEncrypter

Encrypts loaded Minecraft: Bedrock Edition resource packs (PocketMine-MP plugin)
PHP
2
star
72

AntiFly

PHP
1
star
73

BasicHUDwithMoney

PHP
1
star
74

Console

A console in C with commands (omg?)
C
1
star
75

EntityInvMenuTest

Testing InvMenus to work with inventories backed by entity rather than a block
PHP
1
star
76

Daily

A PocketMine plugin (in Development)
PHP
1
star
77

test

PHP
1
star
78

ChestPlate

Allow player to ChestCheat
PHP
1
star
79

arithmexp-demo-site

Demo site for Muqsit/arithmexp as a Next.js site deployed on Cloudflare Pages
TypeScript
1
star
80

SecurityPE

A more secure version of PlanB
PHP
1
star
81

PlayerVaults-Converter

A plugin that converts old PlayerVaults storage format to the new one
PHP
1
star