-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Caching Crafting Recipe Outputs
- Loading branch information
1 parent
ffabbdd
commit f50641a
Showing
7 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/com/nomiceu/nomilabs/groovy/mixinhelper/CraftingOutputCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.nomiceu.nomilabs.groovy.mixinhelper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.item.crafting.IRecipe; | ||
import net.minecraft.util.ResourceLocation; | ||
import net.minecraftforge.fml.common.registry.ForgeRegistries; | ||
|
||
import com.nomiceu.nomilabs.NomiLabs; | ||
import com.nomiceu.nomilabs.util.ItemMeta; | ||
|
||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; | ||
|
||
public class CraftingOutputCache { | ||
|
||
public static Map<ItemMeta, List<ResourceLocation>> cache = null; | ||
|
||
public static void buildCache() { | ||
if (cache != null) return; | ||
|
||
cache = new Object2ObjectOpenHashMap<>(); | ||
|
||
long time = System.currentTimeMillis(); | ||
for (IRecipe recipe : ForgeRegistries.RECIPES) { | ||
ItemStack output = recipe.getRecipeOutput(); | ||
if (output.isEmpty()) continue; | ||
|
||
cache.computeIfAbsent(new ItemMeta(output), k -> new ArrayList<>()) | ||
.add(recipe.getRegistryName()); | ||
} | ||
time = System.currentTimeMillis() - time; | ||
NomiLabs.LOGGER.info("[GrS] Building Crafting Output Cache took {}ms.", time); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/nomiceu/nomilabs/mixin/earlygroovy/CraftingMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.nomiceu.nomilabs.mixin.earlygroovy; | ||
|
||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.util.ResourceLocation; | ||
import net.minecraftforge.fml.common.registry.ForgeRegistries; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import com.cleanroommc.groovyscript.api.GroovyLog; | ||
import com.cleanroommc.groovyscript.api.IIngredient; | ||
import com.cleanroommc.groovyscript.compat.vanilla.Crafting; | ||
import com.cleanroommc.groovyscript.helper.ingredient.IngredientHelper; | ||
import com.cleanroommc.groovyscript.registry.ReloadableRegistryManager; | ||
import com.nomiceu.nomilabs.config.LabsConfig; | ||
import com.nomiceu.nomilabs.groovy.mixinhelper.CraftingOutputCache; | ||
import com.nomiceu.nomilabs.util.ItemMeta; | ||
|
||
@Mixin(value = Crafting.class, remap = false) | ||
public class CraftingMixin { | ||
|
||
@Inject(method = "removeByOutput(Lcom/cleanroommc/groovyscript/api/IIngredient;Z)V", | ||
at = @At(value = "HEAD"), | ||
cancellable = true) | ||
private void useOutputCache(IIngredient output, boolean log, CallbackInfo ci) { | ||
if (LabsConfig.groovyScriptSettings.craftingOutputCacheMode == | ||
LabsConfig.GroovyScriptSettings.CraftingOutputCacheMode.DISABLED) | ||
return; | ||
|
||
// noinspection ConstantValue | ||
if (IngredientHelper.isEmpty(output) || !((Object) output instanceof ItemStack stack)) return; | ||
|
||
ci.cancel(); | ||
CraftingOutputCache.buildCache(); | ||
var itemMeta = new ItemMeta(stack); | ||
|
||
if (!CraftingOutputCache.cache.containsKey(itemMeta)) { | ||
if (log) { | ||
GroovyLog.msg("Error removing Minecraft Crafting recipe") | ||
.add("No recipes found for {}", output) | ||
.error() | ||
.post(); | ||
} | ||
return; | ||
} | ||
|
||
for (ResourceLocation rl : CraftingOutputCache.cache.get(itemMeta)) { | ||
ReloadableRegistryManager.removeRegistryEntry(ForgeRegistries.RECIPES, rl); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"package": "com.nomiceu.nomilabs.mixin.earlygroovy", | ||
"refmap": "mixins.nomilabs.refmap.json", | ||
"target": "@env(DEFAULT)", | ||
"minVersion": "0.8", | ||
"compatibilityLevel": "JAVA_8", | ||
"mixins": [ | ||
"CraftingMixin" | ||
], | ||
"client": [], | ||
"server": [] | ||
} |