Added chest pairing

This commit is contained in:
Shoghi Cervantes
2013-08-27 12:38:00 +02:00
parent e8c273fc10
commit 176c371330
5 changed files with 252 additions and 131 deletions

View File

@ -31,50 +31,44 @@ class ChestBlock extends TransparentBlock{
$this->isActivable = true;
}
public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
$server = ServerAPI::request();
$faces = array(
0 => 4,
1 => 2,
2 => 5,
3 => 3,
);
$facesc = array(
2 => 4,
3 => 2,
4 => 5,
5 => 3,
);
$chest = false;
$this->meta = $faces[$player->entity->getDirection()];
for($side = 2; $side <= 5; ++$side){
if(($this->meta === 4 or $this->meta === 5) and ($side === 4 or $side === 5)){
continue;
}elseif(($this->meta === 3 or $this->meta === 2) and ($side === 2 or $side === 3)){
continue;
}
$c = $this->getSide($side);
if($c instanceof ChestBlock){
/*if($chest !== false){ //No chests in the middle
return false;
}*/
$chest = array($side, $c);
break;
if(($c instanceof ChestBlock) and $c->getMetadata() === $this->meta){
if((($tile = $server->api->tile->get($c)) instanceof Tile) and !$tile->isPaired()){
$chest = $tile;
break;
}
}
}
if($chest !== false and ($chest[1]->getSide($chest[0]) instanceof ChestBlock)){ //Already double chest
return false;
}
if($chest !== false){
$this->meta = $facesc[$chest[0]];
$this->level->setBlock($chest[1], new ChestBlock($this->meta));
}else{
$this->meta = $faces[$player->entity->getDirection()];
}
$this->level->setBlock($block, $this);
$server = ServerAPI::request();
$server->api->tile->add($this->level, TILE_CHEST, $this->x, $this->y, $this->z, array(
$tile = $server->api->tile->add($this->level, TILE_CHEST, $this->x, $this->y, $this->z, array(
"Items" => array(),
"id" => TILE_CHEST,
"x" => $this->x,
"y" => $this->y,
"z" => $this->z
));
if($chest instanceof Tile){
$chest->pairWith($tile);
$tile->pairWith($chest);
}
return true;
}
@ -104,40 +98,13 @@ class ChestBlock extends TransparentBlock{
));
}
if($chest->class !== TILE_CHEST or ($player->gamemode & 0x01) === 0x01){
if(($player->gamemode & 0x01) === 0x01){
return true;
}
$player->windowCnt++;
$player->windowCnt = $id = max(2, $player->windowCnt % 99);
$player->windows[$id] = $chest;
$player->dataPacket(MC_CONTAINER_OPEN, array(
"windowid" => $id,
"type" => WINDOW_CHEST,
"slots" => CHEST_SLOTS,
"title" => "Chest",
));
$server->api->player->broadcastPacket($server->api->player->getAll($this->level), MC_TILE_EVENT, array(
"x" => $this->x,
"y" => $this->y,
"z" => $this->z,
"case1" => 1,
"case2" => 2,
));
$slots = array();
for($s = 0; $s < CHEST_SLOTS; ++$s){
$slot = $chest->getSlot($s);
if($slot->getID() > AIR and $slot->count > 0){
$slots[] = $slot;
}else{
$slots[] = BlockAPI::getItem(AIR, 0, 0);
}
}
$player->dataPacket(MC_CONTAINER_SET_CONTENT, array(
"windowid" => $id,
"count" => count($slots),
"slots" => $slots
));
$chest->openInventory($player);
return true;
}