From fae7fae8eca50da5f8e151245f19c5a88f12ec1f Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Thu, 20 Dec 2012 13:50:20 +0100 Subject: [PATCH] Save placed blocks --- README.md | 2 +- TODO | 1 - classes/API/LevelAPI.php | 26 ++++++++++++++++++++++++++ classes/CustomPacketHandler.class.php | 18 ++++++++++++++++++ classes/Session.class.php | 6 ++++++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 13cede87c..d94748539 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ Current features of the server: ------------------------------- * Players can connect and move around the world (and see each other) * Support for reading/sending chunks! -* Map saving! +* Map saving! Place & remove blocks * Multiple worlds and importing! * PvP, life regeneration and death cause! * Extensible API! diff --git a/TODO b/TODO index 600d9a458..d5123fbe7 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,3 @@ -- Save placed blocks and relay them to other players. - Fix spawn position resetting - Mob spawning, item pick up - Fix metadata orientation diff --git a/classes/API/LevelAPI.php b/classes/API/LevelAPI.php index f9f32a78d..14db516e8 100644 --- a/classes/API/LevelAPI.php +++ b/classes/API/LevelAPI.php @@ -37,10 +37,36 @@ class LevelAPI{ public function init(){ $this->server->addHandler("onBlockBreak", array($this, "handle")); + $this->server->addHandler("onBlockPlace", array($this, "handle")); } public function handle($data, $event){ switch($event){ + case "onBlockPlace": + switch($data["face"]){ + case 0: + --$data["y"]; + break; + case 1: + ++$data["y"]; + break; + case 2: + --$data["z"]; + break; + case 3: + ++$data["z"]; + break; + case 4: + --$data["x"]; + break; + case 5: + ++$data["x"]; + break; + } + $block = $this->getBlock($data["x"], $data["y"], $data["z"]); + console("[DEBUG] EID ".$data["eid"]." placed ".$data["block"].":".$data["meta"]." into ".$block[0].":".$block[1]." at X ".$data["x"]." Y ".$data["y"]." Z ".$data["z"], true, true, 2); + $this->setBlock($data["x"], $data["y"], $data["z"], $data["block"], $data["meta"]); + break; case "onBlockBreak": $block = $this->getBlock($data["x"], $data["y"], $data["z"]); console("[DEBUG] EID ".$data["eid"]." broke block ".$block[0].":".$block[1]." at X ".$data["x"]." Y ".$data["y"]." Z ".$data["z"], true, true, 2); diff --git a/classes/CustomPacketHandler.class.php b/classes/CustomPacketHandler.class.php index 3a9e04bf3..ac9cfb8d8 100644 --- a/classes/CustomPacketHandler.class.php +++ b/classes/CustomPacketHandler.class.php @@ -415,6 +415,24 @@ class CustomPacketHandler{ $this->raw .= Utils::writeInt($this->data["target"]); } break; + case MC_USE_ITEM: + if($this->c === false){ + $this->data["x"] = Utils::readInt($this->get(4)); + $this->data["y"] = Utils::readInt($this->get(4)); + $this->data["z"] = Utils::readInt($this->get(4)); + $this->data["face"] = Utils::readInt($this->get(4)); + $this->data["block"] = Utils::readShort($this->get(2)); + $this->data["meta"] = Utils::readByte($this->get(1)); + $this->data["eid"] = Utils::readInt($this->get(4)); + $this->data["unknown2"] = Utils::readFloat($this->get(4)); + $this->data["unknown3"] = Utils::readFloat($this->get(4)); + $this->data["unknown4"] = Utils::readFloat($this->get(4)); + }else{ + $this->raw .= Utils::writeByte($this->data["action"]); + $this->raw .= Utils::writeInt($this->data["eid"]); + $this->raw .= Utils::writeInt($this->data["target"]); + } + break; case MC_SET_ENTITY_DATA: if($this->c === false){ $this->data["eid"] = Utils::readInt($this->get(4)); diff --git a/classes/Session.class.php b/classes/Session.class.php index f881f0e22..99e70c5d1 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -313,6 +313,12 @@ class Session{ '); console("[INTERNAL] Chunk X ".$data["x"]." Z ".$data["z"]." requested", true, true, 3); break; + case MC_USE_ITEM: + if($data["face"] >= 0 and $data["face"] <= 5 and $data["block"] !== 0){ + $data["eid"] = $this->eid; + $this->server->handle("onBlockPlace", $data); + } + break; case MC_PLACE_BLOCK: var_dump($data); break;