Merge branch 'next-minor' of github.com:pmmp/PocketMine-MP into next-minor

This commit is contained in:
Dylan K. Taylor 2021-12-07 00:41:17 +00:00
commit 49d0d01f9f
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
5 changed files with 210 additions and 56 deletions

View File

@ -836,7 +836,7 @@ However, if we add `src-namespace-prefix: pmmp\TesterPlugin` to the `plugin.yml`
- `Item::clearCreativeItems()` -> `CreativeInventory::clear()`
- `Item::getCreativeItemIndex()` -> `CreativeInventory::getItemIndex()`
- `Item::getCreativeItems()` -> `CreativeInventory::getAll()`
- `Item::initCreativeItems()` -> `CreativeInventory::init()`
- `Item::initCreativeItems()` -> `CreativeInventory::reset()`
- `Item::isCreativeItem()` -> `CreativeInventory::contains()`
- `Item::removeCreativeItem()` -> `CreativeInventory::remove()`
- The following classes have been added:

View File

@ -26,6 +26,8 @@ namespace pocketmine\command\defaults;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\inventory\Inventory;
use pocketmine\item\Item;
use pocketmine\item\LegacyStringToItemParser;
use pocketmine\item\LegacyStringToItemParserException;
use pocketmine\item\StringToItemParser;
@ -33,9 +35,9 @@ use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\player\Player;
use pocketmine\utils\TextFormat;
use function array_merge;
use function count;
use function implode;
use function min;
class ClearCommand extends VanillaCommand{
@ -57,7 +59,6 @@ class ClearCommand extends VanillaCommand{
throw new InvalidCommandSyntaxException();
}
$target = null;
if(isset($args[0])){
$target = $sender->getServer()->getPlayerByPrefix($args[0]);
if($target === null){
@ -77,14 +78,14 @@ class ClearCommand extends VanillaCommand{
throw new InvalidCommandSyntaxException();
}
$item = null;
$targetItem = null;
$maxCount = -1;
if(isset($args[1])){
try{
$item = StringToItemParser::getInstance()->parse($args[1]) ?? LegacyStringToItemParser::getInstance()->parse($args[1]);
$targetItem = StringToItemParser::getInstance()->parse($args[1]) ?? LegacyStringToItemParser::getInstance()->parse($args[1]);
if(isset($args[2])){
$item->setCount($maxCount = $this->getInteger($sender, $args[2], 0));
$targetItem->setCount($maxCount = $this->getInteger($sender, $args[2], -1));
}
}catch(LegacyStringToItemParserException $e){
//vanilla checks this at argument parsing layer, can't come up with a better alternative
@ -93,14 +94,18 @@ class ClearCommand extends VanillaCommand{
}
}
//checking players inventory for all the items matching the criteria
if($item !== null and $maxCount === 0){
$count = 0;
$contents = array_merge($target->getInventory()->all($item), $target->getArmorInventory()->all($item));
foreach($contents as $content){
$count += $content->getCount();
}
/**
* @var Inventory[] $inventories - This is the order that vanilla would clear items in.
*/
$inventories = [
$target->getInventory(),
$target->getCursorInventory(),
$target->getArmorInventory()
];
// Checking player's inventory for all the items matching the criteria
if($targetItem !== null and $maxCount === 0){
$count = $this->countItems($inventories, $targetItem);
if($count > 0){
$sender->sendMessage(KnownTranslationFactory::commands_clear_testing($target->getName(), (string) $count));
}else{
@ -110,65 +115,59 @@ class ClearCommand extends VanillaCommand{
return true;
}
$cleared = 0;
//clear everything from the targets inventory
if($item === null){
$contents = array_merge($target->getInventory()->getContents(), $target->getArmorInventory()->getContents());
foreach($contents as $content){
$cleared += $content->getCount();
$clearedCount = 0;
if($targetItem === null){
// Clear all items from the inventories
$clearedCount += $this->countItems($inventories, null);
foreach($inventories as $inventory){
$inventory->clearAll();
}
$target->getInventory()->clearAll();
$target->getArmorInventory()->clearAll();
//TODO: should the cursor inv be cleared?
}else{
//clear the item from targets inventory irrelevant of the count
// Clear the item from target's inventory irrelevant of the count
if($maxCount === -1){
if(($slot = $target->getArmorInventory()->first($item)) !== -1){
$cleared++;
$target->getArmorInventory()->clear($slot);
}
foreach($target->getInventory()->all($item) as $index => $i){
$cleared += $i->getCount();
$target->getInventory()->clear($index);
$clearedCount += $this->countItems($inventories, $targetItem);
foreach($inventories as $inventory){
$inventory->remove($targetItem);
}
}else{
//clear only the given amount of that particular item from targets inventory
if(($slot = $target->getArmorInventory()->first($item)) !== -1){
$cleared++;
$maxCount--;
$target->getArmorInventory()->clear($slot);
}
if($maxCount > 0){
foreach($target->getInventory()->all($item) as $index => $i){
if($i->getCount() >= $maxCount){
$i->pop($maxCount);
$cleared += $maxCount;
$target->getInventory()->setItem($index, $i);
break;
}
// Clear the item from target's inventory up to maxCount
foreach($inventories as $inventory){
foreach($inventory->all($targetItem) as $index => $item){
// The count to reduce from the item and max count
$reductionCount = min($item->getCount(), $maxCount);
$item->pop($reductionCount);
$clearedCount += $reductionCount;
$inventory->setItem($index, $item);
$maxCount -= $reductionCount;
if($maxCount <= 0){
break;
break 2;
}
$cleared += $i->getCount();
$maxCount -= $i->getCount();
$target->getInventory()->clear($index);
}
}
}
}
if($cleared > 0){
Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_clear_success($target->getName(), (string) $cleared));
if($clearedCount > 0){
Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_clear_success($target->getName(), (string) $clearedCount));
}else{
$sender->sendMessage(KnownTranslationFactory::commands_clear_failure_no_items($target->getName())->prefix(TextFormat::RED));
}
return true;
}
}
/**
* @param Inventory[] $inventories
*/
protected function countItems(array $inventories, ?Item $target) : int{
$count = 0;
foreach($inventories as $inventory){
$contents = $target !== null ? $inventory->all($target) : $inventory->getContents();
foreach($contents as $item){
$count += $item->getCount();
}
}
return $count;
}
}

View File

@ -0,0 +1,40 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\entity\animation;
use pocketmine\entity\object\ItemEntity;
use pocketmine\network\mcpe\protocol\ActorEventPacket;
use pocketmine\network\mcpe\protocol\types\ActorEvent;
final class ItemEntityStackSizeChangeAnimation implements Animation{
public function __construct(
private ItemEntity $itemEntity,
private int $newStackSize
){}
public function encode() : array{
return [ActorEventPacket::create($this->itemEntity->getId(), ActorEvent::ITEM_ENTITY_MERGE, $this->newStackSize)];
}
}

View File

@ -23,11 +23,13 @@ declare(strict_types=1);
namespace pocketmine\entity\object;
use pocketmine\entity\animation\ItemEntityStackSizeChangeAnimation;
use pocketmine\entity\Entity;
use pocketmine\entity\EntitySizeInfo;
use pocketmine\entity\Location;
use pocketmine\event\entity\EntityItemPickupEvent;
use pocketmine\event\entity\ItemDespawnEvent;
use pocketmine\event\entity\ItemMergeEvent;
use pocketmine\event\entity\ItemSpawnEvent;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
@ -43,6 +45,7 @@ class ItemEntity extends Entity{
public static function getNetworkTypeId() : string{ return EntityIds::ITEM; }
public const MERGE_CHECK_PERIOD = 2; //0.1 seconds
public const DEFAULT_DESPAWN_DELAY = 6000; //5 minutes
public const NEVER_DESPAWN = -1;
public const MAX_DESPAWN_DELAY = 32767 + self::DEFAULT_DESPAWN_DELAY; //max value storable by mojang NBT :(
@ -105,6 +108,27 @@ class ItemEntity extends Entity{
if($this->pickupDelay < 0){
$this->pickupDelay = 0;
}
if($this->hasMovementUpdate() and $this->despawnDelay % self::MERGE_CHECK_PERIOD === 0){
$mergeable = [$this]; //in case the merge target ends up not being this
$mergeTarget = $this;
foreach($this->getWorld()->getNearbyEntities($this->boundingBox->expandedCopy(0.5, 0.5, 0.5), $this) as $entity){
if(!$entity instanceof ItemEntity or $entity->isFlaggedForDespawn()){
continue;
}
if($entity->isMergeable($this)){
$mergeable[] = $entity;
if($entity->item->getCount() > $mergeTarget->item->getCount()){
$mergeTarget = $entity;
}
}
}
foreach($mergeable as $itemEntity){
if($itemEntity !== $mergeTarget){
$itemEntity->tryMergeInto($mergeTarget);
}
}
}
$this->despawnDelay -= $tickDiff;
if($this->despawnDelay <= 0){
@ -122,6 +146,37 @@ class ItemEntity extends Entity{
return $hasUpdate;
}
/**
* Returns whether this item entity can merge with the given one.
*/
public function isMergeable(ItemEntity $entity) : bool{
$item = $entity->item;
return $entity !== $this && $entity->pickupDelay !== self::NEVER_DESPAWN and $item->canStackWith($this->item) and $item->getCount() + $this->item->getCount() <= $item->getMaxStackSize();
}
/**
* Attempts to merge this item entity into the given item entity. Returns true if it was successful.
*/
public function tryMergeInto(ItemEntity $consumer) : bool{
if(!$this->isMergeable($consumer)){
return false;
}
$ev = new ItemMergeEvent($this, $consumer);
$ev->call();
if($ev->isCancelled()){
return false;
}
$consumer->setStackSize($consumer->item->getCount() + $this->item->getCount());
$this->flagForDespawn();
$consumer->pickupDelay = max($consumer->pickupDelay, $this->pickupDelay);
$consumer->despawnDelay = max($consumer->despawnDelay, $this->despawnDelay);
return true;
}
protected function tryChangeMovement() : void{
$this->checkObstruction($this->location->x, $this->location->y, $this->location->z);
parent::tryChangeMovement();
@ -217,6 +272,14 @@ class ItemEntity extends Entity{
));
}
public function setStackSize(int $newCount) : void{
if($newCount <= 0){
throw new \InvalidArgumentException("Stack size must be at least 1");
}
$this->item->setCount($newCount);
$this->broadcastAnimation(new ItemEntityStackSizeChangeAnimation($this, $newCount));
}
public function getOffsetPosition(Vector3 $vector3) : Vector3{
return $vector3->add(0, 0.125, 0);
}

View File

@ -0,0 +1,52 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\event\entity;
use pocketmine\entity\object\ItemEntity;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
/**
* Called when an item entity tries to merge into another item entity.
*
* @phpstan-extends EntityEvent<ItemEntity>
*/
class ItemMergeEvent extends EntityEvent implements Cancellable{
use CancellableTrait;
public function __construct(
ItemEntity $entity,
protected ItemEntity $target
){
$this->entity = $entity;
}
/**
* Returns the merge destination.
*/
public function getTarget() : ItemEntity{
return $this->target;
}
}