Esempio n. 1
0
 public static function setCustomColorToArmor(Item $item, Color $color)
 {
     if ($hasTag = $item->hasCompoundTag()) {
         $tag = $item->getNamedTag();
     } else {
         $tag = new CompoundTag("", array());
     }
     $tag->customColor = new IntTag("customColor", $color->getColorCode());
     $item->setCompoundTag($tag);
     //
 }
 public function getCustomColor()
 {
     //
     if ($this->isCustomColor()) {
         $color = $this->namedtag["CustomColor"];
         $green = $color >> 8 & 0xff;
         $red = $color >> 16 & 0xff;
         $blue = $color & 0xff;
         return Color::getRGB($red, $green, $blue);
     }
     return null;
 }
 public function onActivate(Item $item, Player $player = null)
 {
     //long...
     $tile = $this->getLevel()->getTile($this);
     if (!$tile instanceof Cauldron) {
         return false;
     }
     switch ($item->getId()) {
         case Item::BUCKET:
             if ($item->getDamage() === 0) {
                 //empty bucket
                 if (!$this->isFull() or $tile->isCustomColor() or $tile->hasPotion()) {
                     break;
                 }
                 $bucket = clone $item;
                 $bucket->setDamage(8);
                 //water bucket
                 Server::getInstance()->getPluginManager()->callEvent($ev = new PlayerBucketFillEvent($player, $this, 0, $item, $bucket));
                 if (!$ev->isCancelled()) {
                     if ($player->isSurvival()) {
                         $player->getInventory()->setItemInHand($ev->getItem(), $player);
                     }
                     $this->meta = 0;
                     //empty
                     $this->getLevel()->setBlock($this, $this, true);
                     $tile->clearCustomColor();
                     $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5)));
                 }
             } elseif ($item->getDamage() === 8) {
                 //water bucket
                 if ($this->isFull() and !$tile->isCustomColor() and !$tile->hasPotion()) {
                     break;
                 }
                 $bucket = clone $item;
                 $bucket->setDamage(0);
                 //empty bucket
                 Server::getInstance()->getPluginManager()->callEvent($ev = new PlayerBucketEmptyEvent($player, $this, 0, $item, $bucket));
                 if (!$ev->isCancelled()) {
                     if ($player->isSurvival()) {
                         $player->getInventory()->setItemInHand($ev->getItem(), $player);
                     }
                     if ($tile->hasPotion()) {
                         //if has potion
                         $this->meta = 0;
                         //empty
                         $this->getLevel()->setBlock($this, $this, true);
                         $tile->setPotionId(0xffff);
                         //reset potion
                         $tile->clearCustomColor();
                         $this->getLevel()->addSound(new ExplodeSound($this->add(0.5, 0, 0.5)));
                     } else {
                         $this->meta = 6;
                         //fill
                         $this->getLevel()->setBlock($this, $this, true);
                         $tile->clearCustomColor();
                         $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5)));
                     }
                     $this->update();
                 }
             }
             break;
         case Item::DYE:
             if ($tile->hasPotion()) {
                 break;
             }
             $color = Color::getDyeColor($item->getDamage());
             if ($tile->isCustomColor()) {
                 $color = Color::averageColor($color, $tile->getCustomColor());
             }
             if ($player->isSurvival()) {
                 $item->setCount($item->getCount() - 1);
                 /*if($item->getCount() <= 0){
                 			$player->getInventory()->setItemInHand(Item::get(Item::AIR));
                 		}*/
             }
             $tile->setCustomColor($color);
             $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5)));
             $this->update();
             break;
         case Item::LEATHER_CAP:
         case Item::LEATHER_TUNIC:
         case Item::LEATHER_PANTS:
         case Item::LEATHER_BOOTS:
             if ($this->isEmpty()) {
                 break;
             }
             if ($tile->isCustomColor()) {
                 --$this->meta;
                 $this->getLevel()->setBlock($this, $this, true);
                 $newItem = clone $item;
                 Utils::setCustomColorToArmor($newItem, $tile->getCustomColor());
                 $player->getInventory()->setItemInHand($newItem);
                 $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5)));
                 if ($this->isEmpty()) {
                     $tile->clearCustomColor();
                 }
             } else {
                 --$this->meta;
                 $this->getLevel()->setBlock($this, $this, true);
                 $newItem = clone $item;
                 Utils::clearCustomColorToArmor($newItem);
                 $player->getInventory()->setItemInHand($newItem);
                 $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5)));
             }
             break;
         case Item::POTION:
         case Item::SPLASH_POTION:
             if (!$this->isEmpty() and ($tile->getPotionId() !== $item->getDamage() and $item->getDamage() !== Potion::WATER_BOTTLE or $item->getId() === Item::POTION and $tile->getSplashPotion() or $item->getId() === Item::SPLASH_POTION and !$tile->getSplashPotion() or $item->getDamage() === Potion::WATER_BOTTLE and $tile->hasPotion())) {
                 //long...
                 $this->meta = 0x0;
                 $this->getLevel()->setBlock($this, $this, true);
                 $tile->setPotionId(0xffff);
                 //reset
                 $tile->setSplashPotion(false);
                 if ($player->isSurvival()) {
                     $player->getInventory()->setItemInHand(Item::get(Item::GLASS_BOTTLE));
                 }
                 $this->getLevel()->addSound(new ExplodeSound($this->add(0.5, 0, 0.5)));
             } elseif ($item->getDamage() === Potion::WATER_BOTTLE) {
                 //water bottle
                 $this->meta += 2;
                 if ($this->meta > 0x6) {
                     $this->meta = 0x6;
                 }
                 $this->getLevel()->setBlock($this, $this, true);
                 if ($player->isSurvival()) {
                     $player->getInventory()->setItemInHand(Item::get(Item::GLASS_BOTTLE));
                 }
                 $tile->setPotionId(0xffff);
                 $tile->setSplashPotion(false);
                 $tile->clearCustomColor();
                 $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5)));
             } elseif (!$this->isFull()) {
                 $this->meta += 2;
                 if ($this->meta > 0x6) {
                     $this->meta = 0x6;
                 }
                 $this->getLevel()->setBlock($this, $this, true);
                 $tile->setPotionId($item->getDamage());
                 $tile->setSplashPotion($item->getId() === Item::SPLASH_POTION);
                 $tile->clearCustomColor();
                 if ($player->isSurvival()) {
                     $player->getInventory()->setItemInHand(Item::get(Item::GLASS_BOTTLE));
                 }
                 $color = Potion::getColor($item->getDamage());
                 $this->getLevel()->addSound(new SpellSound($this->add(0.5, 1, 0.5), $color[0], $color[1], $color[2]));
             }
             break;
         case Item::GLASS_BOTTLE:
             if ($this->meta < 2) {
                 break;
             }
             if ($tile->hasPotion()) {
                 $this->meta -= 2;
                 if ($this->meta < 0x0) {
                     $this->meta = 0x0;
                 }
                 $this->getLevel()->setBlock($this, $this, true);
                 if ($player->isSurvival()) {
                     $result = Item::get(Item::POTION, $tile->getPotionId());
                     if ($item->getCount() === 1) {
                         $player->getInventory()->setItemInHand($result);
                     } else {
                         $player->getInventory()->addItem($result);
                     }
                 }
                 if ($this->isEmpty()) {
                     $tile->setPotionId(0xffff);
                     //reset
                     $tile->setSplashPotion(false);
                 }
                 $color = Potion::getColor($result->getDamage());
                 $this->getLevel()->addSound(new SpellSound($this->add(0.5, 1, 0.5), $color[0], $color[1], $color[2]));
             } else {
                 $this->meta -= 2;
                 if ($this->meta < 0x0) {
                     $this->meta = 0x0;
                 }
                 $this->getLevel()->setBlock($this, $this, true);
                 if ($player->isSurvival()) {
                     $result = Item::get(Item::POTION, Potion::WATER_BOTTLE);
                     if ($item->getCount() > 1 and $player->getInventory()->canAddItem($result)) {
                         $player->getInventory()->addItem($result);
                     } else {
                         $player->getInventory()->setItemInHand($result);
                     }
                 }
                 $this->getLevel()->addSound(new GraySplashSound($this->add(0.5, 1, 0.5)));
             }
             break;
     }
     return true;
 }
 public function onEnable()
 {
     //flower pot
     //add item
     $this->registerItem(self::ITEM_FLOWER_POT, ItemFlowerPot::class);
     //add block
     $this->registerBlock(self::BLOCK_FLOWER_POT, BlockFlowerPot::class);
     //add block entity(tile)
     Tile::registerTile(FlowerPot::class);
     //add to creative item
     $this->addCreativeItem(Item::get(self::ITEM_FLOWER_POT, 0));
     //extra: skull
     //add item
     $this->registerItem(self::ITEM_SKULL, ItemSkull::class);
     //add block
     $this->registerBlock(self::BLOCK_SKULL, BlockSkull::class);
     //add block entity(tile)
     Tile::registerTile(Skull::class);
     //add to creative item
     $this->addCreativeItem(Item::get(self::ITEM_SKULL, 0));
     $this->addCreativeItem(Item::get(self::ITEM_SKULL, 1));
     $this->addCreativeItem(Item::get(self::ITEM_SKULL, 2));
     $this->addCreativeItem(Item::get(self::ITEM_SKULL, 3));
     $this->addCreativeItem(Item::get(self::ITEM_SKULL, 4));
     //extra: note block
     //add item(block)
     $this->registerItem(self::BLOCK_NOTE, BlockNote::class);
     //add block
     $this->registerBlock(self::BLOCK_NOTE, BlockNote::class);
     //add block entity(tile)
     Tile::registerTile(Note::class);
     //add creative item
     $this->addCreativeItem(Item::get(self::BLOCK_NOTE, 0));
     //extra: item frame
     //add block
     $this->registerBlock(self::BLOCK_ITEM_FRAME, BlockItemFrame::class);
     //add item
     $this->registerItem(self::ITEM_ITEM_FRAME, ItemItemFrame::class);
     //add block entity(tile)
     Tile::registerTile(ItemFrame::class);
     //add to creative item
     $this->addCreativeItem(Item::get(self::ITEM_ITEM_FRAME, 0));
     //add drop packet to network
     Server::getInstance()->getNetWork()->registerPacket(MainClass::PROTOCOL_ITEM_FRAME_DROP_ITEM_PACKET, ItemFrameDropPacket::class);
     //extra: Cauldron
     //add item
     $this->registerItem(self::ITEM_CAULDRON, ItemCauldron::class);
     //add block
     $this->registerBlock(self::BLOCK_CAULDRON, BlockCauldron::class);
     //add block entity(tile)
     Tile::registerTile(Cauldron::class);
     //add creative item
     $this->addCreativeItem(Item::get(self::ITEM_CAULDRON, 0));
     //init Color
     Color::init();
     //fix max stack
     $this->registerItem(Item::POTION, Potion::class);
     $this->registerItem(Item::SPLASH_POTION, SplashPotion::class);
     //Recipes
     $craftingManager = Server::getInstance()->getCraftingManager();
     //.FlowerPot
     $craftingManager->registerRecipe((new BigShapedRecipe(Item::get(MainClass::ITEM_FLOWER_POT, 0, 1), "B B", " B ", "   "))->setIngredient("B", Item::get(Item::BRICK, null, 1)));
     //.NoteBlock
     $craftingManager->registerRecipe((new BigShapedRecipe(Item::get(MainClass::BLOCK_NOTE, 0, 1), "PPP", "PRP", "PPP"))->setIngredient("P", Item::get(Item::PLANK, null, 1))->setIngredient("R", Item::get(Item::REDSTONE, null, 1)));
     //.ItemFrame
     $craftingManager->registerRecipe((new BigShapedRecipe(Item::get(MainClass::ITEM_ITEM_FRAME, 0, 1), "SSS", "SLS", "SSS"))->setIngredient("S", Item::get(Item::STICK, null, 1))->setIngredient("L", Item::get(Item::LEATHER, null, 1)));
     //.Cauldron
     $craftingManager->registerRecipe((new BigShapedRecipe(Item::get(MainClass::ITEM_CAULDRON, 0, 1), "I I", "I I", "III"))->setIngredient("I", Item::get(Item::IRON_INGOT, null, 1)));
     Server::getInstance()->getPluginManager()->registerEvents($this, $this);
 }