Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions xplat/src/main/java/dev/emi/emi/EmiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.Random;
import java.util.stream.Stream;

import org.jetbrains.annotations.Nullable;

import dev.emi.emi.api.EmiApi;
import dev.emi.emi.api.recipe.EmiPlayerInventory;
import dev.emi.emi.api.recipe.EmiRecipe;
Expand Down Expand Up @@ -136,18 +138,18 @@ public static EmiRecipe getPreferredRecipe(EmiIngredient ingredient, EmiPlayerIn
}

@SuppressWarnings({"unchecked", "rawtypes"})
public static EmiRecipe getPreferredRecipe(List<EmiRecipe> recipes, EmiPlayerInventory inventory, boolean requireCraftable) {
public static @Nullable EmiRecipe getPreferredRecipe(List<EmiRecipe> recipes, EmiPlayerInventory inventory, boolean requireCraftable) {
EmiRecipe preferred = null;
int preferredWeight = -1;
HandledScreen<?> hs = EmiApi.getHandledScreen();
EmiCraftContext context = new EmiCraftContext<>(hs, inventory, EmiCraftContext.Type.CRAFTABLE);
EmiCraftContext context = hs == null ? null : new EmiCraftContext<>(hs, inventory, EmiCraftContext.Type.CRAFTABLE);
for (EmiRecipe recipe : recipes) {
if (!recipe.supportsRecipeTree()) {
continue;
}
int weight = 0;
EmiRecipeHandler handler = EmiRecipeFiller.getFirstValidHandler(recipe, hs);
if (handler != null && handler.canCraft(recipe, context)) {
if (context != null && handler != null && handler.canCraft(recipe, context)) {
weight += 16;
} else if (requireCraftable) {
continue;
Expand Down
23 changes: 10 additions & 13 deletions xplat/src/main/java/dev/emi/emi/api/EmiApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ public static EmiStackInteraction getHoveredStack(boolean includeStandard) {
return null;
}

public static HandledScreen<?> getHandledScreen() {
public static @Nullable HandledScreen<?> getHandledScreen() {
Screen s = client.currentScreen;
if (s instanceof HandledScreen<?> hs) {
return hs;
} else if (s instanceof RecipeScreen rs) {
return rs.old;
} else if (s instanceof BoMScreen bs) {
return bs.old;
} else if (s instanceof RecipeScreen rs && rs.old instanceof HandledScreen<?> hs) {
return hs;
} else if (s instanceof BoMScreen bs && bs.old instanceof HandledScreen<?> hs) {
return hs;
}
return null;
}
Expand All @@ -123,7 +123,7 @@ public static void displayRecipeCategory(EmiRecipeCategory category) {
public static void displayRecipe(EmiRecipe recipe) {
setPages(Map.of(recipe.getCategory(), List.of(recipe)), EmiStack.EMPTY);
}

public static void displayRecipes(EmiIngredient stack) {
if (stack instanceof EmiFavorite fav) {
stack = fav.getStack();
Expand Down Expand Up @@ -239,20 +239,17 @@ private static void setPages(Map<EmiRecipeCategory, List<EmiRecipe>> recipes, Em
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
if (!recipes.isEmpty()) {
EmiSidebars.lookup(stack);
if (getHandledScreen() == null) {
client.setScreen(new InventoryScreen(client.player));
}
if (client.currentScreen instanceof HandledScreen<?> hs) {
push();
client.setScreen(new RecipeScreen(hs, recipes));
} else if (client.currentScreen instanceof BoMScreen bs) {
if (client.currentScreen instanceof BoMScreen bs) {
push();
client.setScreen(new RecipeScreen(bs.old, recipes));
} else if (client.currentScreen instanceof RecipeScreen rs) {
push();
RecipeScreen n = new RecipeScreen(rs.old, recipes);
client.setScreen(n);
n.focusCategory(rs.getFocusedCategory());
} else {
push();
client.setScreen(new RecipeScreen(client.currentScreen, recipes));
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions xplat/src/main/java/dev/emi/emi/registry/EmiRecipeFiller.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;
import java.util.function.BiFunction;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;

import com.google.common.collect.Lists;
Expand Down Expand Up @@ -94,8 +95,10 @@ public static <T extends ScreenHandler> List<EmiRecipeHandler<T>> getAllHandlers
return List.of();
}

@Contract("_, null -> null")
@SuppressWarnings("unchecked")
public static <T extends ScreenHandler> @Nullable EmiRecipeHandler<T> getFirstValidHandler(EmiRecipe recipe, HandledScreen<T> screen) {
public static <T extends ScreenHandler> @Nullable EmiRecipeHandler<T> getFirstValidHandler(EmiRecipe recipe, @Nullable HandledScreen<T> screen) {
if (screen == null) return null;
EmiRecipeHandler<T> ret = null;
for (EmiRecipeHandler<T> handler : getAllHandlers(screen)) {
if (handler.supportsRecipe(recipe)) {
Expand All @@ -112,7 +115,8 @@ public static <T extends ScreenHandler> List<EmiRecipeHandler<T>> getAllHandlers
return ret;
}

public static <T extends ScreenHandler> boolean performFill(EmiRecipe recipe, HandledScreen<T> screen,
@Contract("_,null,_,_,_->false")
public static <T extends ScreenHandler> boolean performFill(EmiRecipe recipe, @Nullable HandledScreen<T> screen,
EmiCraftContext.Type type, EmiCraftContext.Destination destination, int amount) {
EmiRecipeHandler<T> handler = getFirstValidHandler(recipe, screen);
if (handler != null && handler.supportsRecipe(recipe)) {
Expand Down
5 changes: 2 additions & 3 deletions xplat/src/main/java/dev/emi/emi/screen/BoMScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.util.InputUtil;
Expand All @@ -73,13 +72,13 @@ public class BoMScreen extends Screen {
private List<Cost> costs = Lists.newArrayList();
private EmiPlayerInventory playerInv;
private boolean hasRemainders = false;;
public HandledScreen<?> old;
public Screen old;
private int nodeWidth = 0;
private int nodeHeight = 0;
private int lastMouseX, lastMouseY;
private double scrollAcc = 0;

public BoMScreen(HandledScreen<?> old) {
public BoMScreen(Screen old) {
super(EmiPort.translatable("screen.emi.recipe_tree"));
this.old = old;
}
Expand Down
4 changes: 2 additions & 2 deletions xplat/src/main/java/dev/emi/emi/screen/RecipeScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class RecipeScreen extends Screen {
private static final Identifier TEXTURE = new Identifier("emi", "textures/gui/background.png");
public static @Nullable EmiIngredient resolve = null;
private Map<EmiRecipeCategory, List<EmiRecipe>> recipes;
public HandledScreen<?> old;
public Screen old;
private List<RecipeTab> tabs = Lists.newArrayList();
private int tabPageSize = 6;
private int tabPage = 0, tab = 0, page = 0;
Expand All @@ -62,7 +62,7 @@ public class RecipeScreen extends Screen {
int x = (this.width - backgroundWidth) / 2;
int y = (this.height - backgroundHeight) / 2;

public RecipeScreen(HandledScreen<?> old, Map<EmiRecipeCategory, List<EmiRecipe>> recipes) {
public RecipeScreen(Screen old, Map<EmiRecipeCategory, List<EmiRecipe>> recipes) {
super(EmiPort.translatable("screen.emi.recipe"));
this.old = old;
arrows = List.of(
Expand Down