private void renderGateFrameAt(int x, int y, byte pos) {
   if (tileentity.shallRenderPin(pos)) {
     final byte powerLevel = tileentity.getPowerLevel(pos);
     if (powerLevel == -1) {
       this.drawTexturedModalRect(x, y, 216, 20, 20, 20);
     } else {
       this.drawTexturedModalRect(
           x, y, 176 + (((powerLevel & 8) >> 1) * 5), (powerLevel & 7) * 20, 20, 20);
     }
   }
 }
  public GuiBrainLogicBlock(TileEntityBlockBrainLogicBlock tileentityblockbrainlogicblock) {
    super(new ContainerBlockBrainLightSensor());
    username = BrainStone.proxy.getPlayer().username;
    tileentity = tileentityblockbrainlogicblock;
    tileentity.logIn(username);
    help = false;

    scrollbarPos = 0;
    mousePos = 1;
    movingPin = -1;
    movingPinOffsetX = 0;
    movingPinOffsetY = 0;
    swappedPin = -1;

    mousePosX = 0;
    mousePosY = 0;

    direction = TileEntityBlockBrainLogicBlock.guiDirection;

    localToBlockDirections =
        new byte[] {
          0,
          1,
          (byte) (2 + direction),
          (byte) (2 + ((direction + 3) & 3)),
          (byte) (2 + ((direction + 2) & 3)),
          (byte) (2 + ((direction + 1) & 3))
        };
  }
  @Override
  public void drawGuiContainerBackgroundLayer(float par1Float, int par2Integer, int par3Integer) {
    if (mousePos == -2) {
      this.mouseMovedOrUp(
          (Mouse.getEventX() * width) / mc.displayWidth,
          height - ((Mouse.getEventY() * height) / mc.displayHeight) - 1,
          -1);
    }

    this.registerTexture();

    GL11.glPushMatrix();

    factor = 1.0F;
    int x = globalX = (width - xSizeMain) / 2;
    int y = globalY = (height - ySizeMain) / 2;
    this.drawTexturedModalRect(x, y, 0, 0, xSizeMain, ySizeMain);

    if (rowsToScroll < 1) {
      this.drawTexturedModalRect(x + 157, y + 78, 244, 0, 12, 15);
    } else {
      this.drawTexturedModalRect(
          x + 157, y + 78 + ((int) (scrollbarPos * pixelPerRow)), 232, 0, 12, 15);
    }

    for (int i = 0; i < 6; i++) {
      if ((Gate.NumberGates >= 6) || (i < Gate.NumberGates)) {
        this.drawTexturedModalRect(
            x + 8,
            y + 78 + (19 * i),
            8,
            ((i + scrollbarPos) == tileentity.getGatePos()) ? 219 : 200,
            143,
            19,
            i == mousePos);
      }
    }

    if (movingPin != 0) {
      this.renderGateFrameAt(x + 38, y + 7, localToBlockDirections[0]);
    } else if (swappedPin != -1) {
      this.renderGateFrameAt(x + 38, y + 7, swappedPin);
    }

    if (movingPin != 1) {
      this.renderGateFrameAt(x + 38, y + 47, localToBlockDirections[1]);
    } else if (swappedPin != -1) {
      this.renderGateFrameAt(x + 38, y + 47, swappedPin);
    }

    if (movingPin != 2) {
      this.renderGateFrameAt(x + 98, y + 7, localToBlockDirections[2]);
    } else if (swappedPin != -1) {
      this.renderGateFrameAt(x + 98, y + 7, swappedPin);
    }

    if (movingPin != 3) {
      this.renderGateFrameAt(x + 78, y + 27, localToBlockDirections[3]);
    } else if (swappedPin != -1) {
      this.renderGateFrameAt(x + 78, y + 27, swappedPin);
    }

    if (movingPin != 4) {
      this.renderGateFrameAt(x + 98, y + 47, localToBlockDirections[4]);
    } else if (swappedPin != -1) {
      this.renderGateFrameAt(x + 98, y + 47, swappedPin);
    }

    if (movingPin != 5) {
      this.renderGateFrameAt(x + 118, y + 27, localToBlockDirections[5]);
    } else if (swappedPin != -1) {
      this.renderGateFrameAt(x + 118, y + 27, swappedPin);
    }

    // END of Textures!
    // BEGIN of Strings!

    GL11.glTranslatef(globalX, globalY, 0.0F);

    for (int i = 0; i < 6; i++) {
      if (i < Gate.NumberGates) {
        this.drawString(Gate.GateNames[i + scrollbarPos], 14, 84 + (19 * i), 0);
      }
    }

    final String directions[] = new String[] {"North", "East", "South", "West"};

    this.drawString("Top", 35 - this.getStringWidth("Top"), 13, 0);
    this.drawString("Bottom", 61, 53, 0);
    this.drawString(directions[direction], 95 - this.getStringWidth(directions[direction]), 13, 0);
    this.drawString(directions[direction ^ 2], 121, 53, 0);

    if (movingPin != 0) {
      this.renderGateLetterAt(42, 11, localToBlockDirections[0]);
    } else if (swappedPin != -1) {
      this.renderGateLetterAt(42, 11, swappedPin);
    }

    if (movingPin != 1) {
      this.renderGateLetterAt(42, 51, localToBlockDirections[1]);
    } else if (swappedPin != -1) {
      this.renderGateLetterAt(42, 51, swappedPin);
    }

    if (movingPin != 2) {
      this.renderGateLetterAt(102, 11, localToBlockDirections[2]);
    } else if (swappedPin != -1) {
      this.renderGateLetterAt(102, 11, swappedPin);
    }

    if (movingPin != 3) {
      this.renderGateLetterAt(82, 31, localToBlockDirections[3]);
    } else if (swappedPin != -1) {
      this.renderGateLetterAt(82, 31, swappedPin);
    }

    if (movingPin != 4) {
      this.renderGateLetterAt(102, 51, localToBlockDirections[4]);
    } else if (swappedPin != -1) {
      this.renderGateLetterAt(102, 51, swappedPin);
    }

    if (movingPin != 5) {
      this.renderGateLetterAt(122, 31, localToBlockDirections[5]);
    } else if (swappedPin != -1) {
      this.renderGateLetterAt(122, 31, swappedPin);
    }

    scrollbarPos = 1 * scrollbarPos;

    GL11.glPopMatrix();

    // MovingPin

    if (movingPin != -1) {
      GL11.glPushMatrix();
      factor = 1.0F;
      this.registerTexture();

      this.renderGateFrameAt(
          (x + mousePosX) - movingPinOffsetX,
          (y + mousePosY) - movingPinOffsetY,
          localToBlockDirections[movingPin]);

      GL11.glTranslatef(globalX, globalY, 0.0F);

      this.renderGateLetterAt(
          (mousePosX - movingPinOffsetX) + 4,
          (mousePosY - movingPinOffsetY) + 4,
          localToBlockDirections[movingPin]);

      GL11.glPopMatrix();
    }

    // Help Screen!

    if (help) {
      this.registerTexture("GuiBrainLogicBlockhelp");

      final int rows = this.getLines(HelpText);
      final int ySizeHelp = 20 + (9 * rows);

      x = (width - xSizeHelp) / 2;
      y = (height - ySizeHelp) / 2;
      this.drawTexturedModalRect(x, y, 0, 0, xSizeHelp, 10);
      this.drawTexturedModalRect(x, (y + ySizeHelp) - 10, 0, 19, xSizeHelp, 10);

      for (int row = 0; row < rows; row++) {
        this.drawTexturedModalRect(x, y + 10 + (row * 9), 0, 10, xSizeHelp, 9);
      }

      fontRenderer.drawSplitString(HelpText, x + 10, y + 10, stringWidth, 0xeeeeee);
    }
  }
 private void closeHelpGui() {
   this.click();
   help = false;
   tileentity.logIn(username);
 }
 private void renderGateLetterAt(int x, int y, byte pos) {
   if (tileentity.shallRenderPin(pos)) {
     this.drawString(tileentity.getGateLetter(pos), x, y, tileentity.getGateColor(pos), 2.0F);
   }
 }
 private void quit() {
   this.click();
   tileentity.logOut(username);
   mc.displayGuiScreen(null);
   mc.setIngameFocus();
 }
  @Override
  protected void mouseClicked(int mouseX, int mouseY, int which) {
    super.mouseClicked(mouseX, mouseY, which);

    if (help) {
      this.closeHelpGui();
    } else {
      mousePosX = mouseX -= globalX;
      mousePosY = mouseY -= globalY;

      for (int i = 0; i < 6; i++) {
        if (this.inField(mouseX, mouseY, 8, 78 + (19 * i), 150, 96 + (19 * i))
            && ((i + scrollbarPos) < Gate.NumberGates)) {
          tileentity.changeGate(Gate.GateNames[i + scrollbarPos], direction);

          break;
        }
      }

      if (this.inField(mouseX, mouseY, 38, 7, 58, 27)
          && tileentity.canBeMovedByMouse(localToBlockDirections[0])) {
        movingPin = 0;
        movingPinOffsetX = mouseX - 38;
        movingPinOffsetY = mouseY - 7;
      } else if (this.inField(mouseX, mouseY, 38, 47, 58, 67)
          && tileentity.canBeMovedByMouse(localToBlockDirections[1])) {
        movingPin = 1;
        movingPinOffsetX = mouseX - 38;
        movingPinOffsetY = mouseY - 47;
      } else if (this.inField(mouseX, mouseY, 98, 7, 118, 27)
          && tileentity.canBeMovedByMouse(localToBlockDirections[2])) {
        movingPin = 2;
        movingPinOffsetX = mouseX - 98;
        movingPinOffsetY = mouseY - 7;
      } else if (this.inField(mouseX, mouseY, 78, 27, 98, 47)
          && tileentity.canBeMovedByMouse(localToBlockDirections[3])) {
        movingPin = 3;
        movingPinOffsetX = mouseX - 78;
        movingPinOffsetY = mouseY - 27;
      } else if (this.inField(mouseX, mouseY, 98, 47, 118, 67)
          && tileentity.canBeMovedByMouse(localToBlockDirections[4])) {
        movingPin = 4;
        movingPinOffsetX = mouseX - 98;
        movingPinOffsetY = mouseY - 47;
      } else if (this.inField(mouseX, mouseY, 118, 27, 138, 47)
          && tileentity.canBeMovedByMouse(localToBlockDirections[5])) {
        movingPin = 5;
        movingPinOffsetX = mouseX - 118;
        movingPinOffsetY = mouseY - 27;
      }

      if (this.inField(mouseX, mouseY, 168, 3, 172, 7)) {
        this.quit();

        return;
      }

      // REMOVE
      // for (byte byte0 = 0; byte0 <
      // TileEntityBlockBrainLogicBlock.numGates; byte0++) {
      // final int l = 12 * byte0;
      //
      // if (this.inField(i, j, 5, 18 + l, 75, 32 + l)) {
      // tileentity.setMode(byte0);
      // }
      // }
      //
      // if (this.inField(i, j, 76, 68, 168, 90)) {
      // tileentity.invertInvertOutput();
      // }
      //
      // if (!tileentity.isSwapable()) {
      // tileentity.setFocused(0);
      // } else if (this.inField(i, j, 124, 7, 143, 26)) {
      // focused = tileentity.getFocused();
      //
      // if (focused != 1) {
      // tileentity.setFocused(1);
      // } else {
      // tileentity.setFocused(0);
      // }
      // } else if (this.inField(i, j, 144, 27, 163, 46)) {
      // if (focused != 2) {
      // tileentity.setFocused(2);
      // } else {
      // tileentity.setFocused(0);
      // }
      // } else if (this.inField(i, j, 104, 27, 123, 46)) {
      // if (focused != 3) {
      // tileentity.setFocused(3);
      // } else {
      // tileentity.setFocused(0);
      // }
      // } else {
      // tileentity.setFocused(0);
      // }
    }
  }