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 f1d9f19 commit a151e50
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ fabric_version=0.60.0+1.19.2

maven_group = eu.pb4

all_version = 0.2.26
core_version = 0.2.26
all_version = 0.2.27
core_version = 0.2.27
blocks_ext_version = 0.1.0-alpha.9
reg_sync_man_version = 0.0.3
autohost_version = 0.1.4
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package eu.pb4.polymer.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.Comparison;
Expand All @@ -13,30 +11,48 @@
import eu.pb4.polymer.impl.PolymerImpl;
import eu.pb4.polymer.impl.client.InternalClientItemGroup;
import eu.pb4.polymer.impl.client.interfaces.ClientItemGroupExtension;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.Version;
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

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

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) -> (PolymerItemUtils.isPolymerServerItem(stack.getItemStack()) || PolymerItemUtils.getServerIdentifier(stack.getItemStack()) != null || PolymerUtils.isServerOnly(stack.getEntry().getValue()));

static {
if (PolymerImpl.IS_CLIENT) {
PolymerClientUtils.ON_CLEAR.register(() -> {
if (MinecraftClient.getInstance().world != null) {
EmiReloadManager.reload();
}
});
PolymerClientUtils.ON_SEARCH_REBUILD.register(() -> {
if (MinecraftClient.getInstance().world != null) {
EmiReloadManager.reload();
}
});
PolymerClientUtils.ON_CLEAR.register(EmiCompatibility::maybeReload);
PolymerClientUtils.ON_SEARCH_REBUILD.register(EmiCompatibility::maybeReload);
}
}

private static void maybeReload() {
if (!MinecraftClient.getInstance().isOnThread()) {
MinecraftClient.getInstance().execute(EmiCompatibility::maybeReload);
return;
}

try {
if (MinecraftClient.getInstance().world != null) {
Class.forName(IS_1_0_0 ? "dev.emi.emi.runtime.EmiReloadManager" : "dev.emi.emi.EmiReloadManager").getMethod("reload").invoke(null);
}
} catch (Throwable e) {
e.printStackTrace();
}
}

Expand All @@ -53,7 +69,7 @@ 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);

for (var group : ItemGroup.GROUPS) {
if (group == ItemGroup.SEARCH) {
Expand All @@ -75,7 +91,7 @@ private static void update(EmiRegistry registry) {
}
}

} catch (Exception e) {
} catch (Throwable e) {
e.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import mezz.jei.api.registration.IRecipeRegistration;
import mezz.jei.api.runtime.IIngredientManager;
import mezz.jei.api.runtime.IJeiRuntime;
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -42,6 +43,11 @@ public void onRuntimeAvailable(IJeiRuntime jeiRuntime) {
}

private static void update(IIngredientManager manager) {
if (!MinecraftClient.getInstance().isOnThread()) {
MinecraftClient.getInstance().execute(() -> update(manager));
return;
}

synchronized (manager) {
try {
var list = manager.getAllIngredients(VanillaTypes.ITEM_STACK).stream().filter(SHOULD_REMOVE).toList();
Expand Down

0 comments on commit a151e50

Please sign in to comment.