From 929b5bab36a1067e23c04acf29c9be208e790861 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 18 Aug 2026 05:55:32 +0300 Subject: [PATCH] initial tileentity rewrite, added missing furnace recipe --- src/API/EntityAPI.php | 4 +- src/API/TileAPI.php | 71 ++- src/Player.php | 31 +- src/PocketMinecraftServer.php | 1 - src/constants/EntityIDs.php | 1 + src/pmf/PMFLevel.php | 2 +- src/recipes/SmeltingData.php | 1 + src/world/Level.php | 14 +- src/world/Tile.php | 480 --------------------- src/world/tileentity/ChestTileEntity.php | 119 +++++ src/world/tileentity/FurnaceTileEntity.php | 114 +++++ src/world/tileentity/SignTileEntity.php | 47 ++ src/world/tileentity/Tile.php | 260 +++++++++++ 13 files changed, 600 insertions(+), 545 deletions(-) delete mode 100644 src/world/Tile.php create mode 100644 src/world/tileentity/ChestTileEntity.php create mode 100644 src/world/tileentity/FurnaceTileEntity.php create mode 100644 src/world/tileentity/SignTileEntity.php create mode 100644 src/world/tileentity/Tile.php diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 1bb95b88..42a94cb3 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -190,8 +190,8 @@ class EntityAPI{ public function addRaw(Entity $e){ $eid = $e->eid; $this->entities[$eid] = $e; - $cX = (int)$this->entities[$eid]->x >> 4; - $cZ = (int)$this->entities[$eid]->z >> 4; + $cX = floor($this->entities[$eid]->x >> 4); + $cZ = floor($this->entities[$eid]->z >> 4); $e->level->entityListPositioned["$cX $cZ"][$eid] = $eid; $e->level->entityList[$eid] = &$this->entities[$eid]; $this->server->handle("entity.add", $this->entities[$eid]); diff --git a/src/API/TileAPI.php b/src/API/TileAPI.php index a5a48118..0190211f 100644 --- a/src/API/TileAPI.php +++ b/src/API/TileAPI.php @@ -3,6 +3,9 @@ class TileAPI{ private $server; + /** + * @var Tile[] + */ private $tiles; private $tCnt = 1; @@ -10,41 +13,32 @@ class TileAPI{ $this->tiles = []; $this->server = ServerAPI::request(); } - public function getXYZ(Level $level, $x, $y, $z){ - $tile = $this->server->query("SELECT * FROM tiles WHERE level = '{$level->getName()}' AND x = $x AND y = $y AND z = $z;", true); - if($tile !== false and $tile !== true and ($tile = $this->getByID($tile["ID"])) !== false){ - return $tile; + public function getXYZ(Level $level, int $x, int $y, int $z){ + $cX = ($x >> 4); + $cZ = ($z >> 4); + foreach($level->tileEntityListPositioned["$cX $cZ"] ?? [] as $tile){ + if($tile->x == $x && $tile->y == $y && $tile->z == $z) return $tile; } return false; } - public function invalidateAll(Level $level, $x, $y, $z){ - $x = (int) $x; - $y = (int) $y; - $z = (int) $z; - $tile = $this->server->query("SELECT id FROM tiles WHERE level = '{$level->getName()}' AND x = $x AND y = $y AND z = $z;", false); + public function invalidateAll(Level $level, int $x, int $y, int $z){ + $cX = ($x >> 4); + $cZ = ($z >> 4); $invcnt = 0; - if($tile instanceof SQLite3Result){ - while(($t = $tile->fetchArray(SQLITE3_ASSOC)) !== false){ - $tl = $this->getByID($t["ID"]); - if($tl instanceof Tile){ - ++$invcnt; - $tl->close(); - } - - if($invcnt > 1){ - ConsoleAPI::warn("{$level->getName()}: ($x $y $z) has more than 1 tile entity! Invalidated ID {$t["ID"]} (Total invaliated: $invcnt)"); - } + foreach($level->tileEntityListPositioned["$cX $cZ"] ?? [] as $tile){ + if($tile->x == $x && $tile->y == $y && $tile->z == $z){ + ++$invcnt; + $tile->close(); + } + if($invcnt > 1){ + ConsoleAPI::warn("{$level->getName()}: ($x $y $z) has more than 1 tile entity! Invalidated ID {$tile->id} (Total invaliated: $invcnt)"); } } } public function get(Position $pos){ - $tile = $this->server->query("SELECT * FROM tiles WHERE level = '" . $pos->level->getName() . "' AND x = {$pos->x} AND y = {$pos->y} AND z = {$pos->z};", true); - if($tile !== false and $tile !== true and ($tile = $this->getByID($tile["ID"])) !== false){ - return $tile; - } - return false; + return $this->getXYZ($pos->level, $pos->x, $pos->y, $pos->z); } public function getByID($id){ @@ -75,7 +69,13 @@ class TileAPI{ public function add(Level $level, $class, $x, $y, $z, $data = []){ $id = $this->tCnt++; - $this->tiles[$id] = new Tile($level, $id, $class, $x, $y, $z, $data); + $clz = Tile::$tileId2tileClass[$class] ?? "Tile"; + $this->tiles[$id] = $t = new $clz($level, $id, $class, $x, $y, $z, $data); + $cX = floor($t->x) >> 4; + $cZ = floor($t->z) >> 4; + $t->level->tileEntityList[$id] = $t; + $t->level->tileEntityListPositioned["$cX $cZ"][$id] = $t; + $this->spawnToAll($this->tiles[$id]); return $this->tiles[$id]; } @@ -96,17 +96,7 @@ class TileAPI{ public function getAll($level = null){ if($level instanceof Level){ - $tiles = []; - $l = $this->server->query("SELECT ID FROM tiles WHERE level = '" . $level->getName() . "';"); - if($l !== false and $l !== true){ - while(($t = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $t = $this->getByID($t["ID"]); - if($t instanceof Tile){ - $tiles[$t->id] = $t; - } - } - } - return $tiles; + return $level->tileEntityList; } return $this->tiles; } @@ -116,9 +106,14 @@ class TileAPI{ $t = $this->tiles[$id]; $this->tiles[$id] = null; unset($this->tiles[$id]); + if($t->level instanceof Level){ + $cX = floor($t->x >> 4); + $cZ = floor($t->z >> 4); + unset($t->level->tileEntityList[$id]); + unset($t->level->tileEntityListPositioned["$cX $cZ"][$id]); + } $t->closed = true; $t->close(); - $this->server->query("DELETE FROM tiles WHERE ID = " . $id . ";"); $this->server->api->dhandle("tile.remove", $t); $t = null; unset($t); diff --git a/src/Player.php b/src/Player.php index 4ad26a57..6c3ed3d2 100755 --- a/src/Player.php +++ b/src/Player.php @@ -882,15 +882,9 @@ class Player{ $pk->data = $this->level->getOrderedChunk($X, $Z, $Yndex); $cnt = $this->blockQueueDataPacket($pk); $this->chunkDataSent["$X:$Z"] = true; - - $tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '{$this->level->getName()}' AND x >= $minX AND x <= $maxX AND z >= $minZ AND z <= $maxZ;"); - $this->lastChunk = false; - 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){ - $tile->spawn($this); - } + foreach($this->level->tileEntityListPositioned["$X $Z"] ?? [] as $tile){ + if($tile instanceof Tile){ + $tile->spawn($this); } } @@ -913,20 +907,14 @@ class Player{ } public function onChunkReceived($blockX, $blockZ){ - $minX = $blockX; - $maxX = $blockX + 15; - $minZ = $blockZ; - $maxZ = $blockZ + 15; - $tiles = $this->server->query("SELECT ID, x, y, z FROM tiles WHERE level = '{$this->level->getName()}' AND x >= $minX AND x <= $maxX AND z >= $minZ AND z <= $maxZ;"); - $this->lastChunk = false; - 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){ - $tile->spawn($this); - } + $blockX >>= 4; + $blockZ >>= 4; + foreach($this->level->tileEntityListPositioned["$blockX $blockZ"] ?? [] as $tile){ + if($tile instanceof Tile){ + $tile->spawn($this); } } + $this->lastChunk = false; } public function getNextChunk($world){ @@ -3275,7 +3263,6 @@ class Player{ if($d["id"] !== TILE_SIGN){ $t->spawn($this); }else{ - $t->setText($d["Text1"], $d["Text2"], $d["Text3"], $d["Text4"]); } } diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index a5257112..276f908b 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -200,7 +200,6 @@ class PocketMinecraftServer{ $this->query("PRAGMA secure_delete = OFF;"); $this->query("CREATE TABLE players (CID INTEGER PRIMARY KEY, EID NUMERIC, ip TEXT, port NUMERIC, name TEXT UNIQUE COLLATE NOCASE);"); $this->query("CREATE TABLE entities (EID INTEGER PRIMARY KEY, level TEXT, type NUMERIC, class NUMERIC, hasUpdate NUMERIC, name TEXT, x NUMERIC, y NUMERIC, z NUMERIC, yaw NUMERIC, pitch NUMERIC, health NUMERIC);"); - $this->query("CREATE TABLE tiles (ID INTEGER PRIMARY KEY, level TEXT, class TEXT, x NUMERIC, y NUMERIC, z NUMERIC, spawnable NUMERIC);"); $this->query("CREATE TABLE actions (ID INTEGER PRIMARY KEY, interval NUMERIC, last NUMERIC, code TEXT, repeat NUMERIC);"); $this->query("CREATE TABLE handlers (ID INTEGER PRIMARY KEY, name TEXT, priority NUMERIC);"); $this->query("CREATE TABLE blockUpdates (level TEXT, x INTEGER, y INTEGER, z INTEGER, type INTEGER, delay NUMERIC);"); diff --git a/src/constants/EntityIDs.php b/src/constants/EntityIDs.php index 4e6b0293..e578fe2e 100644 --- a/src/constants/EntityIDs.php +++ b/src/constants/EntityIDs.php @@ -36,6 +36,7 @@ define("TILE_CHEST", "Chest"); define("CHEST_SLOTS", 27); define("TILE_FURNACE", "Furnace"); define("FURNACE_SLOTS", 3); +define("TILE_NETHER_REACTOR", "NetherReactor"); const CORRECT_ENTITY_CLASSES = [ ENTITY_PLAYER => true, diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 760dce0c..4f9044f3 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -467,7 +467,7 @@ class PMFLevel extends PMF{ if($compatver != self::TYPE_OLD){ $ver = ord(gzread($chunk, 1)); if($ver != self::CHUNK_VERSION){ - ConsoleAPI::error("Failed to load chunk $X $Z: chunk version ($ver) doesnt match current (".self::CHUNK_VERSION.")"); + ConsoleAPI::error("Failed to load chunk $X $Z: chunk version ($ver) doesn't match current (".self::CHUNK_VERSION.")"); @gzclose($chunk); return false; } diff --git a/src/recipes/SmeltingData.php b/src/recipes/SmeltingData.php index 1dd447d3..754aac8d 100644 --- a/src/recipes/SmeltingData.php +++ b/src/recipes/SmeltingData.php @@ -8,6 +8,7 @@ class SmeltingData{ TRUNK => [COAL, 1], //Charcoal GOLD_ORE => [GOLD_INGOT, 0], IRON_ORE => [IRON_INGOT, 0], + DIAMOND_ORE => [DIAMOND, 0], NETHERRACK => [NETHER_BRICK, 0], RAW_PORKCHOP => [COOKED_PORKCHOP, 0], CLAY => [BRICK, 0], diff --git a/src/world/Level.php b/src/world/Level.php index fd31639d..ebb54d0a 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -15,8 +15,19 @@ class Level{ * @var Entity[] */ public $entityList; - + /** + * An array with all of the tileentities in this world + * @var Tile[] + */ + public $tileEntityList; + /** + * @var Entity[] + */ public $entityListPositioned = []; + /** + * @var Tile[] + */ + public $tileEntityListPositioned = []; public $entitiesInLove = []; /** @@ -59,6 +70,7 @@ class Level{ $this->level = $level; $this->level->level = $this; $this->entityList = []; + $this->tileEntityList = []; $this->entities = $entities; $this->tiles = $tiles; $this->blockUpdates = $blockUpdates; diff --git a/src/world/Tile.php b/src/world/Tile.php deleted file mode 100644 index 261c657f..00000000 --- a/src/world/Tile.php +++ /dev/null @@ -1,480 +0,0 @@ -server = ServerAPI::request(); - $this->level = $level; - $this->normal = true; - $this->class = $class; - $this->data = $data; - $this->closed = false; - if($class === false){ - $this->closed = true; - } - $this->name = ""; - $this->lastUpdate = microtime(true); - $this->scheduledUpdate = false; - $this->id = (int) $id; - $this->x = (int) $x; - $this->y = (int) $y; - $this->z = (int) $z; - $this->server->query("INSERT OR REPLACE INTO tiles (ID, level, class, x, y, z) VALUES (" . $this->id . ", '" . $this->level->getName() . "', '" . $this->class . "', " . $this->x . ", " . $this->y . ", " . $this->z . ");"); - switch($this->class){ - case TILE_CHEST: - case TILE_SIGN: - $this->server->query("UPDATE tiles SET spawnable = 1 WHERE ID = " . $this->id . ";"); - break; - case TILE_FURNACE: - if(!isset($this->data["BurnTime"]) or $this->data["BurnTime"] < 0){ - $this->data["BurnTime"] = 0; - } - if(!isset($this->data["CookTime"]) or $this->data["CookTime"] < 0 or ($this->data["BurnTime"] === 0 and $this->data["CookTime"] > 0)){ - $this->data["CookTime"] = 0; - } - if(!isset($this->data["MaxTime"])){ - $this->data["MaxTime"] = $this->data["BurnTime"]; - $this->data["BurnTicks"] = 0; - } - if($this->data["BurnTime"] > 0){ - $this->update(); - } - break; - } - } - - public function update(){ - if($this->closed === true){ - return false; - } - - if($this->class === TILE_FURNACE){ - $fuel = $this->getSlot(1); - $raw = $this->getSlot(0); - $product = $this->getSlot(2); - $smelt = $raw->getSmeltItem(); - $canSmelt = ($smelt !== false and $raw->count > 0 and (($product->getID() === $smelt->getID() and $product->getMetadata() === $smelt->getMetadata() and $product->count < $product->getMaxStackSize()) or $product->getID() === AIR)); - if($this->data["BurnTime"] <= 0 and $canSmelt and $fuel->getFuelTime() !== false and $fuel->count > 0){ - $this->lastUpdate = microtime(true); - $this->data["MaxTime"] = $this->data["BurnTime"] = floor($fuel->getFuelTime() * 20); - $this->data["BurnTicks"] = 0; - --$fuel->count; - if($fuel->count === 0){ - $fuel = BlockAPI::getItem(AIR, 0, 0); - } - $this->setSlot(1, $fuel, false); - $current = $this->level->getBlock($this); - if($current->getID() === FURNACE){ - $this->level->setBlock($this, BlockAPI::get(BURNING_FURNACE, $current->getMetadata()), true, false, true); - } - } - if($this->data["BurnTime"] > 0){ - $ticks = (microtime(true) - $this->lastUpdate) * 20; - $this->data["BurnTime"] -= $ticks; - $this->data["BurnTicks"] = ceil(($this->data["BurnTime"] / $this->data["MaxTime"]) * 200); - if($smelt !== false and $canSmelt){ - $this->data["CookTime"] += $ticks; - if($this->data["CookTime"] >= 200){ //10 seconds - $product = BlockAPI::getItem($smelt->getID(), $smelt->getMetadata(), $product->count + 1); - $this->setSlot(2, $product, false); - --$raw->count; - if($raw->count === 0){ - $raw = BlockAPI::getItem(AIR, 0, 0); - } - $this->setSlot(0, $raw, false); - $this->data["CookTime"] -= 200; - } - }elseif($this->data["BurnTime"] <= 0){ - $this->data["BurnTime"] = 0; - $this->data["CookTime"] = 0; - $this->data["BurnTicks"] = 0; - }else{ - $this->data["CookTime"] = 0; - } - - $this->server->schedule(2, [$this, "update"]); - $this->scheduledUpdate = true; - }else{ - $current = $this->level->getBlock($this); - if($current->getID() === BURNING_FURNACE){ - $this->level->setBlock($this, BlockAPI::get(FURNACE, $current->getMetadata()), true, false, true); - } - $this->data["CookTime"] = 0; - $this->data["BurnTime"] = 0; - $this->data["BurnTicks"] = 0; - $this->scheduledUpdate = false; - } - } - $this->server->handle("tile.update", $this); - $this->lastUpdate = microtime(true); - } - - public function getSlot($s){ - $i = $this->getSlotIndex($s); - if($i === false or $i < 0){ - return BlockAPI::getItem(AIR, 0, 0); - }else{ - return BlockAPI::getItem($this->data["Items"][$i]["id"], $this->data["Items"][$i]["Damage"], $this->data["Items"][$i]["Count"]); - } - } - - public function getSlotIndex($s){ - if($this->class !== TILE_CHEST and $this->class !== TILE_FURNACE){ - return false; - } - foreach($this->data["Items"] as $i => $slot){ - if($slot["Slot"] === $s){ - return $i; - } - } - return -1; - } - - public function setSlot($s, Item $item, $update = true, $offset = 0){ - $i = $this->getSlotIndex($s); - $d = [ - "Count" => $item->count, - "Slot" => $s, - "id" => $item->getID(), - "Damage" => $item->getMetadata(), - ]; - if($i === false){ - return false; - }elseif($item->getID() === AIR or $item->count <= 0){ - if($i >= 0){ - unset($this->data["Items"][$i]); - } - }elseif($i < 0){ - $this->data["Items"][] = $d; - }else{ - $this->data["Items"][$i] = $d; - } - $this->server->api->dhandle("tile.container.slot", [ - "tile" => $this, - "slot" => $s, - "offset" => $offset, - "slotdata" => $item, - ]); - - if($update === true and $this->scheduledUpdate === false){ - $this->update(); - } - return true; - } - - public function pairWith(Tile $tile){ - if($this->isPaired() or $tile->isPaired()){ - return false; - } - - $this->data["pairx"] = $tile->x; - $this->data["pairz"] = $tile->z; - - $tile->data["pairx"] = $this->x; - $tile->data["pairz"] = $this->z; - - $this->server->api->tile->spawnToAll($this); - $this->server->api->tile->spawnToAll($tile); - $this->server->handle("tile.update", $this); - $this->server->handle("tile.update", $tile); - } - - public function isPaired(){ - if($this->class !== TILE_CHEST){ - return false; - } - if(!isset($this->data["pairx"]) or !isset($this->data["pairz"])){ - return false; - } - return true; - } - - public function unpair(){ - if(!$this->isPaired()){ - return false; - } - - $tile = $this->getPair(); - unset($this->data["pairx"], $this->data["pairz"], $tile->data["pairx"], $tile->data["pairz"]); - - $this->server->api->tile->spawnToAll($this); - $this->server->handle("tile.update", $this); - if($tile instanceof Tile){ - $this->server->api->tile->spawnToAll($tile); - $this->server->handle("tile.update", $tile); - } - } - - public function getPair(){ - if($this->isPaired()){ - return $this->server->api->tile->get(new Position((int) $this->data["pairx"], $this->y, (int) $this->data["pairz"], $this->level)); - } - return false; - } - - public function openInventory(Player $player){ - if($this->class === TILE_CHEST){ - $player->windowCnt++; - $player->windowCnt = $id = max(2, $player->windowCnt % 99); - if(($pair = $this->getPair()) !== false){ - if(($pair->x + ($pair->z << 13)) > ($this->x + ($this->z << 13))){ //Order them correctly - $player->windows[$id] = [ - $pair, - $this - ]; - }else{ - $player->windows[$id] = [ - $this, - $pair - ]; - } - }else{ - $player->windows[$id] = $this; - } - - $pk = new ContainerOpenPacket; - $pk->windowid = $id; - $pk->type = WINDOW_CHEST; - $pk->slots = is_array($player->windows[$id]) ? CHEST_SLOTS << 1 : CHEST_SLOTS; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $player->blockQueueDataPacket($pk); - $slots = []; - - if(is_array($player->windows[$id])){ - $all = $this->server->api->player->getAll($this->level); - foreach($player->windows[$id] as $ob){ - $pk = new TileEventPacket; - $pk->x = $ob->x; - $pk->y = $ob->y; - $pk->z = $ob->z; - $pk->case1 = 1; - $pk->case2 = 2; - foreach($this->level->players as $pl){ - $pl->blockQueueDataPacket(clone $pk); - } - for($s = 0; $s < CHEST_SLOTS; ++$s){ - $slot = $ob->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ - $slots[] = $slot; - }else{ - $slots[] = BlockAPI::getItem(AIR, 0, 0); - } - } - } - }else{ - $pk = new TileEventPacket; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->case1 = 1; - $pk->case2 = 2; - foreach($this->level->players as $pl){ - $pl->blockQueueDataPacket(clone $pk); - } - - for($s = 0; $s < CHEST_SLOTS; ++$s){ - $slot = $this->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ - $slots[] = $slot; - }else{ - $slots[] = BlockAPI::getItem(AIR, 0, 0); - } - } - } - - $pk = new ContainerSetContentPacket; - $pk->windowid = $id; - $pk->slots = $slots; - $player->blockQueueDataPacket($pk); - return true; - }elseif($this->class === TILE_FURNACE){ - $player->windowCnt++; - $player->windowCnt = $id = max(2, $player->windowCnt % 99); - $player->windows[$id] = $this; - - $pk = new ContainerOpenPacket; - $pk->windowid = $id; - $pk->type = WINDOW_FURNACE; - $pk->slots = FURNACE_SLOTS; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $player->blockQueueDataPacket($pk); - - $slots = []; - for($s = 0; $s < FURNACE_SLOTS; ++$s){ - $slot = $this->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ - $slots[] = $slot; - }else{ - $slots[] = BlockAPI::getItem(AIR, 0, 0); - } - } - $pk = new ContainerSetContentPacket; - $pk->windowid = $id; - $pk->slots = $slots; - $player->blockQueueDataPacket($pk); - return true; - } - } - - public function spawn($player){ - if($this->closed){ - return false; - } - if(!($player instanceof Player)){ - $player = $this->server->api->player->get($player); - } - switch($this->class){ - case TILE_CHEST: - $nbt = new NBT(); - $nbt->write(chr(NBT::TAG_COMPOUND) . "\x00\x00"); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("x"); - $nbt->writeTAG_Int((int) $this->x); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("y"); - $nbt->writeTAG_Int((int) $this->y); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("z"); - $nbt->writeTAG_Int((int) $this->z); - - if($this->isPaired()){ - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("pairx"); - $nbt->writeTAG_Int((int) $this->data["pairx"]); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("pairz"); - $nbt->writeTAG_Int((int) $this->data["pairz"]); - } - - $nbt->write(chr(NBT::TAG_END)); - - $pk = new EntityDataPacket; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->namedtag = $nbt->binary; - $player->blockQueueDataPacket($pk); - break; - case TILE_SIGN: - $nbt = new NBT(); - $nbt->write(chr(NBT::TAG_COMPOUND) . "\x00\x00"); - - $nbt->write(chr(NBT::TAG_STRING)); - $nbt->writeTAG_String("Text1"); - $nbt->writeTAG_String(mb_substr($this->data["Text1"], 0, 15)); - - $nbt->write(chr(NBT::TAG_STRING)); - $nbt->writeTAG_String("Text2"); - $nbt->writeTAG_String(mb_substr($this->data["Text2"], 0, 15)); - - $nbt->write(chr(NBT::TAG_STRING)); - $nbt->writeTAG_String("Text3"); - $nbt->writeTAG_String(mb_substr($this->data["Text3"], 0, 15)); - - $nbt->write(chr(NBT::TAG_STRING)); - $nbt->writeTAG_String("Text4"); - $nbt->writeTAG_String(mb_substr($this->data["Text4"], 0, 15)); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("x"); - $nbt->writeTAG_Int((int) $this->x); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("y"); - $nbt->writeTAG_Int((int) $this->y); - - $nbt->write(chr(NBT::TAG_INT)); - $nbt->writeTAG_String("z"); - $nbt->writeTAG_Int((int) $this->z); - - $nbt->write(chr(NBT::TAG_END)); - - $pk = new EntityDataPacket; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->namedtag = $nbt->binary; - $player->blockQueueDataPacket($pk); - break; - } - } - - public function setText($line1 = "", $line2 = "", $line3 = "", $line4 = ""){ - if($this->class !== TILE_SIGN){ - return false; - } - if(Level::$disableEmojisOnSigns){ - $line1 = Utils::replaceEmoji($line1); - $line2 = Utils::replaceEmoji($line2); - $line3 = Utils::replaceEmoji($line3); - $line4 = Utils::replaceEmoji($line4); - - } - $this->data["Text1"] = $line1; - $this->data["Text2"] = $line2; - $this->data["Text3"] = $line3; - $this->data["Text4"] = $line4; - $this->server->api->tile->spawnToAll($this); - $this->server->handle("tile.update", $this); - return true; - } - - public function getText(){ - return [ - $this->data["Text1"], - $this->data["Text2"], - $this->data["Text3"], - $this->data["Text4"] - ]; - } - - public function __destruct(){ - $this->close(); - } - - public function close(){ - if($this->closed === false){ - $this->closed = true; - $this->server->api->tile->remove($this->id); - } - } - - public function getName(){ - return $this->name; - } - - - public function setPosition(Vector3 $pos){ - if($pos instanceof Position){ - $this->level = $pos->level; - $this->server->query("UPDATE tiles SET level = '" . $this->level->getName() . "' WHERE ID = " . $this->id . ";"); - } - $this->x = (int) $pos->x; - $this->y = (int) $pos->y; - $this->z = (int) $pos->z; - $this->server->query("UPDATE tiles SET x = " . $this->x . ", y = " . $this->y . ", z = " . $this->z . " WHERE ID = " . $this->id . ";"); - } -} diff --git a/src/world/tileentity/ChestTileEntity.php b/src/world/tileentity/ChestTileEntity.php new file mode 100644 index 00000000..8fe07b11 --- /dev/null +++ b/src/world/tileentity/ChestTileEntity.php @@ -0,0 +1,119 @@ +write(chr(NBT::TAG_COMPOUND) . "\x00\x00"); + + $nbt->write(chr(NBT::TAG_INT)); + $nbt->writeTAG_String("x"); + $nbt->writeTAG_Int((int) $this->x); + + $nbt->write(chr(NBT::TAG_INT)); + $nbt->writeTAG_String("y"); + $nbt->writeTAG_Int((int) $this->y); + + $nbt->write(chr(NBT::TAG_INT)); + $nbt->writeTAG_String("z"); + $nbt->writeTAG_Int((int) $this->z); + + if($this->isPaired()){ + $nbt->write(chr(NBT::TAG_INT)); + $nbt->writeTAG_String("pairx"); + $nbt->writeTAG_Int((int) $this->data["pairx"]); + + $nbt->write(chr(NBT::TAG_INT)); + $nbt->writeTAG_String("pairz"); + $nbt->writeTAG_Int((int) $this->data["pairz"]); + } + + $nbt->write(chr(NBT::TAG_END)); + + $pk = new EntityDataPacket; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->namedtag = $nbt->binary; + return $pk; + } + + public function openInventory(Player $player){ + $player->windowCnt++; + $player->windowCnt = $id = max(2, $player->windowCnt % 99); + if(($pair = $this->getPair()) !== false){ + if(($pair->x + ($pair->z << 13)) > ($this->x + ($this->z << 13))){ //Order them correctly + $player->windows[$id] = [ + $pair, + $this + ]; + }else{ + $player->windows[$id] = [ + $this, + $pair + ]; + } + }else{ + $player->windows[$id] = $this; + } + + $pk = new ContainerOpenPacket; + $pk->windowid = $id; + $pk->type = WINDOW_CHEST; + $pk->slots = is_array($player->windows[$id]) ? CHEST_SLOTS << 1 : CHEST_SLOTS; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $player->blockQueueDataPacket($pk); + $slots = []; + + if(is_array($player->windows[$id])){ + $all = $this->server->api->player->getAll($this->level); + foreach($player->windows[$id] as $ob){ + $pk = new TileEventPacket; + $pk->x = $ob->x; + $pk->y = $ob->y; + $pk->z = $ob->z; + $pk->case1 = 1; + $pk->case2 = 2; + foreach($this->level->players as $pl){ + $pl->blockQueueDataPacket(clone $pk); + } + for($s = 0; $s < CHEST_SLOTS; ++$s){ + $slot = $ob->getSlot($s); + if($slot->getID() > AIR and $slot->count > 0){ + $slots[] = $slot; + }else{ + $slots[] = BlockAPI::getItem(AIR, 0, 0); + } + } + } + }else{ + $pk = new TileEventPacket; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->case1 = 1; + $pk->case2 = 2; + foreach($this->level->players as $pl){ + $pl->blockQueueDataPacket(clone $pk); + } + + for($s = 0; $s < CHEST_SLOTS; ++$s){ + $slot = $this->getSlot($s); + if($slot->getID() > AIR and $slot->count > 0){ + $slots[] = $slot; + }else{ + $slots[] = BlockAPI::getItem(AIR, 0, 0); + } + } + } + + $pk = new ContainerSetContentPacket; + $pk->windowid = $id; + $pk->slots = $slots; + $player->blockQueueDataPacket($pk); + return true; + } +} + diff --git a/src/world/tileentity/FurnaceTileEntity.php b/src/world/tileentity/FurnaceTileEntity.php new file mode 100644 index 00000000..f9eb0272 --- /dev/null +++ b/src/world/tileentity/FurnaceTileEntity.php @@ -0,0 +1,114 @@ +data["BurnTime"]) or $this->data["BurnTime"] < 0){ + $this->data["BurnTime"] = 0; + } + if(!isset($this->data["CookTime"]) or $this->data["CookTime"] < 0 or ($this->data["BurnTime"] === 0 and $this->data["CookTime"] > 0)){ + $this->data["CookTime"] = 0; + } + if(!isset($this->data["MaxTime"])){ + $this->data["MaxTime"] = $this->data["BurnTime"]; + $this->data["BurnTicks"] = 0; + } + if($this->data["BurnTime"] > 0){ + $this->update(); + } + } + + public function openInventory(Player $player){ + $player->windowCnt++; + $player->windowCnt = $id = max(2, $player->windowCnt % 99); + $player->windows[$id] = $this; + + $pk = new ContainerOpenPacket; + $pk->windowid = $id; + $pk->type = WINDOW_FURNACE; + $pk->slots = FURNACE_SLOTS; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $player->blockQueueDataPacket($pk); + + $slots = []; + for($s = 0; $s < FURNACE_SLOTS; ++$s){ + $slot = $this->getSlot($s); + if($slot->getID() > AIR and $slot->count > 0){ + $slots[] = $slot; + }else{ + $slots[] = BlockAPI::getItem(AIR, 0, 0); + } + } + $pk = new ContainerSetContentPacket; + $pk->windowid = $id; + $pk->slots = $slots; + $player->blockQueueDataPacket($pk); + return true; + } + + public function update(){ + if($this->closed === true) return false; + + $fuel = $this->getSlot(1); + $raw = $this->getSlot(0); + $product = $this->getSlot(2); + $smelt = $raw->getSmeltItem(); + $canSmelt = ($smelt !== false and $raw->count > 0 and (($product->getID() === $smelt->getID() and $product->getMetadata() === $smelt->getMetadata() and $product->count < $product->getMaxStackSize()) or $product->getID() === AIR)); + if($this->data["BurnTime"] <= 0 and $canSmelt and $fuel->getFuelTime() !== false and $fuel->count > 0){ + $this->lastUpdate = microtime(true); + $this->data["MaxTime"] = $this->data["BurnTime"] = floor($fuel->getFuelTime() * 20); + $this->data["BurnTicks"] = 0; + --$fuel->count; + if($fuel->count === 0){ + $fuel = BlockAPI::getItem(AIR, 0, 0); + } + $this->setSlot(1, $fuel, false); + $current = $this->level->getBlock($this); + if($current->getID() === FURNACE){ + $this->level->setBlock($this, BlockAPI::get(BURNING_FURNACE, $current->getMetadata()), true, false, true); + } + } + if($this->data["BurnTime"] > 0){ + $ticks = (microtime(true) - $this->lastUpdate) * 20; + $this->data["BurnTime"] -= $ticks; + $this->data["BurnTicks"] = ceil(($this->data["BurnTime"] / $this->data["MaxTime"]) * 200); + if($smelt !== false and $canSmelt){ + $this->data["CookTime"] += $ticks; + if($this->data["CookTime"] >= 200){ //10 seconds + $product = BlockAPI::getItem($smelt->getID(), $smelt->getMetadata(), $product->count + 1); + $this->setSlot(2, $product, false); + --$raw->count; + if($raw->count === 0){ + $raw = BlockAPI::getItem(AIR, 0, 0); + } + $this->setSlot(0, $raw, false); + $this->data["CookTime"] -= 200; + } + }elseif($this->data["BurnTime"] <= 0){ + $this->data["BurnTime"] = 0; + $this->data["CookTime"] = 0; + $this->data["BurnTicks"] = 0; + }else{ + $this->data["CookTime"] = 0; + } + + $this->server->schedule(2, [$this, "update"]); + $this->scheduledUpdate = true; + }else{ + $current = $this->level->getBlock($this); + if($current->getID() === BURNING_FURNACE){ + $this->level->setBlock($this, BlockAPI::get(FURNACE, $current->getMetadata()), true, false, true); + } + $this->data["CookTime"] = 0; + $this->data["BurnTime"] = 0; + $this->data["BurnTicks"] = 0; + $this->scheduledUpdate = false; + } + parent::update(); + } +} + diff --git a/src/world/tileentity/SignTileEntity.php b/src/world/tileentity/SignTileEntity.php new file mode 100644 index 00000000..1a548bcc --- /dev/null +++ b/src/world/tileentity/SignTileEntity.php @@ -0,0 +1,47 @@ +write(chr(NBT::TAG_COMPOUND) . "\x00\x00"); + + $nbt->write(chr(NBT::TAG_STRING)); + $nbt->writeTAG_String("Text1"); + $nbt->writeTAG_String(mb_substr($this->data["Text1"], 0, 15)); + + $nbt->write(chr(NBT::TAG_STRING)); + $nbt->writeTAG_String("Text2"); + $nbt->writeTAG_String(mb_substr($this->data["Text2"], 0, 15)); + + $nbt->write(chr(NBT::TAG_STRING)); + $nbt->writeTAG_String("Text3"); + $nbt->writeTAG_String(mb_substr($this->data["Text3"], 0, 15)); + + $nbt->write(chr(NBT::TAG_STRING)); + $nbt->writeTAG_String("Text4"); + $nbt->writeTAG_String(mb_substr($this->data["Text4"], 0, 15)); + + $nbt->write(chr(NBT::TAG_INT)); + $nbt->writeTAG_String("x"); + $nbt->writeTAG_Int((int) $this->x); + + $nbt->write(chr(NBT::TAG_INT)); + $nbt->writeTAG_String("y"); + $nbt->writeTAG_Int((int) $this->y); + + $nbt->write(chr(NBT::TAG_INT)); + $nbt->writeTAG_String("z"); + $nbt->writeTAG_Int((int) $this->z); + + $nbt->write(chr(NBT::TAG_END)); + + $pk = new EntityDataPacket; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->namedtag = $nbt->binary; + return $pk; + } +} + diff --git a/src/world/tileentity/Tile.php b/src/world/tileentity/Tile.php new file mode 100644 index 00000000..13457cdd --- /dev/null +++ b/src/world/tileentity/Tile.php @@ -0,0 +1,260 @@ + "SignTileEntity", + TILE_CHEST => "ChestTileEntity", + TILE_FURNACE => "FurnaceTileEntity" + //TODO nether reactor tileentity + ]; + + /** + * Numeric tileentity id. Should not be modified + * @var int + */ + public $id; + public $class; + + public $name; + public $normal; + public $data; + public $attach; + public $metadata; + public $closed; + + public $scheduledUpdate, $lastUpdate; + + protected $server; + public function __construct(Level $level, $id, $class, $x, $y, $z, $data = []){ + $this->server = ServerAPI::request(); + $this->level = $level; + $this->normal = true; + $this->class = $class; + $this->data = $data; + $this->closed = false; + if($class === false){ + $this->closed = true; + } + $this->name = ""; + $this->lastUpdate = microtime(true); + $this->scheduledUpdate = false; + $this->id = (int) $id; + $this->x = (int) $x; + $this->y = (int) $y; + $this->z = (int) $z; + } + + /** + * @return array + */ + public function getSaveData(){ + return [ + "id" => $this->id, + "x" => $this->x, + "y" => $this->y, + "z" => $this->z + ]; + } + + public function update(){ + if($this->closed === true) return false; + + $this->server->handle("tile.update", $this); + $this->lastUpdate = microtime(true); + } + + public function getSlot($s){ + $i = $this->getSlotIndex($s); + if($i === false or $i < 0){ + return BlockAPI::getItem(AIR, 0, 0); + }else{ + return BlockAPI::getItem($this->data["Items"][$i]["id"], $this->data["Items"][$i]["Damage"], $this->data["Items"][$i]["Count"]); + } + } + + public function getSlotIndex($s){ + if($this->class !== TILE_CHEST and $this->class !== TILE_FURNACE){ + return false; + } + foreach($this->data["Items"] as $i => $slot){ + if($slot["Slot"] === $s){ + return $i; + } + } + return -1; + } + + public function setSlot($s, Item $item, $update = true, $offset = 0){ + $i = $this->getSlotIndex($s); + $d = [ + "Count" => $item->count, + "Slot" => $s, + "id" => $item->getID(), + "Damage" => $item->getMetadata(), + ]; + if($i === false){ + return false; + }elseif($item->getID() === AIR or $item->count <= 0){ + if($i >= 0){ + unset($this->data["Items"][$i]); + } + }elseif($i < 0){ + $this->data["Items"][] = $d; + }else{ + $this->data["Items"][$i] = $d; + } + $this->server->api->dhandle("tile.container.slot", [ + "tile" => $this, + "slot" => $s, + "offset" => $offset, + "slotdata" => $item, + ]); + + if($update === true and $this->scheduledUpdate === false){ + $this->update(); + } + return true; + } + + public function pairWith(Tile $tile){ + if($this->isPaired() or $tile->isPaired()){ + return false; + } + + $this->data["pairx"] = $tile->x; + $this->data["pairz"] = $tile->z; + + $tile->data["pairx"] = $this->x; + $tile->data["pairz"] = $this->z; + + $this->server->api->tile->spawnToAll($this); + $this->server->api->tile->spawnToAll($tile); + $this->server->handle("tile.update", $this); + $this->server->handle("tile.update", $tile); + } + + public function isPaired(){ + if($this->class !== TILE_CHEST){ + return false; + } + if(!isset($this->data["pairx"]) or !isset($this->data["pairz"])){ + return false; + } + return true; + } + + public function unpair(){ + if(!$this->isPaired()){ + return false; + } + + $tile = $this->getPair(); + unset($this->data["pairx"], $this->data["pairz"], $tile->data["pairx"], $tile->data["pairz"]); + + $this->server->api->tile->spawnToAll($this); + $this->server->handle("tile.update", $this); + if($tile instanceof Tile){ + $this->server->api->tile->spawnToAll($tile); + $this->server->handle("tile.update", $tile); + } + } + + public function getPair(){ + if($this->isPaired()){ + return $this->server->api->tile->get(new Position((int) $this->data["pairx"], $this->y, (int) $this->data["pairz"], $this->level)); + } + return false; + } + + public function openInventory(Player $player){} + public function getSpawnPacket(){ + return null; + } + + public function spawn($player){ + if($this->closed){ + return false; + } + if(!($player instanceof Player)) $player = $this->server->api->player->get($player); + $packet = $this->getSpawnPacket(); + if($packet instanceof RakNetDataPacket){ + $player->blockQueueDataPacket($packet); + } + } + + public function setText($line1 = "", $line2 = "", $line3 = "", $line4 = ""){ + if($this->class !== TILE_SIGN){ + return false; + } + if(Level::$disableEmojisOnSigns){ + $line1 = Utils::replaceEmoji($line1); + $line2 = Utils::replaceEmoji($line2); + $line3 = Utils::replaceEmoji($line3); + $line4 = Utils::replaceEmoji($line4); + + } + $this->data["Text1"] = $line1; + $this->data["Text2"] = $line2; + $this->data["Text3"] = $line3; + $this->data["Text4"] = $line4; + $this->server->api->tile->spawnToAll($this); + $this->server->handle("tile.update", $this); + return true; + } + + public function getText(){ + return [ + $this->data["Text1"], + $this->data["Text2"], + $this->data["Text3"], + $this->data["Text4"] + ]; + } + + public function __destruct(){ + $this->close(); + } + + public function close(){ + if($this->closed === false){ + $this->closed = true; + $this->server->api->tile->remove($this->id); + } + } + + public function getName(){ + return $this->name; + } + + + /** + * @deprecated Should not be used + * @param Vector3|Position $pos + */ + public function setPosition(Vector3 $pos){ + if($pos instanceof Position && $pos->level != $this->level){ + $cX = floor($this->x) >> 4; + $cZ = floor($this->z) >> 4; + unset($this->level->tileEntityList[$this->id]); + unset($this->level->tileEntityListPositioned["$cX $cZ"][$this->id]); + $this->level = $pos->level; + $this->level->tileEntityList[$this->id] = $this; + }else{ + $cX = floor($this->x) >> 4; + $cZ = floor($this->z) >> 4; + unset($this->level->tileEntityListPositioned["$cX $cZ"][$this->id]); + } + + $nCX = floor($pos->x) >> 4; + $nCZ = floor($pos->z) >> 4; + $this->level->tileEntityListPositioned["$nCX $nCZ"][$this->id] = $this; + + $this->x = (int) $pos->x; + $this->y = (int) $pos->y; + $this->z = (int) $pos->z; + } +}