From f18da8d879d0066c4776320725cc7df369a0afb5 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 30 May 2019 18:52:19 +0100 Subject: [PATCH] Added missing tile for Note Block this doesn't come with a full impl because that requires some further changes like adding materials, which is out of the scope of this commit. This is here to prevent additional data loss in imported worlds. --- src/pocketmine/block/BlockFactory.php | 3 +- .../block/{NoteBlock.php => Note.php} | 44 ++++++++++++- src/pocketmine/block/tile/Note.php | 62 +++++++++++++++++++ src/pocketmine/block/tile/TileFactory.php | 2 +- 4 files changed, 108 insertions(+), 3 deletions(-) rename src/pocketmine/block/{NoteBlock.php => Note.php} (50%) create mode 100644 src/pocketmine/block/tile/Note.php 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