PocketMine-MP 5.37.1 git-cef37e7835c666594588f957a47b27d521c6a58e
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 $baseErrorContext = "Campfire ($this->position)";
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::safeNbtDeserialize($tag, "$baseErrorContext slot $slot");
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:1953
setBlock(Vector3 $pos, Block $block, bool $update=true)
Definition World.php:2021