PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
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->foliageSnow,
83 $biomeDefinition->depth,
84 $biomeDefinition->scale,
85 new Color(
86 $mapWaterColour->r,
87 $mapWaterColour->g,
88 $mapWaterColour->b,
89 $mapWaterColour->a
90 ),
91 $biomeDefinition->rain,
92 count($biomeDefinition->tags) > 0 ? $biomeDefinition->tags : null,
93 );
94 }catch(\JsonMapper_Exception $e){
95 throw new \RuntimeException($e->getMessage(), 0, $e);
96 }
97 }
98
99 return $entries;
100 }
101
102 private static function make() : self{
103 return new self(
104 BiomeDefinitionListPacket::fromDefinitions(self::loadBiomeDefinitionModel(BedrockDataFiles::BIOME_DEFINITIONS_JSON)),
105 AvailableActorIdentifiersPacket::create(self::loadCompoundFromFile(BedrockDataFiles::ENTITY_IDENTIFIERS_NBT))
106 );
107 }
108
109 public function __construct(
110 private BiomeDefinitionListPacket $biomeDefs,
111 private AvailableActorIdentifiersPacket $availableActorIdentifiers
112 ){}
113
114 public function getBiomeDefs() : BiomeDefinitionListPacket{
115 return $this->biomeDefs;
116 }
117
118 public function getAvailableActorIdentifiers() : AvailableActorIdentifiersPacket{
119 return $this->availableActorIdentifiers;
120 }
121}