mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-10-19 23:37:45 +00:00
Merge branch 'minor-next' into major-next
This commit is contained in:
@@ -39,6 +39,7 @@ class PlayerDeathEvent extends EntityDeathEvent{
|
||||
protected Player $player;
|
||||
|
||||
private Translatable|string $deathMessage;
|
||||
private Translatable|string $deathScreenMessage;
|
||||
private bool $keepInventory = false;
|
||||
private bool $keepXp = false;
|
||||
|
||||
@@ -50,6 +51,7 @@ class PlayerDeathEvent extends EntityDeathEvent{
|
||||
parent::__construct($entity, $drops, $xp);
|
||||
$this->player = $entity;
|
||||
$this->deathMessage = $deathMessage ?? self::deriveMessage($entity->getDisplayName(), $entity->getLastDamageCause());
|
||||
$this->deathScreenMessage = $this->deathMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,6 +73,14 @@ class PlayerDeathEvent extends EntityDeathEvent{
|
||||
$this->deathMessage = $deathMessage;
|
||||
}
|
||||
|
||||
public function getDeathScreenMessage() : Translatable|string{
|
||||
return $this->deathScreenMessage;
|
||||
}
|
||||
|
||||
public function setDeathScreenMessage(Translatable|string $deathScreenMessage) : void{
|
||||
$this->deathScreenMessage = $deathScreenMessage;
|
||||
}
|
||||
|
||||
public function getKeepInventory() : bool{
|
||||
return $this->keepInventory;
|
||||
}
|
||||
|
@@ -27,16 +27,24 @@ use pocketmine\crafting\CraftingManagerFromDataHelper;
|
||||
use pocketmine\crafting\json\ItemStackData;
|
||||
use pocketmine\data\bedrock\BedrockDataFiles;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\utils\DestructorCallbackTrait;
|
||||
use pocketmine\utils\Filesystem;
|
||||
use pocketmine\utils\ObjectSet;
|
||||
use pocketmine\utils\SingletonTrait;
|
||||
use pocketmine\utils\Utils;
|
||||
|
||||
final class CreativeInventory{
|
||||
use SingletonTrait;
|
||||
use DestructorCallbackTrait;
|
||||
|
||||
/** @var Item[] */
|
||||
private array $creative = [];
|
||||
|
||||
/** @phpstan-var ObjectSet<\Closure() : void> */
|
||||
private ObjectSet $contentChangedCallbacks;
|
||||
|
||||
private function __construct(){
|
||||
$this->contentChangedCallbacks = new ObjectSet();
|
||||
$creativeItems = CraftingManagerFromDataHelper::loadJsonArrayOfObjectsFile(
|
||||
BedrockDataFiles::CREATIVEITEMS_JSON,
|
||||
ItemStackData::class
|
||||
@@ -57,6 +65,7 @@ final class CreativeInventory{
|
||||
*/
|
||||
public function clear() : void{
|
||||
$this->creative = [];
|
||||
$this->onContentChange();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,6 +95,7 @@ final class CreativeInventory{
|
||||
*/
|
||||
public function add(Item $item) : void{
|
||||
$this->creative[] = clone $item;
|
||||
$this->onContentChange();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,10 +106,22 @@ final class CreativeInventory{
|
||||
$index = $this->getItemIndex($item);
|
||||
if($index !== -1){
|
||||
unset($this->creative[$index]);
|
||||
$this->onContentChange();
|
||||
}
|
||||
}
|
||||
|
||||
public function contains(Item $item) : bool{
|
||||
return $this->getItemIndex($item) !== -1;
|
||||
}
|
||||
|
||||
/** @phpstan-return ObjectSet<\Closure() : void> */
|
||||
public function getContentChangedCallbacks() : ObjectSet{
|
||||
return $this->contentChangedCallbacks;
|
||||
}
|
||||
|
||||
private function onContentChange() : void{
|
||||
foreach($this->contentChangedCallbacks as $callback){
|
||||
$callback();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -39,18 +39,16 @@ use pocketmine\inventory\CreativeInventory;
|
||||
use pocketmine\inventory\Inventory;
|
||||
use pocketmine\inventory\transaction\action\SlotChangeAction;
|
||||
use pocketmine\inventory\transaction\InventoryTransaction;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\network\mcpe\cache\CreativeInventoryCache;
|
||||
use pocketmine\network\mcpe\protocol\ClientboundPacket;
|
||||
use pocketmine\network\mcpe\protocol\ContainerClosePacket;
|
||||
use pocketmine\network\mcpe\protocol\ContainerOpenPacket;
|
||||
use pocketmine\network\mcpe\protocol\ContainerSetDataPacket;
|
||||
use pocketmine\network\mcpe\protocol\CreativeContentPacket;
|
||||
use pocketmine\network\mcpe\protocol\InventoryContentPacket;
|
||||
use pocketmine\network\mcpe\protocol\InventorySlotPacket;
|
||||
use pocketmine\network\mcpe\protocol\MobEquipmentPacket;
|
||||
use pocketmine\network\mcpe\protocol\types\BlockPosition;
|
||||
use pocketmine\network\mcpe\protocol\types\inventory\ContainerIds;
|
||||
use pocketmine\network\mcpe\protocol\types\inventory\CreativeContentEntry;
|
||||
use pocketmine\network\mcpe\protocol\types\inventory\ItemStack;
|
||||
use pocketmine\network\mcpe\protocol\types\inventory\ItemStackWrapper;
|
||||
use pocketmine\network\mcpe\protocol\types\inventory\NetworkInventoryAction;
|
||||
@@ -605,16 +603,7 @@ class InventoryManager{
|
||||
}
|
||||
|
||||
public function syncCreative() : void{
|
||||
$typeConverter = $this->session->getTypeConverter();
|
||||
|
||||
$entries = [];
|
||||
if(!$this->player->isSpectator()){
|
||||
//creative inventory may have holes if items were unregistered - ensure network IDs used are always consistent
|
||||
foreach(CreativeInventory::getInstance()->getAll() as $k => $item){
|
||||
$entries[] = new CreativeContentEntry($k, $typeConverter->coreItemStackToNet($item));
|
||||
}
|
||||
}
|
||||
$this->session->sendDataPacket(CreativeContentPacket::create($entries));
|
||||
$this->session->sendDataPacket(CreativeInventoryCache::getInstance()->getCache(CreativeInventory::getInstance()));
|
||||
}
|
||||
|
||||
private function newItemStackId() : int{
|
||||
|
69
src/network/mcpe/cache/CreativeInventoryCache.php
vendored
Normal file
69
src/network/mcpe/cache/CreativeInventoryCache.php
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
<?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\network\mcpe\cache;
|
||||
|
||||
use pocketmine\inventory\CreativeInventory;
|
||||
use pocketmine\network\mcpe\convert\TypeConverter;
|
||||
use pocketmine\network\mcpe\protocol\CreativeContentPacket;
|
||||
use pocketmine\network\mcpe\protocol\types\inventory\CreativeContentEntry;
|
||||
use pocketmine\utils\SingletonTrait;
|
||||
use function spl_object_id;
|
||||
|
||||
final class CreativeInventoryCache{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var CreativeContentPacket[]
|
||||
* @phpstan-var array<int, CreativeContentPacket>
|
||||
*/
|
||||
private array $caches = [];
|
||||
|
||||
public function getCache(CreativeInventory $inventory) : CreativeContentPacket{
|
||||
$id = spl_object_id($inventory);
|
||||
if(!isset($this->caches[$id])){
|
||||
$inventory->getDestructorCallbacks()->add(function() use ($id) : void{
|
||||
unset($this->caches[$id]);
|
||||
});
|
||||
$inventory->getContentChangedCallbacks()->add(function() use ($id) : void{
|
||||
unset($this->caches[$id]);
|
||||
});
|
||||
$this->caches[$id] = $this->buildCreativeInventoryCache($inventory);
|
||||
}
|
||||
return $this->caches[$id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuild the cache for the given inventory.
|
||||
*/
|
||||
private function buildCreativeInventoryCache(CreativeInventory $inventory) : CreativeContentPacket{
|
||||
$entries = [];
|
||||
$typeConverter = TypeConverter::getInstance();
|
||||
//creative inventory may have holes if items were unregistered - ensure network IDs used are always consistent
|
||||
foreach($inventory->getAll() as $k => $item){
|
||||
$entries[] = new CreativeContentEntry($k, $typeConverter->coreItemStackToNet($item));
|
||||
}
|
||||
|
||||
return CreativeContentPacket::create($entries);
|
||||
}
|
||||
}
|
@@ -2350,7 +2350,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
|
||||
$this->startDeathAnimation();
|
||||
|
||||
$this->getNetworkSession()->onServerDeath($ev->getDeathMessage());
|
||||
$this->getNetworkSession()->onServerDeath($ev->getDeathScreenMessage());
|
||||
}
|
||||
|
||||
protected function onDeathUpdate(int $tickDiff) : bool{
|
||||
|
Reference in New Issue
Block a user