Merge branch 'minor-next' into major-next

This commit is contained in:
Dylan K. Taylor
2023-04-05 20:14:59 +01:00
9 changed files with 69 additions and 49 deletions

View File

@@ -28,6 +28,7 @@ use pocketmine\crafting\json\ItemStackData;
use pocketmine\data\bedrock\BedrockDataFiles;
use pocketmine\item\Item;
use pocketmine\utils\SingletonTrait;
use pocketmine\utils\Utils;
final class CreativeInventory{
use SingletonTrait;
@@ -62,11 +63,11 @@ final class CreativeInventory{
* @return Item[]
*/
public function getAll() : array{
return $this->creative;
return Utils::cloneObjectArray($this->creative);
}
public function getItem(int $index) : ?Item{
return $this->creative[$index] ?? null;
return isset($this->creative[$index]) ? clone $this->creative[$index] : null;
}
public function getItemIndex(Item $item) : int{

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe;
use pocketmine\event\server\DataPacketSendEvent;
use pocketmine\network\mcpe\protocol\ClientboundPacket;
use pocketmine\player\Player;
use pocketmine\timings\Timings;
@@ -57,14 +56,6 @@ final class NetworkBroadcastUtils{
return false;
}
$ev = new DataPacketSendEvent($sessions, $packets);
$ev->call();
if($ev->isCancelled()){
return false;
}
$sessions = $ev->getTargets();
$packets = $ev->getPackets();
/** @var PacketBroadcaster[] $uniqueBroadcasters */
$uniqueBroadcasters = [];
/** @var NetworkSession[][] $broadcasterTargets */

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe;
use pocketmine\event\server\DataPacketSendEvent;
use pocketmine\network\mcpe\protocol\serializer\PacketBatch;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializerContext;
@@ -41,6 +42,15 @@ final class StandardPacketBroadcaster implements PacketBroadcaster{
){}
public function broadcastPackets(array $recipients, array $packets) : void{
//TODO: this shouldn't really be called here, since the broadcaster might be replaced by an alternative
//implementation that doesn't fire events
$ev = new DataPacketSendEvent($recipients, $packets);
$ev->call();
if($ev->isCancelled()){
return;
}
$packets = $ev->getPackets();
$compressors = [];
/** @var NetworkSession[][] $targetsByCompressor */

View File

@@ -147,6 +147,11 @@ class ItemStackRequestExecutor{
* @throws ItemStackRequestProcessException
*/
protected function removeItemFromSlot(ItemStackRequestSlotInfo $slotInfo, int $count) : Item{
if($slotInfo->getContainerId() === ContainerUIIds::CREATED_OUTPUT && $slotInfo->getSlotId() === UIInventorySlotOffset::CREATED_ITEM_OUTPUT){
//special case for the "created item" output slot
//TODO: do we need to send a response for this slot info?
return $this->takeCreatedItem($count);
}
$this->requestSlotInfos[] = $slotInfo;
[$inventory, $slot] = $this->getBuilderInventoryAndSlot($slotInfo);
if($count < 1){
@@ -188,6 +193,13 @@ class ItemStackRequestExecutor{
$inventory->setItem($slot, $newItem);
}
protected function dropItem(Item $item, int $count) : void{
if($count < 1){
throw new ItemStackRequestProcessException("Cannot drop less than 1 of an item");
}
$this->builder->addAction(new DropItemAction((clone $item)->setCount($count)));
}
/**
* @throws ItemStackRequestProcessException
*/
@@ -254,7 +266,7 @@ class ItemStackRequestExecutor{
/**
* @throws ItemStackRequestProcessException
*/
protected function takeCreatedItem(ItemStackRequestSlotInfo $destination, int $count) : void{
protected function takeCreatedItem(int $count) : Item{
if($count < 1){
//this should be impossible at the protocol level, but in case of buggy core code this will prevent exploits
throw new ItemStackRequestProcessException("Cannot take less than 1 created item");
@@ -272,10 +284,12 @@ class ItemStackRequestExecutor{
}
$this->createdItemsTakenCount += $count;
$this->addItemToSlot($destination, $createdItem, $count);
$createdItem = clone $createdItem;
$createdItem->setCount($count);
if(!$this->createdItemFromCreativeInventory && $this->createdItemsTakenCount >= $createdItem->getCount()){
$this->setNextCreatedItem(null);
}
return $createdItem;
}
/**
@@ -299,14 +313,7 @@ class ItemStackRequestExecutor{
$action instanceof TakeStackRequestAction ||
$action instanceof PlaceStackRequestAction
){
$source = $action->getSource();
$destination = $action->getDestination();
if($source->getContainerId() === ContainerUIIds::CREATED_OUTPUT && $source->getSlotId() === UIInventorySlotOffset::CREATED_ITEM_OUTPUT){
$this->takeCreatedItem($destination, $action->getCount());
}else{
$this->transferItems($source, $destination, $action->getCount());
}
$this->transferItems($action->getSource(), $action->getDestination(), $action->getCount());
}elseif($action instanceof SwapStackRequestAction){
$this->requestSlotInfos[] = $action->getSlot1();
$this->requestSlotInfos[] = $action->getSlot2();

View File

@@ -63,10 +63,7 @@ use function realpath;
use function shuffle;
use function sprintf;
use function str_contains;
use function str_starts_with;
use function strlen;
use function strtolower;
use function substr;
/**
* Manages all the plugins
@@ -654,11 +651,6 @@ class PluginManager{
throw new PluginException("Plugin attempted to register event handler " . $handlerName . "() to event " . $event . " while not enabled");
}
$prefix = $plugin->getDescription()->getSrcNamespacePrefix();
if(str_starts_with($handlerName, $prefix) && $prefix !== ""){
$handlerName = substr($handlerName, strlen($prefix) + 1);
}
$timings = new TimingsHandler($handlerName . "(" . (new \ReflectionClass($event))->getShortName() . ")", group: $plugin->getDescription()->getFullName());
$registeredListener = new RegisteredListener($handler, $priority, $plugin, $handleCancelled, $timings);

View File

@@ -31,6 +31,7 @@ use pocketmine\network\mcpe\protocol\ServerboundPacket;
use pocketmine\player\Player;
use pocketmine\scheduler\TaskHandler;
use function get_class;
use function str_starts_with;
abstract class Timings{
public const INCLUDED_BY_OTHER_TIMINGS_PREFIX = "** ";
@@ -279,7 +280,12 @@ abstract class Timings{
public static function getEventTimings(Event $event) : TimingsHandler{
$eventClass = get_class($event);
if(!isset(self::$events[$eventClass])){
self::$events[$eventClass] = new TimingsHandler($eventClass, group: "Events");
if(str_starts_with($eventClass, "pocketmine\\event\\")){
$name = (new \ReflectionClass($event))->getShortName();
}else{
$name = $eventClass;
}
self::$events[$eventClass] = new TimingsHandler($name, group: "Events");
}
return self::$events[$eventClass];