View on GitHub

EnderAPI

An API by the people, for the people

Download this project as a .zip file Download this project as a tar.gz file

Developement Team

SuicidalSTDz - Lead Developer
apemanzilla - Developer
skwerlman - Developer
KingofGamesYami - Developer

Accepted Suggestions

Colors API Extension

colors.convertToString( nColor )

function colors.convertToString( nColor )
  for k, v in pairs( colors ) do
    if nColor == v then
      return k
    end
  end
  return nil
end

Fs API Extension

fs.append( sPath, sData )

function fs.append( sPath, sData )
  assert( type( sPath ) == "string", "String expected, got " .. type( sPath ), 2 )
  assert( type( sData ) == "string", "String expected, got " .. type( sData ), 2 )
  local f
  if not fs.exists(sPath) then
    f = fs.open( sPath, "w" )
  else
    f = fs.open( sPath, "a" )
  end
  f.write( sData )
  f.close()
end

fs.read( sPath )

function fs.read( sPath )
  assert( type( sPath ) == "string", "string expected, got " .. type( sPath ), 2 )
  if fs.exists(sPath) then
    local handle = fs.open( sPath, "r" )
    local sData = handle.readAll()
    handle.close()
    return sData, true
  end
  return nil, false
end

fs.save( sPath, sData )

function fs.save( sPath, sData )
  assert( type( sPath ) == "string", "String expected, got " .. type( sPath ), 2 )
  assert( type( sData ) == "string", "String expected, got " .. type( sData ), 2 )
  local f = fs.open( sPath, "w" )
  f.write( sData )
  f.close()
end

HTTP API Extension

http.download( sUrl, sPath )

function http.download( sUrl, sPath )
  assert( type( sUrl ) == "string", "String expected, got " .. type( sUrl ), 2)
  assert( type( sPath ) == "string", "String expected, got " .. type( sPath ), 2)
  assert( not fs.exists( sPath ), "Path already exists", 2)

  local response = http.get( sUrl )
  if response then
    local f = fs.open( sPath, "w" )
    f.write( response.readAll() )
    f.close()
    response.close()
    return true
  end
  return false
end

MessageBox API

Number API

number.generate( nLength, nMin, nMax )

function generate( nLength, nMin, nMax )
  assert( type( nLength ) == "number", "Number expected, got " .. type( nLength ), 2)
  assert( type( nMins ) == "number", "Number expected, got " .. type( nMin ), 2)
  assert( type( nMax ) == "number", "Number expected, got " .. type( nMax ), 2)
  assert( nMin < nMax, "Minimum must be less than maximum", 2)

  local n = math.random( nMin, nMax )
  for i = 1, nLength do
    n = n .. math.random( nMin, nMax )
  end
  return tonumber( n )
end

number.isOdd( n )

function isOdd( n )
  assert( type( n ) == "number", "Number expected, got " .. type( n ), 2)
  return n % 2 ~= 0
end

number.isEven( n )

function isEven( n )
  assert( type( n ) == "number", "Number expected, got " .. type( n ), 2)
  return n % 2 == 0
end

Pastebin API

pastebin.get( sCode, sFile )

function get( sCode, sFile )
  assert( type( sCode ) == "string", "Number expected, got " .. type( sCode ), 2)
  assert( type( sFile ) == "string", "String expected, got " .. type( sFile ), 2)
  local sPath = shell.resolve( sFile )
  assert( not fs.exists( sPath ), "File exists", 2)

  local tResponse = http.get( "http://pastebin.com/raw.php?i=" .. textutils.urlEncode( sCode ) )
  if tResponse then
    local sResponse = tResponse.readAll()
    tResponse.close()

    local handle = fs.open( sPath, "w" )
    handle.write( sResponse )
    handle.close()
    return true
  end
  return false
end

pastebin.put( sFile )

function put( sFile )
  assert( type( sFile ) == "string", "String expected, got " .. type( sFile ), 2)

  local sPath = shell.resolve( sFile )
  assert( not fs.isDir( sPath ), "Cannot upload directories", 2 )
  assert( not fs.exists( sPath ), "File doesn't exist", 2 )

  local sName = fs.getName( sPath )
  local handle = fs.open( sPath, "r" )
  local sText = handle.readAll()
  handle.close()
  local key = "0ec2eb25b6166c0c27a394ae118ad829"
  local response = http.post(
    "http://pastebin.com/api/api_post.php", 
    "api_option=paste&" ..
    "api_dev_key=" .. key .. "&" ..
    "api_paste_format=lua&" ..
    "api_paste_name=" .. textutils.urlEncode( sPath ) .. "&" ..
    "api_paste_code=" .. textutils.urlEncode( sText )
  )

  if response then
    local sResponse = response.readAll()
    local sCode = string.match( sResponse, "[^/]+$" )
    response.close()
    return sCode, true
  end
  return nil, false
