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 27 commits into
base: minor-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ae13dc9
add basic smithing table requirements
HimmelKreis4865 Dec 17, 2023
16de186
Merge branch 'pmmp:stable' into smithing-table
HimmelKreis4865 Dec 17, 2023
fc5e5eb
fixed remaining problems
HimmelKreis4865 Dec 18, 2023
c3ea0ba
phpstan stop crying
HimmelKreis4865 Dec 18, 2023
2a56e41
why are headers missing?
HimmelKreis4865 Dec 18, 2023
4da34c3
updated version with all fixes except for save ids - they must be fixed
HimmelKreis4865 Jan 21, 2024
6dce15d
oops (save ids still not working)
HimmelKreis4865 Jan 21, 2024
b82de65
oops
HimmelKreis4865 Jan 22, 2024
93db5ef
fixes all big remaining issues
HimmelKreis4865 Mar 22, 2024
1ad80e9
Merge remote-tracking branch 'origin/minor-next' into working-smithing
HimmelKreis4865 Mar 22, 2024
5360b77
cs style fixes
HimmelKreis4865 Mar 22, 2024
7433655
resolved some more problems
HimmelKreis4865 Mar 23, 2024
d987a65
made materials & patterns customizable
HimmelKreis4865 Mar 24, 2024
d818686
Merge branch 'minor-next' of https://github.com/pmmp/PocketMine-MP in…
ipad54 Nov 30, 2024
849f9b1
Code overhaul
ipad54 Nov 30, 2024
348a211
Update src/inventory/transaction/SmithingTransaction.php
ipad54 Nov 30, 2024
2294a6f
CS
ipad54 Nov 30, 2024
e19d0d7
Make trim constants private
ipad54 Nov 30, 2024
671a578
Merge branch 'smithing-table' of https://github.com/HimmelKreis4865/P…
ipad54 Nov 30, 2024
30a4edb
Don't use match
ipad54 Dec 1, 2024
d88960e
Use readonly where possible
ipad54 Dec 1, 2024
f479d66
Fix CS
ipad54 Dec 1, 2024
1deacda
Revert "Don't use match"
ipad54 Dec 1, 2024
b8da323
Fix tests
ipad54 Dec 1, 2024
c41cf15
Changes for review
ipad54 Dec 1, 2024
780b373
Declare all offset constants together
ipad54 Dec 1, 2024
a68323a
Merge branch 'minor-next' of https://github.com/pmmp/PocketMine-MP in…
ipad54 Dec 1, 2024
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
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);
}
ipad54 marked this conversation as resolved.
Show resolved Hide resolved

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 */
ipad54 marked this conversation as resolved.
Show resolved Hide resolved
$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");
ipad54 marked this conversation as resolved.
Show resolved Hide resolved
}

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