From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: commandblockguy <commandblockguy1@gmail.com>
Date: Fri, 14 Aug 2020 14:44:14 -0500
Subject: [PATCH] Prevent headless pistons from being created

Prevent headless pistons from being created by explosions or tree/mushroom growth.

diff --git a/src/main/java/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java
index b0fe3f0ac4c706c4b64f2778fcb8d6c26e585d3c..e7d54a510303653c5f3c7374e4e83101321840c6 100644
--- a/src/main/java/com/destroystokyo/paper/PaperConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java
@@ -459,6 +459,12 @@ public class PaperConfig {
         set("settings.unsupported-settings.allow-tnt-duplication", null);
     }
 
+    public static boolean allowHeadlessPistons;
+    private static void allowHeadlessPistons() {
+        config.set("settings.unsupported-settings.allow-headless-pistons-readme", "This setting controls if players should be able to create headless pistons.");
+        allowHeadlessPistons = getBoolean("settings.unsupported-settings.allow-headless-pistons", false);
+    }
+
     public static int playerAutoSaveRate = -1;
     public static int maxPlayerAutoSavePerTick = 10;
     private static void playerAutoSaveRate() {
diff --git a/src/main/java/net/minecraft/world/level/Explosion.java b/src/main/java/net/minecraft/world/level/Explosion.java
index 1575fb0bbad6e11f25fb9ce51fd1f15a1b11e0fe..132140e00865fcf84ebe03ffcbc2f30ac11a0b35 100644
--- a/src/main/java/net/minecraft/world/level/Explosion.java
+++ b/src/main/java/net/minecraft/world/level/Explosion.java
@@ -15,6 +15,7 @@ import java.util.Random;
 import java.util.Set;
 import javax.annotation.Nullable;
 import net.minecraft.core.BlockPos;
+import net.minecraft.core.Direction;
 import net.minecraft.core.Vec3i;
 import net.minecraft.core.particles.ParticleTypes;
 import net.minecraft.server.level.ServerLevel;
@@ -35,6 +36,8 @@ import net.minecraft.world.level.block.BaseFireBlock;
 import net.minecraft.world.level.block.Block;
 import net.minecraft.world.level.block.Blocks;
 import net.minecraft.world.level.block.entity.BlockEntity;
+import net.minecraft.world.level.block.piston.PistonHeadBlock;
+import net.minecraft.world.level.block.piston.PistonMovingBlockEntity;
 import net.minecraft.world.level.block.state.BlockState;
 import net.minecraft.world.level.gameevent.GameEvent;
 import net.minecraft.world.level.material.FluidState;
@@ -189,6 +192,15 @@ public class Explosion {
 
                             if (f > 0.0F && this.damageCalculator.shouldBlockExplode(this, this.level, blockposition, iblockdata, f)) {
                                 set.add(blockposition);
+                                // Paper start - prevent headless pistons from forming
+                                if (!com.destroystokyo.paper.PaperConfig.allowHeadlessPistons && iblockdata.getBlock() == Blocks.MOVING_PISTON) {
+                                    BlockEntity extension = this.level.getBlockEntity(blockposition);
+                                    if (extension instanceof PistonMovingBlockEntity && ((PistonMovingBlockEntity) extension).isSourcePiston()) {
+                                       Direction direction = iblockdata.getValue(PistonHeadBlock.FACING);
+                                       set.add(blockposition.relative(direction.getOpposite()));
+                                    }
+                                }
+                                // Paper end
                             }
 
                             d4 += d0 * 0.30000001192092896D;
