Skip to content

Implement unregister functions in CraftingManager#5809

Open
ShockedPlot7560 wants to merge 12 commits intopmmp:major-nextfrom
ShockedPlot7560:feat/craft-unregister
Open

Implement unregister functions in CraftingManager#5809
ShockedPlot7560 wants to merge 12 commits intopmmp:major-nextfrom
ShockedPlot7560:feat/craft-unregister

Conversation

@ShockedPlot7560
Copy link
Copy Markdown
Member

Introduction

Actually, if we want to delete or replace a recipe, we have to go through Reflection.
This PR provides a means of deleting a recipe to make it disappear, or replacing it by register it immediately afterwards.

There's no need to provide any functions other than this one, as the rest does the job:

  • Find a recipe from its output
  • Add a recipe
  • Delete a recipe (allowing replacement by combining the two)

Relevant issues

Changes

API changes

Add the following functions to the CraftingManager class:

  • unregisterShapedRecipe
  • unregisterShapelessRecipe
  • unregisterPotionTypeRecipe
  • unregisterPotionContainerChangeRecipe

Add the following function to the FurnaceRecipeManager class:

  • unregister

Backwards compatibility

I didn't want to do it directly, to get an opinion, but the observers should be renamed. It now only listens to the registration but also to the deletion.

Follow-up

  • Renaming observers?

Tests

Will remove all craft for Crafting table

$craftingManager = $this->getServer()->getCraftingManager();

foreach($craftingManager->matchRecipeByOutputs([
	VanillaBlocks::CRAFTING_TABLE()->asItem()
]) as $recipe){
	if($recipe instanceof ShapedRecipe){
		$craftingManager->unregisterShapedRecipe($recipe);
	}elseif($recipe instanceof ShapelessRecipe){
		$craftingManager->unregisterShapelessRecipe($recipe);
	}
}

image

@IvanCraft623 IvanCraft623 added Category: API Related to the plugin API Type: Enhancement Contributes features or other improvements to PocketMine-MP labels Jun 7, 2023
Copy link
Copy Markdown
Member

@dktapps dktapps left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks OK, although I haven't tested this myself.

@dktapps
Copy link
Copy Markdown
Member

dktapps commented Jun 11, 2023

Things to watch out for:

  • This code permits recipes to be unregistered at runtime. This might be fine, but we need to make sure that looking up a recipe that's been unregistered doesn't cause anything to crash (e.g. ItemStackRequest).
  • Recipe net ID generation in CraftingDataCache must continue to use the existing keys. The existing keys of the index must not be changed by any recipe being unregistered.

@ShockedPlot7560
Copy link
Copy Markdown
Member Author

Does this PR require any further development action or is it just testing?

@dktapps
Copy link
Copy Markdown
Member

dktapps commented Jun 12, 2023

no, I think the PR code is probably okay.

@ShockedPlot7560
Copy link
Copy Markdown
Member Author

Fixed #5286

@dktapps
Copy link
Copy Markdown
Member

dktapps commented Nov 23, 2024

The code in the PR looks functional.

The thing I'm dubious about is that this code expects plugin devs will have to get the exact instance of the recipe they want to unregister, in order to unregister it. I'm not sure this is the best way to go about it.

May well result in some painful headaches if someone does something like this:

$craftingManager->unregisterShapedRecipe(new ShapedRecipe(
    [
        "WWW",
        "W W",
        "WWW",
    ],
    [
        "W" => new ExactRecipeIngredient(VanillaBlocks::OAK_PLANKS()->asItem()),
    ],
    [VanillaBlocks::CHEST()->asItem()]
));

This code looks fine, but it would not produce the intended result.

Basically, I think we need a better way to compare recipes before implementing this.

@dktapps dktapps requested a review from a team as a code owner November 23, 2024 21:10
@ShockedPlot7560 ShockedPlot7560 added the Status: Blocked Depends on other changes which are yet to be completed label Mar 22, 2025
@ShockedPlot7560
Copy link
Copy Markdown
Member Author

For me, this is blocked until we find a way to compare recipes (probably in an other Pr, this is why I labelled this one as blocked).

pmmp-admin-bot[bot]
pmmp-admin-bot bot previously approved these changes Jul 30, 2025
@ShockedPlot7560
Copy link
Copy Markdown
Member Author

@dktapps I've implemented a new way to compare ShapelessRecipes (for the moment) to make the unregister function accurate. Do we follow the same for others recipes or another system need to be found.

@ShockedPlot7560 ShockedPlot7560 removed the Status: Blocked Depends on other changes which are yet to be completed label Jul 30, 2025
@dktapps
Copy link
Copy Markdown
Member

dktapps commented Jul 30, 2025

@dktapps I've implemented a new way to compare ShapelessRecipes (for the moment) to make the unregister function accurate. Do we follow the same for others recipes or another system need to be found.

Not sure. I'll have to dig into the shapeless logic a bit further, but I'm hoping there's a way to simplify this.

I really don't like using __toString() for the RecipeIngredient comparisons though...

@ShockedPlot7560
Copy link
Copy Markdown
Member Author

For RecipeIngredient, this is the only solution i've found to limit BC breaking. Probably, it's not possible and we need to enforce a new method into this interface to force developpers to provide a way of comparison.

@dktapps
Copy link
Copy Markdown
Member

dktapps commented Jul 31, 2025

I don't think it's reliable for custom recipe types. Best we push this back until PM6 if that's going to be a problem.

@ShockedPlot7560
Copy link
Copy Markdown
Member Author

Sadly it will be the solution, I don't see a better way to do it without breaking BC.

@dktapps dktapps changed the base branch from minor-next to major-next July 31, 2025 11:21
@dktapps dktapps dismissed pmmp-admin-bot[bot]’s stale review July 31, 2025 11:21

The base branch was changed.

@dktapps dktapps added the BC break Breaks API compatibility label Jul 31, 2025
@ShockedPlot7560
Copy link
Copy Markdown
Member Author

ShockedPlot7560 commented Oct 17, 2025

We could probably say that in pm6, we are introducing a new method in the ingredient interface to allow two recipes to be compared with each other? This remove the use of __toString

pmmp-admin-bot[bot]
pmmp-admin-bot bot previously approved these changes Oct 17, 2025
pmmp-admin-bot[bot]
pmmp-admin-bot bot previously approved these changes Oct 17, 2025
pmmp-admin-bot[bot]
pmmp-admin-bot bot previously approved these changes Oct 17, 2025
@pmmp pmmp deleted a comment from dktapps Oct 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BC break Breaks API compatibility Category: API Related to the plugin API Type: Enhancement Contributes features or other improvements to PocketMine-MP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants