Crafting stuff refactor
This commit is contained in:
@@ -3,12 +3,14 @@ package refinedstorage.item;
|
|||||||
import net.minecraft.entity.player.EntityPlayer;
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
|
import net.minecraft.nbt.NBTTagList;
|
||||||
|
import net.minecraftforge.common.util.Constants;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ItemPattern extends ItemBase {
|
public class ItemPattern extends ItemBase {
|
||||||
public static final String NBT_RESULT = "Result";
|
public static final String NBT_RESULT = "Result";
|
||||||
public static final String NBT_SLOT = "Slot_%d";
|
public static final String NBT_INGREDIENTS = "Ingredients";
|
||||||
|
|
||||||
public ItemPattern() {
|
public ItemPattern() {
|
||||||
super("pattern");
|
super("pattern");
|
||||||
@@ -21,27 +23,36 @@ public class ItemPattern extends ItemBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setSlot(ItemStack pattern, int slot, ItemStack stack) {
|
public static void addIngredient(ItemStack pattern, ItemStack stack) {
|
||||||
if (pattern.getTagCompound() == null) {
|
if (pattern.getTagCompound() == null) {
|
||||||
pattern.setTagCompound(new NBTTagCompound());
|
pattern.setTagCompound(new NBTTagCompound());
|
||||||
}
|
}
|
||||||
|
|
||||||
NBTTagCompound tag = new NBTTagCompound();
|
if (!pattern.getTagCompound().hasKey(NBT_INGREDIENTS)) {
|
||||||
stack.writeToNBT(tag);
|
pattern.getTagCompound().setTag(NBT_INGREDIENTS, new NBTTagList());
|
||||||
|
|
||||||
pattern.getTagCompound().setTag(String.format(NBT_SLOT, slot), tag);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ItemStack getSlot(ItemStack pattern, int slot) {
|
pattern.getTagCompound().getTagList(NBT_INGREDIENTS, Constants.NBT.TAG_COMPOUND).appendTag(stack.serializeNBT());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ItemStack[] getIngredients(ItemStack pattern) {
|
||||||
if (pattern.getTagCompound() == null) {
|
if (pattern.getTagCompound() == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pattern.getTagCompound().hasKey(String.format(NBT_SLOT, slot))) {
|
if (!pattern.getTagCompound().hasKey(NBT_INGREDIENTS)) {
|
||||||
return ItemStack.loadItemStackFromNBT(pattern.getTagCompound().getCompoundTag(String.format(NBT_SLOT, slot)));
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
NBTTagList ingredients = pattern.getTagCompound().getTagList(NBT_INGREDIENTS, Constants.NBT.TAG_COMPOUND);
|
||||||
|
|
||||||
|
ItemStack[] ingredientsArray = new ItemStack[ingredients.tagCount()];
|
||||||
|
|
||||||
|
for (int i = 0; i < ingredients.tagCount(); ++i) {
|
||||||
|
ingredientsArray[i] = ItemStack.loadItemStackFromNBT(ingredients.getCompoundTagAt(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ingredientsArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setResult(ItemStack pattern, ItemStack stack) {
|
public static void setResult(ItemStack pattern, ItemStack stack) {
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import refinedstorage.network.MessageWirelessGridItems;
|
|||||||
import refinedstorage.storage.IStorage;
|
import refinedstorage.storage.IStorage;
|
||||||
import refinedstorage.storage.IStorageProvider;
|
import refinedstorage.storage.IStorageProvider;
|
||||||
import refinedstorage.storage.ItemGroup;
|
import refinedstorage.storage.ItemGroup;
|
||||||
|
import refinedstorage.tile.autocrafting.CraftingPattern;
|
||||||
import refinedstorage.tile.autocrafting.CraftingTask;
|
import refinedstorage.tile.autocrafting.CraftingTask;
|
||||||
import refinedstorage.tile.config.IRedstoneModeConfig;
|
import refinedstorage.tile.config.IRedstoneModeConfig;
|
||||||
import refinedstorage.tile.config.RedstoneMode;
|
import refinedstorage.tile.config.RedstoneMode;
|
||||||
@@ -162,12 +163,10 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
|||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
CraftingTask task = it.next();
|
CraftingTask task = it.next();
|
||||||
|
|
||||||
task.attemptCraft(this);
|
if (task.attemptCraft(this)) {
|
||||||
|
|
||||||
if (task.isDone()) {
|
|
||||||
it.remove();
|
it.remove();
|
||||||
|
|
||||||
push(task.getResult());
|
push(task.getPattern().getResult());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -244,8 +243,8 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
|||||||
craftingTasksToAdd.add(task);
|
craftingTasksToAdd.add(task);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ItemStack> getPatterns() {
|
public List<CraftingPattern> getPatterns() {
|
||||||
List<ItemStack> patterns = new ArrayList<ItemStack>();
|
List<CraftingPattern> patterns = new ArrayList<CraftingPattern>();
|
||||||
|
|
||||||
Iterator<TileMachine> it = machines.iterator();
|
Iterator<TileMachine> it = machines.iterator();
|
||||||
|
|
||||||
@@ -257,7 +256,9 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
|||||||
|
|
||||||
for (int i = 0; i < TileCrafter.PATTERN_SLOTS; ++i) {
|
for (int i = 0; i < TileCrafter.PATTERN_SLOTS; ++i) {
|
||||||
if (crafter.getStackInSlot(i) != null) {
|
if (crafter.getStackInSlot(i) != null) {
|
||||||
patterns.add(crafter.getStackInSlot(i));
|
ItemStack pattern = crafter.getStackInSlot(i);
|
||||||
|
|
||||||
|
patterns.add(new CraftingPattern(ItemPattern.getResult(pattern), ItemPattern.getIngredients(pattern), 20));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -266,9 +267,9 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
|||||||
return patterns;
|
return patterns;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStack getPatternForItem(ItemStack stack) {
|
public CraftingPattern getPatternForItem(ItemStack stack) {
|
||||||
for (ItemStack pattern : getPatterns()) {
|
for (CraftingPattern pattern : getPatterns()) {
|
||||||
if (InventoryUtils.compareStackNoQuantity(ItemPattern.getResult(pattern), stack)) {
|
if (InventoryUtils.compareStackNoQuantity(pattern.getResult(), stack)) {
|
||||||
return pattern;
|
return pattern;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -283,8 +284,8 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
|||||||
storage.addItems(itemGroups);
|
storage.addItems(itemGroups);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ItemStack pattern : getPatterns()) {
|
for (CraftingPattern pattern : getPatterns()) {
|
||||||
ItemGroup patternGroup = new ItemGroup(ItemPattern.getResult(pattern));
|
ItemGroup patternGroup = new ItemGroup(pattern.getResult());
|
||||||
patternGroup.setQuantity(0);
|
patternGroup.setQuantity(0);
|
||||||
itemGroups.add(patternGroup);
|
itemGroups.add(patternGroup);
|
||||||
}
|
}
|
||||||
@@ -683,12 +684,12 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
|||||||
public void onCraftingRequested(int id, int quantity) {
|
public void onCraftingRequested(int id, int quantity) {
|
||||||
if (id >= 0 && id < itemGroups.size() && quantity > 0) {
|
if (id >= 0 && id < itemGroups.size() && quantity > 0) {
|
||||||
while (quantity > 0) {
|
while (quantity > 0) {
|
||||||
ItemStack pattern = getPatternForItem(itemGroups.get(id).toItemStack());
|
CraftingPattern pattern = getPatternForItem(itemGroups.get(id).toItemStack());
|
||||||
|
|
||||||
if (pattern != null) {
|
if (pattern != null) {
|
||||||
addCraftingTask(CraftingTask.createFromPattern(pattern));
|
addCraftingTask(new CraftingTask(pattern));
|
||||||
|
|
||||||
quantity -= ItemPattern.getResult(pattern).stackSize;
|
quantity -= pattern.getResult().stackSize;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
package refinedstorage.tile.autocrafting;
|
|
||||||
|
|
||||||
import net.minecraft.item.ItemStack;
|
|
||||||
|
|
||||||
public class CraftingIngredient {
|
|
||||||
private ItemStack stack;
|
|
||||||
private boolean satisfied;
|
|
||||||
private boolean childTaskCreated;
|
|
||||||
|
|
||||||
public CraftingIngredient(ItemStack stack) {
|
|
||||||
this.stack = stack;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStack getStack() {
|
|
||||||
return stack;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isSatisfied() {
|
|
||||||
return satisfied;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSatisfied() {
|
|
||||||
this.satisfied = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isChildTaskCreated() {
|
|
||||||
return childTaskCreated;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setChildTaskCreated() {
|
|
||||||
childTaskCreated = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
27
src/main/java/refinedstorage/tile/autocrafting/CraftingPattern.java
Executable file
27
src/main/java/refinedstorage/tile/autocrafting/CraftingPattern.java
Executable file
@@ -0,0 +1,27 @@
|
|||||||
|
package refinedstorage.tile.autocrafting;
|
||||||
|
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
|
public class CraftingPattern {
|
||||||
|
private ItemStack result;
|
||||||
|
private ItemStack[] ingredients;
|
||||||
|
private int speed;
|
||||||
|
|
||||||
|
public CraftingPattern(ItemStack result, ItemStack[] ingredients, int speed) {
|
||||||
|
this.result = result;
|
||||||
|
this.ingredients = ingredients;
|
||||||
|
this.speed = speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack getResult() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack[] getIngredients() {
|
||||||
|
return ingredients;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSpeed() {
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,32 +8,41 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class CraftingTask {
|
public class CraftingTask {
|
||||||
private ItemStack result;
|
private CraftingPattern pattern;
|
||||||
private List<CraftingIngredient> ingredients;
|
private boolean satisfied[];
|
||||||
|
private boolean childTasks[];
|
||||||
|
|
||||||
public CraftingTask(ItemStack result, List<CraftingIngredient> ingredients) {
|
public CraftingTask(CraftingPattern pattern) {
|
||||||
this.result = result;
|
this.pattern = pattern;
|
||||||
this.ingredients = ingredients;
|
this.satisfied = new boolean[pattern.getIngredients().length];
|
||||||
|
this.childTasks = new boolean[pattern.getIngredients().length];
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStack getResult() {
|
public CraftingPattern getPattern() {
|
||||||
return result;
|
return pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void attemptCraft(TileController controller) {
|
public boolean attemptCraft(TileController controller) {
|
||||||
for (CraftingIngredient ingredient : ingredients) {
|
boolean done = true;
|
||||||
if (!ingredient.isSatisfied()) {
|
|
||||||
ItemStack took = controller.take(ingredient.getStack().copy());
|
for (int i = 0; i < pattern.getIngredients().length; ++i) {
|
||||||
|
ItemStack ingredient = pattern.getIngredients()[i];
|
||||||
|
|
||||||
|
if (!satisfied[i]) {
|
||||||
|
done = false;
|
||||||
|
|
||||||
|
ItemStack took = controller.take(ingredient.copy());
|
||||||
|
|
||||||
if (took != null) {
|
if (took != null) {
|
||||||
ingredient.setSatisfied();
|
satisfied[i] = true;
|
||||||
} else if (!ingredient.isChildTaskCreated()) {
|
} else if (!childTasks[i]) {
|
||||||
ItemStack pattern = controller.getPatternForItem(ingredient.getStack());
|
CraftingPattern pattern = controller.getPatternForItem(ingredient);
|
||||||
|
|
||||||
if (pattern != null) {
|
if (pattern != null) {
|
||||||
CraftingTask childTask = CraftingTask.createFromPattern(pattern);
|
controller.addCraftingTask(new CraftingTask(pattern));
|
||||||
ingredient.setChildTaskCreated();
|
|
||||||
controller.addCraftingTask(childTask);
|
childTasks[i] = true;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -41,29 +50,7 @@ public class CraftingTask {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDone() {
|
return done;
|
||||||
for (CraftingIngredient ingredient : ingredients) {
|
|
||||||
if (!ingredient.isSatisfied()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CraftingTask createFromPattern(ItemStack pattern) {
|
|
||||||
List<CraftingIngredient> ingredients = new ArrayList<CraftingIngredient>();
|
|
||||||
|
|
||||||
for (int i = 0; i < 9; ++i) {
|
|
||||||
ItemStack ingredient = ItemPattern.getSlot(pattern, i);
|
|
||||||
|
|
||||||
if (ingredient != null) {
|
|
||||||
ingredients.add(new CraftingIngredient(ingredient));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new CraftingTask(ItemPattern.getResult(pattern), ingredients);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public class TileCraftingMonitor extends TileMachine {
|
|||||||
buf.writeInt(controller.getCraftingTasks().size());
|
buf.writeInt(controller.getCraftingTasks().size());
|
||||||
|
|
||||||
for (CraftingTask task : controller.getCraftingTasks()) {
|
for (CraftingTask task : controller.getCraftingTasks()) {
|
||||||
ByteBufUtils.writeItemStack(buf, task.getResult());
|
ByteBufUtils.writeItemStack(buf, task.getPattern().getResult());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
buf.writeInt(0);
|
buf.writeInt(0);
|
||||||
|
|||||||
@@ -172,13 +172,14 @@ public class TileGrid extends TileMachine implements IGrid {
|
|||||||
patternsInventory.decrStackSize(0, 1);
|
patternsInventory.decrStackSize(0, 1);
|
||||||
|
|
||||||
ItemStack pattern = new ItemStack(RefinedStorageItems.PATTERN);
|
ItemStack pattern = new ItemStack(RefinedStorageItems.PATTERN);
|
||||||
|
|
||||||
ItemPattern.setResult(pattern, crafted);
|
ItemPattern.setResult(pattern, crafted);
|
||||||
|
|
||||||
for (int i = 0; i < 9; ++i) {
|
for (int i = 0; i < 9; ++i) {
|
||||||
ItemStack ingredient = craftingInventory.getStackInSlot(i);
|
ItemStack ingredient = craftingInventory.getStackInSlot(i);
|
||||||
|
|
||||||
if (ingredient != null) {
|
if (ingredient != null) {
|
||||||
ItemPattern.setSlot(pattern, i, ingredient);
|
ItemPattern.addIngredient(pattern, ingredient);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user