Skip to content

Commit

Permalink
Adds florist to JEI (#10236)
Browse files Browse the repository at this point in the history
Adds florist to JEI.
  • Loading branch information
uecasm authored Nov 3, 2024
1 parent f1fd051 commit 7a43e49
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package com.minecolonies.core.compatibility.jei;

import com.minecolonies.api.blocks.ModBlocks;
import com.minecolonies.api.colony.buildings.ModBuildings;
import com.minecolonies.api.colony.jobs.ModJobs;
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.items.ModItems;
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingFlorist;
import mezz.jei.api.gui.builder.IRecipeLayoutBuilder;
import mezz.jei.api.gui.ingredient.IRecipeSlotsView;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.recipe.IFocusGroup;
import mezz.jei.api.recipe.RecipeIngredientRole;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import static com.minecolonies.api.util.constant.Constants.MAX_BUILDING_LEVEL;
import static com.minecolonies.api.util.constant.TranslationConstants.PARTIAL_JEI_INFO;

/**
* JEI recipe category showing supported flowers.
*/
public class FloristRecipeCategory extends JobBasedRecipeCategory<FloristRecipeCategory.FloristRecipe>
{
/**
* Constructor
*/
public FloristRecipeCategory(@NotNull final IGuiHelper guiHelper)
{
super(ModJobs.florist.get().produceJob(null), ModRecipeTypes.FLOWERS,
new ItemStack(ModBuildings.florist.get().getBuildingBlock()), guiHelper);
}

private static final int LOOT_SLOTS_X = CITIZEN_X + CITIZEN_W + 4;
private static final int LOOT_SLOTS_W = WIDTH - LOOT_SLOTS_X;
private static final int MAX_LOOT_SLOTS = 24;

@NotNull
@Override
protected List<Component> generateInfoBlocks(@NotNull final FloristRecipeCategory.FloristRecipe recipe)
{
return Collections.singletonList(
Component.translatable(PARTIAL_JEI_INFO + "onelevelrestriction",
recipe.level()));
}

@Override
public void setRecipe(@NotNull final IRecipeLayoutBuilder builder,
@NotNull final FloristRecipeCategory.FloristRecipe recipe,
@NotNull final IFocusGroup focuses)
{
builder.addSlot(RecipeIngredientRole.CATALYST, WIDTH - 18, CITIZEN_Y - 20)
.setSlotName("compost")
.setBackground(this.slot, -1, -1)
.addItemStack(new ItemStack(ModItems.compost));

final int initialColumns = LOOT_SLOTS_W / this.slot.getWidth();
final int rows = (recipe.flowers().size() + initialColumns - 1) / initialColumns;
final int columns = (recipe.flowers().size() + rows - 1) / rows;
final int startX = LOOT_SLOTS_X + (LOOT_SLOTS_W - (columns * this.slot.getWidth())) / 2;
int x = startX;
int y = CITIZEN_Y + CITIZEN_H - rows * this.slot.getHeight() + 1;
int c = 0;

for (final List<ItemStack> flowers : recipe.flowers())
{
builder.addSlot(RecipeIngredientRole.OUTPUT, x, y)
.setBackground(this.chanceSlot, -1, -1)
.addItemStacks(flowers);
if (++c >= columns)
{
c = 0;
x = startX;
y += this.slot.getHeight();
}
else
{
x += this.slot.getWidth();
}
}
}

@Override
public void draw(@NotNull final FloristRecipeCategory.FloristRecipe recipe,
@NotNull final IRecipeSlotsView recipeSlotsView,
@NotNull final GuiGraphics stack,
final double mouseX, final double mouseY)
{
super.draw(recipe, recipeSlotsView, stack, mouseX, mouseY);

final BlockState block = ModBlocks.blockCompostedDirt.defaultBlockState();
RenderHelper.renderBlock(stack.pose(), block, WIDTH - 38, CITIZEN_Y - 20, 100, -30F, 30F, 16F);
}

@NotNull
public static List<FloristRecipeCategory.FloristRecipe> findRecipes()
{
final List<FloristRecipeCategory.FloristRecipe> recipes = new ArrayList<>();

for (int level = 1; level <= MAX_BUILDING_LEVEL; ++level)
{
recipes.add(new FloristRecipe(level, compactify(BuildingFlorist.getPlantablesForBuildingLevel(level))));
}

return recipes;
}

/**
* Produces no more than MAX_LOOT_SLOTS lists of lists of items, to avoid overflowing the GUI with slots.
*/
private static List<List<ItemStack>> compactify(@NotNull final Set<ItemStorage> flowers)
{
final List<List<ItemStack>> slots = new ArrayList<>();
final List<ItemStack> flowerList = flowers.stream().map(ItemStorage::getItemStack).toList();

if (flowerList.size() < MAX_LOOT_SLOTS)
{
for (final ItemStack item : flowerList)
{
slots.add(List.of(item));
}
}
else
{
int itemsPerList = flowerList.size() / MAX_LOOT_SLOTS;
int extraItems = flowerList.size() % MAX_LOOT_SLOTS;
int currentIndex = 0;

for (int i = 0; i < MAX_LOOT_SLOTS; ++i)
{
final List<ItemStack> sublist = new ArrayList<>();
for (int j = 0; j < itemsPerList; ++j)
{
sublist.add(flowerList.get(currentIndex++));
}
if (extraItems > 0)
{
sublist.add(flowerList.get(currentIndex++));
--extraItems;
}
slots.add(sublist);
}
}

return slots;
}

/**
* Represents the flowers available at the specified level.
* @param level the building level.
* @param flowers the flowers available at that level, grouped into display slots.
*/
public record FloristRecipe(int level, @NotNull List<List<ItemStack>> flowers)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void registerCategories(@NotNull final IRecipeCategoryRegistration regist
registration.addRecipeCategories(new CropRecipeCategory(guiHelper));
registration.addRecipeCategories(new CompostRecipeCategory(guiHelper));
registration.addRecipeCategories(new FishermanRecipeCategory(guiHelper));
registration.addRecipeCategories(new FloristRecipeCategory(guiHelper));

categories.clear();
for (final BuildingEntry building : IMinecoloniesAPI.getInstance().getBuildingRegistry())
Expand Down Expand Up @@ -129,6 +130,7 @@ public void registerRecipes(@NotNull final IRecipeRegistration registration)
registration.addRecipes(ModRecipeTypes.CROPS, CropRecipeCategory.findRecipes());
registration.addRecipes(ModRecipeTypes.COMPOSTING, CompostRecipeCategory.findRecipes());
registration.addRecipes(ModRecipeTypes.FISHING, FishermanRecipeCategory.findRecipes());
registration.addRecipes(ModRecipeTypes.FLOWERS, FloristRecipeCategory.findRecipes());

final ClientLevel level = Objects.requireNonNull(Minecraft.getInstance().level);
final Map<CraftingType, List<IGenericRecipe>> vanilla = RecipeAnalyzer.buildVanillaRecipesMap(level.getRecipeManager(), level);
Expand Down Expand Up @@ -162,6 +164,7 @@ public void registerRecipeCatalysts(@NotNull final IRecipeCatalystRegistration r
registration.addRecipeCatalyst(new ItemStack(ModBlocks.blockBarrel), ModRecipeTypes.COMPOSTING);
registration.addRecipeCatalyst(new ItemStack(ModBlocks.blockHutComposter), ModRecipeTypes.COMPOSTING);
registration.addRecipeCatalyst(new ItemStack(ModBlocks.blockHutFisherman), ModRecipeTypes.FISHING);
registration.addRecipeCatalyst(new ItemStack(ModBlocks.blockHutFlorist), ModRecipeTypes.FLOWERS);

for (final JobBasedRecipeCategory<?> category : this.categories)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class ModRecipeTypes
public static final RecipeType<ToolUsage> TOOLS =
RecipeType.create(MOD_ID, "tools", ToolUsage.class);

public static final RecipeType<FloristRecipeCategory.FloristRecipe> FLOWERS =
RecipeType.create(MOD_ID, "flowers", FloristRecipeCategory.FloristRecipe.class);

private ModRecipeTypes()
{
// purely static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,7 @@
"com.minecolonies.coremod.jei.farmer": "In addition to tending fields, the farmer can craft things with hay, seeds, or dirt.",
"com.minecolonies.coremod.jei.fisherman": "Catches fish and other items as usual, plus some bonus items at higher levels.",
"com.minecolonies.coremod.jei.fletcher": "Crafts arrows and leather armor. Also plain items made from wool and string.",
"com.minecolonies.coremod.jei.florist": "Grows flowers from compost and composted dirt.",
"com.minecolonies.coremod.jei.glassblower": "Smelts glass itself, plus crafts things made from glass as well.",
"com.minecolonies.coremod.jei.lumberjack": "While they mostly just chop trees, they will also strip and modify logs if required.",
"com.minecolonies.coremod.jei.mechanic": "Crafts mechanisms or other things based on redstone, glowstone, pearls, blaze, etc.",
Expand Down

0 comments on commit 7a43e49

Please sign in to comment.