From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jakub Zacek Date: Mon, 4 Oct 2021 10:16:44 +0200 Subject: [PATCH] Add methods to find targets for lightning strikes diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java index d4129cb5ffa6bea474020c47b82d8905d1f4d9f5..df37739055bc705d9aebf8db4ee2007e366af7dd 100644 --- a/src/main/java/net/minecraft/server/level/ServerLevel.java +++ b/src/main/java/net/minecraft/server/level/ServerLevel.java @@ -751,6 +751,11 @@ public class ServerLevel extends Level implements WorldGenLevel { } protected BlockPos findLightningTargetAround(BlockPos pos) { + // Paper start + return this.findLightningTargetAround(pos, false); + } + public BlockPos findLightningTargetAround(BlockPos pos, boolean returnNullWhenNoTarget) { + // Paper end BlockPos blockposition1 = this.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING, pos); Optional optional = this.findLightningRod(blockposition1); @@ -765,6 +770,7 @@ public class ServerLevel extends Level implements WorldGenLevel { if (!list.isEmpty()) { return ((LivingEntity) list.get(this.random.nextInt(list.size()))).blockPosition(); } else { + if (returnNullWhenNoTarget) return null; // Paper if (blockposition1.getY() == this.getMinBuildHeight() - 1) { blockposition1 = blockposition1.above(2); } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index 518c711264b0d7112c09472764fda4cb6dc32180..004af6a256e76389234723e0f79634f5a8e26c23 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -691,6 +691,23 @@ public class CraftWorld extends CraftRegionAccessor implements World { return (LightningStrike) lightning.getBukkitEntity(); } + // Paper start + @Override + public Location findLightningRod(Location location) { + return this.world.findLightningRod(net.minecraft.server.MCUtil.toBlockPosition(location)) + .map(blockPos -> net.minecraft.server.MCUtil.toLocation(this.world, blockPos) + // get the actual rod pos + .subtract(0, 1, 0)) + .orElse(null); + } + + @Override + public Location findLightningTarget(Location location) { + final BlockPos pos = this.world.findLightningTargetAround(net.minecraft.server.MCUtil.toBlockPosition(location), true); + return pos == null ? null : net.minecraft.server.MCUtil.toLocation(this.world, pos); + } + // Paper end + @Override public boolean generateTree(Location loc, TreeType type) { return generateTree(loc, CraftWorld.rand, type);