Compare commits

..

1 Commits

Author SHA1 Message Date
PrototypeTrousers da39d81d5f optimize JEI transfer handler so it doesnt lock up the client 2023-06-26 16:13:33 -03:00
4 changed files with 53 additions and 66 deletions
@@ -32,14 +32,13 @@ import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketInformPlayer;
import appeng.me.cluster.implementations.CraftingCPUCluster;
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.Optional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Collection;
import java.util.List;
public class CraftingTreeNode {
@@ -112,16 +111,14 @@ public class CraftingTreeNode {
this.what.setStackSize(l);
if (this.getSlot() >= 0 && this.parent != null && this.parent.details.isCraftable()) {
LinkedList<IAEItemStack> itemList = new LinkedList<>();
Collection<IAEItemStack> itemList = new ArrayList<>();
boolean damageableItem = this.what.getItem().isDamageable() || Platform.isGTDamageableItem(this.what.getItem());
if (this.parent.details.canSubstitute()) {
for (IAEItemStack subs : this.parent.details.getSubstituteInputs(this.slot)) {
if (damageableItem) {
for (IAEItemStack i : inventoryList.findFuzzy(subs, FuzzyMode.IGNORE_ALL)) {
itemList.add(i);
}
itemList.addAll(inventoryList.findFuzzy(subs, FuzzyMode.IGNORE_ALL));
}
subs = inventoryList.findPrecise(subs);
if (subs != null) {
@@ -130,9 +127,7 @@ public class CraftingTreeNode {
}
} else {
if (damageableItem) {
for (IAEItemStack i : inventoryList.findFuzzy(this.what, FuzzyMode.IGNORE_ALL)) {
itemList.add(i);
}
itemList.addAll(inventoryList.findFuzzy(this.what, FuzzyMode.IGNORE_ALL));
} else {
final IAEItemStack item = inventoryList.findPrecise(this.what);
if (item != null) {
@@ -142,25 +137,13 @@ public class CraftingTreeNode {
}
for (IAEItemStack fuzz : itemList) {
if (fuzz.getStackSize() == 0) {
continue;
}
if (this.parent.details.isValidItemForSlot(this.getSlot(), fuzz.getDefinition(), this.world)) {
if (this.parent.details.isValidItemForSlot(this.getSlot(), fuzz.copy().getCachedItemStack(1), this.world)) {
fuzz = fuzz.copy();
fuzz.setStackSize(l);
final IAEItemStack available = inv.extractItems(fuzz, Actionable.MODULATE, src);
if (available != null) {
if (available.getItem().hasContainerItem(available.getDefinition())) {
final ItemStack is2 = Platform.getContainerItem(available.createItemStack());
final IAEItemStack o = AEItemStack.fromItemStack(is2);
if (o != null) {
this.parent.addContainers(o);
}
}
if (!this.exhausted) {
final IAEItemStack is = this.job.checkUse(available);
@@ -214,16 +197,6 @@ public class CraftingTreeNode {
final IAEItemStack available = inv.extractItems(madeWhat, Actionable.MODULATE, src);
if (available != null) {
if (parent != null && available.getItem().hasContainerItem(available.getDefinition())) {
final ItemStack is2 = Platform.getContainerItem(available.createItemStack());
final IAEItemStack o = AEItemStack.fromItemStack(is2);
if (o != null) {
this.parent.addContainers(o);
}
}
this.bytes += available.getStackSize();
l -= available.getStackSize();
@@ -267,14 +240,6 @@ public class CraftingTreeNode {
if (job.isSimulation()) {
this.bytes += l;
if (parent != null && this.what.getItem().hasContainerItem(this.what.getDefinition())) {
final ItemStack is2 = Platform.getContainerItem(this.what.copy().setStackSize(1).createItemStack());
final IAEItemStack o = AEItemStack.fromItemStack(is2);
if (o != null) {
this.parent.addContainers(o);
}
}
this.missing += l;
final IAEItemStack rv = this.what.copy();
rv.setStackSize(l);
@@ -27,11 +27,15 @@ import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.core.AEConfig;
import appeng.me.cluster.implementations.CraftingCPUCluster;
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
import com.google.common.collect.ImmutableCollection;
import it.unimi.dsi.fastutil.objects.Object2LongArrayMap;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
@@ -46,7 +50,6 @@ public class CraftingTreeProcess {
boolean possible = true;
private long crafts = 0;
private long bytes = 0;
private ArrayList<IAEItemStack> containers;
public CraftingTreeProcess(final ICraftingGrid cc, final CraftingJob job, final ICraftingPatternDetails details, final CraftingTreeNode craftingTreeNode, final int depth) {
this.parent = craftingTreeNode;
@@ -195,18 +198,32 @@ public class CraftingTreeProcess {
void request(final MECraftingInventory inv, final long amountOfTimes, final IActionSource src) throws CraftBranchFailure, InterruptedException {
addProcess();
this.job.handlePausing();
List<IAEItemStack> containerItems = null;
// request and remove inputs...
for (final Entry<CraftingTreeNode, Long> entry : this.nodes.object2LongEntrySet()) {
final IAEItemStack stack = entry.getKey().request(inv, entry.getValue() * amountOfTimes, src);
if (this.details.isCraftable() && stack.getItem().hasContainerItem(stack.getDefinition())) {
final ItemStack is = Platform.getContainerItem(stack.createItemStack());
final IAEItemStack o = AEItemStack.fromItemStack(is);
if (o != null) {
if (containerItems == null) {
containerItems = new ArrayList<>();
}
this.bytes++;
o.setCachedItemStack(is);
containerItems.add(o);
}
}
}
if (this.containers != null) {
for (IAEItemStack iae : containers) {
inv.injectItems(iae, Actionable.MODULATE, src);
if (containerItems != null) {
for (IAEItemStack i : containerItems) {
inv.injectItems(i, Actionable.MODULATE, src);
}
containers = null;
}
// assume its possible.
// add crafting results..
@@ -218,13 +235,6 @@ public class CraftingTreeProcess {
this.crafts += amountOfTimes;
}
public void addContainers(IAEItemStack container) {
if (this.containers == null) {
this.containers = new ArrayList<>();
}
this.containers.add(container);
}
void dive(final CraftingJob job) {
job.addTask(this.getAmountCrafted(this.parent.getStack(1)), this.crafts, this.details, this.depth);
for (final Entry<CraftingTreeNode, Long> entry : this.nodes.object2LongEntrySet()) {
@@ -74,11 +74,13 @@ public class MultiCraftingTracker {
public boolean handleCrafting(final int x, final long itemToCraft, final IAEItemStack ais, final InventoryAdaptor d, final World w, final IGrid g, final ICraftingGrid cg, final IActionSource mySrc) {
if (ais != null) {
ItemStack inputStack = ais.createItemStack();
ItemStack inputStack = ais.getCachedItemStack(ais.getStackSize());
ItemStack remaining = d.simulateAdd(inputStack);
if (remaining.isEmpty()) {
ais.setCachedItemStack(inputStack);
final Future<ICraftingJob> craftingJob = this.getJob(x);
if (this.getLink(x) != null) {
@@ -115,6 +117,8 @@ public class MultiCraftingTracker {
this.setJob(x, cg.beginCraftingJob(w, g, mySrc, aisC, null));
}
}
} else {
ais.setCachedItemStack(remaining);
}
}
return false;
@@ -47,7 +47,7 @@ public class JEIMissingItem implements IRecipeTransferError {
JEIMissingItem(Container container, @Nonnull IRecipeLayout recipeLayout) {
if (container instanceof ContainerMEMonitorable) {
IItemList<IAEItemStack> ir = ((ContainerMEMonitorable) container).items;
IItemList<IAEItemStack> available = mergeInventories(ir, (ContainerMEMonitorable) container);
boolean found;
@@ -57,7 +57,7 @@ public class JEIMissingItem implements IRecipeTransferError {
IItemList<IAEItemStack> used = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
for (IGuiIngredient<?> i : recipeLayout.getItemStacks().getGuiIngredients().values()) {
found = false;
if (i.isInput() && i.getAllIngredients().size() > 0) {
if (i.isInput() && !i.getAllIngredients().isEmpty()) {
List<?> allIngredients = i.getAllIngredients();
for (Object allIngredient : allIngredients) {
if (allIngredient instanceof ItemStack) {
@@ -66,7 +66,7 @@ public class JEIMissingItem implements IRecipeTransferError {
IAEItemStack search = AEItemStack.fromItemStack(stack);
if (stack.getItem().isDamageable() || Platform.isGTDamageableItem(stack.getItem())) {
Collection<IAEItemStack> fuzzy = available.findFuzzy(search, FuzzyMode.IGNORE_ALL);
if (fuzzy.size() > 0) {
if (!fuzzy.isEmpty()) {
for (IAEItemStack itemStack : fuzzy) {
if (itemStack.getStackSize() > 0) {
if (Platform.isGTDamageableItem(stack.getItem())) {
@@ -145,7 +145,7 @@ public class JEIMissingItem implements IRecipeTransferError {
for (IGuiIngredient<?> i : recipeLayout.getItemStacks().getGuiIngredients().values()) {
found = false;
craftable = false;
ArrayList<ItemStack> valid = new ArrayList<>();
IItemList<IAEItemStack> valid = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList();
if (i.isInput()) {
List<?> allIngredients = i.getAllIngredients();
for (Object allIngredient : allIngredients) {
@@ -155,7 +155,7 @@ public class JEIMissingItem implements IRecipeTransferError {
IAEItemStack search = AEItemStack.fromItemStack(stack);
if (stack.getItem().isDamageable() || Platform.isGTDamageableItem(stack.getItem())) {
Collection<IAEItemStack> fuzzy = available.findFuzzy(search, FuzzyMode.IGNORE_ALL);
if (fuzzy.size() > 0) {
if (!fuzzy.isEmpty()) {
for (IAEItemStack itemStack : fuzzy) {
if (itemStack.getStackSize() > 0) {
if (Platform.isGTDamageableItem(stack.getItem())) {
@@ -165,7 +165,7 @@ public class JEIMissingItem implements IRecipeTransferError {
}
found = true;
used.add(itemStack.copy().setStackSize(1));
valid.add(itemStack.copy().setStackSize(1).createItemStack());
valid.add(itemStack.copy().setStackSize(1));
} else {
if (itemStack.isCraftable()) {
craftable = true;
@@ -180,12 +180,12 @@ public class JEIMissingItem implements IRecipeTransferError {
if (ext.getStackSize() > 0 && (usedStack == null || usedStack.getStackSize() < ext.getStackSize())) {
used.add(ext.copy().setStackSize(1));
if (craftable) {
valid.clear();
valid = null;
}
valid.add(ext.copy().setStackSize(1).createItemStack());
valid.add(ext.copy().setStackSize(1));
found = true;
} else if (ext.isCraftable()) {
valid.add(ext.copy().setStackSize(1).createItemStack());
valid.add(ext.copy().setStackSize(1));
craftable = true;
}
}
@@ -195,21 +195,29 @@ public class JEIMissingItem implements IRecipeTransferError {
}
}
}
if (i.getAllIngredients().size() == 0) {
if (i.getAllIngredients().isEmpty()) {
found = true;
}
ArrayList<ItemStack> validStacks = new ArrayList<>();
if (valid != null) {
valid.forEach(v -> {
ItemStack validStack = v.createItemStack();
validStack.setCount(1);
validStacks.add(validStack);
});
}
if (!found) {
if (craftable) {
i.drawHighlight(minecraft, new Color(0.0f, 0.0f, 1.0f, 0.4f), recipeX, recipeY);
this.craftableSlots.add(currentSlot);
recipeLayout.getItemStacks().set(currentSlot, valid);
recipeLayout.getItemStacks().set(currentSlot, validStacks);
} else {
i.drawHighlight(minecraft, new Color(1.0f, 0.0f, 0.0f, 0.4f), recipeX, recipeY);
}
this.errored = true;
} else {
this.foundSlots.add(currentSlot);
recipeLayout.getItemStacks().set(currentSlot, valid);
recipeLayout.getItemStacks().set(currentSlot, validStacks);
}
}
currentSlot++;