From 56665dfdb330eadfddff5b183ac95615864970ee Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Sun, 9 Dec 2012 12:32:33 +0100 Subject: [PATCH] PvP! --- README | 1 + classes/ChunkParser.class.php | 2 +- classes/CustomPacketHandler.class.php | 22 +++++++++++++++++- classes/PocketMinecraftServer.class.php | 4 ++++ classes/Session.class.php | 30 +++++++++++++++++++++++++ common/default.properties | 1 + pstruct/5.php | 1 + pstruct/dataName.php | 1 + server.php | 13 +++++++++++ 9 files changed, 73 insertions(+), 2 deletions(-) diff --git a/README b/README index 850a901d..8ef0bc17 100644 --- a/README +++ b/README @@ -44,4 +44,5 @@ Current features of the server: - Survival & Creative - Awesome features in server list! - Players spawn and see each other moving! + - PvP and life regeneration! - Multiple worlds and importing! diff --git a/classes/ChunkParser.class.php b/classes/ChunkParser.class.php index f36bbdcc..644db75d 100644 --- a/classes/ChunkParser.class.php +++ b/classes/ChunkParser.class.php @@ -140,7 +140,7 @@ class ChunkParser{ $Z = $z >> 4; $aX = $x - ($X << 4); $aZ = $z - ($Z << 4); - $index = $aZ + ($aX << 4); + $index = $aX + ($aZ << 4); console("[DEBUG] $x $y $z | $X $Z $index", true, true, 2); var_dump($this->map[$X][$Z][0][$index]); $block = ord($this->map[$X][$Z][0][$index]{$y}); diff --git a/classes/CustomPacketHandler.class.php b/classes/CustomPacketHandler.class.php index 464f2ad5..6612cdd5 100644 --- a/classes/CustomPacketHandler.class.php +++ b/classes/CustomPacketHandler.class.php @@ -382,6 +382,17 @@ class CustomPacketHandler{ $this->raw .= Utils::writeShort($this->data["meta"]); } break; + case MC_INTERACT: + if($this->c === false){ + $this->data["action"] = Utils::readByte($this->get(1)); + $this->data["eid"] = Utils::readInt($this->get(4)); + $this->data["target"] = Utils::readInt($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)); @@ -392,13 +403,22 @@ class CustomPacketHandler{ )); } break; - case MC_SET_HEALTH: //SetHealth + case MC_SET_HEALTH: if($this->c === false){ $this->data["health"] = ord($this->get(1)); }else{ $this->raw .= chr($this->data["health"]); } break; + case MC_ANIMATE: + if($this->c === false){ + $this->data["action"] = Utils::readByte($this->get(1)); + $this->data["eid"] = Utils::readInt($this->get(4)); + }else{ + $this->raw .= Utils::writeByte($this->data["action"]); + $this->raw .= Utils::writeInt($this->data["eid"]); + } + break; case MC_RESPAWN: if($this->c === false){ $this->data["eid"] = Utils::readInt($this->get(4)); diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index e71f8613..86cfa7d0 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -42,6 +42,7 @@ class PocketMinecraftServer{ $this->mapName = false; $this->map = false; $this->level = false; + $this->difficulty = 1; $this->tileEntities = array(); $this->entities = array(); $this->custom = array(); @@ -77,6 +78,7 @@ class PocketMinecraftServer{ $this->event("onChat", "eventHandler", true); $this->action(100000, '$this->time += ceil($this->timePerSecond / 10);$this->trigger("onTimeChange", $this->time);'); + $this->action(5000000, '$this->trigger("onHealthRegeneration", 1);'); $this->action(1000000 * 60, '$this->reloadConfig();'); $this->action(1000000 * 60 * 10, '$this->custom = array();'); $this->action(1000000 * 80, '$list = ""; foreach($this->clients as $c){$list .= ", ".$c->username;}$this->chat(false, count($this->clients)."/".$this->maxClients." online: ".substr($list, 2));'); @@ -168,6 +170,8 @@ class PocketMinecraftServer{ console("[INFO] Time: ".$this->time); console("[INFO] Seed: ".$this->seed); console("[INFO] Gamemode: ".($this->gamemode === 0 ? "survival":"creative")); + $d = array(0 => "peaceful", 1 => "easy", 2 => "normal", 3 => "hard"); + console("[INFO] Difficulty: ".$d[$this->difficulty]); console("[INFO] Loading map..."); $this->map = new ChunkParser(); if(!$this->map->loadFile($this->mapDir."chunks.dat")){ diff --git a/classes/Session.class.php b/classes/Session.class.php index 311200d9..cb44dc12 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -110,6 +110,11 @@ class Session{ )); ++$this->counter[0]; break; + case "onHealthRegeneration": + if($this->server->difficulty < 2){ + $this->server->trigger("onHealthChange", array("eid" => $this->eid, "health" => min(20, $this->data["health"] + $data))); + } + break; case "onHealthChange": if($data["eid"] === $this->eid){ $this->send(0x84, array( @@ -171,6 +176,21 @@ class Session{ )); ++$this->counter[0]; break; + case "onAnimate": + if($data["eid"] === $this->eid){ + break; + } + $this->send(0x84, array( + $this->counter[0], + 0x00, + array( + "id" => MC_ANIMATE, + "eid" => $data["eid"], + "action" => $data["action"], + ), + )); + ++$this->counter[0]; + break; case "onChat": $this->send(0x84, array( $this->counter[0], @@ -261,6 +281,8 @@ class Session{ $this->evid[] = array("onEntityRemove", $this->server->event("onEntityRemove", array($this, "eventHandler"))); $this->evid[] = array("onEntityMove", $this->server->event("onEntityMove", array($this, "eventHandler"))); $this->evid[] = array("onHealthChange", $this->server->event("onHealthChange", array($this, "eventHandler"))); + $this->evid[] = array("onHealthRegeneration", $this->server->event("onHealthRegeneration", array($this, "eventHandler"))); + $this->evid[] = array("onAnimate", $this->server->event("onAnimate", array($this, "eventHandler"))); $this->send(0x84, array( $this->counter[0], 0x00, @@ -369,6 +391,14 @@ class Session{ )); ++$this->counter[0]; break; + case MC_INTERACT: + if($this->server->difficulty > 0 and isset($this->server->entities[$data["target"]]) and Utils::distance($this->entity->position, $this->server->entities[$data["target"]]->position) <= 8){ + $this->server->trigger("onHealthChange", array("eid" => $data["eid"], "health" => $this->server->entities[$data["eid"]]->getHealth() - $this->server->difficulty)); + } + break; + case MC_ANIMATE: + $this->server->trigger("onAnimate", array("eid" => $this->eid, "action" => $data["action"])); + break; case MC_RESPAWN: $this->server->trigger("onHealthChange", array("eid" => $this->eid, "health" => 20)); $this->entity->setPosition($data["x"], $data["y"], $data["z"], $data["x"], 0, 0); diff --git a/common/default.properties b/common/default.properties index 77e76863..569c6b4b 100644 --- a/common/default.properties +++ b/common/default.properties @@ -10,6 +10,7 @@ max-players=20 server-type=normal time-per-second=20 gamemode=0 +difficulty=1 seed=false level-name=false server-id=false diff --git a/pstruct/5.php b/pstruct/5.php index 773f2e18..f154db01 100644 --- a/pstruct/5.php +++ b/pstruct/5.php @@ -63,6 +63,7 @@ define("MC_CHUNK_DATA", 0x9e); define("MC_PLAYER_EQUIPMENT", 0x9f); +define("MC_INTERACT", 0xa0); define("MC_USE_ITEM", 0xa1); define("MC_PLAYER_ACTION", 0xa2); define("MC_SET_ENTITY_DATA", 0xa3); diff --git a/pstruct/dataName.php b/pstruct/dataName.php index fad066c0..32c57795 100644 --- a/pstruct/dataName.php +++ b/pstruct/dataName.php @@ -63,6 +63,7 @@ $dataName = array( MC_PLAYER_EQUIPMENT => "PlayerEquipment", + MC_INTERACT => "Interact", MC_USE_ITEM => "UseItem", MC_PLAYER_ACTION => "PlayerAction", MC_SET_ENTITY_DATA => "SetEntityData", diff --git a/server.php b/server.php index c4229f29..3b8df414 100644 --- a/server.php +++ b/server.php @@ -70,6 +70,7 @@ foreach($prop as $line){ case "max-players": case "port": case "debug": + case "difficulty": case "time-per-second": $v = (int) $v; break; @@ -226,6 +227,17 @@ function serverCommands(){ console("[INFO] Gamemode changed to ".$server->gamemode); loadConfig(true); break; + case "difficulty": + $s = trim(array_shift($params)); + if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1)){ + console("[INFO] Usage: /difficulty <0 | 1>"); + break; + } + $config["difficulty"] = (int) $s; + $server->difficulty = $config["difficulty"]; + console("[INFO] Difficulty changed to ".$server->difficulty); + loadConfig(true); + break; case "say": $s = implode(" ", $params); if(trim($s) == ""){ @@ -355,6 +367,7 @@ function serverCommands(){ case "?": console("[INFO] /help: Show available commands"); console("[INFO] /gamemode: Changes default gamemode"); + console("[INFO] /difficulty: Changes difficulty"); console("[INFO] /say: Broadcasts mesages"); console("[INFO] /time: Manages time"); console("[INFO] /list: Lists online users");