PocketMine-MP 5.28.1 git-88cdc2eb67c40075559c3ef51418b418cd5488e9
Loading...
Searching...
No Matches
StaticPacketCache.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe\cache;
25
35use pocketmine\utils\SingletonTrait;
38use function count;
39use function get_debug_type;
40use function is_array;
41use function json_decode;
42
44 use SingletonTrait;
45
49 private static function loadCompoundFromFile(string $filePath) : CacheableNbt{
50 return new CacheableNbt((new NetworkNbtSerializer())->read(Filesystem::fileGetContents($filePath))->mustGetCompoundTag());
51 }
52
56 private static function loadBiomeDefinitionModel(string $filePath) : array{
57 $biomeEntries = json_decode(Filesystem::fileGetContents($filePath), associative: true);
58 if(!is_array($biomeEntries)){
59 throw new SavedDataLoadingException("$filePath root should be an array, got " . get_debug_type($biomeEntries));
60 }
61
62 $jsonMapper = new \JsonMapper();
63 $jsonMapper->bExceptionOnMissingData = true;
64 $jsonMapper->bStrictObjectTypeChecking = true;
65 $jsonMapper->bEnforceMapType = false;
66
67 $entries = [];
68 foreach(Utils::promoteKeys($biomeEntries) as $biomeName => $entry){
69 if(!is_array($entry)){
70 throw new SavedDataLoadingException("$filePath should be an array of objects, got " . get_debug_type($entry));
71 }
72
73 try{
74 $biomeDefinition = $jsonMapper->map($entry, new BiomeDefinitionEntryData());
75
76 $mapWaterColour = $biomeDefinition->mapWaterColour;
77 $entries[] = new BiomeDefinitionEntry(
78 (string) $biomeName,
79 $biomeDefinition->id,
80 $biomeDefinition->temperature,
81 $biomeDefinition->downfall,
82 $biomeDefinition->redSporeDensity,
83 $biomeDefinition->blueSporeDensity,
84 $biomeDefinition->ashDensity,
85 $biomeDefinition->whiteAshDensity,
86 $biomeDefinition->depth,
87 $biomeDefinition->scale,
88 new Color(
89 $mapWaterColour->r,
90 $mapWaterColour->g,
91 $mapWaterColour->b,
92 $mapWaterColour->a
93 ),
94 $biomeDefinition->rain,
95 count($biomeDefinition->tags) > 0 ? $biomeDefinition->tags : null,
96 );
97 }catch(\JsonMapper_Exception $e){
98 throw new \RuntimeException($e->getMessage(), 0, $e);
99 }
100 }
101
102 return $entries;
103 }
104
105 private static function make() : self{
106 return new self(
107 BiomeDefinitionListPacket::fromDefinitions(self::loadBiomeDefinitionModel(BedrockDataFiles::BIOME_DEFINITIONS_JSON)),
108 AvailableActorIdentifiersPacket::create(self::loadCompoundFromFile(BedrockDataFiles::ENTITY_IDENTIFIERS_NBT))
109 );
110 }
111
112 public function __construct(
113 private BiomeDefinitionListPacket $biomeDefs,
114 private AvailableActorIdentifiersPacket $availableActorIdentifiers
115 ){}
116
117 public function getBiomeDefs() : BiomeDefinitionListPacket{
118 return $this->biomeDefs;
119 }
120
121 public function getAvailableActorIdentifiers() : AvailableActorIdentifiersPacket{
122 return $this->availableActorIdentifiers;
123 }
124}