Advertisement
antonsavov

WIP MinecraftSetter

Jan 4th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.69 KB | None | 0 0
  1. -------------------------------
  2. -- /lib/MinecraftSetter
  3. -------------------------------
  4. --- This package keeps all functions that modify the minecraft game world:
  5. -- setblock, fill, clone, teleport, title, tellraw, scoreboard, weather, gamerule, etc.
  6. -- the rest of twenty-k code should not use Minecraft commands directly but only through this package
  7.  
  8. local mc = {}
  9. mcset = mc
  10.  
  11. CLONE_MASK_MODE = {
  12.     REPLACE = "replace", -- Clones all blocks, including air.
  13.     MASKED = "masked", -- Clones only blocks that are not air.
  14.     FILTERED = "filtered" -- Clones only blocks that match the tileName.
  15. }
  16.  
  17. CLONE_MODE = {
  18.     NORMAL = "normal", -- Clone the blocks from the source region to the destination region.
  19.     FORCE = "force", -- Force the clone if the source region and destination region overlap.
  20.     MOVE = "move" -- Clone the blocks from the source region to the destination region. Then replace the cloned blocks in the source region with air (blocks not cloned in the source region will remain unchanged if CLONE_MASK_MODE.FILTERED was used).
  21. }
  22.  
  23.  
  24. local FILL_LIMIT = 32768
  25.  
  26. --- Places a block of type block.block and variant block.variant at x,y,z
  27. function mc.setBlock(x,y,z,block)
  28.     commands.setblock(x,y,z,block.block,block.variant)
  29. end
  30.  
  31. --- Moves the contents of a box from one location to another in the Minecraft World
  32. -- the reference points are the centers of the two boxes
  33. -- returns the new box object it moved to
  34. function mc.moveBoxCenter(from_box,to_box)
  35.     mc.cloneBox(
  36.         from,
  37.         to.corner_x-(from.size_x-to.size_x)/2, to.corner_y, to.corner_z-(from.size_z-to.size_z)/2,
  38.         CLONE_MASK_MODE.REPLACE, CLONE_MODE.MOVE
  39.     )
  40.     return to_box
  41. end
  42.  
  43. --- Copies the contents of a box from one location to another in the Minecraft World
  44. -- the reference points are the centers of the two boxes
  45. -- returns the new box object it copied to
  46. function mc.copyBoxCenter(from_box,to_box)
  47.     mc.cloneBox(
  48.         from,
  49.         to.corner_x-(from.size_x-to.size_x)/2, to.corner_y, to.corner_z-(from.size_z-to.size_z)/2,
  50.         CLONE_MASK_MODE.REPLACE, CLONE_MODE.FORCE
  51.     )
  52.     return to_box
  53. end
  54.  
  55. --- Copies the contents of a box from one location to another in the Minecraft World
  56. -- uses the masked option to copy only blocks that are not air.
  57. -- the reference points are the centers of the two boxes
  58. -- returns the new box object it copied to
  59. function mc.copyBoxCenterMasked(from_box,to_box, with_base)
  60.     mc.cloneBox(
  61.         from,
  62.         to.corner_x-(from.size_x-to.size_x)/2, to.corner_y, to.corner_z-(from.size_z-to.size_z)/2,
  63.         CLONE_MASK_MODE.MASKED, CLONE_MODE.FORCE
  64.     )
  65.     return to_box
  66. end
  67.  
  68. --- Fills a region recursively even if it is bigger by the allowed number of blocks.
  69. -- @param x
  70. -- @param y
  71. -- @param z
  72. -- @param sizeX
  73. -- @param sizeY
  74. -- @param sizeZ
  75. -- @param fill_material A table with field .block and .variant
  76. -- @param replace_material Optional, A table with fields .block and .variant
  77. function mc.fillBox(_box, fill_material,replace_material)
  78.     --check if volume is bigger than maximum blocks 32768
  79.     if _box then
  80.         local x, y, z = _box.corner_x, _box.corner_y, _box.corner_z
  81.         local w, h, l = _box.size_x, _box.size_y, _box.size_z  
  82.         if math.abs(w*l*h) <= FILL_LIMIT then
  83.             --if not then do simple fill
  84.             if replace_material then
  85.                 commands.async.fill(x,y,z,x+w-(w/math.abs(w)),y+h-(h/math.abs(h)),z+l-(l/math.abs(l)),fill_material.block, fill_material.variant, "replace", replace_material.block, replace_material.variant)
  86.             else
  87.                 commands.async.fill(x,y,z,x+w-(w/math.abs(w)),y+h-(h/math.abs(h)),z+l-(l/math.abs(l)),fill_material.block, fill_material.variant )
  88.             end
  89.             --return true
  90.         else
  91.             --otherwise divide in 8 sub-boxes and run again
  92.             local subboxes = box_subdivide(_box)
  93.             if subboxes then
  94.                 for i, sbox in ipairs(subboxes) do
  95.                     mc.fillBox(sbox, fill_material, replace_material)
  96.                 end
  97.             end
  98.             --return true
  99.         end
  100.     end
  101.     --return false
  102. end
  103.  
  104. --- Creates a ring of blocks using coordinates.
  105. function mc.fillRing(_box, fill_material,replace_material)
  106.     local x, y, z = _box.corner_x, _box.corner_y, _box.corner_z
  107.     local w, h, l = _box.size_x, _box.size_y, _box.size_z
  108.     mc.fillBox( box(x,y,z,w,h,1),       fill_material, replace_material)
  109.     mc.fillBox( box(x,y,z,1,h,l),       fill_material, replace_material)
  110.     mc.fillBox( box(x+w-1,y,z,1,h,l),   fill_material, replace_material)
  111.     mc.fillBox( box(x,y,z+l-1,w,h,1),   fill_material, replace_material)
  112. end
  113.  
  114. -- Fills a horizontal region with grid markers.
  115. -- takes the grid origin point from the box's base_center
  116. -- and the grid step from grid_step
  117. function mc.fillGrid( _box, center_x, center_y, center_z, grid_step, fill_material)
  118.     --x,y,z,sizeX,sizeZ, center_x, center_z
  119.    
  120.     --safeguard the arguments
  121.     assert((type(grid_step) == 'number' and grid_step~=0), "fillGrid(): invalid argument [grid_step]: must be a number different from 0")
  122.     assert(type(fill_material) == 'table', "fillGrid(): invalid argument [fill_material]: must be a table")
  123.     assert(fill_material.block, "fillGrid(): invalid argument [fill_material]: missing .block field")
  124.     assert(fill_material.variant, "fillGrid(): invalid argument [fill_material]: missing .variant field")
  125.     -- run the function
  126.     local x, y, z = _box.corner_x, _box.corner_y, _box.corner_z
  127.     local uX = _box.size_x/math.abs(_box.size_x)
  128.     local uY = _box.size_y/math.abs(_box.size_y)
  129.     local uZ = _box.size_z/math.abs(_box.size_z)
  130.     for ix = x, x+_box.size_x-uX, uX do
  131.         --for iy = y, y+_box.size_y-uY, uY do
  132.             for iz = z, z+_box.size_z-uZ, uZ do
  133.                 --check if the coordinate is a grid point
  134.                 if (ix-center_x) % grid_step == 0
  135.                     --[[and (iy-center_y) % grid_step == 0]]
  136.                     and (iz-center_z) % grid_step == 0 then
  137.                     --debug.log("fillGrid(): placing grid marker at: { "..ix..", "..y..", "..iz.." }")
  138.                     commands.async.setblock(ix, y, iz, fill_material.block, fill_material.variant)
  139.                 end
  140.             end
  141.             --end
  142.     end
  143. end
  144.  
  145. --- Clones an area even if it is bigger than the minecraft limit.
  146. -- @param maskMode from CLONE_MASK_MODE
  147. -- @param cloneMode from CLONE_MODE
  148. -- @param filteredMaterial table with .block and .variant
  149. function mc.cloneBox(_box, to_x, to_y, to_z, maskMode, cloneMode, filteredMaterial)
  150.     -- clone command works as this: https://www.digminecraft.com/game_commands/clone_command.php
  151.     -- /clone <x1> <y1> <z1>  <x2> <y2> <z2>  <x> <y> <z> [maskMode] [cloneMode] [tileName] [tileData]
  152.     --check if volume is bigger than maximum blocks 32768
  153.     if _box then
  154.         local x, y, z = _box.corner_x, _box.corner_y, _box.corner_z
  155.         local w, h, l = _box.size_x, _box.size_y, _box.size_z  
  156.         if math.abs(w*l*h) <= FILL_LIMIT then
  157.             --if not then do simple clone
  158.             if filteredMaterial then
  159.                 commands.async.clone(
  160.                     x, y, z,
  161.                     x+w-(w/math.abs(w)), y+h-(h/math.abs(h)), z+l-(l/math.abs(l)),
  162.                     to_x, to_y, to_z,
  163.                     maskMode or CLONE_MASK_MODE.REPLACE, cloneMode or CLONE_MODE.NORMAL, filteredMaterial.block, filteredMaterial.variant) 
  164.             else
  165.                 commands.async.clone(
  166.                     x, y, z,
  167.                     x+w-(w/math.abs(w)), y+h-(h/math.abs(h)), z+l-(l/math.abs(l)),
  168.                     to_x, to_y, to_z,
  169.                     maskMode or CLONE_MASK_MODE.REPLACE, cloneMode or CLONE_MODE.NORMAL)
  170.             end
  171.             --return true
  172.         else
  173.             --otherwise divide in 8 sub-boxes and run again
  174.             local subboxes = box_subdivide(_box)
  175.             if subboxes then
  176.                 for i, sbox in ipairs(subboxes) do
  177.                     mc.cloneBox(sbox, to_x, to_y, to_z, maskMode, cloneMode, filteredMaterial)
  178.                 end
  179.             end        
  180.         end
  181.     end
  182.     --return false
  183. end
  184.  
  185. -- ENTITY FUNCTIONS
  186.  
  187. --- Kills all entities
  188. --if entity_type is specified as string then kills all from that entity type
  189. function mc.killAllEntities(entity_type)
  190.     local selector = ""
  191.     if type(entity_type) == 'string' then selector = "[type="..entity_type.."]" end
  192.     commands.exec("kill @e"..selector)
  193. end
  194.  
  195. --- Removes all labels from the world by killing all ArmorStands
  196. function mc.removeAllLabels()
  197.     mc.killAllEntities("ArmorStand")
  198. end
  199.  
  200. --- Places a label in the world
  201. -- Uses an invisible ArmorStand to do this
  202. function mc.placeLabel(x,y,z,label_text)
  203.     commands.summon("ArmorStand",x,y,z,'{CustomName:"'..label_text..'",Invisible:1b,CustomNameVisible:1b}')
  204. end
  205.  
  206. --PARTICLE FUNCTIONS
  207. --particles shown to player while their key shape is being checked for match
  208. function mc.searchParticle(x,y,z)
  209.     commands.async.particle("fireworksSpark",x,y,z,0.01,3,0.01,0.01,100)
  210. end
  211. -- particles shown to player on successful key match
  212. function mc.successParticle(x,y,z)
  213.     commands.async.particle("happyVillager",x,y,z,2,2,2,1,1000)
  214.     commands.async.playsound("random.levelup","@a",x,y,z,1,1.2)
  215. end
  216. --- particles shown to player on failed key match
  217. function mc.failParticle(x,y,z)
  218.     commands.async.particle("reddust",x,y,z,0.1,0.1,1.5,1,200)
  219.     commands.async.particle("reddust",x,y,z,1.5,0.1,0.1,1,200)
  220.     commands.async.playsound("random.bowhit","@a",x,y,z,1,0.8)
  221. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement