Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved forever the CustomItem bug #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added plugin-banner-white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 7 additions & 5 deletions src/FoxWorn3365/Shopkeepers/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
use pocketmine\network\mcpe\protocol\types\inventory\ItemStack;
use pocketmine\network\mcpe\convert\TypeConverter;

// Exceptions
use pocketmine\network\mcpe\convert\TypeConversionException;

class Core extends PluginBase implements Listener {
protected object $menu;
protected EntityManager $entities;
Expand Down Expand Up @@ -374,12 +377,11 @@ public function onPacket(DataPacketReceiveEvent $event) : void {
$event->getOrigin()->getPlayer()->sendMessage("§cYour inventory is full!");
return;
} else {
if ($result->getId() === 25266) {
// Is a custom item
$item = NbtManager::decode(Utils::comparator($this->trades->{$event->getOrigin()->getPlayer()->getName()}->item, $result->getCount(), $cm->get()->{$cm->getSingleKey()}->items));
} else {
$translator = new TypeConverter();
$translator = new TypeConverter();
try {
$item = $translator->netItemStackToCore($result);
} catch (TypeConversionException $e) {
$item = NbtManager::decode(Utils::comparator($this->trades->{$event->getOrigin()->getPlayer()->getName()}->item, $result->getCount(), $cm->get()->{$cm->getSingleKey()}->items));
}
/*
if ($this->server < 5) {
Expand Down
182 changes: 182 additions & 0 deletions src/muqsit/invmenu/InvMenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

declare(strict_types=1);

namespace muqsit\invmenu;

use Closure;
use LogicException;
use muqsit\invmenu\inventory\SharedInvMenuSynchronizer;
use muqsit\invmenu\session\InvMenuInfo;
use muqsit\invmenu\session\network\PlayerNetwork;
use muqsit\invmenu\transaction\DeterministicInvMenuTransaction;
use muqsit\invmenu\transaction\InvMenuTransaction;
use muqsit\invmenu\transaction\InvMenuTransactionResult;
use muqsit\invmenu\transaction\SimpleInvMenuTransaction;
use muqsit\invmenu\type\InvMenuType;
use muqsit\invmenu\type\InvMenuTypeIds;
use pocketmine\inventory\Inventory;
use pocketmine\inventory\transaction\action\SlotChangeAction;
use pocketmine\inventory\transaction\InventoryTransaction;
use pocketmine\item\Item;
use pocketmine\player\Player;

class InvMenu implements InvMenuTypeIds{

/**
* @param string $identifier
* @param mixed ...$args
* @return InvMenu
*/
public static function create(string $identifier, ...$args) : InvMenu{
return new InvMenu(InvMenuHandler::getTypeRegistry()->get($identifier), ...$args);
}

/**
* @param (Closure(DeterministicInvMenuTransaction) : void)|null $listener
* @return Closure(InvMenuTransaction) : InvMenuTransactionResult
*/
public static function readonly(?Closure $listener = null) : Closure{
return static function(InvMenuTransaction $transaction) use($listener) : InvMenuTransactionResult{
$result = $transaction->discard();
if($listener !== null){
$listener(new DeterministicInvMenuTransaction($transaction, $result));
}
return $result;
};
}

readonly public InvMenuType $type;
protected ?string $name = null;
protected ?Closure $listener = null;
protected ?Closure $inventory_close_listener = null;
protected Inventory $inventory;
protected ?SharedInvMenuSynchronizer $synchronizer = null;

public function __construct(InvMenuType $type, ?Inventory $custom_inventory = null){
if(!InvMenuHandler::isRegistered()){
throw new LogicException("Tried creating menu before calling " . InvMenuHandler::class . "::register()");
}
$this->type = $type;
$this->inventory = $this->type->createInventory();
$this->setInventory($custom_inventory);
}

/**
* @deprecated Access {@see InvMenu::$type} directly
* @return InvMenuType
*/
public function getType() : InvMenuType{
return $this->type;
}

public function getName() : ?string{
return $this->name;
}

public function setName(?string $name) : self{
$this->name = $name;
return $this;
}

/**
* @param (Closure(InvMenuTransaction) : InvMenuTransactionResult)|null $listener
* @return self
*/
public function setListener(?Closure $listener) : self{
$this->listener = $listener;
return $this;
}

/**
* @param (Closure(Player, Inventory) : void)|null $listener
* @return self
*/
public function setInventoryCloseListener(?Closure $listener) : self{
$this->inventory_close_listener = $listener;
return $this;
}

/**
* @param Player $player
* @param string|null $name
* @param (Closure(bool) : void)|null $callback
*/
final public function send(Player $player, ?string $name = null, ?Closure $callback = null) : void{
$player->removeCurrentWindow();

$session = InvMenuHandler::getPlayerManager()->get($player);
$network = $session->network;

// Avoid players from spamming InvMenu::send() and other similar
// requests and filling up queued tasks in memory.
// It would be better if this check were implemented by plugins,
// however I suppose it is more convenient if done within InvMenu...
if($network->getPending() >= 8){
$network->dropPending();
}else{
$network->dropPendingOfType(PlayerNetwork::DELAY_TYPE_OPERATION);
}

$network->waitUntil(PlayerNetwork::DELAY_TYPE_OPERATION, 0, function(bool $success) use($player, $session, $name, $callback) : bool{
if(!$success){
if($callback !== null){
$callback(false);
}
return false;
}

$graphic = $this->type->createGraphic($this, $player);
if($graphic !== null){
$session->setCurrentMenu(new InvMenuInfo($this, $graphic, $name), static function(bool $success) use($callback) : void{
if($callback !== null){
$callback($success);
}
});
}else{
if($callback !== null){
$callback(false);
}
}
return false;
});
}

public function getInventory() : Inventory{
return $this->inventory;
}

public function setInventory(?Inventory $custom_inventory) : void{
if($this->synchronizer !== null){
$this->synchronizer->destroy();
$this->synchronizer = null;
}

if($custom_inventory !== null){
$this->synchronizer = new SharedInvMenuSynchronizer($this, $custom_inventory);
}
}

/**
* @internal use InvMenu::send() instead.
*
* @param Player $player
* @return bool
*/
public function sendInventory(Player $player) : bool{
return $player->setCurrentWindow($this->getInventory());
}

public function handleInventoryTransaction(Player $player, Item $out, Item $in, SlotChangeAction $action, InventoryTransaction $transaction) : InvMenuTransactionResult{
$inv_menu_txn = new SimpleInvMenuTransaction($player, $out, $in, $action, $transaction);
return $this->listener !== null ? ($this->listener)($inv_menu_txn) : $inv_menu_txn->continue();
}

public function onClose(Player $player) : void{
if($this->inventory_close_listener !== null){
($this->inventory_close_listener)($player, $this->getInventory());
}

InvMenuHandler::getPlayerManager()->get($player)->removeCurrentMenu();
}
}
97 changes: 97 additions & 0 deletions src/muqsit/invmenu/InvMenuEventHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace muqsit\invmenu;

use muqsit\invmenu\session\network\PlayerNetwork;
use muqsit\invmenu\session\PlayerManager;
use pocketmine\event\inventory\InventoryCloseEvent;
use pocketmine\event\inventory\InventoryTransactionEvent;
use pocketmine\event\Listener;
use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\inventory\transaction\action\SlotChangeAction;
use pocketmine\network\mcpe\protocol\NetworkStackLatencyPacket;

final class InvMenuEventHandler implements Listener{

public function __construct(
readonly private PlayerManager $player_manager
){}

/**
* @param DataPacketReceiveEvent $event
* @priority NORMAL
*/
public function onDataPacketReceive(DataPacketReceiveEvent $event) : void{
$packet = $event->getPacket();
if($packet instanceof NetworkStackLatencyPacket){
$player = $event->getOrigin()->getPlayer();
if($player !== null){
$this->player_manager->getNullable($player)?->network->notify($packet->timestamp);
}
}
}

/**
* @param InventoryCloseEvent $event
* @priority MONITOR
*/
public function onInventoryClose(InventoryCloseEvent $event) : void{
$player = $event->getPlayer();
$session = $this->player_manager->getNullable($player);
if($session === null){
return;
}

$current = $session->getCurrent();
if($current !== null && $event->getInventory() === $current->menu->getInventory()){
$current->menu->onClose($player);
}
$session->network->waitUntil(PlayerNetwork::DELAY_TYPE_ANIMATION_WAIT, 325, static fn(bool $success) : bool => false);
}

/**
* @param InventoryTransactionEvent $event
* @priority NORMAL
*/
public function onInventoryTransaction(InventoryTransactionEvent $event) : void{
$transaction = $event->getTransaction();
$player = $transaction->getSource();

$player_instance = $this->player_manager->get($player);
$current = $player_instance->getCurrent();
if($current === null){
return;
}

$inventory = $current->menu->getInventory();
$network_stack_callbacks = [];
foreach($transaction->getActions() as $action){
if(!($action instanceof SlotChangeAction) || $action->getInventory() !== $inventory){
continue;
}

$result = $current->menu->handleInventoryTransaction($player, $action->getSourceItem(), $action->getTargetItem(), $action, $transaction);
$network_stack_callback = $result->post_transaction_callback;
if($network_stack_callback !== null){
$network_stack_callbacks[] = $network_stack_callback;
}
if($result->cancelled){
$event->cancel();
break;
}
}

if(count($network_stack_callbacks) > 0){
$player_instance->network->wait(PlayerNetwork::DELAY_TYPE_ANIMATION_WAIT, static function(bool $success) use($player, $network_stack_callbacks) : bool{
if($success){
foreach($network_stack_callbacks as $callback){
$callback($player);
}
}
return false;
});
}
}
}
46 changes: 46 additions & 0 deletions src/muqsit/invmenu/InvMenuHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace muqsit\invmenu;

use InvalidArgumentException;
use LogicException;
use muqsit\invmenu\session\PlayerManager;
use muqsit\invmenu\type\InvMenuTypeRegistry;
use pocketmine\plugin\Plugin;
use pocketmine\Server;

final class InvMenuHandler{

private static ?Plugin $registrant = null;
private static InvMenuTypeRegistry $type_registry;
private static PlayerManager $player_manager;

public static function register(Plugin $plugin) : void{
if(self::isRegistered()){
throw new InvalidArgumentException("{$plugin->getName()} attempted to register " . self::class . " twice.");
}

self::$registrant = $plugin;
self::$type_registry = new InvMenuTypeRegistry();
self::$player_manager = new PlayerManager(self::getRegistrant());
Server::getInstance()->getPluginManager()->registerEvents(new InvMenuEventHandler(self::getPlayerManager()), $plugin);
}

public static function isRegistered() : bool{
return self::$registrant instanceof Plugin;
}

public static function getRegistrant() : Plugin{
return self::$registrant ?? throw new LogicException("Cannot obtain registrant before registration");
}

public static function getTypeRegistry() : InvMenuTypeRegistry{
return self::$type_registry;
}

public static function getPlayerManager() : PlayerManager{
return self::$player_manager;
}
}
23 changes: 23 additions & 0 deletions src/muqsit/invmenu/inventory/InvMenuInventory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace muqsit\invmenu\inventory;

use pocketmine\block\inventory\BlockInventory;
use pocketmine\inventory\SimpleInventory;
use pocketmine\world\Position;

final class InvMenuInventory extends SimpleInventory implements BlockInventory{

private Position $holder;

public function __construct(int $size){
parent::__construct($size);
$this->holder = new Position(0, 0, 0, null);
}

public function getHolder() : Position{
return $this->holder;
}
}
Loading