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

Implemented smithing table functionality #6202

Open
wants to merge 13 commits into
base: minor-next
Choose a base branch
from
28 changes: 27 additions & 1 deletion src/block/inventory/SmithingTableInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,39 @@

use pocketmine\inventory\SimpleInventory;
use pocketmine\inventory\TemporaryInventory;
use pocketmine\item\Item;
use pocketmine\Server;
use pocketmine\world\Position;

final class SmithingTableInventory extends SimpleInventory implements BlockInventory, TemporaryInventory{
use BlockInventoryTrait;

public const SLOT_INPUT = 0;

public const SLOT_ADDITION = 1;

public const SLOT_TEMPLATE = 2;

public function __construct(Position $holder){
$this->holder = $holder;
parent::__construct(3);
parent::__construct(4);
HimmelKreis4865 marked this conversation as resolved.
Show resolved Hide resolved
}

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

public function getAddition() : Item{
return $this->getItem(self::SLOT_ADDITION);
}

public function getTemplate() : Item{
return $this->getItem(self::SLOT_TEMPLATE);
}

public function getOutput() : ?Item{
$craftingManager = Server::getInstance()->getCraftingManager();
$recipe = $craftingManager->matchSmithingRecipe($this->getInput(), $this->getAddition(), $this->getTemplate());
return $recipe?->constructOutput($this->getInput(), $this->getAddition(), $this->getTemplate());
}
HimmelKreis4865 marked this conversation as resolved.
Show resolved Hide resolved
}
31 changes: 31 additions & 0 deletions src/crafting/CraftingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ class CraftingManager{
*/
protected array $potionTypeRecipes = [];

/**
* @var SmithingRecipe[]
* @phpstan-var list<SmithingRecipe>
*/
protected array $smithingRecipes = [];

/**
* @var PotionContainerChangeRecipe[]
* @phpstan-var list<PotionContainerChangeRecipe>
Expand Down Expand Up @@ -197,6 +203,14 @@ public function getPotionContainerChangeRecipes() : array{
return $this->potionContainerChangeRecipes;
}

/**
* @return SmithingRecipe[]
* @phpstan-return list<SmithingRecipe>
*/
public function getSmithingRecipes() : array{
return $this->smithingRecipes;
}

public function registerShapedRecipe(ShapedRecipe $recipe) : void{
$this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
$this->craftingRecipeIndex[] = $recipe;
Expand Down Expand Up @@ -231,6 +245,14 @@ public function registerPotionContainerChangeRecipe(PotionContainerChangeRecipe
}
}

public function registerSmithingRecipe(SmithingRecipe $recipe) : void{
$this->smithingRecipes[] = $recipe;

foreach($this->recipeRegisteredCallbacks as $callback){
$callback();
}
}

/**
* @param Item[] $outputs
*/
Expand Down Expand Up @@ -304,4 +326,13 @@ public function matchBrewingRecipe(Item $input, Item $ingredient) : ?BrewingReci

return null;
}

public function matchSmithingRecipe(Item $input, Item $addition, Item $template) : ?SmithingRecipe{
HimmelKreis4865 marked this conversation as resolved.
Show resolved Hide resolved
foreach($this->smithingRecipes as $recipe){
if($recipe->getInput()->accepts($input) && $recipe->getAddition()->accepts($addition) && $recipe->getTemplate()->accepts($template)){
return $recipe;
}
}
return null;
}
}
23 changes: 21 additions & 2 deletions src/crafting/CraftingManagerFromDataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use pocketmine\crafting\json\RecipeIngredientData;
use pocketmine\crafting\json\ShapedRecipeData;
use pocketmine\crafting\json\ShapelessRecipeData;
use pocketmine\crafting\json\SmithingTransformRecipeData;
use pocketmine\crafting\json\SmithingTrimRecipeData;
use pocketmine\data\bedrock\block\BlockStateData;
use pocketmine\data\bedrock\item\BlockItemIdMap;
use pocketmine\data\bedrock\item\ItemTypeDeserializeException;
Expand Down Expand Up @@ -292,7 +294,6 @@ public static function make(string $directoryPath) : CraftingManager{
$input
));
}

HimmelKreis4865 marked this conversation as resolved.
Show resolved Hide resolved
foreach(self::loadJsonArrayOfObjectsFile(Path::join($directoryPath, 'potion_type.json'), PotionTypeRecipeData::class) as $recipe){
$input = self::deserializeIngredient($recipe->input);
$ingredient = self::deserializeIngredient($recipe->ingredient);
Expand Down Expand Up @@ -329,9 +330,27 @@ public static function make(string $directoryPath) : CraftingManager{
$outputId
));
}
foreach(self::loadJsonArrayOfObjectsFile(Path::join($directoryPath, 'smithing.json'), SmithingTransformRecipeData::class) as $recipe){
$input = self::deserializeIngredient($recipe->input);
$template = self::deserializeIngredient($recipe->template);
$addition = self::deserializeIngredient($recipe->addition);
$output = self::deserializeItemStack($recipe->output);

//TODO: smithing
if($input === null || $template === null || $addition === null || $output === null){
continue;
}
$result->registerSmithingRecipe(new SmithingTransformRecipe($input, $addition, $template, $output));
}
foreach(self::loadJsonArrayOfObjectsFile(Path::join($directoryPath, 'smithing_trim.json'), SmithingTrimRecipeData::class) as $recipe){
$input = self::deserializeIngredient($recipe->input);
$addition = self::deserializeIngredient($recipe->addition);
$template = self::deserializeIngredient($recipe->template);

if($input === null || $template === null || $addition === null){
continue;
}
$result->registerSmithingRecipe(new SmithingTrimRecipe($input, $addition, $template));
}
return $result;
}
}
37 changes: 37 additions & 0 deletions src/crafting/SmithingRecipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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\crafting;

