From 82267125970463b6a85b0a7368be7c9957289942 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 21 Feb 2023 08:14:25 +0300 Subject: [PATCH] added nbt+made world actually load --- src/Player.php | 8 +- src/dependencies.php | 1 + src/nbt/NBT_new.php | 241 ++++++++++++++++++++++++++++++++++++++ src/nbt/tag/Byte.php | 35 ++++++ src/nbt/tag/ByteArray.php | 36 ++++++ src/nbt/tag/Compound.php | 81 +++++++++++++ src/nbt/tag/Double.php | 35 ++++++ src/nbt/tag/End.php | 35 ++++++ src/nbt/tag/Enum.php | 189 ++++++++++++++++++++++++++++++ src/nbt/tag/Float.php | 35 ++++++ src/nbt/tag/Int.php | 35 ++++++ src/nbt/tag/IntArray.php | 42 +++++++ src/nbt/tag/Long.php | 35 ++++++ src/nbt/tag/NamedTag.php | 45 +++++++ src/nbt/tag/Short.php | 36 ++++++ src/nbt/tag/String.php | 36 ++++++ src/nbt/tag/Tag.php | 47 ++++++++ src/world/Level.php | 91 ++++++++++++++ 18 files changed, 1059 insertions(+), 4 deletions(-) create mode 100644 src/nbt/NBT_new.php create mode 100644 src/nbt/tag/Byte.php create mode 100644 src/nbt/tag/ByteArray.php create mode 100644 src/nbt/tag/Compound.php create mode 100644 src/nbt/tag/Double.php create mode 100644 src/nbt/tag/End.php create mode 100644 src/nbt/tag/Enum.php create mode 100644 src/nbt/tag/Float.php create mode 100644 src/nbt/tag/Int.php create mode 100644 src/nbt/tag/IntArray.php create mode 100644 src/nbt/tag/Long.php create mode 100644 src/nbt/tag/NamedTag.php create mode 100644 src/nbt/tag/Short.php create mode 100644 src/nbt/tag/String.php create mode 100644 src/nbt/tag/Tag.php diff --git a/src/Player.php b/src/Player.php index 53c0d934..762da8d9 100755 --- a/src/Player.php +++ b/src/Player.php @@ -459,8 +459,8 @@ class Player{ } $pk = new FullChunkDataPacket; - $pk->putInt($X); - $pk->putInt($Z); + $pk->buffer .= Utils::writeInt($X); + $pk->buffer .= Utils::writeInt($Z); $pk->chunkX = $X; $pk->chunkZ = $Z; $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); @@ -522,10 +522,10 @@ class Player{ $Yndex |= 1 << $iY; } } - $pk = new ChunkDataPacket; + $pk = new FullChunkDataPacket; $pk->chunkX = $X; $pk->chunkZ = $Z; - $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); + $pk->data = $this->level->getOrderedFullChunk($X, $Z); $cnt = $this->dataPacket($pk); if($cnt === false){ return false; diff --git a/src/dependencies.php b/src/dependencies.php index b7af7702..99410216 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -94,6 +94,7 @@ require_once(FILE_PATH . "/src/astarnavigator/providers/MCBlockedProvider.php"); require_once(FILE_PATH . "/src/astarnavigator/ITileNavigator.php"); require_once(FILE_PATH . "/src/astarnavigator/TileNavigator.php"); +require_once(FILE_PATH . "/src/nbt/tag/NamedTag.php"); require_once(FILE_PATH . "/src/entity/ai/tasks/TaskBase.php"); require_once(FILE_PATH. "/src/entity/ai/tasks/TaskTempt.php"); diff --git a/src/nbt/NBT_new.php b/src/nbt/NBT_new.php new file mode 100644 index 00000000..09945c1d --- /dev/null +++ b/src/nbt/NBT_new.php @@ -0,0 +1,241 @@ +offset = strlen($this->buffer) - 1; + + return ""; + }elseif($len === true){ + return substr($this->buffer, $this->offset); + } + if($len > 1024){ + return substr($this->buffer, ($this->offset += $len) - $len, $len); + } + $buffer = ""; + for(; $len > 0; --$len, ++$this->offset){ + $buffer .= @$this->buffer[$this->offset]; + } + + return $buffer; + } + + public function put($v){ + $this->buffer .= $v; + } + + public function feof(){ + return !isset($this->buffer{$this->offset}); + } + + public function __construct($endianness = self::LITTLE_ENDIAN){ + $this->offset = 0; + $this->endianness = $endianness & 0x01; + } + + public function read($buffer){ + $this->offset = 0; + $this->buffer = $buffer; + $this->data = $this->readTag(); + $this->buffer = ""; + } + + public function readCompressed($buffer, $compression = ZLIB_ENCODING_GZIP){ + $this->read(zlib_decode($buffer)); + } + + public function write(){ + $this->offset = 0; + if($this->data instanceof Compound){ + $this->writeTag($this->data); + + return $this->buffer; + }else{ + return false; + } + } + + public function writeCompressed($compression = ZLIB_ENCODING_GZIP, $level = 7){ + if(($write = $this->write()) !== false){ + return zlib_encode($write, $compression, $level); + } + + return false; + } + + public function readTag(){ + switch($this->getByte()){ + case NBT::TAG_Byte: + $tag = new Byte($this->getString()); + $tag->read($this); + break; + case NBT::TAG_Short: + $tag = new Short($this->getString()); + $tag->read($this); + break; + case NBT::TAG_Int: + $tag = new Int($this->getString()); + $tag->read($this); + break; + case NBT::TAG_Long: + $tag = new Long($this->getString()); + $tag->read($this); + break; + case NBT::TAG_Float: + $tag = new Float($this->getString()); + $tag->read($this); + break; + case NBT::TAG_Double: + $tag = new Double($this->getString()); + $tag->read($this); + break; + case NBT::TAG_ByteArray: + $tag = new ByteArray($this->getString()); + $tag->read($this); + break; + case NBT::TAG_String: + $tag = new String($this->getString()); + $tag->read($this); + break; + case NBT::TAG_Enum: + $tag = new Enum($this->getString()); + $tag->read($this); + break; + case NBT::TAG_Compound: + $tag = new Compound($this->getString()); + $tag->read($this); + break; + case NBT::TAG_IntArray: + $tag = new IntArray($this->getString()); + $tag->read($this); + break; + + case NBT::TAG_End: //No named tag + default: + $tag = new End; + break; + } + return $tag; + } + + public function writeTag(Tag $tag){ + $this->putByte($tag->getType()); + if($tag instanceof NamedTAG){ + $this->putString($tag->getName()); + } + $tag->write($this); + } + + public function getByte($signed = false){ + return Utils::readByte($this->get(1), $signed); + } + + public function putByte($v){ + $this->buffer .= Utils::writeByte($v); + } + + public function getShort(){ + return $this->endianness === self::BIG_ENDIAN ? Utils::readShort($this->get(2)) : Utils::readLShort($this->get(2)); + } + + public function putShort($v){ + $this->buffer .= $this->endianness === self::BIG_ENDIAN ? Utils::writeShort($v) : Utils::writeLShort($v); + } + + public function getInt(){ + return $this->endianness === self::BIG_ENDIAN ? Utils::readInt($this->get(4)) : Utils::readLInt($this->get(4)); + } + + public function putInt($v){ + $this->buffer .= $this->endianness === self::BIG_ENDIAN ? Utils::writeInt($v) : Utils::writeLInt($v); + } + + public function getLong(){ + return $this->endianness === self::BIG_ENDIAN ? Utils::readLong($this->get(8)) : Utils::readLLong($this->get(8)); + } + + public function putLong($v){ + $this->buffer .= $this->endianness === self::BIG_ENDIAN ? Utils::writeLong($v) : Utils::writeLLong($v); + } + + public function getFloat(){ + return $this->endianness === self::BIG_ENDIAN ? Utils::readFloat($this->get(4)) : Utils::readLFloat($this->get(4)); + } + + public function putFloat($v){ + $this->buffer .= $this->endianness === self::BIG_ENDIAN ? Utils::writeFloat($v) : Utils::writeLFloat($v); + } + + public function getDouble(){ + return $this->endianness === self::BIG_ENDIAN ? Utils::readDouble($this->get(8)) : Utils::readLDouble($this->get(8)); + } + + public function putDouble($v){ + $this->buffer .= $this->endianness === self::BIG_ENDIAN ? Utils::writeDouble($v) : Utils::writeLDouble($v); + } + + public function getString(){ + return $this->get($this->getShort()); + } + + public function putString($v){ + $this->putShort(strlen($v)); + $this->buffer .= $v; + } + + public function getData(){ + return $this->data; + } + + public function setData(Compound $data){ + $this->data = $data; + } + +} diff --git a/src/nbt/tag/Byte.php b/src/nbt/tag/Byte.php new file mode 100644 index 00000000..4b0a5141 --- /dev/null +++ b/src/nbt/tag/Byte.php @@ -0,0 +1,35 @@ +value = $NBT_new->getByte(true); + } + + public function write(NBT_new $NBT_new){ + $NBT_new->putByte($this->value); + } +} \ No newline at end of file diff --git a/src/nbt/tag/ByteArray.php b/src/nbt/tag/ByteArray.php new file mode 100644 index 00000000..c7abc98e --- /dev/null +++ b/src/nbt/tag/ByteArray.php @@ -0,0 +1,36 @@ +value = $NBT_new->get($NBT_new->getInt()); + } + + public function write(NBT_new $NBT_new){ + $NBT_new->putInt(strlen($this->value)); + $NBT_new->put($this->value); + } +} \ No newline at end of file diff --git a/src/nbt/tag/Compound.php b/src/nbt/tag/Compound.php new file mode 100644 index 00000000..cad3d6e3 --- /dev/null +++ b/src/nbt/tag/Compound.php @@ -0,0 +1,81 @@ +name = $name; + foreach($value as $tag){ + $this->{$tag->getName()} = $tag; + } + } + + public function offsetExists($offset){ + return isset($this->{$offset}); + } + + public function offsetGet($offset){ + if($this->{$offset} instanceof Tag){ + if($this->{$offset} instanceof \ArrayAccess){ + return $this->{$offset}; + }else{ + return $this->{$offset}->getValue(); + } + } + + return null; + } + + public function offsetSet($offset, $value){ + if($value instanceof Tag){ + $this->{$offset} = $value; + }elseif(isset($this->{$offset}) and $this->{$offset} instanceof Tag){ + $this->{$offset}->setValue($value); + } + } + + public function offsetUnset($offset){ + unset($this->{$offset}); + } + + public function getType(){ + return NBT_new::TAG_Compound; + } + + public function read(NBT_new $NBT_new){ + $this->value = []; + do{ + $tag = $NBT_new->readTag(); + if($tag instanceof NamedTag and $tag->getName() !== ""){ + $this->{$tag->getName()} = $tag; + } + }while(!($tag instanceof End) and !$NBT_new->feof()); + } + + public function write(NBT_new $NBT_new){ + foreach($this as $tag){ + if($tag instanceof Tag and !($tag instanceof End)){ + $NBT_new->writeTag($tag); + } + } + $NBT_new->writeTag(new End); + } +} \ No newline at end of file diff --git a/src/nbt/tag/Double.php b/src/nbt/tag/Double.php new file mode 100644 index 00000000..9e7f8c75 --- /dev/null +++ b/src/nbt/tag/Double.php @@ -0,0 +1,35 @@ +value = $NBT_new->getDouble(); + } + + public function write(NBT_new $NBT_new){ + $NBT_new->putDouble($this->value); + } +} \ No newline at end of file diff --git a/src/nbt/tag/End.php b/src/nbt/tag/End.php new file mode 100644 index 00000000..35c0ea84 --- /dev/null +++ b/src/nbt/tag/End.php @@ -0,0 +1,35 @@ +name = $name; + foreach($value as $k => $v){ + $this->{$k} = $v; + } + } + + public function &getValue(){ + $value = []; + foreach($this as $k => $v){ + if($v instanceof Tag){ + $value[$k] = $v; + } + } + return $value; + } + + public function offsetExists($offset){ + return isset($this->{$offset}); + } + + public function offsetGet($offset){ + if($this->{$offset} instanceof Tag){ + if($this->{$offset} instanceof \ArrayAccess){ + return $this->{$offset}; + }else{ + return $this->{$offset}->getValue(); + } + } + + return null; + } + + public function offsetSet($offset, $value){ + if($value instanceof Tag){ + $this->{$offset} = $value; + }elseif($this->{$offset} instanceof Tag){ + $this->{$offset}->setValue($value); + } + } + + public function offsetUnset($offset){ + unset($this->{$offset}); + } + + public function count($mode = COUNT_NORMAL){ + for($i = 0; true; $i++){ + if(!isset($this->{$i})){ + return $i; + } + if($mode === COUNT_RECURSIVE){ + if($this->{$i} instanceof \Countable){ + $i += count($this->{$i}); + } + } + } + } + + public function getType(){ + return NBT_new::TAG_Enum; + } + + public function setTagType($type){ + $this->tagType = $type; + } + + public function getTagType(){ + return $this->tagType; + } + + public function read(NBT_new $NBT_new){ + $this->value = []; + $this->tagType = $NBT_new->getByte(); + $size = $NBT_new->getInt(); + for($i = 0; $i < $size and !$NBT_new->feof(); ++$i){ + switch($this->tagType){ + case NBT_new::TAG_Byte: + $tag = new Byte(false); + $tag->read($NBT_new); + $this->{$i} = $tag; + break; + case NBT_new::TAG_Short: + $tag = new Short(false); + $tag->read($NBT_new); + $this->{$i} = $tag; + break; + case NBT_new::TAG_Int: + $tag = new Int(false); + $tag->read($NBT_new); + $this->{$i} = $tag; + break; + case NBT_new::TAG_Long: + $tag = new Long(false); + $tag->read($NBT_new); + $this->{$i} = $tag; + break; + case NBT_new::TAG_Float: + $tag = new Float(false); + $tag->read($NBT_new); + $this->{$i} = $tag; + break; + case NBT_new::TAG_Double: + $tag = new Double(false); + $tag->read($NBT_new); + $this->{$i} = $tag; + break; + case NBT_new::TAG_ByteArray: + $tag = new ByteArray(false); + $tag->read($NBT_new); + $this->{$i} = $tag; + break; + case NBT_new::TAG_String: + $tag = new String(false); + $tag->read($NBT_new); + $this->{$i} = $tag; + break; + case NBT_new::TAG_Enum: + $tag = new $this(false); + $tag->read($NBT_new); + $this->{$i} = $tag; + break; + case NBT_new::TAG_Compound: + $tag = new Compound(false); + $tag->read($NBT_new); + $this->{$i} = $tag; + break; + case NBT_new::TAG_IntArray: + $tag = new IntArray(false); + $tag->read($NBT_new); + $this->{$i} = $tag; + break; + } + } + } + + public function write(NBT_new $NBT_new){ + if(!isset($this->tagType)){ + foreach($this as $tag){ + if($tag instanceof Tag){ + if(!isset($id)){ + $id = $tag->getType(); + }elseif($id !== $tag->getType()){ + return false; + } + } + } + $this->tagType = @$id; + } + + $NBT_new->putByte($this->tagType); + + /** @var Tag[] $tags */ + $tags = []; + foreach($this as $tag){ + if($tag instanceof Tag){ + $tags[] = $tag; + } + } + $NBT_new->putInt(count($tags)); + foreach($tags as $tag){ + $tag->write($NBT_new); + } + } +} diff --git a/src/nbt/tag/Float.php b/src/nbt/tag/Float.php new file mode 100644 index 00000000..d2ecc209 --- /dev/null +++ b/src/nbt/tag/Float.php @@ -0,0 +1,35 @@ +value = $NBT_new->getFloat(); + } + + public function write(NBT_new $NBT_new){ + $NBT_new->putFloat($this->value); + } +} diff --git a/src/nbt/tag/Int.php b/src/nbt/tag/Int.php new file mode 100644 index 00000000..ae893249 --- /dev/null +++ b/src/nbt/tag/Int.php @@ -0,0 +1,35 @@ +value = $NBT_new->getInt(); + } + + public function write(NBT_new $NBT_new){ + $NBT_new->putInt($this->value); + } +} diff --git a/src/nbt/tag/IntArray.php b/src/nbt/tag/IntArray.php new file mode 100644 index 00000000..1d9a3b69 --- /dev/null +++ b/src/nbt/tag/IntArray.php @@ -0,0 +1,42 @@ +value = []; + $size = $NBT_new->getInt(); + for($i = 0; $i < $size and !$NBT_new->feof(); ++$i){ + $this->value[] = $NBT_new->getInt(); + } + } + + public function write(NBT_new $NBT_new){ + $NBT_new->putInt(count($this->value)); + foreach($this->value as $v){ + $NBT_new->putInt($v); + } + } +} \ No newline at end of file diff --git a/src/nbt/tag/Long.php b/src/nbt/tag/Long.php new file mode 100644 index 00000000..b4c24c51 --- /dev/null +++ b/src/nbt/tag/Long.php @@ -0,0 +1,35 @@ +value = $NBT_new->getLong(); + } + + public function write(NBT_new $NBT_new){ + $NBT_new->putLong($this->value); + } +} \ No newline at end of file diff --git a/src/nbt/tag/NamedTag.php b/src/nbt/tag/NamedTag.php new file mode 100644 index 00000000..f3536e8c --- /dev/null +++ b/src/nbt/tag/NamedTag.php @@ -0,0 +1,45 @@ +name = $name; + if($value !== false){ + $this->value = $value; + } + } + + public function getName(){ + return $this->name === false ? "" : $this->name; + } + + public function setName($name){ + $this->name = $name; + } +} diff --git a/src/nbt/tag/Short.php b/src/nbt/tag/Short.php new file mode 100644 index 00000000..dd977393 --- /dev/null +++ b/src/nbt/tag/Short.php @@ -0,0 +1,36 @@ +value = $NBT_new->getShort(); + } + + public function write(NBT_new $NBT_new){ + $NBT_new->putShort($this->value); + } +} \ No newline at end of file diff --git a/src/nbt/tag/String.php b/src/nbt/tag/String.php new file mode 100644 index 00000000..de6e3110 --- /dev/null +++ b/src/nbt/tag/String.php @@ -0,0 +1,36 @@ +value = $NBT_new->get($NBT_new->getShort()); + } + + public function write(NBT_new $NBT_new){ + $NBT_new->putShort(strlen($this->value)); + $NBT_new->put($this->value); + } +} diff --git a/src/nbt/tag/Tag.php b/src/nbt/tag/Tag.php new file mode 100644 index 00000000..a2bc9e99 --- /dev/null +++ b/src/nbt/tag/Tag.php @@ -0,0 +1,47 @@ +value; + } + + public abstract function getType(); + + public function setValue($value){ + $this->value = $value; + } + + abstract public function write(NBT_new $NBT_new); + + abstract public function read(NBT_new $NBT_new); + + public final function __toString(){ + return $this->value; + } +} diff --git a/src/world/Level.php b/src/world/Level.php index 04868cda..9673ea7e 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -81,6 +81,97 @@ class Level{ unset($this->mobSpawner->level); } + + public function getOrderedFullChunk($X, $Z){ + //todo: make asynchronous + if(!isset($this->level)){ + return false; + } + /*if(ADVANCED_CACHE == true){ + $identifier = "world:{$this->name}:$X:$Z"; + if(($cache = Cache::get($identifier)) !== false){ + return $cache; + } + }*/ + + $orderedIds = ""; + $orderedData = ""; + $orderedSkyLight = str_repeat("\x00", 16*16*64); + $orderedLight = str_repeat("\x00", 16*16*64); + $orderedBiomeIds = str_repeat("\x01", 16*16); //all plains, according to PocketMine 1.4 source + $orderedBiomeColors = str_repeat("\x00\x85\xb2\x4a", 256); // also PM 1.4 + $tileEntities = ""; + $this->level->loadChunk($X, $Z); + + $miniChunks = []; + + for($y = 0; $y < 8; ++$y){ + $miniChunks[$y] = $this->level->getMiniChunk($X, $Z, $y); + } + + for ($i = 0; $i < 16; $i++){ + for ($j = 0; $j < 16; $j++){ + $bIndex = ($i << 5) + ($j << 9); + foreach($miniChunks as $chunk){ + $orderedIds .= substr($chunk, $bIndex, 16); + $orderedData .= substr($chunk, $bIndex + 16, 8); + } + } + } + + $chunkTiles = []; + + $tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '".$this->getName()."' AND x >= ".($X * 16 - 1)." AND x < ".($X * 16 + 17)." AND z >= ".($Z * 16 - 1)." AND z < ".($Z * 16 + 17).";"); + if($tiles !== false and $tiles !== true){ + while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ + $tile = $this->server->api->tile->getByID($tile["ID"]); + if($tile instanceof Tile){ + $chunkTiles[] = $tile; + } + } + } + + $nbt = new NBT_new(NBT_new::LITTLE_ENDIAN); + foreach($chunkTiles as $tile){ + switch($tile->class){ + case "Sign": + $text = $tile->getText(); + $nbt->setData(new Compound("", array( + new String("Text1", $text[0]), + new String("Text2", $text[1]), + new String("Text3", $text[2]), + new String("Text4", $text[3]), + new String("id", "Sign"), + new Int("x", (int) $tile->x), + new Int("y", (int) $tile->y), + new Int("z", (int) $tile->z) + ))); + $tileEntities .= $nbt->write(); + break; + case "Furnace": + //nutting, not spawnable :D + break; + case "Chest": + $nbt->setData(new Compound("", array( + new String("id", "Chest"), + new Int("x", (int) $tile->x), + new Int("y", (int) $tile->y), + new Int("z", (int) $tile->z) + ))); + $tileEntities .= $nbt->write(); + break; + } + } + $orderedUncompressed = Utils::writeLInt($X) . Utils::writeLInt($Z) . + $orderedIds . $orderedData . $orderedSkyLight . $orderedLight . + $orderedBiomeIds . $orderedBiomeColors . $tileEntities; + $ordered = zlib_encode($orderedUncompressed, ZLIB_ENCODING_DEFLATE, 6); + /*if(ADVANCED_CACHE == true){ + Cache::add($identifier, $ordered, 60); + }*/ + return $ordered; + } + public function isDay(){ return $this->getTime() % 19200 < TimeAPI::$phases["sunset"]; }