PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
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 $baseErrorContext = "Campfire ($this->position)";
98 foreach([
99 [0, self::TAG_FIRST_INPUT_ITEM, self::TAG_FIRST_COOKING_TIME],
100 [1, self::TAG_SECOND_INPUT_ITEM, self::TAG_SECOND_COOKING_TIME],
101 [2, self::TAG_THIRD_INPUT_ITEM, self::TAG_THIRD_COOKING_TIME],
102 [3, self::TAG_FOURTH_INPUT_ITEM, self::TAG_FOURTH_COOKING_TIME],
103 ] as [$slot, $itemTag, $cookingTimeTag]){
104 if(($tag = $nbt->getTag($itemTag)) instanceof CompoundTag){
105 $items[$slot] = Item::safeNbtDeserialize($tag, "$baseErrorContext slot $slot");
106 }
107 if(($tag = $nbt->getTag($cookingTimeTag)) instanceof IntTag){
108 $this->cookingTimes[$slot] = $tag->getValue();
109 }
110 }
111 $this->inventory->setContents($items);
112 $this->inventory->getListeners()->add(...$listeners);
113 }
114
115 protected function writeSaveData(CompoundTag $nbt) : void{
116 foreach([
117 [0, self::TAG_FIRST_INPUT_ITEM, self::TAG_FIRST_COOKING_TIME],
118 [1, self::TAG_SECOND_INPUT_ITEM, self::TAG_SECOND_COOKING_TIME],
119 [2, self::TAG_THIRD_INPUT_ITEM, self::TAG_THIRD_COOKING_TIME],
120 [3, self::TAG_FOURTH_INPUT_ITEM, self::TAG_FOURTH_COOKING_TIME],
121 ] as [$slot, $itemTag, $cookingTimeTag]){
122 $item = $this->inventory->getItem($slot);
123 if(!$item->isNull()){
124 $nbt->setTag($itemTag, $item->nbtSerialize());
125 if(isset($this->cookingTimes[$slot])){
126 $nbt->setInt($cookingTimeTag, $this->cookingTimes[$slot]);
127 }
128 }
129 }
130 }
131
132 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
133 foreach([
134 0 => self::TAG_FIRST_INPUT_ITEM,
135 1 => self::TAG_SECOND_INPUT_ITEM,
136 2 => self::TAG_THIRD_INPUT_ITEM,
137 3 => self::TAG_FOURTH_INPUT_ITEM
138 ] as $slot => $tag){
139 $item = $this->inventory->getItem($slot);
140 if(!$item->isNull()){
141 $nbt->setTag($tag, TypeConverter::getInstance()->getItemTranslator()->toNetworkNbt($item));
142 }
143 }
144 }
145}
addAdditionalSpawnData(CompoundTag $nbt)
setCookingTimes(array $cookingTimes)
getBlock(Vector3 $pos, bool $cached=true, bool $addToCache=true)
Definition World.php:1921
setBlock(Vector3 $pos, Block $block, bool $update=true)
Definition World.php:1989