Skip to content

Commit

Permalink
experimental push collision detection improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 3, 2021
1 parent 108a886 commit 0a2f51e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Expand Up @@ -21,7 +21,9 @@
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.utilities.ScriptUtilities;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;

import java.util.List;
Expand Down Expand Up @@ -255,7 +257,7 @@ public void run() {
Vector newVel = v3.multiply(speed);
lastEntity.setVelocity(newVel);
// Check if the entity has collided with something using the most basic possible calculation
if (!ignoreCollision && (!isSafeBlock(lastEntity.getLocation().add(v3))
if (!ignoreCollision && (!isSafeBlock(lastEntity.getLocation().add(v3).add(expandForBoundingBox(lastEntity.getBukkitEntity(), v3)))
|| !isSafeBlock(lastEntity.getLocation().add(newVel)))) {
runs = maxTicks;
}
Expand Down Expand Up @@ -290,4 +292,13 @@ public void run() {
public static boolean isSafeBlock(Location loc) {
return loc.getBlockY() < 0 || loc.getBlockY() > 255 || !loc.getBlock().getType().isSolid();
}

public static Vector expandForBoundingBox(Entity entity, Vector velDir) {
Vector dir = velDir.lengthSquared() == 0 ? new Vector(0, 0, 0) : velDir.clone().normalize();
if (entity == null || !entity.isValid()) {
return velDir;
}
BoundingBox box = entity.getBoundingBox();
return new Vector(box.getWidthX(), box.getHeight(), box.getWidthZ()).multiply(0.5).multiply(dir).add(velDir);
}
}
Expand Up @@ -100,11 +100,13 @@ public void cancelAndRemove() {
EntityAttachedToMap map = toEntityToData.get(to.getUUID());
if (map != null) {
PlayerAttachMap subMap = map.attachedToMap.get(attached.getUUID());
removeFrom(subMap);
if (subMap.everyoneAttachment == null && subMap.playerToAttachment == null) {
map.attachedToMap.remove(attached.getUUID());
if (map.attachedToMap.isEmpty()) {
toEntityToData.remove(to.getUUID());
if (subMap != null) {
removeFrom(subMap);
if (subMap.everyoneAttachment == null && subMap.playerToAttachment == null) {
map.attachedToMap.remove(attached.getUUID());
if (map.attachedToMap.isEmpty()) {
toEntityToData.remove(to.getUUID());
}
}
}
}
Expand Down

0 comments on commit 0a2f51e

Please sign in to comment.