Made crates spread items

This commit is contained in:
Henry Mao 2013-02-10 23:49:47 +08:00
parent afe1260213
commit e9366ccd15
2 changed files with 89 additions and 39 deletions

View file

@ -74,7 +74,7 @@ public class AssemblyLine
public static Block blockConveyorBelt; public static Block blockConveyorBelt;
public static Block blockManipulator; public static Block blockManipulator;
public static Block blockCrate; public static BlockCrate blockCrate;
public static Block blockImprinter; public static Block blockImprinter;
public static Block blockEncoder; public static Block blockEncoder;
public static Block blockDetector; public static Block blockDetector;

View file

@ -9,7 +9,9 @@ import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.UniversalElectricity; import universalelectricity.core.UniversalElectricity;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.BlockMachine; import universalelectricity.prefab.BlockMachine;
import assemblyline.common.AssemblyLine; import assemblyline.common.AssemblyLine;
import assemblyline.common.TabAssemblyLine; import assemblyline.common.TabAssemblyLine;
@ -52,19 +54,7 @@ public class BlockCrate extends BlockMachine
tileEntity.prevClickTime = world.getWorldTime(); tileEntity.prevClickTime = world.getWorldTime();
if (allMode) this.tryEject(tileEntity, player, allMode);
{
this.ejectItems(tileEntity, player, tileEntity.getMaxLimit());
}
else
{
ItemStack stack = tileEntity.getStackInSlot(0);
if (stack != null)
{
this.ejectItems(tileEntity, player, stack.getMaxStackSize());
}
}
} }
} }
@ -97,23 +87,76 @@ public class BlockCrate extends BlockMachine
// Add items // Add items
if (side == 1 || (side > 1 && hitY > 0.5) || !player.capabilities.isCreativeMode) if (side == 1 || (side > 1 && hitY > 0.5) || !player.capabilities.isCreativeMode)
{ {
this.tryInsert(tileEntity, player, allMode);
}
// Remove items
else if (side == 0 || (side > 1 && hitY <= 0.5))
{
this.tryEject(tileEntity, player, allMode);
}
}
}
return true;
}
/**
* Try to inject it into the crate. Otherwise, look around for nearby crates and try to put them
* in.
*/
public void tryInsert(TileEntityCrate tileEntity, EntityPlayer player, boolean allMode, boolean doSearch)
{
boolean success;
if (allMode) if (allMode)
{ {
this.insertAllItems(tileEntity, player); success = this.insertAllItems(tileEntity, player);
} }
else else
{ {
this.insertCurrentItem(tileEntity, player); success = this.insertCurrentItem(tileEntity, player);
}
if (!success && doSearch)
{
int radius = 10;
for (int x = -radius; x < radius; x++)
{
for (int y = -radius; y < radius; y++)
{
for (int z = -radius; z < radius; z++)
{
Vector3 position = Vector3.add(new Vector3(tileEntity), new Vector3(x, y, z));
TileEntity checkTile = position.getTileEntity(tileEntity.worldObj);
if (checkTile instanceof TileEntityCrate)
{
AssemblyLine.blockCrate.tryInsert(((TileEntityCrate) checkTile), player, allMode, false);
} }
} }
// remove items }
else if (side == 0 || (side > 1 && hitY <= 0.5)) }
}
}
public void tryInsert(TileEntityCrate tileEntity, EntityPlayer player, boolean allMode)
{
this.tryInsert(tileEntity, player, allMode, true);
}
public void tryEject(TileEntityCrate tileEntity, EntityPlayer player, boolean allMode)
{ {
if (allMode) if (allMode)
{ {
this.ejectItems(tileEntity, player, tileEntity.getMaxLimit()); this.ejectItems(tileEntity, player, tileEntity.getMaxLimit());
} }
else else
{
if (player.isSneaking())
{
this.ejectItems(tileEntity, player, 1);
}
else
{ {
ItemStack stack = tileEntity.getStackInSlot(0); ItemStack stack = tileEntity.getStackInSlot(0);
@ -124,10 +167,6 @@ public class BlockCrate extends BlockMachine
} }
} }
} }
}
return true;
}
/** /**
* Inserts a the itemStack the player is holding into the crate. * Inserts a the itemStack the player is holding into the crate.
@ -140,9 +179,18 @@ public class BlockCrate extends BlockMachine
{ {
if (currentStack.isStackable()) if (currentStack.isStackable())
{ {
if (tileEntity.getStackInSlot(0) != null)
{
if (!tileEntity.getStackInSlot(0).isItemEqual(currentStack))
{
return false;
}
}
player.inventory.setInventorySlotContents(player.inventory.currentItem, this.putIn(tileEntity, currentStack)); player.inventory.setInventorySlotContents(player.inventory.currentItem, this.putIn(tileEntity, currentStack));
return true; return true;
}// if the item being used is a create then try to merge the items inside }
// If the item being used is a create then try to merge the items inside
else if (currentStack.getItem().itemID == AssemblyLine.blockCrate.blockID) else if (currentStack.getItem().itemID == AssemblyLine.blockCrate.blockID)
{ {
ItemStack containedStack = ItemBlockCrate.getContainingItemStack(currentStack); ItemStack containedStack = ItemBlockCrate.getContainingItemStack(currentStack);
@ -163,7 +211,7 @@ public class BlockCrate extends BlockMachine
/** /**
* Inserts all items of the same type this player has into the crate. * Inserts all items of the same type this player has into the crate.
* *
* @return * @return True on success
*/ */
public boolean insertAllItems(TileEntityCrate tileEntity, EntityPlayer player) public boolean insertAllItems(TileEntityCrate tileEntity, EntityPlayer player)
{ {
@ -197,11 +245,12 @@ public class BlockCrate extends BlockMachine
{ {
((EntityPlayerMP) player).sendContainerToPlayer(player.inventoryContainer); ((EntityPlayerMP) player).sendContainerToPlayer(player.inventoryContainer);
} }
return true;
} }
} }
} }
return true;
} }
} }
@ -249,6 +298,7 @@ public class BlockCrate extends BlockMachine
return true; return true;
} }
return false; return false;
} }