Skip to content

Commit

Permalink
Close #1775 Add basic Compostable recipe category
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Sep 25, 2021
1 parent 09aafa9 commit 41214d7
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -18,7 +18,7 @@ curse_project_id=238222

# Version
version_major=8
version_minor=0
version_minor=1
version_patch=0

# Workaround for Spotless bug
Expand Down
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.world.item.crafting.SmokingRecipe;
import net.minecraft.world.item.crafting.StonecutterRecipe;
import net.minecraft.world.item.crafting.UpgradeRecipe;
import net.minecraft.world.level.block.ComposterBlock;
import net.minecraftforge.common.brewing.BrewingRecipeRegistry;

import mezz.jei.api.ingredients.IIngredientType;
Expand Down Expand Up @@ -100,6 +101,14 @@ public final class VanillaRecipeCategoryUid {
*/
public static final ResourceLocation SMITHING = new ResourceLocation(ModIds.MINECRAFT_ID, "smithing");

/**
* The sompostable recipe category.
*
* Automatically includes every item added to {@link ComposterBlock#COMPOSTABLES}.
* @since JEI 8.1.0
*/
public static final ResourceLocation COMPOSTABLE = new ResourceLocation(ModIds.MINECRAFT_ID, "compostable");

/**
* The JEI info recipe category shows extra information about ingredients.
*
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java
Expand Up @@ -32,6 +32,8 @@
import mezz.jei.plugins.vanilla.brewing.BrewingRecipeCategory;
import mezz.jei.plugins.vanilla.brewing.BrewingRecipeMaker;
import mezz.jei.plugins.vanilla.brewing.PotionSubtypeInterpreter;
import mezz.jei.plugins.vanilla.compostable.CompostableRecipeCategory;
import mezz.jei.plugins.vanilla.compostable.CompostableRecipeMaker;
import mezz.jei.plugins.vanilla.cooking.BlastingCategory;
import mezz.jei.plugins.vanilla.cooking.CampfireCategory;
import mezz.jei.plugins.vanilla.cooking.FurnaceSmeltingCategory;
Expand Down Expand Up @@ -168,6 +170,7 @@ public void registerCategories(IRecipeCategoryRegistration registration) {
blastingCategory = new BlastingCategory(guiHelper),
campfireCategory = new CampfireCategory(guiHelper),
smithingCategory = new SmithingRecipeCategory(guiHelper),
new CompostableRecipeCategory(guiHelper),
new FurnaceFuelCategory(guiHelper, textures),
new BrewingRecipeCategory(guiHelper),
new AnvilRecipeCategory(guiHelper)
Expand Down Expand Up @@ -206,6 +209,7 @@ public void registerRecipes(IRecipeRegistration registration) {
registration.addRecipes(ShulkerBoxColoringRecipeMaker.createShulkerBoxColoringRecipes(), VanillaRecipeCategoryUid.CRAFTING);
registration.addRecipes(AnvilRecipeMaker.getAnvilRecipes(vanillaRecipeFactory, ingredientManager), VanillaRecipeCategoryUid.ANVIL);
registration.addRecipes(vanillaRecipes.getSmithingRecipes(smithingCategory), VanillaRecipeCategoryUid.SMITHING);
registration.addRecipes(CompostableRecipeMaker.getRecipes(ingredientManager), VanillaRecipeCategoryUid.COMPOSTABLE);
}

@Override
Expand Down Expand Up @@ -255,6 +259,7 @@ public void registerRecipeCatalysts(IRecipeCatalystRegistration registration) {
registration.addRecipeCatalyst(new ItemStack(Blocks.BREWING_STAND), VanillaRecipeCategoryUid.BREWING);
registration.addRecipeCatalyst(new ItemStack(Blocks.ANVIL), VanillaRecipeCategoryUid.ANVIL);
registration.addRecipeCatalyst(new ItemStack(Blocks.SMITHING_TABLE), VanillaRecipeCategoryUid.SMITHING);
registration.addRecipeCatalyst(new ItemStack(Blocks.COMPOSTER), VanillaRecipeCategoryUid.COMPOSTABLE);
}

@Nullable
Expand Down
@@ -0,0 +1,24 @@
package mezz.jei.plugins.vanilla.compostable;

import net.minecraft.world.item.ItemStack;

import java.util.Collections;
import java.util.List;

public class CompostableRecipe {
private final List<List<ItemStack>> inputs;
private final float chance;

public CompostableRecipe(ItemStack input, float chance) {
this.inputs = Collections.singletonList(Collections.singletonList(input));
this.chance = chance;
}

public List<List<ItemStack>> getInputs() {
return inputs;
}

public float getChance() {
return chance;
}
}
@@ -0,0 +1,88 @@
package mezz.jei.plugins.vanilla.compostable;

import com.mojang.blaze3d.vertex.PoseStack;
import mezz.jei.api.constants.VanillaRecipeCategoryUid;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.ingredient.IGuiItemStackGroup;
import mezz.jei.api.helpers.IGuiHelper;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.recipe.category.IRecipeCategory;
import mezz.jei.util.Translator;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Blocks;

public class CompostableRecipeCategory implements IRecipeCategory<CompostableRecipe> {
private static final int inputSlot = 0;

public static final int width = 120;
public static final int height = 18;

private final IDrawable background;
private final IDrawable slot;
private final IDrawable icon;
private final Component localizedName;

public CompostableRecipeCategory(IGuiHelper guiHelper) {
background = guiHelper.createBlankDrawable(width, height);
slot = guiHelper.getSlotDrawable();
icon = guiHelper.createDrawableIngredient(new ItemStack(Blocks.COMPOSTER));
localizedName = new TranslatableComponent("gui.jei.category.compostable");
}

@Override
public ResourceLocation getUid() {
return VanillaRecipeCategoryUid.COMPOSTABLE;
}

@Override
public Class<? extends CompostableRecipe> getRecipeClass() {
return CompostableRecipe.class;
}

@Override
public Component getTitle() {
return localizedName;
}

@Override
public IDrawable getBackground() {
return background;
}

@Override
public IDrawable getIcon() {
return icon;
}

@Override
public void setIngredients(CompostableRecipe recipe, IIngredients ingredients) {
ingredients.setInputLists(VanillaTypes.ITEM, recipe.getInputs());
}

@Override
public void setRecipe(IRecipeLayout recipeLayout, CompostableRecipe recipe, IIngredients ingredients) {
IGuiItemStackGroup guiItemStacks = recipeLayout.getItemStacks();
guiItemStacks.init(inputSlot, true, 0, 0);
guiItemStacks.set(ingredients);
}

@Override
public void draw(CompostableRecipe recipe, PoseStack poseStack, double mouseX, double mouseY) {
slot.draw(poseStack);

float chance = recipe.getChance();
int chancePercent = (int) Math.floor(chance * 100);
String text = Translator.translateToLocalFormatted("gui.jei.category.compostable.chance", chancePercent);

Minecraft minecraft = Minecraft.getInstance();
Font font = minecraft.font;
font.draw(poseStack, text, 24, 5, 0xFF808080);
}
}
@@ -0,0 +1,32 @@
package mezz.jei.plugins.vanilla.compostable;

import it.unimi.dsi.fastutil.objects.Object2FloatMap;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.runtime.IIngredientManager;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.ComposterBlock;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

public class CompostableRecipeMaker {
public static List<CompostableRecipe> getRecipes(IIngredientManager ingredientManager) {
List<CompostableRecipe> recipes = new ArrayList<>();
Object2FloatMap<ItemLike> compostables = ComposterBlock.COMPOSTABLES;
Collection<ItemStack> allItemStacks = ingredientManager.getAllIngredients(VanillaTypes.ITEM);
for (ItemStack itemStack : allItemStacks) {
Item item = itemStack.getItem();
float compostValue = compostables.getOrDefault(item, 0);
if (compostValue > 0) {
CompostableRecipe recipe = new CompostableRecipe(itemStack, compostValue);
recipes.add(recipe);
}
}
recipes.sort(Comparator.comparingDouble(CompostableRecipe::getChance));
return recipes;
}
}
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@FieldsAndMethodsAreNonnullByDefault
package mezz.jei.plugins.vanilla.compostable;

import mezz.jei.util.FieldsAndMethodsAreNonnullByDefault;

import javax.annotation.ParametersAreNonnullByDefault;
2 changes: 2 additions & 0 deletions src/main/resources/assets/jei/lang/en_us.json
Expand Up @@ -127,6 +127,8 @@
"gui.jei.category.fuel.smeltCount": "Smelts %s items",
"gui.jei.category.brewing": "Brewing",
"gui.jei.category.brewing.steps": "Steps: %s",
"gui.jei.category.compostable": "Compostable",
"gui.jei.category.compostable.chance": "Chance: %s%%",
"gui.jei.category.itemInformation": "Information",

"_comment": "Messages",
Expand Down

0 comments on commit 41214d7

Please sign in to comment.