Skip to content

Commit 09666da

Browse files
committed
feat: Support any bounding box
Also added support to disable entity centering with "center-target".
1 parent bbd2083 commit 09666da

1 file changed

Lines changed: 43 additions & 21 deletions

File tree

‎core/src/main/java/com/nisovin/magicspells/spells/targeted/EntombSpell.java‎

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bukkit.Material;
99
import org.bukkit.Location;
1010
import org.bukkit.block.Block;
11+
import org.bukkit.util.BoundingBox;
1112
import org.bukkit.event.EventHandler;
1213
import org.bukkit.entity.LivingEntity;
1314
import org.bukkit.block.data.BlockData;
@@ -29,6 +30,8 @@ public class EntombSpell extends TargetedSpell implements TargetedEntitySpell {
2930
private final ConfigData<Integer> duration;
3031

3132
private final boolean allowBreaking;
33+
34+
private final ConfigData<Boolean> centerTarget;
3235
private final ConfigData<Boolean> closeTopAndBottom;
3336
private final ConfigData<Boolean> powerAffectsDuration;
3437

@@ -42,6 +45,8 @@ public EntombSpell(MagicConfig config, String spellName) {
4245
duration = getConfigDataInt("duration", 20);
4346

4447
allowBreaking = getConfigBoolean("allow-breaking", true);
48+
49+
centerTarget = getConfigDataBoolean("center-target", true);
4550
closeTopAndBottom = getConfigDataBoolean("close-top-and-bottom", true);
4651
powerAffectsDuration = getConfigDataBoolean("power-affects-duration", true);
4752

@@ -73,27 +78,44 @@ public CastResult castAtEntity(SpellData data) {
7378
List<Block> tombBlocks = new ArrayList<>();
7479

7580
LivingEntity target = data.target();
76-
Block feet = target.getLocation().getBlock();
77-
float pitch = target.getLocation().getPitch();
78-
float yaw = target.getLocation().getYaw();
79-
80-
Location tpLoc = feet.getLocation().add(0.5, 0, 0.5);
81-
tpLoc.setYaw(yaw);
82-
tpLoc.setPitch(pitch);
83-
target.teleportAsync(tpLoc);
84-
85-
tempBlocks.add(feet.getRelative(1, 0, 0));
86-
tempBlocks.add(feet.getRelative(1, 1, 0));
87-
tempBlocks.add(feet.getRelative(-1, 0, 0));
88-
tempBlocks.add(feet.getRelative(-1, 1, 0));
89-
tempBlocks.add(feet.getRelative(0, 0, 1));
90-
tempBlocks.add(feet.getRelative(0, 1, 1));
91-
tempBlocks.add(feet.getRelative(0, 0, -1));
92-
tempBlocks.add(feet.getRelative(0, 1, -1));
93-
94-
if (closeTopAndBottom.get(data)) {
95-
tempBlocks.add(feet.getRelative(0, -1, 0));
96-
tempBlocks.add(feet.getRelative(0, 2, 0));
81+
82+
if (centerTarget.get(data)) {
83+
Location location = target.getLocation();
84+
location.setX(location.getBlockX() + 0.5);
85+
location.setZ(location.getBlockZ() + 0.5);
86+
target.teleport(location);
87+
}
88+
89+
BoundingBox box = target.getBoundingBox();
90+
BoundingBox exp = box.clone().expand(1 - 1e-7);
91+
92+
int minX = (int) Math.floor(exp.getMinX());
93+
int minY = (int) Math.floor(exp.getMinY());
94+
int minZ = (int) Math.floor(exp.getMinZ());
95+
96+
int maxX = (int) Math.ceil(exp.getMaxX());
97+
int maxY = (int) Math.ceil(exp.getMaxY());
98+
int maxZ = (int) Math.ceil(exp.getMaxZ());
99+
100+
boolean closeTopAndBottom = this.closeTopAndBottom.get(data);
101+
for (int x = minX; x < maxX; x++) {
102+
for (int y = minY; y < maxY; y++) {
103+
for (int z = minZ; z < maxZ; z++) {
104+
Block block = target.getWorld().getBlockAt(x, y, z);
105+
if (box.overlaps(BoundingBox.of(block))) continue;
106+
107+
int boundary = 0;
108+
if (x == minX || x == maxX - 1) boundary++;
109+
if (y == minY || y == maxY - 1) {
110+
if (!closeTopAndBottom) continue;
111+
boundary++;
112+
}
113+
if (z == minZ || z == maxZ - 1) boundary++;
114+
if (boundary > 1) continue;
115+
116+
tempBlocks.add(block);
117+
}
118+
}
97119
}
98120

99121
BlockData blockType = this.blockType.get(data);

0 commit comments

Comments
 (0)