From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: DoctaEnkoda <bierquejason@gmail.com>
Date: Tue, 20 Jul 2021 02:18:23 +0200
Subject: [PATCH] Fix Important Issue Crash with Plugin or Datapack Generation


diff --git a/src/main/java/net/minecraft/world/level/levelgen/feature/DecoratedFeature.java b/src/main/java/net/minecraft/world/level/levelgen/feature/DecoratedFeature.java
index 2d102ea80d3f8e8aad0d57626450a7f30bb6c346..6b99c901d30d4334b1651a1853a79603d74e64bf 100644
--- a/src/main/java/net/minecraft/world/level/levelgen/feature/DecoratedFeature.java
+++ b/src/main/java/net/minecraft/world/level/levelgen/feature/DecoratedFeature.java
@@ -24,12 +24,16 @@ public class DecoratedFeature extends Feature<DecoratedFeatureConfiguration> {
         Random random = getRnd(context.random(), net.pl3x.purpur.PurpurConfig.seedFeatureDecorated); // Purpur
         BlockPos blockPos = context.origin();
         ConfiguredFeature<?, ?> configuredFeature = decoratedFeatureConfiguration.feature.get();
-        decoratedFeatureConfiguration.decorator.getPositions(new DecorationContext(worldGenLevel, chunkGenerator), random, blockPos).forEach((blockPosx) -> {
-            if (configuredFeature.place(worldGenLevel, chunkGenerator, random, blockPosx)) {
-                mutableBoolean.setTrue();
-            }
-
-        });
+        // Purpur Start - Change forEach to Iterator for check with try catch error with caves 1.18
+        java.util.Iterator<BlockPos> blockPosIterator = decoratedFeatureConfiguration.decorator.getPositions(new DecorationContext(worldGenLevel, chunkGenerator), random, blockPos).iterator();
+        while (blockPosIterator.hasNext()) {
+            try {
+                if (configuredFeature.place(worldGenLevel, chunkGenerator, random, blockPosIterator.next())) {
+                    mutableBoolean.setTrue();
+                }
+            } catch (Exception ignored) {} // No need set false (Exception Possible : NullPointerException and RuntimeException
+        }
+        // Purpur End
         return mutableBoolean.isTrue();
     }
 
diff --git a/src/main/java/net/minecraft/world/level/levelgen/feature/TreeFeature.java b/src/main/java/net/minecraft/world/level/levelgen/feature/TreeFeature.java
index 2f71e80f6c9d37a97d1d03b7a1d893c825b315fa..ba36676977cc3665a4ea20c556133d4faf06af3e 100644
--- a/src/main/java/net/minecraft/world/level/levelgen/feature/TreeFeature.java
+++ b/src/main/java/net/minecraft/world/level/levelgen/feature/TreeFeature.java
@@ -149,7 +149,7 @@ public class TreeFeature extends Feature<TreeConfiguration> {
             worldGenLevel.setBlock(pos, state, 19);
         };
         boolean bl = this.doPlace(worldGenLevel, random, blockPos, biConsumer, biConsumer2, treeConfiguration);
-        if (bl && (!set.isEmpty() || !set2.isEmpty())) {
+        if (bl && !set.isEmpty() && !set2.isEmpty()) { // Purpur - Fix Paper#6068
             if (!treeConfiguration.decorators.isEmpty()) {
                 List<BlockPos> list = Lists.newArrayList(set);
                 List<BlockPos> list2 = Lists.newArrayList(set2);