end

pastebin.updateFile( sCode, sFile )

function updateFile( sCode, sFile )
  assert( type( sCode ) == "string", "Number expected, got " .. type( sCode ), 2)
  assert( type( sFile ) == "string", "String expected, got " .. type( sFile ), 2)

  local sPath = shell.resolve( sFile )
  assert( not fs.isDirectory( sPath ), "Cannot update directory", 2)

  local httpHandle = http.get( "http://pastebin.com/raw.php?i=" .. textutils.urlEncode( sCode ) )
  if httpHandle then
    local sResponse = httpHandle.readAll()
    httpHandle.close()

    if sResponse and sResponse ~= "" then
      local fileContent = "[none&set]"
      if fs.exists( sPath ) then
        local fileHandle = fs.open( sPath, "r" )
        fileContent = fileHandle.readAll()
        fileHandle.close()
      end

      if fileContent ~= sResponse then
        fileHandle = fs.open( sPath, "w" )
        fileHandle.write( sResponse )
        fileHandle.close()
        return true
      end
    end
    return false
end

String API

string.generate( nLength, nCharSet )

function string.generate( nLength, nCharSet )
  assert( type( nLength ) == "number", "Number expected, got " .. type( nLength ), 2)
  assert( type( nCharSet ) == "number", "Number expected, got " .. type( nCharSet ), 2)
    local nCharSet = nCharSet or 128
    local str = ""
    for i = 1, nLength do
        str = str .. math.random( 1, nCharSet ):char()
    end
    return str
end

string.replaceChar( str, nPos, sReplace ) OR string:replaceChar( nPos, sReplace )

function string:replaceChar( nPos, sReplace )
  assert( type( nPos ) == "number", "Number expected, got " .. type( nLength ), 2)
  assert( type( sReplace ) == "string", "String expected, got " .. type( nCharSet ), 2)
  return self:sub( 1, nPos - 1 ) .. sReplace .. self:sub( nPos + 1 )
end

string.safePattern()

To be completed

( Insert description here )

string.splitAtWhite( str )

function string:splitAtWhite()
    local tData = {}
    for sArg in self:gmatch( "[^%s]+" ) do
        table.insert( tData, sArg )
    end
    return tData
end

Table API

table.sortLToG( tbl )

function table.sortLToG( tbl )
  assert( type( tbl ) == "table", "Table expected, got " .. type( tbl ), 2 )
  table.sort( tbl, function( a, b ) return a < b end )
  return tbl
end

table.sortGToL( tbl )

function table.sortGToL( tbl )
  table.sort( tbl, function( a, b ) return a > b end )
  return tbl
end

Term API

term.getCursorBlink()

To be completed

( Insert description here )

term.clear( nx, ny, nTextColour, nBackgroundColour )

function term.clear( nx, ny, nTextColour, nBackgroundColour )
  if nTextColour ~= nil then
    assert( type( nTextColour ) == "number", "Number expected, got " .. type( nTextColour ), 2)
    term.setTextColour( nTextColour )
  end
  if nBackgroundColour ~= nil then
    assert( type( nBackgroundColour ) == "number", "Number expected, got " .. type( nBackgroundColour ), 2)
    term.setBackgroundColour( nBackgroundColour )
  end
  if nx ~= nil then
    assert( type( nx ) == "number", "Number expected, got " .. type( nx ), 2)
  end
  if ny ~= nil then
    assert( type( ny ) == "number", "Number expected, got " .. type( nx ), 2)
  end
  term.setCursorPos( nx or currentX, ny or currentY )
  oldTerm.clear()
  local nMaxx,nMaxy = term.getSize()
  -- Clear pixel data
  for i = 1, nMaxx do
    for j = 1, nMaxy do
      tPixels[i.." "..j] = {
        Character = " ",
        TextColor = nTextColour or term.getTextColour(),
        TextColour = nTextColour or term.getTextColour(),
        BackgroundColor = nBackgroundColour or term.getBackgroundColour(),
        BackgroundColour = nBackgroundColour or term.getBackgroundColour()
      }
    end
  end
