support php8019 and some cleanup (by @D-aHTuCT)

This commit is contained in:
kotyara 2022-07-26 16:06:34 +05:00
parent cf0f9df267
commit b60f0877e1
376 changed files with 6528 additions and 13472 deletions

View file

@ -1,233 +1,341 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class BlockAPI{
private $server;
private $scheduledUpdates = array();
private $randomUpdates = array();
public static $creative = array(
public static $creative = [
//Building
array(STONE, 0),
array(COBBLESTONE, 0),
array(STONE_BRICKS, 0),
array(STONE_BRICKS, 1),
array(STONE_BRICKS, 2),
array(MOSS_STONE, 0),
array(WOODEN_PLANKS, 0),
array(WOODEN_PLANKS, 1),
array(WOODEN_PLANKS, 2),
array(WOODEN_PLANKS, 3),
array(BRICKS, 0),
array(DIRT, 0),
array(GRASS, 0),
array(CLAY_BLOCK, 0),
array(SANDSTONE, 0),
array(SANDSTONE, 1),
array(SANDSTONE, 2),
array(SAND, 0),
array(GRAVEL, 0),
array(TRUNK, 0),
array(TRUNK, 1),
array(TRUNK, 2),
array(TRUNK, 3),
array(NETHER_BRICKS, 0),
array(NETHERRACK, 0),
array(BEDROCK, 0),
array(COBBLESTONE_STAIRS, 0),
array(OAK_WOODEN_STAIRS, 0),
array(SPRUCE_WOODEN_STAIRS, 0),
array(BIRCH_WOODEN_STAIRS, 0),
array(JUNGLE_WOODEN_STAIRS, 0),
array(BRICK_STAIRS, 0),
array(SANDSTONE_STAIRS, 0),
array(STONE_BRICK_STAIRS, 0),
array(NETHER_BRICKS_STAIRS, 0),
array(QUARTZ_STAIRS, 0),
array(SLAB, 0),
array(SLAB, 1),
array(WOODEN_SLAB, 0),
array(WOODEN_SLAB, 1),
array(WOODEN_SLAB, 2),
array(WOODEN_SLAB, 3),
array(SLAB, 3),
array(SLAB, 4),
array(SLAB, 5),
array(SLAB, 6),
array(QUARTZ_BLOCK, 0),
array(QUARTZ_BLOCK, 1),
array(QUARTZ_BLOCK, 2),
array(COAL_ORE, 0),
array(IRON_ORE, 0),
array(GOLD_ORE, 0),
array(DIAMOND_ORE, 0),
array(LAPIS_ORE, 0),
array(REDSTONE_ORE, 0),
array(OBSIDIAN, 0),
array(ICE, 0),
array(SNOW_BLOCK, 0),
[STONE, 0],
[COBBLESTONE, 0],
[STONE_BRICKS, 0],
[STONE_BRICKS, 1],
[STONE_BRICKS, 2],
[MOSS_STONE, 0],
[WOODEN_PLANKS, 0],
[WOODEN_PLANKS, 1],
[WOODEN_PLANKS, 2],
[WOODEN_PLANKS, 3],
[BRICKS, 0],
[DIRT, 0],
[GRASS, 0],
[CLAY_BLOCK, 0],
[SANDSTONE, 0],
[SANDSTONE, 1],
[SANDSTONE, 2],
[SAND, 0],
[GRAVEL, 0],
[TRUNK, 0],
[TRUNK, 1],
[TRUNK, 2],
[TRUNK, 3],
[NETHER_BRICKS, 0],
[NETHERRACK, 0],
[BEDROCK, 0],
[COBBLESTONE_STAIRS, 0],
[OAK_WOODEN_STAIRS, 0],
[SPRUCE_WOODEN_STAIRS, 0],
[BIRCH_WOODEN_STAIRS, 0],
[JUNGLE_WOODEN_STAIRS, 0],
[BRICK_STAIRS, 0],
[SANDSTONE_STAIRS, 0],
[STONE_BRICK_STAIRS, 0],
[NETHER_BRICKS_STAIRS, 0],
[QUARTZ_STAIRS, 0],
[SLAB, 0],
[SLAB, 1],
[WOODEN_SLAB, 0],
[WOODEN_SLAB, 1],
[WOODEN_SLAB, 2],
[WOODEN_SLAB, 3],
[SLAB, 3],
[SLAB, 4],
[SLAB, 5],
[SLAB, 6],
[QUARTZ_BLOCK, 0],
[QUARTZ_BLOCK, 1],
[QUARTZ_BLOCK, 2],
[COAL_ORE, 0],
[IRON_ORE, 0],
[GOLD_ORE, 0],
[DIAMOND_ORE, 0],
[LAPIS_ORE, 0],
[REDSTONE_ORE, 0],
[OBSIDIAN, 0],
[ICE, 0],
[SNOW_BLOCK, 0],
//Decoration
array(COBBLESTONE_WALL, 0),
array(COBBLESTONE_WALL, 1),
array(GOLD_BLOCK, 0),
array(IRON_BLOCK, 0),
array(DIAMOND_BLOCK, 0),
array(LAPIS_BLOCK, 0),
array(COAL_BLOCK, 0),
array(SNOW_LAYER, 0),
array(GLASS, 0),
array(GLOWSTONE_BLOCK, 0),
array(NETHER_REACTOR, 0),
array(WOOL, 0),
array(WOOL, 7),
array(WOOL, 6),
array(WOOL, 5),
array(WOOL, 4),
array(WOOL, 3),
array(WOOL, 2),
array(WOOL, 1),
array(WOOL, 15),
array(WOOL, 14),
array(WOOL, 13),
array(WOOL, 12),
array(WOOL, 11),
array(WOOL, 10),
array(WOOL, 9),
array(WOOL, 8),
array(LADDER, 0),
array(SPONGE, 0),
array(GLASS_PANE, 0),
array(WOODEN_DOOR, 0),
array(TRAPDOOR, 0),
array(FENCE, 0),
array(FENCE_GATE, 0),
array(IRON_BARS, 0),
array(BED, 0),
array(BOOKSHELF, 0),
array(PAINTING, 0),
array(WORKBENCH, 0),
array(STONECUTTER, 0),
array(CHEST, 0),
array(FURNACE, 0),
array(DANDELION, 0),
array(CYAN_FLOWER, 0),
array(BROWN_MUSHROOM, 0),
array(RED_MUSHROOM, 0),
array(CACTUS, 0),
array(MELON_BLOCK, 0),
array(PUMPKIN, 0),
array(LIT_PUMPKIN, 0),
array(COBWEB, 0),
array(HAY_BALE, 0),
array(TALL_GRASS, 1),
array(TALL_GRASS, 2),
array(DEAD_BUSH, 0),
array(SAPLING, 0),
array(SAPLING, 1),
array(SAPLING, 2),
array(SAPLING, 3),
array(LEAVES, 0),
array(LEAVES, 1),
array(LEAVES, 2),
array(LEAVES, 3),
array(CAKE, 0),
array(SIGN, 0),
array(CARPET, 0),
array(CARPET, 7),
array(CARPET, 6),
array(CARPET, 5),
array(CARPET, 4),
array(CARPET, 3),
array(CARPET, 2),
array(CARPET, 1),
array(CARPET, 15),
array(CARPET, 14),
array(CARPET, 13),
array(CARPET, 12),
array(CARPET, 11),
array(CARPET, 10),
array(CARPET, 9),
array(CARPET, 8),
[COBBLESTONE_WALL, 0],
[COBBLESTONE_WALL, 1],
[GOLD_BLOCK, 0],
[IRON_BLOCK, 0],
[DIAMOND_BLOCK, 0],
[LAPIS_BLOCK, 0],
[COAL_BLOCK, 0],
[SNOW_LAYER, 0],
[GLASS, 0],
[GLOWSTONE_BLOCK, 0],
[NETHER_REACTOR, 0],
[WOOL, 0],
[WOOL, 7],
[WOOL, 6],
[WOOL, 5],
[WOOL, 4],
[WOOL, 3],
[WOOL, 2],
[WOOL, 1],
[WOOL, 15],
[WOOL, 14],
[WOOL, 13],
[WOOL, 12],
[WOOL, 11],
[WOOL, 10],
[WOOL, 9],
[WOOL, 8],
[LADDER, 0],
[SPONGE, 0],
[GLASS_PANE, 0],
[WOODEN_DOOR, 0],
[TRAPDOOR, 0],
[FENCE, 0],
[FENCE_GATE, 0],
[IRON_BARS, 0],
[BED, 0],
[BOOKSHELF, 0],
[PAINTING, 0],
[WORKBENCH, 0],
[STONECUTTER, 0],
[CHEST, 0],
[FURNACE, 0],
[DANDELION, 0],
[CYAN_FLOWER, 0],
[BROWN_MUSHROOM, 0],
[RED_MUSHROOM, 0],
[CACTUS, 0],
[MELON_BLOCK, 0],
[PUMPKIN, 0],
[LIT_PUMPKIN, 0],
[COBWEB, 0],
[HAY_BALE, 0],
[TALL_GRASS, 1],
[TALL_GRASS, 2],
[DEAD_BUSH, 0],
[SAPLING, 0],
[SAPLING, 1],
[SAPLING, 2],
[SAPLING, 3],
[LEAVES, 0],
[LEAVES, 1],
[LEAVES, 2],
[LEAVES, 3],
[CAKE, 0],
[SIGN, 0],
[CARPET, 0],
[CARPET, 7],
[CARPET, 6],
[CARPET, 5],
[CARPET, 4],
[CARPET, 3],
[CARPET, 2],
[CARPET, 1],
[CARPET, 15],
[CARPET, 14],
[CARPET, 13],
[CARPET, 12],
[CARPET, 11],
[CARPET, 10],
[CARPET, 9],
[CARPET, 8],
//Tools
array(RAIL, 0),
array(POWERED_RAIL, 0),
array(TORCH, 0),
array(BUCKET, 0),
array(BUCKET, 8),
array(BUCKET, 10),
array(TNT, 0),
array(IRON_HOE, 0),
array(IRON_SWORD, 0),
array(BOW, 0),
array(SHEARS, 0),
array(FLINT_AND_STEEL, 0),
array(CLOCK, 0),
array(COMPASS, 0),
array(MINECART, 0),
array(SPAWN_EGG, MOB_CHICKEN),
array(SPAWN_EGG, MOB_COW),
array(SPAWN_EGG, MOB_PIG),
array(SPAWN_EGG, MOB_SHEEP),
array(SPAWN_EGG, MOB_ZOMBIE),
array(SPAWN_EGG, MOB_CREEPER),
array(SPAWN_EGG, MOB_SKELETON),
array(SPAWN_EGG, MOB_SPIDER),
array(SPAWN_EGG, MOB_PIGMAN),
[RAIL, 0],
[POWERED_RAIL, 0],
[TORCH, 0],
[BUCKET, 0],
[BUCKET, 8],
[BUCKET, 10],
[TNT, 0],
[IRON_HOE, 0],
[IRON_SWORD, 0],
[BOW, 0],
[SHEARS, 0],
[FLINT_AND_STEEL, 0],
[CLOCK, 0],
[COMPASS, 0],
[MINECART, 0],
[SPAWN_EGG, MOB_CHICKEN],
[SPAWN_EGG, MOB_COW],
[SPAWN_EGG, MOB_PIG],
[SPAWN_EGG, MOB_SHEEP],
[SPAWN_EGG, MOB_ZOMBIE],
[SPAWN_EGG, MOB_CREEPER],
[SPAWN_EGG, MOB_SKELETON],
[SPAWN_EGG, MOB_SPIDER],
[SPAWN_EGG, MOB_PIGMAN],
//Seeds
array(SUGARCANE, 0),
array(WHEAT, 0),
array(SEEDS, 0),
array(MELON_SEEDS, 0),
array(PUMPKIN_SEEDS, 0),
array(CARROT, 0),
array(POTATO, 0),
array(BEETROOT_SEEDS, 0),
array(EGG, 0),
array(DYE, 0),
array(DYE, 7),
array(DYE, 6),
array(DYE, 5),
array(DYE, 4),
array(DYE, 3),
array(DYE, 2),
array(DYE, 1),
array(DYE, 15),
array(DYE, 14),
array(DYE, 13),
array(DYE, 12),
array(DYE, 11),
array(DYE, 10),
array(DYE, 9),
array(DYE, 8),
);
[SUGARCANE, 0],
[WHEAT, 0],
[SEEDS, 0],
[MELON_SEEDS, 0],
[PUMPKIN_SEEDS, 0],
[CARROT, 0],
[POTATO, 0],
[BEETROOT_SEEDS, 0],
[EGG, 0],
[DYE, 0],
[DYE, 7],
[DYE, 6],
[DYE, 5],
[DYE, 4],
[DYE, 3],
[DYE, 2],
[DYE, 1],
[DYE, 15],
[DYE, 14],
[DYE, 13],
[DYE, 12],
[DYE, 11],
[DYE, 10],
[DYE, 9],
[DYE, 8],
];
private $server;
private $scheduledUpdates = [];
private $randomUpdates = [];
function __construct(){
$this->server = ServerAPI::request();
}
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", "<x> <y> <z> <block[:damage]>", [$this, "commandHandler"]);
$this->server->api->console->register("give", "<player> <item[:damage]> [amount]", [$this, "commandHandler"]);
}
public function commandHandler($cmd, $args, $issuer, $alias){
$output = "";
switch($cmd){
case "setblock":
if(!($issuer instanceof Player)){
$output .= "Please run this command in-game.\n";
break;
}
if(count($args) < 4){
$output .= "Usage: /setblock <x> <y> <z> <block[:damage]>\n";
break;
}
if($args[0] == '~')
$x = round($issuer->entity->x, 0);
elseif((int) $args[0] < 255)
$x = $args[0];
else{
$output .= "Usage: /setblock <x> <y> <z> <block[:damage]>\n";
break;
}
if($args[1] == '~')
$y = round($issuer->entity->y, 0);
elseif((int) $args[1] < 255)
$y = $args[1];
else{
$output .= "Usage: /setblock <x> <y> <z> <block[:damage]>\n";
break;
}
if($args[2] == '~')
$z = round($issuer->entity->z, 0);
elseif((int) $args[2] < 255)
$z = $args[2];
else{
$output .= "Usage: /setblock <x> <y> <z> <block[:damage]>\n";
break;
}
$pos = new Position($x, $y, $z, $issuer->entity->level);
$block = BlockAPI::fromString($args[3])->getBlock();
if(!($block instanceof Block)){
$output .= "Usage: /setblock <x> <y> <z> <block[:damage]>\n";
break;
}else{
$issuer->level->setBlock($pos, $block, true, false, true);
$output .= "Placed " . $block . " in " . $x . ", " . $y . ", " . $z;
}
break;
case "give":
if(count($args) == 1 and $issuer instanceof Player){
$item = BlockAPI::fromString($args[0]);
if(($issuer->gamemode & 0x01) === 0x01){
$output .= "You are in creative mode.\n";
break;
}
if($item->getID() == 0){
$output .= "You cannot give an air block to a player.\n";
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 . "\n";
break;
}
$player = $this->server->api->player->get($args[0]);
$item = BlockAPI::fromString($args[1]);
if(!isset($args[2])){
$item->count = $item->getMaxStackSize();
}else{
$item->count = (int) $args[2];
}
if($player instanceof Player){
if(($player->gamemode & 0x01) === 0x01){
$output .= "Player is in creative mode.\n";
break;
}
if($item->getID() == 0){
$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";
}else{
$output .= "Unknown player.\n";
}
break;
}
return $output;
}
public static function fromString($str, $multiple = false){
if($multiple === true){
$blocks = array();
foreach(explode(",",$str) as $b){
$blocks = [];
foreach(explode(",", $str) as $b){
$blocks[] = BlockAPI::fromString($b, false);
}
return $blocks;
@ -238,7 +346,7 @@ class BlockAPI{
}else{
$meta = ((int) $b[1]) & 0xFFFF;
}
if(defined(strtoupper($b[0]))){
$explodedString = explode(":", constant(strtoupper($b[0])));
if(count($explodedString) == 2){
@ -255,19 +363,6 @@ class BlockAPI{
}
}
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 static function getItem($id, $meta = 0, $count = 1){
$id = (int) $id;
if(isset(Item::$class[$id])){
@ -278,114 +373,44 @@ class BlockAPI{
}
return $i;
}
function __construct(){
$this->server = ServerAPI::request();
}
public function init(){
$this->server->schedule(1, array($this, "blockUpdateTick"), array(), true);
$this->server->api->console->register("setblock", "<x> <y> <z> <block[:damage]>", array($this, "commandHandler"));
$this->server->api->console->register("give", "<player> <item[:damage]> [amount]", array($this, "commandHandler"));
}
public function playerBlockBreak(Player $player, Vector3 $vector){
public function commandHandler($cmd, $args, $issuer, $alias){
$output = "";
switch($cmd){
case "setblock":
if(!($issuer instanceof Player)){
$output .= "Please run this command in-game.\n";
break;
}
if(count($args) < 4){
$output .= "Usage: /setblock <x> <y> <z> <block[:damage]>\n";
break;
}
if($args[0] == '~') $x = round($issuer->entity->x, 0);
elseif((int)$args[0] < 255) $x = $args[0];
else{
$output .= "Usage: /setblock <x> <y> <z> <block[:damage]>\n";
break;
}
if($args[1] == '~') $y = round($issuer->entity->y, 0);
elseif((int)$args[1] < 255) $y = $args[1];
else{
$output .= "Usage: /setblock <x> <y> <z> <block[:damage]>\n";
break;
}
if($args[2] == '~') $z = round($issuer->entity->z, 0);
elseif((int)$args[2] < 255) $z = $args[2];
else{
$output .= "Usage: /setblock <x> <y> <z> <block[:damage]>\n";
break;
}
$pos = new Position($x, $y, $z, $issuer->entity->level);
$block = BlockAPI::fromString($args[3])->getBlock();
if(!($block instanceof Block)){
$output .= "Usage: /setblock <x> <y> <z> <block[:damage]>\n";
break;
}
else{
$issuer->level->setBlock($pos, $block, true, false, true);
$output .= "Placed ".$block." in ".$x.", ".$y.", ".$z;
}
break;
case "give":
if(count($args) == 1 and $issuer instanceof Player){
$item = BlockAPI::fromString($args[0]);
if(($issuer->gamemode & 0x01) === 0x01){
$output .= "You are in creative mode.\n";
break;
}
if($item->getID() == 0){
$output .= "You cannot give an air block to a player.\n";
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."\n";
break;
}
$player = $this->server->api->player->get($args[0]);
$item = BlockAPI::fromString($args[1]);
if(!isset($args[2])){
$item->count = $item->getMaxStackSize();
}else{
$item->count = (int) $args[2];
}
$target = $player->level->getBlock($vector);
$item = $player->getSlot($player->slot);
if($player instanceof Player){
if(($player->gamemode & 0x01) === 0x01){
$output .= "Player is in creative mode.\n";
break;
}
if($item->getID() == 0) {
$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";
}else{
$output .= "Unknown player.\n";
}
break;
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);
}
}
return $output;
if((!$target->isBreakable($item, $player) and $this->server->api->dhandle("player.block.break.invalid", ["player" => $player, "target" => $target, "item" => $item]) !== true) or ($player->gamemode & 0x02) === 0x02 or (($player->lastBreak - $player->getLag() / 1000) + $target->getBreakTime($item, $player) - 0.2) >= microtime(true)){
if($this->server->api->dhandle("player.block.break.bypass", ["player" => $player, "target" => $target, "item" => $item]) !== true){
return $this->cancelAction($target, $player, false);
}
}
$player->lastBreak = microtime(true);
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);
}
if(($player->gamemode & 0x01) === 0 and $item->useOn($target) and $item->getMetadata() >= $item->getMaxDurability()){
$player->setSlot($player->slot, new Item(AIR, 0, 0), false);
}
}else{
return $this->cancelAction($target, $player, false);
}
if(($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){
@ -402,76 +427,37 @@ class BlockAPI{
return false;
}
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", array("type" => "break", "player" => $player, "target" => $target, "item" => $item)) === false){
if($this->server->api->dhandle("player.block.break.bypass", array("player" => $player, "target" => $target, "item" => $item)) !== true){
return $this->cancelAction($target, $player, false);
}
}
if((!$target->isBreakable($item, $player) and $this->server->api->dhandle("player.block.break.invalid", array("player" => $player, "target" => $target, "item" => $item)) !== true) or ($player->gamemode & 0x02) === 0x02 or (($player->lastBreak - $player->getLag() / 1000) + $target->getBreakTime($item, $player) - 0.2) >= microtime(true)){
if($this->server->api->dhandle("player.block.break.bypass", array("player" => $player, "target" => $target, "item" => $item)) !== true){
return $this->cancelAction($target, $player, false);
}
}
$player->lastBreak = microtime(true);
if($this->server->api->dhandle("player.block.break", array("player" => $player, "target" => $target, "item" => $item)) !== false){
$drops = $target->getDrops($item, $player);
if($target->onBreak($item, $player) === false){
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);
}
}else{
return $this->cancelAction($target, $player, false);
}
if(($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;
}
public function playerBlockAction(Player $player, Vector3 $vector, $face, $fx, $fy, $fz){
if($face < 0 or $face > 5){
return false;
}
$target = $player->level->getBlock($vector);
$target = $player->level->getBlock($vector);
$block = $target->getSide($face);
$item = $player->getSlot($player->slot);
if($target->getID() === AIR and $this->server->api->dhandle("player.block.place.invalid", array("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", array("player" => $player, "block" => $block, "target" => $target, "item" => $item)) !== true){
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", array("type" => "place", "player" => $player, "block" => $block, "target" => $target, "item" => $item)) === false){
if($this->server->api->dhandle("player.block.place.bypass", array("player" => $player, "block" => $block, "target" => $target, "item" => $item)) !== true){
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);
}
}
$this->blockUpdate($target, BLOCK_UPDATE_TOUCH);
if($target->isActivable === true){
if($this->server->api->dhandle("player.block.activate", array("player" => $player, "block" => $block, "target" => $target, "item" => $item)) !== false and $target->onActivate($item, $player) === 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", array("player" => $player, "block" => $block, "target" => $target, "item" => $item)) !== true){
if($this->server->api->dhandle("player.block.place.bypass", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) !== true){
return $this->cancelAction($block, $player, false);
}
}
@ -479,7 +465,7 @@ class BlockAPI{
if($block->y > 127 or $block->y < 0){
return false;
}
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);
@ -496,22 +482,22 @@ class BlockAPI{
}else{
return $this->cancelAction($block, $player, false);
}
if(!($block->isReplaceable === true or ($hand->getID() === SLAB and $block->getID() === SLAB))){
return $this->cancelAction($block, $player, false);
}
if($target->isReplaceable === true){
$block = $target;
$hand->position($block);
$hand->position($block);
//$face = -1;
}
if($hand->isSolid === true and $player->entity->inBlock($block)){
return $this->cancelAction($block, $player, false); //Entity in block
}
if($this->server->api->dhandle("player.block.place", array("player" => $player, "block" => $block, "target" => $target, "item" => $item)) === false){
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, false);
@ -531,7 +517,28 @@ class BlockAPI{
return false;
}
public function blockUpdateAround(Position $pos, $type = BLOCK_UPDATE_NORMAL, $delay = false){
public function blockUpdate(Position $pos, $type = BLOCK_UPDATE_NORMAL){
if(!($pos instanceof Block)){
$block = $pos->level->getBlock($pos);
}else{
$pos = new Position($pos->x, $pos->y, $pos->z, $pos->level);
$block = $pos->level->getBlock($pos);
}
if($block === false){
return false;
}
$level = $block->onUpdate($type);
if($level === BLOCK_UPDATE_NORMAL){
$this->blockUpdateAround($block, $level);
$this->server->api->entity->updateRadius($pos, 1);
}elseif($level === BLOCK_UPDATE_RANDOM){
$this->nextRandomUpdate($pos);
}
return $level;
}
public function blockUpdateAround(Position $pos, $type = BLOCK_UPDATE_NORMAL, $delay = false){
if($delay !== false){
$this->scheduleBlockUpdate($pos->getSide(0), $delay, $type);
$this->scheduleBlockUpdate($pos->getSide(1), $delay, $type);
@ -548,46 +555,25 @@ class BlockAPI{
$this->blockUpdate($pos->getSide(5), $type);
}
}
public function blockUpdate(Position $pos, $type = BLOCK_UPDATE_NORMAL){
if(!($pos instanceof Block)){
$block = $pos->level->getBlock($pos);
}else{
$pos = new Position($pos->x, $pos->y, $pos->z, $pos->level);
$block = $pos->level->getBlock($pos);
}
if($block === false){
return false;
}
$level = $block->onUpdate($type);
if($level === BLOCK_UPDATE_NORMAL){
$this->blockUpdateAround($block, $level);
$this->server->api->entity->updateRadius($pos, 1);
}elseif($level === BLOCK_UPDATE_RANDOM){
$this->nextRandomUpdate($pos);
}
return $level;
}
public function scheduleBlockUpdate(Position $pos, $delay, $type = BLOCK_UPDATE_SCHEDULED){
$type = (int) $type;
if($delay < 0){
return false;
}
$index = $pos->x.".".$pos->y.".".$pos->z.".".$pos->level->getName().".".$type;
$delay = microtime(true) + $delay * 0.05;
$index = $pos->x . "." . $pos->y . "." . $pos->z . "." . $pos->level->getName() . "." . $type;
$delay = microtime(true) + $delay * 0.05;
if(!isset($this->scheduledUpdates[$index])){
$this->scheduledUpdates[$index] = $pos;
$this->server->query("INSERT INTO blockUpdates (x, y, z, level, type, delay) VALUES (".$pos->x.", ".$pos->y.", ".$pos->z.", '".$pos->level->getName()."', ".$type.", ".$delay.");");
$this->server->query("INSERT INTO blockUpdates (x, y, z, level, type, delay) VALUES (" . $pos->x . ", " . $pos->y . ", " . $pos->z . ", '" . $pos->level->getName() . "', " . $type . ", " . $delay . ");");
return true;
}
return false;
}
public function nextRandomUpdate(Position $pos){
if(!isset($this->scheduledUpdates[$pos->x.".".$pos->y.".".$pos->z.".".$pos->level->getName().".".BLOCK_UPDATE_RANDOM])){
if(!isset($this->scheduledUpdates[$pos->x . "." . $pos->y . "." . $pos->z . "." . $pos->level->getName() . "." . BLOCK_UPDATE_RANDOM])){
$X = (($pos->x >> 4) << 4);
$Y = (($pos->y >> 4) << 4);
$Z = (($pos->z >> 4) << 4);
@ -596,7 +582,7 @@ class BlockAPI{
$offset = 0;
while(true){
$t = $offset + Utils::getRandomUpdateTicks() * 0.05;
$update = $this->server->query("SELECT COUNT(*) FROM blockUpdates WHERE level = '".$pos->level->getName()."' AND type = ".BLOCK_UPDATE_RANDOM." AND delay >= ".($time + $t - 1)." AND delay <= ".($time + $t + 1).";");
$update = $this->server->query("SELECT COUNT(*) FROM blockUpdates WHERE level = '" . $pos->level->getName() . "' AND type = " . BLOCK_UPDATE_RANDOM . " AND delay >= " . ($time + $t - 1) . " AND delay <= " . ($time + $t + 1) . ";");
if($update instanceof SQLite3Result){
$update = $update->fetchArray(SQLITE3_NUM);
if($update[0] < 3){
@ -610,21 +596,21 @@ class BlockAPI{
$this->scheduleBlockUpdate($pos, $t / 0.05, BLOCK_UPDATE_RANDOM);
}
}
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.";");
$update = $this->server->query("SELECT x,y,z,level,type FROM blockUpdates WHERE delay <= " . $time . ";");
if($update instanceof SQLite3Result){
$upp = array();
$upp = [];
while(($up = $update->fetchArray(SQLITE3_ASSOC)) !== false){
$index = $up["x"].".".$up["y"].".".$up["z"].".".$up["level"].".".$up["type"];
$index = $up["x"] . "." . $up["y"] . "." . $up["z"] . "." . $up["level"] . "." . $up["type"];
if(isset($this->scheduledUpdates[$index])){
$upp[] = array((int) $up["type"], $this->scheduledUpdates[$index]);
$upp[] = [(int) $up["type"], $this->scheduledUpdates[$index]];
unset($this->scheduledUpdates[$index]);
}
}
$this->server->query("DELETE FROM blockUpdates WHERE delay <= ".$time.";");
$this->server->query("DELETE FROM blockUpdates WHERE delay <= " . $time . ";");
foreach($upp as $b){
$this->blockUpdate($b[1], $b[0]);
}