Advertisement
aodminecraft

Arc OS - Worldedit - programs/WE_Cuboid

Aug 1st, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.11 KB | None | 0 0
  1. cuboid={}
  2. function oppositeDirection(dir)
  3.   dir=dir:lower()
  4.   if dir=="east" then
  5.     return "west"
  6.   elseif dir=="west" then
  7.     return "east"
  8.   elseif dir=="north" then
  9.     return "south"
  10.   elseif dir=="south" then
  11.     return "north"
  12.   elseif dir=="up" then
  13.     return "down"
  14.   elseif dir=="down" then
  15.     return "up"
  16.   end
  17.   return "Not a direction"
  18. end
  19.  
  20. function isDirection(dir,getSelf)
  21.   errText=typeError({dir,getSelf},{"string",{"boolean","nil"}})
  22.   if errText then debug.printError(errText) end
  23.   local directions={"up","down","north","south","east","west"}
  24.   for i=1,#directions do
  25.     if dir:lower()==directions[i] then
  26.       return true
  27.     elseif i==#directions and getSelf then
  28.       return dir:lower()=="self"
  29.     end
  30.   end
  31. end
  32.  
  33. function hasDirection(str,getSelf)
  34.   errText=typeError({dir,getSelf},{"string",{"boolean","nil"}})
  35.   if errText then debug.printError(errText) end
  36.   local directions={"up","down","north","south","east","west"}
  37.   for i=1,#directions do
  38.     if str:find(" "..directions[i]) then
  39.       return true
  40.     elseif i==#directions and getSelf then
  41.       return str:find(" self")
  42.     end
  43.   end
  44. end
  45.  
  46. function cuboid.expand() --http://wiki.sk89q.com/wiki/WorldEdit/Selection#Expanding_the_selection
  47.   command=message:sub(8):lower()
  48.   if command:find("vert") then
  49.     y1=1
  50.     y2=256
  51.     makeCuboidSelection()
  52.     sendChat("Region expanded vertically. ("..#Selection..")")
  53.     return
  54.   elseif hasDirection(command,not hasChatBox) then
  55.     local args=stringx.split(command," ")
  56.     for i=1,#args do
  57.       if isDirection(args[i]) then
  58.         Direction=args[i]
  59.       elseif args[i]:find(",") then
  60.         local args2=stringx.split(args[i],",")
  61.         if #args2==2 and tonumber(args2[1]) and tonumber(args2[2]) then
  62.           beforeComma=tonumber(args2[1])
  63.           afterComma=tonumber(args2[2])
  64.         end
  65.       elseif tonumber(args[i]) then
  66.         command=args[i]
  67.       end
  68.     end
  69.   else
  70.     Direction=string.lower(getDirection(true))
  71.     if not Direction then return end
  72.   end
  73.   if not tonumber(command) and (not tonumber(beforeComma) or not tonumber(afterComma)) then
  74.     sendChat((not tonumber(beforeComma) and ("\""..tostring(beforeComma).."\"") or ("\""..tostring(afterComma).."\""))..((not tonumber(beforeComma) and not tonumber(afterComma)) and (" and "..("\""..tostring(afterComma).."\"")) or "").." "..((not tonumber(beforeComma) and not tonumber(afterComma)) and "are" or "is").." not a number"..((not tonumber(beforeComma) and not tonumber(afterComma)) and "s" or "").."!")
  75.     --I may like ternary operators a bit too much...
  76.     return
  77.   end
  78.   if beforeComma and afterComma then
  79.     if Direction=="east" then
  80.       if x1 >=x2 then
  81.         x1=x1+beforeComma
  82.         x2=x2-afterComma
  83.       else
  84.         x1=x1-afterComma
  85.         x2=x2+beforeComma
  86.       end
  87.     elseif Direction=="west" then
  88.       if x1 >=x2 then
  89.         x1=x1+afterComma
  90.         x2=x2-beforeComma
  91.       else
  92.         x1=x1-beforeComma
  93.         x2=x2+afterComma
  94.       end
  95.     elseif Direction=="north" then
  96.       if z1 >=z2 then
  97.         z1=z1+afterComma
  98.         z2=z2-beforeComma
  99.       else
  100.         z1=z1-beforeComma
  101.         z2=z2+afterComma
  102.       end
  103.     elseif Direction=="south" then
  104.       if z1 >=z2 then
  105.         z1=z1+beforeComma
  106.         z2=z2-afterComma
  107.       else
  108.         z1=z1-afterComma
  109.         z2=z2+beforeComma
  110.       end
  111.     elseif Direction=="up" then
  112.       if y1 >=y2 then
  113.         y1=y1+beforeComma
  114.         y2=y2-afterComma
  115.       else
  116.         y1=y1-afterComma
  117.         y2=y2+beforeComma
  118.       end
  119.     elseif Direction=="down" then
  120.       if y1 >=y2 then
  121.         y1=y1+afterComma
  122.         y2=y2-beforeComma
  123.       else
  124.         y1=y1-beforeComma
  125.         y2=y2+afterComma
  126.       end
  127.     else
  128.       sendChat("Invalid direction.")
  129.       return
  130.     end
  131.     makeCuboidSelection()
  132.     sendChat("Region expanded "..beforeComma.." block"..((tonumber(beforeComma)>1 and "s") or "").." "..Direction.." and "..afterComma.." block"..((tonumber(afterComma)>1 and "s") or "").." "..oppositeDirection(Direction)..".".." ("..#Selection..")")
  133.   else
  134.     if not tonumber(command) then
  135.       sendChat("\""..command.."\" is not a number!")
  136.       return
  137.     else
  138.       command=tonumber(command)
  139.     end
  140.     if Direction=="east" then
  141.       if x1 > x2 then
  142.         x1=x1+tonumber(command)
  143.       else
  144.         x2=x2+tonumber(command)
  145.       end
  146.     elseif Direction=="west" then
  147.       if x1 > x2 then
  148.         x2=x2-tonumber(command)
  149.       else
  150.         x1=x1-tonumber(command)
  151.       end
  152.     elseif Direction=="north" then
  153.       if z1 > z2 then
  154.         z2=z2-tonumber(command)
  155.       else
  156.         z1=z1-tonumber(command)
  157.       end
  158.     elseif Direction=="south" then
  159.       if z1 > z2 then
  160.         z1=z1+tonumber(command)
  161.       else
  162.         z2=z2+tonumber(command)
  163.       end
  164.     elseif Direction=="up" then
  165.       if y1 > y2 then
  166.         y1=y1+tonumber(command)
  167.       else
  168.         y2=y2+tonumber(command)
  169.       end
  170.     elseif Direction=="down" then
  171.       if y1 > y2 then
  172.         y2=y2-tonumber(command)
  173.       else
  174.         y1=y1-tonumber(command)
  175.       end
  176.     else
  177.       sendChat("Invalid direction.")
  178.       return
  179.     end
  180.     makeCuboidSelection()
  181.     sendChat("Region expanded "..tonumber(command).." block"..((tonumber(command)>1 and "s") or "").." "..Direction..".".." ("..#Selection..")")
  182.   end
  183. end
  184.  
  185. function cuboid.contract() --http://wiki.sk89q.com/wiki/WorldEdit/Selection#Contracting_the_selection
  186.   local command=message:sub(9):lower()
  187.   if hasDirection(command,not hasChatBox) then
  188.     local args=stringx.split(command," ")
  189.     for i=1,#args do
  190.       if isDirection(args[i]) then
  191.         Direction=args[i]
  192.       elseif args[i]:find(",") then
  193.         local args2=stringx.split(args[i],",")
  194.         if #args2==2 and tonumber(args2[1]) and tonumber(args2[2]) then
  195.           beforeComma=tonumber(args2[1])
  196.           afterComma=tonumber(args2[2])
  197.         end
  198.       elseif tonumber(args[i]) then
  199.         command=args[i]
  200.       end
  201.     end
  202.   else
  203.     Direction=string.lower(getDirection(true))
  204.     if not Direction then return end
  205.   end
  206.   if not tonumber(command) and (not tonumber(beforeComma) or not tonumber(afterComma)) then
  207.     sendChat((not tonumber(beforeComma) and ("\""..tostring(beforeComma).."\"") or ("\""..tostring(afterComma).."\""))..((not tonumber(beforeComma) and not tonumber(afterComma)) and (" and "..("\""..tostring(afterComma).."\"")) or "").." "..((not tonumber(beforeComma) and not tonumber(afterComma)) and "are" or "is").." not a number"..((not tonumber(beforeComma) and not tonumber(afterComma)) and "s" or "").."!")
  208.     --I may like ternary operators a bit too much...
  209.     return
  210.   end
  211.   if beforeComma and afterComma then
  212.     if Direction=="east" then
  213.       if x1 >=x2 then
  214.         x1=x1-beforeComma
  215.         x2=x2+afterComma
  216.       else
  217.         x1=x1+afterComma
  218.         x2=x2-beforeComma
  219.       end
  220.     elseif Direction=="west" then
  221.       if x1 >=x2 then
  222.         x1=x1-afterComma
  223.         x2=x2+beforeComma
  224.       else
  225.         x1=x1+beforeComma
  226.         x2=x2-afterComma
  227.       end
  228.     elseif Direction=="north" then
  229.       if z1 >=z2 then
  230.         z1=z1-afterComma
  231.         z2=z2+beforeComma
  232.       else
  233.         z1=z1+beforeComma
  234.         z2=z2-afterComma
  235.       end
  236.     elseif Direction=="south" then
  237.       if z1 >=z2 then
  238.         z1=z1-beforeComma
  239.         z2=z2+afterComma
  240.       else
  241.         z1=z1+afterComma
  242.         z2=z2-beforeComma
  243.       end
  244.     elseif Direction=="up" then
  245.       if y1 >=y2 then
  246.         y1=y1-beforeComma
  247.         y2=y2+afterComma
  248.       else
  249.         y1=y1+afterComma
  250.         y2=y2-beforeComma
  251.       end
  252.     elseif y1 >=y2 then
  253.       y1=y1-afterComma
  254.       y2=y2+beforeComma
  255.     else
  256.       y1=y1+beforeComma
  257.       y2=y2-afterComma
  258.     end
  259.     makeCuboidSelection()
  260.     sendChat("Region contracted "..beforeComma.." block"..((tonumber(beforeComma)>1 and "s") or "").." "..Direction.." and "..afterComma.." block"..((tonumber(afterComma)>1 and "s") or "").." "..oppositeDirection(Direction)..".")
  261.   else
  262.     if not tonumber(command) then
  263.       sendChat("\""..command.."\" is not a number!")
  264.       return
  265.     else
  266.       command=tonumber(command)
  267.     end
  268.     if Direction=="east" then
  269.       if x1 > x2 then
  270.         x1=x1-command
  271.       else
  272.         x2=x2-command
  273.       end
  274.     elseif Direction=="west" then
  275.       if x1 > x2 then
  276.         x2=x2+command
  277.       else
  278.         x1=x1+command
  279.       end
  280.     elseif Direction=="north" then
  281.       if z1 > z2 then
  282.         z2=z2+command
  283.       else
  284.         z1=z1+command
  285.       end
  286.     elseif Direction=="south" then
  287.       if z1 > z2 then
  288.         z1=z1-command
  289.       else
  290.         z2=z2-command
  291.       end
  292.     elseif Direction=="up" then
  293.       if y1 > y2 then
  294.         y1=y1-command
  295.       else
  296.         y2=y2-command
  297.       end
  298.     elseif Direction=="down" then
  299.       if y1 > y2 then
  300.         y2=y2+command
  301.       else
  302.         y1=y1+command
  303.       end
  304.     else
  305.       sendChat("Incorrect direction.")
  306.       return
  307.     end
  308.     makeCuboidSelection()
  309.     sendChat("Region contracted "..command.." block"..((command>1 and "s") or "").." "..Direction..".".." ("..#Selection..")")
  310.   end
  311. end
  312.  
  313. function cuboid.inset() --http://wiki.sk89q.com/wiki/WorldEdit/Selection#Contracting_2_axes_simultaneously_.28inset.29
  314.   local command=message:sub(6)
  315.   if tonumber(command) then
  316.     command=tonumber(command)
  317.     if x1>x2 then
  318.       x1=x1-command
  319.       x2=x2+command
  320.     else
  321.       x1=x1+command
  322.       x2=x2-command
  323.     end
  324.     if z1>z2 then
  325.       z1=z1-command
  326.       z2=z2+command
  327.     else
  328.       z1=z1+command
  329.       z2=z2-command
  330.     end
  331.     if y1>y2 then
  332.       y1=y1-command
  333.       y2=y2+command
  334.     else
  335.       y1=y1+command
  336.       y2=y2-command
  337.     end
  338.   else
  339.     sendChat("\""..command.."\" is not a number!")
  340.     return
  341.   end
  342.   makeCuboidSelection()
  343.   sendChat("Selection inset "..command.." block"..((tonumber(command)>1 and "s") or "")..".")
  344. end
  345.  
  346. function cuboid.outset() --http://wiki.sk89q.com/wiki/WorldEdit/Selection#Contracting_2_axes_simultaneously_.28inset.29
  347.   local command=message:sub(7)
  348.   if tonumber(command) then
  349.     command=tonumber(command)
  350.     if x1>x2 then
  351.       x1=x1+command
  352.       x2=x2-command
  353.     else
  354.       x1=x1-command
  355.       x2=x2+command
  356.     end
  357.     if z1>z2 then
  358.       z1=z1+command
  359.       z2=z2-command
  360.     else
  361.       z1=z1-command
  362.       z2=z2+command
  363.     end
  364.     if y1>y2 then
  365.       y1=y1+command
  366.       y2=y2-command
  367.       if y1 >256 then
  368.         y1=256
  369.       end
  370.       if y2 < 0 then
  371.         y2=0
  372.       end
  373.     else
  374.       y1=y1-command
  375.       y2=y2+command
  376.       if y2 >256 then
  377.         y2=256
  378.       end
  379.       if y1 < 0 then
  380.         y1=0
  381.       end
  382.     end
  383.   end
  384.   makeCuboidSelection()
  385.   sendChat("Selection outset "..command.." block"..((tonumber(command)>1 and "s") or "")..".")
  386. end
  387.  
  388. function cuboid.shift() --http://wiki.sk89q.com/wiki/WorldEdit/Selection#Shifting_your_selection
  389.   local command=string.lower(message:sub(6))
  390.   local dir=false
  391.   if hasDirection(command,not hasChatBox) then
  392.     local args=stringx.split(command," ")
  393.     for i=1,#args do
  394.       if tonumber(args[i]) then
  395.         dir=true
  396.         command=args[i]
  397.       elseif isDirection(args[i]) then
  398.         Direction=args[i]
  399.       end
  400.     end
  401.   end
  402.   if tonumber(command) and not dir then
  403.     Direction=string.lower(getDirection(true))
  404.     if not Direction then return end
  405.     dir=true
  406.   end
  407.   if Direction=="east" then
  408.     x1=x1+tonumber(command)
  409.     x2=x2+tonumber(command)
  410.   elseif Direction=="west" then
  411.     x2=x2-tonumber(command)
  412.     x1=x1-tonumber(command)
  413.   elseif Direction=="north" then
  414.     z2=z2-tonumber(command)
  415.     z1=z1-tonumber(command)
  416.   elseif Direction=="south" then
  417.     z1=z1+tonumber(command)
  418.     z2=z2+tonumber(command)
  419.   elseif Direction=="up" then
  420.     y1=y1+tonumber(command)
  421.     y2=y2+tonumber(command)
  422.   elseif Direction=="down" then
  423.     y2=y2-tonumber(command)
  424.     y1=y1-tonumber(command)
  425.   else
  426.     sendChat("Incorrect direction.")
  427.     return
  428.   end
  429.   makeCuboidSelection()
  430.   sendChat("Region shifted "..tonumber(command).." block"..((tonumber(command)>1 and "s") or "").." "..Direction..".".." ("..#Selection..")")
  431. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement