Bedrock 1.21.60 (#6627)

Co-authored-by: Dylan K. Taylor <dktapps@pmmp.io>
This commit is contained in:
Dries C
2025-02-16 21:51:53 +01:00
committed by GitHub
parent 9402a20ee3
commit 91ac64783f
24 changed files with 902 additions and 96 deletions

View File

@@ -23,23 +23,30 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\cache;
use pocketmine\inventory\CreativeCategory;
use pocketmine\inventory\CreativeInventory;
use pocketmine\lang\Translatable;
use pocketmine\network\mcpe\convert\TypeConverter;
use pocketmine\network\mcpe\NetworkSession;
use pocketmine\network\mcpe\protocol\CreativeContentPacket;
use pocketmine\network\mcpe\protocol\types\inventory\CreativeContentEntry;
use pocketmine\network\mcpe\protocol\types\inventory\CreativeGroupEntry;
use pocketmine\network\mcpe\protocol\types\inventory\CreativeItemEntry;
use pocketmine\network\mcpe\protocol\types\inventory\ItemStack;
use pocketmine\utils\SingletonTrait;
use function is_string;
use function spl_object_id;
use const PHP_INT_MIN;
final class CreativeInventoryCache{
use SingletonTrait;
/**
* @var CreativeContentPacket[]
* @phpstan-var array<int, CreativeContentPacket>
* @var CreativeInventoryCacheEntry[]
* @phpstan-var array<int, CreativeInventoryCacheEntry>
*/
private array $caches = [];
public function getCache(CreativeInventory $inventory) : CreativeContentPacket{
private function getCacheEntry(CreativeInventory $inventory) : CreativeInventoryCacheEntry{
$id = spl_object_id($inventory);
if(!isset($this->caches[$id])){
$inventory->getDestructorCallbacks()->add(function() use ($id) : void{
@@ -48,7 +55,7 @@ final class CreativeInventoryCache{
$inventory->getContentChangedCallbacks()->add(function() use ($id) : void{
unset($this->caches[$id]);
});
$this->caches[$id] = $this->buildCreativeInventoryCache($inventory);
$this->caches[$id] = $this->buildCacheEntry($inventory);
}
return $this->caches[$id];
}
@@ -56,14 +63,91 @@ final class CreativeInventoryCache{
/**
* Rebuild the cache for the given inventory.
*/
private function buildCreativeInventoryCache(CreativeInventory $inventory) : CreativeContentPacket{
$entries = [];
private function buildCacheEntry(CreativeInventory $inventory) : CreativeInventoryCacheEntry{
$categories = [];
$groups = [];
$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));
$nextIndex = 0;
$groupIndexes = [];
$itemGroupIndexes = [];
foreach($inventory->getAllEntries() as $k => $entry){
$group = $entry->getGroup();
$category = $entry->getCategory();
if($group === null){
$groupId = PHP_INT_MIN;
}else{
$groupId = spl_object_id($group);
unset($groupIndexes[$category->name][PHP_INT_MIN]); //start a new anonymous group for this category
}
//group object may be reused by multiple categories
if(!isset($groupIndexes[$category->name][$groupId])){
$groupIndexes[$category->name][$groupId] = $nextIndex++;
$categories[] = $category;
$groups[] = $group;
}
$itemGroupIndexes[$k] = $groupIndexes[$category->name][$groupId];
}
return CreativeContentPacket::create($entries);
//creative inventory may have holes if items were unregistered - ensure network IDs used are always consistent
$items = [];
foreach($inventory->getAllEntries() as $k => $entry){
$items[] = new CreativeItemEntry(
$k,
$typeConverter->coreItemStackToNet($entry->getItem()),
$itemGroupIndexes[$k]
);
}
return new CreativeInventoryCacheEntry($categories, $groups, $items);
}
public function buildPacket(CreativeInventory $inventory, NetworkSession $session) : CreativeContentPacket{
$player = $session->getPlayer() ?? throw new \LogicException("Cannot prepare creative data for a session without a player");
$language = $player->getLanguage();
$forceLanguage = $player->getServer()->isLanguageForced();
$typeConverter = $session->getTypeConverter();
$cachedEntry = $this->getCacheEntry($inventory);
$translate = function(Translatable|string $translatable) use ($session, $language, $forceLanguage) : string{
if(is_string($translatable)){
$message = $translatable;
}elseif(!$forceLanguage){
[$message,] = $session->prepareClientTranslatableMessage($translatable);
}else{
$message = $language->translate($translatable);
}
return $message;
};
$groupEntries = [];
foreach($cachedEntry->categories as $index => $category){
$group = $cachedEntry->groups[$index];
$categoryId = match ($category) {
CreativeCategory::CONSTRUCTION => CreativeContentPacket::CATEGORY_CONSTRUCTION,
CreativeCategory::NATURE => CreativeContentPacket::CATEGORY_NATURE,
CreativeCategory::EQUIPMENT => CreativeContentPacket::CATEGORY_EQUIPMENT,
CreativeCategory::ITEMS => CreativeContentPacket::CATEGORY_ITEMS
};
if($group === null){
$groupEntries[] = new CreativeGroupEntry($categoryId, "", ItemStack::null());
}else{
$groupIcon = $group->getIcon();
//TODO: HACK! In 1.21.60, Workaround glitchy behaviour when an item is used as an icon for a group it
//doesn't belong to. Without this hack, both instances of the item will show a +, but neither of them
//will actually expand the group work correctly.
$groupIcon->getNamedTag()->setInt("___GroupBugWorkaround___", $index);
$groupName = $group->getName();
$groupEntries[] = new CreativeGroupEntry(
$categoryId,
$translate($groupName),
$typeConverter->coreItemStackToNet($groupIcon)
);
}
}
return CreativeContentPacket::create($groupEntries, $cachedEntry->items);
}
}