server = ServerAPI::request(); foreach(static::$creative as $slot => [$id, $meta]){ $rind = ($id << 24) | ($meta); static::$reverseCreativeLookup[$rind] = $slot; } self::$creativeHotbarSlots[0] = static::hasCreativeItem(STONE, 0); self::$creativeHotbarSlots[1] = static::hasCreativeItem(COBBLESTONE, 0); self::$creativeHotbarSlots[2] = static::hasCreativeItem(DIRT, 0); self::$creativeHotbarSlots[3] = static::hasCreativeItem(PLANKS, 0); self::$creativeHotbarSlots[4] = static::hasCreativeItem(PLANKS, 1); self::$creativeHotbarSlots[5] = static::hasCreativeItem(TORCH, 0); self::$creativeHotbarSlots[6] = static::hasCreativeItem(BRICK_STAIRS, 0); self::$creativeHotbarSlots[7] = static::hasCreativeItem(COBBLE_WALL, 0); self::$creativeHotbarSlots[8] = static::hasCreativeItem(SAPLING, 0); } public static function get($id, $meta = 0, $v = false){ if(isset(Block::$class[$id])){ $classname = Block::$class[$id]; $b = new $classname($meta); }else{ $b = new GenericBlock((int) $id, $meta); } if($v instanceof Position){ $b->position($v); } return $b; } public function init(){ $this->server->schedule(1, [$this, "blockUpdateTick"], [], true); $this->server->api->console->register("setblock", " ", [$this, "commandHandler"]); $this->server->api->console->register("give", " [amount]", [$this, "commandHandler"]); $this->server->api->console->register("id", "Check id of item", [$this, "commandHandler"]); $this->server->api->console->cmdWhitelist("id"); } public function commandHandler($cmd, $args, $issuer, $alias){ $output = ""; switch($cmd){ case "id": if(!($issuer instanceof Player)){ $output .= "Run this command in-game!"; break; } $itemheld = $issuer->getSlot($issuer->slot); $output = self::getItem($itemheld->getID(), $itemheld->getMetadata()).""; break; case "setblock": if(!($issuer instanceof Player)){ if(count($args) < 5){ $output .= "Usage: /setblock "; break; }else{ if(!is_numeric($args[0]) or !is_numeric($args[1]) or !is_numeric($args[2])){ $output .= "You need to use a numeric coords!"; break; } $level = $this->server->api->level->get($args[3]); if($level === false){ $output .= "Unknown level"; break; } $block = self::fromString($args[4])->getBlock(); } }else{ if(count($args) < 4){ $output .= "Usage: /setblock "; break; } $level = $issuer->entity->level; $block = self::fromString($args[3])->getBlock(); } $coords = []; for($i = 0; $i < 3; $i++){ if($args[$i] === '~'){ if($i === 0) $coord = $issuer->entity->x; elseif($i === 1) $coord = $issuer->entity->y; else $coord = $issuer->entity->z; $coords[$i] = round($coord, 0); } elseif(is_numeric($args[$i])){ if($args[$i] >= 0 and $args[$i] <= ($i === 1 ? 127 : 255)){ $coords[$i] = $args[$i]; } } } if(!isset($coords[2])){ $output .= "Usage: /setblock "; break; } $pos = new Position($coords[0], $coords[1], $coords[2], $level); if(!($block instanceof Block)){ $output .= "Usage: /setblock "; break; }else{ $level->setBlock($pos, $block, true, false, true); $output .= "Placed $block in ".implode(", ", $coords).", w:".$level->getName(); } break; case "give": $player = $this->server->api->player->get($args[0] ?? ""); if($player instanceof Player){ $item = self::fromString($args[1] ?? ""); if(($player->gamemode & 0x01) === 0x01){ return "Player is in creative mode."; } if($item->getID() === 0){ return "You cannot give an air block to a player."; } if(!isset($args[2])){ $item->count = $item->getMaxStackSize(); }else{ $item->count = (int) $args[2]; } $player->addItem($item->getID(), $item->getMetadata(), $item->count); $output .= "Giving ".$item->count." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$player->username; break; }else{ $item = self::fromString($args[0] ?? ""); if(!($issuer instanceof Player)){ return "You cant give an item to a non-player."; } if(($issuer->gamemode & 0x01) === 0x01){ $output .= "You are in creative mode."; break; } if($item->getID() === 0){ $output .= "You cannot give an air block to a player."; break; } if(!isset($args[1])){ $item->count = $item->getMaxStackSize(); }else{ $item->count = (int) $args[1]; } $issuer->addItem($item->getID(), $item->getMetadata(), $item->count); $output .= "Giving ".$item->count." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$issuer->username; break; } } return $output; } public static function blockIDFromString($str){ if(defined(strtoupper($str))){ $ida = explode(":", constant(strtoupper($str))); if(count($ida) === 2){} return (int) $ida[0]; }else if(is_numeric($str)){ return (int) $str; } } public static function fromString($str, $multiple = false){ if($multiple === true){ $blocks = []; foreach(explode(",", $str) as $b){ $blocks[] = BlockAPI::fromString($b, false); } return $blocks; }else{ $b = explode(":", str_replace(" ", "_", trim($str))); if(!isset($b[1])){ $meta = 0; }else{ $meta = ((int) $b[1]) & 0xFFFF; } if(defined(strtoupper($b[0]))){ $explodedString = explode(":", constant(strtoupper($b[0]))); if(count($explodedString) === 2){ $meta = (int) $explodedString[1]; } $item = BlockAPI::getItem(constant(strtoupper($b[0])), $meta); if($item->getID() === AIR and strtoupper($b[0]) !== "AIR"){ $item = BlockAPI::getItem(((int) $b[0]) & 0xFFFF, $meta); } }else{ $item = BlockAPI::getItem(((int) $b[0]) & 0xFFFF, $meta); } return $item; } } public static function getItem($id, $meta = 0, $count = 1){ $id = (int) $id; if(isset(Item::$class[$id])){ $classname = Item::$class[$id]; $i = new $classname($meta, $count); }else{ $i = new Item($id, $meta, $count); } return $i; } public function playerBlockBreak(Player $player, Vector3 $vector){ $target = $player->level->getBlock($vector); $item = $player->getSlot($player->slot); if($this->server->api->dhandle("player.block.touch", ["type" => "break", "player" => $player, "target" => $target, "item" => $item]) === false){ if($this->server->api->dhandle("player.block.break.bypass", ["player" => $player, "target" => $target, "item" => $item]) !== true){ return $this->cancelAction($target, $player, false); } } $currentTime = microtime(true); $isIndestructible = (!$target->isBreakable($item, $player) && $this->server->api->dhandle("player.block.break.invalid", ["player" => $player, "target" => $target, "item" => $item]) !== true); $isAdventure = ($player->gamemode & 0x02) === 0x02; $progress = $target->getDestroyProgress($player); $tooFast = false; if($progress == 0 || is_nan($progress)){ $exceptedTimeMS = INF; $tooFast = true; }else{ $exceptedTimeMS = ceil(PocketMinecraftServer::$BLOCK_BREAKING_PROGRESS / $progress)*0.05; } if($player->gamemode == 1){ //creative $exceptedTimeMS = 0.05; //1 block/tick $tooFast = false; } $exceptedTimeAfter = $player->lastBreak + $exceptedTimeMS; if($currentTime < $exceptedTimeAfter) $tooFast = true; if($isIndestructible || $isAdventure || $tooFast){ if($this->server->api->dhandle("player.block.break.bypass", ["player" => $player, "target" => $target, "item" => $item]) !== true){ if($tooFast){ ConsoleAPI::warn("{$player->username} tried destroying block({$target->getID()} {$target->getMetadata()}) too fast($currentTime < $exceptedTimeAfter)"); } return $this->cancelAction($target, $player, false); } } $player->lastBreak = $currentTime; if($this->server->api->dhandle("player.block.break", ["player" => $player, "target" => $target, "item" => $item]) !== false){ $drops = $target->getDrops($item, $player); if($target->onBreak($item, $player) === false){ return $this->cancelAction($target, $player, false); } $item->mineBlock($target, $player); }else{ return $this->cancelAction($target, $player, false); } if(is_array($drops) && ($player->gamemode & 0x01) === 0x00 and count($drops) > 0){ foreach($drops as $drop){ $this->server->api->entity->drop(new Position($target->x + 0.5, $target->y, $target->z + 0.5, $target->level), BlockAPI::getItem($drop[0] & 0xFFFF, $drop[1] & 0xFFFF, $drop[2])); } } return false; } private function cancelAction(Block $block, Player $player, $send = true){ $player->addBlockUpdateIntoQueue($block->x, $block->y, $block->z, $block->getID(), $block->getMetadata()); if($send === true){ $player->sendInventory(); } return false; } public function playerBlockAction(Player $player, Vector3 $vector, $face, $fx, $fy, $fz){ if($face < 0 or $face > 5){ return false; } $target = $player->level->getBlock($vector); $block = $target->getSide($face); $item = $player->getHeldItem(); if($target->getID() === AIR and $this->server->api->dhandle("player.block.place.invalid", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) !== true){ //If no block exists or not allowed in CREATIVE if($this->server->api->dhandle("player.block.place.bypass", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) !== true){ $this->cancelAction($target, $player); return $this->cancelAction($block, $player); } } if($this->server->api->dhandle("player.block.touch", ["type" => "place", "player" => $player, "block" => $block, "target" => $target, "item" => $item]) === false){ if($this->server->api->dhandle("player.block.place.bypass", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) !== true){ return $this->cancelAction($block, $player); } } StaticBlock::getBlock($target->getID())::interact($target->level, $target->x, $target->y, $target->z, $player); if($target->isActivable === true){ if($this->server->api->dhandle("player.block.activate", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) !== false and $target->onActivate($item, $player) === true){ return false; } } if(($player->gamemode & 0x02) === 0x02){ //Adventure mode!! if($this->server->api->dhandle("player.block.place.bypass", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) !== true){ return $this->cancelAction($block, $player); } } if($block->y > 127 or $block->y < 0){ return false; } if($item->isActivable === true && $item->onActivate($player->level, $player, $block, $target, $face, $fx, $fy, $fz) === true){ return false; } if($item->isPlaceable()){ $hand = $item->getBlock(); $hand->position($block); }elseif($block->getID() === FIRE){ $player->level->fastSetBlockUpdate($block->x, $block->y, $block->z, 0, 0, true, false); return false; }else{ return $this->cancelAction($block, $player, false); } if(!($block->isReplaceable === true or ($hand->getID() === SLAB and $block->getID() === SLAB))){ return $this->cancelAction($block, $player); } if($target->isReplaceable === true){ $block = $target; $hand->position($block); //$face = -1; } if($hand->isSolid === true && ($hand->getID() != BED_BLOCK && $hand->getID() != CARPET)){ foreach($block->level->players as $pl){ $aabbs = $hand->getCollisionBoundingBoxes($block->level, $block->x, $block->y, $block->z, $pl->entity); foreach($aabbs as $aabb){ $playerbb = $pl->entity->boundingBox; if(($aabb->maxX > $playerbb->minX && $aabb->minX < $playerbb->maxX) && ($aabb->maxY > ($playerbb->minY+0.21) && $aabb->minY < $playerbb->maxY) && ($aabb->maxZ > $playerbb->minZ && $aabb->minZ < $playerbb->maxZ)){ return $this->cancelAction($block, $player); //Entity in block } } } } if($this->server->api->dhandle("player.block.place", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) === false){ return $this->cancelAction($block, $player); }elseif($hand->place($item, $player, $block, $target, $face, $fx, $fy, $fz) === false){ return $this->cancelAction($block, $player); } if($hand->getID() === SIGN_POST or $hand->getID() === WALL_SIGN){ $t = $this->server->api->tile->addSign($player->level, $block->x, $block->y, $block->z); $t->data["creator"] = $player->username; } $player->consumeSingleItem(); return false; } public function blockUpdate(Position $pos, $type = BLOCK_UPDATE_NORMAL){ $block = StaticBlock::getBlock($pos->level->level->getBlockID($pos->x, $pos->y, $pos->z)); if($block === false){ return false; } $level = $block::onUpdate($pos->level, $pos->x, $pos->y, $pos->z, $type); return $level; } public function blockUpdateAround(Position $pos, $type = BLOCK_UPDATE_NORMAL, $delay = false){ if($type == BLOCK_UPDATE_NORMAL){ try{ throw new Exception("Deprecated: tried updating $pos using BLOCK_UPDATE_NORMAL."); }catch(Exception $e){ ConsoleAPI::error($e->getMessage()); ConsoleAPI::error($e->getTraceAsString()); } return; } if($delay !== false){ $this->scheduleBlockUpdate($pos->getSide(0), $delay, $type); $this->scheduleBlockUpdate($pos->getSide(1), $delay, $type); $this->scheduleBlockUpdate($pos->getSide(2), $delay, $type); $this->scheduleBlockUpdate($pos->getSide(3), $delay, $type); $this->scheduleBlockUpdate($pos->getSide(4), $delay, $type); $this->scheduleBlockUpdate($pos->getSide(5), $delay, $type); }else{ $this->blockUpdate($pos->getSide(0), $type); $this->blockUpdate($pos->getSide(1), $type); $this->blockUpdate($pos->getSide(2), $type); $this->blockUpdate($pos->getSide(3), $type); $this->blockUpdate($pos->getSide(4), $type); $this->blockUpdate($pos->getSide(5), $type); } } public function removeAllBlockUpdates(Level $level){ $update = $this->server->query("SELECT x,y,z,level,type FROM blockUpdates WHERE level = '{$level->getName()}';"); if($update instanceof SQLite3Result){ while(($up = $update->fetchArray(SQLITE3_ASSOC)) !== false){ $index = $up["x"] . "." . $up["y"] . "." . $up["z"] . "." . $up["level"] . "." . $up["type"]; unset($this->scheduledUpdates[$index]); } $this->server->query("DELETE FROM blockUpdates WHERE level = '{$level->getName()}';"); } } public function scheduleBlockUpdateXYZ(Level $level, $x, $y, $z, $type = BLOCK_UPDATE_SCHEDULED, $delay = false){ $type = (int) $type; if($delay < 0){ return false; } $index = $x . "." . $y . "." . $z . "." . $level->getName() . "." . $type; $delay = microtime(true) + $delay * 0.05; if(!isset($this->scheduledUpdates[$index])){ $this->scheduledUpdates[$index] = new Position($x, $y, $z, $level); //TODO dont create a position $this->server->query("INSERT INTO blockUpdates (x, y, z, level, type, delay) VALUES ($x, $y, $z, '{$level->getName()}', $type, $delay);"); return true; } return false; } public function scheduleBlockUpdate(Position $pos, $delay, $type = BLOCK_UPDATE_SCHEDULED){ return $this->scheduleBlockUpdateXYZ($pos->level, $pos->x, $pos->y, $pos->z, $type, $delay); } public function blockUpdateTick(){ $time = microtime(true); if(count($this->scheduledUpdates) > 0){ $update = $this->server->query("SELECT x,y,z,level,type FROM blockUpdates WHERE delay <= " . $time . ";"); if($update instanceof SQLite3Result){ $upp = []; while(($up = $update->fetchArray(SQLITE3_ASSOC)) !== false){ $index = $up["x"] . "." . $up["y"] . "." . $up["z"] . "." . $up["level"] . "." . $up["type"]; if(isset($this->scheduledUpdates[$index])){ $upp[] = [(int) $up["type"], $this->scheduledUpdates[$index]]; unset($this->scheduledUpdates[$index]); } } $this->server->query("DELETE FROM blockUpdates WHERE delay <= " . $time . ";"); foreach($upp as $b){ $this->blockUpdate($b[1], $b[0]); } } } } public static function convertHighItemIdsToOldItemIds(int $protocolId, int $itemId) : int{ if ($protocolId >= ProtocolInfo12::CURRENT_PROTOCOL_12) { return $itemId; } $idMap = [ //default for protocol < 12 LIT_PUMPKIN => MELON_BLOCK, //block PUMPKIN_SEEDS => MELON_SEEDS, //item PUMPKIN_PIE => BREAD, //item BEETROOT => BREAD, //item BEETROOT_SEEDS => SEEDS, //item ]; if ($protocolId < ProtocolInfo9::CURRENT_PROTOCOL_9) { $idMap += [ NETHERRACK => OBSIDIAN, //block NETHER_BRICK => BRICK, //item NETHER_QUARTZ => BRICK, //item ]; } return $idMap[$itemId] ?? $itemId; } /** * @deprecated random updates are handled differently now. */ public function nextRandomUpdate(Position $pos){ } }