diff --git a/src/pocketmine/block/BlockFactory.php b/src/pocketmine/block/BlockFactory.php index 0c9dfed79..b25ebd0f5 100644 --- a/src/pocketmine/block/BlockFactory.php +++ b/src/pocketmine/block/BlockFactory.php @@ -36,6 +36,7 @@ use pocketmine\block\tile\EnderChest as TileEnderChest; use pocketmine\block\tile\FlowerPot as TileFlowerPot; use pocketmine\block\tile\Furnace as TileFurnace; use pocketmine\block\tile\ItemFrame as TileItemFrame; +use pocketmine\block\tile\Note as TileNote; use pocketmine\block\tile\Sign as TileSign; use pocketmine\block\tile\Skull as TileSkull; use pocketmine\block\tile\TileFactory; @@ -241,7 +242,7 @@ class BlockFactory{ self::register(new Solid(new BID(Ids::NETHER_WART_BLOCK), "Nether Wart Block", new BlockBreakInfo(1.0))); self::register(new NetherWartPlant(new BID(Ids::NETHER_WART_PLANT, 0, ItemIds::NETHER_WART), "Nether Wart")); self::register(new Netherrack(new BID(Ids::NETHERRACK), "Netherrack")); - self::register(new NoteBlock(new BID(Ids::NOTEBLOCK), "Note Block")); + self::register(new Note(new BID(Ids::NOTEBLOCK, 0, null, TileNote::class), "Note Block")); self::register(new Solid(new BID(Ids::OBSIDIAN), "Obsidian", new BlockBreakInfo(35.0 /* 50 in PC */, BlockToolType::TYPE_PICKAXE, TieredTool::TIER_DIAMOND, 6000.0))); self::register(new PackedIce(new BID(Ids::PACKED_ICE), "Packed Ice")); self::register(new Podzol(new BID(Ids::PODZOL), "Podzol")); diff --git a/src/pocketmine/block/NoteBlock.php b/src/pocketmine/block/Note.php similarity index 50% rename from src/pocketmine/block/NoteBlock.php rename to src/pocketmine/block/Note.php index 000c6889f..d9be13476 100644 --- a/src/pocketmine/block/NoteBlock.php +++ b/src/pocketmine/block/Note.php @@ -23,15 +23,57 @@ declare(strict_types=1); namespace pocketmine\block; -class NoteBlock extends Solid{ +use pocketmine\block\tile\Note as TileNote; +use function assert; + +class Note extends Solid{ + public const MIN_PITCH = 0; + public const MAX_PITCH = 24; + + /** @var int */ + private $pitch = self::MIN_PITCH; public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){ parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(0.8, BlockToolType::TYPE_AXE)); } + public function readStateFromWorld() : void{ + parent::readStateFromWorld(); + $tile = $this->world->getTile($this); + if($tile instanceof TileNote){ + $this->pitch = $tile->getPitch(); + }else{ + $this->pitch = self::MIN_PITCH; + } + } + + public function writeStateToWorld() : void{ + parent::writeStateToWorld(); + $tile = $this->world->getTile($this); + assert($tile instanceof TileNote); + $tile->setPitch($this->pitch); + } + public function getFuelTime() : int{ return 300; } + /** + * @return int + */ + public function getPitch() : int{ + return $this->pitch; + } + + /** + * @param int $pitch + */ + public function setPitch(int $pitch) : void{ + if($pitch < self::MIN_PITCH or $pitch > self::MAX_PITCH){ + throw new \InvalidArgumentException("Pitch must be in range " . self::MIN_PITCH . " - " . self::MAX_PITCH); + } + $this->pitch = $pitch; + } + //TODO } diff --git a/src/pocketmine/block/tile/Note.php b/src/pocketmine/block/tile/Note.php new file mode 100644 index 000000000..4d178d837 --- /dev/null +++ b/src/pocketmine/block/tile/Note.php @@ -0,0 +1,62 @@ +getByte("note", $this->pitch)) > BlockNote::MIN_PITCH and $pitch <= BlockNote::MAX_PITCH){ + $this->pitch = $pitch; + } + } + + protected function writeSaveData(CompoundTag $nbt) : void{ + $nbt->setByte("note", $this->pitch); + } + + /** + * @return int + */ + public function getPitch() : int{ + return $this->pitch; + } + + /** + * @param int $pitch + */ + public function setPitch(int $pitch) : void{ + if($pitch < BlockNote::MIN_PITCH or $pitch > BlockNote::MAX_PITCH){ + throw new \InvalidArgumentException("Pitch must be in range " . BlockNote::MIN_PITCH . " - " . BlockNote::MAX_PITCH); + } + $this->pitch = $pitch; + } +} diff --git a/src/pocketmine/block/tile/TileFactory.php b/src/pocketmine/block/tile/TileFactory.php index a84912ad6..369260602 100644 --- a/src/pocketmine/block/tile/TileFactory.php +++ b/src/pocketmine/block/tile/TileFactory.php @@ -55,6 +55,7 @@ final class TileFactory{ self::register(FlowerPot::class, ["FlowerPot", "minecraft:flower_pot"]); self::register(Furnace::class, ["Furnace", "minecraft:furnace"]); self::register(ItemFrame::class, ["ItemFrame"]); //this is an entity in PC + self::register(Note::class, ["Music", "minecraft:noteblock"]); self::register(Sign::class, ["Sign", "minecraft:sign"]); self::register(Skull::class, ["Skull", "minecraft:skull"]); @@ -80,7 +81,6 @@ final class TileFactory{ //TODO: Lectern //TODO: MobSpawner //TODO: MovingBlock - //TODO: Music //TODO: NetherReactor //TODO: PistonArm //TODO: ShulkerBox