PocketMine-MP 5.33.2 git-919492bdcad8510eb6606439eb77e1c604f1d1ea
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 ContainerTile{
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 Inventory $inventory;
52 private array $cookingTimes = [];
53
54 public function __construct(World $world, Vector3 $pos){
55 parent::__construct($world, $pos);
56 $this->inventory = new SimpleInventory(4);
57 $this->inventory->setMaxStackSize(1);
58 $this->inventory->getListeners()->add(CallbackInventoryListener::onAnyChange(
59 static function(Inventory $unused) use ($world, $pos) : void{
60 $block = $world->getBlock($pos);
61 if($block instanceof BlockCampfire){
62 $world->setBlock($pos, $block);
63 }
64 })
65 );
66 }
67
68 public function getInventory() : Inventory{
69 return $this->inventory;
70 }
71
72 public function getRealInventory() : Inventory{
73 return $this->inventory;
74 }
75
80 public function getCookingTimes() : array{
81 return $this->cookingTimes;
82 }
83
88 public function setCookingTimes(array $cookingTimes) : void{
89 $this->cookingTimes = $cookingTimes;
90 }
91
92 public function readSaveData(CompoundTag $nbt) : void{
93 $items = [];
94 $listeners = $this->inventory->getListeners()->toArray();
95 $this->inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization
96
97 foreach([
98 [0, self::TAG_FIRST_INPUT_ITEM, self::TAG_FIRST_COOKING_TIME],
99 [1, self::TAG_SECOND_INPUT_ITEM, self::TAG_SECOND_COOKING_TIME],
100 [2, self::TAG_THIRD_INPUT_ITEM, self::TAG_THIRD_COOKING_TIME],
101 [3, self::TAG_FOURTH_INPUT_ITEM, self::TAG_FOURTH_COOKING_TIME],
102 ] as [$slot, $itemTag, $cookingTimeTag]){
103 if(($tag = $nbt->getTag($itemTag)) instanceof CompoundTag){
104 $items[$slot] = Item::nbtDeserialize($tag);
105 }
106 if(($tag = $nbt->getTag($cookingTimeTag)) instanceof IntTag){
107 $this->cookingTimes[$slot] = $tag->getValue();
108 }
109 }
110 $this->inventory->setContents($items);
111 $this->inventory->getListeners()->add(...$listeners);
112 }
113
114 protected function writeSaveData(CompoundTag $nbt) : void{
115 foreach([
116 [0, self::TAG_FIRST_INPUT_ITEM, self::TAG_FIRST_COOKING_TIME],
117 [1, self::TAG_SECOND_INPUT_ITEM, self::TAG_SECOND_COOKING_TIME],
118 [2, self::TAG_THIRD_INPUT_ITEM, self::TAG_THIRD_COOKING_TIME],
119 [3, self::TAG_FOURTH_INPUT_ITEM, self::TAG_FOURTH_COOKING_TIME],
120 ] as [$slot, $itemTag, $cookingTimeTag]){
121 $item = $this->inventory->getItem($slot);
122 if(!$item->isNull()){
123 $nbt->setTag($itemTag, $item->nbtSerialize());
124 if(isset($this->cookingTimes[$slot])){
125 $nbt->setInt($cookingTimeTag, $this->cookingTimes[$slot]);
126 }
127 }
128 }
129 }
130
131 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
132 foreach([
133 0 => self::TAG_FIRST_INPUT_ITEM,
134 1 => self::TAG_SECOND_INPUT_ITEM,
135 2 => self::TAG_THIRD_INPUT_ITEM,
136 3 => self::TAG_FOURTH_INPUT_ITEM
137 ] as $slot => $tag){
138 $item = $this->inventory->getItem($slot);
139 if(!$item->isNull()){
140 $nbt->setTag($tag, TypeConverter::getInstance()->getItemTranslator()->toNetworkNbt($item));
141 }
142 }
143 }
144}
addAdditionalSpawnData(CompoundTag $nbt)
setCookingTimes(array $cookingTimes)
getBlock(Vector3 $pos, bool $cached=true, bool $addToCache=true)
Definition World.php:1933
setBlock(Vector3 $pos, Block $block, bool $update=true)
Definition World.php:2001