Solved New world generator blocks the server Thread

Discussion in 'Plugin Development' started by whereisthemonkey, Apr 10, 2016.

Thread Status:
Not open for further replies.
  1. Hey together, I was lately learning how to mess around with world generators and world populators.

    I am new to the custom world generation, and whenever I start generating the world, the main server Thread blocks until I close the whole program.

    I have the following code called at the world creation:

    Code:java
    1.  
    2. world = Bukkit.getWorld("Parcels");
    3. if(world == null){
    4. WorldCreator creator = new WorldCreator("Parcels");
    5. creator.generateStructures(false);
    6. creator.type(WorldType.FLAT);
    7. creator.environment(Environment.NORMAL);
    8. creator.generator(new ParcelGenerator()); //Currently in production
    9. world = creator.createWorld();
    10. }
    11.  


    This calls my generator. I apologize for the length of the code, but it is a code to respawn a different amount of ores over the whole world:

    Code:java
    1.  
    2. @Override
    3. public List<BlockPopulator> getDefaultPopulators(World world) {
    4. List<BlockPopulator> populators = new ArrayList<>();
    5.  
    6. populators.add(new ParcelGrassPopulator());
    7. populators.add(new ParcelTreePopulator());
    8.  
    9. return populators;
    10. }
    11.  
    12. @Override
    13. public boolean canSpawn(World world, int x, int z) {
    14. return true;
    15. }
    16.  
    17. public int xyzToByte(int x, int y, int z) {
    18. return (x * 16 + z) * 128 + y;
    19. }
    20.  
    21. @SuppressWarnings("deprecation")
    22. @Override
    23. public byte[] generate(World world, Random random, int chunkX, int chunkZ) {
    24. byte[] result = new byte[32768];
    25. int x, y, z;
    26.  
    27. SimplexOctaveGenerator generator = new SimplexOctaveGenerator(new Random(world.getSeed()), 8);
    28. generator.setScale(1.0D / 48.0D);
    29.  
    30. Random rand = new Random();
    31. int coal = rand.nextInt((150 - 130) + 1) + 130;
    32. int iron = rand.nextInt((90 - 60) + 1) + 60;
    33. int gold = rand.nextInt((50 - 20) + 1) + 20;
    34. int lapis = rand.nextInt((5 - 1) + 1) + 1;
    35. int emerald = rand.nextInt((2 - 0) + 1) + 0;
    36. int redstone = rand.nextInt((35 - 20) + 1) + 20;
    37. int diamond = rand.nextInt((5 - 1) + 1) + 1;
    38.  
    39. for(x = 0; x < 16; x++){
    40. for(z = 0; z < 16; z++){
    41. result[xyzToByte(x, 0, z)] = (byte) Material.BEDROCK.getId();
    42. double noise = generator.noise(x + chunkX * 16, z + chunkZ, 0.5D, 0.5D) * 24;
    43. for(y = 1; y < 32 + noise; y++){
    44. int material = rand.nextInt(100);
    45. if(material < 10){ //10%
    46. //Add gravel or sand
    47. for(int i = 0; i < 1; i++){
    48. if(result[xyzToByte(x, y, z)] == (byte) (Material.COAL_ORE.getId()) ||
    49. result[xyzToByte(x, y, z)] == (byte) (Material.IRON_ORE.getId()) ||
    50. result[xyzToByte(x, y, z)] == (byte) (Material.GOLD_ORE.getId()) ||
    51. result[xyzToByte(x, y, z)] == (byte) (Material.LAPIS_ORE.getId()) ||
    52. result[xyzToByte(x, y, z)] == (byte) (Material.EMERALD_ORE.getId()) ||
    53. result[xyzToByte(x, y, z)] == (byte) (Material.REDSTONE_ORE.getId()) ||
    54. result[xyzToByte(x, y, z)] == (byte) (Material.DIAMOND_ORE.getId())){
    55. i--;
    56. }else{
    57. switch (rand.nextInt(2)) {
    58. case 0:
    59. result[xyzToByte(x, y, z)] = (byte) Material.SAND.getId();
    60. break;
    61. case 1:
    62. result[xyzToByte(x, y, z)] = (byte) Material.GRAVEL.getId();
    63. break;
    64. default:
    65. break;
    66. }
    67. }
    68. }
    69. }else if(material < 30){ //20%
    70. //Set to dirt
    71. for(int i = 0; i < 1; i++){
    72. if(result[xyzToByte(x, y, z)] == (byte) (Material.COAL_ORE.getId()) ||
    73. result[xyzToByte(x, y, z)] == (byte) (Material.IRON_ORE.getId()) ||
    74. result[xyzToByte(x, y, z)] == (byte) (Material.GOLD_ORE.getId()) ||
    75. result[xyzToByte(x, y, z)] == (byte) (Material.LAPIS_ORE.getId()) ||
    76. result[xyzToByte(x, y, z)] == (byte) (Material.EMERALD_ORE.getId()) ||
    77. result[xyzToByte(x, y, z)] == (byte) (Material.REDSTONE_ORE.getId()) ||
    78. result[xyzToByte(x, y, z)] == (byte) (Material.DIAMOND_ORE.getId())){
    79. i--;
    80. }else{
    81. result[xyzToByte(x, y, z)] = (byte) Material.DIRT.getId();
    82. }
    83. }
    84. }else if(material < 100){ //40%
    85. //Set to stone
    86. for(int i = 0; i < 1; i++){
    87. if(result[xyzToByte(x, y, z)] == (byte) (Material.COAL_ORE.getId()) ||
    88. result[xyzToByte(x, y, z)] == (byte) (Material.IRON_ORE.getId()) ||
    89. result[xyzToByte(x, y, z)] == (byte) (Material.GOLD_ORE.getId()) ||
    90. result[xyzToByte(x, y, z)] == (byte) (Material.LAPIS_ORE.getId()) ||
    91. result[xyzToByte(x, y, z)] == (byte) (Material.EMERALD_ORE.getId()) ||
    92. result[xyzToByte(x, y, z)] == (byte) (Material.REDSTONE_ORE.getId()) ||
    93. result[xyzToByte(x, y, z)] == (byte) (Material.DIAMOND_ORE.getId())){
    94. i--;
    95. }else{
    96. result[xyzToByte(x, y, z)] = (byte) Material.STONE.getId();
    97. }
    98. }
    99. }
    100. }
    101. result[xyzToByte(x, y, z)] = (byte) Material.GRASS.getId();
    102. }
    103. }
    104. while(diamond != 0){
    105. for(y = 1; y < 20; y++){
    106. z = rand.nextInt(17);
    107. x = rand.nextInt(17);
    108.  
    109. if(coal > 0){
    110. int size = rand.nextInt((12 - 8) + 1) + 8;
    111. if(size > coal) size = coal;
    112. int xx = x;
    113. int yy = y;
    114. int zz = z;
    115. for (int i = 0; i < size; i++) {
    116. if(result[xyzToByte(xx, yy, zz)] != (byte) (Material.STONE.getId()) &&
    117. result[xyzToByte(xx, yy, zz)] != (byte) (Material.DIRT.getId()) &&
    118. result[xyzToByte(xx, yy, zz)] != (byte) (Material.GRAVEL.getId()) &&
    119. result[xyzToByte(xx, yy, zz)] != (byte) (Material.SAND.getId())){
    120. i--;
    121. switch (rand.nextInt(6)) {
    122. case 0:
    123. if(xx > 0){
    124. xx--;
    125. }else{
    126. xx++;
    127. }
    128. break;
    129. case 1:
    130. xx++;
    131. break;
    132. case 2:
    133. if(yy > 0){
    134. yy--;
    135. }else{
    136. yy++;
    137. }
    138. break;
    139. case 3:
    140. yy++;
    141. break;
    142. case 4:
    143. if(zz > 0){
    144. zz--;
    145. }else{
    146. zz++;
    147. }
    148. break;
    149. case 5:
    150. zz++;
    151. break;
    152. default:
    153. break;
    154. }
    155. }else{
    156. result[xyzToByte(xx, yy, zz)] = (byte) Material.COAL_ORE.getId();
    157. switch (rand.nextInt(6)) {
    158. case 0:
    159. if(xx > 0){
    160. xx--;
    161. }else{
    162. xx++;
    163. }
    164. break;
    165. case 1:
    166. xx++;
    167. break;
    168. case 2:
    169. if(yy > 0){
    170. yy--;
    171. }else{
    172. yy++;
    173. }
    174. break;
    175. case 3:
    176. yy++;
    177. break;
    178. case 4:
    179. if(zz > 0){
    180. zz--;
    181. }else{
    182. zz++;
    183. }
    184. break;
    185. case 5:
    186. zz++;
    187. break;
    188. default:
    189. break;
    190. }
    191. }
    192. }
    193. coal -= size;
    194. }else if(iron > 0){
    195. int size = rand.nextInt((6 - 4) + 1) + 4;
    196. if(size > iron) size = iron;
    197. int xx = x;
    198. int yy = y;
    199. int zz = z;
    200. for (int i = 0; i < size; i++) {
    201. if(result[xyzToByte(xx, yy, zz)] != (byte) (Material.STONE.getId()) &&
    202. result[xyzToByte(xx, yy, zz)] != (byte) (Material.DIRT.getId()) &&
    203. result[xyzToByte(xx, yy, zz)] != (byte) (Material.GRAVEL.getId()) &&
    204. result[xyzToByte(xx, yy, zz)] != (byte) (Material.SAND.getId())){
    205. i--;
    206. switch (rand.nextInt(6)) {
    207. case 0:
    208. if(xx > 0){
    209. xx--;
    210. }else{
    211. xx++;
    212. }
    213. break;
    214. case 1:
    215. xx++;
    216. break;
    217. case 2:
    218. if(yy > 0){
    219. yy--;
    220. }else{
    221. yy++;
    222. }
    223. break;
    224. case 3:
    225. yy++;
    226. break;
    227. case 4:
    228. if(zz > 0){
    229. zz--;
    230. }else{
    231. zz++;
    232. }
    233. break;
    234. case 5:
    235. zz++;
    236. break;
    237. default:
    238. break;
    239. }
    240. }else{
    241. result[xyzToByte(xx, yy, zz)] = (byte) Material.IRON_ORE.getId();
    242. switch (rand.nextInt(6)) {
    243. case 0:
    244. if(xx > 0){
    245. xx--;
    246. }else{
    247. xx++;
    248. }
    249. break;
    250. case 1:
    251. xx++;
    252. break;
    253. case 2:
    254. if(yy > 0){
    255. yy--;
    256. }else{
    257. yy++;
    258. }
    259. break;
    260. case 3:
    261. yy++;
    262. break;
    263. case 4:
    264. if(zz > 0){
    265. zz--;
    266. }else{
    267. zz++;
    268. }
    269. break;
    270. case 5:
    271. zz++;
    272. break;
    273. default:
    274. break;
    275. }
    276. }
    277. }
    278. iron -= size;
    279. }else if(gold > 0){
    280. int size = rand.nextInt((6 - 4) + 1) + 4;
    281. if(size > gold) size = gold;
    282. int xx = x;
    283. int yy = y;
    284. int zz = z;
    285. for (int i = 0; i < size; i++) {
    286. if(result[xyzToByte(xx, yy, zz)] != (byte) (Material.STONE.getId()) &&
    287. result[xyzToByte(xx, yy, zz)] != (byte) (Material.DIRT.getId()) &&
    288. result[xyzToByte(xx, yy, zz)] != (byte) (Material.GRAVEL.getId()) &&
    289. result[xyzToByte(xx, yy, zz)] != (byte) (Material.SAND.getId())){
    290. i--;
    291. switch (rand.nextInt(6)) {
    292. case 0:
    293. if(xx > 0){
    294. xx--;
    295. }else{
    296. xx++;
    297. }
    298. break;
    299. case 1:
    300. xx++;
    301. break;
    302. case 2:
    303. if(yy > 0){
    304. yy--;
    305. }else{
    306. yy++;
    307. }
    308. break;
    309. case 3:
    310. yy++;
    311. break;
    312. case 4:
    313. if(zz > 0){
    314. zz--;
    315. }else{
    316. zz++;
    317. }
    318. break;
    319. case 5:
    320. zz++;
    321. break;
    322. default:
    323. break;
    324. }
    325. }else{
    326. result[xyzToByte(xx, yy, zz)] = (byte) Material.GOLD_ORE.getId();
    327. switch (rand.nextInt(6)) {
    328. case 0:
    329. if(xx > 0){
    330. xx--;
    331. }else{
    332. xx++;
    333. }
    334. break;
    335. case 1:
    336. xx++;
    337. break;
    338. case 2:
    339. if(yy > 0){
    340. yy--;
    341. }else{
    342. yy++;
    343. }
    344. break;
    345. case 3:
    346. yy++;
    347. break;
    348. case 4:
    349. if(zz > 0){
    350. zz--;
    351. }else{
    352. zz++;
    353. }
    354. break;
    355. case 5:
    356. zz++;
    357. break;
    358. default:
    359. break;
    360. }
    361. }
    362. }
    363. gold -= size;
    364. }else if(redstone > 0){
    365. int size = rand.nextInt((6 - 4) + 1) + 4;
    366. if(size > redstone) size = redstone;
    367. int xx = x;
    368. int yy = y;
    369. int zz = z;
    370. for (int i = 0; i < size; i++) {
    371. if(result[xyzToByte(xx, yy, zz)] != (byte) (Material.STONE.getId()) &&
    372. result[xyzToByte(xx, yy, zz)] != (byte) (Material.DIRT.getId()) &&
    373. result[xyzToByte(xx, yy, zz)] != (byte) (Material.GRAVEL.getId()) &&
    374. result[xyzToByte(xx, yy, zz)] != (byte) (Material.SAND.getId())){
    375. i--;
    376. switch (rand.nextInt(6)) {
    377. case 0:
    378. if(xx > 0){
    379. xx--;
    380. }else{
    381. xx++;
    382. }
    383. break;
    384. case 1:
    385. xx++;
    386. break;
    387. case 2:
    388. if(yy > 0){
    389. yy--;
    390. }else{
    391. yy++;
    392. }
    393. break;
    394. case 3:
    395. yy++;
    396. break;
    397. case 4:
    398. if(zz > 0){
    399. zz--;
    400. }else{
    401. zz++;
    402. }
    403. break;
    404. case 5:
    405. zz++;
    406. break;
    407. default:
    408. break;
    409. }
    410. }else{
    411. result[xyzToByte(xx, yy, zz)] = (byte) Material.REDSTONE_ORE.getId();
    412. switch (rand.nextInt(6)) {
    413. case 0:
    414. if(xx > 0){
    415. xx--;
    416. }else{
    417. xx++;
    418. }
    419. break;
    420. case 1:
    421. xx++;
    422. break;
    423. case 2:
    424. if(yy > 0){
    425. yy--;
    426. }else{
    427. yy++;
    428. }
    429. break;
    430. case 3:
    431. yy++;
    432. break;
    433. case 4:
    434. if(zz > 0){
    435. zz--;
    436. }else{
    437. zz++;
    438. }
    439. break;
    440. case 5:
    441. zz++;
    442. break;
    443. default:
    444. break;
    445. }
    446. }
    447. }
    448. redstone -= size;
    449. }else if(lapis > 0){
    450. int size = 1;
    451. if(size > lapis) size = lapis;
    452. for (int i = 0; i < size; i++) {
    453. if(result[xyzToByte(x, y, z)] != (byte) (Material.STONE.getId()) &&
    454. result[xyzToByte(x, y, z)] != (byte) (Material.DIRT.getId()) &&
    455. result[xyzToByte(x, y, z)] != (byte) (Material.GRAVEL.getId()) &&
    456. result[xyzToByte(x, y, z)] != (byte) (Material.SAND.getId())){
    457. i--;
    458. }else{
    459. result[xyzToByte(x, y, z)] = (byte) Material.LAPIS_ORE.getId();
    460. }
    461. }
    462. lapis -= size;
    463. }else if(emerald > 0){
    464. int size = 1;
    465. if(size > emerald) size = emerald;
    466. for (int i = 0; i < size; i++) {
    467. if(result[xyzToByte(x, y, z)] != (byte) (Material.STONE.getId()) &&
    468. result[xyzToByte(x, y, z)] != (byte) (Material.DIRT.getId()) &&
    469. result[xyzToByte(x, y, z)] != (byte) (Material.GRAVEL.getId()) &&
    470. result[xyzToByte(x, y, z)] != (byte) (Material.SAND.getId())){
    471. i--;
    472. }else{
    473. result[xyzToByte(x, y, z)] = (byte) Material.EMERALD_ORE.getId();
    474. }
    475. }
    476. emerald -= size;
    477. }else if(diamond > 0){
    478. int size = 2;
    479. if(size > diamond) size = diamond;
    480. for (int i = 0; i < size; i++) {
    481. if(result[xyzToByte(x, y, z)] != (byte) (Material.STONE.getId()) &&
    482. result[xyzToByte(x, y, z)] != (byte) (Material.DIRT.getId()) &&
    483. result[xyzToByte(x, y, z)] != (byte) (Material.GRAVEL.getId()) &&
    484. result[xyzToByte(x, y, z)] != (byte) (Material.SAND.getId())){
    485. i--;
    486. }else{
    487. result[xyzToByte(x, y, z)] = (byte) Material.DIAMOND_ORE.getId();
    488. }
    489. }
    490. diamond -= size;
    491. }
    492. }
    493. }
    494.  
    495. return result;
    496. }
    497.  


    And here are my block poulators:

    Code:java
    1.  
    2. static class ParcelGrassPopulator extends BlockPopulator{
    3.  
    4. //TODO: Grass +/- per chunk biome
    5.  
    6. @Override
    7. public void populate(World world, Random random, Chunk chunk) {
    8. int x, y, z;
    9. for(x = 0; x < 16; x++){
    10. for(z = 0; z < 16; z++){
    11. if(random.nextInt(100) < 15){
    12. for(y = 80; chunk.getBlock(x, y, z).getType() == Material.AIR; y--);
    13.  
    14. chunk.getBlock(x, y + 1, z).setType(Material.GRASS);
    15. }
    16. }
    17. }
    18. }
    19.  
    20. }
    21.  
    22. static class ParcelTreePopulator extends BlockPopulator{
    23.  
    24. //TODO: Trees +/- per chunk biome
    25.  
    26. @Override
    27. public void populate(World world, Random random, Chunk chunk) {
    28. int x, y, z;
    29. for(x = 0; x < 16; x++){
    30. for(z = 0; z < 16; z++){
    31. if(random.nextInt(1000) < 10){
    32. for(y = 80; chunk.getBlock(x, y, z).getType() == Material.AIR; y--);
    33.  
    34. world.generateTree(new Location(world, x, y + 1, z), TreeType.TREE);
    35. }
    36. }
    37. }
    38. }
    39.  
    40. }
    41.  


    Any help is highly appreciated, thank you very much for your time!
     
  2. Offline

    teej107

    Have tried to wait and see what happens? World generation must always be done on the main thread anyway.
     
  3. Yes, I cancelled it after 15 minutes, world generation never takes that long

    @teej107
     
  4. Offline

    mcdorli

    Do you work with spigot? Also, have your tried debugging where the world generation is currently at?
     
  5. Nevermind, I solved it on my own
     
  6. Offline

    bennie3211

    @whereisthemonkey can you explain what your problem was? So when other people have this problem can check if your solution works.
     
Thread Status:
Not open for further replies.

Share This Page