Skip to content

Commit

Permalink
Add support for Mek ingredients when running EMI+JEI
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n committed Mar 7, 2024
1 parent aa13ca3 commit dfb2377
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 2 deletions.
26 changes: 24 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ repositories {
}
}

maven {
url "https://maven.terraformersmc.com"
content {
includeGroup "dev.emi"
}
}

maven {
name = "Local"
url = file("libs").toURI()
Expand All @@ -65,8 +72,23 @@ dependencies {
exclude(group: "com.blamejared.crafttweaker")
}

compileOnly("mezz.jei:jei-${minecraft_version}-common-api:${jei_version}")
runtimeOnly("mezz.jei:jei-${minecraft_version}-neoforge:${jei_version}")
if (project.runtime_itemlist_mod == "jei") {
implementation "mezz.jei:jei-${project.minecraft_version}-neoforge:${project.jei_version}"
} else {
compileOnly "mezz.jei:jei-${project.minecraft_version}-neoforge:${project.jei_version}"
}

if (project.runtime_itemlist_mod == "emi") {
implementation "dev.emi:emi-neoforge:${project.emi_version}+${project.minecraft_version}"
// Add JEI so that we can use EMI's builtin compat layer
runtimeOnly "mezz.jei:jei-${project.minecraft_version}-neoforge:${project.jei_version}"
} else {
compileOnly "dev.emi:emi-neoforge:${project.emi_version}+${project.minecraft_version}"
}

if (!(project.runtime_itemlist_mod in ["jei", "emi"])) {
throw new GradleException("Invalid runtime_itemlist_mod value: " + project.runtime_itemlist_mod)
}

compileOnly("curse.maven:jade-324717:${jade_id}")
}
Expand Down
6 changes: 6 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ neoforge_version=20.4.173
ae2_version=17.0.5-beta
mekanism_version=10.5.0.22
jei_version=17.0.0.30
emi_version=1.1.0
jade_id=4980188

# Dev runtime options
## Set to "jei" or "emi" to pick which tooltip mod gets picked at runtime
## for the dev environment.
runtime_itemlist_mod=emi

loader_version_range=[2,)
neo_version_range=[20.4,)
ae2_version_range=[17.0.0,18.0.0)
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/me/ramidzkh/mekae2/integration/emi/AMEmiPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.ramidzkh.mekae2.integration.emi;

import net.neoforged.fml.ModList;

import dev.emi.emi.api.EmiEntrypoint;
import dev.emi.emi.api.EmiPlugin;
import dev.emi.emi.api.EmiRegistry;

@EmiEntrypoint
public class AMEmiPlugin implements EmiPlugin {
@Override
public void register(EmiRegistry emiRegistry) {
}

static {
if (ModList.get().isLoaded("jei")) {
// We need both EMI and JEI to do anything!
MekanismJemiAdapter.init();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package me.ramidzkh.mekae2.integration.emi;

import com.mojang.logging.LogUtils;

import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.jemi.JemiStack;
import dev.emi.emi.jemi.JemiUtil;
import me.ramidzkh.mekae2.ae2.MekanismKey;
import mekanism.api.chemical.Chemical;
import mekanism.api.chemical.ChemicalStack;

import appeng.api.integrations.emi.EmiStackConverter;
import appeng.api.stacks.GenericStack;

public final class ChemicalIngredientConverter implements EmiStackConverter {
private static final Logger LOGGER = LogUtils.getLogger();

@Override
public Class<?> getKeyType() {
// It doesn't really matter, AE2 only checks that no two converters have the same type.
return Chemical.class;
}

@Override
public @Nullable EmiStack toEmiStack(GenericStack stack) {
try {
if (stack.what() instanceof MekanismKey key) {
return JemiUtil.getStack(key.withAmount(Math.max(1, stack.amount())));
}
} catch (Exception e) { // catch error in case JEMI internals change
LOGGER.error("Failed to convert GenericStack to EmiStack", e);
}
return null;
}

@Override
public @Nullable GenericStack toGenericStack(EmiStack stack) {
try {
if (stack instanceof JemiStack<?> jemiStack) {
var ing = jemiStack.ingredient;
if (ing instanceof ChemicalStack<?> chemicalStack) {
var mekKey = MekanismKey.of(chemicalStack);
if (mekKey != null) {
return new GenericStack(mekKey, stack.getAmount());
}
}
}
} catch (Exception e) { // catch error in case JEMI internals change
LOGGER.error("Failed to convert EmiStack to GenericStack", e);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package me.ramidzkh.mekae2.integration.emi;

import appeng.api.integrations.emi.EmiStackConverters;

public final class MekanismJemiAdapter {
private MekanismJemiAdapter() {
}

public static void init() {
EmiStackConverters.register(new ChemicalIngredientConverter());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package me.ramidzkh.mekae2.integration.emi;

import javax.annotation.ParametersAreNonnullByDefault;

import net.minecraft.MethodsReturnNonnullByDefault;

0 comments on commit dfb2377

Please sign in to comment.