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 13 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
19 changes: 19 additions & 0 deletions src/block/inventory/SmithingTableInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,32 @@

use pocketmine\inventory\SimpleInventory;
use pocketmine\inventory\TemporaryInventory;
use pocketmine\item\Item;
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);
}

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
}
32 changes: 32 additions & 0 deletions src/crafting/CraftingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
use pocketmine\utils\BinaryStream;
use pocketmine\utils\DestructorCallbackTrait;
use pocketmine\utils\ObjectSet;
use function count;
use function spl_object_id;
use function usort;

class CraftingManager{
use DestructorCallbackTrait;

public const SMITHING_RECIPES_OFFSET = 200000;
ipad54 marked this conversation as resolved.
Show resolved Hide resolved

/**
* @var ShapedRecipe[][]
* @phpstan-var array<string, list<ShapedRecipe>>
Expand Down Expand Up @@ -64,6 +67,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 +206,21 @@ public function getPotionContainerChangeRecipes() : array{
return $this->potionContainerChangeRecipes;
}

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

public function getSmithingRecipeFromIndex(int $index) : ?SmithingRecipe{
if($index < self::SMITHING_RECIPES_OFFSET || $index > (self::SMITHING_RECIPES_OFFSET + count($this->smithingRecipes))){
return null;
}
return $this->smithingRecipes[$index - self::SMITHING_RECIPES_OFFSET];
}

public function registerShapedRecipe(ShapedRecipe $recipe) : void{
$this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
$this->craftingRecipeIndex[] = $recipe;
Expand Down Expand Up @@ -231,6 +255,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
22 changes: 21 additions & 1 deletion 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 @@ -329,9 +331,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;
}
}
41 changes: 41 additions & 0 deletions src/crafting/SmithingRecipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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;

interface SmithingRecipe{

public function getInput() : RecipeIngredient;

public function getAddition() : RecipeIngredient;

public function getTemplate() : RecipeIngredient;

/**
* @param Item[] $inputs
* @phpstan-param list<Item> $inputs
*/
public function getResultFor(array $inputs) : ?Item;
}
73 changes: 73 additions & 0 deletions src/crafting/SmithingTransformRecipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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\Item;
use pocketmine\item\TieredTool;

class SmithingTransformRecipe implements SmithingRecipe{

public function __construct(
private readonly RecipeIngredient $input,
private readonly RecipeIngredient $addition,
private readonly 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;
}

/**
* @param Item[] $inputs
* @phpstan-param list<Item> $inputs
*/
public function getResultFor(array $inputs) : ?Item{
dktapps marked this conversation as resolved.
Show resolved Hide resolved
$input = null;
foreach($inputs as $item){
if ($item instanceof Armor || $item instanceof TieredTool){
ipad54 marked this conversation as resolved.
Show resolved Hide resolved
$input = $item;
}
}
if($input === null){
return null;
}
return $this->getResult()->setNamedTag($input->getNamedTag());
ShockedPlot7560 marked this conversation as resolved.
Show resolved Hide resolved
}
}
77 changes: 77 additions & 0 deletions src/crafting/SmithingTrimRecipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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\ArmorTrimRegistry;
use pocketmine\item\Item;

class SmithingTrimRecipe implements SmithingRecipe{

public function __construct(
private readonly RecipeIngredient $input,
private readonly RecipeIngredient $addition,
private readonly RecipeIngredient $template){
ipad54 marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

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

/**
* @param Item[] $inputs
* @phpstan-param list<Item> $inputs
*/
public function getResultFor(array $inputs) : ?Item{
dktapps marked this conversation as resolved.
Show resolved Hide resolved
$input = $template = $addition = null;
foreach($inputs as $item){
if($item instanceof Armor){
ipad54 marked this conversation as resolved.
Show resolved Hide resolved
$input = $item;
}elseif(ArmorTrimRegistry::getInstance()->getPatternFromItem($item) !== null){
ipad54 marked this conversation as resolved.
Show resolved Hide resolved
$template = $item;
}else{
$addition = $item;
}
}

if($input === null || $addition === null || $template === null){
return null;
}
$material = ArmorTrimRegistry::getInstance()->getMaterialFromItem($addition);
$pattern = ArmorTrimRegistry::getInstance()->getPatternFromItem($template);
if($material === null || $pattern === null){
return null;
}
return $input->setTrim(new ArmorTrim($material, $pattern));
HimmelKreis4865 marked this conversation as resolved.
Show resolved Hide resolved
}
}
65 changes: 65 additions & 0 deletions src/inventory/transaction/SmithingTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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\crafting\SmithingRecipe;
use pocketmine\item\Item;
use pocketmine\player\Player;
use function count;

class SmithingTransaction extends InventoryTransaction{

public function __construct(
Player $source,
private readonly SmithingRecipe $recipe,
array $actions = []
){
parent::__construct($source, $actions);
}

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");
}
if(($output = $this->recipe->getResultFor($inputs)) === null){
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