1
+ <?php
2
+
3
+ namespace Aternos \Hawk \Versions \v1139 ;
4
+
5
+ use Aternos \Hawk \Data ;
6
+ use Aternos \Hawk \DataBlock ;
7
+ use Aternos \Hawk \McCoordinates2D ;
8
+ use Aternos \Hawk \McCoordinates3D ;
9
+ use Aternos \Hawk \Palette ;
10
+ use Aternos \Hawk \PaletteBlock ;
11
+ use Aternos \Hawk \Section ;
12
+ use Aternos \Nbt \Tag \ByteTag ;
13
+ use Aternos \Nbt \Tag \CompoundTag ;
14
+ use Aternos \Nbt \Tag \ListTag ;
15
+
16
+ class SectionV1139 extends Section
17
+ {
18
+ protected Palette $ palette ;
19
+
20
+ protected Data $ data ;
21
+
22
+ /**
23
+ * Overloaded with new and newFromTag
24
+ */
25
+ protected function __construct ()
26
+ {
27
+ parent ::__construct ();
28
+ }
29
+
30
+ /**
31
+ * @inheritDoc
32
+ */
33
+ public static function newEmpty (McCoordinates3D $ coordinates , int $ version ): Section
34
+ {
35
+ $ section = new static ();
36
+ $ section ->coordinates = $ coordinates ;
37
+ $ section ->version = $ version ;
38
+
39
+ $ paletteBlocks [] = PaletteBlock::new ();
40
+ $ section ->palette = static ::newPalette ($ paletteBlocks );
41
+
42
+
43
+ $ dataBlocks = [];
44
+ $ dataBlock = new DataBlock (0 , $ paletteBlocks [0 ]);
45
+ for ($ i = 0 ; $ i < static ::BLOCKS_PER_SECTION ; $ i ++) {
46
+ $ dataBlocks [] = $ dataBlock ;
47
+ }
48
+
49
+ $ section ->data = static ::newData ($ dataBlocks );
50
+ return $ section ;
51
+ }
52
+
53
+ /**
54
+ * @inheritDoc
55
+ */
56
+ public static function newFromTag (CompoundTag $ tag , McCoordinates2D $ coordinates , int $ version ): ?Section
57
+ {
58
+ $ section = new static ();
59
+ $ section ->tag = $ tag ;
60
+ $ section ->version = $ version ;
61
+ $ section ->coordinates = new McCoordinates3D ($ coordinates ->x , $ tag ->getByte ("Y " )->getValue (), $ coordinates ->z );
62
+ $ paletteTag = $ tag ->getList ($ section ->paletteTagName );
63
+ if ($ paletteTag === null ) {
64
+ return null ;
65
+ }
66
+ $ section ->palette = $ section ->newPaletteFromTag ($ paletteTag );
67
+ $ section ->data = $ section ->newDataFromTag ($ section , $ tag , $ section ->coordinates ->y );
68
+ return $ section ;
69
+ }
70
+
71
+ /**
72
+ * @param array $dataBlocks
73
+ * @return Data
74
+ */
75
+ protected static function newData (array $ dataBlocks ): Data
76
+ {
77
+ return DataV1139::new ($ dataBlocks );
78
+ }
79
+
80
+ /**
81
+ * @param Section $section
82
+ * @param CompoundTag $tag
83
+ * @param int $sectionY
84
+ * @return Data
85
+ */
86
+ protected static function newDataFromTag (Section $ section , CompoundTag $ tag , int $ sectionY ): Data
87
+ {
88
+ return DataV1139::newFromTag ($ tag ->getLongArray ($ section ->dataTagName ), $ section ->palette , $ section ->version , $ sectionY );
89
+ }
90
+
91
+ /**
92
+ * @param array $paletteBlocks
93
+ * @return Palette
94
+ */
95
+ protected static function newPalette (array $ paletteBlocks ): Palette
96
+ {
97
+ return Palette::new ($ paletteBlocks );
98
+ }
99
+
100
+ /**
101
+ * @param ListTag $paletteTag
102
+ * @return Palette
103
+ */
104
+ protected static function newPaletteFromTag (ListTag $ paletteTag ): Palette
105
+ {
106
+ return Palette::newFromTag ($ paletteTag );
107
+ }
108
+
109
+ /**
110
+ * @inheritDoc
111
+ */
112
+ public function getBlock (McCoordinates3D $ coordinates ): DataBlock
113
+ {
114
+ $ index = $ this ->calcDataBlocksIndex (Section::getBlockCoordinates ($ coordinates ));
115
+
116
+ // If section is empty
117
+ if (isset ($ this ->palette ) && $ this ->getPalette ()->getLength () === 1 && $ this ->getPalette ()->getPaletteBlocks ()[0 ]->getName () === "minecraft:air " ) {
118
+ return new DataBlock (0 , $ this ->getPalette ()->getPaletteBlocks ()[0 ]);
119
+ }
120
+ return $ this ->getData ()->getDataBlock ($ index );
121
+ }
122
+
123
+ /**
124
+ * @inheritDoc
125
+ */
126
+ public function replaceBlock (McCoordinates3D $ coordinates , string $ blockName = "minecraft:stone " ): void
127
+ {
128
+ $ index = $ this ->calcDataBlocksIndex (Section::getBlockCoordinates ($ coordinates ));
129
+ $ dataBlock = $ this ->createDataBlock ($ blockName );
130
+ $ this ->getData ()->setDataBlock ($ index , $ dataBlock );
131
+ }
132
+
133
+ /**
134
+ * @inheritDoc
135
+ */
136
+ public function createTag (): CompoundTag
137
+ {
138
+ $ section = new CompoundTag ();
139
+ $ section ->set ("Y " , (new ByteTag ())->setValue ($ this ->coordinates ->y ));
140
+ $ section ->set ($ this ->dataTagName , $ this ->data ->createTag ($ this ->getPalette ()->getLength (), $ this ->coordinates ->y ));
141
+ $ section ->set ($ this ->paletteTagName , $ this ->palette ->createTag ());
142
+ return $ section ;
143
+ }
144
+
145
+ /**
146
+ * @return Palette
147
+ */
148
+ public function getPalette (): Palette
149
+ {
150
+ return $ this ->palette ;
151
+ }
152
+
153
+ /**
154
+ * @return Data
155
+ */
156
+ public function getData (): Data
157
+ {
158
+ return $ this ->data ;
159
+ }
160
+
161
+ /**
162
+ * @param string $name
163
+ * @param array $properties
164
+ * @return PaletteBlock
165
+ */
166
+ public function createPaletteBlock (string $ name = "minecraft:air " , array $ properties = []): PaletteBlock
167
+ {
168
+ $ paletteBlock = PaletteBlock::new ($ name , $ properties );
169
+ $ this ->getPalette ()->addPaletteBlock ($ paletteBlock );
170
+ return $ paletteBlock ;
171
+ }
172
+
173
+ /**
174
+ * @param string $blockName
175
+ * @return DataBlock
176
+ */
177
+ public function createDataBlock (string $ blockName = "minecraft:stone " ): DataBlock
178
+ {
179
+ $ paletteBlock = $ this ->getPalette ()->findPaletteBlock ($ blockName );
180
+ if ($ paletteBlock === null ) {
181
+ $ paletteBlock = $ this ->createPaletteBlock ($ blockName );
182
+ }
183
+ $ arraySearch = array_search ($ paletteBlock , $ this ->palette ->getPaletteBlocks ());
184
+
185
+ return new DataBlock ($ arraySearch , $ this ->getPalette ()->getPaletteBlocks ()[$ arraySearch ]);
186
+ }
187
+ }
0 commit comments