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

Solved Generate Chests Random

Comments in 'Plugin Development' started by TheBison_, Apr 10, 2016.

  1. TheBison_
    Offline

    TheBison_ New Member

    Joined:
    Mar 31, 2014
    Posts:
    46
    Minecraft User:
    TheBisonXD_
    How can I find out the Y coordinate of the highest placing block if I have the X and Z coordinates given?

    I want to place some chests random!

    PHP:
        public function randomChest(Level $level)
        {
            
    $randx mt_rand(-200200);
            
    $randz mt_rand(-200200);
            
    //$y = Coordinates of the highest placed block
           
            
    $chest Block::get(Block::CHEST);
            
    $level->setBlock(new Vector3($randx$y+1$randz), $chest);
        }
  2. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
    $catch = [09];
    $y 1;
    while(
    $y <= 128){
       if(
    in_array($level->getBlockIdAt($x$y$z), $catchtrue)) break;
    }
    TheBison_ likes this.
  3. TheBison_
    Offline

    TheBison_ New Member

    Joined:
    Mar 31, 2014
    Posts:
    46
    Minecraft User:
    TheBisonXD_
    If the highest placed block a Grass Block is, will the Chest spawn over it, right?
  4. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    While loops should not be used in plugins, because nothing else can run on the thread until the loop is over.
  5. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    It's same with all other loops
    TheBison_ likes this.
  6. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    You may not notice it because of how short the loop lasts, but when you use a while loop the thread stops until the loop is over, which would appear as the server froze for a moment on a pocket mine server.

    I don't think anyone should use while loops in plugins.
  7. TheBison_
    Offline

    TheBison_ New Member

    Joined:
    Mar 31, 2014
    Posts:
    46
    Minecraft User:
    TheBisonXD_
    Thanks it works! :D
  8. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    If you want to spawn a safe random chest. Basically it runs a loop checking if there are any blocks around that are solid just so it doesn't get stuck inside blocks and when it finds open area around it will place a chest there.

    PHP:
    public function safeRandomChest(Level $lvl$rand 10){
    $randVX = (mt_rand() % ($rand*20)-($rand*20) + $rand*20);//random depending on randomness you choose (default = 10)
    $randVZ = (mt_rand() % ($rand*20)-($rand*20) + $rand*20);//random depending on randomness you choose (default = 10)
    for($y 0$y <= 128; ++$y){//run loop for checking
    $b1_low $level->getBlock(new Vector3($randVX-1$y$randVZ));//block low left
    $b2_low $level->getBlock(new Vector3($randVX$y$randVZ+1));//block low right
    $b1_high $level->getBlock(new Vector3($randVX+1$y$randVZ));//block high left
    $b2_high $level->getBlock(new Vector3($randVX$y+1$randVZ));//block high top
    $b3_high $level->getBlock(new Vector3($randVX$y$randVZ+1));//block high right
    if(($b1_low instanceof Solid) and ($b2_low instanceof Solid)
    and (
    $b1_high instanceof Solid) and (b2_high instanceof Solid)
    and (
    b3_high instanceof Solid)) continue;//if all blocks around are solid, continue checking
    $level->setBlock(new Vector3($randVX$y$randVY), Block::get(Block::CHEST));//if around is empty, set block
    break;
    }
    }
    Last edited: Apr 10, 2016
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You are just setting the block ID. There isn't a chest tile associated with the block. You must add it too.
  10. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    associated with what block, add what?
  11. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    You need to add a tile too. Remember that chests are tiles too ;)
    xBeastMode likes this.
  12. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Ahhh ok , thanks for the info :D
  13. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    And that was last time @PEMapModder ever helped a fellow forum user.
    SOFe and Legoboy0215 like this.

Share This Page

Advertisement