Esempio n. 1
0
 public function onActivate(Item $item, Player $player = null)
 {
     $tile = $this->getLevel()->getTile($this);
     if ($tile instanceof FlowerPotTile) {
         if ($tile->getFlowerPotItem() === Item::AIR) {
             switch ($item->getId()) {
                 case Item::TALL_GRASS:
                     if ($item->getDamage() === 1) {
                         break;
                     }
                 case Item::SAPLING:
                 case Item::DEAD_BUSH:
                 case Item::DANDELION:
                 case Item::RED_FLOWER:
                 case Item::BROWN_MUSHROOM:
                 case Item::RED_MUSHROOM:
                 case Item::CACTUS:
                     $tile->setFlowerPotData($item->getId(), $item->getDamage());
                     $this->setDamage($item->getDamage());
                     if ($player->isSurvival()) {
                         $item->count--;
                     }
                     return true;
                     break;
             }
         }
     }
     return false;
 }
Esempio n. 2
0
 public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz)
 {
     $targetBlock = Block::get($this->meta);
     if ($targetBlock instanceof Air) {
         if ($target instanceof Liquid and $target->getDamage() === 0) {
             $result = clone $this;
             $result->setDamage($target->getId());
             $player->getServer()->getPluginManager()->callEvent($ev = new PlayerBucketFillEvent($player, $block, $face, $this, $result));
             if (!$ev->isCancelled()) {
                 $player->getLevel()->setBlock($target, new Air(), true, true);
                 if ($player->isSurvival()) {
                     $player->getInventory()->setItemInHand($ev->getItem(), $player);
                 }
                 return true;
             } else {
                 $player->getInventory()->sendContents($player);
             }
         }
     } elseif ($targetBlock instanceof Liquid) {
         $result = clone $this;
         $result->setDamage(0);
         $player->getServer()->getPluginManager()->callEvent($ev = new PlayerBucketFillEvent($player, $block, $face, $this, $result));
         if (!$ev->isCancelled()) {
             $player->getLevel()->setBlock($block, $targetBlock, true, true);
             if ($player->isSurvival()) {
                 $player->getInventory()->setItemInHand($ev->getItem(), $player);
             }
             return true;
         } else {
             $player->getInventory()->sendContents($player);
         }
     }
     return false;
 }
Esempio n. 3
0
 public function __construct(Player $player, Block $block, Item $item, $instaBreak = false)
 {
     $this->block = $block;
     $this->item = $item;
     $this->player = $player;
     $this->instaBreak = (bool) $instaBreak;
     $drops = $player->isSurvival() ? $block->getDrops($item) : [];
     foreach ($drops as $i) {
         $this->blockDrops[] = Item::get($i[0], $i[1], $i[2]);
     }
 }
Esempio n. 4
0
 public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz)
 {
     $realPos = $block->getSide($face);
     $boat = new BoatEntity($player->getLevel()->getChunk($realPos->getX() >> 4, $realPos->getZ() >> 4), new Compound("", ["Pos" => new Enum("Pos", [new Double("", $realPos->getX()), new Double("", $realPos->getY()), new Double("", $realPos->getZ())]), "Motion" => new Enum("Motion", [new Double("", 0), new Double("", 0), new Double("", 0)]), "Rotation" => new Enum("Rotation", [new Float("", 0), new Float("", 0)])]));
     $boat->spawnToAll();
     if ($player->isSurvival()) {
         $item = $player->getInventory()->getItemInHand();
         $count = $item->getCount();
         if (--$count <= 0) {
             $player->getInventory()->setItemInHand(Item::get(Item::AIR));
             return;
         }
         $item->setCount($count);
         $player->getInventory()->setItemInHand($item);
     }
     return true;
 }
Esempio n. 5
0
 public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz)
 {
     $entity = null;
     $chunk = $level->getChunk($block->getX() >> 4, $block->getZ() >> 4);
     if (!$chunk instanceof FullChunk) {
         return false;
     }
     $nbt = new Compound("", ["Pos" => new Enum("Pos", [new Double("", $block->getX() + 0.5), new Double("", $block->getY()), new Double("", $block->getZ() + 0.5)]), "Motion" => new Enum("Motion", [new Double("", 0), new Double("", 0), new Double("", 0)]), "Rotation" => new Enum("Rotation", [new Float("", lcg_value() * 360), new Float("", 0)])]);
     if ($this->hasCustomName()) {
         $nbt->CustomName = new String("CustomName", $this->getCustomName());
     }
     $entity = Entity::createEntity($this->meta, $chunk, $nbt);
     if ($entity instanceof Entity) {
         $entity->setDataFlag(Entity::DATA_FLAGS, Entity::DATA_NO_AI, true);
         $entity->getLevel()->getServer()->broadcastPopup(TextFormat::RED . "Mob AI isn't implemented yet!");
         if ($player->isSurvival()) {
             --$this->count;
         }
         $entity->spawnToAll();
         return true;
     }
     return false;
 }
