Remove Inventory->sendSlot() and Inventory->sendContents()

the plan is to remove this from inventory entirely and use listeners for this.
This commit is contained in:
Dylan K. Taylor 2019-06-13 18:14:11 +01:00
parent 7ba1dd3242
commit 8551d1e282
7 changed files with 15 additions and 50 deletions

View File

@ -116,7 +116,9 @@ abstract class BaseInventory implements Inventory{
} }
if($send){ if($send){
$this->sendContents($this->getViewers()); foreach($this->getViewers() as $viewer){
$viewer->getNetworkSession()->syncInventoryContents($this);
}
} }
} }
@ -380,34 +382,9 @@ abstract class BaseInventory implements Inventory{
$listener->onSlotChange($this, $index); $listener->onSlotChange($this, $index);
} }
if($send){ if($send){
$this->sendSlot($index, $this->getViewers()); foreach($this->viewers as $viewer){
} $viewer->getNetworkSession()->syncInventorySlot($this, $index);
} }
/**
* @param Player|Player[] $target
*/
public function sendContents($target) : void{
if($target instanceof Player){
$target = [$target];
}
foreach($target as $player){
$player->getNetworkSession()->syncInventoryContents($this);
}
}
/**
* @param int $index
* @param Player|Player[] $target
*/
public function sendSlot(int $index, $target) : void{
if($target instanceof Player){
$target = [$target];
}
foreach($target as $player){
$player->getNetworkSession()->syncInventorySlot($this, $index);
} }
} }

View File

@ -49,7 +49,7 @@ abstract class ContainerInventory extends BaseInventory{
$who->sendDataPacket(ContainerOpenPacket::blockInv($windowId, $this->getNetworkType(), $holder->getFloorX(), $holder->getFloorY(), $holder->getFloorZ())); $who->sendDataPacket(ContainerOpenPacket::blockInv($windowId, $this->getNetworkType(), $holder->getFloorX(), $holder->getFloorY(), $holder->getFloorZ()));
} }
$this->sendContents($who); $who->getNetworkSession()->syncInventoryContents($this);
} }
protected function onClose(Player $who) : void{ protected function onClose(Player $who) : void{

View File

@ -107,17 +107,6 @@ interface Inventory{
*/ */
public function setContents(array $items, bool $send = true) : void; public function setContents(array $items, bool $send = true) : void;
/**
* @param Player|Player[] $target
*/
public function sendContents($target) : void;
/**
* @param int $index
* @param Player|Player[] $target
*/
public function sendSlot(int $index, $target) : void;
/** /**
* Checks if the inventory contains any Item with the same material data. * Checks if the inventory contains any Item with the same material data.
* It will check id, amount, and metadata (if not null) * It will check id, amount, and metadata (if not null)

View File

@ -137,12 +137,12 @@ class PlayerInventory extends BaseInventory{
if(!is_array($target)){ if(!is_array($target)){
$target->sendDataPacket($pk); $target->sendDataPacket($pk);
if($target === $this->getHolder()){ if($target === $this->getHolder()){
$this->sendSlot($this->getHeldItemIndex(), $target); $target->getNetworkSession()->syncInventorySlot($this, $this->getHeldItemIndex());
} }
}else{ }else{
$this->getHolder()->getWorld()->getServer()->broadcastPacket($target, $pk); $this->getHolder()->getWorld()->getServer()->broadcastPacket($target, $pk);
if(in_array($this->getHolder(), $target, true)){ if(in_array($this->getHolder(), $target, true)){
$this->sendSlot($this->getHeldItemIndex(), $this->getHolder()); $target->getNetworkSession()->syncInventorySlot($this, $this->getHeldItemIndex());
} }
} }
} }

View File

@ -282,7 +282,7 @@ class InventoryTransaction{
protected function sendInventories() : void{ protected function sendInventories() : void{
foreach($this->inventories as $inventory){ foreach($this->inventories as $inventory){
$inventory->sendContents($this->source); $this->source->getNetworkSession()->syncInventoryContents($inventory);
} }
} }

View File

@ -27,7 +27,6 @@ use pocketmine\inventory\Inventory;
use pocketmine\inventory\transaction\InventoryTransaction; use pocketmine\inventory\transaction\InventoryTransaction;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\Player; use pocketmine\Player;
use function spl_object_id;
/** /**
* Represents an action causing a change in an inventory slot. * Represents an action causing a change in an inventory slot.
@ -110,9 +109,9 @@ class SlotChangeAction extends InventoryAction{
* @param Player $source * @param Player $source
*/ */
public function onExecuteSuccess(Player $source) : void{ public function onExecuteSuccess(Player $source) : void{
$viewers = $this->inventory->getViewers(); foreach($this->inventory->getViewers() as $viewer){
unset($viewers[spl_object_id($source)]); $viewer->getNetworkSession()->syncInventorySlot($this->inventory, $this->inventorySlot);
$this->inventory->sendSlot($this->inventorySlot, $viewers); }
} }
/** /**
@ -121,6 +120,6 @@ class SlotChangeAction extends InventoryAction{
* @param Player $source * @param Player $source
*/ */
public function onExecuteFail(Player $source) : void{ public function onExecuteFail(Player $source) : void{
$this->inventory->sendSlot($this->inventorySlot, $source); $source->getNetworkSession()->syncInventorySlot($this->inventory, $this->inventorySlot);
} }
} }

View File

@ -339,7 +339,7 @@ class InGameSessionHandler extends SessionHandler{
switch($data->getActionType()){ switch($data->getActionType()){
case ReleaseItemTransactionData::ACTION_RELEASE: case ReleaseItemTransactionData::ACTION_RELEASE:
if(!$this->player->releaseHeldItem()){ if(!$this->player->releaseHeldItem()){
$this->player->getInventory()->sendContents($this->player); $this->session->syncInventoryContents($this->player->getInventory());
} }
return true; return true;
case ReleaseItemTransactionData::ACTION_CONSUME: case ReleaseItemTransactionData::ACTION_CONSUME: