Skip to content

Commit

Permalink
Use JEI scroll widget
Browse files Browse the repository at this point in the history
  • Loading branch information
SihenZhang committed Aug 17, 2024
1 parent 0c4b75c commit 224551c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod_description=Bring Crock Pot from Don't Starve to Minecraft.

## Dependency Properties

jei_version=15.2.0.22
jei_version=15.11.0.43
top_version=10.0.1-3
curios_version=5.2.0-beta.3
architectury_file=5137938
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import com.sihenzhang.crockpot.CrockPot;
import com.sihenzhang.crockpot.base.FoodCategory;
import com.sihenzhang.crockpot.util.I18nUtils;
import com.sihenzhang.crockpot.util.RLUtils;
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.drawable.IDrawableStatic;
import mezz.jei.api.gui.widgets.IScrollGridWidgetFactory;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.recipe.IFocusGroup;
import mezz.jei.api.recipe.RecipeIngredientRole;
Expand All @@ -14,17 +15,25 @@
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;

import java.util.List;
import java.util.Set;

public class FoodValuesCategory implements IRecipeCategory<FoodValuesCategory.FoodCategoryMatchedItems> {
public static final RecipeType<FoodValuesCategory.FoodCategoryMatchedItems> RECIPE_TYPE = RecipeType.create(CrockPot.MOD_ID, "food_values", FoodValuesCategory.FoodCategoryMatchedItems.class);
private final int WIDTH = 178;
private final int HEIGHT = 110;

private final IDrawable background;
private final IDrawable icon;
private final IDrawableStatic slotDrawable;
private final IScrollGridWidgetFactory<?> scrollGridFactory;

public FoodValuesCategory(IGuiHelper guiHelper) {
this.background = guiHelper.createDrawable(RLUtils.createRL("textures/gui/jei/food_values.png"), 0, 0, 166, 117);
this.background = guiHelper.createBlankDrawable(WIDTH, HEIGHT);
this.icon = guiHelper.createDrawable(ModIntegrationJei.ICONS, 16, 0, 16, 16);
this.slotDrawable = guiHelper.getSlotDrawable();
var scrollGridFactory = guiHelper.createScrollGridFactory(9, 5);
scrollGridFactory.setPosition((WIDTH - scrollGridFactory.getArea().width()) / 2, 20);
this.scrollGridFactory = scrollGridFactory;
}

@Override
Expand All @@ -49,11 +58,10 @@ public IDrawable getIcon() {

@Override
public void setRecipe(IRecipeLayoutBuilder builder, FoodValuesCategory.FoodCategoryMatchedItems recipe, IFocusGroup focuses) {
var pagedItemStacks = JeiUtils.getPagedItemStacks(List.copyOf(recipe.items()), focuses, RecipeIngredientRole.INPUT, 45);
for (var i = 0; i < pagedItemStacks.size(); i++) {
builder.addSlot(RecipeIngredientRole.INPUT, 3 + i % 9 * 18, 26 + i / 9 * 18).addItemStacks(pagedItemStacks.get(i));
}
builder.addSlot(RecipeIngredientRole.OUTPUT, 75, 3).addItemStack(FoodCategory.getItemStack(recipe.category()));
recipe.items().forEach(stack -> builder.addSlotToWidget(RecipeIngredientRole.INPUT, scrollGridFactory).addItemStack(stack));
builder.addSlot(RecipeIngredientRole.OUTPUT, (WIDTH - slotDrawable.getWidth()) / 2 + 1, 1)
.addItemStack(FoodCategory.getItemStack(recipe.category()))
.setBackground(slotDrawable, -1, -1);
}

public record FoodCategoryMatchedItems(FoodCategory category, Set<ItemStack> items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Map;
import java.util.Set;

public class FoodValuesDefinitionCache {
public final class FoodValuesDefinitionCache {
private static Map<FoodCategory, Set<ItemStack>> CACHE;

private FoodValuesDefinitionCache() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.ingredient.IRecipeSlotsView;
import mezz.jei.api.gui.widgets.IScrollGridWidgetFactory;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.recipe.IFocusGroup;
import mezz.jei.api.recipe.RecipeIngredientRole;
Expand All @@ -28,10 +29,14 @@ public class PiglinBarteringRecipeCategory implements IRecipeCategory<PiglinBart
public static final RecipeType<PiglinBarteringRecipe> RECIPE_TYPE = RecipeType.create(CrockPot.MOD_ID, "piglin_bartering", PiglinBarteringRecipe.class);
private final IDrawable background;
private final IDrawable icon;
private final IScrollGridWidgetFactory<?> scrollGridFactory;

public PiglinBarteringRecipeCategory(IGuiHelper guiHelper) {
this.background = guiHelper.createDrawable(RLUtils.createRL("textures/gui/jei/piglin_bartering.png"), 0, 0, 176, 112);
this.background = guiHelper.createDrawable(RLUtils.createRL("textures/gui/jei/piglin_bartering.png"), 0, 0, 177, 108);
this.icon = guiHelper.createDrawable(ModIntegrationJei.ICONS, 32, 0, 16, 16);
var scrollGridFactory = guiHelper.createScrollGridFactory(5, 6);
scrollGridFactory.setPosition(70, 0);
this.scrollGridFactory = scrollGridFactory;
}

@Override
Expand All @@ -56,14 +61,10 @@ public IDrawable getIcon() {

@Override
public void setRecipe(IRecipeLayoutBuilder builder, PiglinBarteringRecipe recipe, IFocusGroup focuses) {
builder.addSlot(RecipeIngredientRole.INPUT, 29, 3).setSlotName("inputSlot").addIngredients(recipe.getIngredient());
var weightedOutput = recipe.getWeightedResults().unwrap().stream()
builder.addSlot(RecipeIngredientRole.INPUT, 25, 1).setSlotName("inputSlot").addIngredients(recipe.getIngredient());
recipe.getWeightedResults().unwrap().stream()
.map(e -> NbtUtils.setLoreString(e.getData().item.getDefaultInstance(), StringUtils.formatCountAndChance(e, recipe.getWeightedResults().totalWeight)))
.toList();
var pagedItemStacks = JeiUtils.getPagedItemStacks(weightedOutput, focuses, RecipeIngredientRole.OUTPUT, 30);
for (var i = 0; i < pagedItemStacks.size(); i++) {
builder.addSlot(RecipeIngredientRole.OUTPUT, 85 + i % 5 * 18, 3 + i / 5 * 18).addItemStacks(pagedItemStacks.get(i));
}
.forEach(stack -> builder.addSlotToWidget(RecipeIngredientRole.OUTPUT, scrollGridFactory).addItemStack(stack));
}

@Override
Expand All @@ -90,7 +91,7 @@ public void draw(PiglinBarteringRecipe recipe, IRecipeSlotsView recipeSlotsView,
}
var stack = guiGraphics.pose();
stack.pushPose();
stack.translate(emptyInOffhand ? 29.0 : 37.0, 103.0, 50.0);
stack.translate(emptyInOffhand ? 25.0 : 33.0, 103.0, 50.0);
stack.scale(-32.0F, 32.0F, 32.0F);
stack.mulPose(Axis.ZP.rotationDegrees(180.0F));
if (!emptyInOffhand) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ description='''${mod_description}'''
versionRange="${minecraft_version_range}"
ordering="NONE"
side="BOTH"
[[dependencies.${mod_id}]]
modId="jei"
mandatory=false
versionRange="[15.11.0.43,)"
ordering="NONE"
side="BOTH"
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 224551c

Please sign in to comment.