Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for extending imbuement recipes #1339

Merged
merged 2 commits into from
Jul 21, 2024
Merged
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
Empty file modified gradlew
100644 → 100755
Empty file.
6 changes: 2 additions & 4 deletions src/main/java/com/hollingsworth/arsnouveau/ArsNouveau.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.hollingsworth.arsnouveau;

import com.hollingsworth.arsnouveau.api.registry.BuddingConversionRegistry;
import com.hollingsworth.arsnouveau.api.registry.*;
import com.hollingsworth.arsnouveau.api.event.EventQueue;
import com.hollingsworth.arsnouveau.api.registry.CasterTomeRegistry;
import com.hollingsworth.arsnouveau.api.registry.RitualRegistry;
import com.hollingsworth.arsnouveau.api.registry.ScryRitualRegistry;
import com.hollingsworth.arsnouveau.api.ritual.DispenserRitualBehavior;
import com.hollingsworth.arsnouveau.client.container.CraftingTerminalScreen;
import com.hollingsworth.arsnouveau.client.registry.ClientHandler;
Expand Down Expand Up @@ -101,6 +98,7 @@ public void setup(final FMLCommonSetupEvent event) {
event.enqueueWork(Terrablender::registerBiomes);
}
MinecraftForge.EVENT_BUS.addListener((ServerStartedEvent e) -> {
GenericRecipeRegistry.reloadAll(e.getServer().getRecipeManager());
CasterTomeRegistry.reloadTomeData(e.getServer().getRecipeManager(), e.getServer().getLevel(Level.OVERWORLD));
BuddingConversionRegistry.reloadBuddingConversionRecipes(e.getServer().getRecipeManager());
ScryRitualRegistry.reloadScryRitualRecipes(e.getServer().getRecipeManager());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public Set<RecipeType<? extends IEnchantingRecipe>> getEnchantingRecipeTypes() {
return enchantingRecipeTypes;
}


public List<IEnchantingRecipe> getEnchantingApparatusRecipes(Level world) {
List<IEnchantingRecipe> recipes = new ArrayList<>(enchantingApparatusRecipes);
RecipeManager manager = world.getRecipeManager();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.hollingsworth.arsnouveau.api.imbuement_chamber;

import com.hollingsworth.arsnouveau.common.block.tile.ImbuementTile;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;

public interface IImbuementRecipe extends Recipe<ImbuementTile> {
boolean isMatch(ImbuementTile imbuementTile);

ItemStack getResult(ImbuementTile imbuementTile);

int getSourceCost(ImbuementTile imbuementTile);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.hollingsworth.arsnouveau.api.registry;

import net.minecraft.world.Container;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraftforge.registries.RegistryObject;

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

public class GenericRecipeRegistry<C extends Container, T extends Recipe<C>> {
private final RegistryObject<? extends RecipeType<? extends T>> type;

public GenericRecipeRegistry(RegistryObject<? extends RecipeType<? extends T>> type) {
this.type = type;
REGISTRIES.add(this);
}

public List<T> RECIPES = new ArrayList<>();

public List<T> getRecipes() {
return Collections.unmodifiableList(RECIPES);
}

public RecipeType<? extends T> getType() {
return type.get();
}

public void reload(RecipeManager manager) {
RECIPES.clear();
List<? extends T> recipes = manager.getAllRecipesFor(type.get());
RECIPES.addAll(recipes);
}

public static List<GenericRecipeRegistry<?, ?>> REGISTRIES = new ArrayList<>();

public static void reloadAll(RecipeManager recipeManager) {
for (GenericRecipeRegistry<?, ?> registry : REGISTRIES) {
registry.reload(recipeManager);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hollingsworth.arsnouveau.api.registry;

import com.hollingsworth.arsnouveau.api.imbuement_chamber.IImbuementRecipe;
import com.hollingsworth.arsnouveau.common.block.tile.ImbuementTile;

public class ImbuementRecipeRegistry extends MultiRecipeRegistry<ImbuementTile, IImbuementRecipe> {
public static ImbuementRecipeRegistry INSTANCE = new ImbuementRecipeRegistry();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.hollingsworth.arsnouveau.api.registry;

import com.google.common.collect.ImmutableList;
import net.minecraft.world.Container;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraftforge.registries.RegistryObject;

import java.util.ArrayList;
import java.util.List;

public class MultiRecipeRegistry<C extends Container, T extends Recipe<C>> {
private final List<GenericRecipeRegistry<C, T>> recipeRegistries = new ArrayList();

public <R extends T> void addRecipeType(RegistryObject<? extends RecipeType<R>> recipeType) {
recipeRegistries.add(new GenericRecipeRegistry<>(recipeType));
}

public List<RecipeType<? extends T>> getRecipeTypes() {
ImmutableList.Builder<RecipeType<? extends T>> builder = ImmutableList.builder();
for (GenericRecipeRegistry<C, T> registry : recipeRegistries) {
builder.add(registry.getType());
}
return builder.build();
}

public List<T> getRecipes() {
ImmutableList.Builder<T> builder = ImmutableList.builder();
for (GenericRecipeRegistry<C, T> registry : recipeRegistries) {
builder.addAll(registry.getRecipes());
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.hollingsworth.arsnouveau.api.ArsNouveauAPI;
import com.hollingsworth.arsnouveau.api.enchanting_apparatus.EnchantingApparatusRecipe;
import com.hollingsworth.arsnouveau.api.enchanting_apparatus.IEnchantingRecipe;
import com.hollingsworth.arsnouveau.api.imbuement_chamber.IImbuementRecipe;
import com.hollingsworth.arsnouveau.api.registry.ImbuementRecipeRegistry;
import com.hollingsworth.arsnouveau.common.crafting.recipes.GlyphRecipe;
import com.hollingsworth.arsnouveau.common.crafting.recipes.ImbuementRecipe;
import com.hollingsworth.arsnouveau.setup.registry.RecipeRegistry;
Expand Down Expand Up @@ -38,7 +40,7 @@ protected List<Ingredient> makeIngredients() {
Map<ResourceLocation, ? extends Recipe<?>> map;
if ("enchanting_apparatus".equals(recipeType)) {
EnchantingApparatusRecipe recipe = world.getRecipeManager().getAllRecipesFor(RecipeRegistry.APPARATUS_TYPE.get()).stream().filter(f -> f.id.toString().equals(recipeName)).findFirst().orElse(null);
for(RecipeType type : ArsNouveauAPI.getInstance().getEnchantingRecipeTypes()){
for(RecipeType type : ArsNouveauAPI.getInstance().getEnchantingRecipeTypes()) {
RecipeType<IEnchantingRecipe> enchantingRecipeRecipeType = (RecipeType<IEnchantingRecipe>) type;
Recipe<?> recipe1 = world.getRecipeManager().getAllRecipesFor(enchantingRecipeRecipeType).stream().filter(f -> f.getId().toString().equals(recipeName)).findFirst().orElse(null);
if(recipe1 instanceof EnchantingApparatusRecipe enchantingApparatusRecipe){
Expand All @@ -49,6 +51,14 @@ protected List<Ingredient> makeIngredients() {
return recipe == null ? ImmutableList.of() : recipe.pedestalItems;
} else if ("imbuement_chamber".equals(recipeType)) {
ImbuementRecipe recipe = world.getRecipeManager().getAllRecipesFor(RecipeRegistry.IMBUEMENT_TYPE.get()).stream().filter(f -> f.id.toString().equals(recipeName)).findFirst().orElse(null);
for (RecipeType<? extends IImbuementRecipe> type : ImbuementRecipeRegistry.INSTANCE.getRecipeTypes()) {
RecipeType<IImbuementRecipe> imbuementRecipeType = (RecipeType<IImbuementRecipe>) type;
Recipe<?> recipe1 = world.getRecipeManager().getAllRecipesFor(imbuementRecipeType).stream().filter(f -> f.getId().toString().equals(recipeName)).findFirst().orElse(null);
if (recipe1 instanceof ImbuementRecipe imbuementRecipe){
recipe = imbuementRecipe;
break;
}
}
return recipe == null ? ImmutableList.of() : recipe.pedestalItems;
} else if ("glyph_recipe".equals(recipeType)) {
GlyphRecipe recipe = (GlyphRecipe) world.getRecipeManager().byKey(new ResourceLocation(recipeName)).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.hollingsworth.arsnouveau.common.block.tile;

import com.hollingsworth.arsnouveau.api.ArsNouveauAPI;
import com.hollingsworth.arsnouveau.api.block.IPedestalMachine;
import com.hollingsworth.arsnouveau.api.client.ITooltipProvider;
import com.hollingsworth.arsnouveau.api.imbuement_chamber.IImbuementRecipe;
import com.hollingsworth.arsnouveau.api.registry.ImbuementRecipeRegistry;
import com.hollingsworth.arsnouveau.api.source.AbstractSourceMachine;
import com.hollingsworth.arsnouveau.api.source.ISpecialSourceProvider;
import com.hollingsworth.arsnouveau.api.util.SourceUtil;
Expand Down Expand Up @@ -51,7 +54,7 @@ public class ImbuementTile extends AbstractSourceMachine implements Container, I
public ItemStack stack = ItemStack.EMPTY;
public ItemEntity entity;
public boolean draining;
ImbuementRecipe recipe;
IImbuementRecipe recipe;
int backoff;
public float frames;
boolean hasRecipe;
Expand Down Expand Up @@ -118,7 +121,7 @@ public void tick() {
}
}

if (recipe == null || !recipe.matches(this, level)) {
if (recipe == null || !recipe.isMatch(this)) {
backoff = 20;
recipe = null;
if (this.draining) {
Expand All @@ -130,12 +133,13 @@ public void tick() {

int transferRate = 200;

int cost = recipe.getSourceCost(this);

if (this.level.getGameTime() % 20 == 0 && this.getSource() < recipe.source) {
if (!canAcceptSource(Math.min(200, recipe.source)))
if (this.level.getGameTime() % 20 == 0 && this.getSource() < cost) {
if (!canAcceptSource(Math.min(200, cost)))
return;

ISpecialSourceProvider takePos = SourceUtil.takeSource(worldPosition, level, 2, Math.min(200, recipe.source));
ISpecialSourceProvider takePos = SourceUtil.takeSource(worldPosition, level, 2, Math.min(200, cost));
if (takePos != null) {
this.addSource(transferRate);
EntityFlyingItem item = new EntityFlyingItem(level, takePos.getCurrentPos().above(), worldPosition, 255, 50, 80)
Expand All @@ -156,9 +160,9 @@ public void tick() {

}

if (this.getSource() >= recipe.source && craftTicks <= 0) {
this.setItem(0, recipe.output.copy());
this.addSource(-recipe.source);
if (this.getSource() >= cost && craftTicks <= 0) {
this.setItem(0, recipe.getResult(this).copy());
this.addSource(-cost);
ParticleUtil.spawnTouchPacket(level, worldPosition, ParticleColor.defaultParticleColor());
updateBlock();
}
Expand Down Expand Up @@ -206,7 +210,7 @@ public boolean canPlaceItem(int slot, ItemStack stack) {
if (stack.isEmpty() || !this.stack.isEmpty())
return false;
this.stack = stack.copy();
ImbuementRecipe recipe = getRecipeNow();
IImbuementRecipe recipe = getRecipeNow();
this.stack = ItemStack.EMPTY;
return recipe != null;
}
Expand Down Expand Up @@ -306,18 +310,18 @@ public List<BlockPos> getNearbyPedestals() {
return pedestalList(getBlockPos(), 1, getLevel());
}

public @Nullable ImbuementRecipe getRecipeNow(){
return level.getRecipeManager().getAllRecipesFor(RecipeRegistry.IMBUEMENT_TYPE.get()).stream()
.filter(f -> f.matches(this, level)).findFirst().orElse(null);
public @Nullable IImbuementRecipe getRecipeNow(){
return ImbuementRecipeRegistry.INSTANCE.getRecipes().stream().filter(r -> r.isMatch(this)).findFirst().orElse(null);
}

@Override
public void getTooltip(List<Component> tooltip) {
var recipe = getRecipeNow();
if(recipe != null && !recipe.output.isEmpty() && stack != null && !stack.isEmpty()) {
tooltip.add(Component.translatable("ars_nouveau.crafting", recipe.output.getHoverName()));
if(recipe.source > 0) {
tooltip.add(Component.translatable("ars_nouveau.crafting_progress", Math.min(100, (getSource() * 100) / recipe.source)).withStyle(ChatFormatting.GOLD));
if(recipe != null && !recipe.getResult(this).isEmpty() && stack != null && !stack.isEmpty()) {
int cost = recipe.getSourceCost(this);
tooltip.add(Component.translatable("ars_nouveau.crafting", recipe.getResult(this).getHoverName()));
if(cost > 0) {
tooltip.add(Component.translatable("ars_nouveau.crafting_progress", Math.min(100, (getSource() * 100) / cost)).withStyle(ChatFormatting.GOLD));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.JsonObject;
import com.hollingsworth.arsnouveau.ArsNouveau;
import com.hollingsworth.arsnouveau.api.enchanting_apparatus.EnchantingApparatusRecipe;
import com.hollingsworth.arsnouveau.api.imbuement_chamber.IImbuementRecipe;
import com.hollingsworth.arsnouveau.common.block.tile.ImbuementTile;
import com.hollingsworth.arsnouveau.setup.registry.RecipeRegistry;
import net.minecraft.core.RegistryAccess;
Expand All @@ -31,7 +32,7 @@
import static com.hollingsworth.arsnouveau.setup.registry.RegistryHelper.getRegistryName;


public class ImbuementRecipe implements Recipe<ImbuementTile> {
public class ImbuementRecipe implements IImbuementRecipe {
public final Ingredient input;
public final ItemStack output;
public final int source;
Expand Down Expand Up @@ -74,9 +75,19 @@ public ImbuementRecipe withPedestalItem(ItemLike i) {
return this;
}

public boolean isMatch(List<ItemStack> pedestalItems, ItemStack reagent, ImbuementTile imbuementTile, @Nullable Player player) {
public boolean isMatch(ImbuementTile imbuementTile) {
pedestalItems = pedestalItems.stream().filter(itemStack -> !itemStack.isEmpty()).collect(Collectors.toList());
return doesReagentMatch(reagent) && this.pedestalItems.size() == pedestalItems.size() && EnchantingApparatusRecipe.doItemsMatch(pedestalItems, this.pedestalItems);
return doesReagentMatch(imbuementTile.getItem(0)) && this.pedestalItems.size() == imbuementTile.getPedestalItems().size() && EnchantingApparatusRecipe.doItemsMatch(imbuementTile.getPedestalItems(), this.pedestalItems);
}

@Override
public ItemStack getResult(ImbuementTile imbuementTile) {
return this.output;
}

@Override
public int getSourceCost(ImbuementTile imbuementTile) {
return this.source;
}

public boolean doesReagentMatch(ItemStack reag) {
Expand All @@ -85,7 +96,7 @@ public boolean doesReagentMatch(ItemStack reag) {

@Override
public boolean matches(ImbuementTile pContainer, Level pLevel) {
return this.input.test(pContainer.getItem(0)) && EnchantingApparatusRecipe.doItemsMatch(pContainer.getPedestalItems(), pedestalItems);
return isMatch(pContainer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
import com.hollingsworth.arsnouveau.api.perk.PerkAttributes;
import com.hollingsworth.arsnouveau.api.recipe.DispelEntityRecipe;
import com.hollingsworth.arsnouveau.api.recipe.MultiRecipeWrapper;
import com.hollingsworth.arsnouveau.api.registry.BuddingConversionRegistry;
import com.hollingsworth.arsnouveau.api.registry.CasterTomeRegistry;
import com.hollingsworth.arsnouveau.api.registry.RitualRegistry;
import com.hollingsworth.arsnouveau.api.registry.ScryRitualRegistry;
import com.hollingsworth.arsnouveau.api.registry.*;
import com.hollingsworth.arsnouveau.api.registry.GenericRecipeRegistry;
import com.hollingsworth.arsnouveau.api.ritual.RitualEventQueue;
import com.hollingsworth.arsnouveau.api.util.BlockUtil;
import com.hollingsworth.arsnouveau.api.util.CuriosUtil;
Expand Down Expand Up @@ -108,6 +106,7 @@ protected void apply(Object pObject, ResourceManager pResourceManager, ProfilerF
@Override
public void tickEvent(TickEvent event) {
if (event instanceof TickEvent.ServerTickEvent serverTickEvent) {
GenericRecipeRegistry.reloadAll(serverTickEvent.getServer().getRecipeManager());
CasterTomeRegistry.reloadTomeData(serverTickEvent.getServer().getRecipeManager(), serverTickEvent.getServer().getLevel(Level.OVERWORLD));
BuddingConversionRegistry.reloadBuddingConversionRecipes(serverTickEvent.getServer().getRecipeManager());
ScryRitualRegistry.reloadScryRitualRecipes(serverTickEvent.getServer().getRecipeManager());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public static void setup() {
registerPerk(VampiricPerk.INSTANCE);
registerPerk(KnockbackResistPerk.INSTANCE);

ImbuementRecipeRegistry.INSTANCE.addRecipeType(RecipeRegistry.IMBUEMENT_TYPE);
}

//register things only in dev, safe from production
Expand Down