diff --git a/.gitignore b/.gitignore index 2d09f3731..4513168e6 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,6 @@ CHANGELOG !/src/ /src/minecraft/* - !/src/minecraft/assemblyline/ !/src/minecraft/universalelectricity/ !/resources/ diff --git a/resources/assemblyline/textures/sensor.png b/resources/assemblyline/textures/sensor.png new file mode 100644 index 000000000..aeab69d9e Binary files /dev/null and b/resources/assemblyline/textures/sensor.png differ diff --git a/src/minecraft/assemblyline/client/ClientProxy.java b/src/minecraft/assemblyline/client/ClientProxy.java index 163cbcda9..301e1597c 100644 --- a/src/minecraft/assemblyline/client/ClientProxy.java +++ b/src/minecraft/assemblyline/client/ClientProxy.java @@ -22,6 +22,7 @@ import cpw.mods.fml.client.registry.RenderingRegistry; public class ClientProxy extends CommonProxy { + @Override public void preInit() { diff --git a/src/minecraft/assemblyline/client/gui/GuiItemSensor.java b/src/minecraft/assemblyline/client/gui/GuiItemSensor.java new file mode 100644 index 000000000..06cfdaff9 --- /dev/null +++ b/src/minecraft/assemblyline/client/gui/GuiItemSensor.java @@ -0,0 +1,85 @@ +package assemblyline.client.gui; + +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiSmallButton; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.StatCollector; + +import org.lwjgl.opengl.GL11; + +import assemblyline.common.machine.sensor.ContainerItemSensor; +import assemblyline.common.machine.sensor.TileItemSensor; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +@SideOnly(Side.CLIENT) +public class GuiItemSensor extends GuiContainer +{ + private EntityPlayer player; + private GuiSmallButton invert; + private TileItemSensor tileEntity; + + public GuiItemSensor(EntityPlayer player, TileItemSensor tileEntity) + { + super(new ContainerItemSensor(player.inventory, tileEntity)); + this.tileEntity = tileEntity; + this.player = player; + this.allowUserInput = false; + short baseHeight = 222; + int var4 = baseHeight - 108; + this.ySize = var4 + 3 * 18; + } + + @Override + public void initGui() + { + super.initGui(); + + invert = new GuiSmallButton(0, this.guiLeft + 82, this.guiTop + 5, 12, 12, "i"); + controlList.add(invert); + } + + @Override + protected void actionPerformed(GuiButton button) + { + switch (button.id) + { + case 0: //invert + { + PacketHandler.sendTileEntityAction(player, tileEntity.xCoord, tileEntity.yCoord, tileEntity.zCoord, PacketHandler.PACKET_ACTION_ITEM_SENSOR); + } + } + } + + @Override + public void updateScreen() + { + invert.displayString = tileEntity.isItemCheckInverted() ? "e" : "i"; + super.updateScreen(); + } + + /** + * Draw the foreground layer for the GuiContainer (everything in front of the items) + */ + protected void drawGuiContainerForegroundLayer(int par1, int par2) + { + fontRenderer.drawString("Item Sensor", 8, 6, 4210752); + fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, ySize - 96 + 2, 4210752); + } + + /** + * Draw the background layer for the GuiContainer (everything behind the items) + */ + protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) + { + int var4 = this.mc.renderEngine.getTexture("/gui/container.png"); + GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); + this.mc.renderEngine.bindTexture(var4); + int var5 = (this.width - this.xSize) / 2; + int var6 = (this.height - this.ySize) / 2; + this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, 3 * 18 + 17); + this.drawTexturedModalRect(var5, var6 + 3 * 18 + 17, 0, 126, this.xSize, 96); + } +} diff --git a/src/minecraft/assemblyline/client/model/ModelHelper.java b/src/minecraft/assemblyline/client/model/ModelHelper.java new file mode 100644 index 000000000..52a88a45a --- /dev/null +++ b/src/minecraft/assemblyline/client/model/ModelHelper.java @@ -0,0 +1,216 @@ +package assemblyline.client.model; + +import static org.lwjgl.opengl.GL11.GL_QUADS; +import static org.lwjgl.opengl.GL11.glBegin; +import static org.lwjgl.opengl.GL11.glEnd; +import static org.lwjgl.opengl.GL11.glTexCoord2f; +import static org.lwjgl.opengl.GL11.glVertex3d; +import assemblyline.common.BlockSide; +import net.minecraft.util.Vec3; + +public class ModelHelper +{ + private static int gTexWidth = 64; + private static int gTexHeight = 32; + private static int sTexWidth = 64; + private static int sTexHeight = 32; + private static int texOffsetX = 0; + private static int texOffsetY = 0; + private static float texScale = 16f; // 16 pixels per world unit + private static boolean clip = false; // clip textures instead of scaling them + + /** + * + * @param v1 + * Top Left + * @param v2 + * Top Right + * @param v3 + * Bottom Right + * @param v4 + * Bottom Left + */ + private static void drawQuadRaw(Vec3 v1, Vec3 v2, Vec3 v3, Vec3 v4, int side) + { + glBegin(GL_QUADS); + + float quadWidth = 1; + float quadHeight = 1; + + float subWidth = ((float) sTexWidth / (float) gTexWidth); + float subHeight = ((float) sTexHeight / (float) gTexHeight); + float xMin = ((float) texOffsetX / sTexWidth) * subWidth; + float yMin = ((float) texOffsetY / sTexHeight) * subHeight; + float subSqX = 0; + float subSqY = 0; + float subSqWidth = 0.25f * ((float) sTexWidth / (float) gTexWidth); // constant for now + float subSqHeight = 0.5f * ((float) sTexHeight / (float) gTexHeight); + + // System.out.println("SubWidth: " + subWidth); + + switch (side) + { + case BlockSide.YPLUS: // top + { + subSqX = 2f * subSqWidth; + subSqY = 0; + quadWidth = (float) Math.abs(v2.xCoord - v1.xCoord); + quadHeight = (float) Math.abs(v4.zCoord - v1.zCoord); + break; + } + case BlockSide.YMINUS: // bottom + { + subSqX = 1f * subSqWidth; + subSqY = 0; + quadWidth = (float) Math.abs(v2.xCoord - v1.xCoord); + quadHeight = (float) Math.abs(v4.zCoord - v1.zCoord); + break; + } + case BlockSide.XPLUS: // right + { + subSqX = 0; + subSqY = subSqHeight; + quadWidth = (float) Math.abs(v2.zCoord - v1.zCoord); + quadHeight = (float) Math.abs(v4.yCoord - v1.yCoord); + break; + } + case BlockSide.XMINUS: // left + { + subSqX = 2f * subSqWidth; + subSqY = subSqHeight; + quadWidth = (float) Math.abs(v2.zCoord - v1.zCoord); + quadHeight = (float) Math.abs(v4.yCoord - v1.yCoord); + break; + } + case BlockSide.ZPLUS: // back + { + subSqX = subSqWidth; + subSqY = subSqHeight; + quadWidth = (float) Math.abs(v2.xCoord - v1.xCoord); + quadHeight = (float) Math.abs(v4.yCoord - v1.yCoord); + break; + } + case BlockSide.ZMINUS: // front + { + subSqX = 3f * subSqWidth; + subSqY = subSqHeight; + quadWidth = (float) Math.abs(v2.xCoord - v1.xCoord); + quadHeight = (float) Math.abs(v4.yCoord - v1.yCoord); + break; + } + } + + float xMax, yMax; + + xMin += subSqX; + yMin += subSqY; + + if (clip) + { + xMin += (1f - quadWidth) * subSqWidth; + yMin += (1f - quadHeight) * subSqHeight; + xMax = xMin + (subSqWidth * quadWidth); + yMax = yMin + (subSqHeight * quadHeight); + } + else + { + xMax = xMin + (subSqWidth); + yMax = yMin + (subSqHeight); + } + + + + // System.out.println("xMin: " + xMin + "; xMax: " + xMax); + + glTexCoord2f(xMin, yMin); + glVertex3d(v1.xCoord, v1.yCoord, v1.zCoord); + glTexCoord2f(xMax, yMin); + glVertex3d(v2.xCoord, v2.yCoord, v2.zCoord); + glTexCoord2f(xMax, yMax); + glVertex3d(v3.xCoord, v3.yCoord, v3.zCoord); + glTexCoord2f(xMin, yMax); + glVertex3d(v4.xCoord, v4.yCoord, v4.zCoord); + + glEnd(); + } + + /** + * + * @param v1 + * Top Left Back + * @param v2 + * Top Right Back + * @param v3 + * Top Right Front + * @param v4 + * Top Left Front + * @param v5 + * Bottom Left Front + * @param v6 + * Bottom Right Front + * @param v7 + * Bottom Right Back + * @param v8 + * Bottom Left Back + */ + private static void drawCuboidRaw(Vec3 v1, Vec3 v2, Vec3 v3, Vec3 v4, Vec3 v5, Vec3 v6, Vec3 v7, Vec3 v8) + { + drawQuadRaw(v1, v2, v3, v4, BlockSide.YPLUS); // top + drawQuadRaw(v7, v6, v3, v2, BlockSide.XPLUS); // right + drawQuadRaw(v5, v6, v7, v8, BlockSide.YMINUS); // bottom + drawQuadRaw(v5, v8, v1, v4, BlockSide.XMINUS); // left + drawQuadRaw(v6, v5, v4, v3, BlockSide.ZMINUS); // front + drawQuadRaw(v8, v7, v2, v1, BlockSide.ZPLUS); // back + } + + public static void drawCuboid(float xOffset, float yOffset, float zOffset, float xSize, float ySize, float zSize) + { + Vec3 v1, v2, v3, v4, v5, v6, v7, v8; + float x, y, z; + float x2, y2, z2; + x = xOffset; + y = yOffset; + z = zOffset; + x2 = x + xSize; + y2 = y + ySize; + z2 = z + zSize; + v1 = Vec3.createVectorHelper(x, y2, z2); + v2 = Vec3.createVectorHelper(x2, y2, z2); + v3 = Vec3.createVectorHelper(x2, y2, z); + v4 = Vec3.createVectorHelper(x, y2, z); + v5 = Vec3.createVectorHelper(x, y, z); + v6 = Vec3.createVectorHelper(x2, y, z); + v7 = Vec3.createVectorHelper(x2, y, z2); + v8 = Vec3.createVectorHelper(x, y, z2); + drawCuboidRaw(v1, v2, v3, v4, v5, v6, v7, v8); + } + + public static void setTextureOffset(int xOffset, int yOffset) + { + texOffsetX = xOffset; + texOffsetY = yOffset; + } + + public static void setGlobalTextureResolution(int width, int height) + { + gTexWidth = width; + gTexHeight = height; + } + + public static void setTextureSubResolution(int width, int height) + { + sTexWidth = width; + sTexHeight = height; + } + + /** + * Sets whether or not to clip the texture. + * + * @param clip + * If true, textures on blocks less than 1x1x1 will be clipped. If false, they will be scaled. + */ + public static void setTextureClip(boolean clip) + { + ModelHelper.clip = clip; + } +} diff --git a/src/minecraft/assemblyline/client/model/ModelItemSensor.java b/src/minecraft/assemblyline/client/model/ModelItemSensor.java new file mode 100644 index 000000000..6ffaf7212 --- /dev/null +++ b/src/minecraft/assemblyline/client/model/ModelItemSensor.java @@ -0,0 +1,62 @@ +package assemblyline.client.model; + +import static assemblyline.client.model.ModelHelper.*; +import static org.lwjgl.opengl.GL11.glPopMatrix; +import static org.lwjgl.opengl.GL11.glPushMatrix; +import static org.lwjgl.opengl.GL11.glScalef; +import static org.lwjgl.opengl.GL11.glTranslated; +import net.minecraft.client.model.ModelBase; +import net.minecraftforge.client.ForgeHooksClient; + +import assemblyline.common.AssemblyLine; +import assemblyline.common.CommonProxy; +import assemblyline.common.machine.sensor.TileItemSensor; + +public class ModelItemSensor extends ModelBase +{ + + public void render(TileItemSensor entity, double x, double y, double z) + { + glPushMatrix(); + glTranslated(x, y, z); + + ForgeHooksClient.bindTexture(AssemblyLine.TEXTURE_PATH + "sensor.png", 0); + + setGlobalTextureResolution(128, 128); + setTextureClip(false); + ModelHelper.setTextureOffset(0, 64); + setTextureSubResolution(64, 64); + drawCuboid(0.45f, 12f/16f, 0.45f, 2f/16f, 4f/16f, 2f/16f); //stand + ModelHelper.setTextureOffset(0, 0); + setTextureSubResolution(128, 64); + drawCuboid(0.25f, 0.25f, 0.25f, 8f/16f, 8f/16f, 8f/16f); //block + ModelHelper.setTextureOffset(64, 64); + setTextureSubResolution(64, 32); + drawCuboid(0.375f, 0.25f - (1f/16f), 0.375f, 4f/16f, 1f/16f, 4f/16f); //lens + + glPopMatrix(); + } + + public void render() + { + glPushMatrix(); + glScalef(1.5f, 1.5f, 1.5f); + + ForgeHooksClient.bindTexture(AssemblyLine.TEXTURE_PATH + "sensor.png", 0); + + setGlobalTextureResolution(128, 128); + setTextureClip(false); + ModelHelper.setTextureOffset(0, 64); + setTextureSubResolution(64, 64); + drawCuboid(0.45f, 12f/16f, 0.45f, 2f/16f, 4f/16f, 2f/16f); //stand + ModelHelper.setTextureOffset(0, 0); + setTextureSubResolution(128, 64); + drawCuboid(0.25f, 0.25f, 0.25f, 8f/16f, 8f/16f, 8f/16f); //block + ModelHelper.setTextureOffset(64, 64); + setTextureSubResolution(64, 32); + drawCuboid(0.375f, 0.25f - (1f/16f), 0.375f, 4f/16f, 1f/16f, 4f/16f); //lens + + glPopMatrix(); + } + +} diff --git a/src/minecraft/assemblyline/client/render/RenderItemSensor.java b/src/minecraft/assemblyline/client/render/RenderItemSensor.java new file mode 100644 index 000000000..e28841ee3 --- /dev/null +++ b/src/minecraft/assemblyline/client/render/RenderItemSensor.java @@ -0,0 +1,27 @@ +package assemblyline.client.render; + +import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; +import net.minecraft.tileentity.TileEntity; + +import assemblyline.client.model.ModelItemSensor; +import assemblyline.common.machine.sensor.TileItemSensor; + +public class RenderItemSensor extends TileEntitySpecialRenderer +{ + ModelItemSensor model; + + public RenderItemSensor() + { + model = new ModelItemSensor(); + } + + @Override + public void renderTileEntityAt(TileEntity tileEntity, double x, double y, double z, float var8) + { + if (tileEntity instanceof TileItemSensor) + { + model.render((TileItemSensor) tileEntity, x, y, z); + } + } + +} diff --git a/src/minecraft/assemblyline/common/BlockSide.java b/src/minecraft/assemblyline/common/BlockSide.java new file mode 100644 index 000000000..62abad838 --- /dev/null +++ b/src/minecraft/assemblyline/common/BlockSide.java @@ -0,0 +1,11 @@ +package assemblyline.common; + +public class BlockSide +{ + public static final int YMINUS = 0; + public static final int YPLUS = 1; + public static final int ZMINUS = 2; + public static final int ZPLUS = 3; + public static final int XMINUS = 4; + public static final int XPLUS = 5; +} diff --git a/src/minecraft/assemblyline/common/CommonProxy.java b/src/minecraft/assemblyline/common/CommonProxy.java index 339c64689..beef074d3 100644 --- a/src/minecraft/assemblyline/common/CommonProxy.java +++ b/src/minecraft/assemblyline/common/CommonProxy.java @@ -16,6 +16,9 @@ public class CommonProxy implements IGuiHandler public static final int GUI_REJECTOR = 0; public static final int GUI_STAMPER = 1; public static final int GUI_ARCHITECHT_TABLE = 2; + public static final int GUI_SENSOR = 3; + + public static final int RENDER_SENSOR = 0; public void preInit() { diff --git a/src/minecraft/assemblyline/common/machine/sensor/BlockItemSensor.java b/src/minecraft/assemblyline/common/machine/sensor/BlockItemSensor.java new file mode 100644 index 000000000..a4d185e36 --- /dev/null +++ b/src/minecraft/assemblyline/common/machine/sensor/BlockItemSensor.java @@ -0,0 +1,222 @@ +package assemblyline.common.machine.sensor; + +import java.util.Random; + +import assemblyline.client.ClientProxy; +import assemblyline.common.AssemblyLine; +import assemblyline.common.BlockSide; +import assemblyline.common.CommonProxy; + +import net.minecraft.block.BlockContainer; +import net.minecraft.block.material.Material; +import net.minecraft.entity.Entity; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; +import net.minecraftforge.common.ForgeDirection; + +public class BlockItemSensor extends BlockContainer +{ + private Random random = new Random(); + + public BlockItemSensor(int blockID, Material material) + { + super(blockID, material); + this.blockIndexInTexture = 0; + setBlockBounds(0.25f, 0, 0.25f, 0.75f, 0.75f, 0.75f); + } + + @Override + public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9) + { + TileEntity tileEntity = world.getBlockTileEntity(x, y, z); + if (tileEntity == null || player.isSneaking()) + { + return false; + } + + if (tileEntity instanceof TileItemSensor) + { + player.openGui(AssemblyLine.instance, ClientProxy.GUI_SENSOR, world, x, y, z); + return true; + } + + return false; + } + + /*@Override + public void onBlockPlacedBy(World world, int x, int y, int z, EntityLiving entity) + { + if (entity instanceof EntityPlayer) + { + world.notifyBlocksOfNeighborChange(x + 1, y, z, blockID); + world.notifyBlocksOfNeighborChange(x - 1, y, z, blockID); + world.notifyBlocksOfNeighborChange(x, y, z + 1, blockID); + world.notifyBlocksOfNeighborChange(x, y, z - 1, blockID); + } + }*/ + + @Override + public void breakBlock(World world, int x, int y, int z, int int1, int int2) + { + TileItemSensor te = (TileItemSensor) world.getBlockTileEntity(x, y, z); + + if (te != null) + { + for (int i = 0; i < te.getSizeInventory(); ++i) + { + ItemStack stack = te.getStackInSlot(i); + + if (stack != null) + { + float xShift = this.random.nextFloat() * 0.8F + 0.1F; + float yShift = this.random.nextFloat() * 0.8F + 0.1F; + EntityItem eI; + + for (float zShift = this.random.nextFloat() * 0.8F + 0.1F; stack.stackSize > 0; world.spawnEntityInWorld(eI)) + { + int count = this.random.nextInt(21) + 10; + + if (count > stack.stackSize) + { + count = stack.stackSize; + } + + stack.stackSize -= count; + eI = new EntityItem(world, (double) ((float) x + xShift), (double) ((float) y + yShift), (double) ((float) z + zShift), new ItemStack(stack.itemID, count, stack.getItemDamage())); + float var15 = 0.05F; + eI.motionX = (double) ((float) this.random.nextGaussian() * var15); + eI.motionY = (double) ((float) this.random.nextGaussian() * var15 + 0.2F); + eI.motionZ = (double) ((float) this.random.nextGaussian() * var15); + + if (stack.hasTagCompound()) + { + eI.func_92014_d().setTagCompound((NBTTagCompound) stack.getTagCompound().copy()); + } + } + } + } + } + + super.breakBlock(world, x, y, z, int1, int2); + } + + @Override + public void onNeighborBlockChange(World world, int x, int y, int z, int blockID) + { + if (!canBlockStay(world, x, y, z)) + { + this.dropBlockAsItem(world, x, y, z, world.getBlockMetadata(x, y, z), 0); + world.setBlockWithNotify(x, y, z, 0); + } + } + + @Override + public void setBlockBoundsBasedOnState(IBlockAccess par1iBlockAccess, int par2, int par3, int par4) + { + setBlockBounds(0.25f, 0.25f, 0.25f, 0.75f, 0.75f, 0.75f); + } + + @Override + public boolean isOpaqueCube() + { + return false; + } + + @Override + public boolean isBlockSolidOnSide(World world, int x, int y, int z, ForgeDirection side) + { + return false; + } + + @Override + public boolean renderAsNormalBlock() + { + return false; + } + + @Override + public int getRenderType() + { + return CommonProxy.RENDER_SENSOR; + } + + @Override + public String getTextureFile() + { + return AssemblyLine.BLOCK_TEXTURE_PATH; + } + + @Override + public void onEntityWalking(World world, int x, int y, int z, Entity entity) + { + onEntityCollidedWithBlock(world, x, y, z, entity); + } + + @Override + public boolean isProvidingStrongPower(IBlockAccess world, int x, int y, int z, int direction) + { + if (direction == BlockSide.YMINUS) + { + TileEntity te = world.getBlockTileEntity(x, y, z); + if (te != null) + { + if (te instanceof TileItemSensor) + { + TileItemSensor tis = (TileItemSensor) te; + if (tis.isPowering()) + return true; + } + } + } + + return false; + } + + @Override + public boolean isProvidingWeakPower(IBlockAccess world, int x, int y, int z, int direction) + { + if (direction != BlockSide.YMINUS && direction != BlockSide.YPLUS) + { + TileEntity te = world.getBlockTileEntity(x, y, z); + + if (te != null) + { + if (te instanceof TileItemSensor) + { + TileItemSensor tis = (TileItemSensor) te; + if (tis.isPowering()) + return true; + } + } + } + + return false; + } + + @Override + public boolean canBlockStay(World world, int x, int y, int z) + { + if (world.isBlockSolidOnSide(x, y + 1, z, ForgeDirection.DOWN)) + return true; + + return false; + } + + @Override + public boolean canPlaceBlockAt(World world, int x, int y, int z) + { + return canBlockStay(world, x, y, z); + } + + @Override + public TileEntity createNewTileEntity(World world) + { + return new TileItemSensor(); + } + +} diff --git a/src/minecraft/assemblyline/common/machine/sensor/ContainerItemSensor.java b/src/minecraft/assemblyline/common/machine/sensor/ContainerItemSensor.java new file mode 100644 index 000000000..93ea3da94 --- /dev/null +++ b/src/minecraft/assemblyline/common/machine/sensor/ContainerItemSensor.java @@ -0,0 +1,100 @@ +package assemblyline.common.machine.sensor; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; + +public class ContainerItemSensor extends Container +{ + protected TileItemSensor tileEntity; + + public ContainerItemSensor(InventoryPlayer inventoryPlayer, TileItemSensor te) + { + tileEntity = te; + + int numRows = 3; + int var3 = (numRows - 4) * 18; + int x; + int y; + + for (x = 0; x < numRows; ++x) + { + for (y = 0; y < 9; ++y) + { + this.addSlotToContainer(new Slot(te, y + x * 9, 8 + y * 18, 18 + x * 18)); + } + } + + for (x = 0; x < 3; ++x) + { + for (y = 0; y < 9; ++y) + { + this.addSlotToContainer(new Slot(inventoryPlayer, y + x * 9 + 9, 8 + y * 18, 103 + x * 18 + var3)); + } + } + + for (x = 0; x < 9; ++x) + { + this.addSlotToContainer(new Slot(inventoryPlayer, x, 8 + x * 18, 161 + var3)); + } + } + + public ItemStack func_82846_b(EntityPlayer player, int slotNum) + { + ItemStack stack = null; + Slot slot = (Slot)this.inventorySlots.get(slotNum); + + if (slot != null && slot.getHasStack()) + { + ItemStack slotStack = slot.getStack(); + stack = slotStack.copy(); + + if (slotNum < 3 * 9) + { + if (!this.mergeItemStack(slotStack, 3 * 9, this.inventorySlots.size(), true)) + { + return null; + } + } + else if (!this.mergeItemStack(slotStack, 0, 3 * 9, false)) + { + return null; + } + + if (slotStack.stackSize == 0) + { + slot.putStack((ItemStack)null); + } + else + { + slot.onSlotChanged(); + } + } + + return stack; + } + + @Override + public boolean canInteractWith(EntityPlayer player) + { + return tileEntity.isUseableByPlayer(player); + } + + protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) + { + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 9; j++) + { + addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); + } + } + + for (int i = 0; i < 9; i++) + { + addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142)); + } + } +} \ No newline at end of file diff --git a/src/minecraft/assemblyline/common/machine/sensor/TileItemSensor.java b/src/minecraft/assemblyline/common/machine/sensor/TileItemSensor.java new file mode 100644 index 000000000..f2a4122f4 --- /dev/null +++ b/src/minecraft/assemblyline/common/machine/sensor/TileItemSensor.java @@ -0,0 +1,328 @@ +package assemblyline.common.machine.sensor; + +import java.util.ArrayList; + +import assemblyline.common.AssemblyLine; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.network.INetworkManager; +import net.minecraft.network.packet.Packet; +import net.minecraft.network.packet.Packet132TileEntityData; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.AxisAlignedBB; + +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.relauncher.Side; + +public class TileItemSensor extends TileEntity implements IInventory +{ + private boolean powering; + private boolean invertItemCheck; + private ItemStack[] iContents = new ItemStack[27]; + + public TileItemSensor() + { + powering = false; + } + + public boolean hasItems() + { + for (int i = 0; i < iContents.length; i++) + { + if (iContents[i] != null) + { + return true; + } + } + + return false; + } + + @Override + public void updateEntity() + { + int meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord) & 3; + AxisAlignedBB testArea = AxisAlignedBB.getBoundingBox(xCoord, yCoord - 1, zCoord, xCoord + 1, yCoord, zCoord + 1); + + ArrayList entities = (ArrayList) worldObj.getEntitiesWithinAABB(EntityItem.class, testArea); + boolean tPowering = false; + if (entities.size() > 0) + { + if (hasItems()) + { + for (int i = 0; i < entities.size(); i++) + { + EntityItem e = (EntityItem) entities.get(i); + ItemStack item = e.func_92014_d(); + boolean tFound = false; + + for (int ii = 0; ii < iContents.length; ii++) + { + ItemStack compare = iContents[ii]; + if (compare != null) + { + if (invertItemCheck) + { + if (item.itemID == compare.itemID) + { + if (item.getItemDamage() == compare.getItemDamage()) + { + if (item.hasTagCompound()) + { + if (item.getTagCompound().equals(compare.getTagCompound())) + { + tFound = true; + break; + } + } + else + { + tFound = true; + break; + } + } + } + } + else + { + if (item.itemID == compare.itemID) + { + if (item.getItemDamage() == compare.getItemDamage()) + { + if (item.hasTagCompound()) + { + if (item.getTagCompound().equals(compare.getTagCompound())) + { + tPowering = true; + break; + } + } + else + { + tPowering = true; + break; + } + } + } + } + } + } + if (invertItemCheck) + { + if (!tFound) + { + tPowering = true; + break; + } + } + } + } + else + { + tPowering = true; + } + } + else + { + tPowering = false; + } + if (tPowering != powering) + { + powering = tPowering; + worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, AssemblyLine.blockSensor.blockID); + worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord + 1, zCoord, AssemblyLine.blockSensor.blockID); + for (int x = xCoord - 1; x <= xCoord + 1; x++) + { + for (int z = zCoord - 1; z <= zCoord + 1; z++) + { + worldObj.notifyBlocksOfNeighborChange(x, yCoord + 1, z, AssemblyLine.blockSensor.blockID); + } + } + } + } + + public boolean isPowering() + { + return powering; + } + + public boolean isItemCheckInverted() + { + return invertItemCheck; + } + + public void setItemCheckInverted(boolean inverted) + { + invertItemCheck = inverted; + } + + @Override + public Packet getDescriptionPacket() + { + Packet132TileEntityData pack = new Packet132TileEntityData(); + pack.xPosition = xCoord; + pack.yPosition = yCoord; + pack.zPosition = zCoord; + pack.actionType = 0; + NBTTagCompound tag = new NBTTagCompound(); + writeToNBT(tag); + pack.customParam1 = tag; + return pack; + } + + @Override + public void onDataPacket(INetworkManager netManager, Packet132TileEntityData packet) + { + super.onDataPacket(netManager, packet); + readFromNBT(packet.customParam1); + } + + @Override + public void readFromNBT(NBTTagCompound tag) + { + super.readFromNBT(tag); + NBTTagList itemList = tag.getTagList("Items"); + this.iContents = new ItemStack[this.getSizeInventory()]; + + for (int i = 0; i < itemList.tagCount(); ++i) + { + NBTTagCompound itemAt = (NBTTagCompound) itemList.tagAt(i); + int itemSlot = itemAt.getByte("Slot") & 255; + + if (itemSlot >= 0 && itemSlot < this.iContents.length) + { + this.iContents[itemSlot] = ItemStack.loadItemStackFromNBT(itemAt); + } + } + + invertItemCheck = tag.getBoolean("inverted"); + } + + @Override + public void writeToNBT(NBTTagCompound tag) + { + super.writeToNBT(tag); + NBTTagList itemList = new NBTTagList(); + + for (int i = 0; i < this.iContents.length; ++i) + { + if (this.iContents[i] != null) + { + NBTTagCompound itemAt = new NBTTagCompound(); + itemAt.setByte("Slot", (byte) i); + this.iContents[i].writeToNBT(itemAt); + itemList.appendTag(itemAt); + } + } + + tag.setTag("Items", itemList); + tag.setBoolean("inverted", invertItemCheck); + } + + @Override + public int getSizeInventory() + { + return 27; + } + + @Override + public ItemStack getStackInSlot(int slot) + { + return iContents[slot]; + } + + @Override + public ItemStack decrStackSize(int slot, int amount) + { + if (this.iContents[slot] != null) + { + ItemStack var3; + + if (this.iContents[slot].stackSize <= amount) + { + var3 = this.iContents[slot]; + this.iContents[slot] = null; + this.onInventoryChanged(); + return var3; + } + else + { + var3 = this.iContents[slot].splitStack(amount); + + if (this.iContents[slot].stackSize == 0) + { + this.iContents[slot] = null; + } + + this.onInventoryChanged(); + return var3; + } + } + else + { + return null; + } + } + + @Override + public ItemStack getStackInSlotOnClosing(int slot) + { + if (this.iContents[slot] != null) + { + ItemStack var2 = this.iContents[slot]; + this.iContents[slot] = null; + return var2; + } + else + { + return null; + } + } + + @Override + public void setInventorySlotContents(int slot, ItemStack stack) + { + this.iContents[slot] = stack; + + if (stack != null && stack.stackSize > this.getInventoryStackLimit()) + { + stack.stackSize = this.getInventoryStackLimit(); + } + + this.onInventoryChanged(); + } + + @Override + public String getInvName() + { + return "container.itemSensor"; + } + + @Override + public int getInventoryStackLimit() + { + return 64; + } + + @Override + public boolean isUseableByPlayer(EntityPlayer player) + { + return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double) this.xCoord + 0.5D, (double) this.yCoord + 0.5D, (double) this.zCoord + 0.5D) <= 64.0D; + } + + @Override + public void openChest() + { + } + + @Override + public void closeChest() + { + } +}