mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-16 02:38:54 +00:00
Removed some copy-pasted code, fix leaves2 drops
This commit is contained in:
parent
3e141afe48
commit
21887b283f
@ -27,8 +27,8 @@ use pocketmine\event\block\LeavesDecayEvent;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\item\Tool;
|
||||
use pocketmine\level\Level;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\Player;
|
||||
use pocketmine\Server;
|
||||
|
||||
class Leaves extends Transparent{
|
||||
const OAK = 0;
|
||||
@ -39,6 +39,7 @@ class Leaves extends Transparent{
|
||||
const DARK_OAK = 1;
|
||||
|
||||
protected $id = self::LEAVES;
|
||||
protected $woodType = self::WOOD;
|
||||
|
||||
public function __construct($meta = 0){
|
||||
$this->meta = $meta;
|
||||
@ -66,18 +67,18 @@ class Leaves extends Transparent{
|
||||
return true;
|
||||
}
|
||||
|
||||
private function findLog(Block $pos, array $visited, $distance, &$check, $fromSide = null){
|
||||
protected function findLog(Block $pos, array $visited, $distance, &$check, $fromSide = null){
|
||||
++$check;
|
||||
$index = $pos->x . "." . $pos->y . "." . $pos->z;
|
||||
if(isset($visited[$index])){
|
||||
return false;
|
||||
}
|
||||
if($pos->getId() === self::WOOD){
|
||||
if($pos->getId() === $this->woodType){
|
||||
return true;
|
||||
}elseif($pos->getId() === self::LEAVES and $distance < 3){
|
||||
}elseif($pos->getId() === $this->id and $distance < 3){
|
||||
$visited[$index] = true;
|
||||
$down = $pos->getSide(0)->getId();
|
||||
if($down === Item::WOOD){
|
||||
$down = $pos->getSide(Vector3::SIDE_DOWN)->getId();
|
||||
if($down === $this->woodType){
|
||||
return true;
|
||||
}
|
||||
if($fromSide === null){
|
||||
@ -143,7 +144,7 @@ class Leaves extends Transparent{
|
||||
$visited = [];
|
||||
$check = 0;
|
||||
|
||||
Server::getInstance()->getPluginManager()->callEvent($ev = new LeavesDecayEvent($this));
|
||||
$this->getLevel()->getServer()->getPluginManager()->callEvent($ev = new LeavesDecayEvent($this));
|
||||
|
||||
if($ev->isCancelled() or $this->findLog($this, $visited, 0, $check) === true){
|
||||
$this->getLevel()->setBlock($this, $this, false, false);
|
||||
@ -166,7 +167,7 @@ class Leaves extends Transparent{
|
||||
public function getDrops(Item $item){
|
||||
$drops = [];
|
||||
if($item->isShears()){
|
||||
$drops[] = [Item::LEAVES, $this->meta & 0x03, 1];
|
||||
$drops[] = [$this->id, $this->meta & 0x03, 1];
|
||||
}else{
|
||||
if(mt_rand(1, 20) === 1){ //Saplings
|
||||
$drops[] = [Item::SAPLING, $this->meta & 0x03, 1];
|
||||
|
@ -32,10 +32,7 @@ use pocketmine\Server;
|
||||
class Leaves2 extends Leaves{
|
||||
|
||||
protected $id = self::LEAVES2;
|
||||
|
||||
public function __construct($meta = 0){
|
||||
$this->meta = $meta;
|
||||
}
|
||||
protected $woodType = self::WOOD2;
|
||||
|
||||
public function getName(){
|
||||
static $names = [
|
||||
@ -45,110 +42,13 @@ class Leaves2 extends Leaves{
|
||||
return $names[$this->meta & 0x01];
|
||||
}
|
||||
|
||||
private function findLog(Block $pos, array $visited, $distance, &$check, $fromSide = null){
|
||||
++$check;
|
||||
$index = $pos->x . "." . $pos->y . "." . $pos->z;
|
||||
if(isset($visited[$index])){
|
||||
return false;
|
||||
}
|
||||
if($pos->getId() === self::WOOD2){
|
||||
return true;
|
||||
}elseif($pos->getId() === self::LEAVES2 and $distance < 3){
|
||||
$visited[$index] = true;
|
||||
$down = $pos->getSide(0)->getId();
|
||||
if($down === Item::WOOD2){
|
||||
return true;
|
||||
}
|
||||
if($fromSide === null){
|
||||
for($side = 2; $side <= 5; ++$side){
|
||||
if($this->findLog($pos->getSide($side), $visited, $distance + 1, $check, $side) === true){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}else{ //No more loops
|
||||
switch($fromSide){
|
||||
case 2:
|
||||
if($this->findLog($pos->getSide(2), $visited, $distance + 1, $check, $fromSide) === true){
|
||||
return true;
|
||||
}elseif($this->findLog($pos->getSide(4), $visited, $distance + 1, $check, $fromSide) === true){
|
||||
return true;
|
||||
}elseif($this->findLog($pos->getSide(5), $visited, $distance + 1, $check, $fromSide) === true){
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if($this->findLog($pos->getSide(3), $visited, $distance + 1, $check, $fromSide) === true){
|
||||
return true;
|
||||
}elseif($this->findLog($pos->getSide(4), $visited, $distance + 1, $check, $fromSide) === true){
|
||||
return true;
|
||||
}elseif($this->findLog($pos->getSide(5), $visited, $distance + 1, $check, $fromSide) === true){
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
if($this->findLog($pos->getSide(2), $visited, $distance + 1, $check, $fromSide) === true){
|
||||
return true;
|
||||
}elseif($this->findLog($pos->getSide(3), $visited, $distance + 1, $check, $fromSide) === true){
|
||||
return true;
|
||||
}elseif($this->findLog($pos->getSide(4), $visited, $distance + 1, $check, $fromSide) === true){
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
if($this->findLog($pos->getSide(2), $visited, $distance + 1, $check, $fromSide) === true){
|
||||
return true;
|
||||
}elseif($this->findLog($pos->getSide(3), $visited, $distance + 1, $check, $fromSide) === true){
|
||||
return true;
|
||||
}elseif($this->findLog($pos->getSide(5), $visited, $distance + 1, $check, $fromSide) === true){
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function onUpdate($type){
|
||||
if($type === Level::BLOCK_UPDATE_NORMAL){
|
||||
if(($this->meta & 0b00001100) === 0){
|
||||
$this->meta |= 0x08;
|
||||
$this->getLevel()->setBlock($this, $this, true, false);
|
||||
}
|
||||
}elseif($type === Level::BLOCK_UPDATE_RANDOM){
|
||||
if(($this->meta & 0b00001100) === 0x08){
|
||||
$this->meta &= 0x03;
|
||||
$visited = [];
|
||||
$check = 0;
|
||||
|
||||
Server::getInstance()->getPluginManager()->callEvent($ev = new LeavesDecayEvent($this));
|
||||
|
||||
if($ev->isCancelled() or $this->findLog($this, $visited, 0, $check) === true){
|
||||
$this->getLevel()->setBlock($this, $this, false, false);
|
||||
}else{
|
||||
$this->getLevel()->useBreakOn($this);
|
||||
|
||||
return Level::BLOCK_UPDATE_NORMAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){
|
||||
$this->meta |= 0x04;
|
||||
$this->getLevel()->setBlock($this, $this, true);
|
||||
}
|
||||
|
||||
public function getDrops(Item $item){
|
||||
$drops = [];
|
||||
if($item->isShears()){
|
||||
$drops[] = [Item::LEAVES2, $this->meta & 0x03, 1];
|
||||
$drops[] = [$this->id, $this->meta & 0x01, 1];
|
||||
}else{
|
||||
if(mt_rand(1, 20) === 1){ //Saplings
|
||||
$drops[] = [Item::SAPLING, $this->meta & 0x03, 1];
|
||||
$drops[] = [Item::SAPLING, ($this->meta & 0x01) + 4, 1];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@ use pocketmine\item\Tool;
|
||||
use pocketmine\math\AxisAlignedBB;
|
||||
use pocketmine\Player;
|
||||
|
||||
class Slab extends Transparent{
|
||||
class Slab extends WoodSlab{
|
||||
const STONE = 0;
|
||||
const SANDSTONE = 1;
|
||||
const WOODEN = 2;
|
||||
@ -40,6 +40,8 @@ class Slab extends Transparent{
|
||||
|
||||
protected $id = self::SLAB;
|
||||
|
||||
protected $doubleId = self::DOUBLE_SLAB;
|
||||
|
||||
public function __construct($meta = 0){
|
||||
$this->meta = $meta;
|
||||
}
|
||||
@ -62,76 +64,8 @@ class Slab extends Transparent{
|
||||
return (($this->meta & 0x08) > 0 ? "Upper " : "") . $names[$this->meta & 0x07] . " Slab";
|
||||
}
|
||||
|
||||
protected function recalculateBoundingBox(){
|
||||
|
||||
if(($this->meta & 0x08) > 0){
|
||||
return new AxisAlignedBB(
|
||||
$this->x,
|
||||
$this->y + 0.5,
|
||||
$this->z,
|
||||
$this->x + 1,
|
||||
$this->y + 1,
|
||||
$this->z + 1
|
||||
);
|
||||
}else{
|
||||
return new AxisAlignedBB(
|
||||
$this->x,
|
||||
$this->y,
|
||||
$this->z,
|
||||
$this->x + 1,
|
||||
$this->y + 0.5,
|
||||
$this->z + 1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){
|
||||
$this->meta &= 0x07;
|
||||
if($face === 0){
|
||||
if($target->getId() === self::SLAB and ($target->getDamage() & 0x08) === 0x08 and ($target->getDamage() & 0x07) === ($this->meta & 0x07)){
|
||||
$this->getLevel()->setBlock($target, Block::get(Item::DOUBLE_SLAB, $this->meta), true);
|
||||
|
||||
return true;
|
||||
}elseif($block->getId() === self::SLAB and ($block->getDamage() & 0x07) === ($this->meta & 0x07)){
|
||||
$this->getLevel()->setBlock($block, Block::get(Item::DOUBLE_SLAB, $this->meta), true);
|
||||
|
||||
return true;
|
||||
}else{
|
||||
$this->meta |= 0x08;
|
||||
}
|
||||
}elseif($face === 1){
|
||||
if($target->getId() === self::SLAB and ($target->getDamage() & 0x08) === 0 and ($target->getDamage() & 0x07) === ($this->meta & 0x07)){
|
||||
$this->getLevel()->setBlock($target, Block::get(Item::DOUBLE_SLAB, $this->meta), true);
|
||||
|
||||
return true;
|
||||
}elseif($block->getId() === self::SLAB and ($block->getDamage() & 0x07) === ($this->meta & 0x07)){
|
||||
$this->getLevel()->setBlock($block, Block::get(Item::DOUBLE_SLAB, $this->meta), true);
|
||||
|
||||
return true;
|
||||
}
|
||||
//TODO: check for collision
|
||||
}else{
|
||||
if($block->getId() === self::SLAB){
|
||||
if(($block->getDamage() & 0x07) === ($this->meta & 0x07)){
|
||||
$this->getLevel()->setBlock($block, Block::get(Item::DOUBLE_SLAB, $this->meta), true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}else{
|
||||
if($fy > 0.5){
|
||||
$this->meta |= 0x08;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($block->getId() === self::SLAB and ($target->getDamage() & 0x07) !== ($this->meta & 0x07)){
|
||||
return false;
|
||||
}
|
||||
$this->getLevel()->setBlock($block, $this, true, true);
|
||||
|
||||
return true;
|
||||
public function getToolType(){
|
||||
return Tool::TYPE_PICKAXE;
|
||||
}
|
||||
|
||||
public function getDrops(Item $item){
|
||||
@ -143,9 +77,4 @@ class Slab extends Transparent{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function getToolType(){
|
||||
return Tool::TYPE_PICKAXE;
|
||||
}
|
||||
}
|
@ -32,6 +32,8 @@ class WoodSlab extends Transparent{
|
||||
|
||||
protected $id = self::WOOD_SLAB;
|
||||
|
||||
protected $doubleId = self::DOUBLE_WOODEN_SLAB;
|
||||
|
||||
public function __construct($meta = 0){
|
||||
$this->meta = $meta;
|
||||
}
|
||||
@ -80,31 +82,31 @@ class WoodSlab extends Transparent{
|
||||
public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){
|
||||
$this->meta &= 0x07;
|
||||
if($face === 0){
|
||||
if($target->getId() === self::WOOD_SLAB and ($target->getDamage() & 0x08) === 0x08 and ($target->getDamage() & 0x07) === ($this->meta & 0x07)){
|
||||
$this->getLevel()->setBlock($target, Block::get(Item::DOUBLE_WOOD_SLAB, $this->meta), true);
|
||||
if($target->getId() === $this->id and ($target->getDamage() & 0x08) === 0x08 and ($target->getDamage() & 0x07) === ($this->meta)){
|
||||
$this->getLevel()->setBlock($target, Block::get($this->doubleId, $this->meta), true);
|
||||
|
||||
return true;
|
||||
}elseif($block->getId() === self::WOOD_SLAB and ($block->getDamage() & 0x07) === ($this->meta & 0x07)){
|
||||
$this->getLevel()->setBlock($block, Block::get(Item::DOUBLE_WOOD_SLAB, $this->meta), true);
|
||||
}elseif($block->getId() === $this->id and ($block->getDamage() & 0x07) === ($this->meta)){
|
||||
$this->getLevel()->setBlock($block, Block::get($this->doubleId, $this->meta), true);
|
||||
|
||||
return true;
|
||||
}else{
|
||||
$this->meta |= 0x08;
|
||||
}
|
||||
}elseif($face === 1){
|
||||
if($target->getId() === self::WOOD_SLAB and ($target->getDamage() & 0x08) === 0 and ($target->getDamage() & 0x07) === ($this->meta & 0x07)){
|
||||
$this->getLevel()->setBlock($target, Block::get(Item::DOUBLE_WOOD_SLAB, $this->meta), true);
|
||||
if($target->getId() === $this->id and ($target->getDamage() & 0x08) === 0 and ($target->getDamage() & 0x07) === $this->meta){
|
||||
$this->getLevel()->setBlock($target, Block::get($this->doubleId, $this->meta), true);
|
||||
|
||||
return true;
|
||||
}elseif($block->getId() === self::WOOD_SLAB and ($block->getDamage() & 0x07) === ($this->meta & 0x07)){
|
||||
$this->getLevel()->setBlock($block, Block::get(Item::DOUBLE_WOOD_SLAB, $this->meta), true);
|
||||
}elseif($block->getId() === $this->id and ($block->getDamage() & 0x07) === $this->meta){
|
||||
$this->getLevel()->setBlock($block, Block::get($this->doubleId, $this->meta), true);
|
||||
|
||||
return true;
|
||||
}
|
||||
}else{ //TODO: collision
|
||||
if($block->getId() === self::WOOD_SLAB){
|
||||
if(($block->getDamage() & 0x07) === ($this->meta & 0x07)){
|
||||
$this->getLevel()->setBlock($block, Block::get(Item::DOUBLE_WOOD_SLAB, $this->meta), true);
|
||||
if($block->getId() === $this->id){
|
||||
if(($block->getDamage() & 0x07) === $this->meta){
|
||||
$this->getLevel()->setBlock($block, Block::get($this->doubleId, $this->meta), true);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -117,7 +119,7 @@ class WoodSlab extends Transparent{
|
||||
}
|
||||
}
|
||||
|
||||
if($block->getId() === self::WOOD_SLAB and ($target->getDamage() & 0x07) !== ($this->meta & 0x07)){
|
||||
if($block->getId() === $this->id and ($target->getDamage() & 0x07) !== ($this->meta & 0x07)){
|
||||
return false;
|
||||
}
|
||||
$this->getLevel()->setBlock($block, $this, true, true);
|
||||
|
Loading…
x
Reference in New Issue
Block a user