-
Notifications
You must be signed in to change notification settings - Fork 4
/
BlockRegion.php
166 lines (151 loc) · 4.96 KB
/
BlockRegion.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace Aternos\Hawk;
use Aternos\Nbt\Tag\CompoundTag;
use Exception;
class BlockRegion extends Region
{
public function __construct(AbstractFile $file)
{
parent::__construct($file);
}
/**
* @param int $location
* @param int $offset
* @param int $compressedDataLength
* @param int $compressionScheme
* @param CompoundTag $tag
* @param McCoordinates2D $coordinates
* @param int $version
* @return Chunk
* @throws Exception
*/
public function addNewChunk(int $location, int $offset, int $compressedDataLength, int $compressionScheme, CompoundTag $tag, McCoordinates2D $coordinates, int $version): Chunk
{
$chunkClass = VersionHelper::getChunkClassFromVersion($this->version);
if(VersionHelper::hasLevelTag($this->version)){
$tag = $tag->getCompound("Level");
}
$chunk = new $chunkClass($location, $offset, $compressedDataLength, $compressionScheme, $tag, $coordinates, $version);
$this->chunks[] = $chunk;
return $chunk;
}
/**
* @param McCoordinates3D $coordinates
* @return DataBlock
* @throws Exception
*/
public function getBlock(McCoordinates3D $coordinates): DataBlock
{
$chunk = $this->getChunkFromBlock($coordinates);
if ($chunk instanceof BlockChunk) {
return $chunk->getBlock($coordinates);
}
throw new Exception("Wrong chunk type");
}
/**
* @param string $name
* @param McCoordinatesFloat $coordinates
* @return Entity[]
* @throws Exception
*/
public function getEntities(string $name,McCoordinatesFloat $coordinates): array
{
$chunk = $this->getChunkFromBlock(McCoordinatesFloat::get3DCoordinates($coordinates));
if(!VersionHelper::hasEntitiesTag($this->version)){
throw new Exception("Entities not stored in block region");
}
if (!($chunk instanceof BlockChunk)) {
throw new Exception("Wrong chunk type");
}
return $chunk->getEntities($name, $coordinates);
}
/**
* @param string $name
* @param McCoordinatesFloat $coordinates
* @return array
* @throws Exception
*/
public function getBlockEntities(string $name, McCoordinatesFloat $coordinates): array
{
$chunk = $this->getChunkFromBlock(McCoordinatesFloat::get3DCoordinates($coordinates));
if (!($chunk instanceof BlockChunk)) {
throw new Exception("Wrong chunk type");
}
return $chunk->getBlockEntities($name, $coordinates);
}
/**
* @param McCoordinates3D $blockCoordinates
* @return array
* @throws Exception
*/
public function getAllEntitiesFromBlockChunk(McCoordinates3D $blockCoordinates): array
{
$chunk = $this->getChunkFromBlock($blockCoordinates);
if (!($chunk instanceof BlockChunk)) {
throw new Exception("Wrong chunk type");
}
if(!VersionHelper::hasEntitiesTag($this->version)){
throw new Exception("Entities not stored in block region");
}
return $chunk->getAllEntities();
}
/**
* @param McCoordinates3D $blockCoordinates
* @return array
* @throws Exception
*/
public function getAllBlockEntitiesFromBlockChunk(McCoordinates3D $blockCoordinates): array
{
$chunk = $this->getChunkFromBlock($blockCoordinates);
if (!($chunk instanceof BlockChunk)) {
throw new Exception("Wrong chunk type");
}
return $chunk->getAllBlockEntities();
}
/**
* Reads chunk and replaces block at $coordinates with $blockName
*
* @codeCoverageIgnore
* @param McCoordinates3D $coordinates
* @param string $blockName
* @return void
* @throws Exception
*/
public function replaceBlock(McCoordinates3D $coordinates, string $blockName = "minecraft:stone"): void
{
$chunk = $this->getChunkFromBlock($coordinates);
if ($chunk instanceof BlockChunk) {
$chunk->replaceBlock($coordinates, $blockName);
return;
}
throw new Exception("Wrong chunk type");
}
/**
* @param Entity $entity
* @return void
* @throws Exception
*/
public function deleteEntity(Entity $entity):void
{
$chunk = $this->getChunkFromBlock(McCoordinatesFloat::get3DCoordinates($entity->getCoordinates()));
if ($chunk instanceof BlockChunk) {
$chunk->deleteEntity($entity);
return;
}
throw new Exception("Wrong chunk type");
}
/**
* @param BlockEntity $entity
* @return void
* @throws Exception
*/
public function deleteBlockEntity(BlockEntity $entity):void
{
$chunk = $this->getChunkFromBlock($entity->getCoordinates());
if ($chunk instanceof BlockChunk) {
$chunk->deleteBlockEntity($entity);
return;
}
throw new Exception("Wrong chunk type");
}
}