use pocketmine\item\Item;

abstract class SmithingRecipe{
HimmelKreis4865 marked this conversation as resolved.
Show resolved Hide resolved

abstract public function getInput() : RecipeIngredient;

abstract public function getAddition() : RecipeIngredient;

abstract public function getTemplate() : RecipeIngredient;

abstract public function constructOutput(Item $input, Item $addition, Item $template) : ?Item;
HimmelKreis4865 marked this conversation as resolved.
Show resolved Hide resolved
}
58 changes: 58 additions & 0 deletions src/crafting/SmithingTransformRecipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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\crafting;

use pocketmine\item\Item;

class SmithingTransformRecipe extends SmithingRecipe{

public function __construct(
private RecipeIngredient $input,
private RecipeIngredient $addition,
private RecipeIngredient $template,
private Item $result
){
$this->result = clone $this->result;
}

public function getInput() : RecipeIngredient{
return $this->input;
}

public function getAddition() : RecipeIngredient{
return $this->addition;
}

public function getTemplate() : RecipeIngredient{
return $this->template;
}

public function getResult() : Item{
return clone $this->result;
}

public function constructOutput(Item $input, Item $addition, Item $template) : ?Item{
return $this->getResult()->setNamedTag($input->getNamedTag());
ShockedPlot7560 marked this conversation as resolved.
Show resolved Hide resolved
}
}
61 changes: 61 additions & 0 deletions src/crafting/SmithingTrimRecipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\crafting;

use pocketmine\item\Armor;
use pocketmine\item\ArmorTrim;
use pocketmine\item\ArmorTrimMaterial;
use pocketmine\item\ArmorTrimPattern;
use pocketmine\item\Item;

class SmithingTrimRecipe extends SmithingRecipe{

public function __construct(
private RecipeIngredient $input,
private RecipeIngredient $addition,
private RecipeIngredient $template){
}

public function getInput() : RecipeIngredient{
return $this->input;
}

public function getAddition() : RecipeIngredient{
return $this->addition;
}

public function getTemplate() : RecipeIngredient{
return $this->template;
}

public function constructOutput(Item $input, Item $addition, Item $template) : ?Item{
if(!$input instanceof Armor){
return null;
}
if(($material = ArmorTrimMaterial::fromItem($addition)) === null || ($pattern = ArmorTrimPattern::fromItem($template)) === null){
return null;
}
return $input->setTrim(new ArmorTrim($material, $pattern));
HimmelKreis4865 marked this conversation as resolved.
Show resolved Hide resolved
}
}
83 changes: 83 additions & 0 deletions src/inventory/transaction/SmithingTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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\item\Armor;
use pocketmine\item\Item;
use pocketmine\item\ItemTypeIds;
use pocketmine\item\TieredTool;
use pocketmine\Server;
use function count;

class SmithingTransaction extends InventoryTransaction{

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

/** @var Item[] $inputs */
$inputs = [];
/** @var Item[] $outputs */
$outputs = [];
$this->matchItems($outputs, $inputs);

if(($inputCount = count($inputs)) !== 3){
throw new TransactionValidationException("Expected 3 input items, got $inputCount");
}
if(($outputCount = count($outputs)) !== 1){
throw new TransactionValidationException("Expected 1 output item, but received $outputCount");
}

$input = $addition = $template = null;
foreach($inputs as $item){
switch(true){
HimmelKreis4865 marked this conversation as resolved.
Show resolved Hide resolved
case $item instanceof Armor || $item instanceof TieredTool:
$input = $item;
break;
case $item->getTypeId() >= ItemTypeIds::NETHERITE_UPGRADE_SMITHING_TEMPLATE && $item->getTypeId() <= ItemTypeIds::SPIRE_ARMOR_TRIM_SMITHING_TEMPLATE:
HimmelKreis4865 marked this conversation as resolved.
Show resolved Hide resolved
$template = $item;
break;
default:
$addition = $item;
break;
}
}

if($input === null || $addition === null || $template === null){
throw new TransactionValidationException("The given inputs are no valid smithing ingredients");
HimmelKreis4865 marked this conversation as resolved.
Show resolved Hide resolved
}

$craftingManager = Server::getInstance()->getCraftingManager();
$recipe = $craftingManager->matchSmithingRecipe($input, $addition, $template);

if(($output = $recipe?->constructOutput($input, $addition, $template)) === null){
HimmelKreis4865 marked this conversation as resolved.
Show resolved Hide resolved
throw new TransactionValidationException("Could find a matching output item for the given inputs");
}

if(!$output->equalsExact($outputs[0])){
throw new TransactionValidationException("Invalid output item");
}
}
}
Loading