Skip to content

Commit

Permalink
Restructure how we define aliases in preparation of JEI's upcoming ab…
Browse files Browse the repository at this point in the history
…ility to define aliases
  • Loading branch information
pupnewfster committed Aug 25, 2024
1 parent 8a8e25f commit 9cc8430
Show file tree
Hide file tree
Showing 26 changed files with 419 additions and 353 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mekanism.additions.client.recipe_viewer.aliases;

import mekanism.additions.common.registries.AdditionsBlocks;
import mekanism.additions.common.registries.AdditionsItems;
import mekanism.client.recipe_viewer.alias.IAliasMapping;
import mekanism.client.recipe_viewer.alias.RVAliasHelper;

public class AdditionsAliasMapping implements IAliasMapping {

@Override
public <ITEM, FLUID, CHEMICAL> void addAliases(RVAliasHelper<ITEM, FLUID, CHEMICAL> rv) {
rv.addAliases(AdditionsItems.WALKIE_TALKIE, AdditionsAliases.WALKIE_TALKIE_RADIO);
rv.addAliases(AdditionsBlocks.GLOW_PANELS.values(), AdditionsAliases.GLOW_PANEL_LIGHT_SOURCE);
rv.addAliases(AdditionsBlocks.PLASTIC_ROADS.values(), AdditionsAliases.PLASTIC_ROAD_PATH);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package mekanism.additions.client.integration;
package mekanism.additions.client.recipe_viewer.aliases;

import mekanism.additions.common.MekanismAdditions;
import mekanism.api.annotations.NothingNullByDefault;
import mekanism.client.integration.IAliasedTranslation;
import mekanism.client.recipe_viewer.alias.IAliasedTranslation;
import net.minecraft.Util;

@NothingNullByDefault
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mekanism.additions.client;

import java.util.Map;
import mekanism.additions.client.integration.AdditionsAliases;
import mekanism.additions.client.recipe_viewer.aliases.AdditionsAliases;
import mekanism.additions.common.AdditionsLang;
import mekanism.additions.common.AdditionsTags;
import mekanism.additions.common.MekanismAdditions;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import mekanism.additions.client.AdditionsLangProvider;
import mekanism.additions.client.AdditionsSoundProvider;
import mekanism.additions.client.AdditionsSpriteSourceProvider;
import mekanism.additions.client.integration.emi.AdditionsEmiAliasProvider;
import mekanism.additions.client.integration.emi.AdditionsEmiDefaults;
import mekanism.additions.client.recipe_viewer.aliases.AdditionsAliasMapping;
import mekanism.additions.common.loot.AdditionsLootProvider;
import mekanism.additions.common.recipe.AdditionsRecipeProvider;
import mekanism.common.BasePackMetadataGenerator;
Expand Down Expand Up @@ -51,7 +51,7 @@ public static void gatherData(GatherDataEvent event) {
gen.addProvider(event.includeServer(), new AdditionsAdvancementProvider(output, lookupProvider, existingFileHelper));
//Data generator to help with persisting data when porting across MC versions when optional deps aren't updated yet
// DO NOT ADD OTHERS AFTER THIS ONE
PersistingDisabledProvidersProvider.addDisabledEmiProvider(event, lookupProvider, MekanismAdditions.MODID, () -> AdditionsEmiAliasProvider::new,
PersistingDisabledProvidersProvider.addDisabledEmiProvider(event, lookupProvider, MekanismAdditions.MODID, AdditionsAliasMapping::new,
() -> AdditionsEmiDefaults::new);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mekanism.generators.client;

import mekanism.client.lang.BaseLanguageProvider;
import mekanism.generators.client.integration.GeneratorsAliases;
import mekanism.generators.client.recipe_viewer.alias.GeneratorsAliases;
import mekanism.generators.common.GeneratorTags;
import mekanism.generators.common.GeneratorsLang;
import mekanism.generators.common.MekanismGenerators;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import mekanism.generators.client.GeneratorsLangProvider;
import mekanism.generators.client.GeneratorsSoundProvider;
import mekanism.generators.client.GeneratorsSpriteSourceProvider;
import mekanism.generators.client.integration.emi.GeneratorsEmiAliasProvider;
import mekanism.generators.client.integration.emi.GeneratorsEmiDefaults;
import mekanism.generators.client.recipe_viewer.alias.GeneratorsAliasMapping;
import mekanism.generators.common.loot.GeneratorsLootProvider;
import net.minecraft.core.HolderLookup;
import net.minecraft.data.DataGenerator;
Expand Down Expand Up @@ -47,7 +47,7 @@ public static void gatherData(GatherDataEvent event) {
gen.addProvider(event.includeServer(), new GeneratorsAdvancementProvider(output, lookupProvider, existingFileHelper));
//Data generator to help with persisting data when porting across MC versions when optional deps aren't updated yet
// DO NOT ADD OTHERS AFTER THIS ONE
PersistingDisabledProvidersProvider.addDisabledEmiProvider(event, lookupProvider, MekanismGenerators.MODID, () -> GeneratorsEmiAliasProvider::new,
PersistingDisabledProvidersProvider.addDisabledEmiProvider(event, lookupProvider, MekanismGenerators.MODID, GeneratorsAliasMapping::new,
() -> GeneratorsEmiDefaults::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.emi.emi.api.EmiInitRegistry;
import dev.emi.emi.api.neoforge.NeoForgeEmiStack;
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.api.stack.serializer.EmiIngredientSerializer;
Expand All @@ -16,23 +17,22 @@
import dev.emi.emi.registry.EmiPluginContainer;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Supplier;
import mekanism.api.SerializationConstants;
import mekanism.api.annotations.NothingNullByDefault;
import mekanism.api.gear.IModuleHelper;
import mekanism.api.providers.IChemicalProvider;
import mekanism.api.providers.IFluidProvider;
import mekanism.api.text.IHasTranslationKey;
import mekanism.client.recipe_viewer.alias.IAliasMapping;
import mekanism.client.recipe_viewer.alias.RVAliasHelper;
import mekanism.client.recipe_viewer.emi.ChemicalEmiStack;
import mekanism.common.DataGenSerializationConstants;
import mekanism.common.content.gear.IModuleItem;
import mekanism.common.integration.MekanismHooks;
import mekanism.common.lib.collection.HashList;
import mekanism.common.registration.impl.ItemDeferredRegister;
import net.minecraft.core.HolderLookup;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
Expand All @@ -41,12 +41,12 @@
import net.minecraft.data.PackOutput.Target;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.fluids.FluidStack;

@NothingNullByDefault
public abstract class BaseEmiAliasProvider implements DataProvider {
public class EmiAliasProvider implements DataProvider, RVAliasHelper<EmiIngredient, EmiIngredient, EmiIngredient> {

private static boolean emiSerializersInitialized;

Expand All @@ -65,55 +65,74 @@ private static void bootstrapEmi() {

private final CompletableFuture<HolderLookup.Provider> registries;
private final HashList<AliasInfo> data = new HashList<>();
private final Supplier<IAliasMapping> mappings;
private final PathProvider pathProvider;
private final String modid;

protected BaseEmiAliasProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> registries, String modid) {
public EmiAliasProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> registries, String modid, Supplier<IAliasMapping> mappings) {
this.pathProvider = output.createPathProvider(Target.RESOURCE_PACK, "aliases");
this.registries = registries;
this.modid = modid;
this.mappings = mappings;
}

@Override
public final CompletableFuture<?> run(CachedOutput cachedOutput) {
bootstrapEmi();
return this.registries.thenCompose(lookupProvider -> {
addAliases(lookupProvider);
IAliasMapping mapping = mappings.get();
mapping.addAliases(this);
Path path = pathProvider.json(ResourceLocation.fromNamespaceAndPath(MekanismHooks.EMI_MOD_ID, modid));
return DataProvider.saveStable(cachedOutput, lookupProvider, AliasInfo.LIST_CODEC, data.elements(), path);
});
}

protected EmiIngredient ingredient(IFluidProvider fluidProvider) {
@Override
public EmiIngredient ingredient(ItemLike itemLike) {
return EmiStack.of(itemLike);
}

@Override
public EmiIngredient ingredient(ItemStack item) {
return EmiStack.of(item);
}

@Override
public EmiIngredient ingredient(IFluidProvider fluidProvider) {
return EmiStack.of(fluidProvider.getFluid(), 1);
}

protected EmiIngredient ingredient(IChemicalProvider chemicalProvider) {
return ChemicalEmiStack.create(chemicalProvider, 1);
@Override
public EmiIngredient ingredient(FluidStack fluid) {
return NeoForgeEmiStack.of(fluid);
}

protected abstract void addAliases(HolderLookup.Provider lookupProvider);
@Override
public EmiIngredient ingredient(IChemicalProvider chemicalProvider) {
return ChemicalEmiStack.create(chemicalProvider, 1);
}

protected void addAlias(IHasTranslationKey alias, ItemLike... items) {
if (items.length == 0) {
throw new IllegalArgumentException("Expected to have at least one item");
}
addAliases(Arrays.stream(items).<EmiIngredient>map(EmiStack::of).toList(), alias);
@Override
public void addAliases(IFluidProvider fluidProvider, IChemicalProvider chemicalProvider, IHasTranslationKey... aliases) {
addAliases(List.of(ingredient(fluidProvider), ingredient(chemicalProvider)), aliases);
}

protected void addAliases(ItemLike item, IHasTranslationKey... aliases) {
addAliases(EmiStack.of(item), aliases);
@Override
public void addItemAliases(List<EmiIngredient> stacks, IHasTranslationKey... aliases) {
addAliases(stacks, aliases);
}

protected void addAliases(Collection<? extends ItemLike> stacks, IHasTranslationKey... aliases) {
addAliases(stacks.stream().<EmiIngredient>map(EmiStack::of).toList(), aliases);
@Override
public void addFluidAliases(List<EmiIngredient> stacks, IHasTranslationKey... aliases) {
addAliases(stacks, aliases);
}

protected void addAliases(EmiIngredient stack, IHasTranslationKey... aliases) {
addAliases(List.of(stack), aliases);
@Override
public void addChemicalAliases(List<EmiIngredient> stacks, IHasTranslationKey... aliases) {
addAliases(stacks, aliases);
}

protected void addAliases(List<EmiIngredient> stacks, IHasTranslationKey... aliases) {
private void addAliases(List<EmiIngredient> stacks, IHasTranslationKey... aliases) {
if (aliases.length == 0) {
throw new IllegalArgumentException("Expected to have at least one alias");
}
Expand All @@ -130,18 +149,6 @@ protected void addAliases(List<EmiIngredient> stacks, IHasTranslationKey... alia
}
}

protected void addModuleAliases(ItemDeferredRegister items) {
for (DeferredHolder<Item, ? extends Item> entry : items.getEntries()) {
if (entry.get() instanceof IModuleItem module) {
addAliases(entry.get(), IModuleHelper.INSTANCE.getSupported(module.getModuleData())
.stream()
.map(item -> (IHasTranslationKey) item::getDescriptionId)
.toArray(IHasTranslationKey[]::new)
);
}
}
}

@Override
public String getName() {
return "EMI Alias Provider: " + modid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import mekanism.api.providers.IBlockProvider;
import mekanism.api.providers.IModuleDataProvider;
import mekanism.api.text.IHasTranslationKey;
import mekanism.client.integration.IAliasedTranslation;
import mekanism.client.recipe_viewer.alias.IAliasedTranslation;
import mekanism.client.lang.FormatSplitter.Component;
import mekanism.common.Mekanism;
import mekanism.common.advancements.MekanismAdvancement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import mekanism.api.robit.RobitSkin;
import mekanism.api.text.APILang;
import mekanism.api.text.EnumColor;
import mekanism.client.integration.MekanismAliases;
import mekanism.client.recipe_viewer.alias.MekanismAliases;
import mekanism.common.Mekanism;
import mekanism.common.MekanismLang;
import mekanism.common.advancements.MekanismAdvancements;
Expand Down
Loading

0 comments on commit 9cc8430

Please sign in to comment.