would you look at that, autocrafting is back

This commit is contained in:
way2muchnoise
2016-11-27 22:11:57 +01:00
parent 8e16cc5f65
commit 77ebfeea4a
15 changed files with 104 additions and 82 deletions

View File

@@ -44,18 +44,18 @@ public interface IItemGridHandler {
/**
* Called when a player requests the crafting preview window to be opened.
*
* @param hash the hash of the item we want a preview for, see {@link IRSAPI#getItemStackHashCode(ItemStack)}
* @param stack the {@link ItemStack} to request a craft for
* @param quantity the amount of that item that we need a preview for
*/
void onCraftingPreviewRequested(EntityPlayerMP player, int hash, int quantity);
void onCraftingPreviewRequested(EntityPlayerMP player, ItemStack stack, int quantity);
/**
* Called when a player requested crafting for an item.
*
* @param hash the hash of the item we're requesting crafting for, see {@link IRSAPI#getItemStackHashCode(ItemStack)}
* @param stack the {@link ItemStack} to request a craft for
* @param quantity the amount of that item that has to be crafted
*/
void onCraftingRequested(int hash, int quantity);
void onCraftingRequested(ItemStack stack, int quantity);
/**
* Called when a player wants to cancel a crafting task.

View File

@@ -34,20 +34,18 @@ public interface IItemStackList {
*
* @param stack the stack
* @param size the size to remove
* @param removeIfReachedZero true to remove the stack if the count reaches 0, false otherwise
* @return whether the remove was successful for the full amount
*/
boolean remove(@Nonnull ItemStack stack, int size, boolean removeIfReachedZero);
boolean remove(@Nonnull ItemStack stack, int size);
/**
* Decrements the count of that stack in the list.
*
* @param stack the stack
* @param removeIfReachedZero true to remove the stack if the count reaches 0, false otherwise
* @return whether the remove was successful for the full amount
*/
default boolean remove(@Nonnull ItemStack stack, boolean removeIfReachedZero) {
return remove(stack, stack.getCount(), removeIfReachedZero);
default boolean remove(@Nonnull ItemStack stack) {
return remove(stack, stack.getCount());
}
/**
@@ -56,21 +54,19 @@ public interface IItemStackList {
*
* @param stack the stack
* @param size the size to remove
* @param removeIfReachedZero true to remove the stack if the count reaches 0, false otherwise
* @return whether the remove was successful for the full amount
*/
boolean trackedRemove(@Nonnull ItemStack stack, int size, boolean removeIfReachedZero);
boolean trackedRemove(@Nonnull ItemStack stack, int size);
/**
* Decrements the count of that stack in the list.
* Keeps track of remove items and can be undone by calling {@link #undo()}
*
* @param stack the stack
* @param removeIfReachedZero true to remove the stack if the count reaches 0, false otherwise
* @return whether the remove was successful for the full amount
*/
default boolean trackedRemove(@Nonnull ItemStack stack, boolean removeIfReachedZero) {
return trackedRemove(stack, stack.getCount(), removeIfReachedZero);
default boolean trackedRemove(@Nonnull ItemStack stack) {
return trackedRemove(stack, stack.getCount());
}
/**

View File

@@ -204,8 +204,10 @@ public class CraftingPattern implements ICraftingPattern {
}, 3, 3);
for (int i = 0; i < 9; ++i) {
if (took[i] != null) {
inv.setInventorySlotContents(i, took[i]);
}
}
ItemStack cleaned = recipe.getCraftingResult(inv).copy();
if (mekanism && cleaned.hasTagCompound()) {
@@ -233,8 +235,10 @@ public class CraftingPattern implements ICraftingPattern {
}, 3, 3);
for (int i = 0; i < 9; ++i) {
if (took[i] != null) {
inv.setInventorySlotContents(i, took[i]);
}
}
for (ItemStack remaining : recipe.getRemainingItems(inv)) {
if (remaining != null) {

View File

@@ -163,13 +163,13 @@ public abstract class CraftingStep implements ICraftingStep {
}
protected AvailableType isItemAvailable(IItemStackList items, IFluidStackList fluids, ItemStack stack, ItemStack actualStack, int compare) {
if (actualStack == null || actualStack.getCount() == 0 || !items.trackedRemove(actualStack, stack.getCount(), true)) {
if (actualStack == null || actualStack.getCount() == 0 || !items.trackedRemove(actualStack, stack.getCount())) {
FluidStack fluidInItem = RSUtils.getFluidFromStack(stack, true).getValue();
if (fluidInItem != null && RSUtils.hasFluidBucket(fluidInItem)) {
FluidStack fluidStack = fluids.get(fluidInItem, compare);
ItemStack bucket = items.get(RSUtils.EMPTY_BUCKET, compare);
if (bucket != null && fluidStack != null && fluids.trackedRemove(fluidStack, fluidInItem.amount, true) && items.trackedRemove(bucket, 1, true)) {
if (bucket != null && fluidStack != null && fluids.trackedRemove(fluidStack, fluidInItem.amount, true) && items.trackedRemove(bucket, 1)) {
return AvailableType.FLUID;
}
}

View File

@@ -163,7 +163,7 @@ public class CraftingTask implements ICraftingTask {
if (!inputStack.isItemStackDamageable() || !inputStack.isItemDamaged()) {
toCraft.add(inputStack);
}
toInsert.remove(inputStack, true);
toInsert.remove(inputStack);
if (input.getCount() > 0) {
extraStack = toInsert.get(input, compare);
}
@@ -173,7 +173,7 @@ public class CraftingTask implements ICraftingTask {
toTake.add(inputStack.copy());
actualInputs.add(inputStack.copy());
input.shrink(takeQuantity);
networkList.remove(inputStack, true);
networkList.remove(inputStack);
if (input.getCount() > 0) {
networkStack = networkList.get(inputStack, compare);
}
@@ -194,7 +194,7 @@ public class CraftingTask implements ICraftingTask {
// Calculate added all the crafted outputs toInsert
// So we remove the ones we use from toInsert
ItemStack inserted = toInsert.get(inputCrafted, compare);
toInsert.remove(inserted, craftQuantity, true);
toInsert.remove(inserted, craftQuantity);
}
} else {
// Fluid checks are with a stack size of one
@@ -250,12 +250,12 @@ public class CraftingTask implements ICraftingTask {
ItemStack bucket = toInsert.get(RSUtils.EMPTY_BUCKET);
boolean hasBucket = false;
if (bucket != null && bucket.getCount() > 0) {
hasBucket = toInsert.remove(RSUtils.EMPTY_BUCKET, 1, false);
hasBucket = toInsert.remove(RSUtils.EMPTY_BUCKET, 1);
}
if (!hasBucket) {
bucket = networkList.get(RSUtils.EMPTY_BUCKET);
if (bucket != null && bucket.getCount() > 0) {
hasBucket = networkList.remove(RSUtils.EMPTY_BUCKET, 1, false);
hasBucket = networkList.remove(RSUtils.EMPTY_BUCKET, 1);
}
}
@@ -267,7 +267,7 @@ public class CraftingTask implements ICraftingTask {
} else {
toCraft.add(RSUtils.EMPTY_BUCKET.copy());
calculate(networkList, networkFluidList, bucketPattern, toInsert);
toInsert.remove(RSUtils.EMPTY_BUCKET, 1, false);
toInsert.remove(RSUtils.EMPTY_BUCKET, 1);
}
}
@@ -311,7 +311,7 @@ public class CraftingTask implements ICraftingTask {
if (!missing.isEmpty()) {
for (ItemStack missing : this.missing.getStacks()) {
if (!oreDictPrepped.trackedRemove(missing, true)) {
if (!oreDictPrepped.trackedRemove(missing)) {
oreDictPrepped.undo();
return false;
}

View File

@@ -2,6 +2,7 @@ package com.raoulvdberge.refinedstorage.apiimpl.network.grid;
import com.raoulvdberge.refinedstorage.RS;
import com.raoulvdberge.refinedstorage.RSUtils;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPattern;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingTask;
import com.raoulvdberge.refinedstorage.api.network.INetworkMaster;
import com.raoulvdberge.refinedstorage.api.network.grid.IItemGridHandler;
@@ -141,16 +142,14 @@ public class ItemGridHandler implements IItemGridHandler {
}
@Override
public void onCraftingPreviewRequested(EntityPlayerMP player, int hash, int quantity) {
ItemStack stack = network.getItemStorageCache().getList().get(hash);
public void onCraftingPreviewRequested(EntityPlayerMP player, ItemStack stack, int quantity) {
if (stack != null) {
Thread calculationThread = new Thread(() -> {
ICraftingTask task = new CraftingTask(network, stack, network.getPattern(stack), quantity);
task.calculate();
RS.INSTANCE.network.sendTo(new MessageGridCraftingPreviewResponse(task.getPreviewStacks(), hash, quantity), player);
RS.INSTANCE.network.sendTo(new MessageGridCraftingPreviewResponse(task.getPreviewStacks(), stack, quantity), player);
}, "RS crafting calculation");
calculationThread.start();
@@ -158,13 +157,11 @@ public class ItemGridHandler implements IItemGridHandler {
}
@Override
public void onCraftingRequested(int hash, int quantity) {
public void onCraftingRequested(ItemStack stack, int quantity) {
if (quantity <= 0) {
return;
}
ItemStack stack = network.getItemStorageCache().getList().get(hash);
if (stack != null) {
ICraftingTask task = new CraftingTask(network, stack, network.getPattern(stack), quantity);

View File

@@ -58,7 +58,7 @@ public class ItemStorageCache implements IItemStorageCache {
@Override
public synchronized void remove(@Nonnull ItemStack stack, int size) {
if (list.remove(stack, size, true)) {
if (list.remove(stack, size)) {
network.sendItemStorageDeltaToClient(stack, -size);
}
}

View File

@@ -7,6 +7,7 @@ import com.raoulvdberge.refinedstorage.apiimpl.API;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemHandlerHelper;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -18,6 +19,7 @@ import java.util.stream.Collectors;
public class ItemStackList implements IItemStackList {
private ArrayListMultimap<Item, ItemStack> stacks = ArrayListMultimap.create();
private List<ItemStack> removeTracker = new LinkedList<>();
protected boolean needsCleanup = false;
@Override
public void add(@Nonnull ItemStack stack, int size) {
@@ -37,16 +39,18 @@ public class ItemStackList implements IItemStackList {
}
@Override
public boolean remove(@Nonnull ItemStack stack, int size, boolean removeIfReachedZero) {
public boolean remove(@Nonnull ItemStack stack, int size) {
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (API.instance().getComparer().isEqualNoQuantity(otherStack, stack)) {
if (otherStack.getCount() - size <= 0 && removeIfReachedZero) {
stacks.remove(otherStack.getItem(), otherStack);
} else {
boolean success = otherStack.getCount() - size >= 0;
otherStack.shrink(size);
if (otherStack.isEmpty()) {
needsCleanup = true;
}
return true;
return success;
}
}
@@ -54,18 +58,17 @@ public class ItemStackList implements IItemStackList {
}
@Override
public boolean trackedRemove(@Nonnull ItemStack stack, int size, boolean removeIfReachedZero) {
public boolean trackedRemove(@Nonnull ItemStack stack, int size) {
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (API.instance().getComparer().isEqualNoQuantity(otherStack, stack)) {
ItemStack removed = ItemHandlerHelper.copyStackWithSize(otherStack, Math.min(size, otherStack.getCount()));
this.removeTracker.add(removed);
boolean success = otherStack.getCount() - size >= 0;
otherStack.shrink(size);
boolean success = otherStack.getCount() >= 0;
if (otherStack.getCount() <= 0 && removeIfReachedZero) {
stacks.remove(otherStack.getItem(), otherStack);
if (otherStack.isEmpty()) {
needsCleanup = true;
}
return success;
@@ -102,6 +105,10 @@ public class ItemStackList implements IItemStackList {
@Override
@Nullable
public ItemStack get(int hash) {
if (needsCleanup) {
clean();
}
for (ItemStack stack : this.stacks.values()) {
if (API.instance().getItemStackHashCode(stack) == hash) {
return stack;
@@ -118,11 +125,14 @@ public class ItemStackList implements IItemStackList {
@Override
public void clean() {
List<ItemStack> toRemove = stacks.values().stream()
.filter(stack -> stack.getCount() <= 0)
List<Pair<Item, ItemStack>> toRemove = stacks.asMap().entrySet().stream()
.flatMap(entry -> entry.getValue().stream().map(value -> Pair.of(entry.getKey(), value)))
.filter(pair -> pair.getValue().getCount() <= 0)
.collect(Collectors.toList());
toRemove.forEach(stack -> stacks.remove(stack.getItem(), stack));
toRemove.forEach(pair -> stacks.remove(pair.getLeft(), pair.getRight()));
needsCleanup = false;
}
@Override
@@ -133,6 +143,9 @@ public class ItemStackList implements IItemStackList {
@Nonnull
@Override
public Collection<ItemStack> getStacks() {
if (needsCleanup) {
clean();
}
return stacks.values();
}
@@ -141,6 +154,10 @@ public class ItemStackList implements IItemStackList {
public IItemStackList copy() {
ItemStackList list = new ItemStackList();
if (needsCleanup) {
clean();
}
for (ItemStack stack : stacks.values()) {
list.stacks.put(stack.getItem(), stack.copy());
}
@@ -151,6 +168,9 @@ public class ItemStackList implements IItemStackList {
@Nonnull
@Override
public IItemStackList getOredicted() {
if (needsCleanup) {
clean();
}
return new ItemStackListOredicted(this);
}
@@ -173,7 +193,7 @@ public class ItemStackList implements IItemStackList {
ItemStack actualInput = list.get(input, compare);
ItemStack taken = ItemHandlerHelper.copyStackWithSize(actualInput, input.getCount());
took[i] = taken;
list.remove(taken, true);
list.remove(taken);
}
}
return took;

View File

@@ -14,13 +14,13 @@ import java.util.Map;
import java.util.stream.Collectors;
public class ItemStackListOredicted implements IItemStackList {
private IItemStackList underlyingList;
private ItemStackList underlyingList;
private ArrayListMultimap<Integer, ItemStack> stacks = ArrayListMultimap.create();
private ItemStackListOredicted() {
}
public ItemStackListOredicted(IItemStackList list) {
public ItemStackListOredicted(ItemStackList list) {
this.underlyingList = list;
initOreDict();
}
@@ -45,19 +45,19 @@ public class ItemStackListOredicted implements IItemStackList {
}
@Override
public boolean remove(@Nonnull ItemStack stack, int size, boolean removeIfReachedZero) {
boolean rvalue = underlyingList.remove(stack, size, removeIfReachedZero);
if (removeIfReachedZero) {
localClean();
public boolean remove(@Nonnull ItemStack stack, int size) {
boolean rvalue = underlyingList.remove(stack, size);
if (underlyingList.needsCleanup) {
clean();
}
return rvalue;
}
@Override
public boolean trackedRemove(@Nonnull ItemStack stack, int size, boolean removeIfReachedZero) {
boolean rvalue = underlyingList.trackedRemove(stack, size, removeIfReachedZero);
if (removeIfReachedZero) {
localClean();
public boolean trackedRemove(@Nonnull ItemStack stack, int size) {
boolean rvalue = underlyingList.trackedRemove(stack, size);
if (underlyingList.needsCleanup) {
clean();
}
return rvalue;
}
@@ -139,7 +139,7 @@ public class ItemStackListOredicted implements IItemStackList {
@Override
public IItemStackList copy() {
ItemStackListOredicted newList = new ItemStackListOredicted();
newList.underlyingList = this.underlyingList.copy();
newList.underlyingList = (ItemStackList) this.underlyingList.copy();
for (Map.Entry<Integer, ItemStack> entry : this.stacks.entries()) {
newList.stacks.put(entry.getKey(), entry.getValue());
}

View File

@@ -42,7 +42,7 @@ public class GuiCraftingPreview extends GuiBase {
private List<ICraftingPreviewElement> stacks;
private GuiScreen parent;
private int hash;
private ItemStack stack;
private int quantity;
private GuiButton startButton;
@@ -50,7 +50,7 @@ public class GuiCraftingPreview extends GuiBase {
private IElementDrawers drawers = new CraftingPreviewElementDrawers();
public GuiCraftingPreview(GuiScreen parent, List<ICraftingPreviewElement> stacks, int hash, int quantity) {
public GuiCraftingPreview(GuiScreen parent, List<ICraftingPreviewElement> stacks, ItemStack stack, int quantity) {
super(new Container() {
@Override
public boolean canInteractWith(EntityPlayer player) {
@@ -61,7 +61,7 @@ public class GuiCraftingPreview extends GuiBase {
this.stacks = new ArrayList<>(stacks);
this.parent = parent;
this.hash = hash;
this.stack = stack;
this.quantity = quantity;
this.scrollbar = new Scrollbar(149, 20, 12, 119);
@@ -180,7 +180,7 @@ public class GuiCraftingPreview extends GuiBase {
}
private void startRequest() {
RS.INSTANCE.network.sendToServer(new MessageGridCraftingStart(hash, quantity));
RS.INSTANCE.network.sendToServer(new MessageGridCraftingStart(stack, quantity));
close();
}

View File

@@ -176,7 +176,7 @@ public class GuiCraftingStart extends GuiBase {
Integer quantity = Ints.tryParse(amountField.getText());
if (quantity != null && quantity > 0) {
RS.INSTANCE.network.sendToServer(new MessageGridCraftingPreview(stack.getHash(), quantity));
RS.INSTANCE.network.sendToServer(new MessageGridCraftingPreview(stack.getStack(), quantity));
startButton.enabled = false;
}

View File

@@ -4,29 +4,31 @@ import com.raoulvdberge.refinedstorage.container.ContainerGrid;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
public class MessageGridCraftingPreview extends MessageHandlerPlayerToServer<MessageGridCraftingPreview> implements IMessage {
private int hash;
private ItemStack stack;
private int quantity;
public MessageGridCraftingPreview() {
}
public MessageGridCraftingPreview(int hash, int quantity) {
this.hash = hash;
public MessageGridCraftingPreview(ItemStack stack, int quantity) {
this.stack = stack;
this.quantity = quantity;
}
@Override
public void fromBytes(ByteBuf buf) {
hash = buf.readInt();
stack = ByteBufUtils.readItemStack(buf);
quantity = buf.readInt();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(hash);
ByteBufUtils.writeItemStack(buf, stack);
buf.writeInt(quantity);
}
@@ -35,7 +37,7 @@ public class MessageGridCraftingPreview extends MessageHandlerPlayerToServer<Mes
Container container = player.openContainer;
if (container instanceof ContainerGrid) {
((ContainerGrid) container).getGrid().getItemHandler().onCraftingPreviewRequested(player, message.hash, message.quantity);
((ContainerGrid) container).getGrid().getItemHandler().onCraftingPreviewRequested(player, message.stack, message.quantity);
}
}
}

View File

@@ -4,6 +4,7 @@ import com.raoulvdberge.refinedstorage.api.autocrafting.preview.ICraftingPreview
import com.raoulvdberge.refinedstorage.apiimpl.API;
import com.raoulvdberge.refinedstorage.proxy.ProxyClient;
import io.netty.buffer.ByteBuf;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
@@ -14,21 +15,21 @@ import java.util.List;
public class MessageGridCraftingPreviewResponse implements IMessage, IMessageHandler<MessageGridCraftingPreviewResponse, IMessage> {
public List<ICraftingPreviewElement> stacks;
public int hash;
public ItemStack stack;
public int quantity;
public MessageGridCraftingPreviewResponse() {
}
public MessageGridCraftingPreviewResponse(List<ICraftingPreviewElement> stacks, int hash, int quantity) {
public MessageGridCraftingPreviewResponse(List<ICraftingPreviewElement> stacks, ItemStack stack, int quantity) {
this.stacks = stacks;
this.hash = hash;
this.stack = stack;
this.quantity = quantity;
}
@Override
public void fromBytes(ByteBuf buf) {
this.hash = buf.readInt();
this.stack = ByteBufUtils.readItemStack(buf);
this.quantity = buf.readInt();
this.stacks = new LinkedList<>();
@@ -42,7 +43,7 @@ public class MessageGridCraftingPreviewResponse implements IMessage, IMessageHan
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(this.hash);
ByteBufUtils.writeItemStack(buf, this.stack);
buf.writeInt(this.quantity);
buf.writeInt(stacks.size());

View File

@@ -5,29 +5,31 @@ import com.raoulvdberge.refinedstorage.container.ContainerGrid;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
public class MessageGridCraftingStart extends MessageHandlerPlayerToServer<MessageGridCraftingStart> implements IMessage {
private int hash;
private ItemStack stack;
private int quantity;
public MessageGridCraftingStart() {
}
public MessageGridCraftingStart(int hash, int quantity) {
this.hash = hash;
public MessageGridCraftingStart(ItemStack stack, int quantity) {
this.stack = stack;
this.quantity = quantity;
}
@Override
public void fromBytes(ByteBuf buf) {
hash = buf.readInt();
stack = ByteBufUtils.readItemStack(buf);
quantity = buf.readInt();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(hash);
ByteBufUtils.writeItemStack(buf, stack);
buf.writeInt(quantity);
}
@@ -39,7 +41,7 @@ public class MessageGridCraftingStart extends MessageHandlerPlayerToServer<Messa
IItemGridHandler handler = ((ContainerGrid) container).getGrid().getItemHandler();
if (handler != null) {
handler.onCraftingRequested(message.hash, message.quantity);
handler.onCraftingRequested(message.stack, message.quantity);
}
}
}

View File

@@ -254,7 +254,7 @@ public class ProxyClient extends ProxyCommon {
screen = ((GuiCraftingStart) screen).getParent();
}
FMLCommonHandler.instance().showGuiScreen(new GuiCraftingPreview(screen, message.stacks, message.hash, message.quantity));
FMLCommonHandler.instance().showGuiScreen(new GuiCraftingPreview(screen, message.stacks, message.stack, message.quantity));
});
}