forked from shoghicp/SimpleWorlds
/
SimpleWorlds.php
393 lines (333 loc) · 12 KB
/
SimpleWorlds.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/*
__PocketMine Plugin__
name=SimpleWorlds
description=Simple world loader, generator, exporter
version=0.3.1
author=PocketMine Team
class=SimpleWorlds
apiversion=7,8,9,10,11,12
*/
/*
Small Changelog
===============
0.1
- Initial release
0.2
- Unload support
0.3
- Export levels to MCPE format
0.3.1
- Alpha_1.3.12
*/
class SimpleWorlds implements Plugin{
public static $chunkHeader = "base64://7c1Td6thEIbhado0TdIUSWbbtm3btm3btm3btm3btu37L+z1nn6z1nU4z60BIhoFKZEPldECfTEJy7EHF/EMv+ENEE2KXDwbnpZnpwl6YhwWYwfO4hG+I8wmmhDZUBoN0BWjbOb9+exswUncw2e4AkXjIhOKow46YhhmYwOO4lagef89O8FBojGRDoVRA20xCNOxBgdxDa9hs4tGRSq7eT8/O1XQEv0wGSuwF5fwHH/gCxZNhtyogKboFWzeH8/OEuzEOTzGD4Q7RBMhO8qgIbphNBZgK045zPv32fkCd4hoPGRGCdRFJwzHHGzEMdzGBzicorGc5v307BRBTbTDYMzAWhzCdbxBoEs0GlKjAKqilcu835+dKViJfbiMF/gLv1s0OfKgIpqhNyZgKXa5zfvn2XmCn4gIFU2MHCiLRuiOMViIbTiNB/iKUI95P75HNAtKoh46YwTmYhOO4w4+IiRMNDYyoChqhZn327MzBDOxDodxA28RFC4aHWlQENXQGgMwFavCzfv72bmCl5AIUUUK5EUlNEcfTMQy7MYFPMWvCPN+ZKRoEuREOTRGD4zFImzHGTzEN3i8ogmQFaW85v367HTBSMzDZpzAXXyC0ycaBxlRDLXRAUMxy2feX8/OEdzEO9j9ojGQFoVQHW0wENOwGgdwFa/8Yp111llnnXXWWfe/9w8=";
private $api, $config;
public function __construct(ServerAPI $api, $server = false){
$this->api = $api;
if(substr(self::$chunkHeader, 0, 9) === "base64://"){
self::$chunkHeader = gzinflate(base64_decode(substr(self::$chunkHeader, 9)));
}
}
public function init(){
$this->api->console->register("simpleworlds", "<command> [parameters...]", array($this, "command"));
$this->api->console->alias("sw", "simpleworlds");
$this->api->console->alias("swe", "simpleworlds export");
$this->api->console->alias("swu", "simpleworlds unload");
$this->api->console->alias("swl", "simpleworlds load");
$this->api->console->alias("swg", "simpleworlds generate");
$this->api->console->alias("swls", "simpleworlds list");
$this->api->console->alias("swd", "simpleworlds delete");
$this->config = new Config($this->api->plugin->configPath($this)."config.yml", CONFIG_YAML, array(
"default-generator" => "SuperflatGenerator",
"autogenerate" => false,
"autoload" => array(),
));
console("[SimpleWorlds] Loading levels...");
foreach($this->config->get("autoload") as $level){
$this->loadLevel($level);
}
}
public function command($cmd, $params, $issuer, $alias){
$output = "";
if($cmd === "simpleworlds"){
if(count($params) < 2 && $params[0] != "list"){
$output .= "Usage: /$cmd <command> [parameters...]\n";
$output .= "/$cmd load <levelName>: Loads a level on file.\n";
$output .= "/$cmd unload <levelName>: Safely unload a level.\n";
$output .= "/$cmd export <levelName>: Exports a loaded level to MCPE format.\n";
$output .= "/$cmd generate <seed> <generatorName> <levelName>: Generates a new world usign a generator with an specific seed.\n";
$output .= "/$cmd list: List all available levels.\n";
$output .= "/$cmd delete: Delete the specific level.\n";
return $output;
}
$subcmd = strtolower(array_shift($params));
switch($subcmd){
case "unload":
$level = $this->api->level->get(implode(" ", $params));
if($level instanceof Level){
if($this->api->level->unloadLevel($level) === true){
$output .= "Level unloaded.\n";
break;
}
}
$output .= "Error unloading level.\n";
break;
case "load":
if($this->loadLevel(implode(" ", $params)) === false){
$output .= "Error loading level.\n";
}else{
$output .= "Level loaded.\n";
}
break;
case "export":
$level = $this->api->level->get(implode(" ", $params));
if($level instanceof Level){
if($this->exportLevel($level) === false){
$output .= "Error exporting level.\n";
}else{
$output .= "Level correctly exported.\n";
}
}else{
$output .= "Level not loaded.\n";
}
break;
case "generate":
$seed = intval(array_shift($params));
$generator = $params[0] === "default" ? false:$params[0];
array_shift($params);
if($this->generateLevel(implode(" ", $params), $seed, $generator) === false){
$output .= "Error generating level.\n";
}else{
$output .= "Level generated.\n";
}
break;
case "list":
$ouput .= "The following levels are available:";
if ($handle = opendir(DATA_PATH . '/worlds/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry[0] != ".") {
$output .= $entry . "\n";
}
}
closedir($handle);
}
break;
case "delete":
$name = implode(" ", $params);
if (strpos($name, "..") !== false){
$output .= "Invalid level name";
break;
}
$level = $this->api->level->get($name);
if($level instanceof Level){
if($this->api->level->unloadLevel($level) !== true){
$output .= "Failed to unload level " . $name . "\n";
break;
}
}
$dir = DATA_PATH . "worlds/" . $name;
if($this->deleteDir($dir)){
$output .= "Deleted level " . $name . "\n";
}else{
$output .= "Failed to delete level " . $name . "\n";
}
break;
}
}
return $output;
}
public function exportLevel(Level $level){
$level->save(false, true);
@mkdir($this->api->plugin->configPath($this)."export/");
$path = $this->api->plugin->configPath($this)."export/".$level->getName()."/";
@mkdir($path);
$chunks = fopen($path."chunks.dat", "w");
fwrite($chunks, self::$chunkHeader);
for($Z = 0; $Z < 16; ++$Z){
for($X = 0; $X < 16; ++$X){
$chunk = "";
$miniChunks = array();
for($Y = 0; $Y < 8; ++$Y){
$miniChunks[$Y] = $level->level->getMiniChunk($X, $Z, $Y);
}
$columns = array();
for($x = 0; $x < 16; ++$x){
for($z = 0; $z < 16; ++$z){
$index = ($x << 4) + $z;
$j = ($z << 9) + ($x << 5);
$columns[$index] = array("", "", str_repeat("\x00", 64), str_repeat("\x00", 64));
foreach($miniChunks as $raw){
$columns[$index][0] .= substr($raw, $j, 16); //Block IDs
$columns[$index][1] .= substr($raw, $j + 16, 8); //Block Metadata
}
}
}
for($i = 0; $i < 4; ++$i){
for($x = 0; $x < 16; ++$x){
for($z = 0; $z < 16; ++$z){
$chunk .= $columns[($x << 4) + $z][$i];
}
}
}
unset($columns, $miniChunks);
$chunk = str_pad($chunk, 86012, "\x00", STR_PAD_RIGHT);
fwrite($chunks, Utils::writeLInt(strlen($chunk)) . $chunk);
}
}
fclose($chunks);
$nbt = new NBT();
$nbt->write(chr(NBT::TAG_COMPOUND)."\x00\x00");
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("GameType");
$nbt->writeTAG_Int(0);
//$nbt->writeTAG_Int((int) $level->getGamemode());
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("LastPlayed");
$nbt->writeTAG_Int(time());
$nbt->write(chr(NBT::TAG_STRING));
$nbt->writeTAG_String("LevelName");
$nbt->writeTAG_String($level->getName());
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("Platform");
$nbt->writeTAG_Int(2);
$nbt->write(chr(NBT::TAG_COMPOUND));
$nbt->writeTAG_String("Player");
//Nothing here!
$nbt->write(chr(NBT::TAG_END));
$nbt->write(chr(NBT::TAG_LONG));
$nbt->writeTAG_String("RandomSeed");
$nbt->writeTAG_Long($level->getSeed());
$nbt->write(chr(NBT::TAG_LONG));
$nbt->writeTAG_String("SizeOnDisk");
$nbt->writeTAG_Long(filesize($path."chunks.dat"));
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("SpawnX");
$nbt->writeTAG_Int((int) $level->getSpawn()->x);
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("SpawnY");
$nbt->writeTAG_Int((int) $level->getSpawn()->y);
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("SpawnZ");
$nbt->writeTAG_Int((int) $level->getSpawn()->z);
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("StorageVersion");
$nbt->writeTAG_Int(3);
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("Time");
$nbt->writeTAG_Int($level->getTime());
$nbt->write(chr(NBT::TAG_END));
@file_put_contents($path."level.dat", Utils::writeLInt(3).Utils::writeLInt(strlen($nbt->binary)).$nbt->binary);
$nbt->binary = b"";
$nbt->write(chr(NBT::TAG_COMPOUND)."\x00\x00");
$nbt->write(chr(NBT::TAG_LIST));
$nbt->writeTAG_String("Entities");
$nbt->writeTAG_Byte(NBT::TAG_COMPOUND);
$nbt->writeTAG_Int(0);
$nbt->write(chr(NBT::TAG_LIST));
$nbt->writeTAG_String("TileEntities");
$nbt->writeTAG_Byte(NBT::TAG_COMPOUND);
$tiles = $level->tiles->getAll();
$nbt->writeTAG_Int(count($tiles));
foreach($tiles as $tid => $data){
$nbt->write(chr(NBT::TAG_STRING));
$nbt->writeTAG_String("id");
$nbt->writeTAG_String($data["id"]);
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("x");
$nbt->writeTAG_Int($data["x"]);
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("y");
$nbt->writeTAG_Int($data["y"]);
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("z");
$nbt->writeTAG_Int($data["z"]);
if($data["id"] === "Sign"){
$nbt->write(chr(NBT::TAG_STRING));
$nbt->writeTAG_String("Text1");
$nbt->writeTAG_String($data["Text1"]);
$nbt->write(chr(NBT::TAG_STRING));
$nbt->writeTAG_String("Text2");
$nbt->writeTAG_String($data["Text2"]);
$nbt->write(chr(NBT::TAG_STRING));
$nbt->writeTAG_String("Text3");
$nbt->writeTAG_String($data["Text3"]);
$nbt->write(chr(NBT::TAG_STRING));
$nbt->writeTAG_String("Text4");
$nbt->writeTAG_String($data["Text4"]);
}elseif($data["id"] === "Furnace"){
$nbt->write(chr(NBT::TAG_SHORT));
$nbt->writeTAG_String("BurnTime");
$nbt->writeTAG_Short($data["BurnTime"]);
$nbt->write(chr(NBT::TAG_SHORT));
$nbt->writeTAG_String("CookTime");
$nbt->writeTAG_Short($data["CookTime"]);
}elseif($data["id"] === "Chest" and isset($data["pairx"]) and isset($data["pairz"])){
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("pairx");
$nbt->writeTAG_Int($data["pairx"]);
$nbt->write(chr(NBT::TAG_INT));
$nbt->writeTAG_String("pairz");
$nbt->writeTAG_Int($data["pairz"]);
}
if($data["id"] === "Furnace" or $data["id"] === "Chest"){
$nbt->write(chr(NBT::TAG_LIST));
$nbt->writeTAG_String("Items");
$nbt->writeTAG_Byte(NBT::TAG_COMPOUND);
$nbt->writeTAG_Int(count($data["Items"]));
foreach($data["Items"] as $index => $item){
$nbt->write(chr(NBT::TAG_BYTE));
$nbt->writeTAG_String("Slot");
$nbt->writeTAG_Byte($item["Slot"]);
$nbt->write(chr(NBT::TAG_SHORT));
$nbt->writeTAG_String("id");
$nbt->writeTAG_Short($data["id"]);
$nbt->write(chr(NBT::TAG_SHORT));
$nbt->writeTAG_String("Damage");
$nbt->writeTAG_Short($data["Damage"]);
$nbt->write(chr(NBT::TAG_BYTE));
$nbt->writeTAG_String("Count");
$nbt->writeTAG_Byte($item["Count"]);
$nbt->write(chr(NBT::TAG_END));
}
}
$nbt->write(chr(NBT::TAG_END));
}
$nbt->write(chr(NBT::TAG_END));
@file_put_contents($path."entities.dat", "ENT\x00".Utils::writeLInt(1).Utils::writeLInt(strlen($nbt->binary)).$nbt->binary);
}
public function loadLevel($name){
if(($ret = $this->api->level->loadLevel($name)) === false and $this->config->get("autogenerate") == true){
$this->api->level->generateLevel($name, false, $this->config->get("default-generator"));
$ret = $this->api->level->loadLevel($name);
}
return $ret;
}
public function generateLevel($name, $seed, $generator = false){
if($this->api->level->levelExists($name)){
return false;
}
if($generator === false){
$generator = $this->config->get("default-generator");
}
return $this->api->level->generateLevel($name, $seed, $generator);
}
public function deleteDir($dirPath) {
if (!is_dir($dirPath)) {
return false;
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
if (!$this->deleteDir($file))
return false;
} else {
unlink($file);
}
}
rmdir($dirPath);
return true;
}
public function __destruct(){
}
}