Added caching to the CraftingManager (#3171)
* Added caching to the CraftingManager * Fixed import problems.
This commit is contained in:
@@ -15,7 +15,9 @@ import net.minecraft.nbt.ListTag;
|
|||||||
import net.minecraft.nbt.Tag;
|
import net.minecraft.nbt.Tag;
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
import net.minecraft.world.item.Item;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.level.material.Fluid;
|
||||||
import net.minecraftforge.fluids.FluidStack;
|
import net.minecraftforge.fluids.FluidStack;
|
||||||
import net.minecraftforge.items.IItemHandlerModifiable;
|
import net.minecraftforge.items.IItemHandlerModifiable;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
@@ -40,6 +42,8 @@ public class CraftingManager implements ICraftingManager {
|
|||||||
private final Map<ICraftingPattern, Set<ICraftingPatternContainer>> patternToContainer = new HashMap<>();
|
private final Map<ICraftingPattern, Set<ICraftingPatternContainer>> patternToContainer = new HashMap<>();
|
||||||
|
|
||||||
private final List<ICraftingPattern> patterns = new ArrayList<>();
|
private final List<ICraftingPattern> patterns = new ArrayList<>();
|
||||||
|
private final Map<FluidStackKey, ICraftingPattern> fluidPatternsByOutput = new HashMap<>();
|
||||||
|
private final Map<ItemStackKey, ICraftingPattern> itemPatternsByOutput = new HashMap<>();
|
||||||
|
|
||||||
private final Map<UUID, ICraftingTask> tasks = new LinkedHashMap<>();
|
private final Map<UUID, ICraftingTask> tasks = new LinkedHashMap<>();
|
||||||
private final List<ICraftingTask> tasksToAdd = new ArrayList<>();
|
private final List<ICraftingTask> tasksToAdd = new ArrayList<>();
|
||||||
@@ -48,6 +52,52 @@ public class CraftingManager implements ICraftingManager {
|
|||||||
private final Set<ICraftingMonitorListener> listeners = new HashSet<>();
|
private final Set<ICraftingMonitorListener> listeners = new HashSet<>();
|
||||||
private ListTag tasksToRead;
|
private ListTag tasksToRead;
|
||||||
|
|
||||||
|
private static class FluidStackKey {
|
||||||
|
private final Fluid fluid;
|
||||||
|
private final CompoundTag tag;
|
||||||
|
|
||||||
|
public FluidStackKey(FluidStack fluidStack) {
|
||||||
|
this.fluid = fluidStack.getFluid();
|
||||||
|
this.tag = fluidStack.getTag();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
FluidStackKey that = (FluidStackKey) o;
|
||||||
|
return fluid.equals(that.fluid) && Objects.equals(tag, that.tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(fluid, tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ItemStackKey {
|
||||||
|
private final Item item;
|
||||||
|
private final CompoundTag tag;
|
||||||
|
|
||||||
|
public ItemStackKey(ItemStack itemStack) {
|
||||||
|
this.item = itemStack.getItem();
|
||||||
|
this.tag = itemStack.getTag();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
ItemStackKey that = (ItemStackKey) o;
|
||||||
|
return item.equals(that.item) && Objects.equals(tag, that.tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(item, tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public CraftingManager(INetwork network) {
|
public CraftingManager(INetwork network) {
|
||||||
this.network = network;
|
this.network = network;
|
||||||
}
|
}
|
||||||
@@ -357,6 +407,8 @@ public class CraftingManager implements ICraftingManager {
|
|||||||
this.network.getFluidStorageCache().getCraftablesList().clear();
|
this.network.getFluidStorageCache().getCraftablesList().clear();
|
||||||
|
|
||||||
this.patterns.clear();
|
this.patterns.clear();
|
||||||
|
this.fluidPatternsByOutput.clear();
|
||||||
|
this.itemPatternsByOutput.clear();
|
||||||
this.containerInventories.clear();
|
this.containerInventories.clear();
|
||||||
this.patternToContainer.clear();
|
this.patternToContainer.clear();
|
||||||
|
|
||||||
@@ -368,10 +420,12 @@ public class CraftingManager implements ICraftingManager {
|
|||||||
|
|
||||||
for (ItemStack output : pattern.getOutputs()) {
|
for (ItemStack output : pattern.getOutputs()) {
|
||||||
network.getItemStorageCache().getCraftablesList().add(output);
|
network.getItemStorageCache().getCraftablesList().add(output);
|
||||||
|
this.itemPatternsByOutput.put(new ItemStackKey(output), pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (FluidStack output : pattern.getFluidOutputs()) {
|
for (FluidStack output : pattern.getFluidOutputs()) {
|
||||||
network.getFluidStorageCache().getCraftablesList().add(output);
|
network.getFluidStorageCache().getCraftablesList().add(output);
|
||||||
|
this.fluidPatternsByOutput.put(new FluidStackKey(output), pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<ICraftingPatternContainer> containersForPattern = this.patternToContainer.computeIfAbsent(pattern, key -> new LinkedHashSet<>());
|
Set<ICraftingPatternContainer> containersForPattern = this.patternToContainer.computeIfAbsent(pattern, key -> new LinkedHashSet<>());
|
||||||
@@ -410,28 +464,12 @@ public class CraftingManager implements ICraftingManager {
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public ICraftingPattern getPattern(ItemStack pattern) {
|
public ICraftingPattern getPattern(ItemStack pattern) {
|
||||||
for (ICraftingPattern patternInList : patterns) {
|
return itemPatternsByOutput.get(new ItemStackKey(pattern));
|
||||||
for (ItemStack output : patternInList.getOutputs()) {
|
|
||||||
if (API.instance().getComparer().isEqualNoQuantity(output, pattern)) {
|
|
||||||
return patternInList;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public ICraftingPattern getPattern(FluidStack pattern) {
|
public ICraftingPattern getPattern(FluidStack pattern) {
|
||||||
for (ICraftingPattern patternInList : patterns) {
|
return fluidPatternsByOutput.get(new FluidStackKey(pattern));
|
||||||
for (FluidStack output : patternInList.getFluidOutputs()) {
|
|
||||||
if (API.instance().getComparer().isEqual(output, pattern, IComparer.COMPARE_NBT)) {
|
|
||||||
return patternInList;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user