From 98f0e59b67b0d1960ff3852d2b218b1504843a5e Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Tue, 3 Jun 2014 23:37:47 -0500 Subject: [PATCH] Begun work on the crafting memory / tree --- crafting/CraftingJob.java | 80 +++++++++++++++++++++++++ crafting/CraftingTask.java | 106 ++++++++++++++++++++++++++++++++++ crafting/ICraftingHost.java | 10 ++++ crafting/ICraftingParent.java | 16 +++++ 4 files changed, 212 insertions(+) create mode 100644 crafting/CraftingJob.java create mode 100644 crafting/CraftingTask.java create mode 100644 crafting/ICraftingHost.java create mode 100644 crafting/ICraftingParent.java diff --git a/crafting/CraftingJob.java b/crafting/CraftingJob.java new file mode 100644 index 000000000..8b93338ca --- /dev/null +++ b/crafting/CraftingJob.java @@ -0,0 +1,80 @@ +package appeng.crafting; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; + +import net.minecraft.nbt.NBTTagCompound; +import appeng.api.storage.data.IAEItemStack; +import appeng.api.storage.data.IItemList; + +import com.google.common.collect.Multimap; + +public class CraftingJob implements ICraftingParent +{ + + IAEItemStack output; + + IItemList storage; + + HashSet prophecies; + + Multimap bottom; + CraftingTask top; + + public Collection getBottom() + { + return bottom.values(); + } + + public IAEItemStack addProgress(IAEItemStack progress) + { + Collection task = bottom.get( progress ); + + if ( task == null ) + return progress; + + Iterator i = task.iterator(); + + while (i.hasNext()) + { + // adding to the first item.. + progress = i.next().addProgress( progress ); + + // if nothing remains return + if ( progress == null ) + return null; + + // if something remains continue.. + i = task.iterator(); + } + + return null; + } + + @Override + public void writeToNBT(NBTTagCompound out) + { + + } + + @Override + public void removeChild(CraftingTask craftingTask) + { + // TODO Auto-generated method stub + } + + public ICraftingHost getHost() + { + // TODO Auto-generated method stub + return null; + } + + @Override + public Collection getSubJobs() + { + return Collections.singleton( top ); + } + +} diff --git a/crafting/CraftingTask.java b/crafting/CraftingTask.java new file mode 100644 index 000000000..19997f3ee --- /dev/null +++ b/crafting/CraftingTask.java @@ -0,0 +1,106 @@ +package appeng.crafting; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import appeng.api.networking.crafting.ICraftingPatternDetails; +import appeng.api.storage.data.IAEItemStack; +import appeng.items.misc.ItemEncodedPattern; +import appeng.util.item.AEItemStack; + +public class CraftingTask implements ICraftingParent +{ + + final IAEItemStack task; + + final ICraftingPatternDetails process; + + final List subtasks; + final ICraftingParent parenttask; + + final CraftingJob job; + + public CraftingTask(IAEItemStack myTask, ICraftingPatternDetails pattern, ICraftingParent parent, CraftingJob craftingJob) { + task = myTask; + process = pattern; + parenttask = parent; + job = craftingJob; + subtasks = new ArrayList(); + + // register as part of the bottom. + job.bottom.put( task.copy(), this ); + } + + public CraftingTask(NBTTagCompound c, ICraftingParent parent, CraftingJob craftingJob) { + task = AEItemStack.loadItemStackFromNBT( c.getCompoundTag( "task" ) ); + ItemStack pattern = ItemStack.loadItemStackFromNBT( c.getCompoundTag( "pattern" ) ); + + if ( pattern == null || !(pattern.getItem() instanceof ItemEncodedPattern) ) + throw new RuntimeException( "Failed to load crafting job" ); + + ItemEncodedPattern iep = (ItemEncodedPattern) pattern.getItem(); + process = iep.getPatternForItem( pattern, craftingJob.getHost().getWorld() ); + + subtasks = new ArrayList(); + parenttask = parent; + job = craftingJob; + } + + public IAEItemStack addProgress(IAEItemStack progress) + { + long amt = progress.getStackSize(); + long mine = task.getStackSize(); + + if ( amt >= mine ) + { + parenttask.removeChild( this ); + + task.reset(); + job.bottom.remove( task, this ); + + progress = progress.copy(); + progress.decStackSize( mine ); + return progress; + } + else + { + task.decStackSize( amt ); + return null; + } + } + + public void addSubTask(CraftingTask task) + { + boolean wasEmpty = subtasks.isEmpty(); + + subtasks.add( task ); + + if ( wasEmpty ) + job.bottom.remove( task, this ); + } + + public void removeChild(CraftingTask craftingTask) + { + subtasks.remove( craftingTask ); + if ( subtasks.isEmpty() ) + { + job.bottom.put( task.copy(), this ); + } + } + + @Override + public Collection getSubJobs() + { + return subtasks; + } + + @Override + public void writeToNBT(NBTTagCompound out) + { + + } + +} diff --git a/crafting/ICraftingHost.java b/crafting/ICraftingHost.java new file mode 100644 index 000000000..ea5026219 --- /dev/null +++ b/crafting/ICraftingHost.java @@ -0,0 +1,10 @@ +package appeng.crafting; + +import net.minecraft.world.World; + +public interface ICraftingHost +{ + + World getWorld(); + +} diff --git a/crafting/ICraftingParent.java b/crafting/ICraftingParent.java new file mode 100644 index 000000000..cf51a4a7b --- /dev/null +++ b/crafting/ICraftingParent.java @@ -0,0 +1,16 @@ +package appeng.crafting; + +import java.util.Collection; + +import net.minecraft.nbt.NBTTagCompound; + +public interface ICraftingParent +{ + + Collection getSubJobs(); + + void writeToNBT(NBTTagCompound out); + + void removeChild(CraftingTask craftingTask); + +}