rewrite most the autocraft calculations (#472)
* rewrite most the autocraft calculations, mainly to improve the preview generation * fix impl being in the api and general code style * fix code style * remove breakpoint line
This commit is contained in:
@@ -34,7 +34,7 @@ public interface ICraftingPattern {
|
||||
boolean isOredict();
|
||||
|
||||
/**
|
||||
* @return the inputs
|
||||
* @return the inputs, can contain nulls
|
||||
*/
|
||||
List<ItemStack> getInputs();
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package refinedstorage.api.autocrafting.preview;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public interface ICraftingPreviewStack {
|
||||
/**
|
||||
* @return the stack to display
|
||||
*/
|
||||
ItemStack getStack();
|
||||
|
||||
/**
|
||||
* @return available amount of the {@link #getStack()}
|
||||
*/
|
||||
int getAvailable();
|
||||
|
||||
/**
|
||||
* @return toCraft or missing (depends on {@link #hasMissing()} amount of the {@link #getStack()}
|
||||
*/
|
||||
int getToCraft();
|
||||
|
||||
/**
|
||||
* When this is true {@link #getToCraft()} will be the missing items
|
||||
*
|
||||
* @return true when items are missing
|
||||
*/
|
||||
boolean hasMissing();
|
||||
|
||||
/**
|
||||
* @param buf byte buf to write to
|
||||
*/
|
||||
void writeToByteBuf(ByteBuf buf);
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package refinedstorage.api.autocrafting.task;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import refinedstorage.api.autocrafting.ICraftingPattern;
|
||||
import refinedstorage.api.autocrafting.craftingmonitor.ICraftingMonitorElement;
|
||||
import refinedstorage.api.autocrafting.preview.ICraftingPreviewStack;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -27,6 +28,7 @@ public interface ICraftingTask {
|
||||
|
||||
/**
|
||||
* Updates this task. Gets called every few ticks, depending on the speed of the pattern container.
|
||||
* {@link ICraftingTask#calculate()} must be run before this
|
||||
*
|
||||
* @return true if this crafting task is finished and can be deleted from the list, false otherwise
|
||||
*/
|
||||
@@ -46,6 +48,8 @@ public interface ICraftingTask {
|
||||
NBTTagCompound writeToNBT(NBTTagCompound tag);
|
||||
|
||||
/**
|
||||
* {@link ICraftingTask#calculate()} must be run before this
|
||||
*
|
||||
* @return the elements of this task for display in the crafting monitor
|
||||
*/
|
||||
List<ICraftingMonitorElement> getCraftingMonitorElements();
|
||||
@@ -56,7 +60,25 @@ public interface ICraftingTask {
|
||||
ICraftingPattern getPattern();
|
||||
|
||||
/**
|
||||
* {@link ICraftingTask#calculate()} must be run before this
|
||||
*
|
||||
* @return the processable items in this task
|
||||
*/
|
||||
List<IProcessable> getToProcess();
|
||||
|
||||
/**
|
||||
* Used to check if the crafting task has recursive elements
|
||||
* (eg. block needs 9 ingots, ingots are crafted by a block)
|
||||
* {@link ICraftingTask#calculate()} must be run before this
|
||||
*
|
||||
* @return true if no recursion was found
|
||||
*/
|
||||
boolean isValid();
|
||||
|
||||
/**
|
||||
* {@link ICraftingTask#calculate()} must be run before this
|
||||
*
|
||||
* @return get a list of {@link ICraftingPreviewStack}s
|
||||
*/
|
||||
List<ICraftingPreviewStack> getPreviewStacks();
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import refinedstorage.item.ItemPattern;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class CraftingPattern implements ICraftingPattern {
|
||||
private World world;
|
||||
@@ -38,14 +39,8 @@ public class CraftingPattern implements ICraftingPattern {
|
||||
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
ItemStack slot = ItemPattern.getSlot(stack, i);
|
||||
|
||||
if (slot != null) {
|
||||
for (int j = 0; j < slot.stackSize; ++j) {
|
||||
inputs.add(ItemHandlerHelper.copyStackWithSize(slot, 1));
|
||||
}
|
||||
|
||||
inv.setInventorySlotContents(i, slot);
|
||||
}
|
||||
inputs.add(slot);
|
||||
inv.setInventorySlotContents(i, slot);
|
||||
}
|
||||
|
||||
if (!ItemPattern.isProcessing(stack)) {
|
||||
@@ -77,7 +72,7 @@ public class CraftingPattern implements ICraftingPattern {
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return !inputs.isEmpty() && !outputs.isEmpty();
|
||||
return inputs.stream().filter(Objects::nonNull).count() > 0 && !outputs.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package refinedstorage.apiimpl.autocrafting.preview;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.items.ItemHandlerHelper;
|
||||
import refinedstorage.api.autocrafting.preview.ICraftingPreviewStack;
|
||||
|
||||
public class CraftingPreviewStack implements ICraftingPreviewStack {
|
||||
private ItemStack stack;
|
||||
private int available;
|
||||
private boolean missing;
|
||||
private int toCraft;
|
||||
// if missing is true then toCraft is the missing amount
|
||||
|
||||
public CraftingPreviewStack(ItemStack stack) {
|
||||
this.stack = ItemHandlerHelper.copyStackWithSize(stack, 1);
|
||||
}
|
||||
|
||||
public CraftingPreviewStack(ItemStack stack, int available, boolean missing, int toCraft) {
|
||||
this.stack = stack;
|
||||
this.available = available;
|
||||
this.missing = missing;
|
||||
this.toCraft = toCraft;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToByteBuf(ByteBuf buf) {
|
||||
buf.writeInt(Item.getIdFromItem(stack.getItem()));
|
||||
buf.writeInt(stack.getMetadata());
|
||||
buf.writeInt(available);
|
||||
buf.writeBoolean(missing);
|
||||
buf.writeInt(toCraft);
|
||||
}
|
||||
|
||||
public static CraftingPreviewStack fromByteBuf(ByteBuf buf) {
|
||||
Item item = Item.getItemById(buf.readInt());
|
||||
int meta = buf.readInt();
|
||||
int available = buf.readInt();
|
||||
boolean missing = buf.readBoolean();
|
||||
int toCraft = buf.readInt();
|
||||
|
||||
return new CraftingPreviewStack(new ItemStack(item, 1, meta), available, missing, toCraft);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStack() {
|
||||
return stack;
|
||||
}
|
||||
|
||||
public void addAvailable(int amount) {
|
||||
this.available += amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailable() {
|
||||
return available;
|
||||
}
|
||||
|
||||
public void addToCraft(int amount) {
|
||||
this.toCraft += amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getToCraft() {
|
||||
return this.toCraft;
|
||||
}
|
||||
|
||||
public void setMissing(boolean missing) {
|
||||
this.missing = missing;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMissing() {
|
||||
return missing;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import net.minecraftforge.items.ItemHandlerHelper;
|
||||
import refinedstorage.RSUtils;
|
||||
import refinedstorage.api.autocrafting.ICraftingPattern;
|
||||
import refinedstorage.api.autocrafting.craftingmonitor.ICraftingMonitorElement;
|
||||
import refinedstorage.api.autocrafting.preview.ICraftingPreviewStack;
|
||||
import refinedstorage.api.autocrafting.task.ICraftingTask;
|
||||
import refinedstorage.api.autocrafting.task.IProcessable;
|
||||
import refinedstorage.api.network.INetworkMaster;
|
||||
@@ -18,11 +19,9 @@ import refinedstorage.apiimpl.API;
|
||||
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementFluidRender;
|
||||
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementItemRender;
|
||||
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementText;
|
||||
import refinedstorage.apiimpl.autocrafting.preview.CraftingPreviewStack;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CraftingTask implements ICraftingTask {
|
||||
@@ -32,10 +31,11 @@ public class CraftingTask implements ICraftingTask {
|
||||
private int quantity;
|
||||
private List<IProcessable> toProcess = new ArrayList<>();
|
||||
private IItemStackList toTake = API.instance().createItemStackList();
|
||||
private IFluidStackList toTakeFluids = API.instance().createFluidStackList();
|
||||
private IItemStackList toCraft = API.instance().createItemStackList();
|
||||
private IFluidStackList toTakeFluids = API.instance().createFluidStackList();
|
||||
private IItemStackList missing = API.instance().createItemStackList();
|
||||
private IItemStackList extras = API.instance().createItemStackList();
|
||||
private Set<ICraftingPattern> usedPatterns = new HashSet<>();
|
||||
private boolean recurseFound = false;
|
||||
private Deque<ItemStack> toInsert = new ArrayDeque<>();
|
||||
private int compare = IComparer.COMPARE_DAMAGE | IComparer.COMPARE_NBT;
|
||||
private List<ItemStack> took = new ArrayList<>();
|
||||
@@ -52,109 +52,132 @@ public class CraftingTask implements ICraftingTask {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void calculate() {
|
||||
IItemStackList list = network.getItemStorageCache().getList().copy();
|
||||
IItemStackList networkList = network.getItemStorageCache().getList().copy();
|
||||
IItemStackList toInsert = API.instance().createItemStackList();
|
||||
|
||||
int newQuantity = quantity;
|
||||
|
||||
while (newQuantity > 0) {
|
||||
calculate(list, pattern, true);
|
||||
|
||||
for (ItemStack output : pattern.getOutputs()) {
|
||||
toInsert.add(output.copy());
|
||||
}
|
||||
|
||||
newQuantity -= requested == null ? newQuantity : pattern.getQuantityPerRequest(requested);
|
||||
toCraft.add(ItemHandlerHelper.copyStackWithSize(requested, quantity));
|
||||
int quantity = this.quantity;
|
||||
while (quantity > 0 && !recurseFound) {
|
||||
calculate(networkList, pattern, toInsert);
|
||||
quantity -= pattern.getQuantityPerRequest(requested);
|
||||
}
|
||||
|
||||
for (ItemStack extra : extras.getStacks()) {
|
||||
toInsert.add(extra);
|
||||
if (!recurseFound) {
|
||||
this.toInsert.addAll(toInsert.getStacks());
|
||||
}
|
||||
usedPatterns.clear();
|
||||
}
|
||||
|
||||
private void calculate(IItemStackList list, ICraftingPattern pattern, boolean basePattern) {
|
||||
private void calculate(IItemStackList networkList, ICraftingPattern pattern, IItemStackList toInsert) {
|
||||
recurseFound = !usedPatterns.add(pattern);
|
||||
if (recurseFound) {
|
||||
return;
|
||||
}
|
||||
|
||||
ItemStack[] took = new ItemStack[9];
|
||||
|
||||
if (pattern.isProcessing()) {
|
||||
toProcess.add(new Processable(pattern));
|
||||
}
|
||||
|
||||
if (!basePattern) {
|
||||
pattern.getOutputs().stream()
|
||||
.filter(o -> o.stackSize > 1)
|
||||
.forEach(o -> extras.add(ItemHandlerHelper.copyStackWithSize(o, o.stackSize - 1)));
|
||||
}
|
||||
|
||||
for (int i = 0; i < pattern.getInputs().size(); ++i) {
|
||||
ItemStack input = pattern.getInputs().get(i);
|
||||
|
||||
ItemStack inputInNetwork = list.get(input, compare);
|
||||
|
||||
if (inputInNetwork == null || inputInNetwork.stackSize == 0) {
|
||||
ItemStack extra = extras.get(input, compare);
|
||||
|
||||
if (extra != null) {
|
||||
ItemStack extraToRemove = ItemHandlerHelper.copyStackWithSize(extra, 1);
|
||||
|
||||
if (!pattern.isProcessing()) {
|
||||
took[i] = extraToRemove;
|
||||
}
|
||||
|
||||
extras.remove(extraToRemove, true);
|
||||
} else {
|
||||
ICraftingPattern inputPattern = network.getPattern(input, compare);
|
||||
|
||||
if (inputPattern != null) {
|
||||
for (ItemStack output : inputPattern.getOutputs()) {
|
||||
toCraft.add(output);
|
||||
}
|
||||
|
||||
calculate(list, inputPattern, false);
|
||||
} else {
|
||||
FluidStack fluidInItem = RSUtils.getFluidFromStack(input, true);
|
||||
|
||||
if (fluidInItem != null && RSUtils.hasFluidBucket(fluidInItem)) {
|
||||
FluidStack fluidInStorage = network.getFluidStorageCache().getList().get(fluidInItem);
|
||||
|
||||
if (fluidInStorage == null || fluidInStorage.amount < fluidInItem.amount) {
|
||||
missing.add(input);
|
||||
} else {
|
||||
boolean hasBucket = network.getItemStorageCache().getList().get(RSUtils.EMPTY_BUCKET) != null;
|
||||
ICraftingPattern bucketPattern = network.getPattern(RSUtils.EMPTY_BUCKET);
|
||||
|
||||
if (!hasBucket) {
|
||||
if (bucketPattern == null) {
|
||||
missing.add(RSUtils.EMPTY_BUCKET.copy());
|
||||
} else {
|
||||
calculate(list, bucketPattern, false);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasBucket || bucketPattern != null) {
|
||||
toTakeFluids.add(fluidInItem.copy());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
missing.add(input);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!pattern.isProcessing()) {
|
||||
ItemStack take = ItemHandlerHelper.copyStackWithSize(inputInNetwork, 1);
|
||||
|
||||
toTake.add(take);
|
||||
|
||||
took[i] = take;
|
||||
}
|
||||
|
||||
list.remove(inputInNetwork, 1, true);
|
||||
IItemStackList inputs = API.instance().createItemStackList();
|
||||
IItemStackList actualInputs = API.instance().createItemStackList();
|
||||
for (ItemStack input : pattern.getInputs()) {
|
||||
if (input != null) {
|
||||
inputs.add(input.copy());
|
||||
}
|
||||
}
|
||||
|
||||
for (ItemStack byproduct : (pattern.isOredict() ? pattern.getByproducts(took) : pattern.getByproducts())) {
|
||||
extras.add(byproduct.copy());
|
||||
for (ItemStack input : inputs.getStacks()) {
|
||||
ItemStack extraStack = toInsert.get(input, compare);
|
||||
ItemStack networkStack = networkList.get(input, compare);
|
||||
|
||||
while (input.stackSize > 0) {
|
||||
if (extraStack != null && extraStack.stackSize > 0) {
|
||||
int takeQuantity = Math.min(extraStack.stackSize, input.stackSize);
|
||||
ItemStack inputStack = ItemHandlerHelper.copyStackWithSize(extraStack, takeQuantity);
|
||||
actualInputs.add(inputStack.copy());
|
||||
input.stackSize -= takeQuantity;
|
||||
toInsert.remove(inputStack, true);
|
||||
} else if (networkStack != null && networkStack.stackSize > 0) {
|
||||
int takeQuantity = Math.min(networkStack.stackSize, input.stackSize);
|
||||
ItemStack inputStack = ItemHandlerHelper.copyStackWithSize(networkStack, takeQuantity);
|
||||
toTake.add(inputStack.copy());
|
||||
actualInputs.add(inputStack.copy());
|
||||
input.stackSize -= takeQuantity;
|
||||
networkList.remove(inputStack, true);
|
||||
} else {
|
||||
ICraftingPattern inputPattern = network.getPattern(input, compare);
|
||||
if (inputPattern != null) {
|
||||
int craftQuantity = Math.min(inputPattern.getQuantityPerRequest(input), input.stackSize);
|
||||
ItemStack inputCrafted = ItemHandlerHelper.copyStackWithSize(input, craftQuantity);
|
||||
toCraft.add(inputCrafted.copy());
|
||||
actualInputs.add(inputCrafted.copy());
|
||||
calculate(networkList, inputPattern, toInsert);
|
||||
input.stackSize -= craftQuantity;
|
||||
// Calculate added all the crafted outputs toInsert
|
||||
// So we remove the ones we use from toInsert
|
||||
toInsert.remove(inputCrafted, true);
|
||||
} else if (!doFluidCalculation(networkList, input, toInsert)) {
|
||||
missing.add(input.copy());
|
||||
input.stackSize = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (missing.isEmpty()) {
|
||||
for (int i = 0; i < pattern.getInputs().size(); i++) {
|
||||
ItemStack input = pattern.getInputs().get(i);
|
||||
if (input != null) {
|
||||
ItemStack actualInput = actualInputs.get(input, compare);
|
||||
ItemStack taken = ItemHandlerHelper.copyStackWithSize(actualInput, input.stackSize);
|
||||
took[i] = taken;
|
||||
actualInputs.remove(taken, true);
|
||||
}
|
||||
}
|
||||
|
||||
for (ItemStack byproduct : (pattern.isOredict() ? pattern.getByproducts(took) : pattern.getByproducts())) {
|
||||
toInsert.add(byproduct.copy());
|
||||
}
|
||||
|
||||
for (ItemStack output : pattern.getOutputs()) {
|
||||
toInsert.add(output.copy());
|
||||
}
|
||||
}
|
||||
usedPatterns.remove(pattern);
|
||||
}
|
||||
|
||||
private boolean doFluidCalculation(IItemStackList networkList, ItemStack input, IItemStackList toInsert) {
|
||||
FluidStack fluidInItem = RSUtils.getFluidFromStack(input, true);
|
||||
|
||||
if (fluidInItem != null && RSUtils.hasFluidBucket(fluidInItem)) {
|
||||
FluidStack fluidInStorage = network.getFluidStorageCache().getList().get(fluidInItem);
|
||||
|
||||
if (fluidInStorage == null || fluidInStorage.amount < fluidInItem.amount) {
|
||||
missing.add(input);
|
||||
} else {
|
||||
boolean hasBucket = networkList.get(RSUtils.EMPTY_BUCKET) != null;
|
||||
ICraftingPattern bucketPattern = network.getPattern(RSUtils.EMPTY_BUCKET);
|
||||
|
||||
if (!hasBucket) {
|
||||
if (bucketPattern == null) {
|
||||
missing.add(RSUtils.EMPTY_BUCKET.copy());
|
||||
} else {
|
||||
toCraft.add(RSUtils.EMPTY_BUCKET.copy());
|
||||
calculate(networkList, bucketPattern, toInsert);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasBucket || bucketPattern != null) {
|
||||
toTakeFluids.add(fluidInItem.copy());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -173,14 +196,13 @@ public class CraftingTask implements ICraftingTask {
|
||||
return "\nCraftingTask{quantity=" + quantity +
|
||||
"\n, toTake=" + toTake +
|
||||
"\n, toTakeFluids=" + toTakeFluids +
|
||||
"\n, toCraft=" + toCraft +
|
||||
"\n, toProcess=" + toProcess +
|
||||
"\n, extras=" + extras +
|
||||
"\n, toCraft=" + toProcess +
|
||||
"\n, toInsert=" + toInsert +
|
||||
"\n, missing=" + missing +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean update() {
|
||||
for (IProcessable processable : toProcess) {
|
||||
IItemHandler inventory = processable.getPattern().getContainer().getFacingInventory();
|
||||
@@ -226,7 +248,7 @@ public class CraftingTask implements ICraftingTask {
|
||||
if (isFinished()) {
|
||||
ItemStack insert = toInsert.peek();
|
||||
|
||||
if (network.insertItem(insert, insert.stackSize, true) == null) {
|
||||
if (insert != null && network.insertItem(insert, insert.stackSize, true) == null) {
|
||||
network.insertItem(insert, insert.stackSize, false);
|
||||
|
||||
toInsert.pop();
|
||||
@@ -330,6 +352,53 @@ public class CraftingTask implements ICraftingTask {
|
||||
return toProcess;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return !recurseFound;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ICraftingPreviewStack> getPreviewStacks() {
|
||||
if (!isValid()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
Map<Integer, CraftingPreviewStack> map = new LinkedHashMap<>();
|
||||
|
||||
for (ItemStack stack : toCraft.getStacks()) {
|
||||
int hash = API.instance().getItemStackHashCode(stack);
|
||||
CraftingPreviewStack previewStack = map.get(hash);
|
||||
if (previewStack == null) {
|
||||
previewStack = new CraftingPreviewStack(stack);
|
||||
}
|
||||
previewStack.addToCraft(stack.stackSize);
|
||||
map.put(hash, previewStack);
|
||||
}
|
||||
|
||||
for (ItemStack stack : toTake.getStacks()) {
|
||||
int hash = API.instance().getItemStackHashCode(stack);
|
||||
CraftingPreviewStack previewStack = map.get(hash);
|
||||
if (previewStack == null) {
|
||||
previewStack = new CraftingPreviewStack(stack);
|
||||
}
|
||||
previewStack.addAvailable(stack.stackSize);
|
||||
map.put(hash, previewStack);
|
||||
}
|
||||
|
||||
for (ItemStack stack : missing.getStacks()) {
|
||||
int hash = API.instance().getItemStackHashCode(stack);
|
||||
CraftingPreviewStack previewStack = map.get(hash);
|
||||
if (previewStack == null) {
|
||||
previewStack = new CraftingPreviewStack(stack);
|
||||
}
|
||||
previewStack.setMissing(true);
|
||||
previewStack.addToCraft(stack.stackSize);
|
||||
map.put(hash, previewStack);
|
||||
}
|
||||
|
||||
return new ArrayList<>(map.values());
|
||||
}
|
||||
|
||||
private boolean isFinished() {
|
||||
return toTake.isEmpty() && toTakeFluids.isEmpty() && missing.isEmpty() && hasProcessedItems();
|
||||
}
|
||||
|
||||
@@ -6,11 +6,13 @@ import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.ItemHandlerHelper;
|
||||
import refinedstorage.RS;
|
||||
import refinedstorage.api.autocrafting.ICraftingPattern;
|
||||
import refinedstorage.api.autocrafting.task.ICraftingTask;
|
||||
import refinedstorage.api.network.INetworkMaster;
|
||||
import refinedstorage.api.network.grid.IItemGridHandler;
|
||||
import refinedstorage.apiimpl.API;
|
||||
import refinedstorage.apiimpl.autocrafting.task.CraftingTask;
|
||||
import refinedstorage.network.MessageGridCraftingPreviewResponse;
|
||||
|
||||
public class ItemGridHandler implements IItemGridHandler {
|
||||
private INetworkMaster network;
|
||||
@@ -122,45 +124,25 @@ public class ItemGridHandler implements IItemGridHandler {
|
||||
ItemStack stack = network.getItemStorageCache().getList().get(hash);
|
||||
|
||||
if (stack != null) {
|
||||
CraftingTask task = new CraftingTask(network, stack, network.getPattern(stack), quantity);
|
||||
|
||||
ICraftingTask task = new CraftingTask(network, stack, network.getPattern(stack), quantity);
|
||||
task.calculate();
|
||||
|
||||
System.out.println(task.toString());
|
||||
|
||||
network.addCraftingTask(task);
|
||||
|
||||
/*CraftingPreviewData previewData = new CraftingPreviewData(network);
|
||||
|
||||
previewData.calculate(stack, quantity);
|
||||
|
||||
RefinedStorage.INSTANCE.network.sendTo(new MessageGridCraftingPreviewResponse(previewData.values(), hash, quantity), player);*/
|
||||
RS.INSTANCE.network.sendTo(new MessageGridCraftingPreviewResponse(task.getPreviewStacks(), hash, quantity), player);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCraftingRequested(int hash, int quantity) {
|
||||
/*if (quantity <= 0) {
|
||||
if (quantity <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
ItemStack stack = network.getItemStorage().get(hash);
|
||||
ItemStack stack = network.getItemStorageCache().getList().get(hash);
|
||||
|
||||
if (stack == null) {
|
||||
return;
|
||||
if (stack != null) {
|
||||
ICraftingTask task = new CraftingTask(network, stack, network.getPattern(stack), quantity);
|
||||
task.calculate();
|
||||
network.addCraftingTask(task);
|
||||
}
|
||||
|
||||
ICraftingPattern pattern = NetworkUtils.getPattern(network, stack);
|
||||
|
||||
if (pattern != null) {
|
||||
int quantityPerRequest = pattern.getQuantityPerRequest(stack);
|
||||
|
||||
while (quantity > 0) {
|
||||
network.addCraftingTask(NetworkUtils.createCraftingTask(network, 0, pattern));
|
||||
|
||||
quantity -= quantityPerRequest;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,20 +1,27 @@
|
||||
package refinedstorage.gui;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.GuiButton;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.renderer.RenderHelper;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.fml.client.FMLClientHandler;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
import refinedstorage.RS;
|
||||
import refinedstorage.api.autocrafting.preview.ICraftingPreviewStack;
|
||||
import refinedstorage.network.MessageGridCraftingStart;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GuiCraftingPreview extends GuiBase {
|
||||
private static final int VISIBLE_ROWS = 4;
|
||||
|
||||
//private List<CraftingPreviewStack> stacks;
|
||||
private List<ICraftingPreviewStack> stacks;
|
||||
private GuiScreen parent;
|
||||
|
||||
private int hash;
|
||||
@@ -23,7 +30,7 @@ public class GuiCraftingPreview extends GuiBase {
|
||||
private GuiButton startButton;
|
||||
private GuiButton cancelButton;
|
||||
|
||||
public GuiCraftingPreview(GuiScreen parent, /*Collection<CraftingPreviewStack> stacks, */int hash, int quantity) {
|
||||
public GuiCraftingPreview(GuiScreen parent, List<ICraftingPreviewStack> stacks, int hash, int quantity) {
|
||||
super(new Container() {
|
||||
@Override
|
||||
public boolean canInteractWith(EntityPlayer player) {
|
||||
@@ -31,7 +38,7 @@ public class GuiCraftingPreview extends GuiBase {
|
||||
}
|
||||
}, 168, 171);
|
||||
|
||||
//this.stacks = new ArrayList<>(stacks);
|
||||
this.stacks = new ArrayList<>(stacks);
|
||||
this.parent = parent;
|
||||
|
||||
this.hash = hash;
|
||||
@@ -44,7 +51,7 @@ public class GuiCraftingPreview extends GuiBase {
|
||||
public void init(int x, int y) {
|
||||
cancelButton = addButton(x + 16, y + 144, 50, 20, t("gui.cancel"));
|
||||
startButton = addButton(x + 85, y + 144, 50, 20, t("misc.refinedstorage:start"));
|
||||
//startButton.enabled = !stacks.isEmpty();
|
||||
startButton.enabled = !stacks.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -59,7 +66,7 @@ public class GuiCraftingPreview extends GuiBase {
|
||||
|
||||
drawTexture(x, y, 0, 0, width, height);
|
||||
|
||||
/*if (stacks.isEmpty()) {
|
||||
if (stacks.isEmpty()) {
|
||||
drawRect(x + 7, y + 20, x + 142, y + 139, 0xFFDBDBDB);
|
||||
}
|
||||
else {
|
||||
@@ -70,9 +77,9 @@ public class GuiCraftingPreview extends GuiBase {
|
||||
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
if (slot < stacks.size()) {
|
||||
CraftingPreviewStack stack = stacks.get(slot);
|
||||
ICraftingPreviewStack stack = stacks.get(slot);
|
||||
|
||||
if (stack.cantCraft()) {
|
||||
if (stack.hasMissing()) {
|
||||
drawTexture(x, y, 189, 0, 67, 29);
|
||||
}
|
||||
}
|
||||
@@ -86,7 +93,7 @@ public class GuiCraftingPreview extends GuiBase {
|
||||
|
||||
slot++;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,7 +104,7 @@ public class GuiCraftingPreview extends GuiBase {
|
||||
int y = 22;
|
||||
float scale = 0.5f;
|
||||
|
||||
/*if (stacks.isEmpty()) {
|
||||
if (stacks.isEmpty()) {
|
||||
GlStateManager.pushMatrix();
|
||||
GlStateManager.scale(scale, scale, 1);
|
||||
|
||||
@@ -115,7 +122,7 @@ public class GuiCraftingPreview extends GuiBase {
|
||||
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
if (slot < stacks.size()) {
|
||||
CraftingPreviewStack stack = stacks.get(slot);
|
||||
ICraftingPreviewStack stack = stacks.get(slot);
|
||||
|
||||
drawItem(x, y + 5, stack.getStack());
|
||||
|
||||
@@ -124,15 +131,15 @@ public class GuiCraftingPreview extends GuiBase {
|
||||
|
||||
int yy = y + 8;
|
||||
|
||||
if (stack.needsCrafting()) {
|
||||
String format = stack.cantCraft() ? "gui.refinedstorage:crafting_preview.missing" : "gui.refinedstorage:crafting_preview.to_craft";
|
||||
if (stack.getToCraft() > 0) {
|
||||
String format = stack.hasMissing() ? "gui.refinedstorage:crafting_preview.missing" : "gui.refinedstorage:crafting_preview.to_craft";
|
||||
drawString(calculateOffsetOnScale(x + 23, scale), calculateOffsetOnScale(yy, scale), t(format, stack.getToCraft()));
|
||||
|
||||
yy += 7;
|
||||
}
|
||||
|
||||
if (stack.getStock() > 0) {
|
||||
drawString(calculateOffsetOnScale(x + 23, scale), calculateOffsetOnScale(yy, scale), t("gui.refinedstorage:crafting_preview.available", stack.getStock()));
|
||||
if (stack.getAvailable() > 0) {
|
||||
drawString(calculateOffsetOnScale(x + 23, scale), calculateOffsetOnScale(yy, scale), t("gui.refinedstorage:crafting_preview.available", stack.getAvailable()));
|
||||
}
|
||||
|
||||
GlStateManager.popMatrix();
|
||||
@@ -155,7 +162,7 @@ public class GuiCraftingPreview extends GuiBase {
|
||||
if (hoveringStack != null) {
|
||||
drawTooltip(mouseX, mouseY, hoveringStack.getTooltip(Minecraft.getMinecraft().thePlayer, false));
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -187,8 +194,7 @@ public class GuiCraftingPreview extends GuiBase {
|
||||
}
|
||||
|
||||
private int getRows() {
|
||||
/*return Math.max(0, (int) Math.ceil((float) stacks.size() / 2F));*/
|
||||
return 0;
|
||||
return Math.max(0, (int) Math.ceil((float) stacks.size() / 2F));
|
||||
}
|
||||
|
||||
private void close() {
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
package refinedstorage.network;
|
||||
|
||||
/*
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
import refinedstorage.api.autocrafting.preview.ICraftingPreviewStack;
|
||||
import refinedstorage.apiimpl.autocrafting.preview.CraftingPreviewStack;
|
||||
import refinedstorage.proxy.ClientProxy;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class MessageGridCraftingPreviewResponse implements IMessage, IMessageHandler<MessageGridCraftingPreviewResponse, IMessage> {
|
||||
public Collection<CraftingPreviewStack> stacks;
|
||||
public List<ICraftingPreviewStack> stacks;
|
||||
public int hash;
|
||||
public int quantity;
|
||||
|
||||
public MessageGridCraftingPreviewResponse() {
|
||||
}
|
||||
|
||||
public MessageGridCraftingPreviewResponse(Collection<CraftingPreviewStack> stacks, int hash, int quantity) {
|
||||
public MessageGridCraftingPreviewResponse(List<ICraftingPreviewStack> stacks, int hash, int quantity) {
|
||||
this.stacks = stacks;
|
||||
this.hash = hash;
|
||||
this.quantity = quantity;
|
||||
@@ -46,7 +46,7 @@ public class MessageGridCraftingPreviewResponse implements IMessage, IMessageHan
|
||||
|
||||
buf.writeInt(stacks.size());
|
||||
|
||||
for (CraftingPreviewStack stack : stacks) {
|
||||
for (ICraftingPreviewStack stack : stacks) {
|
||||
stack.writeToByteBuf(buf);
|
||||
}
|
||||
}
|
||||
@@ -57,5 +57,4 @@ public class MessageGridCraftingPreviewResponse implements IMessage, IMessageHan
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import mcmultipart.client.multipart.ModelMultipartContainer;
|
||||
import mcmultipart.raytrace.PartMOP;
|
||||
import mcmultipart.raytrace.RayTraceUtils;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.client.renderer.VertexBuffer;
|
||||
@@ -20,6 +22,7 @@ import net.minecraftforge.client.event.DrawBlockHighlightEvent;
|
||||
import net.minecraftforge.client.event.ModelBakeEvent;
|
||||
import net.minecraftforge.client.model.ModelLoader;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.EventPriority;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
@@ -28,7 +31,10 @@ import refinedstorage.RS;
|
||||
import refinedstorage.RSBlocks;
|
||||
import refinedstorage.RSItems;
|
||||
import refinedstorage.block.*;
|
||||
import refinedstorage.gui.GuiCraftingPreview;
|
||||
import refinedstorage.gui.grid.GuiCraftingStart;
|
||||
import refinedstorage.item.*;
|
||||
import refinedstorage.network.MessageGridCraftingPreviewResponse;
|
||||
import refinedstorage.tile.TileController;
|
||||
|
||||
import java.util.List;
|
||||
@@ -297,7 +303,7 @@ public class ClientProxy extends CommonProxy {
|
||||
});
|
||||
}
|
||||
|
||||
/*public static void onReceiveCraftingPreviewResponse(MessageGridCraftingPreviewResponse message) {
|
||||
public static void onReceiveCraftingPreviewResponse(MessageGridCraftingPreviewResponse message) {
|
||||
Minecraft.getMinecraft().addScheduledTask(() -> {
|
||||
GuiScreen screen = Minecraft.getMinecraft().currentScreen;
|
||||
|
||||
@@ -307,5 +313,5 @@ public class ClientProxy extends CommonProxy {
|
||||
|
||||
FMLCommonHandler.instance().showGuiScreen(new GuiCraftingPreview(screen, message.stacks, message.hash, message.quantity));
|
||||
});
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ public class CommonProxy {
|
||||
RS.INSTANCE.network.registerMessage(MessageProcessingPatternEncoderClear.class, MessageProcessingPatternEncoderClear.class, id++, Side.SERVER);
|
||||
RS.INSTANCE.network.registerMessage(MessageGridFilterUpdate.class, MessageGridFilterUpdate.class, id++, Side.SERVER);
|
||||
RS.INSTANCE.network.registerMessage(MessageGridCraftingPreview.class, MessageGridCraftingPreview.class, id++, Side.SERVER);
|
||||
//RefinedStorage.INSTANCE.network.registerMessage(MessageGridCraftingPreviewResponse.class, MessageGridCraftingPreviewResponse.class, id++, Side.CLIENT);
|
||||
RS.INSTANCE.network.registerMessage(MessageGridCraftingPreviewResponse.class, MessageGridCraftingPreviewResponse.class, id++, Side.CLIENT);
|
||||
RS.INSTANCE.network.registerMessage(MessageProcessingPatternEncoderTransfer.class, MessageProcessingPatternEncoderTransfer.class, id++, Side.SERVER);
|
||||
|
||||
NetworkRegistry.INSTANCE.registerGuiHandler(RS.INSTANCE, new GuiHandler());
|
||||
|
||||
@@ -273,7 +273,9 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
|
||||
craftingTasksToCancel.clear();
|
||||
|
||||
for (ICraftingTask task : craftingTasksToAdd) {
|
||||
craftingTasks.add(task);
|
||||
if (task.isValid()) {
|
||||
craftingTasks.add(task);
|
||||
}
|
||||
}
|
||||
|
||||
craftingTasksToAdd.clear();
|
||||
|
||||
Reference in New Issue
Block a user