PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
tile/Campfire.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block\tile;
25
26use pocketmine\block\Campfire as BlockCampfire;
36
37class Campfire extends Spawnable implements Container{
39
40 private const TAG_FIRST_INPUT_ITEM = "Item1"; //TAG_Compound
41 private const TAG_SECOND_INPUT_ITEM = "Item2"; //TAG_Compound
42 private const TAG_THIRD_INPUT_ITEM = "Item3"; //TAG_Compound
43 private const TAG_FOURTH_INPUT_ITEM = "Item4"; //TAG_Compound
44
45 private const TAG_FIRST_COOKING_TIME = "ItemTime1"; //TAG_Int
46 private const TAG_SECOND_COOKING_TIME = "ItemTime2"; //TAG_Int
47 private const TAG_THIRD_COOKING_TIME = "ItemTime3"; //TAG_Int
48 private const TAG_FOURTH_COOKING_TIME = "ItemTime4"; //TAG_Int
49
50 protected CampfireInventory $inventory;
52 private array $cookingTimes = [];
53
54 public function __construct(World $world, Vector3 $pos){
55 parent::__construct($world, $pos);
56 $this->inventory = new CampfireInventory($this->position);
57 $this->inventory->getListeners()->add(CallbackInventoryListener::onAnyChange(
58 static function(Inventory $unused) use ($world, $pos) : void{
59 $block = $world->getBlock($pos);
60 if($block instanceof BlockCampfire){
61 $world->setBlock($pos, $block);
62 }
63 })
64 );
65 }
66
67 public function getInventory() : CampfireInventory{
68 return $this->inventory;
69 }
70
71 public function getRealInventory() : CampfireInventory{
72 return $this->inventory;
73 }
74
79 public function getCookingTimes() : array{
80 return $this->cookingTimes;
81 }
82
87 public function setCookingTimes(array $cookingTimes) : void{
88 $this->cookingTimes = $cookingTimes;
89 }
90
91 public function readSaveData(CompoundTag $nbt) : void{
92 $items = [];
93 $listeners = $this->inventory->getListeners()->toArray();
94 $this->inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization
95
96 foreach([
97 [0, self::TAG_FIRST_INPUT_ITEM, self::TAG_FIRST_COOKING_TIME],
98 [1, self::TAG_SECOND_INPUT_ITEM, self::TAG_SECOND_COOKING_TIME],
99 [2, self::TAG_THIRD_INPUT_ITEM, self::TAG_THIRD_COOKING_TIME],
100 [3, self::TAG_FOURTH_INPUT_ITEM, self::TAG_FOURTH_COOKING_TIME],
101 ] as [$slot, $itemTag, $cookingTimeTag]){
102 if(($tag = $nbt->getTag($itemTag)) instanceof CompoundTag){
103 $items[$slot] = Item::nbtDeserialize($tag);
104 }
105 if(($tag = $nbt->getTag($cookingTimeTag)) instanceof IntTag){
106 $this->cookingTimes[$slot] = $tag->getValue();
107 }
108 }
109 $this->inventory->setContents($items);
110 $this->inventory->getListeners()->add(...$listeners);
111 }
112
113 protected function writeSaveData(CompoundTag $nbt) : void{
114 foreach([
115 [0, self::TAG_FIRST_INPUT_ITEM, self::TAG_FIRST_COOKING_TIME],
116 [1, self::TAG_SECOND_INPUT_ITEM, self::TAG_SECOND_COOKING_TIME],
117 [2, self::TAG_THIRD_INPUT_ITEM, self::TAG_THIRD_COOKING_TIME],
118 [3, self::TAG_FOURTH_INPUT_ITEM, self::TAG_FOURTH_COOKING_TIME],
119 ] as [$slot, $itemTag, $cookingTimeTag]){
120 $item = $this->inventory->getItem($slot);
121 if(!$item->isNull()){
122 $nbt->setTag($itemTag, $item->nbtSerialize());
123 if(isset($this->cookingTimes[$slot])){
124 $nbt->setInt($cookingTimeTag, $this->cookingTimes[$slot]);
125 }
126 }
127 }
128 }
129
130 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
131 foreach([
132 0 => self::TAG_FIRST_INPUT_ITEM,
133 1 => self::TAG_SECOND_INPUT_ITEM,
134 2 => self::TAG_THIRD_INPUT_ITEM,
135 3 => self::TAG_FOURTH_INPUT_ITEM
136 ] as $slot => $tag){
137 $item = $this->inventory->getItem($slot);
138 if(!$item->isNull()){
139 $nbt->setTag($tag, TypeConverter::getInstance()->getItemTranslator()->toNetworkNbt($item));
140 }
141 }
142 }
143}
addAdditionalSpawnData(CompoundTag $nbt)
setCookingTimes(array $cookingTimes)
getBlock(Vector3 $pos, bool $cached=true, bool $addToCache=true)
Definition World.php:1870
setBlock(Vector3 $pos, Block $block, bool $update=true)
Definition World.php:1934