Skip to content

Commit

Permalink
Fix compatibility with EMI 1.0.0-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed May 14, 2023
1 parent 04178a8 commit 67b5760
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric_version=0.77.0+1.19.4

maven_group = eu.pb4

mod_version = 0.4.8
mod_version = 0.4.9

minecraft_version_supported = ">=1.19.4-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,30 @@ public static void iterateItems(Consumer<ItemStack> consumer) {
}
}

public static void registerReload(Runnable runnable) {
public static void registerSyncReload(Runnable runnable) {
if (PolymerImpl.IS_CLIENT) {
PolymerClientUtils.ON_CLEAR.register(() -> {
if (MinecraftClient.getInstance().world != null) {
runnable.run();
}
});
PolymerClientUtils.ON_SEARCH_REBUILD.register(() -> {
if (MinecraftClient.getInstance().world != null) {
runnable.run();
}
});
PolymerClientUtils.ON_CLEAR.register(safeRunnable(runnable));
PolymerClientUtils.ON_SEARCH_REBUILD.register(safeRunnable(runnable));
}
}

private static Runnable safeRunnable(Runnable runnable) {
return () -> {
if (!MinecraftClient.getInstance().isOnThread()) {
MinecraftClient.getInstance().execute(() -> {
if (MinecraftClient.getInstance().world != null) {
runnable.run();
}
});
return;
}

if (MinecraftClient.getInstance().world != null) {
runnable.run();
}
};
}

public static String getModName(ItemStack stack) {
var id = PolymerItemUtils.getServerIdentifier(stack);
if (id != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
package eu.pb4.polymer.core.impl.client.compat;

import dev.emi.emi.EmiReloadManager;
import dev.emi.emi.EmiStackList;
import dev.emi.emi.api.EmiPlugin;
import dev.emi.emi.api.EmiRegistry;
import dev.emi.emi.api.stack.EmiStack;
import eu.pb4.polymer.core.impl.PolymerImpl;
import eu.pb4.polymer.core.impl.PolymerImplUtils;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.Version;
import org.jetbrains.annotations.ApiStatus;

import java.util.List;
import java.util.function.Predicate;

@ApiStatus.Internal
public class EmiCompatibility implements EmiPlugin {
public static final boolean IS_1_0_0 = FabricLoader.getInstance().getModContainer("emi").map(x -> {
try {
return x.getMetadata().getVersion().compareTo(Version.parse("1.0.0-")) >= 0;
} catch (Throwable e) {
return false;
}
}).orElse(false);

private static final Predicate<EmiStack> SHOULD_REMOVE = (stack) -> PolymerImplUtils.isPolymerControlled(stack.getItemStack());

static {
CompatUtils.registerReload(() -> EmiReloadManager.reload());
CompatUtils.registerSyncReload(EmiCompatibility::tryReloading);
}

private static void tryReloading() {
try {
Class.forName(IS_1_0_0 ? "dev.emi.emi.runtime.EmiReloadManager" : "dev.emi.emi.EmiReloadManager").getMethod("reload").invoke(null);
} catch (Throwable e) {
e.printStackTrace();
}
}

@Override
Expand All @@ -32,9 +49,9 @@ private static void update(EmiRegistry registry) {
}
synchronized (registry) {
try {
EmiStackList.stacks.removeIf(SHOULD_REMOVE);
((List<EmiStack>) Class.forName(IS_1_0_0 ? "dev.emi.emi.registry.EmiStackList" : "dev.emi.emi.EmiStackList").getField("stacks").get(null)).removeIf(SHOULD_REMOVE);
CompatUtils.iterateItems(stack -> registry.addEmiStack(EmiStack.of(stack)));
} catch (Exception e) {
} catch (Throwable e) {
e.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public void registerRecipes(IRecipeRegistration registration) {
}

public void onRuntimeAvailable(IJeiRuntime jeiRuntime) {
CompatUtils.registerReload(() -> update(jeiRuntime.getIngredientManager()));
CompatUtils.registerSyncReload(() -> update(jeiRuntime.getIngredientManager()));
}


private static void update(IIngredientManager manager) {
synchronized (manager) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static void update(EntryRegistry registry) {
}

public static void registerEvents() {
CompatUtils.registerReload(() -> {
CompatUtils.registerSyncReload(() -> {
try {
RoughlyEnoughItemsCoreClient.reloadPlugins(null, null);
}catch (Throwable e) {
Expand Down

0 comments on commit 67b5760

Please sign in to comment.