From c498d82f355bbd23026fd866796ec0230c0b41f1 Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Tue, 16 Aug 2022 22:27:23 -0500 Subject: [PATCH 01/10] Implement goat horns --- build/generate-runtime-enum-serializers.php | 4 +- src/data/bedrock/GoatHornTypeIdMap.php | 70 ++++++++++++++++ src/data/bedrock/GoatHornTypeIds.php | 35 ++++++++ src/data/bedrock/item/ItemDeserializer.php | 10 ++- src/data/bedrock/item/ItemSerializer.php | 3 + .../runtime/RuntimeEnumDeserializerTrait.php | 14 ++++ .../runtime/RuntimeEnumSerializerTrait.php | 14 ++++ src/item/GoatHorn.php | 71 ++++++++++++++++ src/item/GoatHornType.php | 83 +++++++++++++++++++ src/item/ItemTypeIds.php | 3 +- src/item/StringToItemParser.php | 6 ++ src/item/VanillaItems.php | 1 + src/world/sound/GoatHornAdmireSound.php | 35 ++++++++ src/world/sound/GoatHornCallSound.php | 35 ++++++++ src/world/sound/GoatHornDreamSound.php | 35 ++++++++ src/world/sound/GoatHornFeelSound.php | 35 ++++++++ src/world/sound/GoatHornPonderSound.php | 35 ++++++++ src/world/sound/GoatHornSeekSound.php | 35 ++++++++ src/world/sound/GoatHornSingSound.php | 35 ++++++++ src/world/sound/GoatHornYearnSound.php | 35 ++++++++ 20 files changed, 591 insertions(+), 3 deletions(-) create mode 100644 src/data/bedrock/GoatHornTypeIdMap.php create mode 100644 src/data/bedrock/GoatHornTypeIds.php create mode 100644 src/item/GoatHorn.php create mode 100644 src/item/GoatHornType.php create mode 100644 src/world/sound/GoatHornAdmireSound.php create mode 100644 src/world/sound/GoatHornCallSound.php create mode 100644 src/world/sound/GoatHornDreamSound.php create mode 100644 src/world/sound/GoatHornFeelSound.php create mode 100644 src/world/sound/GoatHornPonderSound.php create mode 100644 src/world/sound/GoatHornSeekSound.php create mode 100644 src/world/sound/GoatHornSingSound.php create mode 100644 src/world/sound/GoatHornYearnSound.php diff --git a/build/generate-runtime-enum-serializers.php b/build/generate-runtime-enum-serializers.php index 7118abf4144..7d084b6d2d4 100644 --- a/build/generate-runtime-enum-serializers.php +++ b/build/generate-runtime-enum-serializers.php @@ -33,6 +33,7 @@ use pocketmine\block\utils\MushroomBlockType; use pocketmine\block\utils\SkullType; use pocketmine\block\utils\SlabType; +use pocketmine\item\GoatHornType; use pocketmine\item\PotionType; use function array_key_first; use function array_keys; @@ -167,7 +168,8 @@ function buildEnumReaderFunc(array $enumMembers, string &$functionName) : array{ MushroomBlockType::getAll(), SkullType::getAll(), SlabType::getAll(), - PotionType::getAll() + PotionType::getAll(), + GoatHornType::getAll() ]; $readerFuncs = [ diff --git a/src/data/bedrock/GoatHornTypeIdMap.php b/src/data/bedrock/GoatHornTypeIdMap.php new file mode 100644 index 00000000000..f116857690e --- /dev/null +++ b/src/data/bedrock/GoatHornTypeIdMap.php @@ -0,0 +1,70 @@ + + */ + private array $idToEnum; + + /** + * @var int[] + * @phpstan-var array + */ + private array $enumToId; + + private function __construct(){ + $this->register(GoatHornTypeIds::PONDER, GoatHornType::PONDER()); + $this->register(GoatHornTypeIds::SING, GoatHornType::SING()); + $this->register(GoatHornTypeIds::SEEK, GoatHornType::SEEK()); + $this->register(GoatHornTypeIds::FEEL, GoatHornType::FEEL()); + $this->register(GoatHornTypeIds::ADMIRE, GoatHornType::ADMIRE()); + $this->register(GoatHornTypeIds::CALL, GoatHornType::CALL()); + $this->register(GoatHornTypeIds::YEARN, GoatHornType::YEARN()); + $this->register(GoatHornTypeIds::DREAM, GoatHornType::DREAM()); + } + + private function register(int $id, GoatHornType $type) : void{ + $this->idToEnum[$id] = $type; + $this->enumToId[$type->id()] = $id; + } + + public function fromId(int $id) : ?GoatHornType{ + return $this->idToEnum[$id] ?? null; + } + + public function toId(GoatHornType $type) : int{ + if(!isset($this->enumToId[$type->id()])){ + throw new \InvalidArgumentException("Type does not have a mapped ID"); + } + return $this->enumToId[$type->id()]; + } +} \ No newline at end of file diff --git a/src/data/bedrock/GoatHornTypeIds.php b/src/data/bedrock/GoatHornTypeIds.php new file mode 100644 index 00000000000..374f4b60261 --- /dev/null +++ b/src/data/bedrock/GoatHornTypeIds.php @@ -0,0 +1,35 @@ +map(Ids::GLOWSTONE_DUST, fn() => Items::GLOWSTONE_DUST()); - //TODO: minecraft:goat_horn + $this->map(Ids::GOAT_HORN, function(Data $data) : Item{ + $meta = $data->getMeta(); + $goatHornType = GoatHornTypeIdMap::getInstance()->fromId($meta); + if($goatHornType === null){ + throw new ItemTypeDeserializeException("Unknown goat horn type ID $meta"); + } + return Items::GOAT_HORN()->setType($goatHornType); + }); //TODO: minecraft:goat_spawn_egg $this->map(Ids::GOLD_INGOT, fn() => Items::GOLD_INGOT()); $this->map(Ids::GOLD_NUGGET, fn() => Items::GOLD_NUGGET()); diff --git a/src/data/bedrock/item/ItemSerializer.php b/src/data/bedrock/item/ItemSerializer.php index 19754739d4a..78b1b8a697c 100644 --- a/src/data/bedrock/item/ItemSerializer.php +++ b/src/data/bedrock/item/ItemSerializer.php @@ -33,12 +33,14 @@ use pocketmine\data\bedrock\block\BlockStateSerializer; use pocketmine\data\bedrock\CompoundTypeIds; use pocketmine\data\bedrock\DyeColorIdMap; +use pocketmine\data\bedrock\GoatHornTypeIdMap; use pocketmine\data\bedrock\item\ItemTypeNames as Ids; use pocketmine\data\bedrock\item\SavedItemData as Data; use pocketmine\data\bedrock\PotionTypeIdMap; use pocketmine\item\Banner; use pocketmine\item\CoralFan; use pocketmine\item\Dye; +use pocketmine\item\GoatHorn; use pocketmine\item\Item; use pocketmine\item\ItemBlock; use pocketmine\item\Potion; @@ -392,6 +394,7 @@ private function registerSerializers() : void{ $this->map(Items::GLISTERING_MELON(), self::id(Ids::GLISTERING_MELON_SLICE)); $this->map(Items::GLOWSTONE_DUST(), self::id(Ids::GLOWSTONE_DUST)); $this->map(Items::GLOW_INK_SAC(), self::id(Ids::GLOW_INK_SAC)); + $this->map(Items::GOAT_HORN(), fn(GoatHorn $item) => new Data(Ids::GOAT_HORN, GoatHornTypeIdMap::getInstance()->toId($item->getType()))); $this->map(Items::GOLDEN_APPLE(), self::id(Ids::GOLDEN_APPLE)); $this->map(Items::GOLDEN_AXE(), self::id(Ids::GOLDEN_AXE)); $this->map(Items::GOLDEN_BOOTS(), self::id(Ids::GOLDEN_BOOTS)); diff --git a/src/data/runtime/RuntimeEnumDeserializerTrait.php b/src/data/runtime/RuntimeEnumDeserializerTrait.php index 7bcbd09ed42..e81e6288aeb 100644 --- a/src/data/runtime/RuntimeEnumDeserializerTrait.php +++ b/src/data/runtime/RuntimeEnumDeserializerTrait.php @@ -102,6 +102,20 @@ public function froglightType(\pocketmine\block\utils\FroglightType &$value) : v }; } + public function goatHornType(\pocketmine\item\GoatHornType &$value) : void{ + $value = match($this->readInt(3)){ + 0 => \pocketmine\item\GoatHornType::ADMIRE(), + 1 => \pocketmine\item\GoatHornType::CALL(), + 2 => \pocketmine\item\GoatHornType::DREAM(), + 3 => \pocketmine\item\GoatHornType::FEEL(), + 4 => \pocketmine\item\GoatHornType::PONDER(), + 5 => \pocketmine\item\GoatHornType::SEEK(), + 6 => \pocketmine\item\GoatHornType::SING(), + 7 => \pocketmine\item\GoatHornType::YEARN(), + default => throw new InvalidSerializedRuntimeDataException("Invalid serialized value for GoatHornType") + }; + } + public function leverFacing(\pocketmine\block\utils\LeverFacing &$value) : void{ $value = match($this->readInt(3)){ 0 => \pocketmine\block\utils\LeverFacing::DOWN_AXIS_X(), diff --git a/src/data/runtime/RuntimeEnumSerializerTrait.php b/src/data/runtime/RuntimeEnumSerializerTrait.php index e3bdcbccbb1..aa503f205e2 100644 --- a/src/data/runtime/RuntimeEnumSerializerTrait.php +++ b/src/data/runtime/RuntimeEnumSerializerTrait.php @@ -102,6 +102,20 @@ public function froglightType(\pocketmine\block\utils\FroglightType $value) : vo }); } + public function goatHornType(\pocketmine\item\GoatHornType $value) : void{ + $this->int(3, match($value){ + \pocketmine\item\GoatHornType::ADMIRE() => 0, + \pocketmine\item\GoatHornType::CALL() => 1, + \pocketmine\item\GoatHornType::DREAM() => 2, + \pocketmine\item\GoatHornType::FEEL() => 3, + \pocketmine\item\GoatHornType::PONDER() => 4, + \pocketmine\item\GoatHornType::SEEK() => 5, + \pocketmine\item\GoatHornType::SING() => 6, + \pocketmine\item\GoatHornType::YEARN() => 7, + default => throw new \pocketmine\utils\AssumptionFailedError("All GoatHornType cases should be covered") + }); + } + public function leverFacing(\pocketmine\block\utils\LeverFacing $value) : void{ $this->int(3, match($value){ \pocketmine\block\utils\LeverFacing::DOWN_AXIS_X() => 0, diff --git a/src/item/GoatHorn.php b/src/item/GoatHorn.php new file mode 100644 index 00000000000..23e131cab05 --- /dev/null +++ b/src/item/GoatHorn.php @@ -0,0 +1,71 @@ +goatHornType = GoatHornType::PONDER(); + parent::__construct($identifier, $name); + } + + protected function encodeType(RuntimeDataWriter $w) : void{ + $w->goatHornType($this->goatHornType); + } + + public function getType() : GoatHornType{ return $this->goatHornType; } + + /** + * @return $this + */ + public function setType(GoatHornType $type) : self{ + $this->goatHornType = $type; + return $this; + } + + public function getMaxStackSize() : int{ + return 1; + } + + public function getCooldownTicks() : int{ + return 140; + } + + public function canStartUsingItem(Player $player) : bool{ + return true; + } + + public function onClickAir(Player $player, Vector3 $directionVector, array &$returnedItems) : ItemUseResult{ + $position = $player->getPosition(); + $position->getWorld()->addSound($position, $this->goatHornType->getSound()); + + return ItemUseResult::SUCCESS(); + } +} diff --git a/src/item/GoatHornType.php b/src/item/GoatHornType.php new file mode 100644 index 00000000000..44524e85129 --- /dev/null +++ b/src/item/GoatHornType.php @@ -0,0 +1,83 @@ + new GoatHornPonderSound()), + new self("sing", fn() => new GoatHornSingSound()), + new self("seek", fn() => new GoatHornSeekSound()), + new self("feel", fn() => new GoatHornFeelSound()), + new self("admire", fn() => new GoatHornAdmireSound()), + new self("call", fn() => new GoatHornCallSound()), + new self("yearn", fn() => new GoatHornYearnSound()), + new self("dream", fn() => new GoatHornDreamSound()) + ); + } + + /** + * @phpstan-param \Closure() : Sound + */ + private function __construct( + string $enumName, + private \Closure $soundGetter + ){ + $this->Enum___construct($enumName); + } + + public function getSound() : Sound{ + return ($this->soundGetter)(); + } +} \ No newline at end of file diff --git a/src/item/ItemTypeIds.php b/src/item/ItemTypeIds.php index 6f23fb9f80c..b6552aca63f 100644 --- a/src/item/ItemTypeIds.php +++ b/src/item/ItemTypeIds.php @@ -297,8 +297,9 @@ private function __construct(){ public const POWDER_SNOW_BUCKET = 20258; public const LINGERING_POTION = 20259; public const FIRE_CHARGE = 20260; + public const GOAT_HORN = 20261; - public const FIRST_UNUSED_ITEM_ID = 20261; + public const FIRST_UNUSED_ITEM_ID = 20262; private static int $nextDynamicId = self::FIRST_UNUSED_ITEM_ID; diff --git a/src/item/StringToItemParser.php b/src/item/StringToItemParser.php index d0bdcb5f9eb..851e1eb6a6b 100644 --- a/src/item/StringToItemParser.php +++ b/src/item/StringToItemParser.php @@ -1118,6 +1118,11 @@ private static function registerDynamicItems(self $result) : void{ $result->register($prefix("dye"), fn() => Items::DYE()->setColor($color)); } + foreach(GoatHornType::getAll() as $goatHornType){ + $prefix = fn(string $name) => $goatHornType->name() . "_" . $name; + + $result->register($prefix("goat_horn"), fn() => Items::GOAT_HORN()->setType($goatHornType)); + } } private static function registerItems(self $result) : void{ @@ -1259,6 +1264,7 @@ private static function registerItems(self $result) : void{ $result->register("glistering_melon", fn() => Items::GLISTERING_MELON()); $result->register("glow_ink_sac", fn() => Items::GLOW_INK_SAC()); $result->register("glowstone_dust", fn() => Items::GLOWSTONE_DUST()); + $result->register("goat_horn", fn() => Items::GOAT_HORN()); $result->register("gold_axe", fn() => Items::GOLDEN_AXE()); $result->register("gold_boots", fn() => Items::GOLDEN_BOOTS()); $result->register("gold_chestplate", fn() => Items::GOLDEN_CHESTPLATE()); diff --git a/src/item/VanillaItems.php b/src/item/VanillaItems.php index 11129238ef3..264f6b46520 100644 --- a/src/item/VanillaItems.php +++ b/src/item/VanillaItems.php @@ -433,6 +433,7 @@ protected static function setup() : void{ self::register("glistering_melon", new Item(new IID(Ids::GLISTERING_MELON), "Glistering Melon")); self::register("glow_ink_sac", new Item(new IID(Ids::GLOW_INK_SAC), "Glow Ink Sac")); self::register("glowstone_dust", new Item(new IID(Ids::GLOWSTONE_DUST), "Glowstone Dust")); + self::register("goat_horn", new GoatHorn(new IID(Ids::GOAT_HORN), "Goat Horn")); self::register("gold_ingot", new Item(new IID(Ids::GOLD_INGOT), "Gold Ingot")); self::register("gold_nugget", new Item(new IID(Ids::GOLD_NUGGET), "Gold Nugget")); self::register("golden_apple", new GoldenApple(new IID(Ids::GOLDEN_APPLE), "Golden Apple")); diff --git a/src/world/sound/GoatHornAdmireSound.php b/src/world/sound/GoatHornAdmireSound.php new file mode 100644 index 00000000000..ac3239bd7ec --- /dev/null +++ b/src/world/sound/GoatHornAdmireSound.php @@ -0,0 +1,35 @@ + Date: Wed, 17 Aug 2022 01:04:46 -0500 Subject: [PATCH 02/10] Make PHPStan happy --- src/item/GoatHorn.php | 2 +- src/item/GoatHornType.php | 2 +- src/item/VanillaItems.php | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/item/GoatHorn.php b/src/item/GoatHorn.php index 23e131cab05..5d3a5a77020 100644 --- a/src/item/GoatHorn.php +++ b/src/item/GoatHorn.php @@ -24,8 +24,8 @@ namespace pocketmine\item; use pocketmine\data\runtime\RuntimeDataWriter; -use pocketmine\player\Player; use pocketmine\math\Vector3; +use pocketmine\player\Player; class GoatHorn extends Item implements Releasable{ diff --git a/src/item/GoatHornType.php b/src/item/GoatHornType.php index 44524e85129..472f6659ea5 100644 --- a/src/item/GoatHornType.php +++ b/src/item/GoatHornType.php @@ -68,7 +68,7 @@ protected static function setup() : void{ } /** - * @phpstan-param \Closure() : Sound + * @phpstan-param \Closure() : Sound $soundGetter */ private function __construct( string $enumName, diff --git a/src/item/VanillaItems.php b/src/item/VanillaItems.php index 264f6b46520..9131cb5eb4a 100644 --- a/src/item/VanillaItems.php +++ b/src/item/VanillaItems.php @@ -165,6 +165,7 @@ * @method static Item GLISTERING_MELON() * @method static Item GLOWSTONE_DUST() * @method static Item GLOW_INK_SAC() + * @method static GoatHorn GOAT_HORN() * @method static GoldenApple GOLDEN_APPLE() * @method static Axe GOLDEN_AXE() * @method static Armor GOLDEN_BOOTS() From a4ad69bfbde822e172af09b036ece8c418ade9e6 Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Wed, 17 Aug 2022 01:11:36 -0500 Subject: [PATCH 03/10] ... --- src/item/GoatHornType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/item/GoatHornType.php b/src/item/GoatHornType.php index 472f6659ea5..5ae9395c255 100644 --- a/src/item/GoatHornType.php +++ b/src/item/GoatHornType.php @@ -80,4 +80,4 @@ private function __construct( public function getSound() : Sound{ return ($this->soundGetter)(); } -} \ No newline at end of file +} From 74ee330f9029614c8d98fa22dab8d9ae0bc8e70b Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Wed, 14 Dec 2022 13:35:26 -0500 Subject: [PATCH 04/10] Usage of LevelSoundEvent const --- src/data/bedrock/GoatHornTypeIdMap.php | 2 +- src/data/bedrock/GoatHornTypeIds.php | 2 +- .../item/ItemSerializerDeserializerRegistrar.php | 10 ++++++++++ src/world/sound/GoatHornAdmireSound.php | 2 +- src/world/sound/GoatHornCallSound.php | 2 +- src/world/sound/GoatHornDreamSound.php | 2 +- src/world/sound/GoatHornFeelSound.php | 2 +- src/world/sound/GoatHornPonderSound.php | 2 +- src/world/sound/GoatHornSeekSound.php | 2 +- src/world/sound/GoatHornSingSound.php | 2 +- src/world/sound/GoatHornYearnSound.php | 2 +- 11 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/data/bedrock/GoatHornTypeIdMap.php b/src/data/bedrock/GoatHornTypeIdMap.php index f116857690e..60f43adc4ba 100644 --- a/src/data/bedrock/GoatHornTypeIdMap.php +++ b/src/data/bedrock/GoatHornTypeIdMap.php @@ -67,4 +67,4 @@ public function toId(GoatHornType $type) : int{ } return $this->enumToId[$type->id()]; } -} \ No newline at end of file +} diff --git a/src/data/bedrock/GoatHornTypeIds.php b/src/data/bedrock/GoatHornTypeIds.php index 374f4b60261..048d246fe0a 100644 --- a/src/data/bedrock/GoatHornTypeIds.php +++ b/src/data/bedrock/GoatHornTypeIds.php @@ -32,4 +32,4 @@ final class GoatHornTypeIds{ public const CALL = 5; public const YEARN = 6; public const DREAM = 7; -} \ No newline at end of file +} diff --git a/src/data/bedrock/item/ItemSerializerDeserializerRegistrar.php b/src/data/bedrock/item/ItemSerializerDeserializerRegistrar.php index c213397c9f3..c2adce39122 100644 --- a/src/data/bedrock/item/ItemSerializerDeserializerRegistrar.php +++ b/src/data/bedrock/item/ItemSerializerDeserializerRegistrar.php @@ -32,12 +32,14 @@ use pocketmine\block\VanillaBlocks as Blocks; use pocketmine\data\bedrock\CompoundTypeIds; use pocketmine\data\bedrock\DyeColorIdMap; +use pocketmine\data\bedrock\GoatHornTypeIdMap; use pocketmine\data\bedrock\item\ItemTypeNames as Ids; use pocketmine\data\bedrock\item\SavedItemData as Data; use pocketmine\data\bedrock\PotionTypeIdMap; use pocketmine\data\bedrock\SuspiciousStewTypeIdMap; use pocketmine\item\Banner; use pocketmine\item\Dye; +use pocketmine\item\GoatHorn; use pocketmine\item\Item; use pocketmine\item\Potion; use pocketmine\item\SplashPotion; @@ -462,6 +464,14 @@ function(Banner $item, int $meta) : void{ }, fn(Banner $item) => DyeColorIdMap::getInstance()->toInvertedId($item->getColor()) ); + $this->map1to1ItemWithMeta( + Ids::GOAT_HORN, + Items::GOAT_HORN(), + function(GoatHorn $item, int $meta) : void{ + $item->setType(GoatHornTypeIdMap::getInstance()->fromId($meta) ?? throw new ItemTypeDeserializeException("Unknown goat horn type ID $meta")); + }, + fn(GoatHorn $item) => GoatHornTypeIdMap::getInstance()->toId($item->getType()) + ); $this->map1to1ItemWithMeta( Ids::POTION, Items::POTION(), diff --git a/src/world/sound/GoatHornAdmireSound.php b/src/world/sound/GoatHornAdmireSound.php index ac3239bd7ec..1cfafe30375 100644 --- a/src/world/sound/GoatHornAdmireSound.php +++ b/src/world/sound/GoatHornAdmireSound.php @@ -30,6 +30,6 @@ class GoatHornAdmireSound implements Sound{ public function encode(Vector3 $pos) : array{ - return [LevelSoundEventPacket::nonActorSound(387, $pos, false)]; + return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::HORN_CALL4, $pos, false)]; } } diff --git a/src/world/sound/GoatHornCallSound.php b/src/world/sound/GoatHornCallSound.php index e34652c338a..20deef6f65a 100644 --- a/src/world/sound/GoatHornCallSound.php +++ b/src/world/sound/GoatHornCallSound.php @@ -30,6 +30,6 @@ class GoatHornCallSound implements Sound{ public function encode(Vector3 $pos) : array{ - return [LevelSoundEventPacket::nonActorSound(388, $pos, false)]; + return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::HORN_CALL5, $pos, false)]; } } diff --git a/src/world/sound/GoatHornDreamSound.php b/src/world/sound/GoatHornDreamSound.php index c92699216c5..80fb90c5a6f 100644 --- a/src/world/sound/GoatHornDreamSound.php +++ b/src/world/sound/GoatHornDreamSound.php @@ -30,6 +30,6 @@ class GoatHornDreamSound implements Sound{ public function encode(Vector3 $pos) : array{ - return [LevelSoundEventPacket::nonActorSound(390, $pos, false)]; + return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::HORN_CALL7, $pos, false)]; } } diff --git a/src/world/sound/GoatHornFeelSound.php b/src/world/sound/GoatHornFeelSound.php index 8507c7227f7..7533226cfc4 100644 --- a/src/world/sound/GoatHornFeelSound.php +++ b/src/world/sound/GoatHornFeelSound.php @@ -30,6 +30,6 @@ class GoatHornFeelSound implements Sound{ public function encode(Vector3 $pos) : array{ - return [LevelSoundEventPacket::nonActorSound(386, $pos, false)]; + return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::HORN_CALL3, $pos, false)]; } } diff --git a/src/world/sound/GoatHornPonderSound.php b/src/world/sound/GoatHornPonderSound.php index dd7ea192fcd..56fc51393c7 100644 --- a/src/world/sound/GoatHornPonderSound.php +++ b/src/world/sound/GoatHornPonderSound.php @@ -30,6 +30,6 @@ class GoatHornPonderSound implements Sound{ public function encode(Vector3 $pos) : array{ - return [LevelSoundEventPacket::nonActorSound(383, $pos, false)]; + return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::HORN_CALL0, $pos, false)]; } } diff --git a/src/world/sound/GoatHornSeekSound.php b/src/world/sound/GoatHornSeekSound.php index d4f722c8371..7992a9f7e58 100644 --- a/src/world/sound/GoatHornSeekSound.php +++ b/src/world/sound/GoatHornSeekSound.php @@ -30,6 +30,6 @@ class GoatHornSeekSound implements Sound{ public function encode(Vector3 $pos) : array{ - return [LevelSoundEventPacket::nonActorSound(385, $pos, false)]; + return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::HORN_CALL2, $pos, false)]; } } diff --git a/src/world/sound/GoatHornSingSound.php b/src/world/sound/GoatHornSingSound.php index 15aacfad2e1..adac13dfd93 100644 --- a/src/world/sound/GoatHornSingSound.php +++ b/src/world/sound/GoatHornSingSound.php @@ -30,6 +30,6 @@ class GoatHornSingSound implements Sound{ public function encode(Vector3 $pos) : array{ - return [LevelSoundEventPacket::nonActorSound(384, $pos, false)]; + return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::HORN_CALL1, $pos, false)]; } } diff --git a/src/world/sound/GoatHornYearnSound.php b/src/world/sound/GoatHornYearnSound.php index f2f7e9e24a0..3c4ada86060 100644 --- a/src/world/sound/GoatHornYearnSound.php +++ b/src/world/sound/GoatHornYearnSound.php @@ -30,6 +30,6 @@ class GoatHornYearnSound implements Sound{ public function encode(Vector3 $pos) : array{ - return [LevelSoundEventPacket::nonActorSound(389, $pos, false)]; + return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::HORN_CALL6, $pos, false)]; } } From 063fb51da16f8f586a52cd2f37d3b5ad3c168e9a Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Fri, 23 Dec 2022 16:44:54 -0500 Subject: [PATCH 05/10] GoatHornTypeIdMap: fixed uninitialized fields --- src/data/bedrock/GoatHornTypeIdMap.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/data/bedrock/GoatHornTypeIdMap.php b/src/data/bedrock/GoatHornTypeIdMap.php index 60f43adc4ba..de1250d361b 100644 --- a/src/data/bedrock/GoatHornTypeIdMap.php +++ b/src/data/bedrock/GoatHornTypeIdMap.php @@ -33,13 +33,13 @@ final class GoatHornTypeIdMap{ * @var GoatHornType[] * @phpstan-var array */ - private array $idToEnum; + private array $idToEnum = []; /** * @var int[] * @phpstan-var array */ - private array $enumToId; + private array $enumToId = []; private function __construct(){ $this->register(GoatHornTypeIds::PONDER, GoatHornType::PONDER()); From b544bb496b0596f83b4f246da5f61d86e06d91bd Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Wed, 3 Jan 2024 14:59:46 -0500 Subject: [PATCH 06/10] Modernize code - Usage of new native enums instead of EnumTrait - Usage of new IntSaveIdMapTrait - Remove unnecesary GoatHorn*Sound classes --- src/data/bedrock/GoatHornTypeIdMap.php | 48 ++++-------- src/item/GoatHorn.php | 18 ++--- src/item/GoatHornType.php | 73 ++++++------------- src/item/StringToItemParser.php | 4 +- src/world/sound/GoatHornCallSound.php | 35 --------- src/world/sound/GoatHornDreamSound.php | 35 --------- src/world/sound/GoatHornFeelSound.php | 35 --------- src/world/sound/GoatHornPonderSound.php | 35 --------- src/world/sound/GoatHornSeekSound.php | 35 --------- src/world/sound/GoatHornSingSound.php | 35 --------- ...tHornAdmireSound.php => GoatHornSound.php} | 7 +- src/world/sound/GoatHornYearnSound.php | 35 --------- 12 files changed, 48 insertions(+), 347 deletions(-) delete mode 100644 src/world/sound/GoatHornCallSound.php delete mode 100644 src/world/sound/GoatHornDreamSound.php delete mode 100644 src/world/sound/GoatHornFeelSound.php delete mode 100644 src/world/sound/GoatHornPonderSound.php delete mode 100644 src/world/sound/GoatHornSeekSound.php delete mode 100644 src/world/sound/GoatHornSingSound.php rename src/world/sound/{GoatHornAdmireSound.php => GoatHornSound.php} (79%) delete mode 100644 src/world/sound/GoatHornYearnSound.php diff --git a/src/data/bedrock/GoatHornTypeIdMap.php b/src/data/bedrock/GoatHornTypeIdMap.php index de1250d361b..0510a09cee7 100644 --- a/src/data/bedrock/GoatHornTypeIdMap.php +++ b/src/data/bedrock/GoatHornTypeIdMap.php @@ -28,43 +28,21 @@ final class GoatHornTypeIdMap{ use SingletonTrait; - - /** - * @var GoatHornType[] - * @phpstan-var array - */ - private array $idToEnum = []; - - /** - * @var int[] - * @phpstan-var array - */ - private array $enumToId = []; + /** @phpstan-use IntSaveIdMapTrait */ + use IntSaveIdMapTrait; private function __construct(){ - $this->register(GoatHornTypeIds::PONDER, GoatHornType::PONDER()); - $this->register(GoatHornTypeIds::SING, GoatHornType::SING()); - $this->register(GoatHornTypeIds::SEEK, GoatHornType::SEEK()); - $this->register(GoatHornTypeIds::FEEL, GoatHornType::FEEL()); - $this->register(GoatHornTypeIds::ADMIRE, GoatHornType::ADMIRE()); - $this->register(GoatHornTypeIds::CALL, GoatHornType::CALL()); - $this->register(GoatHornTypeIds::YEARN, GoatHornType::YEARN()); - $this->register(GoatHornTypeIds::DREAM, GoatHornType::DREAM()); - } - - private function register(int $id, GoatHornType $type) : void{ - $this->idToEnum[$id] = $type; - $this->enumToId[$type->id()] = $id; - } - - public function fromId(int $id) : ?GoatHornType{ - return $this->idToEnum[$id] ?? null; - } - - public function toId(GoatHornType $type) : int{ - if(!isset($this->enumToId[$type->id()])){ - throw new \InvalidArgumentException("Type does not have a mapped ID"); + foreach(GoatHornType::cases() as $case){ + $this->register(match($case){ + GoatHornType::PONDER => GoatHornTypeIds::PONDER, + GoatHornType::SING => GoatHornTypeIds::SING, + GoatHornType::SEEK => GoatHornTypeIds::SEEK, + GoatHornType::FEEL => GoatHornTypeIds::FEEL, + GoatHornType::ADMIRE => GoatHornTypeIds::ADMIRE, + GoatHornType::CALL => GoatHornTypeIds::CALL, + GoatHornType::YEARN => GoatHornTypeIds::YEARN, + GoatHornType::DREAM => GoatHornTypeIds::DREAM + }, $case); } - return $this->enumToId[$type->id()]; } } diff --git a/src/item/GoatHorn.php b/src/item/GoatHorn.php index 5d3a5a77020..43fd4ff2dd6 100644 --- a/src/item/GoatHorn.php +++ b/src/item/GoatHorn.php @@ -23,21 +23,17 @@ namespace pocketmine\item; -use pocketmine\data\runtime\RuntimeDataWriter; +use pocketmine\data\runtime\RuntimeDataDescriber; use pocketmine\math\Vector3; use pocketmine\player\Player; +use pocketmine\world\sound\GoatHornSound; class GoatHorn extends Item implements Releasable{ - private GoatHornType $goatHornType; + private GoatHornType $goatHornType = GoatHornType::PONDER; - public function __construct(ItemIdentifier $identifier, string $name){ - $this->goatHornType = GoatHornType::PONDER(); - parent::__construct($identifier, $name); - } - - protected function encodeType(RuntimeDataWriter $w) : void{ - $w->goatHornType($this->goatHornType); + protected function describeState(RuntimeDataDescriber $w) : void{ + $w->enum($this->goatHornType); } public function getType() : GoatHornType{ return $this->goatHornType; } @@ -64,8 +60,8 @@ public function canStartUsingItem(Player $player) : bool{ public function onClickAir(Player $player, Vector3 $directionVector, array &$returnedItems) : ItemUseResult{ $position = $player->getPosition(); - $position->getWorld()->addSound($position, $this->goatHornType->getSound()); + $position->getWorld()->addSound($position, new GoatHornSound($this->goatHornType)); - return ItemUseResult::SUCCESS(); + return ItemUseResult::SUCCESS; } } diff --git a/src/item/GoatHornType.php b/src/item/GoatHornType.php index 5ae9395c255..0aa89cd4b5c 100644 --- a/src/item/GoatHornType.php +++ b/src/item/GoatHornType.php @@ -23,61 +23,32 @@ namespace pocketmine\item; -use pocketmine\utils\EnumTrait; -use pocketmine\world\sound\GoatHornAdmireSound; -use pocketmine\world\sound\GoatHornCallSound; -use pocketmine\world\sound\GoatHornDreamSound; -use pocketmine\world\sound\GoatHornFeelSound; -use pocketmine\world\sound\GoatHornPonderSound; -use pocketmine\world\sound\GoatHornSeekSound; -use pocketmine\world\sound\GoatHornSingSound; -use pocketmine\world\sound\GoatHornYearnSound; -use pocketmine\world\sound\Sound; +use pocketmine\network\mcpe\protocol\types\LevelSoundEvent; -/** - * This doc-block is generated automatically, do not modify it manually. - * This must be regenerated whenever registry members are added, removed or changed. - * @see build/generate-registry-annotations.php - * @generate-registry-docblock - * - * @method static GoatHornType ADMIRE() - * @method static GoatHornType CALL() - * @method static GoatHornType DREAM() - * @method static GoatHornType FEEL() - * @method static GoatHornType PONDER() - * @method static GoatHornType SEEK() - * @method static GoatHornType SING() - * @method static GoatHornType YEARN() - */ -final class GoatHornType{ - use EnumTrait { - __construct as Enum___construct; - } +enum GoatHornType{ - protected static function setup() : void{ - self::registerAll( - new self("ponder", fn() => new GoatHornPonderSound()), - new self("sing", fn() => new GoatHornSingSound()), - new self("seek", fn() => new GoatHornSeekSound()), - new self("feel", fn() => new GoatHornFeelSound()), - new self("admire", fn() => new GoatHornAdmireSound()), - new self("call", fn() => new GoatHornCallSound()), - new self("yearn", fn() => new GoatHornYearnSound()), - new self("dream", fn() => new GoatHornDreamSound()) - ); - } + case PONDER; + case SING; + case SEEK; + case FEEL; + case ADMIRE; + case CALL; + case YEARN; + case DREAM; /** - * @phpstan-param \Closure() : Sound $soundGetter + * @phpstan-return LevelSoundEvent::* */ - private function __construct( - string $enumName, - private \Closure $soundGetter - ){ - $this->Enum___construct($enumName); - } - - public function getSound() : Sound{ - return ($this->soundGetter)(); + public function getSoundId() : int{ + return match($this){ + self::PONDER => LevelSoundEvent::HORN_CALL0, + self::SING => LevelSoundEvent::HORN_CALL1, + self::SEEK => LevelSoundEvent::HORN_CALL2, + self::FEEL => LevelSoundEvent::HORN_CALL3, + self::ADMIRE => LevelSoundEvent::HORN_CALL4, + self::CALL => LevelSoundEvent::HORN_CALL5, + self::YEARN => LevelSoundEvent::HORN_CALL6, + self::DREAM => LevelSoundEvent::HORN_CALL7 + }; } } diff --git a/src/item/StringToItemParser.php b/src/item/StringToItemParser.php index 0e1a8744fd2..e2363ad951b 100644 --- a/src/item/StringToItemParser.php +++ b/src/item/StringToItemParser.php @@ -1165,8 +1165,8 @@ private static function registerDynamicItems(self $result) : void{ $result->register($prefix("dye"), fn() => Items::DYE()->setColor($color)); } - foreach(GoatHornType::getAll() as $goatHornType){ - $prefix = fn(string $name) => $goatHornType->name() . "_" . $name; + foreach(GoatHornType::cases() as $goatHornType){ + $prefix = fn(string $name) => strtolower($goatHornType->name) . "_" . $name; $result->register($prefix("goat_horn"), fn() => Items::GOAT_HORN()->setType($goatHornType)); } diff --git a/src/world/sound/GoatHornCallSound.php b/src/world/sound/GoatHornCallSound.php deleted file mode 100644 index 20deef6f65a..00000000000 --- a/src/world/sound/GoatHornCallSound.php +++ /dev/null @@ -1,35 +0,0 @@ -goatHornType->getSoundId(), $pos, false)]; } } diff --git a/src/world/sound/GoatHornYearnSound.php b/src/world/sound/GoatHornYearnSound.php deleted file mode 100644 index 3c4ada86060..00000000000 --- a/src/world/sound/GoatHornYearnSound.php +++ /dev/null @@ -1,35 +0,0 @@ - Date: Mon, 19 Feb 2024 18:27:11 -0500 Subject: [PATCH 07/10] getType => getHornType --- src/data/bedrock/item/ItemSerializerDeserializerRegistrar.php | 4 ++-- src/item/GoatHorn.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/data/bedrock/item/ItemSerializerDeserializerRegistrar.php b/src/data/bedrock/item/ItemSerializerDeserializerRegistrar.php index b83c8f3a8cb..02e60e8f6a3 100644 --- a/src/data/bedrock/item/ItemSerializerDeserializerRegistrar.php +++ b/src/data/bedrock/item/ItemSerializerDeserializerRegistrar.php @@ -494,9 +494,9 @@ function(Banner $item, int $meta) : void{ Ids::GOAT_HORN, Items::GOAT_HORN(), function(GoatHorn $item, int $meta) : void{ - $item->setType(GoatHornTypeIdMap::getInstance()->fromId($meta) ?? throw new ItemTypeDeserializeException("Unknown goat horn type ID $meta")); + $item->setHornType(GoatHornTypeIdMap::getInstance()->fromId($meta) ?? throw new ItemTypeDeserializeException("Unknown goat horn type ID $meta")); }, - fn(GoatHorn $item) => GoatHornTypeIdMap::getInstance()->toId($item->getType()) + fn(GoatHorn $item) => GoatHornTypeIdMap::getInstance()->toId($item->getHornType()) ); $this->map1to1ItemWithMeta( Ids::MEDICINE, diff --git a/src/item/GoatHorn.php b/src/item/GoatHorn.php index 43fd4ff2dd6..ca1b1b80ef8 100644 --- a/src/item/GoatHorn.php +++ b/src/item/GoatHorn.php @@ -36,12 +36,12 @@ protected function describeState(RuntimeDataDescriber $w) : void{ $w->enum($this->goatHornType); } - public function getType() : GoatHornType{ return $this->goatHornType; } + public function getHornType() : GoatHornType{ return $this->goatHornType; } /** * @return $this */ - public function setType(GoatHornType $type) : self{ + public function setHornType(GoatHornType $type) : self{ $this->goatHornType = $type; return $this; } From 206a2ca688518eaba71fcb44935cb2aa07ad8989 Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Mon, 19 Feb 2024 18:30:22 -0500 Subject: [PATCH 08/10] Forgot to change StringToItemParser :P --- src/item/StringToItemParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/item/StringToItemParser.php b/src/item/StringToItemParser.php index b7e37455732..3518eadfdc8 100644 --- a/src/item/StringToItemParser.php +++ b/src/item/StringToItemParser.php @@ -1168,7 +1168,7 @@ private static function registerDynamicItems(self $result) : void{ foreach(GoatHornType::cases() as $goatHornType){ $prefix = fn(string $name) => strtolower($goatHornType->name) . "_" . $name; - $result->register($prefix("goat_horn"), fn() => Items::GOAT_HORN()->setType($goatHornType)); + $result->register($prefix("goat_horn"), fn() => Items::GOAT_HORN()->setHornType($goatHornType)); } foreach(SuspiciousStewType::cases() as $suspiciousStewType){ From 965851d965ccf37c09faa98a42e3cc7539266edf Mon Sep 17 00:00:00 2001 From: IvanCraft623 Date: Tue, 20 Feb 2024 16:37:22 -0500 Subject: [PATCH 09/10] Remove GoatHornType::getSoundId() method --- src/item/GoatHornType.php | 18 ------------------ src/world/sound/GoatHornSound.php | 12 +++++++++++- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/item/GoatHornType.php b/src/item/GoatHornType.php index 0aa89cd4b5c..6c0c3b2f7fc 100644 --- a/src/item/GoatHornType.php +++ b/src/item/GoatHornType.php @@ -23,8 +23,6 @@ namespace pocketmine\item; -use pocketmine\network\mcpe\protocol\types\LevelSoundEvent; - enum GoatHornType{ case PONDER; @@ -35,20 +33,4 @@ enum GoatHornType{ case CALL; case YEARN; case DREAM; - - /** - * @phpstan-return LevelSoundEvent::* - */ - public function getSoundId() : int{ - return match($this){ - self::PONDER => LevelSoundEvent::HORN_CALL0, - self::SING => LevelSoundEvent::HORN_CALL1, - self::SEEK => LevelSoundEvent::HORN_CALL2, - self::FEEL => LevelSoundEvent::HORN_CALL3, - self::ADMIRE => LevelSoundEvent::HORN_CALL4, - self::CALL => LevelSoundEvent::HORN_CALL5, - self::YEARN => LevelSoundEvent::HORN_CALL6, - self::DREAM => LevelSoundEvent::HORN_CALL7 - }; - } } diff --git a/src/world/sound/GoatHornSound.php b/src/world/sound/GoatHornSound.php index 8b6b5fbe740..3987db3daee 100644 --- a/src/world/sound/GoatHornSound.php +++ b/src/world/sound/GoatHornSound.php @@ -26,11 +26,21 @@ use pocketmine\item\GoatHornType; use pocketmine\math\Vector3; use pocketmine\network\mcpe\protocol\LevelSoundEventPacket; +use pocketmine\network\mcpe\protocol\types\LevelSoundEvent; class GoatHornSound implements Sound{ public function __construct(private GoatHornType $goatHornType){} public function encode(Vector3 $pos) : array{ - return [LevelSoundEventPacket::nonActorSound($this->goatHornType->getSoundId(), $pos, false)]; + return [LevelSoundEventPacket::nonActorSound(match($this->goatHornType){ + GoatHornType::PONDER => LevelSoundEvent::HORN_CALL0, + GoatHornType::SING => LevelSoundEvent::HORN_CALL1, + GoatHornType::SEEK => LevelSoundEvent::HORN_CALL2, + GoatHornType::FEEL => LevelSoundEvent::HORN_CALL3, + GoatHornType::ADMIRE => LevelSoundEvent::HORN_CALL4, + GoatHornType::CALL => LevelSoundEvent::HORN_CALL5, + GoatHornType::YEARN => LevelSoundEvent::HORN_CALL6, + GoatHornType::DREAM => LevelSoundEvent::HORN_CALL7 + }, $pos, false)]; } } From 705d2d620e2553e867de591cf03e06587b5658fe Mon Sep 17 00:00:00 2001 From: ipad54 <63200545+ipad54@users.noreply.github.com> Date: Mon, 11 Nov 2024 13:44:34 +0300 Subject: [PATCH 10/10] Added missing cooldown --- src/item/GoatHorn.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/item/GoatHorn.php b/src/item/GoatHorn.php index ca1b1b80ef8..088701e3986 100644 --- a/src/item/GoatHorn.php +++ b/src/item/GoatHorn.php @@ -54,6 +54,10 @@ public function getCooldownTicks() : int{ return 140; } + public function getCooldownTag() : ?string{ + return ItemCooldownTags::GOAT_HORN; + } + public function canStartUsingItem(Player $player) : bool{ return true; }