Esempio n. 6
0
 /**
  * Tries to break a block using a item, including Player time checks if available
  * It'll try to lower the durability if Item is a tool, and set it to Air if broken.
  *
  * @param Vector3 $vector
  * @param Item    &$item (if null, can break anything)
  * @param Player  $player
  * @param bool    $createParticles
  *
  * @return boolean
  */
 public function useBreakOn(Vector3 $vector, Item &$item = null, Player $player = null, $createParticles = false)
 {
     $target = $this->getBlock($vector);
     //TODO: Adventure mode checks
     if ($item === null) {
         $item = Item::get(Item::AIR, 0, 0);
     }
     if ($player !== null) {
         $ev = new BlockBreakEvent($player, $target, $item, $player->isCreative() ? true : false);
         if ($player->isSpectator() or !$player->isOp() and $player->isAdventure()) {
             $ev->setCancelled();
         }
         if ($item instanceof Tool) {
             $item->setDamage($item->getDamage() + $item->getDamageStep($target));
             $player->getInventory()->setItemInHand($item);
         }
         if ($player->isSurvival() and $item instanceof Item and !$target->isBreakable($item)) {
             $ev->setCancelled();
         }
         if (!$player->isOp() and ($distance = $this->server->getSpawnRadius()) > -1) {
             $t = new Vector2($target->x, $target->z);
             $s = new Vector2($this->getSpawnLocation()->x, $this->getSpawnLocation()->z);
             if ($t->distance($s) <= $distance) {
                 $ev->setCancelled();
             }
         }
         $this->server->getPluginManager()->callEvent($ev);
         if ($ev->isCancelled()) {
             return false;
         }
         $breakTime = $target->getBreakTime($item);
         if ($player->isCreative() and $breakTime > 0.15) {
             $breakTime = 0.15;
         }
         if ($player->hasEffect(Effect::SWIFTNESS)) {
             $breakTime *= 1 - 0.2 * ($player->getEffect(Effect::SWIFTNESS)->getAmplifier() + 1);
         }
         if ($player->hasEffect(Effect::MINING_FATIGUE)) {
             $breakTime *= 1 + 0.3 * ($player->getEffect(Effect::MINING_FATIGUE)->getAmplifier() + 1);
         }
         $breakTime -= 0.05;
         //1 tick compensation
         if (!$ev->getInstaBreak() and $player->lastBreak + $breakTime > microtime(true)) {
             return false;
         }
         $player->lastBreak = microtime(true);
         $drops = $ev->getDrops();
         if ($this->server->getProperty("experience.enable", true) and $this->server->getProperty("experience.break-drop", true) and $player->isSurvival()) {
             $exp = $target->getExperience();
             $this->spawnExperienceOrb($vector->add(0, 1, 0), $exp);
         }
     } elseif ($item !== null and !$target->isBreakable($item)) {
         return false;
     } else {
         $drops = $target->getDrops($item);
         //Fixes tile entities being deleted before getting drops
         foreach ($drops as $k => $i) {
             $drops[$k] = Item::get($i[0], $i[1], $i[2]);
         }
     }
     $tag = $item->getNamedTagEntry("CanDestroy");
     if ($tag instanceof Enum) {
         $canBreak = false;
         foreach ($tag as $v) {
             if ($v instanceof String) {
                 $entry = Item::fromString($v->getValue());
                 if ($entry->getId() > 0 and $entry->getBlock() !== null and $entry->getBlock()->getId() === $target->getId()) {
                     $canBreak = true;
                     break;
                 }
             }
         }
         if (!$canBreak) {
             return false;
         }
     }
     if ($createParticles) {
         $players = $this->getChunkPlayers($target->x >> 4, $target->z >> 4);
         if ($player !== null) {
             unset($players[$player->getLoaderId()]);
         }
         $this->addParticle(new DestroyBlockParticle($target->add(0.5), $target), $players);
     }
     $target->onBreak($item);
     $tile = $this->getTile($target);
     if ($tile !== null) {
         if ($tile instanceof InventoryHolder) {
             if ($tile instanceof Chest) {
                 $tile->unpair();
             }
             foreach ($tile->getInventory()->getContents() as $chestItem) {
                 $this->dropItem($target, $chestItem);
             }
         }
         $tile->close();
     }
     if ($item !== null) {
         $item->useOn($target);
         if ($item->isTool() and $item->getDamage() >= $item->getMaxDurability()) {
             $item = Item::get(Item::AIR, 0, 0);
         }
     }
     if ($player === null or $player->isSurvival()) {
         foreach ($drops as $drop) {
             if ($drop->getCount() > 0) {
                 $this->dropItem($vector->add(0.5, 0.5, 0.5), $drop);
             }
         }
     }
     return true;
 }