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

Implement unregister functions in CraftingManager #5809

Open
wants to merge 5 commits into
base: minor-next
Choose a base branch
from

Conversation

ShockedPlot7560
Copy link
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
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
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
Member Author

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

@dktapps
Copy link
Member

dktapps commented Jun 12, 2023

no, I think the PR code is probably okay.

@ShockedPlot7560
Copy link
Member Author

Fixed #5286

Joshy3282 added a commit to Joshy3282/PocketMine-MP that referenced this pull request Oct 2, 2024
$edited = true;
}

if($edited){
Copy link
Member

Choose a reason for hiding this comment

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

$changed would be more consistent with code convention

$edited = true;
}

if($edited){
Copy link
Member

Choose a reason for hiding this comment

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

same again here

@dktapps
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: API Related to the plugin API Status: Waiting on Author 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