mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-10 21:45:35 +00:00
Remove usage of Ds\Deque from WritableBookBase
ext-ds is not well maintained, and also not very well written. I'm planning to drop this extension for stability's sake.
This commit is contained in:
parent
e061028b69
commit
b03ca1953a
@ -23,11 +23,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\item;
|
||||
|
||||
use Ds\Deque;
|
||||
use pocketmine\nbt\NBT;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\nbt\tag\ListTag;
|
||||
use pocketmine\nbt\tag\StringTag;
|
||||
use function array_push;
|
||||
use function array_slice;
|
||||
use function array_values;
|
||||
use function count;
|
||||
|
||||
abstract class WritableBookBase extends Item{
|
||||
public const TAG_PAGES = "pages"; //TAG_List<TAG_Compound>
|
||||
@ -35,14 +38,13 @@ abstract class WritableBookBase extends Item{
|
||||
public const TAG_PAGE_PHOTONAME = "photoname"; //TAG_String - TODO
|
||||
|
||||
/**
|
||||
* @var WritableBookPage[]|Deque
|
||||
* @phpstan-var Deque<WritableBookPage>
|
||||
* @var WritableBookPage[]
|
||||
* @phpstan-var list<WritableBookPage>
|
||||
*/
|
||||
private $pages;
|
||||
private $pages = [];
|
||||
|
||||
public function __construct(ItemIdentifier $identifier, string $name){
|
||||
parent::__construct($identifier, $name);
|
||||
$this->pages = new Deque();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -71,7 +73,7 @@ abstract class WritableBookBase extends Item{
|
||||
$this->addPage($pageId);
|
||||
}
|
||||
|
||||
$this->pages->set($pageId, new WritableBookPage($pageText));
|
||||
$this->pages[$pageId] = new WritableBookPage($pageText);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -86,8 +88,8 @@ abstract class WritableBookBase extends Item{
|
||||
throw new \InvalidArgumentException("Page number \"$pageId\" is out of range");
|
||||
}
|
||||
|
||||
for($current = $this->pages->count(); $current <= $pageId; $current++){
|
||||
$this->pages->push(new WritableBookPage(""));
|
||||
for($current = count($this->pages); $current <= $pageId; $current++){
|
||||
$this->pages[] = new WritableBookPage("");
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
@ -98,7 +100,8 @@ abstract class WritableBookBase extends Item{
|
||||
* @return $this
|
||||
*/
|
||||
public function deletePage(int $pageId) : self{
|
||||
$this->pages->remove($pageId);
|
||||
unset($this->pages[$pageId]);
|
||||
$this->pages = array_values($this->pages);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -108,7 +111,13 @@ abstract class WritableBookBase extends Item{
|
||||
* @return $this
|
||||
*/
|
||||
public function insertPage(int $pageId, string $pageText = "") : self{
|
||||
$this->pages->insert($pageId, new WritableBookPage($pageText));
|
||||
if($pageId < 0 || $pageId > count($this->pages)){
|
||||
throw new \InvalidArgumentException("Page ID must not be negative");
|
||||
}
|
||||
$newPages = array_slice($this->pages, 0, $pageId);
|
||||
$newPages[] = new WritableBookPage($pageText);
|
||||
array_push($newPages, ...array_slice($this->pages, $pageId));
|
||||
$this->pages = $newPages;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -137,7 +146,7 @@ abstract class WritableBookBase extends Item{
|
||||
* @return WritableBookPage[]
|
||||
*/
|
||||
public function getPages() : array{
|
||||
return $this->pages->toArray();
|
||||
return $this->pages;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -146,25 +155,25 @@ abstract class WritableBookBase extends Item{
|
||||
* @return $this
|
||||
*/
|
||||
public function setPages(array $pages) : self{
|
||||
$this->pages = new Deque($pages);
|
||||
$this->pages = array_values($pages);
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function deserializeCompoundTag(CompoundTag $tag) : void{
|
||||
parent::deserializeCompoundTag($tag);
|
||||
$this->pages = new Deque();
|
||||
$this->pages = [];
|
||||
|
||||
$pages = $tag->getListTag(self::TAG_PAGES);
|
||||
if($pages !== null){
|
||||
if($pages->getTagType() === NBT::TAG_Compound){ //PE format
|
||||
/** @var CompoundTag $page */
|
||||
foreach($pages as $page){
|
||||
$this->pages->push(new WritableBookPage($page->getString(self::TAG_PAGE_TEXT), $page->getString(self::TAG_PAGE_PHOTONAME, "")));
|
||||
$this->pages[] = new WritableBookPage($page->getString(self::TAG_PAGE_TEXT), $page->getString(self::TAG_PAGE_PHOTONAME, ""));
|
||||
}
|
||||
}elseif($pages->getTagType() === NBT::TAG_String){ //PC format
|
||||
/** @var StringTag $page */
|
||||
foreach($pages as $page){
|
||||
$this->pages->push(new WritableBookPage($page->getValue()));
|
||||
$this->pages[] = new WritableBookPage($page->getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -172,7 +181,7 @@ abstract class WritableBookBase extends Item{
|
||||
|
||||
protected function serializeCompoundTag(CompoundTag $tag) : void{
|
||||
parent::serializeCompoundTag($tag);
|
||||
if(!$this->pages->isEmpty()){
|
||||
if(count($this->pages) > 0){
|
||||
$pages = new ListTag();
|
||||
foreach($this->pages as $page){
|
||||
$pages->push(CompoundTag::create()
|
||||
@ -185,10 +194,4 @@ abstract class WritableBookBase extends Item{
|
||||
$tag->removeTag(self::TAG_PAGES);
|
||||
}
|
||||
}
|
||||
|
||||
public function __clone(){
|
||||
parent::__clone();
|
||||
//no need to deep-copy each page, the objects are immutable
|
||||
$this->pages = $this->pages->copy();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user