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 2 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
12 changes: 3 additions & 9 deletions src/crafting/SmithingTransformRecipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

namespace pocketmine\crafting;

use pocketmine\item\Armor;
use pocketmine\item\Item;
use pocketmine\item\TieredTool;

class SmithingTransformRecipe implements SmithingRecipe{

Expand Down Expand Up @@ -59,15 +57,11 @@ public function getResult() : Item{
* @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){
$input = $item;
if($this->input->accepts($item)){
return $this->getResult()->setNamedTag($item->getNamedTag());
}
}
if($input === null){
return null;
}
return $this->getResult()->setNamedTag($input->getNamedTag());
return null;
}
}
36 changes: 18 additions & 18 deletions src/crafting/SmithingTrimRecipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@

namespace pocketmine\crafting;

use pocketmine\data\bedrock\ArmorTrimMaterialTypeIdMap;
use pocketmine\data\bedrock\ArmorTrimPatternTypeIdMap;
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){
}
private RecipeIngredient $input,
private RecipeIngredient $addition,
private RecipeIngredient $template
){}

public function getInput() : RecipeIngredient{
return $this->input;
Expand All @@ -55,23 +56,22 @@ public function getTemplate() : RecipeIngredient{
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){
if($this->input->accepts($item) && $item instanceof Armor){
$input = $item;
}elseif(ArmorTrimRegistry::getInstance()->getPatternFromItem($item) !== null){
$template = $item;
}else{
}elseif($this->addition->accepts($item)){
$addition = $item;
}elseif($this->template->accepts($item)){
$template = $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;
if($input !== null && $addition !== null && $template !== null){
$material = ArmorTrimMaterialTypeIdMap::getInstance()->fromItem($addition);
$pattern = ArmorTrimPatternTypeIdMap::getInstance()->fromItem($template);
if($material !== null && $pattern !== null){
return (clone $input)->setTrim(new ArmorTrim($material, $pattern));
}
}
return $input->setTrim(new ArmorTrim($material, $pattern));

return null;
}
}
101 changes: 101 additions & 0 deletions src/data/bedrock/ArmorTrimMaterialTypeIdMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?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\data\bedrock;

use pocketmine\data\bedrock\ArmorTrimMaterialTypeIds as Ids;
use pocketmine\item\ArmorTrimMaterial;
use pocketmine\item\Item;
use pocketmine\item\VanillaArmorTrimMaterials as Materials;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\SingletonTrait;
use function array_key_exists;
use function array_values;
use function spl_object_id;

final class ArmorTrimMaterialTypeIdMap{
use SingletonTrait;

/**
* @var ArmorTrimMaterial[]
* @phpstan-var array<string, ArmorTrimMaterial>
*/
private array $idToMaterial = [];
/**
* @var ArmorTrimMaterial[]
* @phpstan-var array<int, ArmorTrimMaterial>
*/
private array $itemToMaterial = [];
/**
* @var string[]
* @phpstan-var array<int, string>
*/
private array $materialToId = [];

public function __construct(){
foreach(Materials::getAll() as $case){
$this->register(match($case) {
Materials::AMETHYST() => Ids::AMETHYST,
Materials::COPPER() => Ids::COPPER,
Materials::DIAMOND() => Ids::DIAMOND,
Materials::EMERALD() => Ids::EMERALD,
Materials::GOLD() => Ids::GOLD,
Materials::IRON() => Ids::IRON,
Materials::LAPIS() => Ids::LAPIS,
Materials::NETHERITE() => Ids::NETHERITE,
Materials::QUARTZ() => Ids::QUARTZ,
Materials::REDSTONE() => Ids::REDSTONE,
default => throw new AssumptionFailedError("Unhandled armor trim material type")
}, $case);
}
}

public function register(string $stringId, ArmorTrimMaterial $material) : void{
$this->idToMaterial[$stringId] = $material;
$this->itemToMaterial[$material->getItem()->getStateId()] = $material;
$this->materialToId[spl_object_id($material)] = $stringId;
}

public function fromId(string $id) : ?ArmorTrimMaterial{
return $this->idToMaterial[$id] ?? null;
}

public function fromItem(Item $item) : ?ArmorTrimMaterial{
return $this->itemToMaterial[$item->getStateId()] ?? null;
}

public function toId(ArmorTrimMaterial $material) : string{
$k = spl_object_id($material);
ipad54 marked this conversation as resolved.
Show resolved Hide resolved
if(!array_key_exists($k, $this->materialToId)){
throw new \InvalidArgumentException("Missing mapping for armor trim material with item" . $material->getItem()->getName() . " and color " . $material->getColor());
}
return $this->materialToId[$k];
}

/**
* @return ArmorTrimMaterial[]
*/
public function getAllMaterials() : array{
return array_values($this->idToMaterial);
}
}
37 changes: 37 additions & 0 deletions src/data/bedrock/ArmorTrimMaterialTypeIds.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\data\bedrock;

final class ArmorTrimMaterialTypeIds{
public const AMETHYST = "amethyst";
public const COPPER = "copper";
public const DIAMOND = "diamond";
public const EMERALD = "emerald";
public const GOLD = "gold";
public const IRON = "iron";
public const LAPIS = "lapis";
public const NETHERITE = "netherite";
public const QUARTZ = "quartz";
public const REDSTONE = "redstone";
}
107 changes: 107 additions & 0 deletions src/data/bedrock/ArmorTrimPatternTypeIdMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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\data\bedrock;

use pocketmine\data\bedrock\ArmorTrimPatternTypeIds as Ids;
use pocketmine\item\ArmorTrimPattern;
use pocketmine\item\Item;
use pocketmine\item\VanillaArmorTrimPatterns as Patterns;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\SingletonTrait;
use function array_key_exists;
use function array_values;
use function spl_object_id;

final class ArmorTrimPatternTypeIdMap{
use SingletonTrait;

/**
* @var ArmorTrimPattern[]
* @phpstan-var array<string, ArmorTrimPattern>
*/
private array $idToPattern = [];
/**
* @var ArmorTrimPattern[]
* @phpstan-var array<int, ArmorTrimPattern>
*/
private array $itemToPattern = [];
/**
* @var string[]
* @phpstan-var array<int, string>
*/
private array $patternToId = [];

public function __construct(){
foreach(Patterns::getAll() as $case){
$this->register(match($case){
Patterns::COAST() => Ids::COAST,
ipad54 marked this conversation as resolved.
Show resolved Hide resolved
Patterns::DUNE() => Ids::DUNE,
Patterns::EYE() => Ids::EYE,
Patterns::HOST() => Ids::HOST,
Patterns::RAISER() => Ids::RAISER,
Patterns::RIB() => Ids::RIB,
Patterns::SENTRY() => Ids::SENTRY,
Patterns::SHAPER() => Ids::SHAPER,
Patterns::SILENCE() => Ids::SILENCE,
Patterns::SNOUT() => Ids::SNOUT,
Patterns::SPIRE() => Ids::SPIRE,
Patterns::TIDE() => Ids::TIDE,
Patterns::VEX() => Ids::VEX,
Patterns::WARD() => Ids::WARD,
Patterns::WAYFINDER() => Ids::WAYFINDER,
Patterns::WILD() => Ids::WILD,
default => throw new AssumptionFailedError("Unhandled armor trim pattern type")
}, $case);
}
}

public function register(string $stringId, ArmorTrimPattern $pattern) : void{
$this->idToPattern[$stringId] = $pattern;
$this->itemToPattern[$pattern->getItem()->getStateId()] = $pattern;
$this->patternToId[spl_object_id($pattern)] = $stringId;
}

public function fromId(string $id) : ?ArmorTrimPattern{
return $this->idToPattern[$id] ?? null;
}

public function fromItem(Item $item) : ?ArmorTrimPattern{
return $this->itemToPattern[$item->getStateId()] ?? null;
}

public function toId(ArmorTrimPattern $pattern) : string{
$k = spl_object_id($pattern);
ipad54 marked this conversation as resolved.
Show resolved Hide resolved
if(!array_key_exists($k, $this->patternToId)){
throw new \InvalidArgumentException("Missing mapping for armor trim pattern with item" . $pattern->getItem()->getName());
}
return $this->patternToId[$k];
}

/**
* @return ArmorTrimPattern[]
*/
public function getAllPatterns() : array{
return array_values($this->idToPattern);
}
}
43 changes: 43 additions & 0 deletions src/data/bedrock/ArmorTrimPatternTypeIds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\data\bedrock;

final class ArmorTrimPatternTypeIds{
public const COAST = "coast";
public const DUNE = "dune";
public const EYE = "eye";
public const HOST = "host";
public const RAISER = "raiser";
public const RIB = "rib";
public const SENTRY = "sentry";
public const SHAPER = "shaper";
public const SILENCE = "silence";
public const SNOUT = "snout";
public const SPIRE = "spire";
public const TIDE = "tide";
public const VEX = "vex";
public const WARD = "ward";
public const WAYFINDER = "wayfinder";
public const WILD = "wild";
}
2 changes: 1 addition & 1 deletion src/inventory/transaction/SmithingTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SmithingTransaction extends InventoryTransaction{

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