Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,24 @@
import aztech.modern_industrialization.machines.gui.MachineScreen;
import aztech.modern_industrialization.machines.guicomponents.EnergyBarClient;
import aztech.modern_industrialization.machines.guicomponents.ProgressBarClient;
import aztech.modern_industrialization.machines.init.MIMachineRecipeTypes;
import aztech.modern_industrialization.machines.init.MachineTier;
import aztech.modern_industrialization.machines.recipe.MachineRecipe;
import aztech.modern_industrialization.machines.recipe.RecipeConversions;
import aztech.modern_industrialization.thirdparty.fabrictransfer.api.fluid.FluidVariant;
import aztech.modern_industrialization.util.TextHelper;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.ComposterBlock;

public class MachineCategory extends ViewerCategory<RecipeHolder<MachineRecipe>> {
public static MachineCategory create(MachineCategoryParams params) {
Expand Down Expand Up @@ -96,41 +92,10 @@ public void buildWorkstations(WorkstationConsumer consumer) {

@Override
public void buildRecipes(RecipeManager recipeManager, RegistryAccess registryAccess, Consumer<RecipeHolder<MachineRecipe>> consumer) {
var machineRecipes = recipeManager.getRecipes().stream()
.filter(r -> r.value() instanceof MachineRecipe)
.map(r -> (RecipeHolder<MachineRecipe>) r)
.toList();

// regular recipes
machineRecipes.stream()
.filter(r -> params.recipePredicate.test(r.value()))
params.recipeType.getRecipesWithoutCache(Minecraft.getInstance().level).stream()
.filter(recipe -> params.recipePredicate.test(recipe.value()))
.sorted(Comparator.comparing(RecipeHolder::id))
.forEach(consumer);

// converted recipes
if (params.category.getNamespace().equals(MI.ID)) {
switch (params.category.getPath()) {
case "bronze_furnace" -> {
recipeManager.getAllRecipesFor(RecipeType.SMELTING)
.stream()
.map(r -> RecipeConversions.ofSmelting(r, MIMachineRecipeTypes.FURNACE, registryAccess))
.forEach(consumer);
}
case "bronze_cutting_machine" -> {
recipeManager.getAllRecipesFor(RecipeType.STONECUTTING)
.stream()
.map(r -> RecipeConversions.ofStonecutting(r, MIMachineRecipeTypes.CUTTING_MACHINE, registryAccess))
.forEach(consumer);
}
case "centrifuge" -> {
ComposterBlock.COMPOSTABLES.keySet()
.stream()
.map(RecipeConversions::ofCompostable)
.filter(Objects::nonNull)
.forEach(consumer);
}
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import aztech.modern_industrialization.inventory.SlotPositions;
import aztech.modern_industrialization.machines.guicomponents.ProgressBar;
import aztech.modern_industrialization.machines.recipe.MachineRecipe;
import aztech.modern_industrialization.machines.recipe.MachineRecipeType;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
Expand All @@ -39,21 +40,23 @@ public class MachineCategoryParams {
public final SlotPositions fluidInputs;
public final SlotPositions fluidOutputs;
public final ProgressBar.Parameters progressBarParams;
public final MachineRecipeType recipeType;
public final Predicate<MachineRecipe> recipePredicate;
public final boolean isMultiblock;
public final SteamMode steamMode;
public final List<ResourceLocation> workstations = new ArrayList<>();

public MachineCategoryParams(String englishName, ResourceLocation category, SlotPositions itemInputs, SlotPositions itemOutputs,
SlotPositions fluidInputs, SlotPositions fluidOutputs, ProgressBar.Parameters progressBarParams, Predicate<MachineRecipe> recipePredicate,
boolean isMultiblock, SteamMode steamMode) {
SlotPositions fluidInputs, SlotPositions fluidOutputs, ProgressBar.Parameters progressBarParams, MachineRecipeType recipeType,
Predicate<MachineRecipe> recipePredicate, boolean isMultiblock, SteamMode steamMode) {
this.englishName = englishName;
this.category = category;
this.itemInputs = itemInputs;
this.itemOutputs = itemOutputs;
this.fluidInputs = fluidInputs;
this.fluidOutputs = fluidOutputs;
this.progressBarParams = progressBarParams;
this.recipeType = recipeType;
this.recipePredicate = recipePredicate;
this.isMultiblock = isMultiblock;
this.steamMode = steamMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import aztech.modern_industrialization.machines.recipe.MachineRecipeType;
import aztech.modern_industrialization.util.Tickable;
import java.util.UUID;
import net.minecraft.world.level.Level;
import net.minecraft.server.level.ServerLevel;
import org.jetbrains.annotations.Nullable;

public abstract class AbstractCraftingMachineBlockEntity extends MachineBlockEntity implements CrafterComponent.Behavior, Tickable,
Expand Down Expand Up @@ -106,8 +106,8 @@ public MIInventory getInventory() {
}

@Override
public Level getCrafterWorld() {
return level;
public ServerLevel getCrafterWorld() {
return (ServerLevel) level;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
import java.util.List;
import java.util.UUID;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

public abstract class AbstractElectricCraftingMultiblockBlockEntity extends AbstractCraftingMultiblockBlockEntity
Expand Down Expand Up @@ -109,8 +109,8 @@ public final long consumeEu(long max, Simulation simulation) {
}

@Override
public final Level getCrafterWorld() {
return level;
public final ServerLevel getCrafterWorld() {
return (ServerLevel) level;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
import java.util.UUID;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

public class SteamCraftingMultiblockBlockEntity extends AbstractCraftingMultiblockBlockEntity {
Expand Down Expand Up @@ -105,8 +105,8 @@ public long getMaxRecipeEu() {
}

@Override
public Level getCrafterWorld() {
return level;
public ServerLevel getCrafterWorld() {
return (ServerLevel) level;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.material.Fluid;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -104,7 +103,7 @@ default boolean isOverdriving() {
}

// can't use getWorld() or the remapping will fail
Level getCrafterWorld();
ServerLevel getCrafterWorld();

default int getMaxFluidOutputs() {
return Integer.MAX_VALUE;
Expand Down Expand Up @@ -627,7 +626,7 @@ protected void clearLocks() {

public void lockRecipe(ResourceLocation recipeId, net.minecraft.world.entity.player.Inventory inventory) {
// Find MachineRecipe
Optional<RecipeHolder<MachineRecipe>> optionalMachineRecipe = behavior.recipeType().getRecipes(behavior.getCrafterWorld()).stream()
Optional<RecipeHolder<MachineRecipe>> optionalMachineRecipe = behavior.recipeType().getRecipesWithCache(behavior.getCrafterWorld()).stream()
.filter(recipe -> recipe.id().equals(recipeId)).findFirst();
if (optionalMachineRecipe.isEmpty())
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ public final void register() {

ReiMachineRecipes.registerCategory(category, new MachineCategoryParams(englishName, category,
itemInputs.build(), itemOutputs.build(), fluidInputs.build(), fluidOutputs.build(),
progressBarParams, recipe -> recipe.getType() == recipeType && extraTest.test(recipe), true, steamMode));
progressBarParams, recipeType, extraTest, true, steamMode));
for (ResourceLocation workstation : workstations) {
ReiMachineRecipes.registerWorkstation(category, workstation);
ReiMachineRecipes.registerRecipeCategoryForMachine(workstation, category, ReiMachineRecipes.MachineScreenPredicate.MULTIBLOCK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public static void registerMachineTiers(String englishName, String machine, Mach
new MachineCategoryParams(null, null, items.sublist(0, itemInputCount),
items.sublist(itemInputCount, itemInputCount + itemOutputCount),
fluids.sublist(0, fluidInputCount), fluids.sublist(fluidInputCount, fluidInputCount + fluidOutputCount), progressBarParams,
null, false, SteamMode.BOTH),
null, null, false, SteamMode.BOTH),
tiers);
}

Expand All @@ -265,7 +265,7 @@ private static void registerReiTiers(String englishName, String machine, Machine
MachineCategoryParams category = new MachineCategoryParams(fullEnglishName, MI.id(itemId), categoryParams.itemInputs,
categoryParams.itemOutputs,
categoryParams.fluidInputs, categoryParams.fluidOutputs, categoryParams.progressBarParams,
recipe -> recipe.getType() == recipeType && minEu <= recipe.eu && recipe.eu <= maxEu, false,
recipeType, recipe -> minEu <= recipe.eu && recipe.eu <= maxEu, false,
i < 2 ? SteamMode.BOTH : SteamMode.ELECTRIC_ONLY);
ReiMachineRecipes.registerCategory(MI.id(itemId), category);
ReiMachineRecipes.registerMachineClickArea(MI.id(itemId), categoryParams.progressBarParams.toRectangle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
*/
package aztech.modern_industrialization.machines.recipe;

import java.util.List;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.ComposterBlock;

Expand All @@ -34,7 +36,7 @@ public CentrifugeMachineRecipeType(ResourceLocation id) {
}

@Override
protected void fillRecipeList(Level world) {
protected void fillRecipeList(Level world, List<RecipeHolder<MachineRecipe>> recipeList) {
recipeList.addAll(getManagerRecipes(world));

for (var itemCompostable : ComposterBlock.COMPOSTABLES.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
*/
package aztech.modern_industrialization.machines.recipe;

import java.util.List;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;

Expand All @@ -33,7 +35,7 @@ public CuttingMachineRecipeType(ResourceLocation id) {
}

@Override
protected void fillRecipeList(Level world) {
protected void fillRecipeList(Level world, List<RecipeHolder<MachineRecipe>> recipeList) {
// Add all regular cutting machine recipes
recipeList.addAll(getManagerRecipes(world));
// Add all stone cutter recipes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import aztech.modern_industrialization.MI;
import java.util.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;

Expand All @@ -34,7 +35,7 @@ public FurnaceRecipeProxy(ResourceLocation id) {
super(id);
}

protected void fillRecipeList(Level world) {
protected void fillRecipeList(Level world, List<RecipeHolder<MachineRecipe>> recipeList) {
for (var smeltingRecipe : world.getRecipeManager().getAllRecipesFor(RecipeType.SMELTING)) {
var recipe = RecipeConversions.ofSmelting(smeltingRecipe, this, world.registryAccess());
recipeList.add(recipe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package aztech.modern_industrialization.machines.recipe;

import com.google.gson.*;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.MapCodec;
import java.util.*;
Expand Down Expand Up @@ -59,17 +58,21 @@ public MachineRecipeType(ResourceLocation id) {
/**
* Never modify or store the result!
*/
protected Collection<RecipeHolder<MachineRecipe>> getManagerRecipes(Level world) {
return world.getRecipeManager().getAllRecipesFor(this);
protected Collection<RecipeHolder<MachineRecipe>> getManagerRecipes(Level level) {
return level.getRecipeManager().getAllRecipesFor(this);
}

public Collection<RecipeHolder<MachineRecipe>> getRecipes(Level world) {
return getManagerRecipes(world);
public Collection<RecipeHolder<MachineRecipe>> getRecipesWithoutCache(Level level) {
return getManagerRecipes(level);
}

public Collection<RecipeHolder<MachineRecipe>> getRecipesWithCache(ServerLevel level) {
return getManagerRecipes(level);
}

@Nullable
public RecipeHolder<MachineRecipe> getRecipe(Level world, ResourceLocation id) {
return getRecipes(world).stream().filter(r -> r.id().equals(id)).findFirst().orElse(null);
public RecipeHolder<MachineRecipe> getRecipe(ServerLevel world, ResourceLocation id) {
return getRecipesWithCache(world).stream().filter(r -> r.id().equals(id)).findFirst().orElse(null);
}

/*
Expand Down Expand Up @@ -97,7 +100,7 @@ private void updateRecipeCache(ServerLevel world) {
lastUpdate = time;
recipeCache.clear();
fluidOnlyRecipes.clear();
for (RecipeHolder<MachineRecipe> recipe : getRecipes(world)) {
for (RecipeHolder<MachineRecipe> recipe : getRecipesWithCache(world)) {
if (recipe.value().itemInputs.size() == 0) {
if (recipe.value().fluidInputs.size() > 0) {
fluidOnlyRecipes.add(recipe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Collection;
import java.util.List;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.Level;

Expand All @@ -40,17 +41,24 @@ public ProxyableMachineRecipeType(ResourceLocation id) {

private long lastUpdate = 0;
private static final long UPDATE_INTERVAL = 20 * 1000;
protected List<RecipeHolder<MachineRecipe>> recipeList = new ArrayList<>();
private final List<RecipeHolder<MachineRecipe>> recipeList = new ArrayList<>();

protected abstract void fillRecipeList(Level world);
protected abstract void fillRecipeList(Level world, List<RecipeHolder<MachineRecipe>> recipeList);

@Override
public Collection<RecipeHolder<MachineRecipe>> getRecipes(Level world) {
public final Collection<RecipeHolder<MachineRecipe>> getRecipesWithoutCache(Level level) {
var recipes = new ArrayList<RecipeHolder<MachineRecipe>>();
fillRecipeList(level, recipes);
return recipes;
}

@Override
public final Collection<RecipeHolder<MachineRecipe>> getRecipesWithCache(ServerLevel level) {
long time = System.currentTimeMillis();
if (time - lastUpdate > UPDATE_INTERVAL) {
lastUpdate = time;
recipeList.clear();
fillRecipeList(world);
fillRecipeList(level, recipeList);
}
return recipeList;
}
Expand Down
Loading