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

Item Material Information Refactor #2591

Open
wants to merge 26 commits into
base: 1.20.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9b3cfc2
uhhh stuff and things
YoungOnionMC Dec 18, 2024
8b7fd55
get machine based recipe material info mapping working
YoungOnionMC Dec 19, 2024
bf5259c
tinker with more recipes
YoungOnionMC Dec 19, 2024
d74c3bd
better material stack application
YoungOnionMC Dec 19, 2024
f446a3e
i hate fluid tags
YoungOnionMC Dec 19, 2024
45cfe78
Merge branch '1.20.1' into yo/material-decomp-refactor
screret Dec 21, 2024
5f5e5ad
remake MaterialEntry toString
screret Dec 21, 2024
067547e
it would work if tags were loaded, TODO figure out a working entry po…
screret Dec 21, 2024
a7d0415
Loads of decomp recipes, also get fluid handling working in a capacity
YoungOnionMC Dec 22, 2024
4433bf9
Merge remote-tracking branch 'origin/yo/material-decomp-refactor' int…
YoungOnionMC Dec 22, 2024
80cda00
Merge branch '1.20.1' of https://github.com/GregTechCEu/GregTech-Mode…
YoungOnionMC Dec 22, 2024
a26e00b
merge screrets stuff and fix fluid tags dying
YoungOnionMC Dec 22, 2024
44ea568
spotless
YoungOnionMC Dec 22, 2024
21cf5b2
oops
YoungOnionMC Dec 22, 2024
d122cda
oops
YoungOnionMC Dec 22, 2024
dd66cdb
Merge branch '1.20.1' of https://github.com/GregTechCEu/GregTech-Mode…
YoungOnionMC Dec 27, 2024
01dbe7c
more unificationentry changes
YoungOnionMC Dec 27, 2024
49064f4
change the material decomp fields to private
YoungOnionMC Dec 27, 2024
3b5e887
stuff
YoungOnionMC Dec 27, 2024
a52da2d
attach material stacks from kube to the recipe builder
YoungOnionMC Dec 28, 2024
74f78f2
get kube to work with material info removals and overriding
YoungOnionMC Dec 29, 2024
5e11db3
spotless
YoungOnionMC Dec 29, 2024
cb79247
Merge branch '1.20.1' of https://github.com/GregTechCEu/GregTech-Mode…
YoungOnionMC Dec 29, 2024
7272430
merge
YoungOnionMC Dec 29, 2024
95b21f5
fix errant code
YoungOnionMC Dec 30, 2024
7a263fe
fix errant code
YoungOnionMC Dec 30, 2024
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 @@ -169,7 +169,7 @@ public List<AbstractMapIngredient> convertToMapIngredient(Object obj) {
}
TagPrefix prefix = ChemicalHelper.getPrefix(stack.getItem());
if (prefix != null && TagPrefix.ORES.containsKey(prefix)) {
Material material = ChemicalHelper.getMaterial(stack.getItem()).material();
Material material = ChemicalHelper.getMaterialStack(stack.getItem()).material();
ingredients.add(new MapIntersectionIngredient((IntersectionIngredient) IntersectionIngredient.of(
Ingredient.of(prefix.getItemTags(material)[0]), Ingredient.of(prefix.getItemParentTags()[0]))));
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package com.gregtechceu.gtceu.api.data.chemical.material;

import com.gregtechceu.gtceu.api.GTCEuAPI;
import com.gregtechceu.gtceu.api.data.chemical.material.properties.FluidProperty;
import com.gregtechceu.gtceu.api.data.chemical.material.properties.PropertyKey;
import com.gregtechceu.gtceu.api.data.chemical.material.stack.ItemMaterialInfo;
import com.gregtechceu.gtceu.api.data.chemical.material.stack.MaterialEntry;
import com.gregtechceu.gtceu.api.data.chemical.material.stack.MaterialStack;
import com.gregtechceu.gtceu.api.data.tag.TagPrefix;
import com.gregtechceu.gtceu.api.fluids.store.FluidStorageKey;
import com.gregtechceu.gtceu.common.data.GTMaterialItems;
import com.gregtechceu.gtceu.data.recipe.misc.RecyclingRecipes;
import com.gregtechceu.gtceu.data.recipe.misc.WoodMachineRecipes;
import com.gregtechceu.gtceu.data.tags.TagsHandler;
import com.gregtechceu.gtceu.utils.ItemStackHashStrategy;
import com.gregtechceu.gtceu.utils.SupplierMemoizer;

import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.recipes.FinishedRecipe;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.common.crafting.conditions.ICondition;
import net.minecraftforge.registries.RegistryObject;

import com.mojang.datafixers.util.Pair;
import com.tterrag.registrate.util.entry.BlockEntry;
import com.tterrag.registrate.util.entry.ItemEntry;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenCustomHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class ItemMaterialData {

/** Used for custom material data for items that do not fall into the normal "prefix, material" pair */
public static final Map<ItemLike, ItemMaterialInfo> ITEM_MATERIAL_INFO = new ConcurrentHashMap<>();
/** Mapping of an item to a "prefix, material" pair */
public static final Set<Map.Entry<Supplier<? extends ItemLike>, MaterialEntry>> ITEM_MATERIAL_ENTRY = ConcurrentHashMap
.newKeySet();
public static final Map<ItemLike, MaterialEntry> ITEM_MATERIAL_ENTRY_COLLECTED = new ConcurrentHashMap<>();
/** Mapping of a tag to a "prefix, material" pair */
public static final Map<TagKey<Item>, MaterialEntry> TAG_MATERIAL_ENTRY = new Object2ObjectLinkedOpenHashMap<>();
/** Mapping of a fluid to a material */
public static final Map<Fluid, Material> FLUID_MATERIAL = new ConcurrentHashMap<>();
/** Mapping of all items that represent a "prefix, material" pair */
public static final Map<MaterialEntry, ArrayList<Supplier<? extends ItemLike>>> MATERIAL_ENTRY_ITEM_MAP = new ConcurrentHashMap<>();
public static final Map<MaterialEntry, ArrayList<Supplier<? extends Block>>> MATERIAL_ENTRY_BLOCK_MAP = new ConcurrentHashMap<>();
/** Mapping of stone type blockState to "prefix, material" */
public static final Map<Supplier<BlockState>, TagPrefix> ORES_INVERSE = new ConcurrentHashMap<>();

public static final Map<ItemStack, List<ItemStack>> UNRESOLVED_ITEM_MATERIAL_INFO = new Object2ReferenceOpenCustomHashMap<>(
ItemStackHashStrategy.comparingAllButCount());

public static void registerMaterialInfo(ItemLike item, ItemMaterialInfo materialInfo) {
ITEM_MATERIAL_INFO.put(item, materialInfo);
}

public static ItemMaterialInfo getMaterialInfo(Object item) {
if (item instanceof ItemLike itemLike) {
return getMaterialInfo(itemLike);
} else if (item instanceof ItemStack stack) {
return getMaterialInfo(stack.getItem());
}
return null;
}

public static ItemMaterialInfo getMaterialInfo(ItemLike item) {
if (item instanceof Block block) {
return ITEM_MATERIAL_INFO.get(block);
} else if (item instanceof BlockItem blockItem) {
var info = ITEM_MATERIAL_INFO.get(blockItem.getBlock());
if (info != null) return info;
return ITEM_MATERIAL_INFO.get(item);
} else if (item instanceof ItemEntry<?> entry) {
return ITEM_MATERIAL_INFO.get(entry.asItem());
}
return ITEM_MATERIAL_INFO.get(item);
}

public static void clearMaterialInfo(ItemLike item) {
if (item instanceof Block block) {
ITEM_MATERIAL_INFO.remove(block);
return;
} else if (item instanceof BlockItem blockItem) {
var info = ITEM_MATERIAL_INFO.get(blockItem.getBlock());
if (info != null) {
ITEM_MATERIAL_INFO.remove(blockItem.getBlock());
return;
}
ITEM_MATERIAL_INFO.remove(item);
return;
} else if (item instanceof ItemEntry<?> entry) {
ITEM_MATERIAL_INFO.remove(entry.asItem());
return;
}
ITEM_MATERIAL_INFO.remove(item);
}

@SafeVarargs
public static void registerMaterialInfoItems(MaterialEntry materialEntry,
Supplier<? extends ItemLike>... items) {
MATERIAL_ENTRY_ITEM_MAP.computeIfAbsent(materialEntry, entry -> new ArrayList<>())
.addAll(Arrays.asList(items));
for (Supplier<? extends ItemLike> item : items) {
ITEM_MATERIAL_ENTRY.add(Map.entry(item, materialEntry));
if (item instanceof Block block) {
MATERIAL_ENTRY_BLOCK_MAP.computeIfAbsent(materialEntry, entry -> new ArrayList<>())
.add(() -> block);
} else if (item instanceof BlockEntry<?> blockEntry) {
MATERIAL_ENTRY_BLOCK_MAP.computeIfAbsent(materialEntry, entry -> new ArrayList<>())
.add(blockEntry);
} else if (item instanceof RegistryObject<?> registryObject) {
if (registryObject.getKey().isFor(Registries.BLOCK)) {
MATERIAL_ENTRY_BLOCK_MAP.computeIfAbsent(materialEntry, entry -> new ArrayList<>())
.add((RegistryObject<Block>) registryObject);
}
} else if (item instanceof SupplierMemoizer.MemoizedBlockSupplier<? extends Block> supplier) {
MATERIAL_ENTRY_BLOCK_MAP.computeIfAbsent(materialEntry, entry -> new ArrayList<>())
.add(supplier);
}
}
if (TagPrefix.ORES.containsKey(materialEntry.tagPrefix()) &&
!ORES_INVERSE.containsValue(materialEntry.tagPrefix())) {
ORES_INVERSE.put(TagPrefix.ORES.get(materialEntry.tagPrefix()).stoneType(), materialEntry.tagPrefix());
}
for (TagKey<Item> tag : materialEntry.tagPrefix().getAllItemTags(materialEntry.material())) {
TAG_MATERIAL_ENTRY.putIfAbsent(tag, materialEntry);
}
}

@SafeVarargs
public static void registerMaterialInfoItems(TagPrefix tagPrefix, @Nullable Material material,
Supplier<? extends ItemLike>... items) {
registerMaterialInfoItems(new MaterialEntry(tagPrefix, material), items);
}

public static void registerMaterialInfoItems(TagPrefix tagPrefix, @Nullable Material material, ItemLike... items) {
registerMaterialInfoItems(new MaterialEntry(tagPrefix, material),
Arrays.stream(items).map(item -> (Supplier<ItemLike>) () -> item).toArray(Supplier[]::new));
for (ItemLike item : items) {
ITEM_MATERIAL_ENTRY_COLLECTED.put(item, new MaterialEntry(tagPrefix, material));
}
}

@ApiStatus.Internal
public static void reinitializeMaterialData(ICondition.IContext context) {
// Clear old data
MATERIAL_ENTRY_ITEM_MAP.clear();
MATERIAL_ENTRY_BLOCK_MAP.clear();
ITEM_MATERIAL_ENTRY.clear();
FLUID_MATERIAL.clear();

// Load new data
TagsHandler.initExtraUnificationEntries();
for (TagPrefix prefix : TagPrefix.values()) {
prefix.getIgnored().forEach((mat, items) -> {
if (items.length > 0) {
registerMaterialInfoItems(prefix, mat, items);
}
});
}
GTMaterialItems.toUnify.forEach(ItemMaterialData::registerMaterialInfoItems);
WoodMachineRecipes.registerUnificationInfo();
// resolveFluidMaterialInfos(context);
}

@ApiStatus.Internal
public static void resolveItemMaterialInfos(Consumer<FinishedRecipe> provider) {
for (var entry : UNRESOLVED_ITEM_MATERIAL_INFO.entrySet()) {
List<MaterialStack> stacks = new ArrayList<>();
for (var input : entry.getValue()) {
var matStack = getMaterialInfo(input.getItem());
if (matStack != null) {
matStack.getMaterials().forEach(ms -> stacks.add(ms.copy(ms.amount() / entry.getKey().getCount())));
}
}
if (stacks.isEmpty())
continue;
var matInfo = ITEM_MATERIAL_INFO.get(entry.getKey().getItem());
if (matInfo == null) {
ITEM_MATERIAL_INFO.put(entry.getKey().getItem(), new ItemMaterialInfo(stacks));
} else {
matInfo.addMaterialStacks(stacks);
}
RecyclingRecipes.registerRecyclingRecipes(provider, entry.getKey().copyWithCount(1),
ITEM_MATERIAL_INFO.get(entry.getKey().getItem()).getMaterials(), false, null);
}
UNRESOLVED_ITEM_MATERIAL_INFO.clear();
}

@ApiStatus.Internal
public static void resolveFluidMaterialInfos(ICondition.IContext context) {
if (context == null) {
return;
}

var allFluidTags = context.getAllTags(Registries.FLUID);
for (final Material material : GTCEuAPI.materialManager.getRegisteredMaterials()) {
if (material.hasProperty(PropertyKey.FLUID)) {
FluidProperty property = material.getProperty(PropertyKey.FLUID);
FluidStorageKey.allKeys().stream()
.map(property::get)
.filter(Objects::nonNull)
.map(f -> Pair.of(f,
new ResourceLocation("forge", BuiltInRegistries.FLUID.getKey(f).getPath())))
.filter(pair -> allFluidTags.containsKey(pair.getSecond()))
.forEach(pair -> {
allFluidTags.remove(pair.getSecond());
ItemMaterialData.FLUID_MATERIAL.put(pair.getFirst(), material);
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.data.chemical.ChemicalHelper;
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
import com.gregtechceu.gtceu.api.data.chemical.material.stack.UnificationEntry;
import com.gregtechceu.gtceu.api.data.chemical.material.stack.MaterialEntry;
import com.gregtechceu.gtceu.api.data.medicalcondition.MedicalCondition;
import com.gregtechceu.gtceu.api.data.tag.TagPrefix;
import com.gregtechceu.gtceu.api.item.GTBucketItem;
Expand Down Expand Up @@ -191,10 +191,10 @@ public static Material getValidHazardMaterial(ItemStack item) {
isFluid = true;
}
} else if (ConfigHolder.INSTANCE.gameplay.universalHazards) {
UnificationEntry entry = ChemicalHelper.getUnificationEntry(item.getItem());
if (entry != null && entry.material != null) {
material = entry.material;
prefix = entry.tagPrefix;
MaterialEntry entry = ChemicalHelper.getMaterialEntry(item.getItem());
if (entry != null && entry.material() != null) {
material = entry.material();
prefix = entry.tagPrefix();
}
}
if (material == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,60 @@
package com.gregtechceu.gtceu.api.data.chemical.material.stack;

import com.gregtechceu.gtceu.api.data.chemical.material.Material;

import com.google.common.collect.ImmutableList;
import it.unimi.dsi.fastutil.objects.Reference2LongLinkedOpenHashMap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class ItemMaterialInfo {

private final List<MaterialStack> materials = new ArrayList<>();
private Reference2LongLinkedOpenHashMap<Material> materials = new Reference2LongLinkedOpenHashMap<>();
private List<MaterialStack> sortedMaterials = new ArrayList<>();

public ItemMaterialInfo(MaterialStack... materials) {
this.materials.addAll(Arrays.asList(materials));
public ItemMaterialInfo(MaterialStack... materialStacks) {
for (var mat : materialStacks) {
materials.merge(mat.material(), mat.amount(), Long::sum);
}
setSortedMaterials();
}

public ItemMaterialInfo(List<MaterialStack> materials) {
this.materials.addAll(materials);
public ItemMaterialInfo(List<MaterialStack> materialStacks) {
for (var mat : materialStacks) {
materials.merge(mat.material(), mat.amount(), Long::sum);
}
setSortedMaterials();
}

/**
* Returns the first MaterialStack in the "materials" list
*/
public MaterialStack getMaterial() {
return materials.size() == 0 ? null : materials.get(0);
return sortedMaterials.isEmpty() ? null : sortedMaterials.get(0);
}

/**
* Returns all MaterialStacks associated with this Object.
*/
public ImmutableList<MaterialStack> getMaterials() {
return ImmutableList.copyOf(materials);
return ImmutableList.copyOf(sortedMaterials);
}

public void addMaterialStacks(List<MaterialStack> stacks) {
for (var mat : stacks) {
materials.merge(mat.material(), mat.amount(), Long::sum);
}
setSortedMaterials();
}

private void setSortedMaterials() {
sortedMaterials.clear();
for (var m : materials.keySet()) {
sortedMaterials.add(new MaterialStack(m, materials.getLong(m)));
}
sortedMaterials.sort(Comparator.comparingLong(MaterialStack::amount));
}

@Override
Expand All @@ -38,16 +63,16 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;

ItemMaterialInfo that = (ItemMaterialInfo) o;
return materials.equals(that.materials);
return sortedMaterials.equals(that.sortedMaterials);
}

@Override
public int hashCode() {
return materials.hashCode();
return sortedMaterials.hashCode();
}

@Override
public String toString() {
return materials.size() == 0 ? "" : materials.get(0).material().toCamelCaseString();
return sortedMaterials.isEmpty() ? "" : sortedMaterials.get(0).material().toCamelCaseString();
}
}
Loading
Loading