1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Asynchronous Block Placement

Discussion in 'Snippets' started by Pikachu, Sep 10, 2017.

  1. Pikachu

    Supporter Addon Developer

    Joined:
    Jan 25, 2017
    Messages:
    871
    Likes Received:
    139
    Medals:
    Here is a snippet that allows you to place blocks in a cuboid asynchronously (and therefore without lag, great for minigames and many other uses)
    Code (Skript):
    1. on script load:
    2.   import "com.sk89q.worldedit.regions.CuboidRegion"
    3.   import "com.sk89q.worldedit.Vector" as {WorldEditVector}
    4.   import "com.sk89q.worldedit.EditSession"
    5.   import "com.boydti.fawe.FaweAPI"
    6.   import "com.boydti.fawe.util.EditSessionBuilder"
    7.   import "com.sk89q.worldedit.blocks.BaseBlock"
    8.   import "com.sk89q.worldedit.patterns.BlockChance"
    9.   import "java.util.Arrays"
    10.   import "com.sk89q.worldedit.patterns.RandomFillPattern"
    11.   import "java.util.stream.Collectors"
    12.  
    13. function getRegion(pos1: location, pos2: location) :: object:
    14.   loop 1 and 2:
    15.     set {_vec::%loop-num%} to new {WorldEditVector}(x-loc of {_pos%loop-num%}, y-loc of {_pos%loop-num%} and z-loc of {_pos%loop-num%})
    16.   return new {CuboidRegion}({_vec::*})
    17.  
    18. function getBaseBlock(block: itemtype) :: object:
    19.   return new {BaseBlock}({_block}.getRandom().getTypeId() and {_block}.getRandom().getData().getData())
    20.  
    21. function getBlockChance(block: itemtype, chance: number) :: object:
    22.   return new {BlockChance}(getBaseBlock({_block}) and {_chance})
    23.  
    24. function getWorld(world: world) :: object:
    25.   return {FaweAPI}.getWorld("%{_world}%")
    26.  
    27. function getEditSession(world: world) :: object:
    28.   return new {EditSessionBuilder}(getWorld({_world})).fastmode(true).build()
    29.  
    30. function setBlocks(pos1: location, pos2: location, block: object) :: boolean:
    31.   set {_session} to getEditSession(world of {_pos1})
    32.   set {_args::*} to getRegion({_pos1}, {_pos2}) and getBaseBlock({_block})
    33.   if {_block} is a text:
    34.     loop split {_block} at ",":
    35.       set {_it::*} to join (split loop-value at "%%") with " " parsed as "%integer% %item%"
    36.       if {_it::*} is set:
    37.         add getBlockChance({_it::2}, {_it::1}) to {_c::*}
    38.         delete {_it::*}
    39.       else:
    40.         return false
    41.     set {_args::*} to getRegion({_pos1}, {_pos2})
    42.     add new {RandomFillPattern}({Arrays}.stream([{_c::*} as (java type "%{BlockChance}%")]).collect({Collectors}.toList())) to {_args::*}
    43.   {_session}.setBlocks({_args::*});
    44.   {_session}.flushQueue();
    45.   return true
    Usage:
    • Basic:
      setBlocks({corner one}, {corner two}, dirt)

    • Patterns:
      setBlocks({corner one}, {corner two}, "50%%dirt,25%%air,25%%diamond ore")
    Requires:
    • skript-mirror
    • Fast Async WorldEdit
     
    #1 Pikachu, Sep 10, 2017
    Last edited: Sep 21, 2017
    • Useful Useful x 3
    • Like Like x 1
  2. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,184
    Likes Received:
    227
    Is there any way to count the blocks in progress?
     
  3. ShaneBee

    Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,184
    Likes Received:
    227
    Not as far as I know. If you can find a method for it in the FAWE api, I'll try to implement it. You can use skquery to get the total amount of blocks however that could cause a bit of lag if the region is large
     
    • Like Like x 1
  4. Simuciokas

    Simuciokas New Member

    Joined:
    Apr 26, 2017
    Messages:
    6
    Likes Received:
    5
    Medals:
    For updated skript-mirror version
    Tested on Skript Dev36 and skript-mirror 0.16.0
    Code (Text):
    1.  
    2. import:
    3.     com.sk89q.worldedit.regions.CuboidRegion
    4.     com.sk89q.worldedit.Vector as WorldEditVector
    5.     com.sk89q.worldedit.EditSession
    6.     com.boydti.fawe.FaweAPI
    7.     com.boydti.fawe.util.EditSessionBuilder
    8.     com.sk89q.worldedit.blocks.BaseBlock
    9.     com.sk89q.worldedit.patterns.BlockChance
    10.     java.util.Arrays
    11.     com.sk89q.worldedit.patterns.RandomFillPattern
    12.     java.util.stream.Collectors
    13.  
    14. function getRegion(pos1: location, pos2: location) :: object:
    15.     loop 1 and 2:
    16.         set {_vec::%loop-value%} to new WorldEditVector((x-coord of {_pos%loop-value%}), (y-coord of {_pos%loop-value%}) and (z-coord of {_pos%loop-value%}))
    17.     return new CuboidRegion({_vec::*})
    18.  
    19. function getBaseBlock(block: itemtype) :: object:
    20.     return new BaseBlock({_block}.getRandom().getTypeId() and {_block}.getRandom().getData().getData())
    21.  
    22. function getBlockChance(block: itemtype, chance: number) :: object:
    23.     return new BlockChance(getBaseBlock({_block}) and {_chance})
    24.  
    25. function getWorld(world: world) :: object:
    26.     return FaweAPI.getWorld("%{_world}%")
    27.  
    28. function getEditSession(world: world) :: object:
    29.     return new EditSessionBuilder(getWorld({_world})).fastmode(true).build()
    30.  
    31. function setBlocks(pos1: location, pos2: location, block: object) :: boolean:
    32.     set {_session} to getEditSession(world of {_pos1})
    33.     set {_args::*} to getRegion({_pos1}, {_pos2}) and getBaseBlock({_block})
    34.     if {_block} is a text:
    35.         loop split {_block} at ",":
    36.             set {_it::*} to join (split loop-value at "%%") with " " parsed as "%integer% %item%"
    37.             if {_it::*} is set:
    38.                 add getBlockChance({_it::2}, {_it::1}) to {_c::*}
    39.                 delete {_it::*}
    40.             else:
    41.                 return false
    42.         set {_args::*} to getRegion({_pos1}, {_pos2})
    43.         add new RandomFillPattern(Arrays.stream([{_c::*}]).collect(Collectors.toList())) to {_args::*}
    44.     {_session}.setBlocks({_args::*})
    45.     {_session}.flushQueue()
    46.     return true
    47.  
     
    • Like Like x 2
  5. feerko

    feerko Member

    Joined:
    Feb 19, 2017
    Messages:
    70
    Likes Received:
    1
    Can you add function to count all blocks without air?
     

Share This Page

Loading...