-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Features: Custom Inventory Size & others
- Loading branch information
1 parent
6a04145
commit c4f6aae
Showing
7 changed files
with
237 additions
and
12 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/muqsit/customsizedinvmenu/CustomSizedActorInvMenuGraphic.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace muqsit\customsizedinvmenu; | ||
|
||
use muqsit\invmenu\type\graphic\InvMenuGraphic; | ||
use muqsit\invmenu\type\graphic\network\InvMenuGraphicNetworkTranslator; | ||
use pocketmine\inventory\Inventory; | ||
use pocketmine\player\Player; | ||
|
||
final class CustomSizedActorInvMenuGraphic implements InvMenuGraphic | ||
{ | ||
|
||
readonly private string $size_data; | ||
|
||
public function __construct( | ||
readonly private InvMenuGraphic $inner, | ||
readonly private ?string $name, | ||
readonly private int $length, | ||
readonly private bool $scrollbar | ||
) | ||
{ | ||
$scroll = (int)$this->scrollbar; | ||
$this->size_data = "§{$this->length}§{$scroll}§r§r§r§r§r§r§r§r§r§r"; | ||
} | ||
|
||
public function send(Player $player, ?string $name): void | ||
{ | ||
$this->inner->send($player, $this->size_data . ($name ?? $this->name ?? "Inventory")); | ||
} | ||
|
||
public function sendInventory(Player $player, Inventory $inventory): bool | ||
{ | ||
return $this->inner->sendInventory($player, $inventory); | ||
} | ||
|
||
public function remove(Player $player): void | ||
{ | ||
$this->inner->remove($player); | ||
} | ||
|
||
public function getNetworkTranslator(): ?InvMenuGraphicNetworkTranslator | ||
{ | ||
return $this->inner->getNetworkTranslator(); | ||
} | ||
|
||
public function getAnimationDuration(): int | ||
{ | ||
return $this->inner->getAnimationDuration(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace muqsit\customsizedinvmenu; | ||
|
||
use muqsit\invmenu\InvMenu; | ||
use muqsit\invmenu\InvMenuHandler; | ||
use pocketmine\command\Command; | ||
use pocketmine\command\CommandSender; | ||
use pocketmine\item\VanillaItems; | ||
use pocketmine\nbt\tag\CompoundTag; | ||
use pocketmine\network\mcpe\cache\StaticPacketCache; | ||
use pocketmine\player\Player; | ||
use pocketmine\plugin\PluginBase; | ||
use pocketmine\utils\TextFormat; | ||
use RuntimeException; | ||
use function array_rand; | ||
use function assert; | ||
use function is_numeric; | ||
|
||
final class CustomSizedInvMenu extends PluginBase | ||
{ | ||
|
||
private const RESOURCE_PACK_ID = "21f0427f-572a-416d-a90e-c5d9becb0fa3"; | ||
private const TYPE_DYNAMIC_PREFIX = "muqsit:customsizedinvmenu_"; | ||
|
||
public function onEnable(): void | ||
{ | ||
if ($this->getServer()->getResourcePackManager()->getPackById(self::RESOURCE_PACK_ID) === null) { | ||
$this->getLogger()->warning("Resource pack 'Inventory UI Resource Pack' could not be found."); | ||
$this->getLogger()->warning("This plugin cannot be loaded. Please download the resource pack from: https://github.com/tedo0627/InventoryUIResourcePack"); | ||
throw new RuntimeException("Resource pack 'Inventory UI Resource Pack' has not been loaded"); | ||
} | ||
|
||
if (!InvMenuHandler::isRegistered()) { | ||
InvMenuHandler::register($this); | ||
} | ||
|
||
$packet = StaticPacketCache::getInstance()->getAvailableActorIdentifiers(); | ||
$tag = $packet->identifiers->getRoot(); | ||
assert($tag instanceof CompoundTag); | ||
$id_list = $tag->getListTag("idlist"); | ||
assert($id_list !== null); | ||
$id_list->push(CompoundTag::create() | ||
->setString("bid", "") | ||
->setByte("hasspawnegg", 0) | ||
->setString("id", CustomSizedInvMenuType::ACTOR_NETWORK_ID) | ||
->setByte("summonable", 0) | ||
); | ||
} | ||
|
||
public static function create(int $size): InvMenu | ||
{ | ||
static $ids_by_size = []; | ||
if (!isset($ids_by_size[$size])) { | ||
$id = self::TYPE_DYNAMIC_PREFIX . $size; | ||
InvMenuHandler::getTypeRegistry()->register($id, CustomSizedInvMenuType::ofSize($size)); | ||
$ids_by_size[$size] = $id; | ||
} | ||
return InvMenu::create($ids_by_size[$size]); | ||
} | ||
|
||
public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool | ||
{ | ||
if (!($sender instanceof Player)) { | ||
$sender->sendMessage(TextFormat::RED . "Please use this command in-game."); | ||
return true; | ||
} | ||
|
||
if (!isset($args[0]) || !is_numeric($args[0]) || (int)$args[0] <= 0) { | ||
$sender->sendMessage(TextFormat::RED . "/" . $command . " <numSlots : int> [title : string]"); | ||
return true; | ||
} | ||
|
||
$menu = CustomSizedInvMenu::create((int)$args[0]); | ||
$menu->setName($args[1] ?? null); | ||
|
||
$items = VanillaItems::getAll(); | ||
for ($i = 0, $max = $menu->getInventory()->getSize(); $i < $max; $i++) { | ||
$menu->getInventory()->setItem($i, $items[array_rand($items)]); | ||
} | ||
|
||
$menu->send($sender); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace muqsit\customsizedinvmenu; | ||
|
||
use muqsit\invmenu\inventory\InvMenuInventory; | ||
use muqsit\invmenu\InvMenu; | ||
use muqsit\invmenu\type\graphic\ActorInvMenuGraphic; | ||
use muqsit\invmenu\type\graphic\InvMenuGraphic; | ||
use muqsit\invmenu\type\graphic\network\ActorInvMenuGraphicNetworkTranslator; | ||
use muqsit\invmenu\type\InvMenuType; | ||
use pocketmine\entity\Entity; | ||
use pocketmine\inventory\Inventory; | ||
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection; | ||
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties; | ||
use pocketmine\network\mcpe\protocol\types\inventory\WindowTypes; | ||
use pocketmine\player\Player; | ||
use function intdiv; | ||
use function min; | ||
|
||
final class CustomSizedInvMenuType implements InvMenuType | ||
{ | ||
|
||
public const ACTOR_NETWORK_ID = "inventoryui:inventoryui"; | ||
readonly private ActorInvMenuGraphic $inner_graphic; | ||
|
||
public function __construct( | ||
readonly private int $size, | ||
readonly private int $length, | ||
readonly private bool $scrollbar | ||
) | ||
{ | ||
$actor_runtime_identifier = Entity::nextRuntimeId(); | ||
|
||
$properties = new EntityMetadataCollection(); | ||
$properties->setByte(EntityMetadataProperties::CONTAINER_TYPE, WindowTypes::INVENTORY); | ||
$properties->setInt(EntityMetadataProperties::CONTAINER_BASE_SIZE, $this->size); | ||
|
||
$this->inner_graphic = new ActorInvMenuGraphic( | ||
self::ACTOR_NETWORK_ID, | ||
$actor_runtime_identifier, | ||
$properties->getAll(), | ||
new ActorInvMenuGraphicNetworkTranslator($actor_runtime_identifier) | ||
); | ||
} | ||
|
||
public static function ofSize(int $size): self | ||
{ | ||
$length = intdiv($size, 9) + ($size % 9 === 0 ? 0 : 1); | ||
$length = min($length, 6); | ||
return new self($size, $length, $length * 9 < $size); | ||
} | ||
|
||
public function createGraphic(InvMenu $menu, Player $player): ?InvMenuGraphic | ||
{ | ||
return new CustomSizedActorInvMenuGraphic($this->inner_graphic, $menu->getName(), $this->length, $this->scrollbar); | ||
} | ||
|
||
public function createInventory(): Inventory | ||
{ | ||
return new InvMenuInventory($this->size); | ||
} | ||
} |