Implemented Auto Crafting with ArmBot USE command

Also fixed some major crafting bugs in Imprinter
Did *NOT* add *ANY* easter eggs. None. Zero. Zip. Nada.
This commit is contained in:
Brian Ricketts 2013-01-20 02:51:24 -06:00
parent 76e81cf00a
commit 9a1aa77adf
8 changed files with 217 additions and 34 deletions

View file

@ -1,5 +1,6 @@
package assemblyline.common.machine.armbot; package assemblyline.api;
import assemblyline.common.machine.armbot.TileEntityArmbot;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
/** /**
@ -9,7 +10,7 @@ import net.minecraft.entity.Entity;
* @author Briman0094 * @author Briman0094
* *
*/ */
public interface IUseable public interface IArmbotUseable
{ {
/** /**

View file

@ -30,6 +30,7 @@ import universalelectricity.prefab.TranslationHelper;
import universalelectricity.prefab.multiblock.IMultiBlock; import universalelectricity.prefab.multiblock.IMultiBlock;
import universalelectricity.prefab.network.IPacketReceiver; import universalelectricity.prefab.network.IPacketReceiver;
import universalelectricity.prefab.network.PacketManager; import universalelectricity.prefab.network.PacketManager;
import assemblyline.api.IArmbotUseable;
import assemblyline.common.AssemblyLine; import assemblyline.common.AssemblyLine;
import assemblyline.common.machine.TileEntityAssemblyNetwork; import assemblyline.common.machine.TileEntityAssemblyNetwork;
import assemblyline.common.machine.command.Command; import assemblyline.common.machine.command.Command;
@ -102,8 +103,20 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult
{ {
if (Block.isNormalCube(block.blockID)) if (Block.isNormalCube(block.blockID))
{ {
block.dropBlockAsItem(this.worldObj, this.xCoord, this.yCoord, this.zCoord, handPosition.getBlockMetadata(this.worldObj), 0); TileEntity tileEntity = this.getHandPosition().getTileEntity(this.worldObj);
handPosition.setBlockWithNotify(this.worldObj, 0); if (tileEntity == null)
{
block.dropBlockAsItem(this.worldObj, this.xCoord, this.yCoord, this.zCoord, handPosition.getBlockMetadata(this.worldObj), 0);
handPosition.setBlockWithNotify(this.worldObj, 0);
}
else
{
if (!(tileEntity instanceof IArmbotUseable))
{
block.dropBlockAsItem(this.worldObj, this.xCoord, this.yCoord, this.zCoord, handPosition.getBlockMetadata(this.worldObj), 0);
handPosition.setBlockWithNotify(this.worldObj, 0);
}
}
} }
} }
@ -628,7 +641,24 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult
} }
case 6: case 6:
{ {
this.commandManager.addCommand(this, CommandUse.class); if (arguments.length > 0)
{
try
// try to cast to Float
{
int times = (Integer) arguments[0];
this.commandManager.addCommand(this, CommandUse.class, new String[] { Integer.toString(times) });
}
catch (Exception ex)
{
ex.printStackTrace();
throw new IllegalArgumentException("expected number");
}
}
else
{
this.commandManager.addCommand(this, CommandUse.class);
}
break; break;
} }
} }

View file

@ -20,8 +20,7 @@ public class CommandDrop extends Command
entity.worldObj = this.tileEntity.worldObj; entity.worldObj = this.tileEntity.worldObj;
if (entity instanceof EntityItem) if (entity instanceof EntityItem)
world.spawnEntityInWorld(entity); //items don't move right, so we render them manually world.spawnEntityInWorld(entity); //items don't move right, so we render them manually
if (this.world.isRemote) this.world.playSound(this.tileEntity.xCoord, this.tileEntity.yCoord, this.tileEntity.zCoord, "random.pop", 0.2F, ((this.tileEntity.worldObj.rand.nextFloat() - this.tileEntity.worldObj.rand.nextFloat()) * 0.7F + 1.0F) * 1.0F, true);
Minecraft.getMinecraft().sndManager.playSound("random.pop", this.tileEntity.xCoord, this.tileEntity.yCoord, this.tileEntity.zCoord, 0.2F, ((this.tileEntity.worldObj.rand.nextFloat() - this.tileEntity.worldObj.rand.nextFloat()) * 0.7F + 1.0F) * 1.0F);
} }
} }
this.tileEntity.grabbedEntities.clear(); this.tileEntity.grabbedEntities.clear();

View file

@ -45,9 +45,8 @@ public class CommandGrab extends Command
{ {
this.tileEntity.grabbedEntities.add(found.get(i)); this.tileEntity.grabbedEntities.add(found.get(i));
if (found.get(i) instanceof EntityItem) if (found.get(i) instanceof EntityItem)
this.tileEntity.worldObj.removeEntity(found.get(i)); // items don't move right, so we render them manually this.tileEntity.worldObj.removeEntity(found.get(i)); // items don't move right, so we render them manually\
if (this.world.isRemote) this.world.playSound(this.tileEntity.xCoord, this.tileEntity.yCoord, this.tileEntity.zCoord, "random.pop", 0.2F, ((this.tileEntity.worldObj.rand.nextFloat() - this.tileEntity.worldObj.rand.nextFloat()) * 0.7F + 1.0F) * 1.0F, true);
Minecraft.getMinecraft().sndManager.playSound("random.pop", this.tileEntity.xCoord, this.tileEntity.yCoord, this.tileEntity.zCoord, 0.2F, ((this.tileEntity.worldObj.rand.nextFloat() - this.tileEntity.worldObj.rand.nextFloat()) * 0.7F + 1.0F) * 1.0F);
found.get(i).isDead = false; found.get(i).isDead = false;
return false; return false;
} }

View file

@ -2,10 +2,28 @@ package assemblyline.common.machine.command;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import assemblyline.common.machine.armbot.IUseable; import assemblyline.api.IArmbotUseable;
public class CommandUse extends Command public class CommandUse extends Command
{ {
private int times;
private int curTimes;
@Override
public void onTaskStart()
{
times = 0;
curTimes = 0;
if (this.getArgs().length > 0)
{
times = this.getIntArg(0);
}
if (times <= 0)
times = 1;
}
@Override @Override
protected boolean doTask() protected boolean doTask()
{ {
@ -15,11 +33,17 @@ public class CommandUse extends Command
handEntity = this.tileEntity.grabbedEntities.get(0); handEntity = this.tileEntity.grabbedEntities.get(0);
if (handTile != null) if (handTile != null)
{ {
if (handTile instanceof IUseable) if (handTile instanceof IArmbotUseable)
{ {
((IUseable) handTile).onUse(this.tileEntity, handEntity); ((IArmbotUseable) handTile).onUse(this.tileEntity, handEntity);
} }
} }
return false;
curTimes++;
if (curTimes >= times)
return false;
return true;
} }
} }

View file

@ -20,6 +20,13 @@ public class BlockImprinter extends BlockMachine
this.setTextureFile(AssemblyLine.BLOCK_TEXTURE_PATH); this.setTextureFile(AssemblyLine.BLOCK_TEXTURE_PATH);
} }
@Override
public void onBlockAdded(World world, int x, int y, int z)
{
super.onBlockAdded(world, x, y, z);
System.out.println(String.format("Initializing instance of 'Crafting Simulator 2014' at coordinates (%d, %d, %d)...", x, y, z));
}
/** /**
* Returns the block texture based on the side being looked at. Args: side * Returns the block texture based on the side being looked at. Args: side
*/ */

View file

@ -46,7 +46,7 @@ public class SlotCraftingResult extends Slot
if (checkStack != null) if (checkStack != null)
{ {
if (searchStack.isItemEqual(checkStack)) if (searchStack.isItemEqual(checkStack) || (searchStack.itemID == checkStack.itemID && searchStack.getItemDamage() < 0))
{ {
this.container.tileEntity.decrStackSize(i, 1); this.container.tileEntity.decrStackSize(i, 1);
break; break;

View file

@ -3,6 +3,8 @@ package assemblyline.common.machine.imprinter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
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.item.crafting.CraftingManager; import net.minecraft.item.crafting.CraftingManager;
@ -17,10 +19,13 @@ import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe; import net.minecraftforge.oredict.ShapelessOreRecipe;
import universalelectricity.prefab.TranslationHelper; import universalelectricity.prefab.TranslationHelper;
import universalelectricity.prefab.tile.TileEntityAdvanced; import universalelectricity.prefab.tile.TileEntityAdvanced;
import assemblyline.api.IArmbotUseable;
import assemblyline.common.AssemblyLine;
import assemblyline.common.Pair; import assemblyline.common.Pair;
import assemblyline.common.machine.armbot.TileEntityArmbot;
import cpw.mods.fml.relauncher.ReflectionHelper; import cpw.mods.fml.relauncher.ReflectionHelper;
public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInventory public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInventory, IArmbotUseable
{ {
public static final int START_INVENTORY = 5; public static final int START_INVENTORY = 5;
public static final int INVENTORY_LENGTH = 9; public static final int INVENTORY_LENGTH = 9;
@ -47,7 +52,10 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
@Override @Override
public int getSizeInventorySide(ForgeDirection side) public int getSizeInventorySide(ForgeDirection side)
{ {
if (side == ForgeDirection.UP || side == ForgeDirection.DOWN) { return 1; } if (side == ForgeDirection.UP || side == ForgeDirection.DOWN)
{
return 1;
}
return INVENTORY_LENGTH; return INVENTORY_LENGTH;
} }
@ -64,8 +72,7 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
} }
/** /**
* Removes from an inventory slot (first arg) up to a specified number (second arg) of items and * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a new stack.
* returns them in a new stack.
*/ */
@Override @Override
public ItemStack decrStackSize(int i, int amount) public ItemStack decrStackSize(int i, int amount)
@ -99,8 +106,7 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
} }
/** /**
* When some containers are closed they call this on each slot, then drop whatever it returns as * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem - like when you close a workbench GUI.
* an EntityItem - like when you close a workbench GUI.
*/ */
@Override @Override
public ItemStack getStackInSlotOnClosing(int slot) public ItemStack getStackInSlotOnClosing(int slot)
@ -118,8 +124,7 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
} }
/** /**
* Sets the given item stack to the specified slot in the inventory (can be crafting or armor * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
* sections).
*/ */
@Override @Override
public void setInventorySlotContents(int par1, ItemStack par2ItemStack) public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
@ -258,11 +263,17 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
{ {
if (object instanceof ShapedRecipes) if (object instanceof ShapedRecipes)
{ {
if (this.hasResource(((ShapedRecipes) object).recipeItems) != null) { return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), ((ShapedRecipes) object).recipeItems); } if (this.hasResource(((ShapedRecipes) object).recipeItems) != null)
{
return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), ((ShapedRecipes) object).recipeItems);
}
} }
else if (object instanceof ShapelessRecipes) else if (object instanceof ShapelessRecipes)
{ {
if (this.hasResource(((ShapelessRecipes) object).recipeItems.toArray(new ItemStack[1])) != null) { return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), (ItemStack[]) ((ShapelessRecipes) object).recipeItems.toArray(new ItemStack[1])); } if (this.hasResource(((ShapelessRecipes) object).recipeItems.toArray(new ItemStack[1])) != null)
{
return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), (ItemStack[]) ((ShapelessRecipes) object).recipeItems.toArray(new ItemStack[1]));
}
} }
else if (object instanceof ShapedOreRecipe) else if (object instanceof ShapedOreRecipe)
{ {
@ -271,7 +282,11 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
ArrayList<ItemStack> hasResources = this.hasResource(oreRecipeInput); ArrayList<ItemStack> hasResources = this.hasResource(oreRecipeInput);
if (hasResources != null) { return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), hasResources.toArray(new ItemStack[1])); } if (hasResources != null)
{
return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), hasResources.toArray(new ItemStack[1]));
}
} }
else if (object instanceof ShapelessOreRecipe) else if (object instanceof ShapelessOreRecipe)
{ {
@ -280,7 +295,10 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
List<ItemStack> hasResources = this.hasResource(oreRecipeInput.toArray()); List<ItemStack> hasResources = this.hasResource(oreRecipeInput.toArray());
if (hasResources != null) { return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), hasResources.toArray(new ItemStack[1])); } if (hasResources != null)
{
return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), hasResources.toArray(new ItemStack[1]));
}
} }
} }
} }
@ -297,6 +315,10 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
*/ */
public ArrayList<ItemStack> hasResource(Object[] recipeItems) public ArrayList<ItemStack> hasResource(Object[] recipeItems)
{ {
TileEntityImprinter test = new TileEntityImprinter();
NBTTagCompound cloneData = new NBTTagCompound();
this.writeToNBT(cloneData);
test.readFromNBT(cloneData);
/** /**
* The actual amount of resource required. Each ItemStack will only have stacksize of 1. * The actual amount of resource required. Each ItemStack will only have stacksize of 1.
*/ */
@ -312,15 +334,16 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
if (recipeItem != null) if (recipeItem != null)
{ {
for (int i = START_INVENTORY; i < this.getSizeInventory(); i++) for (int i = START_INVENTORY; i < test.getSizeInventory(); i++)
{ {
ItemStack checkStack = this.getStackInSlot(i); ItemStack checkStack = test.getStackInSlot(i);
if (checkStack != null) if (checkStack != null)
{ {
if (recipeItem.isItemEqual(checkStack)) if (recipeItem.isItemEqual(checkStack) || (recipeItem.itemID == checkStack.itemID && recipeItem.getItemDamage() < 0))
{ {
// TODO Do NBT CHecking // TODO Do NBT Checking
test.decrStackSize(i, 1);
itemMatch++; itemMatch++;
break; break;
} }
@ -343,15 +366,16 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
if (recipeItem != null) if (recipeItem != null)
{ {
for (int i = START_INVENTORY; i < this.getSizeInventory(); i++) for (int i = START_INVENTORY; i < test.getSizeInventory(); i++)
{ {
ItemStack checkStack = this.getStackInSlot(i); ItemStack checkStack = test.getStackInSlot(i);
if (checkStack != null) if (checkStack != null)
{ {
if (recipeItem.isItemEqual(checkStack)) if (recipeItem.isItemEqual(checkStack) || (recipeItem.itemID == checkStack.itemID && recipeItem.getItemDamage() < 0))
{ {
// TODO Do NBT CHecking // TODO Do NBT CHecking
test.decrStackSize(i, 1);
itemMatch++; itemMatch++;
break optionsLoop; break optionsLoop;
} }
@ -414,4 +438,103 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
nbt.setTag("Items", var2); nbt.setTag("Items", var2);
} }
@Override
public boolean onUse(TileEntityArmbot tileEntity, Entity heldEntity)
{
if (heldEntity != null)
{
if (heldEntity instanceof EntityItem)
{
ItemStack stack = ((EntityItem) heldEntity).func_92014_d();
if (this.getStackInSlot(3) == null && stack != null && stack.itemID == AssemblyLine.itemImprint.itemID) // no crafting imprint and stack not null and stack is imprint
{
this.setInventorySlotContents(3, stack);
this.onInventoryChanged();
tileEntity.grabbedEntities.remove(0);
return true;
}
else if (this.getStackInSlot(3) != null && stack != null)
{
ItemStack result = this.getStackInSlot(4); // crafting result
if (result != null)
{
result = this.getStackInSlot(4);
if (stack.isItemEqual(result))
{
if (result != null)
{
ItemStack[] requiredItems = this.getIdealRecipe(result).getValue().clone();
if (requiredItems != null)
{
for (ItemStack searchStack : requiredItems)
{
for (int i = 0; i < this.getSizeInventory(); i++)
{
ItemStack checkStack = this.getStackInSlot(i);
if (checkStack != null)
{
if (searchStack.isItemEqual(checkStack))
{
this.decrStackSize(i, 1);
break;
}
}
}
}
}
}
if (stack.isStackable())
{
stack.stackSize += result.stackSize;
this.onInventoryChanged();
tileEntity.grabbedEntities.remove(0);
tileEntity.grabbedEntities.add(new EntityItem(this.worldObj, this.xCoord, this.yCoord, this.zCoord, stack));
return true;
}
}
}
}
}
}
else
{
ItemStack result = this.getStackInSlot(4); // crafting result
if (result != null)
{
result = this.getStackInSlot(4);
if (result != null)
{
ItemStack[] requiredItems = this.getIdealRecipe(result).getValue().clone();
if (requiredItems != null)
{
for (ItemStack searchStack : requiredItems)
{
for (int i = 0; i < this.getSizeInventory(); i++)
{
ItemStack checkStack = this.getStackInSlot(i);
if (checkStack != null)
{
if (searchStack.isItemEqual(checkStack) || (searchStack.itemID == checkStack.itemID && searchStack.getItemDamage() < 0))
{
this.decrStackSize(i, 1);
break;
}
}
}
}
}
}
this.onInventoryChanged();
tileEntity.grabbedEntities.add(new EntityItem(this.worldObj, this.xCoord, this.yCoord, this.zCoord, result));
return true;
}
}
return false;
}
} }