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

multi Level Cache #5117

Open
wants to merge 4 commits into
base: 1.18.2
Choose a base branch
from
Open
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 @@ -10,12 +10,10 @@
import slimeknights.mantle.block.entity.MantleBlockEntity;
import slimeknights.tconstruct.common.network.InventorySlotSyncPacket;
import slimeknights.tconstruct.common.network.TinkerNetwork;
import slimeknights.tconstruct.library.recipe.TinkerRecipeTypes;
import slimeknights.tconstruct.library.recipe.melting.IMeltingContainer;
import slimeknights.tconstruct.library.recipe.melting.IMeltingRecipe;

import javax.annotation.Nullable;
import java.util.Optional;
import java.util.function.Predicate;

/**
Expand All @@ -33,7 +31,9 @@ public class MeltingModule implements IMeltingContainer, ContainerData {
private static final int REQUIRED_TEMP = 2;

/** Tile entity containing this melting module */
private final MantleBlockEntity parent;
private final MantleBlockEntity be;
/** Melting Inventory containing this melting module */
private final MeltingModuleInventory inventory;
/** Function that accepts fluid output from this module */
private final Predicate<IMeltingRecipe> outputFunction;
/** Function that boosts the ores based on the rate type */
Expand Down Expand Up @@ -78,9 +78,9 @@ private void resetRecipe() {
*/
public void setStack(ItemStack newStack) {
// send a slot update to the client when items change, so we can update the TESR
Level world = parent.getLevel();
Level world = be.getLevel();
if (slotIndex != -1 && world != null && !world.isClientSide && !ItemStack.matches(stack, newStack)) {
TinkerNetwork.getInstance().sendToClientsAround(new InventorySlotSyncPacket(newStack, slotIndex, parent.getBlockPos()), world, parent.getBlockPos());
TinkerNetwork.getInstance().sendToClientsAround(new InventorySlotSyncPacket(newStack, slotIndex, be.getBlockPos()), world, be.getBlockPos());
}

// clear progress if setting to empty or the items do not match
Expand All @@ -103,7 +103,7 @@ public void setStack(ItemStack newStack) {
}
requiredTime = newTime;
requiredTemp = newTemp;
parent.setChangedFast();
be.setChangedFast();
}


Expand Down Expand Up @@ -165,7 +165,7 @@ public void coolItem() {
*/
@Nullable
private IMeltingRecipe findRecipe() {
Level world = parent.getLevel();
Level world = be.getLevel();
if (world == null) {
return null;
}
Expand All @@ -175,10 +175,11 @@ private IMeltingRecipe findRecipe() {
if (last != null && last.matches(this, world)) {
return last;
}

// if that fails, try to find a new recipe
Optional<IMeltingRecipe> newRecipe = world.getRecipeManager().getRecipeFor(TinkerRecipeTypes.MELTING.get(), this, world);
if (newRecipe.isPresent()) {
lastRecipe = newRecipe.get();
IMeltingRecipe newRecipe = inventory.findRecipe(this, lastRecipe);
if (newRecipe != null) {
lastRecipe = newRecipe;
return lastRecipe;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
import net.minecraft.nbt.Tag;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler.FluidAction;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
import slimeknights.mantle.block.entity.MantleBlockEntity;
import slimeknights.tconstruct.library.recipe.TinkerRecipeTypes;
import slimeknights.tconstruct.library.recipe.melting.IMeltingContainer.IOreRate;
import slimeknights.tconstruct.library.recipe.melting.IMeltingRecipe;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;

/**
Expand All @@ -26,6 +30,8 @@ public class MeltingModuleInventory implements IItemHandlerModifiable {
private static final String TAG_ITEMS = "items";
private static final String TAG_SIZE = "size";

/** Last used recipe by any slot in the inventory */
private IMeltingRecipe lastRecipe;
/** Parent tile entity */
private final MantleBlockEntity parent;
/** Fluid handler for outputs */
Expand Down Expand Up @@ -120,6 +126,29 @@ public int getRequiredTemp(int slot) {
return hasModule(slot) ? modules[slot].getRequiredTemp() : 0;
}

/**
* Finds a melting recipe
* @param module Melting Module asking for a recipe
* @param slotRecipe Last used Recipe of that slot
* @return Melting recipe found, or null if no match
*/
@Nullable
public IMeltingRecipe findRecipe(MeltingModule module, @Nullable IMeltingRecipe slotRecipe) {
Level world = parent.getLevel();
if (world == null) {
return null;
}
if (lastRecipe != null && slotRecipe != lastRecipe && lastRecipe.matches(module, world)) {
return lastRecipe;
}
Optional<IMeltingRecipe> newRecipe = world.getRecipeManager().getRecipeFor(TinkerRecipeTypes.MELTING.get(), module, world);
ferriarnus marked this conversation as resolved.
Show resolved Hide resolved
if (newRecipe.isPresent()) {
lastRecipe = newRecipe.get();
return lastRecipe;
}
return null;
}


/* Sub modules */

Expand All @@ -134,7 +163,7 @@ public MeltingModule getModule(int slot) {
throw new IndexOutOfBoundsException();
}
if (modules[slot] == null) {
modules[slot] = new MeltingModule(parent, recipe -> tryFillTank(slot, recipe), oreRate, slot);
modules[slot] = new MeltingModule(parent, this, recipe -> tryFillTank(slot, recipe), oreRate, slot);
}
return modules[slot];
}
Expand Down