Improve performance of RecipeMap#removeRecipe

This commit is contained in:
Jake Potrebic
2024-10-31 20:36:41 -07:00
parent 7acf73ce66
commit 9467a08b36
3 changed files with 37 additions and 16 deletions

View File

@@ -92,7 +92,7 @@
+ // CraftBukkit start + // CraftBukkit start
+ public boolean removeRecipe(ResourceKey<Recipe<?>> mcKey) { + public boolean removeRecipe(ResourceKey<Recipe<?>> mcKey) {
+ boolean removed = this.recipes.removeRecipe(mcKey); + boolean removed = this.recipes.removeRecipe((ResourceKey<Recipe<RecipeInput>>) (ResourceKey) mcKey); // Paper - generic fix
+ if (removed) { + if (removed) {
+ this.finalizeRecipeLoading(); + this.finalizeRecipeLoading();
+ } + }

View File

@@ -11,7 +11,7 @@
public class RecipeMap { public class RecipeMap {
@@ -35,11 +39,39 @@ @@ -35,11 +39,56 @@
com_google_common_collect_immutablemap_builder.put(recipeholder.id(), recipeholder); com_google_common_collect_immutablemap_builder.put(recipeholder.id(), recipeholder);
} }
@@ -31,21 +31,38 @@
+ } + }
+ } + }
+ +
+ public boolean removeRecipe(ResourceKey<Recipe<?>> mcKey) { + // public boolean removeRecipe(ResourceKey<Recipe<?>> mcKey) {
+ boolean removed = false; + // boolean removed = false;
+ Iterator<RecipeHolder<?>> iter = this.byType.values().iterator(); + // Iterator<RecipeHolder<?>> iter = this.byType.values().iterator();
+ while (iter.hasNext()) { + // while (iter.hasNext()) {
+ RecipeHolder<?> recipe = iter.next(); + // RecipeHolder<?> recipe = iter.next();
+ if (recipe.id().equals(mcKey)) { + // if (recipe.id().equals(mcKey)) {
+ iter.remove(); + // iter.remove();
+ removed = true; + // removed = true;
+ } + // }
+ } + // }
+ removed |= this.byKey.remove(mcKey) != null; + // removed |= this.byKey.remove(mcKey) != null;
+ + //
+ return removed; + // return removed;
+ } + // }
+ // CraftBukkit end + // CraftBukkit end
+
+
+ // Paper start - replace removeRecipe implementation
+ public <T extends RecipeInput> boolean removeRecipe(ResourceKey<Recipe<T>> mcKey) {
+ //noinspection unchecked
+ final RecipeHolder<Recipe<T>> remove = (RecipeHolder<Recipe<T>>) this.byKey.remove(mcKey);
+ if (remove == null) {
+ return false;
+ }
+ final Collection<? extends RecipeHolder<? extends Recipe<T>>> recipes = this.byType(remove.value().getType());
+ if (recipes.remove(remove)) {
+ return true;
+ }
+ return false;
+ // Paper end - why are you using a loop???
+ }
+ // Paper end - replace removeRecipe implementation
+ +
public <I extends RecipeInput, T extends Recipe<I>> Collection<RecipeHolder<T>> byType(RecipeType<T> type) { public <I extends RecipeInput, T extends Recipe<I>> Collection<RecipeHolder<T>> byType(RecipeType<T> type) {
- return this.byType.get(type); - return this.byType.get(type);

View File

@@ -32,5 +32,9 @@ public class RecipeIterator implements Iterator<Recipe> {
public void remove() { public void remove() {
MinecraftServer.getServer().getRecipeManager().recipes.byKey.remove(this.currentRecipe.id()); // Paper - fix removing recipes from RecipeIterator MinecraftServer.getServer().getRecipeManager().recipes.byKey.remove(this.currentRecipe.id()); // Paper - fix removing recipes from RecipeIterator
this.recipes.remove(); this.recipes.remove();
// Paper start - correctly reload recipes
MinecraftServer.getServer().getRecipeManager().finalizeRecipeLoading();
MinecraftServer.getServer().getPlayerList().reloadRecipes();
// Paper end - correctly reload recipes
} }
} }