Added keep on death methods for items (#5395)

This commit is contained in:
Javier León 2022-12-15 17:10:20 -03:00 committed by GitHub
parent 95d0a3bf41
commit c5d716dc9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 4 deletions

View File

@ -391,7 +391,7 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
$this->inventory !== null ? array_values($this->inventory->getContents()) : [],
$this->armorInventory !== null ? array_values($this->armorInventory->getContents()) : [],
$this->offHandInventory !== null ? array_values($this->offHandInventory->getContents()) : [],
), function(Item $item) : bool{ return !$item->hasEnchantment(VanillaEnchantments::VANISHING()); });
), function(Item $item) : bool{ return !$item->hasEnchantment(VanillaEnchantments::VANISHING()) && !$item->keepOnDeath(); });
}
public function saveNBT() : CompoundTag{

View File

@ -65,6 +65,8 @@ class Item implements \JsonSerializable{
public const TAG_DISPLAY_NAME = "Name";
public const TAG_DISPLAY_LORE = "Lore";
public const TAG_KEEP_ON_DEATH = "minecraft:keep_on_death";
private ItemIdentifier $identifier;
private CompoundTag $nbt;
@ -96,6 +98,8 @@ class Item implements \JsonSerializable{
*/
protected $canDestroy;
protected bool $keepOnDeath = false;
/**
* Constructs a new Item type. This constructor should ONLY be used when constructing a new item TYPE to register
* into the index.
@ -222,6 +226,17 @@ class Item implements \JsonSerializable{
}
}
/**
* Returns whether players will retain this item on death. If a non-player dies it will be excluded from the drops.
*/
public function keepOnDeath() : bool{
return $this->keepOnDeath;
}
public function setKeepOnDeath(bool $keepOnDeath) : void{
$this->keepOnDeath = $keepOnDeath;
}
/**
* Returns whether this Item has a non-empty NBT.
*/
@ -320,6 +335,8 @@ class Item implements \JsonSerializable{
$this->canDestroy[$entry->getValue()] = $entry->getValue();
}
}
$this->keepOnDeath = $tag->getByte(self::TAG_KEEP_ON_DEATH, 0) !== 0;
}
protected function serializeCompoundTag(CompoundTag $tag) : void{
@ -377,6 +394,12 @@ class Item implements \JsonSerializable{
}else{
$tag->removeTag("CanDestroy");
}
if($this->keepOnDeath){
$tag->setByte(self::TAG_KEEP_ON_DEATH, 1);
}else{
$tag->removeTag(self::TAG_KEEP_ON_DEATH);
}
}
public function getCount() : int{

View File

@ -131,6 +131,7 @@ use pocketmine\world\sound\Sound;
use pocketmine\world\World;
use Ramsey\Uuid\UuidInterface;
use function abs;
use function array_filter;
use function array_map;
use function assert;
use function count;
@ -2265,15 +2266,16 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->getWorld()->dropItem($this->location, $item);
}
$clearInventory = fn(Inventory $inventory) => $inventory->setContents(array_filter($inventory->getContents(), fn(Item $item) => $item->keepOnDeath()));
if($this->inventory !== null){
$this->inventory->setHeldItemIndex(0);
$this->inventory->clearAll();
$clearInventory($this->inventory);
}
if($this->armorInventory !== null){
$this->armorInventory->clearAll();
$clearInventory($this->armorInventory);
}
if($this->offHandInventory !== null){
$this->offHandInventory->clearAll();
$clearInventory($this->offHandInventory);
}
}