From a529e7566aa8b727fa750827d046baa6e73bcb2f Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Wed, 5 Mar 2014 00:54:12 +0100 Subject: [PATCH] Removed not needed network steps, corrected slot methods --- src/API/AchievementAPI.php | 164 ------ src/API/BanAPI.php | 8 +- src/API/BlockAPI.php | 22 +- src/API/ChatAPI.php | 8 +- src/API/ConsoleAPI.php | 12 +- src/API/PlayerAPI.php | 20 +- src/Entity.php | 2 +- src/EntityOLD.php | 12 +- src/MainServer.php | 16 +- src/Player.php | 276 ++++++---- src/dependencies.php | 2 +- src/entity/HumanEntity.php | 4 +- src/entity/PlayerEntity.php | 2 +- src/functions.php | 2 +- src/material/block/solid/BurningFurnace.php | 4 +- src/material/block/solid/Chest.php | 4 +- src/material/item/generic/Painting.php | 2 +- src/material/item/tool/FlintSteel.php | 2 +- src/network/MinecraftInterface.php | 28 +- src/network/query/QueryHandler.php | 4 +- src/network/raknet/RakNetCodec.php | 185 ------- src/network/raknet/RakNetDataPacket.php | 2 +- src/network/raknet/RakNetPacket.php | 484 ++++++++++++++++++ src/network/raknet/RakNetParser.php | 367 ------------- src/tile/ContainerTileTrait.php | 10 +- src/tile/FurnaceTile.php | 14 +- src/utils/Utils.php | 2 +- src/world/Level.php | 4 +- .../generator/noise/NoiseGeneratorPerlin.php | 6 +- .../generator/noise/NoiseGeneratorSimplex.php | 10 +- src/world/generator/object/OreObject.php | 12 +- 31 files changed, 761 insertions(+), 929 deletions(-) delete mode 100644 src/API/AchievementAPI.php delete mode 100644 src/network/raknet/RakNetCodec.php delete mode 100644 src/network/raknet/RakNetParser.php diff --git a/src/API/AchievementAPI.php b/src/API/AchievementAPI.php deleted file mode 100644 index f8b047965..000000000 --- a/src/API/AchievementAPI.php +++ /dev/null @@ -1,164 +0,0 @@ - array( - "name" => "Taking Inventory", - "requires" => array(), - ),*/ - "mineWood" => array( - "name" => "Getting Wood", - "requires" => array( - //"openInventory", - ), - ), - "buildWorkBench" => array( - "name" => "Benchmarking", - "requires" => array( - "mineWood", - ), - ), - "buildPickaxe" => array( - "name" => "Time to Mine!", - "requires" => array( - "buildWorkBench", - ), - ), - "buildFurnace" => array( - "name" => "Hot Topic", - "requires" => array( - "buildPickaxe", - ), - ), - "acquireIron" => array( - "name" => "Acquire hardware", - "requires" => array( - "buildFurnace", - ), - ), - "buildHoe" => array( - "name" => "Time to Farm!", - "requires" => array( - "buildWorkBench", - ), - ), - "makeBread" => array( - "name" => "Bake Bread", - "requires" => array( - "buildHoe", - ), - ), - "bakeCake" => array( - "name" => "The Lie", - "requires" => array( - "buildHoe", - ), - ), - "buildBetterPickaxe" => array( - "name" => "Getting an Upgrade", - "requires" => array( - "buildPickaxe", - ), - ), - "buildSword" => array( - "name" => "Time to Strike!", - "requires" => array( - "buildWorkBench", - ), - ), - "diamonds" => array( - "name" => "DIAMONDS!", - "requires" => array( - "acquireIron", - ), - ), - - ); - - function __construct(){ - } - - public static function broadcastAchievement(Player $player, $achievementId){ - if(isset(self::$achievements[$achievementId])){ - $result = ServerAPI::request()->api->dhandle("achievement.broadcast", array("player" => $player, "achievementId" => $achievementId)); - if($result !== false and $result !== true){ - if(ServerAPI::request()->api->getProperty("announce-player-achievements") == true){ - ServerAPI::request()->api->chat->broadcast($player->username." has just earned the achievement ".self::$achievements[$achievementId]["name"]); - }else{ - $player->sendChat("You have just earned the achievement ".self::$achievements[$achievementId]["name"]); - } - } - return true; - } - return false; - } - - public static function addAchievement($achievementId, $achievementName, array $requires = array()){ - if(!isset(self::$achievements[$achievementId])){ - self::$achievements[$achievementId] = array( - "name" => $achievementName, - "requires" => $requires, - ); - return true; - } - return false; - } - - public static function hasAchievement(Player $player, $achievementId){ - if(!isset(self::$achievements[$achievementId]) or !isset($player->achievements)){ - $player->achievements = array(); - return false; - } - - if(!isset($player->achievements[$achievementId]) or $player->achievements[$achievementId] == false){ - return false; - } - return true; - } - - public static function grantAchievement(Player $player, $achievementId){ - if(isset(self::$achievements[$achievementId]) and !self::hasAchievement($player, $achievementId)){ - foreach(self::$achievements[$achievementId]["requires"] as $requerimentId){ - if(!self::hasAchievement($player, $requerimentId)){ - return false; - } - } - if(ServerAPI::request()->api->dhandle("achievement.grant", array("player" => $player, "achievementId" => $achievementId)) !== false){ - $player->achievements[$achievementId] = true; - self::broadcastAchievement($player, $achievementId); - return true; - }else{ - return false; - } - } - return false; - } - - public static function removeAchievement(Player $player, $achievementId){ - if(self::hasAchievement($player, $achievementId)){ - $player->achievements[$achievementId] = false; - } - } - - public function init(){ - } -} diff --git a/src/API/BanAPI.php b/src/API/BanAPI.php index 18e44357a..6579ff356 100644 --- a/src/API/BanAPI.php +++ b/src/API/BanAPI.php @@ -139,7 +139,7 @@ class BanAPI{ break; } $this->server->api->console->run(implode(" ", $params), $player); - $output .= "Command ran as ".$player->username.".\n"; + $output .= "Command ran as ".$player->getUsername().".\n"; break; case "op": $user = strtolower($params[0]); @@ -188,9 +188,9 @@ class BanAPI{ $this->server->schedule(60, array($player, "close"), "You have been kicked: ".$reason); //Forces a kick $player->blocked = true; if($issuer instanceof Player){ - $this->server->api->chat->broadcast($player->username." has been kicked by ".$issuer->username.": $reason\n"); + $this->server->api->chat->broadcast($player->getUsername()." has been kicked by ".$issuer->getUsername().": $reason\n"); }else{ - $this->server->api->chat->broadcast($player->username." has been kicked: $reason\n"); + $this->server->api->chat->broadcast($player->getUsername()." has been kicked: $reason\n"); } } } @@ -286,7 +286,7 @@ class BanAPI{ $player->close("You have been banned"); } if($issuer instanceof Player){ - $this->server->api->chat->broadcast($user." has been banned by ".$issuer->username."\n"); + $this->server->api->chat->broadcast($user." has been banned by ".$issuer->getUsername()."\n"); }else{ $this->server->api->chat->broadcast($user." has been banned\n"); } diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 918fd9ce3..63fa1c415 100644 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -291,9 +291,9 @@ class BlockAPI{ $item = BlockAPI::fromString($params[1]); if(!isset($params[2])){ - $item->count = $item->getMaxStackSize(); + $item->setCount($item->getMaxStackSize()); }else{ - $item->count = (int) $params[2]; + $item->setCount((int) $params[2]); } if($player instanceof Player){ @@ -305,8 +305,8 @@ class BlockAPI{ $output .= "You cannot give an air block to a player.\n"; break; } - $player->addItem($item->getID(), $item->getMetadata(), $item->count); - $output .= "Giving ".$item->count." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$player->username."\n"; + $player->addItem($item); + $output .= "Giving ".$item->getCount()." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$player->getUsername()."\n"; }else{ $output .= "Unknown player.\n"; } @@ -354,7 +354,7 @@ class BlockAPI{ return $this->cancelAction($target, $player, false); } if(($player->gamemode & 0x01) === 0 and $item->useOn($target) and $item->getMetadata() >= $item->getMaxDurability()){ - $player->setSlot($player->slot, new Item(AIR, 0, 0), false); + $player->setSlot($player->slot, new Item(AIR, 0, 0)); } }else{ return $this->cancelAction($target, $player, false); @@ -410,8 +410,8 @@ class BlockAPI{ } if($item->isActivable === true and $item->onActivate($player->level, $player, $block, $target, $face, $fx, $fy, $fz) === true){ - if($item->count <= 0){ - $player->setSlot($player->slot, BlockAPI::getItem(AIR, 0, 0), false); + if($item->getCount() <= 0){ + $player->setSlot($player->slot, BlockAPI::getItem(AIR, 0, 0)); } return false; } @@ -456,14 +456,14 @@ class BlockAPI{ "Text2" => new NBTTag_String("Text2", ""), "Text3" => new NBTTag_String("Text3", ""), "Text4" => new NBTTag_String("Text4", ""), - "creator" => new NBTTag_String("creator", $player->username) + "creator" => new NBTTag_String("creator", $player->getUsername()) ))); } if(($player->gamemode & 0x01) === 0x00){ - --$item->count; - if($item->count <= 0){ - $player->setSlot($player->slot, BlockAPI::getItem(AIR, 0, 0), false); + $item->setCount($item->getCount() - 1); + if($item->getCount() <= 0){ + $player->setSlot($player->slot, BlockAPI::getItem(AIR, 0, 0)); } } diff --git a/src/API/ChatAPI.php b/src/API/ChatAPI.php index d82e1e4b0..658906b94 100644 --- a/src/API/ChatAPI.php +++ b/src/API/ChatAPI.php @@ -62,7 +62,7 @@ class ChatAPI{ $sender = ucfirst($issuer); } }else{ - $sender = $issuer->username; + $sender = $issuer->getUsername(); } $this->broadcast("* $sender ".implode(" ", $params)); break; @@ -74,12 +74,12 @@ class ChatAPI{ if(!($issuer instanceof Player)){ $sender = ucfirst($issuer); }else{ - $sender = $issuer->username; + $sender = $issuer->getUsername(); } $n = array_shift($params); $target = Player::get($n); if($target instanceof Player){ - $target = $target->username; + $target = $target->getUsername(); }else{ $target = strtolower($n); if($target === "server" or $target === "console" or $target === "rcon"){ @@ -129,7 +129,7 @@ class ChatAPI{ if($owner !== false){ if($owner instanceof Player){ if($whitelist === false){ - console("[INFO] <".$owner->username."> ".$text); + console("[INFO] <".$owner->getUsername()."> ".$text); } }else{ if($whitelist === false){ diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 7df51187e..24328935f 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -177,7 +177,7 @@ class ConsoleAPI{ return $this->run($this->alias[$cmd] . ($params !== "" ? " " .$params:""), $issuer, $cmd); } if($issuer instanceof Player){ - console("[DEBUG] ".TextFormat::AQUA.$issuer->username.TextFormat::RESET." issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2); + console("[DEBUG] ".TextFormat::AQUA.$issuer->getUsername().TextFormat::RESET." issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2); }else{ console("[DEBUG] ".TextFormat::YELLOW."*".$issuer.TextFormat::RESET." issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2); } @@ -194,7 +194,7 @@ class ConsoleAPI{ case "u": case "player": case "username": - $p = ($issuer instanceof Player) ? $issuer->username:$issuer; + $p = ($issuer instanceof Player) ? $issuer->getUsername():$issuer; $params = substr_replace($params, $p, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1); $offsetshift -= strlen($selector[0]) - strlen($p) + 1; break; @@ -207,10 +207,10 @@ class ConsoleAPI{ case "a": case "all": if($issuer instanceof Player){ - if($this->server->api->ban->isOp($issuer->username)){ + if($this->server->api->ban->isOp($issuer->getUsername())){ $output = ""; foreach(Player::getAll() as $p){ - $output .= $this->run($cmd . " ". substr_replace($params, $p->username, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1), $issuer, $alias); + $output .= $this->run($cmd . " ". substr_replace($params, $p->getUsername(), $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1), $issuer, $alias); } }else{ $issuer->sendChat("You don't have permissions to use this command.\n"); @@ -218,7 +218,7 @@ class ConsoleAPI{ }else{ $output = ""; foreach(Player::getAll() as $p){ - $output .= $this->run($cmd . " ". substr_replace($params, $p->username, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1), $issuer, $alias); + $output .= $this->run($cmd . " ". substr_replace($params, $p->getUsername(), $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1), $issuer, $alias); } } return $output; @@ -234,7 +234,7 @@ class ConsoleAPI{ return; } - $p = $l[mt_rand(0, count($l) - 1)]->username; + $p = $l[mt_rand(0, count($l) - 1)]->getUsername(); $params = substr_replace($params, $p, $selector[1] + $offsetshift - 1, strlen($selector[0]) + 1); $offsetshift -= strlen($selector[0]) - strlen($p) + 1; break; diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index a0e1b55d7..ce1d8bf59 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -108,7 +108,7 @@ class PlayerAPI{ break; } } - $this->server->api->chat->broadcast($data["player"]->username . $message); + $this->server->api->chat->broadcast($data["player"]->getUsername() . $message); return true; } } @@ -156,7 +156,7 @@ class PlayerAPI{ if($target instanceof Level){ $output .= "Spawnpoint of world ".$target->getName()." set correctly!\n"; }elseif($target !== $issuer){ - $output .= "Spawnpoint of ".$target->username." set correctly!\n"; + $output .= "Spawnpoint of ".$target->getUsername()." set correctly!\n"; }else{ $output .= "Spawnpoint set correctly!\n"; } @@ -219,13 +219,13 @@ class PlayerAPI{ break; } if($player->setGamemode($gms[strtolower($setgm)])){ - $output .= "Gamemode of ".$player->username." changed to ".$player->getGamemode()."\n"; + $output .= "Gamemode of ".$player->getUsername()." changed to ".$player->getGamemode()."\n"; } break; case "tp": if(count($params) <= 2 or substr($params[0], 0, 2) === "w:" or substr($params[1], 0, 2) === "w:"){ if((!isset($params[1]) or substr($params[0], 0, 2) === "w:") and isset($params[0]) and ($issuer instanceof Player)){ - $name = $issuer->username; + $name = $issuer->getUsername(); $target = implode(" ", $params); }elseif(isset($params[1]) and isset($params[0])){ $name = array_shift($params); @@ -241,7 +241,7 @@ class PlayerAPI{ } }else{ if(!isset($params[3]) and isset($params[2]) and isset($params[1]) and isset($params[0]) and ($issuer instanceof Player)){ - $name = $issuer->username; + $name = $issuer->getUsername(); $x = $params[0]; $y = $params[1]; $z = $params[2]; @@ -281,7 +281,7 @@ class PlayerAPI{ break; } foreach(Player::$list as $c){ - $output .= $c->username.", "; + $output .= $c->getUsername().", "; } $output = substr($output, 0, -2)."\n"; break; @@ -295,7 +295,7 @@ class PlayerAPI{ if($lv instanceof Level){ $origin = Player::get($name); if($origin instanceof Player){ - $name = $origin->username; + $name = $origin->getUsername(); return $origin->teleport($lv->getSafeSpawn()); } }else{ @@ -304,10 +304,10 @@ class PlayerAPI{ } $player = Player::get($target); if(($player instanceof Player) and ($player->entity instanceof Entity)){ - $target = $player->username; + $target = $player->getUsername(); $origin = Player::get($name); if($origin instanceof Player){ - $name = $origin->username; + $name = $origin->getUsername(); return $origin->teleport($player->entity); } } @@ -317,7 +317,7 @@ class PlayerAPI{ public function tppos(&$name, &$x, &$y, &$z){ $player = Player::get($name); if(($player instanceof Player) and ($player->entity instanceof Entity)){ - $name = $player->username; + $name = $player->getUsername(); $x = $x{0} === "~" ? $player->entity->x + floatval(substr($x, 1)):floatval($x); $y = $y{0} === "~" ? $player->entity->y + floatval(substr($y, 1)):floatval($y); $z = $z{0} === "~" ? $player->entity->z + floatval(substr($z, 1)):floatval($z); diff --git a/src/Entity.php b/src/Entity.php index f94db7349..c36ff1168 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -457,7 +457,7 @@ abstract class Entity extends Position{ public function spawnToAll(){ foreach($this->level->getPlayers() as $player){ - if($player->eid !== false or $player->spawned !== true){ + if(isset($player->id) and $player->spawned === true){ $this->spawnTo($player); } } diff --git a/src/EntityOLD.php b/src/EntityOLD.php index 8c12f4347..1cf56834a 100644 --- a/src/EntityOLD.php +++ b/src/EntityOLD.php @@ -107,7 +107,7 @@ class EntityOLD extends Position{ case ENTITY_ITEM: if(isset($data["item"]) and ($data["item"] instanceof Item)){ $this->meta = $this->data["item"]->getMetadata(); - $this->stack = $this->data["item"]->count; + $this->stack = $this->data["item"]->getCount(); }else{ $this->meta = (int) $this->data["meta"]; $this->stack = (int) $this->data["stack"]; @@ -177,15 +177,15 @@ class EntityOLD extends Position{ for($i = 0; $i < PLAYER_SURVIVAL_SLOTS; ++$i){ $slot = $this->player->getSlot($i); $this->player->setSlot($i, BlockAPI::getItem(AIR, 0, 0)); - if($slot->getID() !== AIR and $slot->count > 0){ - $inv[] = array($slot->getID(), $slot->getMetadata(), $slot->count); + if($slot->getID() !== AIR and $slot->getCount() > 0){ + $inv[] = array($slot->getID(), $slot->getMetadata(), $slot->getCount()); } } for($re = 0; $re < 4; $re++){ $slot = $this->player->getArmor($re); $this->player->setArmor($re, BlockAPI::getItem(AIR, 0, 0)); - if($slot->getID() !== AIR and $slot->count > 0){ - $inv[] = array($slot->getID(), $slot->getMetadata(), $slot->count); + if($slot->getID() !== AIR and $slot->getCount() > 0){ + $inv[] = array($slot->getID(), $slot->getMetadata(), $slot->getCount()); } } return $inv; @@ -254,7 +254,7 @@ class EntityOLD extends Position{ if($this->class === ENTITY_PLAYER and ($this->player instanceof Player) and $this->player->spawned === true and $this->player->blocked !== true){ foreach($this->server->api->entity->getRadius($this, 1.5, ENTITY_ITEM) as $item){ if($item->closed === false and $item->spawntime > 0 and ($time - $item->spawntime) >= 0.6){ - if((($this->player->gamemode & 0x01) === 1 or $this->player->hasSpace($item->type, $item->meta, $item->stack) === true) and $this->server->api->dhandle("player.pickup", array( + if((($this->player->gamemode & 0x01) === 1 or $this->player->canAddItem($item->type, $item->meta, $item->stack) === true) and $this->server->api->dhandle("player.pickup", array( "eid" => $this->player->eid, "player" => $this->player, "entity" => $item, diff --git a/src/MainServer.php b/src/MainServer.php index 9ce51c2c0..42517be2b 100644 --- a/src/MainServer.php +++ b/src/MainServer.php @@ -545,7 +545,7 @@ class MainServer{ new Player($packet->clientID, $packet->ip, $packet->port, $packet->mtuSize); //New Session! $pk = new RakNetPacket(RakNetInfo::OPEN_CONNECTION_REPLY_2); $pk->serverID = $this->serverID; - $pk->port = $this->port; + $pk->serverPort = $this->port; $pk->mtuSize = $packet->mtuSize; $pk->ip = $packet->ip; $pk->port = $packet->port; @@ -566,21 +566,21 @@ class MainServer{ if($packet instanceof Packet){ $this->packetHandler($packet); $lastLoop = 0; - }elseif($this->tick() > 0){ + } + if($this->tick() > 0){ $lastLoop = 0; }else{ ++$lastLoop; - if($lastLoop < 16){ - usleep(1); - }elseif($lastLoop < 128){ - usleep(100); + if($lastLoop < 64){ + }elseif($lastLoop < 256){ + usleep(100); + }elseif($lastLoop < 512){ usleep(512); }else{ - usleep(10000); + usleep(5000); } } - $this->tick(); } } diff --git a/src/Player.php b/src/Player.php index f499c4883..1652bbd76 100644 --- a/src/Player.php +++ b/src/Player.php @@ -43,10 +43,10 @@ class Player extends PlayerEntity{ protected $ip; protected $port; private $counter = array(0, 0, 0, 0); - private $username; - private $iusername; - private $startAction = false; - protected $isSleeping = false; + protected $username; + protected $iusername; + protected $startAction = false; + protected $sleeping = false; public $auth = false; public $CID; public $MTU; @@ -63,7 +63,7 @@ class Player extends PlayerEntity{ public $achievements = array(); public $chunksLoaded = array(); private $viewDistance; - private $chunksOrder = array(); + protected $chunksOrder = array(); private $lastMeasure = 0; private $bandwidthRaw = 0; private $bandwidthStats = array(0, 0, 0); @@ -72,7 +72,7 @@ class Player extends PlayerEntity{ private $spawnPosition; private $packetLoss = 0; private $lastChunk = false; - private $hunkScheduled = 0; + private $chunkScheduled = 0; public $lastCorrect; private $bigCnt; private $packetStats; @@ -82,13 +82,7 @@ class Player extends PlayerEntity{ private $chunkCount = array(); private $received = array(); public $loginData = array(); - - public function __get($name){ - if(isset($this->{$name})){ - return ($this->{$name}); - } - return null; - } + public static function get($name, $alike = true, $multiple = false){ $name = trim(strtolower($name)); @@ -236,6 +230,69 @@ class Player extends PlayerEntity{ $player->dataPacket(clone $packet); } } + + public function grantAchievement($achievementId){ + if(isset(Achievement::$list[$achievementId]) and !$this->hasAchievement($achievementId)){ + foreach(Achievement::$list[$achievementId]["requires"] as $requerimentId){ + if(!$this->hasAchievement($requerimentId)){ + return false; + } + } + if($this->server->api->dhandle("achievement.grant", array("player" => $this, "achievementId" => $achievementId)) !== false){ + $this->achievements[$achievementId] = true; + Achievement::broadcast($this, $achievementId); + return true; + }else{ + return false; + } + } + return false; + } + + public function hasAchievement($achievementId){ + if(!isset(Achievement::$list[$achievementId]) or !isset($this->achievements)){ + $this->achievements = array(); + return false; + } + + if(!isset($this->achievements[$achievementId]) or $this->achievements[$achievementId] == false){ + return false; + } + return true; + } + + public function removeAchievement($achievementId){ + if($this->hasAchievement($achievementId)){ + $this->achievements[$achievementId] = false; + } + } + + public function getUsername(){ + return $this->username; + } + + public function isConnected(){ + return $this->connected === true; + } + + public function getIP(){ + return $this->ip; + } + + public function getPort(){ + return $this->port; + } + + public function isSleeping(){ + return $this->sleeping instanceof Vector3; + } + + public function setChunkIndex($index, $flags){ + if(isset($this->chunksLoaded[$index])){ + $this->chunksLoaded[$index] |= $flags; + } + } + /** * @param integer $clientID @@ -552,34 +609,34 @@ class Player extends PlayerEntity{ */ public function sleepOn(Vector3 $pos){ foreach($this->level->getPlayers() as $p){ - if($p->isSleeping instanceof Vector3){ - if($pos->distance($p->isSleeping) <= 0.1){ + if($p->sleeping instanceof Vector3){ + if($pos->distance($p->sleeping) <= 0.1){ return false; } } } - $this->isSleeping = $pos; + $this->sleeping = $pos; $this->teleport(new Position($pos->x + 0.5, $pos->y + 1, $pos->z + 0.5, $this->level)); - if($this->entity instanceof Entity){ - $this->entity->updateMetadata(); - } + /*if($this->entity instanceof Entity){ + $this->updateMetadata(); + }*/ $this->setSpawn($pos); $this->server->schedule(60, array($this, "checkSleep")); return true; } public function stopSleep(){ - $this->isSleeping = false; + $this->sleeping = false; if($this->entity instanceof Entity){ $this->entity->updateMetadata(); } } public function checkSleep(){ - if($this->isSleeping !== false){ + if($this->sleeping !== false){ if($this->server->api->time->getPhase($this->level) === "night"){ foreach($this->level->getPlayers() as $p){ - if($p->isSleeping === false){ + if($p->sleeping === false){ return false; } } @@ -675,7 +732,7 @@ class Player extends PlayerEntity{ break; case "player.armor": if($data["player"]->level === $this->level){ - if($data["eid"] === $this->eid){ + if($data["eid"] === $this->id){ $this->sendArmor($this); break; } @@ -686,32 +743,32 @@ class Player extends PlayerEntity{ } break; case "player.pickup": - if($data["eid"] === $this->eid){ + if($data["eid"] === $this->id){ $data["eid"] = 0; $pk = new TakeItemEntityPacket; $pk->eid = 0; - $pk->target = $data["entity"]->eid; + $pk->target = $data["entity"]->getID(); $this->dataPacket($pk); if(($this->gamemode & 0x01) === 0x00){ - $this->addItem($data["entity"]->type, $data["entity"]->meta, $data["entity"]->stack, false); + //$this->addItem($data["entity"]->type, $data["entity"]->meta, $data["entity"]->stack); } switch($data["entity"]->type){ case WOOD: - AchievementAPI::grantAchievement($this, "mineWood"); + $this->grantAchievement("mineWood"); break; case DIAMOND: - AchievementAPI::grantAchievement($this, "diamond"); + $this->grantAchievement("diamond"); break; } }elseif($data["entity"]->level === $this->level){ $pk = new TakeItemEntityPacket; $pk->eid = $data["eid"]; - $pk->target = $data["entity"]->eid; + $pk->target = $data["entity"]->getID(); $this->dataPacket($pk); } break; case "player.equipment.change": - if($data["eid"] === $this->eid or $data["player"]->level !== $this->level){ + if($data["eid"] === $this->id or $data["player"]->level !== $this->level){ break; } $data["slot"] = 0; @@ -725,18 +782,18 @@ class Player extends PlayerEntity{ break; case "entity.motion": - if($data->eid === $this->eid or $data->level !== $this->level){ + if($data->getID() === $this->id or $data->level !== $this->level){ break; } $pk = new SetEntityMotionPacket; - $pk->eid = $data->eid; + $pk->eid = $data->getID(); $pk->speedX = $data->speedX; $pk->speedY = $data->speedY; $pk->speedZ = $data->speedZ; $this->dataPacket($pk); break; case "entity.animate": - if($data["eid"] === $this->eid or $data["entity"]->level !== $this->level){ + if($data["eid"] === $this->id or $data["entity"]->level !== $this->level){ break; } $pk = new AnimatePacket; @@ -745,10 +802,10 @@ class Player extends PlayerEntity{ $this->dataPacket($pk); break; case "entity.metadata": - if($data->eid === $this->eid){ + if($data->getID() === $this->id){ $eid = 0; }else{ - $eid = $data->eid; + $eid = $data->getID(); } if($data->level === $this->level){ $pk = new SetEntityDataPacket; @@ -758,10 +815,10 @@ class Player extends PlayerEntity{ } break; case "entity.event": - if($data["entity"]->eid === $this->eid){ + if($data["entity"]->getID() === $this->id){ $eid = 0; }else{ - $eid = $data["entity"]->eid; + $eid = $data["entity"]->getID(); } if($data["entity"]->level === $this->level){ $pk = new EntityEventPacket; @@ -887,7 +944,7 @@ class Player extends PlayerEntity{ }else{ $craftItem[1] = $item->getMetadata(); } - $craftItem[2] += $item->count; + $craftItem[2] += $item->getCount(); } } @@ -895,12 +952,12 @@ class Player extends PlayerEntity{ $recipeItems = array(); foreach($recipe as $slot => $item){ if(!isset($recipeItems[$item->getID()])){ - $recipeItems[$item->getID()] = array($item->getID(), $item->getMetadata(), $item->count); + $recipeItems[$item->getID()] = array($item->getID(), $item->getMetadata(), $item->getCount()); }else{ if($item->getMetadata() !== $recipeItems[$item->getID()][1]){ $recipeItems[$item->getID()][1] = false; } - $recipeItems[$item->getID()][2] += $item->count; + $recipeItems[$item->getID()][2] += $item->getCount(); } } @@ -920,52 +977,52 @@ class Player extends PlayerEntity{ } foreach($recipe as $slot => $item){ $s = $this->getSlot($slot); - $s->count -= $item->count; - if($s->count <= 0){ - $this->setSlot($slot, BlockAPI::getItem(AIR, 0, 0), false); + $s->setCount($s->getCount() - $item->getCount()); + if($s->getCount() <= 0){ + $this->setSlot($slot, BlockAPI::getItem(AIR, 0, 0)); } } foreach($craft as $slot => $item){ $s = $this->getSlot($slot); - if($s->count <= 0 or $s->getID() === AIR){ - $this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $item->count), false); + if($s->getCount() <= 0 or $s->getID() === AIR){ + $this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $item->getCount())); }else{ - $this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $s->count + $item->count), false); + $this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $s->getCount() + $item->getCount())); } switch($item->getID()){ case WORKBENCH: - AchievementAPI::grantAchievement($this, "buildWorkBench"); + $this->grantAchievement("buildWorkBench"); break; case WOODEN_PICKAXE: - AchievementAPI::grantAchievement($this, "buildPickaxe"); + $this->grantAchievement("buildPickaxe"); break; case FURNACE: - AchievementAPI::grantAchievement($this, "buildFurnace"); + $this->grantAchievement("buildFurnace"); break; case WOODEN_HOE: - AchievementAPI::grantAchievement($this, "buildHoe"); + $this->grantAchievement("buildHoe"); break; case BREAD: - AchievementAPI::grantAchievement($this, "makeBread"); + $this->grantAchievement("makeBread"); break; case CAKE: - AchievementAPI::grantAchievement($this, "bakeCake"); + $this->grantAchievement("bakeCake"); break; case STONE_PICKAXE: case GOLD_PICKAXE: case IRON_PICKAXE: case DIAMOND_PICKAXE: - AchievementAPI::grantAchievement($this, "buildBetterPickaxe"); + $this->grantAchievement("buildBetterPickaxe"); break; case WOODEN_SWORD: - AchievementAPI::grantAchievement($this, "buildSword"); + $this->grantAchievement("buildSword"); break; case DIAMOND: - AchievementAPI::grantAchievement($this, "diamond"); + $this->grantAchievement("diamond"); break; case CAKE: - $this->addItem(BUCKET, 0, 3, false); + $this->addItem(BlockAPI::getItem(BUCKET, 0, 3)); break; } @@ -973,18 +1030,12 @@ class Player extends PlayerEntity{ } return $res; } - - /** - * @param Vector3 $pos - * @param float|boolean $yaw - * @param float|boolean $pitch - * @param float|boolean $terrain - * @param float|boolean $force - * - * @return boolean - */ public function getGamemode(){ + return $this->gamemode; + } + + public function getGamemodeString(){ switch($this->gamemode){ case SURVIVAL: return "survival"; @@ -1575,7 +1626,7 @@ class Player extends PlayerEntity{ } $this->craftingItems = array(); $this->toCraft = array(); - $packet->eid = $this->eid; + $packet->eid = $this->id; $data = array(); $data["eid"] = $packet->eid; $data["player"] = $this; @@ -1635,7 +1686,7 @@ class Player extends PlayerEntity{ if($this->spawned === false or $this->blocked === true){ break; } - $packet->eid = $this->eid; + $packet->eid = $this->id; $this->craftingItems = array(); $this->toCraft = array(); @@ -1732,7 +1783,7 @@ class Player extends PlayerEntity{ $this->craftingItems = array(); $this->toCraft = array(); - $packet->eid = $this->eid; + $packet->eid = $this->id; for($i = 0; $i < 4; ++$i){ $s = $packet->slots[$i]; if($s === 0 or $s === 255){ @@ -1742,16 +1793,16 @@ class Player extends PlayerEntity{ } $slot = $this->armor[$i]; if($slot->getID() !== AIR and $s->getID() === AIR){ - $this->addItem($slot->getID(), $slot->getMetadata(), 1, false); + $this->addItem($slot); $this->armor[$i] = BlockAPI::getItem(AIR, 0, 0); $packet->slots[$i] = 255; }elseif($s->getID() !== AIR and $slot->getID() === AIR and ($sl = $this->hasItem($s->getID())) !== false){ $this->armor[$i] = $this->getSlot($sl); - $this->setSlot($sl, BlockAPI::getItem(AIR, 0, 0), false); + $this->setSlot($sl, BlockAPI::getItem(AIR, 0, 0)); }elseif($s->getID() !== AIR and $slot->getID() !== AIR and ($slot->getID() !== $s->getID() or $slot->getMetadata() !== $s->getMetadata()) and ($sl = $this->hasItem($s->getID())) !== false){ $item = $this->armor[$i]; $this->armor[$i] = $this->getSlot($sl); - $this->setSlot($sl, $item, false); + $this->setSlot($sl, $item); }else{ $packet->slots[$i] = 255; } @@ -1767,7 +1818,7 @@ class Player extends PlayerEntity{ if($this->spawned === false){ break; } - $packet->eid = $this->eid; + $packet->eid = $this->id; $data = array(); $data["target"] = $packet->target; $data["eid"] = $packet->eid; @@ -1842,10 +1893,10 @@ class Player extends PlayerEntity{ default: $damage = 1;//$this->server->difficulty; } - $target->harm($damage, $this->eid); + $target->harm($damage, $this->id); if($slot->isTool() === true and ($this->gamemode & 0x01) === 0){ if($slot->useOn($target) and $slot->getMetadata() >= $slot->getMaxDurability()){ - $this->setSlot($this->slot, new Item(AIR, 0, 0), false); + $this->setSlot($this->slot, new Item(AIR, 0, 0)); } } } @@ -1856,7 +1907,7 @@ class Player extends PlayerEntity{ if($this->spawned === false){ break; } - $packet->eid = $this->eid; + $packet->eid = $this->id; $this->server->api->dhandle("entity.animate", array("eid" => $packet->eid, "entity" => $this->entity, "action" => $packet->action)); break;*/ case ProtocolInfo::RESPAWN_PACKET: @@ -1889,7 +1940,7 @@ class Player extends PlayerEntity{ } $this->craftingItems = array(); $this->toCraft = array(); - $packet->eid = $this->eid; + $packet->eid = $this->id; if($this->entity->inAction === true){ $this->entity->inAction = false; $this->entity->updateMetadata(); @@ -1926,12 +1977,12 @@ class Player extends PlayerEntity{ $this->dataPacket($pk); $this->entity->heal($items[$slot->getID()], "eating"); - --$slot->count; - if($slot->count <= 0){ - $this->setSlot($this->slot, BlockAPI::getItem(AIR, 0, 0), false); + //--$slot->count; + if($slot->getCount() <= 0){ + $this->setSlot($this->slot, BlockAPI::getItem(AIR, 0, 0)); } if($slot->getID() === MUSHROOM_STEW or $slot->getID() === BEETROOT_SOUP){ - $this->addItem(BOWL, 0, 1, false); + $this->addItem(BlockAPI::getItem(BOWL, 0, 1)); } } break; @@ -1941,7 +1992,7 @@ class Player extends PlayerEntity{ if($this->spawned === false or $this->blocked === true){ break; } - $packet->eid = $this->eid; + $packet->eid = $this->id; $packet->item = $this->getSlot($this->slot); $this->craftingItems = array(); $this->toCraft = array(); @@ -2032,23 +2083,23 @@ class Player extends PlayerEntity{ if($packet->windowid === 0){ $craft = false; $slot = $this->getSlot($packet->slot); - if($slot->count >= $packet->item->count and (($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()) or ($packet->item->getID() === AIR and $packet->item->count === 0)) and !isset($this->craftingItems[$packet->slot])){ //Crafting recipe - $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count - $packet->item->count); + if($slot->getCount() >= $packet->item->getCount() and (($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()) or ($packet->item->getID() === AIR and $packet->item->getCount() === 0)) and !isset($this->craftingItems[$packet->slot])){ //Crafting recipe + $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->getCount() - $packet->item->getCount()); $this->craftingItems[$packet->slot] = $use; $craft = true; - }elseif($slot->count <= $packet->item->count and ($slot->getID() === AIR or ($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()))){ //Crafting final - $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count - $slot->count); + }elseif($slot->getCount() <= $packet->item->getCount() and ($slot->getID() === AIR or ($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()))){ //Crafting final + $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->getCount() - $slot->getCount()); if(count($this->toCraft) === 0){ $this->toCraft[-1] = 0; } $this->toCraft[$packet->slot] = $craftItem; $craft = true; - }elseif(((count($this->toCraft) === 1 and isset($this->toCraft[-1])) or count($this->toCraft) === 0) and $slot->count > 0 and $slot->getID() > AIR and ($slot->getID() !== $packet->item->getID() or $slot->getMetadata() !== $packet->item->getMetadata())){ //Crafting final - $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); + }elseif(((count($this->toCraft) === 1 and isset($this->toCraft[-1])) or count($this->toCraft) === 0) and $slot->getCount() > 0 and $slot->getID() > AIR and ($slot->getID() !== $packet->item->getID() or $slot->getMetadata() !== $packet->item->getMetadata())){ //Crafting final + $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->getCount()); if(count($this->toCraft) === 0){ $this->toCraft[-1] = 0; } - $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count); + $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->getCount()); $this->craftingItems[$packet->slot] = $use; $this->toCraft[$packet->slot] = $craftItem; $craft = true; @@ -2089,7 +2140,7 @@ class Player extends PlayerEntity{ break; } - $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); + $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->getCount()); $slot = $tile->getSlot($slotn); if($this->server->api->dhandle("player.container.slot", array( @@ -2108,20 +2159,24 @@ class Player extends PlayerEntity{ break; } if($item->getID() !== AIR and $slot->getID() == $item->getID()){ - if($slot->count < $item->count){ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ + if($slot->getCount() < $item->getCount()){ + $it = clone $item; + $it->setCount($item->getCount() - $slot->getCount()); + if($this->removeItem($it) === false){ $this->sendInventory(); break; } - }elseif($slot->count > $item->count){ - $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); + }elseif($slot->getCount() > $item->getCount()){ + $it = clone $item; + $it->setCount($slot->getCount() - $item->getCount()); + $this->addItem($it); } }else{ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ + if($this->removeItem($item) === false){ $this->sendInventory(); break; } - $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); + $this->addItem($slot); } $tile->setSlot($slotn, $item, true, $offset); }else{ @@ -2138,7 +2193,7 @@ class Player extends PlayerEntity{ ){ break; } - $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); + $item = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->getCount()); $slot = $tile->getSlot($packet->slot); if($this->server->api->dhandle("player.container.slot", array( @@ -2159,26 +2214,30 @@ class Player extends PlayerEntity{ if($tile instanceof FurnaceTile and $packet->slot == 2){ switch($slot->getID()){ case IRON_INGOT: - AchievementAPI::grantAchievement($this, "acquireIron"); + $this->grantAchievement("acquireIron"); break; } } if($item->getID() !== AIR and $slot->getID() == $item->getID()){ - if($slot->count < $item->count){ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){ + if($slot->getCount() < $item->getCount()){ + $it = clone $item; + $it->setCount($item->getCount() - $slot->getCount()); + if($this->removeItem($it) === false){ $this->sendInventory(); break; } - }elseif($slot->count > $item->count){ - $this->addItem($item->getID(), $item->getMetadata(), $slot->count - $item->count, false); + }elseif($slot->getCount() > $item->getCount()){ + $it = clone $item; + $it->setCount($slot->count - $item->count); + $this->addItem($it); } }else{ - if($this->removeItem($item->getID(), $item->getMetadata(), $item->count, false) === false){ + if($this->removeItem($item) === false){ $this->sendInventory(); break; } - $this->addItem($slot->getID(), $slot->getMetadata(), $slot->count, false); + $this->addItem($slot); } $tile->setSlot($packet->slot, $item); } @@ -2221,7 +2280,7 @@ class Player extends PlayerEntity{ public function sendArmor($player = false){ $data = array( "player" => $this, - "eid" => $this->eid, + "eid" => $this->id, "slots" => array() ); for($i = 0; $i < 4; ++$i){ @@ -2240,7 +2299,7 @@ class Player extends PlayerEntity{ $this->dataPacket($pk); }else{ $pk = new PlayerArmorEquipmentPacket; - $pk->eid = $this->eid; + $pk->eid = $this->id; $pk->slots = $data["slots"]; $player->dataPacket($pk); } @@ -2375,5 +2434,10 @@ class Player extends PlayerEntity{ $this->bufferLen += 6 + $len; return array(); } + + public function setSlot($slot, Item $item){ + parent::setSlot($slot, $item); + $this->sendInventorySlot($slot); + } } diff --git a/src/dependencies.php b/src/dependencies.php index 92301025a..2eae613a1 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -96,7 +96,7 @@ require_once(FILE_PATH."/src/math/Vector3.php"); require_once(FILE_PATH."/src/math/Position.php"); require_once(FILE_PATH."/src/pmf/PMF.php"); -require_all(FILE_PATH . "src/", array("notimplemented")); //REMOVE LATER! +require_all(FILE_PATH . "src/", array_flip(array("notimplemented"))); //REMOVE LATER! $inc = get_included_files(); $inc[] = array_shift($inc); diff --git a/src/entity/HumanEntity.php b/src/entity/HumanEntity.php index f892c5c2e..2ab986a41 100644 --- a/src/entity/HumanEntity.php +++ b/src/entity/HumanEntity.php @@ -101,9 +101,9 @@ class HumanEntity extends CreatureEntity implements ProjectileSourceEntity, Inve }elseif($this->type === OBJECT_PRIMEDTNT){ $d[16]["value"] = (int) max(0, $this->data["fuse"] - (microtime(true) - $this->spawntime) * 20); }elseif($this->class === ENTITY_PLAYER){ - if($this->player->isSleeping !== false){ + if($this->player->sleeping !== false){ $d[16]["value"] = 2; - $d[17]["value"] = array($this->player->isSleeping->x, $this->player->isSleeping->y, $this->player->isSleeping->z); + $d[17]["value"] = array($this->player->sleeping->x, $this->player->sleeping->y, $this->player->sleeping->z); } }*/ return $d; diff --git a/src/entity/PlayerEntity.php b/src/entity/PlayerEntity.php index 6a554cd91..f2fa53b84 100644 --- a/src/entity/PlayerEntity.php +++ b/src/entity/PlayerEntity.php @@ -19,7 +19,7 @@ * */ -class PlayerEntity extends HumanEntity{ +abstract class PlayerEntity extends HumanEntity{ protected function initEntity(){ $this->level->players[$this->CID] = $this; diff --git a/src/functions.php b/src/functions.php index 70ed9eb02..f03a0501d 100644 --- a/src/functions.php +++ b/src/functions.php @@ -84,7 +84,7 @@ function require_all($path, array $ignore = array(), &$count = 0){ $dir = dir($path."/"); $dirs = array(); while(false !== ($file = $dir->read())){ - if($file !== "." and $file !== ".." and !in_array($file, $ignore, true)){ + if($file !== "." and $file !== ".." and !isset($ignore[$file])){ if(!is_dir($path.$file) and strtolower(substr($file, -3)) === "php"){ require_once($path.$file); ++$count; diff --git a/src/material/block/solid/BurningFurnace.php b/src/material/block/solid/BurningFurnace.php index e358523d8..56c7b64f3 100644 --- a/src/material/block/solid/BurningFurnace.php +++ b/src/material/block/solid/BurningFurnace.php @@ -96,8 +96,8 @@ class BurningFurnaceBlock extends SolidBlock{ if($t instanceof FurnaceTile){ for($s = 0; $s < FurnaceTile::SLOTS; ++$s){ $slot = $t->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ - $drops[] = array($slot->getID(), $slot->getMetadata(), $slot->count); + if($slot->getID() > AIR and $slot->getCount() > 0){ + $drops[] = array($slot->getID(), $slot->getMetadata(), $slot->getCount()); } } } diff --git a/src/material/block/solid/Chest.php b/src/material/block/solid/Chest.php index ca48fcacd..e3c396ff4 100644 --- a/src/material/block/solid/Chest.php +++ b/src/material/block/solid/Chest.php @@ -114,8 +114,8 @@ class ChestBlock extends TransparentBlock{ if($t instanceof ChestTile){ for($s = 0; $s < ChestTile::SLOTS; ++$s){ $slot = $t->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ - $drops[] = array($slot->getID(), $slot->getMetadata(), $slot->count); + if($slot->getID() > AIR and $slot->getCount() > 0){ + $drops[] = array($slot->getID(), $slot->getMetadata(), $slot->getCount()); } } } diff --git a/src/material/item/generic/Painting.php b/src/material/item/generic/Painting.php index 7e40d7c77..c8cca031d 100644 --- a/src/material/item/generic/Painting.php +++ b/src/material/item/generic/Painting.php @@ -76,7 +76,7 @@ class PaintingItem extends Item{ $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_PAINTING, $data); $e->spawnToAll(); if(($player->gamemode & 0x01) === 0x00){ - $player->removeItem($this->getID(), $this->getMetadata(), 1, false); + $player->removeItem(BlockAPI::getItem($this->getID(), $this->getMetadata(), 1)); } return true; } diff --git a/src/material/item/tool/FlintSteel.php b/src/material/item/tool/FlintSteel.php index 2f476c182..1492248e9 100644 --- a/src/material/item/tool/FlintSteel.php +++ b/src/material/item/tool/FlintSteel.php @@ -28,7 +28,7 @@ class FlintSteelItem extends Item{ public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ if(($player->gamemode & 0x01) === 0 and $this->useOn($block) and $this->getMetadata() >= $this->getMaxDurability()){ - $player->setSlot($player->slot, new Item(AIR, 0, 0), false); + $player->setSlot($player->slot, new Item(AIR, 0, 0)); } if($block->getID() === AIR and ($target instanceof SolidBlock)){ diff --git a/src/network/MinecraftInterface.php b/src/network/MinecraftInterface.php index 37a0862b7..9d4663c3e 100644 --- a/src/network/MinecraftInterface.php +++ b/src/network/MinecraftInterface.php @@ -36,33 +36,29 @@ class MinecraftInterface{ public function close(){ return $this->socket->close(false); } - + public function readPacket(){ $buf = null; $source = null; $port = null; - $len = $this->socket->read($buf, $source, $port); + $len = $this->socket->read($buffer, $source, $port); if($len === false or $len === 0){ return false; } $this->bandwidth[0] += $len; - return $this->parsePacket($buf, $source, $port); - } - - private function parsePacket($buffer, $source, $port){ + $pid = ord($buffer{0}); if(RakNetInfo::isValid($pid)){ - $parser = new RakNetParser($buffer); - if($parser->packet !== false){ - $parser->packet->ip = $source; - $parser->packet->port = $port; - if(EventHandler::callEvent(new PacketReceiveEvent($parser->packet)) === BaseEvent::DENY){ - return false; - } - return $parser->packet; + $packet = new RakNetPacket($pid); + $packet->buffer =& $buffer; + $packet->ip = $source; + $packet->port = $port; + $packet->decode(); + if(EventHandler::callEvent(new PacketReceiveEvent($packet)) === BaseEvent::DENY){ + return false; } - return false; + return $packet; }elseif($pid === 0xfe and $buffer{1} === "\xfd" and ServerAPI::request()->api->query instanceof QueryHandler){ $packet = new QueryPacket; $packet->ip = $source; @@ -86,7 +82,7 @@ class MinecraftInterface{ if(EventHandler::callEvent(new PacketSendEvent($packet)) === BaseEvent::DENY){ return 0; }elseif($packet instanceof RakNetPacket){ - new RakNetCodec($packet); + $packet->encode(); } $write = $this->socket->write($packet->buffer, $packet->ip, $packet->port); $this->bandwidth[1] += $write; diff --git a/src/network/query/QueryHandler.php b/src/network/query/QueryHandler.php index 422063d1b..c579a7c10 100644 --- a/src/network/query/QueryHandler.php +++ b/src/network/query/QueryHandler.php @@ -80,8 +80,8 @@ class QueryHandler{ } $str .= "\x00\x01player_\x00\x00"; foreach(Player::$list as $player){ - if($player->username != ""){ - $str .= $player->username."\x00"; + if($player->getUsername() != ""){ + $str .= $player->getUsername()."\x00"; } } $str .= "\x00"; diff --git a/src/network/raknet/RakNetCodec.php b/src/network/raknet/RakNetCodec.php deleted file mode 100644 index c0c908760..000000000 --- a/src/network/raknet/RakNetCodec.php +++ /dev/null @@ -1,185 +0,0 @@ -packet = $packet; - $this->buffer =& $this->packet->buffer; - $this->encode(); - } - - private function encode(){ - if(strlen($this->packet->buffer) > 0){ - return; - } - $this->buffer .= chr($this->packet->pid()); - - switch($this->packet->pid()){ - case RakNetInfo::OPEN_CONNECTION_REPLY_1: - $this->buffer .= RakNetInfo::MAGIC; - $this->putLong($this->packet->serverID); - $this->putByte(0); //server security - $this->putShort($this->packet->mtuSize); - break; - case RakNetInfo::OPEN_CONNECTION_REPLY_2: - $this->buffer .= RakNetInfo::MAGIC; - $this->putLong($this->packet->serverID); - $this->putShort($this->packet->port); - $this->putShort($this->packet->mtuSize); - $this->putByte(0); //Server security - break; - case RakNetInfo::INCOMPATIBLE_PROTOCOL_VERSION: - $this->putByte(RakNetInfo::STRUCTURE); - $this->buffer .= RakNetInfo::MAGIC; - $this->putLong($this->packet->serverID); - break; - case RakNetInfo::UNCONNECTED_PONG: - case RakNetInfo::ADVERTISE_SYSTEM: - $this->putLong($this->packet->pingID); - $this->putLong($this->packet->serverID); - $this->buffer .= RakNetInfo::MAGIC; - $this->putString($this->packet->serverType); - break; - case RakNetInfo::DATA_PACKET_0: - case RakNetInfo::DATA_PACKET_1: - case RakNetInfo::DATA_PACKET_2: - case RakNetInfo::DATA_PACKET_3: - case RakNetInfo::DATA_PACKET_4: - case RakNetInfo::DATA_PACKET_5: - case RakNetInfo::DATA_PACKET_6: - case RakNetInfo::DATA_PACKET_7: - case RakNetInfo::DATA_PACKET_8: - case RakNetInfo::DATA_PACKET_9: - case RakNetInfo::DATA_PACKET_A: - case RakNetInfo::DATA_PACKET_B: - case RakNetInfo::DATA_PACKET_C: - case RakNetInfo::DATA_PACKET_D: - case RakNetInfo::DATA_PACKET_E: - case RakNetInfo::DATA_PACKET_F: - $this->putLTriad($this->packet->seqNumber); - foreach($this->packet->data as $pk){ - $this->encodeDataPacket($pk); - } - break; - case RakNetInfo::NACK: - case RakNetInfo::ACK: - $payload = b""; - $records = 0; - $pointer = 0; - sort($this->packet->packets, SORT_NUMERIC); - $max = count($this->packet->packets); - - while($pointer < $max){ - $type = true; - $curr = $start = $this->packet->packets[$pointer]; - for($i = $start + 1; $i < $max; ++$i){ - $n = $this->packet->packets[$i]; - if(($n - $curr) === 1){ - $curr = $end = $n; - $type = false; - $pointer = $i + 1; - }else{ - break; - } - } - ++$pointer; - if($type === false){ - $payload .= "\x00"; - $payload .= strrev(Utils::writeTriad($start)); - $payload .= strrev(Utils::writeTriad($end)); - }else{ - $payload .= Utils::writeBool(true); - $payload .= strrev(Utils::writeTriad($start)); - } - ++$records; - } - $this->putShort($records); - $this->buffer .= $payload; - break; - default: - - } - - } - - private function encodeDataPacket(RakNetDataPacket $pk){ - $this->putByte(($pk->reliability << 5) | ($pk->hasSplit > 0 ? 0b00010000:0)); - $this->putShort(strlen($pk->buffer) << 3); - if($pk->reliability === 2 - or $pk->reliability === 3 - or $pk->reliability === 4 - or $pk->reliability === 6 - or $pk->reliability === 7){ - $this->putLTriad($pk->messageIndex); - } - - if($pk->reliability === 1 - or $pk->reliability === 3 - or $pk->reliability === 4 - or $pk->reliability === 7){ - $this->putLTriad($pk->orderIndex); - $this->putByte($pk->orderChannel); - } - - if($pk->hasSplit === true){ - $this->putInt($pk->splitCount); - $this->putShort($pk->splitID); - $this->putInt($pk->splitIndex); - } - - $this->buffer .= $pk->buffer; - } - - protected function put($str){ - $this->buffer .= $str; - } - - protected function putLong($v){ - $this->buffer .= Utils::writeLong($v); - } - - protected function putInt($v){ - $this->buffer .= Utils::writeInt($v); - } - - protected function putShort($v){ - $this->buffer .= Utils::writeShort($v); - } - - protected function putTriad($v){ - $this->buffer .= Utils::writeTriad($v); - } - - protected function putLTriad($v){ - $this->buffer .= strrev(Utils::writeTriad($v)); - } - - protected function putByte($v){ - $this->buffer .= chr($v); - } - - protected function putString($v){ - $this->putShort(strlen($v)); - $this->put($v); - } -} \ No newline at end of file diff --git a/src/network/raknet/RakNetDataPacket.php b/src/network/raknet/RakNetDataPacket.php index b2452f77c..9dc5f0d3d 100644 --- a/src/network/raknet/RakNetDataPacket.php +++ b/src/network/raknet/RakNetDataPacket.php @@ -152,7 +152,7 @@ abstract class RakNetDataPacket extends stdClass{ protected function putSlot(Item $item){ $this->putShort($item->getID()); - $this->putByte($item->count); + $this->putByte($item->getCount()); $this->putShort($item->getMetadata()); } diff --git a/src/network/raknet/RakNetPacket.php b/src/network/raknet/RakNetPacket.php index fdb310ef9..70b560df9 100644 --- a/src/network/raknet/RakNetPacket.php +++ b/src/network/raknet/RakNetPacket.php @@ -21,6 +21,8 @@ class RakNetPacket extends Packet{ private $packetID; + private $offset = 1; + public $data = array(); public function __construct($packetID){ $this->packetID = (int) $packetID; @@ -30,5 +32,487 @@ class RakNetPacket extends Packet{ return $this->packetID; } + private function get($len){ + if($len <= 0){ + $this->offset = strlen($this->buffer) - 1; + return ""; + } + if($len === true){ + return substr($this->buffer, $this->offset); + } + $this->offset += $len; + return substr($this->buffer, $this->offset - $len, $len); + } + + private function getLong($unsigned = false){ + return Utils::readLong($this->get(8), $unsigned); + } + + private function getInt(){ + return Utils::readInt($this->get(4)); + } + + private function getShort($unsigned = false){ + return Utils::readShort($this->get(2), $unsigned); + } + + private function getLTriad(){ + return Utils::readTriad(strrev($this->get(3))); + } + + private function getByte(){ + return ord($this->get(1)); + } + + private function feof(){ + return !isset($this->buffer{$this->offset}); + } + + public function decode(){ + $this->offset = 1; + switch($this->packetID){ + case RakNetInfo::UNCONNECTED_PING: + case RakNetInfo::UNCONNECTED_PING_OPEN_CONNECTIONS: + $this->pingID = $this->getLong(); + $this->offset += 16; //Magic + break; + case RakNetInfo::OPEN_CONNECTION_REQUEST_1: + $this->offset += 16; //Magic + $this->structure = $this->getByte(); + $this->mtuSize = strlen($this->get(true)); + break; + case RakNetInfo::OPEN_CONNECTION_REQUEST_2: + $this->offset += 16; //Magic + $this->security = $this->get(5); + $this->clientPort = $this->getShort(false); + $this->mtuSize = $this->getShort(false); + $this->clientID = $this->getLong(); + break; + case RakNetInfo::DATA_PACKET_0: + case RakNetInfo::DATA_PACKET_1: + case RakNetInfo::DATA_PACKET_2: + case RakNetInfo::DATA_PACKET_3: + case RakNetInfo::DATA_PACKET_4: + case RakNetInfo::DATA_PACKET_5: + case RakNetInfo::DATA_PACKET_6: + case RakNetInfo::DATA_PACKET_7: + case RakNetInfo::DATA_PACKET_8: + case RakNetInfo::DATA_PACKET_9: + case RakNetInfo::DATA_PACKET_A: + case RakNetInfo::DATA_PACKET_B: + case RakNetInfo::DATA_PACKET_C: + case RakNetInfo::DATA_PACKET_D: + case RakNetInfo::DATA_PACKET_E: + case RakNetInfo::DATA_PACKET_F: + $this->seqNumber = $this->getLTriad(); + $this->data = array(); + while(!$this->feof() and $this->parseDataPacket() !== false){ + + } + break; + case RakNetInfo::NACK: + case RakNetInfo::ACK: + $count = $this->getShort(); + $this->packets = array(); + for($i = 0; $i < $count and !$this->feof(); ++$i){ + if($this->getByte() === 0){ + $start = $this->getLTriad(); + $end = $this->getLTriad(); + if(($end - $start) > 4096){ + $end = $start + 4096; + } + for($c = $start; $c <= $end; ++$c){ + $this->packets[] = $c; + } + }else{ + $this->packets[] = $this->getLTriad(); + } + } + break; + default: + break; + } + } + + private function parseDataPacket(){ + $packetFlags = $this->getByte(); + $reliability = ($packetFlags & 0b11100000) >> 5; + $hasSplit = ($packetFlags & 0b00010000) > 0; + $length = (int) ceil($this->getShort() / 8); + if($reliability === 2 + or $reliability === 3 + or $reliability === 4 + or $reliability === 6 + or $reliability === 7){ + $messageIndex = $this->getLTriad(); + }else{ + $messageIndex = false; + } + + if($reliability === 1 + or $reliability === 3 + or $reliability === 4 + or $reliability === 7){ + $orderIndex = $this->getLTriad(); + $orderChannel = $this->getByte(); + }else{ + $orderIndex = false; + $orderChannel = false; + } + + if($hasSplit == true){ + $splitCount = $this->getInt(); + $splitID = $this->getShort(); + $splitIndex = $this->getInt(); + }else{ + $splitCount = false; + $splitID = false; + $splitIndex = false; + } + + if($length <= 0 + or $orderChannel >= 32 + or ($hasSplit === true and $splitIndex >= $splitCount)){ + return false; + }else{ + $pid = $this->getByte(); + $buffer = $this->get($length - 1); + if(strlen($buffer) < ($length - 1)){ + return false; + } + switch($pid){ + case ProtocolInfo::PING_PACKET: + $data = new PingPacket; + break; + case ProtocolInfo::PONG_PACKET: + $data = new PongPacket; + break; + case ProtocolInfo::CLIENT_CONNECT_PACKET: + $data = new ClientConnectPacket; + break; + case ProtocolInfo::SERVER_HANDSHAKE_PACKET: + $data = new ServerHandshakePacket; + break; + case ProtocolInfo::DISCONNECT_PACKET: + $data = new DisconnectPacket; + break; + case ProtocolInfo::LOGIN_PACKET: + $data = new LoginPacket; + break; + case ProtocolInfo::LOGIN_STATUS_PACKET: + $data = new LoginStatusPacket; + break; + case ProtocolInfo::READY_PACKET: + $data = new ReadyPacket; + break; + case ProtocolInfo::MESSAGE_PACKET: + $data = new MessagePacket; + break; + case ProtocolInfo::SET_TIME_PACKET: + $data = new SetTimePacket; + break; + case ProtocolInfo::START_GAME_PACKET: + $data = new StartGamePacket; + break; + case ProtocolInfo::ADD_MOB_PACKET: + $data = new AddMobPacket; + break; + case ProtocolInfo::ADD_PLAYER_PACKET: + $data = new AddPlayerPacket; + break; + case ProtocolInfo::REMOVE_PLAYER_PACKET: + $data = new RemovePlayerPacket; + break; + case ProtocolInfo::ADD_ENTITY_PACKET: + $data = new AddEntityPacket; + break; + case ProtocolInfo::REMOVE_ENTITY_PACKET: + $data = new RemoveEntityPacket; + break; + case ProtocolInfo::ADD_ITEM_ENTITY_PACKET: + $data = new AddItemEntityPacket; + break; + case ProtocolInfo::TAKE_ITEM_ENTITY_PACKET: + $data = new TakeItemEntityPacket; + break; + case ProtocolInfo::MOVE_ENTITY_PACKET: + $data = new MoveEntityPacket; + break; + case ProtocolInfo::MOVE_ENTITY_PACKET_POSROT: + $data = new MoveEntityPacket_PosRot; + break; + case ProtocolInfo::ROTATE_HEAD_PACKET: + $data = new RotateHeadPacket; + break; + case ProtocolInfo::MOVE_PLAYER_PACKET: + $data = new MovePlayerPacket; + break; + case ProtocolInfo::REMOVE_BLOCK_PACKET: + $data = new RemoveBlockPacket; + break; + case ProtocolInfo::UPDATE_BLOCK_PACKET: + $data = new UpdateBlockPacket; + break; + case ProtocolInfo::ADD_PAINTING_PACKET: + $data = new AddPaintingPacket; + break; + case ProtocolInfo::EXPLODE_PACKET: + $data = new ExplodePacket; + break; + case ProtocolInfo::LEVEL_EVENT_PACKET: + $data = new LevelEventPacket; + break; + case ProtocolInfo::TILE_EVENT_PACKET: + $data = new TileEventPacket; + break; + case ProtocolInfo::ENTITY_EVENT_PACKET: + $data = new EntityEventPacket; + break; + case ProtocolInfo::REQUEST_CHUNK_PACKET: + $data = new RequestChunkPacket; + break; + case ProtocolInfo::CHUNK_DATA_PACKET: + $data = new ChunkDataPacket; + break; + case ProtocolInfo::PLAYER_EQUIPMENT_PACKET: + $data = new PlayerEquipmentPacket; + break; + case ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET: + $data = new PlayerArmorEquipmentPacket; + break; + case ProtocolInfo::INTERACT_PACKET: + $data = new InteractPacket; + break; + case ProtocolInfo::USE_ITEM_PACKET: + $data = new UseItemPacket; + break; + case ProtocolInfo::PLAYER_ACTION_PACKET: + $data = new PlayerActionPacket; + break; + case ProtocolInfo::HURT_ARMOR_PACKET: + $data = new HurtArmorPacket; + break; + case ProtocolInfo::SET_ENTITY_DATA_PACKET: + $data = new SetEntityDataPacket; + break; + case ProtocolInfo::SET_ENTITY_MOTION_PACKET: + $data = new SetEntityMotionPacket; + break; + case ProtocolInfo::SET_HEALTH_PACKET: + $data = new SetHealthPacket; + break; + case ProtocolInfo::SET_SPAWN_POSITION_PACKET: + $data = new SetSpawnPositionPacket; + break; + case ProtocolInfo::ANIMATE_PACKET: + $data = new AnimatePacket; + break; + case ProtocolInfo::RESPAWN_PACKET: + $data = new RespawnPacket; + break; + case ProtocolInfo::SEND_INVENTORY_PACKET: + $data = new SendInventoryPacket; + break; + case ProtocolInfo::DROP_ITEM_PACKET: + $data = new DropItemPacket; + break; + case ProtocolInfo::CONTAINER_OPEN_PACKET: + $data = new ContainerOpenPacket; + break; + case ProtocolInfo::CONTAINER_CLOSE_PACKET: + $data = new ContainerClosePacket; + break; + case ProtocolInfo::CONTAINER_SET_SLOT_PACKET: + $data = new ContainerSetSlotPacket; + break; + case ProtocolInfo::CONTAINER_SET_DATA_PACKET: + $data = new ContainerSetDataPacket; + break; + case ProtocolInfo::CONTAINER_SET_CONTENT_PACKET: + $data = new ContainerSetContentPacket; + break; + case ProtocolInfo::CHAT_PACKET: + $data = new ChatPacket; + break; + case ProtocolInfo::ADVENTURE_SETTINGS_PACKET: + $data = new AdventureSettingsPacket; + break; + case ProtocolInfo::ENTITY_DATA_PACKET: + $data = new EntityDataPacket; + break; + default: + $data = new UnknownPacket(); + $data->packetID = $pid; + break; + } + $data->reliability = $reliability; + $data->hasSplit = $hasSplit; + $data->messageIndex = $messageIndex; + $data->orderIndex = $orderIndex; + $data->orderChannel = $orderChannel; + $data->splitCount = $splitCount; + $data->splitID = $splitID; + $data->splitIndex = $splitIndex; + $data->setBuffer($buffer); + $this->data[] = $data; + } + return true; + } + + public function encode(){ + if(strlen($this->buffer) > 0){ + return; + } + $this->buffer = chr($this->packetID); + + switch($this->packetID){ + case RakNetInfo::OPEN_CONNECTION_REPLY_1: + $this->buffer .= RakNetInfo::MAGIC; + $this->putLong($this->serverID); + $this->putByte(0); //server security + $this->putShort($this->mtuSize); + break; + case RakNetInfo::OPEN_CONNECTION_REPLY_2: + $this->buffer .= RakNetInfo::MAGIC; + $this->putLong($this->serverID); + $this->putShort($this->serverPort); + $this->putShort($this->mtuSize); + $this->putByte(0); //Server security + break; + case RakNetInfo::INCOMPATIBLE_PROTOCOL_VERSION: + $this->putByte(RakNetInfo::STRUCTURE); + $this->buffer .= RakNetInfo::MAGIC; + $this->putLong($this->serverID); + break; + case RakNetInfo::UNCONNECTED_PONG: + case RakNetInfo::ADVERTISE_SYSTEM: + $this->putLong($this->pingID); + $this->putLong($this->serverID); + $this->buffer .= RakNetInfo::MAGIC; + $this->putString($this->serverType); + break; + case RakNetInfo::DATA_PACKET_0: + case RakNetInfo::DATA_PACKET_1: + case RakNetInfo::DATA_PACKET_2: + case RakNetInfo::DATA_PACKET_3: + case RakNetInfo::DATA_PACKET_4: + case RakNetInfo::DATA_PACKET_5: + case RakNetInfo::DATA_PACKET_6: + case RakNetInfo::DATA_PACKET_7: + case RakNetInfo::DATA_PACKET_8: + case RakNetInfo::DATA_PACKET_9: + case RakNetInfo::DATA_PACKET_A: + case RakNetInfo::DATA_PACKET_B: + case RakNetInfo::DATA_PACKET_C: + case RakNetInfo::DATA_PACKET_D: + case RakNetInfo::DATA_PACKET_E: + case RakNetInfo::DATA_PACKET_F: + $this->putLTriad($this->seqNumber); + foreach($this->data as $pk){ + $this->encodeDataPacket($pk); + } + break; + case RakNetInfo::NACK: + case RakNetInfo::ACK: + $payload = b""; + $records = 0; + $pointer = 0; + sort($this->packets, SORT_NUMERIC); + $max = count($this->packets); + + while($pointer < $max){ + $type = true; + $curr = $start = $this->packets[$pointer]; + for($i = $start + 1; $i < $max; ++$i){ + $n = $this->packets[$i]; + if(($n - $curr) === 1){ + $curr = $end = $n; + $type = false; + $pointer = $i + 1; + }else{ + break; + } + } + ++$pointer; + if($type === false){ + $payload .= "\x00"; + $payload .= strrev(Utils::writeTriad($start)); + $payload .= strrev(Utils::writeTriad($end)); + }else{ + $payload .= Utils::writeBool(true); + $payload .= strrev(Utils::writeTriad($start)); + } + ++$records; + } + $this->putShort($records); + $this->buffer .= $payload; + break; + default: + + } + + } + + private function encodeDataPacket(RakNetDataPacket $pk){ + $this->putByte(($pk->reliability << 5) | ($pk->hasSplit > 0 ? 0b00010000:0)); + $this->putShort(strlen($pk->buffer) << 3); + if($pk->reliability === 2 + or $pk->reliability === 3 + or $pk->reliability === 4 + or $pk->reliability === 6 + or $pk->reliability === 7){ + $this->putLTriad($pk->messageIndex); + } + + if($pk->reliability === 1 + or $pk->reliability === 3 + or $pk->reliability === 4 + or $pk->reliability === 7){ + $this->putLTriad($pk->orderIndex); + $this->putByte($pk->orderChannel); + } + + if($pk->hasSplit === true){ + $this->putInt($pk->splitCount); + $this->putShort($pk->splitID); + $this->putInt($pk->splitIndex); + } + + $this->buffer .= $pk->buffer; + } + + protected function put($str){ + $this->buffer .= $str; + } + + protected function putLong($v){ + $this->buffer .= Utils::writeLong($v); + } + + protected function putInt($v){ + $this->buffer .= Utils::writeInt($v); + } + + protected function putShort($v){ + $this->buffer .= Utils::writeShort($v); + } + + protected function putTriad($v){ + $this->buffer .= Utils::writeTriad($v); + } + + protected function putLTriad($v){ + $this->buffer .= strrev(Utils::writeTriad($v)); + } + + protected function putByte($v){ + $this->buffer .= chr($v); + } + + protected function putString($v){ + $this->putShort(strlen($v)); + $this->put($v); + } + public function __destruct(){} } \ No newline at end of file diff --git a/src/network/raknet/RakNetParser.php b/src/network/raknet/RakNetParser.php deleted file mode 100644 index a8d99a2ca..000000000 --- a/src/network/raknet/RakNetParser.php +++ /dev/null @@ -1,367 +0,0 @@ -buffer =& $buffer; - $this->offset = 0; - if(strlen($this->buffer) > 0){ - $this->parse(); - }else{ - $this->packet = false; - } - } - - private function get($len){ - if($len <= 0){ - $this->offset = strlen($this->buffer) - 1; - return ""; - } - if($len === true){ - return substr($this->buffer, $this->offset); - } - $this->offset += $len; - return substr($this->buffer, $this->offset - $len, $len); - } - - private function getLong($unsigned = false){ - return Utils::readLong($this->get(8), $unsigned); - } - - private function getInt(){ - return Utils::readInt($this->get(4)); - } - - private function getShort($unsigned = false){ - return Utils::readShort($this->get(2), $unsigned); - } - - private function getLTriad(){ - return Utils::readTriad(strrev($this->get(3))); - } - - private function getByte(){ - return ord($this->get(1)); - } - - - private function feof(){ - return !isset($this->buffer{$this->offset}); - } - - private function parse(){ - $this->packet = new RakNetPacket(ord($this->get(1))); - $this->packet->buffer =& $this->buffer; - $this->packet->length = strlen($this->buffer); - switch($this->packet->pid()){ - case RakNetInfo::UNCONNECTED_PING: - case RakNetInfo::UNCONNECTED_PING_OPEN_CONNECTIONS: - $this->packet->pingID = $this->getLong(); - $this->offset += 16; //Magic - break; - case RakNetInfo::OPEN_CONNECTION_REQUEST_1: - $this->offset += 16; //Magic - $this->packet->structure = $this->getByte(); - $this->packet->mtuSize = strlen($this->get(true)); - break; - case RakNetInfo::OPEN_CONNECTION_REQUEST_2: - $this->offset += 16; //Magic - $this->packet->security = $this->get(5); - $this->packet->port = $this->getShort(false); - $this->packet->mtuSize = $this->getShort(false); - $this->packet->clientID = $this->getLong(); - break; - case RakNetInfo::DATA_PACKET_0: - case RakNetInfo::DATA_PACKET_1: - case RakNetInfo::DATA_PACKET_2: - case RakNetInfo::DATA_PACKET_3: - case RakNetInfo::DATA_PACKET_4: - case RakNetInfo::DATA_PACKET_5: - case RakNetInfo::DATA_PACKET_6: - case RakNetInfo::DATA_PACKET_7: - case RakNetInfo::DATA_PACKET_8: - case RakNetInfo::DATA_PACKET_9: - case RakNetInfo::DATA_PACKET_A: - case RakNetInfo::DATA_PACKET_B: - case RakNetInfo::DATA_PACKET_C: - case RakNetInfo::DATA_PACKET_D: - case RakNetInfo::DATA_PACKET_E: - case RakNetInfo::DATA_PACKET_F: - $this->packet->seqNumber = $this->getLTriad(); - $this->packet->data = array(); - while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){ - $this->packet->data[] = $pk; - } - break; - case RakNetInfo::NACK: - case RakNetInfo::ACK: - $count = $this->getShort(); - $this->packet->packets = array(); - for($i = 0; $i < $count and !$this->feof(); ++$i){ - if($this->getByte() === 0){ - $start = $this->getLTriad(); - $end = $this->getLTriad(); - if(($end - $start) > 4096){ - $end = $start + 4096; - } - for($c = $start; $c <= $end; ++$c){ - $this->packet->packets[] = $c; - } - }else{ - $this->packet->packets[] = $this->getLTriad(); - } - } - break; - default: - $this->packet = false; - break; - } - } - - private function parseDataPacket(){ - $packetFlags = $this->getByte(); - $reliability = ($packetFlags & 0b11100000) >> 5; - $hasSplit = ($packetFlags & 0b00010000) > 0; - $length = (int) ceil($this->getShort() / 8); - if($reliability === 2 - or $reliability === 3 - or $reliability === 4 - or $reliability === 6 - or $reliability === 7){ - $messageIndex = $this->getLTriad(); - }else{ - $messageIndex = false; - } - - if($reliability === 1 - or $reliability === 3 - or $reliability === 4 - or $reliability === 7){ - $orderIndex = $this->getLTriad(); - $orderChannel = $this->getByte(); - }else{ - $orderIndex = false; - $orderChannel = false; - } - - if($hasSplit == true){ - $splitCount = $this->getInt(); - $splitID = $this->getShort(); - $splitIndex = $this->getInt(); - }else{ - $splitCount = false; - $splitID = false; - $splitIndex = false; - } - - if($length <= 0 - or $orderChannel >= 32 - or ($hasSplit === true and $splitIndex >= $splitCount)){ - return false; - }else{ - $pid = $this->getByte(); - $buffer = $this->get($length - 1); - if(strlen($buffer) < ($length - 1)){ - return false; - } - switch($pid){ - case ProtocolInfo::PING_PACKET: - $data = new PingPacket; - break; - case ProtocolInfo::PONG_PACKET: - $data = new PongPacket; - break; - case ProtocolInfo::CLIENT_CONNECT_PACKET: - $data = new ClientConnectPacket; - break; - case ProtocolInfo::SERVER_HANDSHAKE_PACKET: - $data = new ServerHandshakePacket; - break; - case ProtocolInfo::DISCONNECT_PACKET: - $data = new DisconnectPacket; - break; - case ProtocolInfo::LOGIN_PACKET: - $data = new LoginPacket; - break; - case ProtocolInfo::LOGIN_STATUS_PACKET: - $data = new LoginStatusPacket; - break; - case ProtocolInfo::READY_PACKET: - $data = new ReadyPacket; - break; - case ProtocolInfo::MESSAGE_PACKET: - $data = new MessagePacket; - break; - case ProtocolInfo::SET_TIME_PACKET: - $data = new SetTimePacket; - break; - case ProtocolInfo::START_GAME_PACKET: - $data = new StartGamePacket; - break; - case ProtocolInfo::ADD_MOB_PACKET: - $data = new AddMobPacket; - break; - case ProtocolInfo::ADD_PLAYER_PACKET: - $data = new AddPlayerPacket; - break; - case ProtocolInfo::REMOVE_PLAYER_PACKET: - $data = new RemovePlayerPacket; - break; - case ProtocolInfo::ADD_ENTITY_PACKET: - $data = new AddEntityPacket; - break; - case ProtocolInfo::REMOVE_ENTITY_PACKET: - $data = new RemoveEntityPacket; - break; - case ProtocolInfo::ADD_ITEM_ENTITY_PACKET: - $data = new AddItemEntityPacket; - break; - case ProtocolInfo::TAKE_ITEM_ENTITY_PACKET: - $data = new TakeItemEntityPacket; - break; - case ProtocolInfo::MOVE_ENTITY_PACKET: - $data = new MoveEntityPacket; - break; - case ProtocolInfo::MOVE_ENTITY_PACKET_POSROT: - $data = new MoveEntityPacket_PosRot; - break; - case ProtocolInfo::ROTATE_HEAD_PACKET: - $data = new RotateHeadPacket; - break; - case ProtocolInfo::MOVE_PLAYER_PACKET: - $data = new MovePlayerPacket; - break; - case ProtocolInfo::REMOVE_BLOCK_PACKET: - $data = new RemoveBlockPacket; - break; - case ProtocolInfo::UPDATE_BLOCK_PACKET: - $data = new UpdateBlockPacket; - break; - case ProtocolInfo::ADD_PAINTING_PACKET: - $data = new AddPaintingPacket; - break; - case ProtocolInfo::EXPLODE_PACKET: - $data = new ExplodePacket; - break; - case ProtocolInfo::LEVEL_EVENT_PACKET: - $data = new LevelEventPacket; - break; - case ProtocolInfo::TILE_EVENT_PACKET: - $data = new TileEventPacket; - break; - case ProtocolInfo::ENTITY_EVENT_PACKET: - $data = new EntityEventPacket; - break; - case ProtocolInfo::REQUEST_CHUNK_PACKET: - $data = new RequestChunkPacket; - break; - case ProtocolInfo::CHUNK_DATA_PACKET: - $data = new ChunkDataPacket; - break; - case ProtocolInfo::PLAYER_EQUIPMENT_PACKET: - $data = new PlayerEquipmentPacket; - break; - case ProtocolInfo::PLAYER_ARMOR_EQUIPMENT_PACKET: - $data = new PlayerArmorEquipmentPacket; - break; - case ProtocolInfo::INTERACT_PACKET: - $data = new InteractPacket; - break; - case ProtocolInfo::USE_ITEM_PACKET: - $data = new UseItemPacket; - break; - case ProtocolInfo::PLAYER_ACTION_PACKET: - $data = new PlayerActionPacket; - break; - case ProtocolInfo::HURT_ARMOR_PACKET: - $data = new HurtArmorPacket; - break; - case ProtocolInfo::SET_ENTITY_DATA_PACKET: - $data = new SetEntityDataPacket; - break; - case ProtocolInfo::SET_ENTITY_MOTION_PACKET: - $data = new SetEntityMotionPacket; - break; - case ProtocolInfo::SET_HEALTH_PACKET: - $data = new SetHealthPacket; - break; - case ProtocolInfo::SET_SPAWN_POSITION_PACKET: - $data = new SetSpawnPositionPacket; - break; - case ProtocolInfo::ANIMATE_PACKET: - $data = new AnimatePacket; - break; - case ProtocolInfo::RESPAWN_PACKET: - $data = new RespawnPacket; - break; - case ProtocolInfo::SEND_INVENTORY_PACKET: - $data = new SendInventoryPacket; - break; - case ProtocolInfo::DROP_ITEM_PACKET: - $data = new DropItemPacket; - break; - case ProtocolInfo::CONTAINER_OPEN_PACKET: - $data = new ContainerOpenPacket; - break; - case ProtocolInfo::CONTAINER_CLOSE_PACKET: - $data = new ContainerClosePacket; - break; - case ProtocolInfo::CONTAINER_SET_SLOT_PACKET: - $data = new ContainerSetSlotPacket; - break; - case ProtocolInfo::CONTAINER_SET_DATA_PACKET: - $data = new ContainerSetDataPacket; - break; - case ProtocolInfo::CONTAINER_SET_CONTENT_PACKET: - $data = new ContainerSetContentPacket; - break; - case ProtocolInfo::CHAT_PACKET: - $data = new ChatPacket; - break; - case ProtocolInfo::ADVENTURE_SETTINGS_PACKET: - $data = new AdventureSettingsPacket; - break; - case ProtocolInfo::ENTITY_DATA_PACKET: - $data = new EntityDataPacket; - break; - default: - $data = new UnknownPacket(); - $data->packetID = $pid; - break; - } - $data->reliability = $reliability; - $data->hasSplit = $hasSplit; - $data->messageIndex = $messageIndex; - $data->orderIndex = $orderIndex; - $data->orderChannel = $orderChannel; - $data->splitCount = $splitCount; - $data->splitID = $splitID; - $data->splitIndex = $splitIndex; - $data->setBuffer($buffer); - } - return $data; - } - -} \ No newline at end of file diff --git a/src/tile/ContainerTileTrait.php b/src/tile/ContainerTileTrait.php index 32a35eed7..5e70a7bbd 100644 --- a/src/tile/ContainerTileTrait.php +++ b/src/tile/ContainerTileTrait.php @@ -62,7 +62,7 @@ trait ContainerTileTrait{ Player::broadcastPacket($all, $pk); for($s = 0; $s < ChestTile::SLOTS; ++$s){ $slot = $ob->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ + if($slot->getID() > AIR and $slot->getCount() > 0){ $slots[] = $slot; }else{ $slots[] = BlockAPI::getItem(AIR, 0, 0); @@ -79,7 +79,7 @@ trait ContainerTileTrait{ Player::broadcastPacket($this->level->getPlayers(), $pk); for($s = 0; $s < ChestTile::SLOTS; ++$s){ $slot = $this->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ + if($slot->getID() > AIR and $slot->getCount() > 0){ $slots[] = $slot; }else{ $slots[] = BlockAPI::getItem(AIR, 0, 0); @@ -109,7 +109,7 @@ trait ContainerTileTrait{ $slots = array(); for($s = 0; $s < FurnaceTile::SLOTS; ++$s){ $slot = $this->getSlot($s); - if($slot->getID() > AIR and $slot->count > 0){ + if($slot->getID() > AIR and $slot->getCount() > 0){ $slots[] = $slot; }else{ $slots[] = BlockAPI::getItem(AIR, 0, 0); @@ -144,14 +144,14 @@ trait ContainerTileTrait{ public function setSlot($s, Item $item, $update = true, $offset = 0){ $i = $this->getSlotIndex($s); $d = new NBTTag_Compound(false, array( - "Count" => new NBTTag_Byte("Count", $item->count), + "Count" => new NBTTag_Byte("Count", $item->getCount()), "Slot" => new NBTTag_Byte("Slot", $s), "id" => new NBTTag_Short("id", $item->getID()), "Damage" => new NBTTag_Short("Damage", $item->getMetadata()), )); if($i === false){ return false; - }elseif($item->getID() === AIR or $item->count <= 0){ + }elseif($item->getID() === AIR or $item->getCount() <= 0){ if($i >= 0){ unset($this->namedtag->Items[$i]); } diff --git a/src/tile/FurnaceTile.php b/src/tile/FurnaceTile.php index 8bbb60240..926163189 100644 --- a/src/tile/FurnaceTile.php +++ b/src/tile/FurnaceTile.php @@ -57,13 +57,13 @@ class FurnaceTile extends Tile{ $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->namedtag->BurnTime <= 0 and $canSmelt and $fuel->getFuelTime() !== false and $fuel->count > 0){ + $canSmelt = ($smelt !== false and $raw->getCount() > 0 and (($product->getID() === $smelt->getID() and $product->getMetadata() === $smelt->getMetadata() and $product->getCount() < $product->getMaxStackSize()) or $product->getID() === AIR)); + if($this->namedtag->BurnTime <= 0 and $canSmelt and $fuel->getFuelTime() !== false and $fuel->getCount() > 0){ $this->lastUpdate = microtime(true); $this->namedtag->MaxTime = $this->namedtag->BurnTime = floor($fuel->getFuelTime() * 20); $this->namedtag->BurnTicks = 0; - --$fuel->count; - if($fuel->count === 0){ + $fuel->setCount($fuel->getCount() - 1); + if($fuel->getCount() === 0){ $fuel = BlockAPI::getItem(AIR, 0, 0); } $this->setSlot(1, $fuel, false); @@ -79,10 +79,10 @@ class FurnaceTile extends Tile{ if($smelt !== false and $canSmelt){ $this->namedtag->CookTime += $ticks; if($this->namedtag->CookTime >= 200){ //10 seconds - $product = BlockAPI::getItem($smelt->getID(), $smelt->getMetadata(), $product->count + 1); + $product = BlockAPI::getItem($smelt->getID(), $smelt->getMetadata(), $product->getCount() + 1); $this->setSlot(2, $product, false); - --$raw->count; - if($raw->count === 0){ + $raw->setCount($raw->getCount() - 1); + if($raw->getCount() === 0){ $raw = BlockAPI::getItem(AIR, 0, 0); } $this->setSlot(0, $raw, false); diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 761e76a05..b46bf9d23 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -189,7 +189,7 @@ class Utils{ } public static function writeSlot(Item $item){ - return Utils::writeShort($item->getID()).chr($item->count).Utils::writeShort($item->getMetadata()); + return Utils::writeShort($item->getID()).chr($item->getCount()).Utils::writeShort($item->getMetadata()); } public static function readSlot($ob){ diff --git a/src/world/Level.php b/src/world/Level.php index 55cf89675..27b3dd91a 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -122,9 +122,7 @@ class Level{ continue; }else{ foreach($this->players as $p){ - if(isset($p->chunksLoaded[$index])){ - $p->chunksLoaded[$index] |= $mini; - } + $p->setChunkIndex($index, $mini); } unset($this->changedBlocks[$index][$Y]); } diff --git a/src/world/generator/noise/NoiseGeneratorPerlin.php b/src/world/generator/noise/NoiseGeneratorPerlin.php index 8fcf6a34c..ef53b42f6 100644 --- a/src/world/generator/noise/NoiseGeneratorPerlin.php +++ b/src/world/generator/noise/NoiseGeneratorPerlin.php @@ -61,9 +61,9 @@ class NoiseGeneratorPerlin extends NoiseGenerator{ $y += $this->offsetY; $z += $this->offsetZ; - $floorX = self::floor($x); - $floorY = self::floor($y); - $floorZ = self::floor($z); + $floorX = (int) floor($x); + $floorY = (int) floor($y); + $floorZ = (int) floor($z); $X = $floorX & 0xFF; $Y = $floorY & 0xFF; diff --git a/src/world/generator/noise/NoiseGeneratorSimplex.php b/src/world/generator/noise/NoiseGeneratorSimplex.php index 2989ba718..3498acbc3 100644 --- a/src/world/generator/noise/NoiseGeneratorSimplex.php +++ b/src/world/generator/noise/NoiseGeneratorSimplex.php @@ -81,9 +81,9 @@ class NoiseGeneratorSimplex extends NoiseGeneratorPerlin{ // Skew the input space to determine which simplex cell we're in $s = ($x + $y + $z) * self::$F3; // Very nice and simple skew factor for 3D - $i = self::floor($x + $s); - $j = self::floor($y + $s); - $k = self::floor($z + $s); + $i = (int) floor($x + $s); + $j = (int) floor($y + $s); + $k = (int) floor($z + $s); $t = ($i + $j + $k) * self::$G3; $X0 = $i - $t; // Unskew the cell origin back to (x,y,z) space $Y0 = $j - $t; @@ -214,8 +214,8 @@ class NoiseGeneratorSimplex extends NoiseGeneratorPerlin{ // Skew the input space to determine which simplex cell we're in $s = ($x + $y) * self::$F2; // Hairy factor for 2D - $i = self::floor($x + $s); - $j = self::floor($y + $s); + $i = (int) floor($x + $s); + $j = (int) floor($y + $s); $t = ($i + $j) * self::$G2; $X0 = $i - $t; // Unskew the cell origin back to (x,y) space $Y0 = $j - $t; diff --git a/src/world/generator/object/OreObject.php b/src/world/generator/object/OreObject.php index 8ad0c64f8..cc4c136e2 100644 --- a/src/world/generator/object/OreObject.php +++ b/src/world/generator/object/OreObject.php @@ -60,13 +60,19 @@ class OreObject{ $endZ = (int) ($seedZ + $size); for($x = $startX; $x <= $endX; ++$x){ - $sizeX = pow(($x + 0.5 - $seedX) / $size, 2); + $sizeX = ($x + 0.5 - $seedX) / $size; + $sizeX *= $sizeX; + if($sizeX < 1){ for($y = $startY; $y <= $endY; ++$y){ - $sizeY = pow(($y + 0.5 - $seedY) / $size, 2); + $sizeY = ($y + 0.5 - $seedY) / $size; + $sizeY *= $sizeY; + if($y > 0 and ($sizeX + $sizeY) < 1){ for($z = $startZ; $z <= $endZ; ++$z){ - $sizeZ = pow(($z + 0.5 - $seedZ) / $size, 2); + $sizeZ = ($z + 0.5 - $seedZ) / $size; + $sizeZ *= $sizeZ; + if(($sizeX + $sizeY + $sizeZ) < 1 and $level->level->getBlockID($x, $y, $z) === STONE){ $level->setBlockRaw(new Vector3($x, $y, $z), $this->type->material); }