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

Fix Recipe Serialization #2348

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.minecraft.world.level.Level;

import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -121,7 +122,13 @@ public GTRecipe(GTRecipeType recipeType,
this.data = data;
this.duration = duration;
this.isFuel = isFuel;
this.recipeCategory = recipeCategory;
this.recipeCategory = (recipeCategory != GTRecipeCategory.EMPTY) ?
recipeCategory : GTRecipeCategory.of(recipeType);
if (id != null) {
this.recipeType.getCategoryMap()
.computeIfAbsent(this.recipeCategory, k -> new ObjectLinkedOpenHashSet<>())
.add(this);
}
}

public Map<RecipeCapability<?>, List<Content>> copyContents(Map<RecipeCapability<?>, List<Content>> contents,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public GTRecipe fromNetwork(@NotNull ResourceLocation id, @NotNull FriendlyByteB

GTRecipeType type = (GTRecipeType) BuiltInRegistries.RECIPE_TYPE.get(recipeType);
GTRecipeCategory category = GTRegistries.RECIPE_CATEGORIES.get(categoryLoc);
if (category == null || category == GTRecipeCategory.EMPTY) category = GTRecipeCategory.of(type);

GTRecipe recipe = new GTRecipe(type, id,
inputs, outputs, tickInputs, tickOutputs,
Expand Down Expand Up @@ -213,7 +214,7 @@ private static Codec<GTRecipe> makeCodec(boolean isKubeLoaded) {
CompoundTag.CODEC.optionalFieldOf("data", new CompoundTag()).forGetter(val -> val.data),
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("duration").forGetter(val -> val.duration),
Codec.BOOL.optionalFieldOf("isFuel", false).forGetter(val -> val.isFuel),
GTRegistries.RECIPE_CATEGORIES.codec().fieldOf("category").forGetter(val -> val.recipeCategory))
GTRegistries.RECIPE_CATEGORIES.codec().optionalFieldOf("category", GTRecipeCategory.EMPTY).forGetter(val -> val.recipeCategory))
.apply(instance, (type,
inputs, outputs, tickInputs, tickOutputs,
inputChanceLogics, outputChanceLogics, tickInputChanceLogics, tickOutputChanceLogics,
Expand Down Expand Up @@ -241,7 +242,7 @@ private static Codec<GTRecipe> makeCodec(boolean isKubeLoaded) {
CompoundTag.CODEC.optionalFieldOf("data", new CompoundTag()).forGetter(val -> val.data),
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("duration").forGetter(val -> val.duration),
Codec.BOOL.optionalFieldOf("isFuel", false).forGetter(val -> val.isFuel),
GTRegistries.RECIPE_CATEGORIES.codec().fieldOf("category").forGetter(val -> val.recipeCategory))
GTRegistries.RECIPE_CATEGORIES.codec().optionalFieldOf("category", GTRecipeCategory.EMPTY).forGetter(val -> val.recipeCategory))
.apply(instance, GTRecipe::new));
}
// @formatter:on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class GTRecipeType implements RecipeType<GTRecipe> {
@Getter
protected final Map<RecipeType<?>, List<GTRecipe>> proxyRecipes;
@Getter
private final Map<GTRecipeCategory, List<GTRecipe>> recipeByCategory = new Object2ObjectOpenHashMap<>();
private final Map<GTRecipeCategory, Set<GTRecipe>> categoryMap = new Object2ObjectOpenHashMap<>();
private CompoundTag customUICache;
@Getter
private final GTRecipeLookup lookup = new GTRecipeLookup(this);
Expand All @@ -112,7 +112,7 @@ public GTRecipeType(ResourceLocation registryName, String group, RecipeType<?>..
this.group = group;
recipeBuilder = new GTRecipeBuilder(registryName, this);
recipeBuilder.category(
GTRecipeCategory.of(GTCEu.MOD_ID, registryName.getPath(), registryName.toLanguageKey(), this));
GTRecipeCategory.of(GTCEu.MOD_ID, registryName.getPath(), this, registryName.toLanguageKey()));
// must be linked to stop json contents from shuffling
Map<RecipeType<?>, List<GTRecipe>> map = new Object2ObjectLinkedOpenHashMap<>();
for (RecipeType<?> proxyRecipe : proxyRecipes) {
Expand Down Expand Up @@ -342,8 +342,8 @@ public GTRecipe toGTrecipe(ResourceLocation id, Recipe<?> recipe) {
}

@NotNull
public Map<GTRecipeCategory, List<GTRecipe>> getRecipesByCategory() {
return Collections.unmodifiableMap(recipeByCategory);
public Map<GTRecipeCategory, Set<GTRecipe>> getRecipesByCategory() {
return Collections.unmodifiableMap(categoryMap);
}

public interface ICustomRecipeLogic {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
import com.gregtechceu.gtceu.api.registry.GTRegistries;
import com.gregtechceu.gtceu.common.data.GTRecipeTypes;

import net.minecraft.resources.ResourceLocation;

Expand All @@ -17,9 +18,10 @@
public class GTRecipeCategory {

private static final Map<String, GTRecipeCategory> categories = new Object2ObjectOpenHashMap<>();
public static final GTRecipeCategory EMPTY = new GTRecipeCategory();

@Getter
private final String modid;
private final String modID;
@Getter
private final String name;
@Getter
Expand All @@ -35,18 +37,31 @@ public class GTRecipeCategory {
private ResourceLocation resourceLocation;

public static GTRecipeCategory of(@NotNull String modID, @NotNull String categoryName,
@NotNull String translationKey, @NotNull GTRecipeType recipeType) {
@NotNull GTRecipeType recipeType, @NotNull String translationKey) {
return categories.computeIfAbsent(categoryName,
(k) -> new GTRecipeCategory(modID, categoryName, translationKey, recipeType));
(k) -> new GTRecipeCategory(modID, categoryName, recipeType, translationKey));
}

public static GTRecipeCategory of(@NotNull String modID, @NotNull String categoryName,
@NotNull GTRecipeType recipeType) {
return of(modID, categoryName, recipeType, "%s.recipe.category.%s".formatted(modID, categoryName));
}

public static GTRecipeCategory of(@NotNull GTRecipeType recipeType) {
return of(GTCEu.MOD_ID, recipeType.registryName.getPath(), recipeType.registryName.toLanguageKey(), recipeType);
return of(GTCEu.MOD_ID, recipeType.registryName.getPath(), recipeType, recipeType.registryName.toLanguageKey());
}

private GTRecipeCategory(@NotNull String modID, @NotNull String categoryName, @NotNull String translationKey,
@NotNull GTRecipeType recipeType) {
this.modid = modID;
private GTRecipeCategory() {
this.modID = "";
this.name = "";
this.uniqueID = "";
this.translation = "";
this.recipeType = GTRecipeTypes.DUMMY_RECIPES;
}

private GTRecipeCategory(@NotNull String modID, @NotNull String categoryName, @NotNull GTRecipeType recipeType,
@NotNull String translationKey) {
this.modID = modID;
this.name = categoryName;
this.uniqueID = modID + ":" + this.name;
this.translation = translationKey;
Expand All @@ -60,6 +75,10 @@ public GTRecipeCategory setIcon(@Nullable Object icon) {
return this;
}

public boolean isXEIVisible() {
return recipeType.getRecipeUI().isXEIVisible();
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.mojang.datafixers.util.Either;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -452,7 +453,7 @@ protected List<List<AbstractMapIngredient>> fromHolder(@NotNull IRecipeCapabilit
public void removeAllRecipes() {
this.lookup.getNodes().clear();
this.lookup.getSpecialNodes().clear();
this.recipeType.getRecipeByCategory().clear();
this.recipeType.getCategoryMap().clear();
}

/**
Expand All @@ -467,7 +468,7 @@ public boolean addRecipe(GTRecipe recipe) {
}
if (recipe.recipeCategory == null) {
recipe.recipeCategory = GTRecipeCategory.of(GTCEu.MOD_ID, recipe.recipeType.registryName.getPath(),
recipe.recipeType.registryName.toLanguageKey(), recipe.recipeType);
recipe.recipeType, recipe.recipeType.registryName.toLanguageKey());
}
// Add combustion fuels to the Powerless Jetpack
if (recipe.getType() == GTRecipeTypes.COMBUSTION_GENERATOR_FUELS) {
Expand All @@ -477,11 +478,8 @@ public boolean addRecipe(GTRecipe recipe) {
}
List<List<AbstractMapIngredient>> items = fromRecipe(recipe);
if (recurseIngredientTreeAdd(recipe, items, lookup, 0, 0)) {
recipeType.getRecipeByCategory().compute(recipe.recipeCategory, (k, v) -> {
if (v == null) v = new ArrayList<>();
v.add(recipe);
return v;
});
recipeType.getCategoryMap().computeIfAbsent(recipe.recipeCategory, k -> new ObjectLinkedOpenHashSet<>())
.add(recipe);
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public static void init() {
GTEntityTypes.init();
GTBlockEntities.init();
GTRecipeTypes.init();
GTRecipeCategories.init();
GTMachines.init();
GTFoods.init();
GTItems.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2798,7 +2798,7 @@ public static void init() {
"bedrock_ore_miner", BedrockOreMinerMachine::new, (tier, builder) -> builder
.rotationState(RotationState.NON_Y_AXIS)
.langValue("%s Bedrock Ore Miner %s".formatted(VLVH[tier], VLVT[tier]))
.recipeType(new GTRecipeType(GTCEu.id("bedrock_ore_miner"), "dummy"))
.recipeType(DUMMY_RECIPES)
.tooltips(
Component.translatable("gtceu.machine.bedrock_ore_miner.description"),
Component.translatable("gtceu.machine.bedrock_ore_miner.depletion",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
package com.gregtechceu.gtceu.common.data;

import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.GTCEuAPI;
import com.gregtechceu.gtceu.api.gui.GuiTextures;
import com.gregtechceu.gtceu.api.recipe.category.GTRecipeCategory;
import com.gregtechceu.gtceu.api.registry.GTRegistries;

import net.minecraftforge.fml.ModLoader;

public class GTRecipeCategories {

// Used for recipes you don't want in a category
public static final GTRecipeCategory DUMMY = GTRecipeCategory.of(GTRecipeTypes.DUMMY_RECIPES);

public static final GTRecipeCategory ARC_FURNACE_RECYCLING = GTRecipeCategory
.of(GTCEu.MOD_ID, "arc_furnace_recycling",
"gtceu.recipe.category.arc_furnace_recycling",
GTRecipeTypes.ARC_FURNACE_RECIPES)
.of(GTCEu.MOD_ID, "arc_furnace_recycling", GTRecipeTypes.ARC_FURNACE_RECIPES)
.setIcon(GuiTextures.ARC_FURNACE_RECYCLING_CATEGORY);

public static final GTRecipeCategory MACERATOR_RECYCLING = GTRecipeCategory
.of(GTCEu.MOD_ID, "macerator_recycling",
"gtceu.recipe.category.macerator_recycling",
GTRecipeTypes.MACERATOR_RECIPES)
.of(GTCEu.MOD_ID, "macerator_recycling", GTRecipeTypes.MACERATOR_RECIPES)
.setIcon(GuiTextures.MACERATOR_RECYCLING_CATEGORY);

public static final GTRecipeCategory EXTRACTOR_RECYCLING = GTRecipeCategory
.of(GTCEu.MOD_ID, "extractor_recycling",
"gtceu.recipe.category.extractor_recycling",
GTRecipeTypes.EXTRACTOR_RECIPES)
.of(GTCEu.MOD_ID, "extractor_recycling", GTRecipeTypes.EXTRACTOR_RECIPES)
.setIcon(GuiTextures.EXTRACTOR_RECYCLING_CATEGORY);

private GTRecipeCategories() {}
public static void init() {
GTRegistries.RECIPE_CATEGORIES.remove(DUMMY.getResourceLocation());
ModLoader.get().postEvent(new GTCEuAPI.RegisterEvent<>(GTRegistries.RECIPE_CATEGORIES, GTRecipeCategory.class));
GTRegistries.RECIPE_CATEGORIES.freeze();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,6 @@ public static void init() {
}
ModLoader.get().postEvent(new GTCEuAPI.RegisterEvent<>(GTRegistries.RECIPE_TYPES, GTRecipeType.class));
GTRegistries.RECIPE_TYPES.freeze();
ModLoader.get().postEvent(new GTCEuAPI.RegisterEvent<>(GTRegistries.RECIPE_CATEGORIES, GTRecipeCategory.class));

GTRegistries.register(BuiltInRegistries.RECIPE_SERIALIZER, GTCEu.id("machine"),
GTRecipeSerializer.SERIALIZER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.GTValues;
import com.gregtechceu.gtceu.api.machine.MachineDefinition;
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
import com.gregtechceu.gtceu.api.recipe.category.GTRecipeCategory;
import com.gregtechceu.gtceu.api.registry.GTRegistries;
import com.gregtechceu.gtceu.common.data.GTMachines;
import com.gregtechceu.gtceu.common.data.GTRecipeTypes;
import com.gregtechceu.gtceu.config.ConfigHolder;
Expand All @@ -21,9 +21,6 @@
import com.lowdragmc.lowdraglib.Platform;
import com.lowdragmc.lowdraglib.gui.modular.ModularUIContainer;

import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.item.crafting.RecipeType;

import appeng.menu.me.items.PatternEncodingTermMenu;
import de.mari_023.ae2wtlib.wet.WETMenu;
import dev.emi.emi.api.EmiEntrypoint;
Expand All @@ -49,13 +46,9 @@ public void register(EmiRegistry registry) {
registry.addCategory(GTBedrockFluidEmiCategory.CATEGORY);
if (ConfigHolder.INSTANCE.machines.doBedrockOres)
registry.addCategory(GTBedrockOreEmiCategory.CATEGORY);
for (RecipeType<?> recipeType : BuiltInRegistries.RECIPE_TYPE) {
if (recipeType instanceof GTRecipeType gtRecipeType) {
if (Platform.isDevEnv() || gtRecipeType.getRecipeUI().isXEIVisible()) {
for (GTRecipeCategory category : gtRecipeType.getRecipesByCategory().keySet()) {
registry.addCategory(new GTRecipeEMICategory(gtRecipeType, category));
}
}
for (GTRecipeCategory category : GTRegistries.RECIPE_CATEGORIES) {
if (Platform.isDevEnv() || category.isXEIVisible()) {
registry.addCategory(GTRecipeEMICategory.CATEGORIES.apply(category));
}
}
registry.addRecipeHandler(ModularUIContainer.MENUTYPE, new GTEmiRecipeHandler());
Expand Down Expand Up @@ -95,7 +88,8 @@ public void register(EmiRegistry registry) {
EmiStack.of(GTMachines.STEAM_FURNACE.right().asStack()));
registry.addWorkstation(VanillaEmiRecipeCategories.SMELTING, EmiStack.of(GTMachines.STEAM_OVEN.asStack()));
registry.addWorkstation(VanillaEmiRecipeCategories.SMELTING, EmiStack.of(GTMachines.MULTI_SMELTER.asStack()));
registry.addWorkstation(GTRecipeEMICategory.getCategoryFor(GTRecipeCategory.of(GTRecipeTypes.CHEMICAL_RECIPES)),
registry.addWorkstation(
GTRecipeEMICategory.CATEGORIES.apply(GTRecipeCategory.of(GTRecipeTypes.CHEMICAL_RECIPES)),
EmiStack.of(GTMachines.LARGE_CHEMICAL_REACTOR.asStack()));
}
}
Loading