Skip to content

Commit

Permalink
fix: loading order issues on fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Jun 20, 2024
1 parent b30f9cf commit f87e91d
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 69 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Potential loading order issues on Fabric.

## [0.2.1] - 2024-06-16

### Fixed
Expand Down
1 change: 1 addition & 0 deletions config/checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
<suppressions>
<!-- Test directories don't need a package-info.java file -->
<suppress checks="JavadocPackage" files="test[\\/].*.java"/>
<suppress checks="HideUtilityClassConstructor" files="ModInitializer.java"/>
</suppressions>
1 change: 1 addition & 0 deletions refinedstorage-jei-integration-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ commonProject()
dependencies {
api "com.refinedmods.refinedstorage2:refinedstorage2-platform-common:${refinedstorageVersion}"
api "mezz.jei:jei-${minecraftVersion}-common-api:${jeiVersion}"
api "mezz.jei:jei-${minecraftVersion}-common:${jeiVersion}"
}

enablePublishing()
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.refinedmods.refinedstorage.jei.common;

import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import net.minecraft.resources.ResourceLocation;

import static java.util.Objects.requireNonNull;

public final class Common {
Expand All @@ -17,4 +20,16 @@ public static void setPlatform(final Platform platform) {
public static Platform getPlatform() {
return requireNonNull(platform, "Platform isn't set yet");
}

public static void init(final PlatformApi platformApi) {
platformApi.addIngredientConverter(new JeiRecipeModIngredientConverter());
platformApi.getGridSynchronizerRegistry().register(
new ResourceLocation(Common.MOD_ID, "jei"),
new JeiGridSynchronizer(false)
);
platformApi.getGridSynchronizerRegistry().register(
new ResourceLocation(Common.MOD_ID, "jei_two_way"),
new JeiGridSynchronizer(true)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.Collections;
import java.util.List;

import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.support.resource.PlatformResourceKey;
import com.refinedmods.refinedstorage2.platform.api.support.resource.RecipeModIngredientConverter;
import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractBaseScreen;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.AbstractResourceContainerMenu;
Expand All @@ -16,12 +16,6 @@

@SuppressWarnings("rawtypes")
class GhostIngredientHandler implements IGhostIngredientHandler<AbstractBaseScreen> {
private final RecipeModIngredientConverter ingredientConverter;

GhostIngredientHandler(final RecipeModIngredientConverter ingredientConverter) {
this.ingredientConverter = ingredientConverter;
}

@Override
public <I> List<Target<I>> getTargetsTyped(final AbstractBaseScreen screen,
final ITypedIngredient<I> ingredient,
Expand All @@ -36,7 +30,7 @@ private <I> List<Target<I>> getTargets(final AbstractBaseScreen screen,
final I ingredient,
final AbstractResourceContainerMenu menu) {
final List<Target<I>> targets = new ArrayList<>();
ingredientConverter.convertToResource(ingredient).ifPresent(resource -> {
PlatformApi.INSTANCE.getIngredientConverter().convertToResource(ingredient).ifPresent(resource -> {
for (final ResourceSlot slot : menu.getResourceSlots()) {
if (slot.isFilter() && slot.isValid(resource)) {
final Rect2i bounds = getBounds(screen, slot);
Expand Down Expand Up @@ -72,7 +66,7 @@ public Rect2i getArea() {

@Override
public void accept(final I ingredient) {
ingredientConverter.convertToResource(ingredient).ifPresent(this::accept);
PlatformApi.INSTANCE.getIngredientConverter().convertToResource(ingredient).ifPresent(this::accept);
}

private void accept(final PlatformResourceKey resource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import java.util.Optional;

import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.grid.view.PlatformGridResource;
import com.refinedmods.refinedstorage2.platform.api.support.resource.PlatformResourceKey;
import com.refinedmods.refinedstorage2.platform.api.support.resource.RecipeModIngredientConverter;
import com.refinedmods.refinedstorage2.platform.common.grid.screen.AbstractGridScreen;
import mezz.jei.api.gui.handlers.IGuiContainerHandler;
import mezz.jei.api.ingredients.IIngredientHelper;
Expand All @@ -13,11 +13,9 @@
import mezz.jei.api.runtime.IIngredientManager;

class GridGuiContainerHandler implements IGuiContainerHandler<AbstractGridScreen<?>> {
private final RecipeModIngredientConverter converter;
private final IIngredientManager ingredientManager;

GridGuiContainerHandler(final RecipeModIngredientConverter converter, final IIngredientManager ingredientManager) {
this.converter = converter;
GridGuiContainerHandler(final IIngredientManager ingredientManager) {
this.ingredientManager = ingredientManager;
}

Expand All @@ -35,7 +33,7 @@ public Optional<IClickableIngredient<?>> getClickableIngredientUnderMouse(
if (underlyingResource == null) {
return Optional.empty();
}
return converter.convertToIngredient(underlyingResource).flatMap(
return PlatformApi.INSTANCE.getIngredientConverter().convertToIngredient(underlyingResource).flatMap(
ingredient -> convertToClickableIngredient(mouseX, mouseY, ingredient)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ class JeiGridSynchronizer extends AbstractGridSynchronizer {
"gui.%s.grid.synchronizer.two_way.help".formatted(MOD_ID)
);

private final JeiProxy jeiProxy;
private final boolean twoWay;

JeiGridSynchronizer(final JeiProxy jeiProxy, final boolean twoWay) {
this.jeiProxy = jeiProxy;
JeiGridSynchronizer(final boolean twoWay) {
this.twoWay = twoWay;
}

Expand All @@ -38,13 +36,13 @@ public Component getHelpText() {

@Override
public void synchronizeFromGrid(final String text) {
jeiProxy.setSearchFieldText(text);
JeiHelper.setSearchFieldText(text);
}

@Override
@Nullable
public String getTextToSynchronizeToGrid() {
return twoWay ? jeiProxy.getSearchFieldText() : null;
return twoWay ? JeiHelper.getSearchFieldText() : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import mezz.jei.api.runtime.IJeiRuntime;

public class JeiProxy {
public String getSearchFieldText() {
public final class JeiHelper {
private JeiHelper() {
}

public static String getSearchFieldText() {
final IJeiRuntime runtime = RefinedStorageModPlugin.getRuntime();
if (runtime == null) {
return "";
}
return runtime.getIngredientFilter().getFilterText();
}

public void setSearchFieldText(final String text) {
public static void setSearchFieldText(final String text) {
final IJeiRuntime runtime = RefinedStorageModPlugin.getRuntime();
if (runtime != null) {
runtime.getIngredientFilter().setFilterText(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@
import com.refinedmods.refinedstorage2.platform.common.support.resource.FluidResource;
import com.refinedmods.refinedstorage2.platform.common.support.resource.ItemResource;
import mezz.jei.api.helpers.IPlatformFluidHelper;
import mezz.jei.common.platform.Services;
import net.minecraft.world.item.ItemStack;

class JeiRecipeModIngredientConverter implements RecipeModIngredientConverter {
private final IPlatformFluidHelper<?> fluidHelper;

JeiRecipeModIngredientConverter(final IPlatformFluidHelper<?> fluidHelper) {
this.fluidHelper = fluidHelper;
}

@Override
public Optional<PlatformResourceKey> convertToResource(final Object ingredient) {
final var fluid = Common.getPlatform().convertJeiIngredientToFluid(ingredient);
Expand All @@ -34,6 +29,7 @@ public Optional<Object> convertToIngredient(final PlatformResourceKey resource)
return Optional.of(itemResource.toItemStack());
}
if (resource instanceof FluidResource fluidResource) {
final IPlatformFluidHelper<?> fluidHelper = Services.PLATFORM.getFluidHelper();
return Optional.of(fluidHelper.create(
fluidResource.fluid(),
fluidHelper.bucketVolume(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@

import javax.annotation.Nullable;

import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.support.resource.RecipeModIngredientConverter;
import com.refinedmods.refinedstorage2.platform.common.grid.screen.AbstractGridScreen;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractBaseScreen;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.JeiPlugin;
import mezz.jei.api.constants.RecipeTypes;
import mezz.jei.api.helpers.IPlatformFluidHelper;
import mezz.jei.api.registration.IGuiHandlerRegistration;
import mezz.jei.api.registration.IRecipeTransferRegistration;
import mezz.jei.api.runtime.IJeiRuntime;
import net.minecraft.resources.ResourceLocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@JeiPlugin
public class RefinedStorageModPlugin implements IModPlugin {
private static final ResourceLocation ID = new ResourceLocation(Common.MOD_ID, "plugin");
private static final Logger LOGGER = LoggerFactory.getLogger(RefinedStorageModPlugin.class);
@Nullable
private static IJeiRuntime runtime;

Expand All @@ -36,54 +30,33 @@ public void registerRecipeTransferHandlers(final IRecipeTransferRegistration reg

@Override
public void onRuntimeAvailable(final IJeiRuntime newRuntime) {
if (runtime == null) {
initializePlatform(newRuntime.getJeiHelpers().getPlatformFluidHelper());
}
RefinedStorageModPlugin.runtime = newRuntime;
}

@Override
public void onRuntimeUnavailable() {
RefinedStorageModPlugin.runtime = null;
}

@Override
public void registerGuiHandlers(final IGuiHandlerRegistration registration) {
final RecipeModIngredientConverter converter = PlatformApi.INSTANCE.getIngredientConverter();
registration.addGenericGuiContainerHandler(
AbstractBaseScreen.class,
new ResourceGuiContainerHandler(converter, registration.getJeiHelpers().getIngredientManager())
new ResourceGuiContainerHandler(registration.getJeiHelpers().getIngredientManager())
);
registration.addGenericGuiContainerHandler(
AbstractGridScreen.class,
new GridGuiContainerHandler(converter, registration.getJeiHelpers().getIngredientManager())
new GridGuiContainerHandler(registration.getJeiHelpers().getIngredientManager())
);
registration.addGenericGuiContainerHandler(
AbstractBaseScreen.class,
new ExclusionZonesGuiContainerHandler()
);
registration.addGhostIngredientHandler(AbstractBaseScreen.class, new GhostIngredientHandler(converter));
registration.addGhostIngredientHandler(AbstractBaseScreen.class, new GhostIngredientHandler());
}

@Nullable
public static IJeiRuntime getRuntime() {
return runtime;
}

private void initializePlatform(final IPlatformFluidHelper<?> fluidHelper) {
LOGGER.debug("Enabling JEI integration");
registerIngredientConverters(fluidHelper);
registerGridSynchronizers();
}

private void registerGridSynchronizers() {
final JeiProxy jeiProxy = new JeiProxy();
PlatformApi.INSTANCE.getGridSynchronizerRegistry().register(
new ResourceLocation(Common.MOD_ID, "jei"),
new JeiGridSynchronizer(jeiProxy, false)
);
PlatformApi.INSTANCE.getGridSynchronizerRegistry().register(
new ResourceLocation(Common.MOD_ID, "jei_two_way"),
new JeiGridSynchronizer(jeiProxy, true)
);
}

private void registerIngredientConverters(final IPlatformFluidHelper<?> fluidHelper) {
PlatformApi.INSTANCE.addIngredientConverter(new JeiRecipeModIngredientConverter(fluidHelper));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.util.Optional;
import javax.annotation.Nullable;

import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.support.resource.PlatformResourceKey;
import com.refinedmods.refinedstorage2.platform.api.support.resource.RecipeModIngredientConverter;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractBaseScreen;
import mezz.jei.api.gui.handlers.IGuiContainerHandler;
import mezz.jei.api.ingredients.IIngredientHelper;
Expand All @@ -13,12 +13,9 @@
import mezz.jei.api.runtime.IIngredientManager;

class ResourceGuiContainerHandler implements IGuiContainerHandler<AbstractBaseScreen<?>> {
private final RecipeModIngredientConverter converter;
private final IIngredientManager ingredientManager;

ResourceGuiContainerHandler(final RecipeModIngredientConverter converter,
final IIngredientManager ingredientManager) {
this.converter = converter;
ResourceGuiContainerHandler(final IIngredientManager ingredientManager) {
this.ingredientManager = ingredientManager;
}

Expand All @@ -35,7 +32,7 @@ public Optional<Object> convertToIngredient(@Nullable final PlatformResourceKey
if (resource == null) {
return Optional.empty();
}
return converter.convertToIngredient(resource);
return PlatformApi.INSTANCE.getIngredientConverter().convertToIngredient(resource);
}

private Optional<IClickableIngredient<?>> convertToClickableIngredient(final Object ingredient) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.refinedmods.refinedstorage.jei.fabric;

import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.RefinedStoragePlugin;

import static com.refinedmods.refinedstorage.jei.common.Common.init;

public class JeiRefinedStoragePlugin implements RefinedStoragePlugin {
@Override
public void onPlatformApiAvailable(final PlatformApi platformApi) {
init(platformApi);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
],
"jei_mod_plugin": [
"com.refinedmods.refinedstorage.jei.common.RefinedStorageModPlugin"
],
"refinedstorage_plugin": [
"com.refinedmods.refinedstorage.jei.fabric.JeiRefinedStoragePlugin"
]
},
"custom": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@

import com.refinedmods.refinedstorage.jei.common.Common;

import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;

import static com.refinedmods.refinedstorage.jei.common.Common.init;

@Mod(Common.MOD_ID)
public class ModInitializer {
public ModInitializer(final IEventBus eventBus) {
Common.setPlatform(new ForgePlatform());
eventBus.addListener(ModInitializer::onCommonSetup);
}

@SubscribeEvent
public static void onCommonSetup(final FMLCommonSetupEvent e) {
init(PlatformApi.INSTANCE);
}
}

0 comments on commit f87e91d

Please sign in to comment.