Use fabric resource conditions api. Remove reborncore's.

This commit is contained in:
modmuss50 2022-01-17 22:34:47 +00:00
parent 6b01bf64e2
commit b59c535c3d
21 changed files with 116 additions and 254 deletions

View file

@ -1,140 +0,0 @@
/*
* This file is part of RebornCore, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 TeamReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package reborncore.common.crafting;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.tag.ItemTags;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.SimpleRegistry;
import org.apache.commons.lang3.Validate;
import java.util.HashMap;
import java.util.function.Function;
public final class ConditionManager {
private static final HashMap<Identifier, RecipeCondition<?>> RECIPE_CONDITIONS = new HashMap<>();
private static final HashMap<Identifier, Class<?>> RECIPE_CONDITION_TYPES = new HashMap<>();
static {
//Only loads the recipe in a development env
register("development", Boolean.class, (bool) -> bool == FabricLoader.getInstance().isDevelopmentEnvironment());
//Only loads the recipe when the item is registered
register("item", Identifier.class, (id) -> registryContains(Registry.ITEM, id));
//Only loads the recipe when the fluid is registered
register("fluid", Identifier.class, (id) -> registryContains(Registry.FLUID, id));
//Only loads the recipe when the tag is loaded
register("tag", Identifier.class, s -> ItemTags.getTagGroup().getTags().containsKey(s));
//Only load the recipe if the provided mod is loaded
register("mod", String.class, s -> FabricLoader.getInstance().isModLoaded(s));
//Never load, just pass whatever in as the string
register("never", String.class, s -> false);
}
private static boolean registryContains(SimpleRegistry<?> registry, Identifier ident) {
return registry.containsId(ident);
}
public static <T> void register(String name, Class<T> type, RecipeCondition<T> recipeCondition){
register(new Identifier("reborncore", name), type, recipeCondition);
}
public static <T> void register(Identifier identifier, Class<T> type, RecipeCondition<T> recipeCondition){
Validate.isTrue(!RECIPE_CONDITIONS.containsKey(identifier), "Recipe condition already registered");
RECIPE_CONDITIONS.put(identifier, recipeCondition);
RECIPE_CONDITION_TYPES.put(identifier, type);
}
public static RecipeCondition<?> getRecipeCondition(Identifier identifier){
RecipeCondition<?> condition = RECIPE_CONDITIONS.get(identifier);
if(condition == null){
throw new UnsupportedOperationException("Could not find recipe condition for " + identifier.toString());
}
return condition;
}
public static boolean shouldLoadRecipe(JsonObject jsonObject){
if(!jsonObject.has("conditions")) return true;
return jsonObject.get("conditions").getAsJsonObject().entrySet().stream()
.allMatch(entry -> shouldLoad(entry.getKey(), entry.getValue()));
}
public static boolean shouldLoad(String ident, JsonElement jsonElement){
Identifier identifier = parseIdent(ident);
RecipeCondition<?> recipeCondition = getRecipeCondition(identifier);
Class<?> type = RECIPE_CONDITION_TYPES.get(identifier);
return shouldLoad(type, jsonElement, recipeCondition);
}
@SuppressWarnings("unchecked")
private static boolean shouldLoad(Class type, JsonElement jsonElement, RecipeCondition recipeCondition){
Object val = TypeHelper.getValue(type, jsonElement);
return recipeCondition.shouldLoad(val);
}
private static Identifier parseIdent(String string) {
if(string.contains(":")){
return new Identifier(string);
}
return new Identifier("reborncore", string);
}
@FunctionalInterface
public interface RecipeCondition<T> {
boolean shouldLoad(T t);
}
private static class TypeHelper {
private static final HashMap<Class<?>, Function<JsonElement, ?>> FUNCTIONS = new HashMap<>();
static {
register(String.class, JsonElement::getAsString);
register(Boolean.class, JsonElement::getAsBoolean);
register(Identifier.class, element -> new Identifier(element.getAsString()));
}
private static <T> void register(Class<T> type, Function<JsonElement, T> function){
Validate.isTrue(!FUNCTIONS.containsKey(type), "Function for this class is already registered");
FUNCTIONS.put(type, function);
}
public static <T> T getValue(Class<T> type, JsonElement jsonElement){
Validate.isTrue(FUNCTIONS.containsKey(type), "Function for this class could not be found");
//noinspection unchecked
return (T) FUNCTIONS.get(type).apply(jsonElement);
}
}
}

View file

@ -52,11 +52,6 @@ public record RebornRecipeType<R extends RebornRecipe>(
try { try {
R recipe = newRecipe(recipeId); R recipe = newRecipe(recipeId);
if (!ConditionManager.shouldLoadRecipe(json)) {
recipe.makeDummy();
return recipe;
}
recipe.deserialize(json); recipe.deserialize(json);
return recipe; return recipe;
} catch (Throwable t) { } catch (Throwable t) {

View file

@ -1,60 +0,0 @@
/*
* This file is part of RebornCore, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 TeamReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package reborncore.mixin.common;
import com.google.gson.JsonObject;
import net.minecraft.recipe.RecipeManager;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.minecraft.util.profiler.Profiler;
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 reborncore.common.crafting.ConditionManager;
import java.util.Iterator;
import java.util.Map;
@Mixin(RecipeManager.class)
public class MixinRecipeManager {
@Inject(method = "apply", at = @At("HEAD"))
private void deserialize(Map<Identifier, JsonObject> map, ResourceManager resourceManager, Profiler profiler, CallbackInfo info) {
Iterator<Map.Entry<Identifier, JsonObject>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Identifier, JsonObject> entry = iterator.next();
Identifier id = entry.getKey();
JsonObject json = entry.getValue();
// TODO dont hard code this as its awful
if (id.getNamespace().equals("reborncore") || id.getNamespace().equals("techreborn")) {
if (!ConditionManager.shouldLoadRecipe(json)) {
iterator.remove();
}
}
}
}
}

View file

@ -14,7 +14,6 @@
"MixinItemStack", "MixinItemStack",
"MixinLivingEntity", "MixinLivingEntity",
"MixinPlayerEntity", "MixinPlayerEntity",
"MixinRecipeManager",
"MixinServerPlayerEntity" "MixinServerPlayerEntity"
], ],
"injectors": { "injectors": {

View file

@ -12,7 +12,7 @@ plugins {
id 'eclipse' id 'eclipse'
id 'maven-publish' id 'maven-publish'
id "org.cadixdev.licenser" version "0.6.1" id "org.cadixdev.licenser" version "0.6.1"
id "fabric-loom" version "0.10-SNAPSHOT" id "fabric-loom" version "0.11-SNAPSHOT"
id "com.matthewprenger.cursegradle" version "1.4.0" id "com.matthewprenger.cursegradle" version "1.4.0"
id "de.undercouch.download" version "4.1.1" id "de.undercouch.download" version "4.1.1"
} }

View file

@ -9,10 +9,10 @@ org.gradle.jvmargs=-Xmx2G
minecraft_version=1.18.1 minecraft_version=1.18.1
yarn_version=1.18.1+build.18 yarn_version=1.18.1+build.18
loader_version=0.12.12 loader_version=0.12.12
fapi_version=0.46.0+1.18 fapi_version=0.46.1+1.18
# Dependencies # Dependencies
energy_version=2.0.0-beta1 energy_version=2.1.0
rei_version=7.1.361 rei_version=7.1.361
trinkets_version=3.0.2 trinkets_version=3.0.2
autoswitch_version=-SNAPSHOT autoswitch_version=-SNAPSHOT

View file

@ -12,7 +12,12 @@
"item": "byg:ametrine_gems" "item": "byg:ametrine_gems"
} }
], ],
"conditions": { "fabric:load_conditions": [
"reborncore:mod": "byg" {
} "condition": "fabric:all_mods_loaded",
"values": [
"byg"
]
}
]
} }

View file

@ -13,7 +13,12 @@
"count": 2 "count": 2
} }
], ],
"conditions": { "fabric:load_conditions": [
"reborncore:mod": "byg" {
} "condition": "fabric:all_mods_loaded",
"values": [
"byg"
]
}
]
} }

View file

@ -12,7 +12,12 @@
"item": "techreborn:coal_dust" "item": "techreborn:coal_dust"
} }
], ],
"conditions": { "fabric:load_conditions": [
"reborncore:mod": "byg" {
} "condition": "fabric:all_mods_loaded",
"values": [
"byg"
]
}
]
} }

View file

@ -12,7 +12,12 @@
"item": "techreborn:coal_dust" "item": "techreborn:coal_dust"
} }
], ],
"conditions": { "fabric:load_conditions": [
"reborncore:mod": "byg" {
} "condition": "fabric:all_mods_loaded",
"values": [
"byg"
]
}
]
} }

View file

@ -13,7 +13,12 @@
"count": 2 "count": 2
} }
], ],
"conditions": { "fabric:load_conditions": [
"reborncore:mod": "byg" {
} "condition": "fabric:all_mods_loaded",
"values": [
"byg"
]
}
]
} }

View file

@ -12,7 +12,12 @@
"item": "byg:pendorite_scraps" "item": "byg:pendorite_scraps"
} }
], ],
"conditions": { "fabric:load_conditions": [
"reborncore:mod": "byg" {
} "condition": "fabric:all_mods_loaded",
"values": [
"byg"
]
}
]
} }

View file

@ -13,7 +13,12 @@
"item": "ae2:certus_quartz_dust" "item": "ae2:certus_quartz_dust"
} }
], ],
"conditions": { "fabric:load_conditions": [
"reborncore:mod": "ae2" {
} "condition": "fabric:all_mods_loaded",
"values": [
"ae2"
]
}
]
} }

View file

@ -13,7 +13,12 @@
"count": 5 "count": 5
} }
], ],
"conditions": { "fabric:load_conditions": [
"reborncore:mod": "ae2" {
} "condition": "fabric:all_mods_loaded",
"values": [
"ae2"
]
}
]
} }

View file

@ -13,7 +13,12 @@
"item": "ae2:fluix_dust" "item": "ae2:fluix_dust"
} }
], ],
"conditions": { "fabric:load_conditions": [
"reborncore:mod": "ae2" {
} "condition": "fabric:all_mods_loaded",
"values": [
"ae2"
]
}
]
} }

View file

@ -14,8 +14,12 @@
"item": "techreborn:marble_dust" "item": "techreborn:marble_dust"
} }
], ],
"fabric:load_conditions": [
"conditions": { {
"reborncore:tag": "c:limestone" "condition": "fabric:item_tags_populated",
} "values": [
"c:limestone"
]
}
]
} }

View file

@ -14,8 +14,12 @@
"item": "techreborn:marble_dust" "item": "techreborn:marble_dust"
} }
], ],
"fabric:load_conditions": [
"conditions": { {
"reborncore:tag": "c:marble" "condition": "fabric:item_tags_populated",
} "values": [
"c:marble"
]
}
]
} }

View file

@ -12,7 +12,12 @@
"item": "techreborn:sulfur_dust" "item": "techreborn:sulfur_dust"
} }
], ],
"conditions": { "fabric:load_conditions": [
"reborncore:tag": "c:sulfurs" {
} "condition": "fabric:item_tags_populated",
"values": [
"c:sulfurs"
]
}
]
} }

View file

@ -13,7 +13,12 @@
"count": 2 "count": 2
} }
], ],
"conditions": { "fabric:load_conditions": [
"reborncore:tag": "c:sulfur_ores" {
} "condition": "fabric:item_tags_populated",
"values": [
"c:sulfur_ores"
]
}
]
} }

View file

@ -21,7 +21,12 @@
"count": 5 "count": 5
} }
], ],
"conditions": { "fabric:load_conditions": [
"reborncore:mod": "ae2" {
} "condition": "fabric:all_mods_loaded",
"values": [
"ae2"
]
}
]
} }

View file

@ -34,7 +34,7 @@
}, },
"depends": { "depends": {
"fabricloader": ">=0.12.0", "fabricloader": ">=0.12.0",
"fabric": ">=0.40.0", "fabric": ">=0.46.1",
"reborncore": "*", "reborncore": "*",
"team_reborn_energy": ">=2.0.0-beta1", "team_reborn_energy": ">=2.0.0-beta1",
"fabric-biome-api-v1": ">=3.0.0" "fabric-biome-api-v1": ">=3.0.0"