Skip to content

Commit 2c7043d

Browse files
Add validation for negative XP cost in AnvilCraftResult
1 parent 084feae commit 2c7043d

File tree

5 files changed

+10
-6
lines changed

5 files changed

+10
-6
lines changed

src/block/utils/AnvilHelper.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ final class AnvilHelper{
3636
* Returns null if the operation can't do anything.
3737
*/
3838
public static function calculateResult(Item $base, Item $material, ?string $customName, bool $isCreative) : ?AnvilCraftResult{
39-
4039
$recipe = Server::getInstance()->getCraftingManager()->matchAnvilRecipe($base, $material);
4140
if($recipe === null){
4241
return null;

src/crafting/AnvilCraftResult.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@
3030
*/
3131
final class AnvilCraftResult{
3232
/**
33+
* @param int $xpCost The experience points cost required to craft the output item. (positive integer, 0 means no cost)
34+
* @param Item $output The item given as output of the crafting process.
3335
* @param Item|null $sacrificeResult If the given item is considered as null (count <= 0), the value will be set to null.
3436
*/
3537
public function __construct(
3638
private int $xpCost,
3739
private Item $output,
3840
private ?Item $sacrificeResult
3941
){
42+
if($this->xpCost < 0){
43+
throw new \InvalidArgumentException("XP cost cannot be negative");
44+
}
4045
if($this->sacrificeResult !== null && $this->sacrificeResult->isNull()){
4146
$this->sacrificeResult = null;
4247
}

src/crafting/CraftingManagerFromDataHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public static function make(string $directoryPath) : CraftingManager{
211211
$result = new CraftingManager();
212212

213213
foreach(self::loadJsonArrayOfObjectsFile(Path::join($directoryPath, 'shapeless_crafting.json'), ShapelessRecipeData::class) as $recipe){
214-
$recipeType = match ($recipe->block) {
214+
$recipeType = match($recipe->block){
215215
"crafting_table" => ShapelessRecipeType::CRAFTING,
216216
"stonecutter" => ShapelessRecipeType::STONECUTTER,
217217
"smithing_table" => ShapelessRecipeType::SMITHING,
@@ -272,7 +272,7 @@ public static function make(string $directoryPath) : CraftingManager{
272272
));
273273
}
274274
foreach(self::loadJsonArrayOfObjectsFile(Path::join($directoryPath, 'smelting.json'), FurnaceRecipeData::class) as $recipe){
275-
$furnaceType = match ($recipe->block) {
275+
$furnaceType = match ($recipe->block){
276276
"furnace" => FurnaceType::FURNACE,
277277
"blast_furnace" => FurnaceType::BLAST_FURNACE,
278278
"smoker" => FurnaceType::SMOKER,

src/crafting/ItemCombineRecipe.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ public function getResultFor(Item $input, Item $material) : ?AnvilCraftResult{
8787
$xpCost += (int) floor($costAddition * $levelDifference);
8888
$result->addEnchantment($instance);
8989

90-
$xpCost += 2 ** $input->getAnvilRepairCost() - 1;
91-
$xpCost += 2 ** $material->getAnvilRepairCost() - 1;
90+
$xpCost += (2 ** $input->getAnvilRepairCost()) - 1;
91+
$xpCost += (2 ** $material->getAnvilRepairCost()) - 1;
9292
$result->setAnvilRepairCost(
9393
max($result->getAnvilRepairCost(), $material->getAnvilRepairCost()) + 1
9494
);

src/inventory/transaction/AnvilTransaction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function execute() : void{
145145

146146
protected function callExecuteEvent() : bool{
147147
if($this->baseItem === null){
148-
throw new AssumptionFailedError("Expected that baseItem are not null before executing the event");
148+
throw new AssumptionFailedError("Expected that baseItem is not null before executing the event");
149149
}
150150

151151
$ev = new PlayerUseAnvilEvent($this->source, $this->baseItem, $this->materialItem, $this->expectedResult->getOutput(), $this->customName, $this->expectedResult->getXpCost());

0 commit comments

Comments
 (0)