Skip to content

Commit

Permalink
Merge pull request #19 from Ramen5914/18-add-full-recipe-support-to-a…
Browse files Browse the repository at this point in the history
…dvanced-grindstone-in-emi

Finalized EMI integration for advanced grindstone
  • Loading branch information
Ramen5914 authored Sep 15, 2024
2 parents 6520f40 + c47f786 commit 472b07a
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 32 deletions.
Binary file added Blender/Renders/markdownHiRes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Blender/modLogo.blend
Binary file not shown.
Binary file modified Blender/modLogo.blend1
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ loader_version_range=[4,)
mod_id=ramensadditions
mod_name=Ramen's Additions
mod_license=GNU GPLv3
mod_version=0.1.2
mod_version=0.1.3
mod_group_id=net.ramen5914.ramensadditions
mod_authors=Omar Rahman (Ramen5914)
mod_description=A collection of small additions to Minecraft that I wish were in the game.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"values": [
"minecraft:netherite_ingot"
"minecraft:netherite_ingot",
"minecraft:nether_star"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public ModItemTagProvider(PackOutput output, CompletableFuture<HolderLookup.Prov
@Override
protected void addTags(HolderLookup.Provider provider) {
tag(ModTags.Items.DISENCHANTING_CATALYSTS)
.add(Items.NETHERITE_INGOT);
.add(Items.NETHERITE_INGOT)
.add(Items.NETHER_STAR);
}
}
52 changes: 38 additions & 14 deletions src/main/java/net/ramen5914/ramensadditions/emi/ModEmiPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import net.minecraft.client.Minecraft;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.enchantment.Enchantment;
import net.ramen5914.ramensadditions.RamensAdditions;
import net.ramen5914.ramensadditions.block.ModBlocks;
import net.ramen5914.ramensadditions.emi.recipes.AdvancedGrindingRecipe;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.function.Consumer;

@EmiEntrypoint
public class ModEmiPlugin implements EmiPlugin {
Expand Down Expand Up @@ -58,20 +65,37 @@ public void register(EmiRegistry registry) {
)
);

registry.addRecipe(new AdvancedGrindingRecipe());
for (Item i : BuiltInRegistries.ITEM) {
try {
ItemStack defaultStack = i.getDefaultInstance();
int acceptableEnchantments = 0;
for (Enchantment e : targetedEnchantments) {
if (defaultStack.supportsEnchantment(Holder.direct(e))) {
acceptableEnchantments++;
}
}
if (acceptableEnchantments > 0) {
for (Enchantment e : universalEnchantments) {
if (defaultStack.supportsEnchantment(Holder.direct(e))) {
acceptableEnchantments++;
}
}
String itemNamespace = BuiltInRegistries.ITEM.getKey(i).getNamespace();
String itemPath = BuiltInRegistries.ITEM.getKey(i).getPath();
String id = String.format("/advanced_grindstone/disenchanting/%s/%s", itemNamespace, itemPath);

// for (Item i : BuiltInRegistries.ITEM) {
// try {
// if (i.components().getOrDefault(DataComponents.MAX_DAMAGE, 0) > 0) {
// RamensAdditions.LOGGER.info(i.toString());
// String format = String.format("/%s/%s/%s", "advanced_grindstone/disenchanting", BuiltInRegistries.ITEM.getKey(i).getNamespace(), BuiltInRegistries.ITEM.getKey(i).getPath());
// RamensAdditions.LOGGER.info(format);
// registry.addRecipe(new AdvancedGrindingRecipe(i, ResourceLocation.fromNamespaceAndPath(RamensAdditions.MOD_ID, format)));
// }
// } catch (Throwable t) {
// RamensAdditions.LOGGER.error("Exception thrown registering repair recipes");
// t.printStackTrace();
// }
// }
registry.addRecipe(new AdvancedGrindingRecipe(i, ResourceLocation.fromNamespaceAndPath(RamensAdditions.MOD_ID, id)));
}
} catch (Throwable t) {
RamensAdditions.LOGGER.warn("Exception thrown registering enchantment recipes");
t.printStackTrace();
StringWriter writer = new StringWriter();
t.printStackTrace(new PrintWriter(writer, true));
String[] strings = writer.getBuffer().toString().split("/");
for (String s : strings) {
RamensAdditions.LOGGER.warn(s);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
package net.ramen5914.ramensadditions.emi.recipes;

import com.google.common.collect.Lists;
import dev.emi.emi.api.recipe.EmiRecipe;
import dev.emi.emi.api.recipe.EmiRecipeCategory;
import dev.emi.emi.api.recipe.VanillaEmiRecipeCategories;
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import dev.emi.emi.api.widget.WidgetHolder;
import net.minecraft.client.Minecraft;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.EnchantmentTags;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.ramen5914.ramensadditions.ModTags;
import net.ramen5914.ramensadditions.RamensAdditions;
import net.ramen5914.ramensadditions.emi.ModEmiPlugin;
import net.ramen5914.ramensadditions.gui.screen.AdvancedGrindstoneScreen;
import net.ramen5914.ramensadditions.util.CustomFunctions;
import org.jetbrains.annotations.Nullable;
import oshi.util.tuples.Pair;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class AdvancedGrindingRecipe implements EmiRecipe {
private List<EmiIngredient> inputs = List.of(EmiIngredient.of(Ingredient.of(Items.NETHERITE_SWORD)), EmiIngredient.of(Ingredient.of(Items.NETHERITE_INGOT)));
private List<EmiStack> outputs = List.of(EmiStack.of(Items.NETHERITE_SWORD));

public AdvancedGrindingRecipe() {
private final int uniq = new Random().nextInt();
private final Item tool;
private final ResourceLocation id;

public AdvancedGrindingRecipe(Item tool, ResourceLocation id) {
this.tool = tool;
this.id = id;
}

@Override
Expand All @@ -30,17 +50,17 @@ public EmiRecipeCategory getCategory() {

@Override
public @Nullable ResourceLocation getId() {
return ResourceLocation.fromNamespaceAndPath(RamensAdditions.MOD_ID, "/advanced_grindstone/test");
return id;
}

@Override
public List<EmiIngredient> getInputs() {
return inputs;
return List.of(EmiStack.of(tool), EmiIngredient.of(ModTags.Items.DISENCHANTING_CATALYSTS));
}

@Override
public List<EmiStack> getOutputs() {
return outputs;
return List.of(EmiStack.of(tool));
}

@Override
Expand All @@ -60,10 +80,56 @@ public boolean supportsRecipeTree() {

@Override
public void addWidgets(WidgetHolder widgets) {
widgets.addTexture(AdvancedGrindstoneScreen.ADVANCED_GRINDSTONE_BACKGROUND, 0, 0, getDisplayWidth(), getDisplayHeight(), 11, 15);
widgets.addTexture(AdvancedGrindstoneScreen.ADVANCED_GRINDSTONE_BACKGROUND, 0, 0, 154, 79, 11, 15);

widgets.addGeneratedSlot(r -> getTool(r, true, widgets), uniq, 22, 14).drawBack(false);
widgets.addSlot(EmiIngredient.of(ModTags.Items.DISENCHANTING_CATALYSTS), 83, 18).drawBack(false);
widgets.addGeneratedSlot(r -> getTool(r, false, widgets), uniq, 132, 18).drawBack(false).recipeContext(this);

widgets.addText(Component.literal("All"), 5, 66, 0xffffff, true);
}

private EmiStack getTool(Random random, Boolean enchant, WidgetHolder widgets) {
ItemStack itemStack = new ItemStack(tool);
int enchantments = 1 + Math.max(random.nextInt(5), random.nextInt(3));

List<Enchantment> list = Lists.newArrayList();

final Registry<Enchantment> ENCHANTMENT_REGISTRY = Minecraft.getInstance().level.registryAccess().registryOrThrow(Registries.ENCHANTMENT);

outer:
for (int i = 0; i < enchantments; i++) {
Enchantment enchantment = getEnchantment(random);

int maxLvl = enchantment.getMaxLevel();
int minLvl = enchantment.getMinLevel();

int lvl = maxLvl > 0 ? random.nextInt(maxLvl) + 1 : 0;

if (lvl < minLvl) {
lvl = minLvl;
}

for (Enchantment e : list) {
if (e == enchantment || !Enchantment.areCompatible(ENCHANTMENT_REGISTRY.wrapAsHolder(e), ENCHANTMENT_REGISTRY.wrapAsHolder(enchantment))) {
continue outer;
}
}
list.add(enchantment);

if (enchant) {
itemStack.enchant(ENCHANTMENT_REGISTRY.wrapAsHolder(enchantment), lvl);
}
}

return EmiStack.of(itemStack);
}

private Enchantment getEnchantment(Random random) {
final Registry<Enchantment> ENCHANTMENT_REGISTRY = Minecraft.getInstance().level.registryAccess().registryOrThrow(Registries.ENCHANTMENT);

widgets.addSlot(inputs.getFirst(), 22, 14);
widgets.addSlot(inputs.get(1), 83, 18);
widgets.addSlot(outputs.getFirst(), 132, 18).recipeContext(this);
List<Enchantment> enchantments = ENCHANTMENT_REGISTRY.stream().filter(i -> tool.getDefaultInstance().supportsEnchantment(Holder.direct(i))).toList();
int enchantment = random.nextInt(enchantments.size());
return enchantments.get(enchantment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ protected void containerTick() {
protected void init() {
super.init();

RamensAdditions.LOGGER.info("INIT HERE");

initGatherOptions();

addRenderableWidget(enchantmentSelector);
Expand Down
4 changes: 2 additions & 2 deletions update.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"1.0.0": "First release of my mod."
},
"promos": {
"1.21.1-latest": "0.1.2",
"1.21.1-recommended": "0.1.2"
"1.21.1-latest": "0.1.3",
"1.21.1-recommended": "0.1.3"
},
"versions": "1.21.1"
}

0 comments on commit 472b07a

Please sign in to comment.