Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement enchanting system #5953

Merged
merged 97 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
741486e
Add an enchanting system
S3v3Nice Jul 31, 2023
a5e12e8
Merge branch 'enchanting' into minor-next
S3v3Nice Jul 31, 2023
54dfd70
Register incompatibility for Fortune and Silk Touch enchantments
S3v3Nice Jul 31, 2023
774fde8
Fix phpstan errors
S3v3Nice Jul 31, 2023
50688e1
Fix phpstan errors 2
S3v3Nice Jul 31, 2023
1ed7559
Fix code style
S3v3Nice Jul 31, 2023
7066c22
Changes on review
S3v3Nice Aug 1, 2023
ef2a87d
Add isDyeable to ArmorMaterial
S3v3Nice Aug 1, 2023
2e63d39
Small code style fix
S3v3Nice Aug 1, 2023
1409be2
Changes on review; Add EnchantmentOption
S3v3Nice Aug 1, 2023
9754989
Small code style fix
S3v3Nice Aug 1, 2023
f26b11d
Small phpstan fix
S3v3Nice Aug 1, 2023
6767a8f
Merge branch 'minor-next' into minor-next
S3v3Nice Aug 1, 2023
f941d23
Small code style fix
S3v3Nice Aug 1, 2023
c920898
Move send PlayerEnchantOptionsPacket logic to NetworkSession
S3v3Nice Aug 1, 2023
0ec6773
Changes on review
S3v3Nice Aug 1, 2023
0c351df
Very small code style fix
S3v3Nice Aug 1, 2023
a07056f
Specify enchantmentFlag of carved pumpkin and mob head in their own c…
S3v3Nice Aug 1, 2023
189c9ed
Fix default value for $minCost and $maxCost in Enchantment
S3v3Nice Aug 1, 2023
7a673b0
Prevent crash on incorrect enchantment option id being sent by client
S3v3Nice Aug 1, 2023
27dfe40
Remove output caching (`prepareOutput`) and `getLapis` from EnchantIn…
S3v3Nice Aug 1, 2023
e671f54
Merge branch 'minor-next' into minor-next
S3v3Nice Aug 1, 2023
f90d5e3
Changes on review
S3v3Nice Aug 1, 2023
5446787
Merge remote-tracking branch 'origin/minor-next' into minor-next
S3v3Nice Aug 1, 2023
e3e8595
Fix `EnchantmentHelper::getEnchantability`
S3v3Nice Aug 2, 2023
bb3a2ed
Changes on review
S3v3Nice Aug 2, 2023
84d3f47
Changes on review
S3v3Nice Aug 3, 2023
777f83d
Remove @var from EnchantmentHelper
S3v3Nice Aug 3, 2023
c1a8e70
Changes on review
S3v3Nice Aug 3, 2023
dd4b577
Make the enchantment incompatibility system tag-based
S3v3Nice Aug 3, 2023
0654da6
Add `Human::setXpSeed`, Remove `Player::onEnchant`
S3v3Nice Aug 3, 2023
2d9e622
Add EnchantingTableOptionRegistry; Remove `Enchantment::isTreasure`
S3v3Nice Aug 3, 2023
82ab503
Rename `minCost`, `maxCost` to `minEnchantingPower`, `maxEnchantingPo…
S3v3Nice Aug 3, 2023
1107ad1
Add PlayerEnchantmentOptionsRequestEvent and some changes
S3v3Nice Aug 3, 2023
b6fb6d5
Fix code style
S3v3Nice Aug 3, 2023
3fd94aa
Add `Item::getEnchantability` and some other changes
S3v3Nice Aug 4, 2023
e079ccb
Small fixes
S3v3Nice Aug 4, 2023
686ca71
Rename `enchantmentOptions` to `options` in PlayerEnchantmentOptionsR…
S3v3Nice Aug 4, 2023
1f3a62d
PlayerEnchantmentOptionsRequestEvent: Remove `item` and `enchantmentT…
S3v3Nice Aug 4, 2023
99e6bf4
Add `EnchantInventory::getLapis`
S3v3Nice Aug 4, 2023
386a595
Changes on review
S3v3Nice Aug 4, 2023
b2c5be0
Changes on review
S3v3Nice Aug 5, 2023
aead55c
Specify `$enchantments` parameter type in EnchantmentHelper
S3v3Nice Aug 5, 2023
a6f5cc3
Merge branch 'minor-next' into minor-next
S3v3Nice Aug 5, 2023
971e67f
Edit `EnchantItemEvent::getCost` doc
S3v3Nice Aug 5, 2023
7e7e152
Some code cleanup in EnchantInventory
S3v3Nice Aug 7, 2023
5e82fd6
Fix Pumpkin and LitPumpkin break info
S3v3Nice Aug 7, 2023
1c18017
Changes on review
S3v3Nice Aug 7, 2023
4616416
Specify that result of `EnchantInventory::getOptionCost` is not null …
S3v3Nice Aug 7, 2023
ba57dca
Make ItemStackRequestExecutor subtract option id offset
S3v3Nice Aug 7, 2023
1c0be37
Complete remove $optionIds from `NetworkSession::sendEnchantOptions`
S3v3Nice Aug 7, 2023
36aa974
Remove unnecessary second use of EnchantmentHelper in EnchantInventory
S3v3Nice Aug 8, 2023
7d054bc
Merge branch 'minor-next' into minor-next
S3v3Nice Aug 8, 2023
115c168
Use double quotes instead of single quotes for strings
S3v3Nice Aug 8, 2023
96fbba5
New item enchantment tag system
S3v3Nice Aug 8, 2023
e6bae92
Fix code style and phpstan errors
S3v3Nice Aug 8, 2023
a4ab760
Changes on review
S3v3Nice Aug 8, 2023
815d644
Fix code style
S3v3Nice Aug 8, 2023
0090328
Changes on review
S3v3Nice Aug 8, 2023
03883ec
Fix code style and phpstan errors
S3v3Nice Aug 8, 2023
0624be8
Fix validation for already set `$this->inputItem` in EnchantTransaction
S3v3Nice Aug 8, 2023
05c4c52
Merge branch 'minor-next' into minor-next
S3v3Nice Aug 8, 2023
6be6a1d
Fix docs of `Block::getEnchantmentTags` and `Item::getEnchantmentTags`
S3v3Nice Aug 8, 2023
b8583d3
Rename `getXpSeed`, `setXpSeed`, `generateXpSeed` to `getEnchantmentS…
S3v3Nice Aug 8, 2023
c4f1618
Small code cleanup and enhancements
S3v3Nice Aug 8, 2023
a4374dd
Convert ArmorMaterial from 'enum' to simple class; Add VanillaArmorMa…
S3v3Nice Aug 9, 2023
87759b6
Remove unnecessary `BREAKABLE` item enchantment tag
S3v3Nice Aug 9, 2023
2422a1b
Change EnchantingTableOptionRegistry to AvailableEnchantmentRegistry
S3v3Nice Aug 9, 2023
2f0d409
Fix code style
S3v3Nice Aug 9, 2023
7856d9b
Add `isAvailableForItem` into AvailableEnchantmentRegistry
S3v3Nice Aug 9, 2023
a5e16e7
Merge branch 'minor-next' into minor-next
S3v3Nice Aug 9, 2023
b2216dc
Remove `$onlyPrimaryTags` param, Add `getAvailableEnchantingTableEnch…
S3v3Nice Aug 9, 2023
835f1ff
Rename two methods in AvailableEnchantmentRegistry
S3v3Nice Aug 10, 2023
ddb534d
Rename `getNonGroupingTags` to `getLeafTags` in ItemEnchantmentTagReg…
S3v3Nice Aug 10, 2023
3fe1bda
Optimize `getLeafTags` in ItemEnchantmentTagRegistry
S3v3Nice Aug 10, 2023
8c9c409
Fix code style and phpstan errors
S3v3Nice Aug 10, 2023
9d6a762
Optimize `isTagArraySubset`, make `getLeafTags` private in ItemEnchan…
S3v3Nice Aug 10, 2023
7f51c37
Place `getLeafTagsForArray` below `isTagArraySubset` in ItemEnchantme…
S3v3Nice Aug 10, 2023
6698b40
Put assertion in `unregister` later in ItemEnchantmentTagRegistry
S3v3Nice Aug 10, 2023
73b1cc7
Improve documentation for two methods in AvailableEnchantmentRegistry
S3v3Nice Aug 10, 2023
fac7e28
Merge branch 'minor-next' into minor-next
S3v3Nice Aug 10, 2023
5c6114e
Adjust the exception message in `ItemEnchantmentTagRegistry::assertNo…
S3v3Nice Aug 10, 2023
8c476d3
Small cleanup for `AvailableEnchantmentRegistry::getEnchantingTableEn…
S3v3Nice Aug 10, 2023
7343a8a
Add missing documentation for `ToolTier::getEnchantability` and `Bloc…
S3v3Nice Aug 10, 2023
de1e1be
Better handling of enchantment recipe IDs
dktapps Aug 15, 2023
9d79ac4
Change `isTagArraySubset` to `isTagArrayIntersection` in ItemEnchantm…
S3v3Nice Aug 15, 2023
c9e0dff
Fix phpstan error
S3v3Nice Aug 15, 2023
c67ebd8
Fix @see in docs of `getEnchantmentTags`
S3v3Nice Aug 15, 2023
fee0085
Revert LEATHER as the default armor material
S3v3Nice Aug 15, 2023
e136faa
Limit the number of enchantment options in PlayerEnchantmentOptionsRe…
S3v3Nice Aug 15, 2023
9c49c5d
Update docs for `getEnchantmentTags`
S3v3Nice Aug 15, 2023
cb0c2c7
EnchantmentHelper: do not check space twice if bookshelves are stacked
dktapps Aug 15, 2023
27962fa
Rename and move some stuff
dktapps Aug 15, 2023
1524b23
Use the conventional way of passing player for player events
dktapps Aug 15, 2023
afcf067
Docs cleanup, rename some things
dktapps Aug 15, 2023
51d9424
IncompatibleEnchantmentRegistry: Avoid unncessary array_searches
dktapps Aug 15, 2023
87edda4
Added WEAPONS tag
dktapps Aug 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/block/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\entity\Entity;
use pocketmine\entity\projectile\Projectile;
use pocketmine\item\enchantment\ItemEnchantmentFlags;
use pocketmine\item\enchantment\VanillaEnchantments;
use pocketmine\item\Item;
use pocketmine\item\ItemBlock;
Expand Down Expand Up @@ -67,6 +68,7 @@ class Block{
protected BlockIdentifier $idInfo;
protected string $fallbackName;
protected BlockTypeInfo $typeInfo;
protected int $itemEnchantmentFlag;
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
protected Position $position;

/** @var AxisAlignedBB[]|null */
Expand All @@ -80,10 +82,11 @@ class Block{
/**
* @param string $name English name of the block type (TODO: implement translations)
*/
public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo){
public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, int $itemEnchantmentFlag = ItemEnchantmentFlags::NONE){
$this->idInfo = $idInfo;
$this->fallbackName = $name;
$this->typeInfo = $typeInfo;
$this->itemEnchantmentFlag = $itemEnchantmentFlag;
$this->position = new Position(0, 0, 0, null);

$calculator = new RuntimeDataSizeCalculator();
Expand Down
2 changes: 1 addition & 1 deletion src/block/EnchantingTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player
if($player instanceof Player){
//TODO lock

$player->setCurrentWindow(new EnchantInventory($this->position));
$player->setCurrentWindow(new EnchantInventory($this->position, $player));
}

return true;
Expand Down
3 changes: 2 additions & 1 deletion src/block/VanillaBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
use pocketmine\block\utils\WoodType;
use pocketmine\crafting\FurnaceType;
use pocketmine\entity\projectile\Projectile;
use pocketmine\item\enchantment\ItemEnchantmentFlags;
use pocketmine\item\Item;
use pocketmine\item\ToolTier;
use pocketmine\math\Facing;
Expand Down Expand Up @@ -964,7 +965,7 @@ public function getBreakTime(Item $item) : float{

$pumpkinBreakInfo = new Info(BreakInfo::axe(1.0));
self::register("pumpkin", new Pumpkin(new BID(Ids::PUMPKIN), "Pumpkin", $pumpkinBreakInfo));
self::register("carved_pumpkin", new CarvedPumpkin(new BID(Ids::CARVED_PUMPKIN), "Carved Pumpkin", $pumpkinBreakInfo));
self::register("carved_pumpkin", new CarvedPumpkin(new BID(Ids::CARVED_PUMPKIN), "Carved Pumpkin", $pumpkinBreakInfo, ItemEnchantmentFlags::HEAD));
self::register("lit_pumpkin", new LitPumpkin(new BID(Ids::LIT_PUMPKIN), "Jack o'Lantern", $pumpkinBreakInfo));

self::register("pumpkin_stem", new PumpkinStem(new BID(Ids::PUMPKIN_STEM), "Pumpkin Stem", new Info(BreakInfo::instant())));
Expand Down
75 changes: 74 additions & 1 deletion src/block/inventory/EnchantInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@

namespace pocketmine\block\inventory;

use pocketmine\data\bedrock\EnchantmentIdMap;
use pocketmine\inventory\SimpleInventory;
use pocketmine\inventory\TemporaryInventory;
use pocketmine\item\enchantment\EnchantmentHelper as Helper;
use pocketmine\item\enchantment\EnchantmentInstance;
use pocketmine\item\Item;
use pocketmine\item\ItemTypeIds;
use pocketmine\item\VanillaItems;
use pocketmine\network\mcpe\protocol\PlayerEnchantOptionsPacket;
use pocketmine\network\mcpe\protocol\types\Enchant;
use pocketmine\network\mcpe\protocol\types\EnchantOption;
use pocketmine\player\Player;
use pocketmine\world\Position;

class EnchantInventory extends SimpleInventory implements BlockInventory, TemporaryInventory{
Expand All @@ -33,8 +43,71 @@ class EnchantInventory extends SimpleInventory implements BlockInventory, Tempor
public const SLOT_INPUT = 0;
public const SLOT_LAPIS = 1;

public function __construct(Position $holder){
private Player $player;

/** @var EnchantOption[] $options */
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
private array $options = [];
private array $outputs = [];

public function __construct(Position $holder, Player $player){
$this->holder = $holder;
$this->player = $player;
parent::__construct(2);
}

protected function onSlotChange(int $index, Item $before) : void{
if($index == self::SLOT_INPUT){
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
$this->options = Helper::getEnchantOptions($this->holder, $this->getItem(self::SLOT_INPUT), $this->player->getXpSeed());
$this->outputs = [];

if(!empty($this->options)){
$this->player->getNetworkSession()->sendDataPacket(PlayerEnchantOptionsPacket::create($this->options));
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
}
}

parent::onSlotChange($index, $before);
}

public function getInput() : ?Item{
return $this->getItem(self::SLOT_INPUT);
}

public function getLapis() : ?Item{
return $this->getItem(self::SLOT_LAPIS);
}

public function getOutput(int $optionId) : Item{
$this->prepareOutput($optionId);
return $this->outputs[$optionId];
}

private function prepareOutput(int $optionId) : void{
if(isset($this->outputs[$optionId])){
return;
}

$option = $this->options[$optionId] ?? null;
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
if(is_null($option)){
throw new \RuntimeException("Failed to find enchantment option with network id $optionId");
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
}

$enchantments = array_map(
fn(Enchant $e) => new EnchantmentInstance(EnchantmentIdMap::getInstance()->fromId($e->getId()), $e->getLevel()),
array_merge(
$option->getEquipActivatedEnchantments(),
$option->getHeldActivatedEnchantments(),
$option->getSelfActivatedEnchantments()
)
);

$outputItem = $this->getItem(self::SLOT_INPUT);
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
if($outputItem->getTypeId() === ItemTypeIds::BOOK){
$outputItem = VanillaItems::ENCHANTED_BOOK();
}
foreach($enchantments as $enchantment){
$outputItem->addEnchantment($enchantment);
}

$this->outputs[$optionId] = $outputItem;
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ private function register1to1ItemMappings() : void{
$this->map1to1Item(Ids::ECHO_SHARD, Items::ECHO_SHARD());
$this->map1to1Item(Ids::EGG, Items::EGG());
$this->map1to1Item(Ids::EMERALD, Items::EMERALD());
$this->map1to1Item(Ids::ENCHANTED_BOOK, Items::ENCHANTED_BOOK());
$this->map1to1Item(Ids::ENCHANTED_GOLDEN_APPLE, Items::ENCHANTED_GOLDEN_APPLE());
$this->map1to1Item(Ids::ENDER_PEARL, Items::ENDER_PEARL());
$this->map1to1Item(Ids::EXPERIENCE_BOTTLE, Items::EXPERIENCE_BOTTLE());
Expand Down
10 changes: 9 additions & 1 deletion src/entity/Human.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ public function getXpManager() : ExperienceManager{
return $this->xpManager;
}

public function getXpSeed() : int{
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
return $this->xpSeed;
}

protected function setRandomXpSeed() : void{
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
$this->xpSeed = random_int(Limits::INT32_MIN, Limits::INT32_MAX);
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
}

public function getXpDropAmount() : int{
//this causes some XP to be lost on death when above level 1 (by design), dropping at most enough points for
//about 7.5 levels of XP.
Expand Down Expand Up @@ -334,7 +342,7 @@ function(Inventory $unused, array $oldItems) use ($syncHeldItem) : void{
if(($xpSeedTag = $nbt->getTag(self::TAG_XP_SEED)) instanceof IntTag){
$this->xpSeed = $xpSeedTag->getValue();
}else{
$this->xpSeed = random_int(Limits::INT32_MIN, Limits::INT32_MAX);
$this->setRandomXpSeed();
}
}

Expand Down
88 changes: 88 additions & 0 deletions src/inventory/transaction/EnchantTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\inventory\transaction;

use pocketmine\block\inventory\EnchantInventory;
use pocketmine\inventory\transaction\action\InventoryAction;
use pocketmine\item\Item;
use pocketmine\item\ItemTypeIds;
use pocketmine\player\Player;

class EnchantTransaction extends InventoryTransaction{

private int $optionId;
private ?Item $inputItem = null;
private int $lapisCost = 0;

public function __construct(Player $source, int $optionId){
$this->optionId = $optionId;
parent::__construct($source);
}

public function validate() : void{
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
if(count($this->actions) < 1){
throw new TransactionValidationException("Transaction must have at least one action to be executable");
}

/** @var EnchantInventory $enchantWindow */
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
$enchantWindow = $this->source->getCurrentWindow();
if(!$this->inputItem->equalsExact($enchantWindow->getInput())){
throw new TransactionValidationException("Incorrect input item");
}

if(!$this->source->isCreative()){
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
$enchantLevel = $this->getEnchantLevel();
if($this->lapisCost !== $enchantLevel){
throw new TransactionValidationException("Expected the amount of lapis lazuli spent to be {$this->getEnchantLevel()}, but received $this->lapisCost");
}

$xpLevel = $this->source->getXpManager()->getXpLevel();
if($xpLevel < $enchantLevel){
throw new TransactionValidationException("Expected player to have xp level at least of $enchantLevel, but received $xpLevel");
}
}
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
}

public function addAction(InventoryAction $action) : void{
parent::addAction($action);

$sourceItem = $action->getSourceItem();
$targetItem = $action->getTargetItem();

if($sourceItem->getTypeId() === ItemTypeIds::LAPIS_LAZULI){
$this->lapisCost = $sourceItem->getCount() - $targetItem->getCount();
}else{
$this->inputItem = $sourceItem;
}
}

public function execute() : void{
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved
parent::execute();
$this->source->onEnchant($this->getEnchantLevel());
}

public function getEnchantLevel() : int{
return $this->optionId + 1;
}
}
5 changes: 3 additions & 2 deletions src/item/Armor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use pocketmine\color\Color;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\inventory\ArmorInventory;
use pocketmine\item\enchantment\ItemEnchantmentFlags;
use pocketmine\item\enchantment\ProtectionEnchantment;
use pocketmine\item\enchantment\VanillaEnchantments;
use pocketmine\math\Vector3;
Expand All @@ -44,8 +45,8 @@ class Armor extends Durable{

protected ?Color $customColor = null;

public function __construct(ItemIdentifier $identifier, string $name, ArmorTypeInfo $info){
parent::__construct($identifier, $name);
public function __construct(ItemIdentifier $identifier, string $name, ArmorTypeInfo $info, int $enchantmentFlag = ItemEnchantmentFlags::NONE){
parent::__construct($identifier, $name, $enchantmentFlag);
$this->armorInfo = $info;
}

Expand Down
30 changes: 30 additions & 0 deletions src/item/EnchantedBook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\item;

class EnchantedBook extends Item{
public function getMaxStackSize() : int{
return 1;
}
}
8 changes: 7 additions & 1 deletion src/item/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use pocketmine\entity\Entity;
use pocketmine\entity\Living;
use pocketmine\item\enchantment\EnchantmentInstance;
use pocketmine\item\enchantment\ItemEnchantmentFlags;
use pocketmine\math\Vector3;
use pocketmine\nbt\LittleEndianNbtSerializer;
use pocketmine\nbt\NBT;
Expand Down Expand Up @@ -110,7 +111,8 @@ class Item implements \JsonSerializable{
*/
public function __construct(
private ItemIdentifier $identifier,
protected string $name = "Unknown"
protected string $name = "Unknown",
private int $enchantmentFlag = ItemEnchantmentFlags::NONE
){
$this->nbt = new CompoundTag();
}
Expand Down Expand Up @@ -455,6 +457,10 @@ public function getVanillaName() : string{
return $this->name;
}

public function getEnchantmentFlag() : int{
return $this->enchantmentFlag;
}
S3v3Nice marked this conversation as resolved.
Show resolved Hide resolved

final public function canBePlaced() : bool{
return $this->getBlock()->canBePlaced();
}
Expand Down
6 changes: 4 additions & 2 deletions src/item/ItemBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use pocketmine\block\Block;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\item\enchantment\ItemEnchantmentFlags;

/**
* Class used for Items that directly represent blocks, such as stone, dirt, wood etc.
Expand All @@ -34,9 +35,10 @@
*/
final class ItemBlock extends Item{
public function __construct(
private Block $block
private Block $block,
int $enchantmentFlag = ItemEnchantmentFlags::NONE
){
parent::__construct(ItemIdentifier::fromBlock($block), $block->getName());
parent::__construct(ItemIdentifier::fromBlock($block), $block->getName(), $enchantmentFlag);
}

protected function describeState(RuntimeDataDescriber $w) : void{
Expand Down
3 changes: 2 additions & 1 deletion src/item/ItemTypeIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,9 @@ private function __construct(){
public const MANGROVE_BOAT = 20264;
public const GLOW_BERRIES = 20265;
public const CHERRY_SIGN = 20266;
public const ENCHANTED_BOOK = 20267;

public const FIRST_UNUSED_ITEM_ID = 20267;
public const FIRST_UNUSED_ITEM_ID = 20268;

private static int $nextDynamicId = self::FIRST_UNUSED_ITEM_ID;

Expand Down
1 change: 1 addition & 0 deletions src/item/StringToItemParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,7 @@ private static function registerItems(self $result) : void{
$result->register("egg", fn() => Items::EGG());
$result->register("elixir", fn() => Items::MEDICINE()->setType(MedicineType::ELIXIR()));
$result->register("emerald", fn() => Items::EMERALD());
$result->register("enchanted_book", fn() => Items::ENCHANTED_BOOK());
$result->register("enchanted_golden_apple", fn() => Items::ENCHANTED_GOLDEN_APPLE());
$result->register("enchanting_bottle", fn() => Items::EXPERIENCE_BOTTLE());
$result->register("ender_pearl", fn() => Items::ENDER_PEARL());
Expand Down
Loading