Advertisement
King0fGamesYami

CommandsPlus

May 31st, 2016
1,380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.47 KB | None | 0 0
  1. local ok, result = commands.testfor( "@e[type=ArmorStand,name=pData]" )
  2. if not result[ 1 ] then
  3.   commands.summon( "ArmorStand ~ ~ ~ {CustomName:pData,Invisible:1b,NoGravity:1b}" )
  4. end
  5.  
  6. local lastPlayer, lastPlayerPosition, lastPlayerNBT = "", {}, {}, {}
  7.  
  8. function getPlayerPosition( playerName )
  9.   if playerName == lastPlayer and lastPlayerPosition.time == os.time() then
  10.     return unpack( lastPlayerPosition )
  11.   end
  12.   commands.tp( "@e[type=ArmorStand,name=pData] " .. playerName )
  13.   local ok, result = commands.execute( playerName .. " ~ ~ ~ tp @e[type=ArmorStand,name=pData] ~ ~2 ~" )
  14.   lastPlayer = playerName
  15.   local x, y, z = result[1]:match( "to (%S+), (%S+), (%S+)" )
  16.   lastPlayerPosition = { tonumber( x ), tonumber( y ) - 2, tonumber( z ), time = os.time()}
  17.   return tonumber( x ), tonumber( y ) - 2, tonumber( z )
  18. end
  19.  
  20. local function getPlayerNBT( playerName )
  21.   if lastPlayer == playerName and lastPlayerNBT.time == os.time() then
  22.     return unpack( lastPlayerNBT )
  23.   elseif lastPlayer == playerName and lastPlayerPosition.time ~= os.time() then
  24.     commands.tp( "@e[type=ArmorStand,name=pData] " .. playerName )
  25.     commands.execAsync( "/execute " .. playerName .. " ~ ~ ~ tp @e[type=ArmorStand,name=pData] ~ ~2 ~" )
  26.   end
  27.   local ok, result = commands.entitydata( "@e[type=ArmorStand,name=pData] {}" )
  28.   lastPlayerNBT = {result[ 1 ], time = os.time()}
  29.   return result[ 1 ]
  30. end
  31.  
  32. function getPlayerRotation( playerName )
  33.   local a, b = getPlayerNBT( playerName ):match( "Rotation:%[0:(%S-)f,1:(%S+)f,%]" )
  34.   return tonumber( a ), tonumber( b )
  35. end
  36.  
  37. function getNearbyPlayers( nLimit )
  38.   local ok, result
  39.   if nLimit then
  40.     ok, result = commands.testfor( "@a[r=" .. tostring( nLimit ) .. "]" )
  41.   else
  42.     ok, result = commands.testfor( "@a" )
  43.   end
  44.   local tPlayers = {}
  45.   for k, v in pairs( result ) do
  46.     tPlayers[ k ] = v:match( "Found (.+)" )
  47.   end
  48.   return tPlayers
  49. end
  50.  
  51. function getGameruleValue( gamerule )
  52.   local ok, result = commands.gamerule( gamerule )
  53.   local v = result[ 1 ]:match( "= (.+)" )
  54.   if v == "true" then return true
  55.   elseif v == "false" then return false
  56.   else return tonumber( v ) or v end
  57. end
  58.  
  59. function getDaysPassed()
  60.   local ok, result = commands.time( "query day" )
  61.   return tonumber( result[ 1 ]:match( "is (%S+)" ) )
  62. end
  63.  
  64. function getGametime()
  65.   local ok, result = commands.time( "query gametime" )
  66.   return tonumber( result[ 1 ]:match( "is (%S+)" ) )
  67. end
  68.  
  69. function getDaytime()
  70.   local ok, result = commands.time( "query daytime" )
  71.   return tonumber( result[ 1 ]:match( "is (%S+)" ) )
  72. end
  73.  
  74. function getWorldborder()
  75.   local ok, result = commands.worldborder( "get" )
  76.   return tonumber( result[ 1 ]:match( "%d+" ) )
  77. end
  78.  
  79. function getFormattedBlockInfos( x, y, z, x2, y2, z2 )
  80.   --find the minimum and maximum verticies
  81.   local minx, miny, minz = math.floor( math.min( x, x2 ) ), math.floor( math.min( y, y2 ) ), math.floor( math.min( z, z2 ) )
  82.   local maxx, maxy, maxz = math.floor( math.max( x, x2 ) ), math.floor( math.max( y, y2 ) ), math.floor( math.max( z, z2 ) )
  83.  
  84.   local tBlockInfos = commands.getBlockInfos( minx, miny, minz, maxx, maxy, maxz )
  85.   local tFormattedBlockInfos = {}
  86.   local iTablePosition = 1
  87.  
  88.   for iy = miny, maxy do
  89.     for iz = minz, maxz do
  90.       for ix = minx, maxx do
  91.         tFormattedBlockInfos[ ix ] = tFormattedBlockInfos[ ix ] or {}
  92.         tFormattedBlockInfos[ ix ][ iy ] = tFormattedBlockInfos[ ix ][ iy ] or {}
  93.         tFormattedBlockInfos[ ix ][ iy ][ iz ] = tBlockInfos[ iTablePosition ]
  94.         iTablePosition = iTablePosition + 1
  95.       end
  96.     end
  97.   end
  98.   return tFormattedBlockInfos
  99. end
  100.  
  101. local tIgnoredBlocks = {
  102.   ["minecraft:air"] = true,
  103.   ["minecraft:water"] = true,
  104.   ["minecraft:flowing_water"] = true,
  105.   ["minecraft:lava"] = true,
  106.   ["minecraft:flowing_lava"] = true,
  107. }
  108.  
  109. function getObservedBlock( playerName )
  110.   --credit to moomoomoo3O9 (http://www.computercraft.info/forums2/index.php?/user/23178-moomoomoo3o9/) for the original function
  111.   --which I have modified in order to restrict the results to the actual reach of the player, and utilize getBlockInfos
  112.   local rotationx, rotationy = getPlayerRotation( playerName )
  113.   local px, py, pz = getPlayerPosition( playerName )
  114.   py = py + 1.62 --#The player's eyes are 1.62 blocks from the ground
  115.   --Convert pitch/yaw into Vec3 from http://stackoverflow.com/questions/10569659/camera-pitch-yaw-to-direction-vector
  116.   local xzLen=-math.cos(math.rad(rotationy))
  117.   local x, y, z = xzLen * math.sin( -math.rad( rotationx+180 ) ), math.sin( math.rad( -rotationy ) ), xzLen * math.cos( math.rad( rotationx + 180 ) )
  118.  
  119.   local maxProjectedLength = 5
  120.  
  121.   local tBlockInfos = getFormattedBlockInfos( px, py, pz, (x * maxProjectedLength) + px, (y * maxProjectedLength) + py, (z * maxProjectedLength) + pz )
  122.  
  123.   local lastX, lastY, lastZ, skip
  124.   for mult = 0, maxProjectedLength - 1, 0.05 do  --Extend the vector linearly
  125.     local currX,currY,currZ= math.floor( (x*mult )+px ), math.floor( (y*mult)+py ), math.floor( (z*mult)+pz )
  126.     skip = lastX and currX == lastX and currY== lastY and currZ == lastZ
  127.     if not skip and not tIgnoredBlocks[ tBlockInfos[ currX ][ currY ][ currZ ].name ] then
  128.       return currX, currY, currZ
  129.     end
  130.     lastX,lastY,lastZ=currX,currY,currZ
  131.   end
  132. end
  133.  
  134. function getForgeTPS()
  135.   local ok, result = commands.forge( "tps" )
  136.   local t = {}
  137.   for k, v in pairs( result ) do
  138.     local dim, time, tps = v:match( "D?i?m? ?(%S+).-time\\?: (%S+).-TPS\\?: (%S+)" )
  139.     t[ tonumber( dim ) or dim ] = { time = tonumber( time ), tps = tonumber( tps ) }
  140.   end
  141.   return t
  142. end
  143.  
  144. function listScoreboardTeams()
  145.   local ok, result = commands.scoreboard( "teams list" )
  146.   if not ok then return {} end
  147.   local t = {}
  148.   table.remove( result, 1 )
  149.   for k, v in pairs( result ) do
  150.     t[ v:match( "%- (%S+):" ) ] = v:match( "has (%d+)" )
  151.   end
  152.   return t
  153. end
  154.  
  155. function listScoreboardObjectives()
  156.   local ok, result = commands.scoreboard( "objectives list"  )
  157.   local t = {}
  158.   table.remove( result, 1 )
  159.   for k, v in pairs( result ) do
  160.     local name, displayname, objectivetype = v:match( "%- (%S+):.- '(.-)'.-type '(.-)'" )
  161.     t[ name ] = {displayName = displayname, type = objectivetype}
  162.   end
  163. end
  164.  
  165. function listScoreboardPlayers()
  166.   local ok, result = commands.scoreboard( "players list" )
  167.   table.remove( result, 1 )
  168.   return result
  169. end
  170.  
  171. function listScoreboardTeamPlayers( teamName )
  172.   local ok, result = commands.scoreboard( "teams list " .. teamName )
  173.   table.remove( result, 1 )
  174.   return result
  175. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement