From 61f2a35a61922a4cd4666a1094890db4f8e3bfa0 Mon Sep 17 00:00:00 2001 From: PepperBell <44146161+PepperCode1@users.noreply.github.com> Date: Wed, 28 Apr 2021 13:37:21 -0700 Subject: [PATCH] Add ISimpleReloadListener ISimpleReloadListener is a reload listener functional interface meant to be used when preparation is not needed. All current reload listeners have been moved to this interface, which allows them to be lambdas and not have to create a new Object for every reload. --- .../fluids/recipe/FluidTransferRecipes.java | 22 ++++--------- .../recipe/PotionMixingRecipeManager.java | 32 +++++++------------ .../foundation/ResourceReloadHandler.java | 11 ++----- .../utility/ISimpleReloadListener.java | 23 +++++++++++++ .../utility/recipe/RecipeFinder.java | 17 ++-------- 5 files changed, 46 insertions(+), 59 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/utility/ISimpleReloadListener.java diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/recipe/FluidTransferRecipes.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/recipe/FluidTransferRecipes.java index 9102302d9..66d157a88 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/recipe/FluidTransferRecipes.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/recipe/FluidTransferRecipes.java @@ -3,7 +3,8 @@ package com.simibubi.create.content.contraptions.fluids.recipe; import java.util.ArrayList; import java.util.List; -import net.minecraft.client.resources.ReloadListener; +import com.simibubi.create.foundation.utility.ISimpleReloadListener; + import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.profiler.IProfiler; @@ -14,20 +15,9 @@ public class FluidTransferRecipes { public static List POTION_ITEMS = new ArrayList<>(); public static List FILLED_BUCKETS = new ArrayList<>(); - - - public static final ReloadListener LISTENER = new ReloadListener() { - - @Override - protected Object prepare(IResourceManager p_212854_1_, IProfiler p_212854_2_) { - return new Object(); - } - - @Override - protected void apply(Object p_212853_1_, IResourceManager p_212853_2_, IProfiler p_212853_3_) { - POTION_ITEMS.clear(); - FILLED_BUCKETS.clear(); - } - + public static final ISimpleReloadListener LISTENER = (IResourceManager resourceManager, IProfiler profiler) -> { + POTION_ITEMS.clear(); + FILLED_BUCKETS.clear(); }; + } diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/recipe/PotionMixingRecipeManager.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/recipe/PotionMixingRecipeManager.java index 7639a06ed..1a424c82b 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/recipe/PotionMixingRecipeManager.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/recipe/PotionMixingRecipeManager.java @@ -15,8 +15,8 @@ import com.simibubi.create.content.contraptions.fluids.potion.PotionFluidHandler import com.simibubi.create.content.contraptions.processing.HeatCondition; import com.simibubi.create.content.contraptions.processing.ProcessingRecipeBuilder; import com.simibubi.create.foundation.fluid.FluidIngredient; +import com.simibubi.create.foundation.utility.ISimpleReloadListener; -import net.minecraft.client.resources.ReloadListener; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.Ingredient; @@ -133,27 +133,17 @@ public class PotionMixingRecipeManager { .collect(Collectors.toList()); } - public static final ReloadListener LISTENER = new ReloadListener() { - - @Override - protected Object prepare(IResourceManager p_212854_1_, IProfiler p_212854_2_) { - return new Object(); - } - - @Override - protected void apply(Object p_212853_1_, IResourceManager p_212853_2_, IProfiler p_212853_3_) { - ALL.clear(); - getAllBrewingRecipes().forEach(recipe -> { - for (Ingredient ingredient : recipe.getIngredients()) { - for (ItemStack itemStack : ingredient.getMatchingStacks()) { - ALL.computeIfAbsent(itemStack.getItem(), t -> new ArrayList<>()) - .add(recipe); - return; - } + public static final ISimpleReloadListener LISTENER = (IResourceManager resourceManager, IProfiler profiler) -> { + ALL.clear(); + getAllBrewingRecipes().forEach(recipe -> { + for (Ingredient ingredient : recipe.getIngredients()) { + for (ItemStack itemStack : ingredient.getMatchingStacks()) { + ALL.computeIfAbsent(itemStack.getItem(), t -> new ArrayList<>()) + .add(recipe); + return; } - }); - } - + } + }); }; } diff --git a/src/main/java/com/simibubi/create/foundation/ResourceReloadHandler.java b/src/main/java/com/simibubi/create/foundation/ResourceReloadHandler.java index 3203fa4d4..30cdb88eb 100644 --- a/src/main/java/com/simibubi/create/foundation/ResourceReloadHandler.java +++ b/src/main/java/com/simibubi/create/foundation/ResourceReloadHandler.java @@ -3,20 +3,15 @@ package com.simibubi.create.foundation; import com.simibubi.create.CreateClient; import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation; import com.simibubi.create.foundation.block.render.SpriteShifter; +import com.simibubi.create.foundation.utility.ISimpleReloadListener; -import net.minecraft.client.resources.ReloadListener; import net.minecraft.profiler.IProfiler; import net.minecraft.resources.IResourceManager; -public class ResourceReloadHandler extends ReloadListener { +public class ResourceReloadHandler implements ISimpleReloadListener { @Override - protected Object prepare(IResourceManager resourceManagerIn, IProfiler profilerIn) { - return new Object(); - } - - @Override - protected void apply(Object $, IResourceManager resourceManagerIn, IProfiler profilerIn) { + public void onReload(IResourceManager resourceManagerIn, IProfiler profilerIn) { SpriteShifter.reloadUVs(); CreateClient.invalidateRenderers(); IHaveGoggleInformation.numberFormat.update(); diff --git a/src/main/java/com/simibubi/create/foundation/utility/ISimpleReloadListener.java b/src/main/java/com/simibubi/create/foundation/utility/ISimpleReloadListener.java new file mode 100644 index 000000000..1b361b789 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/utility/ISimpleReloadListener.java @@ -0,0 +1,23 @@ +package com.simibubi.create.foundation.utility; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; + +import net.minecraft.profiler.IProfiler; +import net.minecraft.resources.IFutureReloadListener; +import net.minecraft.resources.IResourceManager; +import net.minecraft.util.Unit; + +@FunctionalInterface +public interface ISimpleReloadListener extends IFutureReloadListener { + + @Override + default CompletableFuture reload(IFutureReloadListener.IStage stage, IResourceManager resourceManager, IProfiler prepareProfiler, IProfiler applyProfiler, Executor prepareExecutor, Executor applyExecutor) { + return stage.markCompleteAwaitingOthers(Unit.INSTANCE).thenRunAsync(() -> { + onReload(resourceManager, applyProfiler); + }, applyExecutor); + } + + void onReload(IResourceManager resourceManager, IProfiler profiler); + +} diff --git a/src/main/java/com/simibubi/create/foundation/utility/recipe/RecipeFinder.java b/src/main/java/com/simibubi/create/foundation/utility/recipe/RecipeFinder.java index 96e1ba2b6..f36318ae2 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/recipe/RecipeFinder.java +++ b/src/main/java/com/simibubi/create/foundation/utility/recipe/RecipeFinder.java @@ -10,8 +10,8 @@ import javax.annotation.Nullable; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import com.simibubi.create.foundation.utility.ISimpleReloadListener; -import net.minecraft.client.resources.ReloadListener; import net.minecraft.item.crafting.IRecipe; import net.minecraft.profiler.IProfiler; import net.minecraft.resources.IResourceManager; @@ -58,19 +58,8 @@ public class RecipeFinder { return list; } - - public static final ReloadListener LISTENER = new ReloadListener() { - - @Override - protected Object prepare(IResourceManager p_212854_1_, IProfiler p_212854_2_) { - return new Object(); - } - - @Override - protected void apply(Object p_212853_1_, IResourceManager p_212853_2_, IProfiler p_212853_3_) { - cachedSearches.invalidateAll(); - } - + public static final ISimpleReloadListener LISTENER = (IResourceManager resourceManager, IProfiler profiler) -> { + cachedSearches.invalidateAll(); }; }