end

( Insert description here )

term.reset()

function term.reset()
  term.setTextColour(colors.white)
  term.setBackgroundColour(colors.black)
  term.setCursorPos(1,1)
  term.setCursorBlink(false)
  term.clear()
end

( Insert description here )

term.getPixelData( nx, ny )

function term.getPixelData( nx, ny )
  assert( type( nx ) == "number", "Number expected, got " .. type( nx ), 2)
  assert( type( ny ) == "number", "Number expected, got " .. type( ny ), 2)
  return tPixels[ nx .. " " .. ny ]
end

( Insert description here )

term.getTextColor()

function term.getTextColor()
  return currentTextColour
end

term.getBackgroundColour()

function term.getBackgroundColour()
  return currentBackgroundColour
end

Text API

text.bracket( sText, nx, ny, nTextColour, nBracketColour, nBackgroundColour )

function bracket( sText, nx, ny, nTextColour, nBracketColour, nBackgroundColour )
    assert( type( sText ) == "string", "String expected, got " .. type( sText ), 2)
    assert( type( nx ) == "number", "Number expected, got " .. type( nx ), 2)
    assert( type( ny ) == "number", "Number expected, got " .. type( ny ), 2)

  if nTextColour then
    assert( type( nTextColour ) == "number", "Number/nil expected, got " .. type( nTextColour ), 2)
  end
  if nBracketColour then
    assert( type( nBracketColour ) == "number", "Number/nil expected, got " .. type( nBracketColour ), 2)
  end
  if nBackgroundColour then
        assert( type( nBackgroundColour ) == "number", "Number/nil expected, got " .. type( nBackgroundColour ), 2)
    term.setBackgroundColour( nBackgroundColour )
    end

    local xPos, yPos = term.getCursorPos()
    term.setCursorPos( nx, ny )
    term.setTextColour( nBracketColour or colours.white )
    term.write( "[" .. string.rep(" ", #sText) .. "]" )

    term.setTextColour( nTextColour or colours.white )
  term.setCursorPos( nx + 1, ny )
    term.write( sText )
end

( Insert description here )

text.center( sText, nx, ny )

function center( sText, nx, ny )
    assert( type( sText ) == "string", "String expected, got " .. type( sText ), 2)
    assert( type( nx ) == "number", "Number expected, got " .. type( nx ), 2)
    assert( type( ny ) == "number", "Number expected, got " .. type( ny ), 2)
    term.setCursorPos( (( nx - #sText ) / 2) - 1, ny )
    term.write( sText )
end

( Insert description here )

text.printColorFormat( sText )

function printColourFormat( sText )
  assert( type( sText ) == "string", "String expected, got " .. type( sText ), 2)
  local char200 = string.char( 200 )

  local storage = {}
  local function matchLong( sMode1, sColour1, sMode2, sColour2 )
    if (sMode1 == "t" and sMode2 == "b") or (sMode1 == "b" and sMode2 == "t") then
      table.insert( storage, { 
        t = ( sMode1 == "t" and tonumber( sColour1 ) or tonumber( sColour2 ) ),
        b = ( sMode1 == "b" and tonumber( sColour1 ) or tonumber( sColour2 ) )
      } )
    end

    if sMode1 == "b" and sMode2 == "b" then
      table.insert( storage, { b = tonumber( sColour2 ) } )
    elseif sMode1 == "t" and sMode2 == "t" then
      table.insert( storage, { t = tonumber( sColour2 ) } )
    end
    return char200
  end

  local function matchShort( sMode, sColour ) 
    return matchLong( sMode, sColour, sMode, sColour )
  end

  -- Parse the text
  local parsed = sText:gsub( "%[([bt])%s*=%s*(%d-);%s*([bt])%s*=%s*(%d-)%]", matchLong )
  parsed = parsed:gsub( "%[([bt])%s*=%s*(%d-)%]", matchShort )

  -- Print it out on the terminal
  local index = 1
  local sMatch = "[^" .. char200 .. "]+"
  for i = 1, #parsed do
    local char = parsed:sub( i, i )
    if storage[index] and char == char200 then
      if storage[index].t then
        term.setTextColour( 2 ^ storage[index].t )
      end
      if storage[index].b then
        term.setBackgroundColour( 2 ^ storage[index].b )
      end
      index = index + 1
    else
       write( char )
    end
  end
end

( Insert description here )