From f9ab8ea66a85b0c529809e2a9ddb5d9f6d418747 Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Mon, 31 Mar 2025 23:07:49 +0800 Subject: [PATCH] added collider --- .../furniture/BukkitFurnitureElement.java | 5 ++- .../furniture/BukkitFurnitureManager.java | 11 +------ .../entity/furniture/LoadedFurniture.java | 32 +++++++++++++++++-- .../furniture/hitbox/HappyGhastHitBox.java | 3 +- .../furniture/hitbox/InteractionHitBox.java | 6 ++-- .../furniture/hitbox/ShulkerHitBox.java | 10 ++++-- .../core/entity/furniture/Collider.java | 9 +----- .../entity/furniture/FurnitureElement.java | 2 +- .../core/entity/furniture/HitBox.java | 3 +- gradle.properties | 2 +- 10 files changed, 50 insertions(+), 33 deletions(-) diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureElement.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureElement.java index 1471a4a91..b310cacfb 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureElement.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureElement.java @@ -9,7 +9,6 @@ import net.momirealms.craftengine.core.entity.furniture.ItemDisplayContext; import net.momirealms.craftengine.core.item.Item; import net.momirealms.craftengine.core.plugin.CraftEngine; import net.momirealms.craftengine.core.util.Key; -import net.momirealms.craftengine.core.util.QuaternionUtils; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.joml.Quaternionf; @@ -34,9 +33,9 @@ public class BukkitFurnitureElement extends AbstractFurnitureElement { } @Override - public void addSpawnPackets(int entityId, double x, double y, double z, float yaw, Consumer packets) { + public void addSpawnPackets(int entityId, double x, double y, double z, float yaw, Quaternionf conjugated, Consumer packets) { try { - Vector3f offset = QuaternionUtils.toQuaternionf(0, Math.toRadians(180 - yaw), 0).conjugate().transform(new Vector3f(position())); + Vector3f offset = conjugated.transform(new Vector3f(position())); packets.accept(Reflections.constructor$ClientboundAddEntityPacket.newInstance( entityId, UUID.randomUUID(), x + offset.x, y + offset.y, z - offset.z, 0, yaw, Reflections.instance$EntityType$ITEM_DISPLAY, 0, Reflections.instance$Vec3$Zero, 0 diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureManager.java index 6aba837bb..33adbc8d8 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureManager.java @@ -1,7 +1,6 @@ package net.momirealms.craftengine.bukkit.entity.furniture; import net.momirealms.craftengine.bukkit.entity.furniture.hitbox.InteractionHitBox; -import net.momirealms.craftengine.bukkit.nms.CollisionEntity; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine; import net.momirealms.craftengine.bukkit.util.EntityUtils; @@ -145,10 +144,9 @@ public class BukkitFurnitureManager implements FurnitureManager { List> colliderConfigs = (List>) placementArguments.getOrDefault("colliders", List.of()); List colliders = new ArrayList<>(); for (Map config : colliderConfigs) { - if (!config.containsKey("width") && !config.containsKey("height")) { + if (!config.containsKey("position")) { colliders.add(new Collider( (boolean) config.getOrDefault("can-be-hit-by-projectile", false), - MiscUtils.getVector3f(config.getOrDefault("position", "0")), MiscUtils.getVector3f(config.getOrDefault("point-1", "0")), MiscUtils.getVector3f(config.getOrDefault("point-2", "0")) )); @@ -234,13 +232,6 @@ public class BukkitFurnitureManager implements FurnitureManager { tryLeavingSeat(player, vehicle); } } - for (World world : Bukkit.getWorlds()) { - for (Entity entity : world.getEntities()) { - if (FastNMS.INSTANCE.method$CraftEntity$getHandle(entity) instanceof CollisionEntity) { - entity.remove(); - } - } - } } @Override diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/LoadedFurniture.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/LoadedFurniture.java index 392c51e64..25faaa9b7 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/LoadedFurniture.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/LoadedFurniture.java @@ -1,5 +1,7 @@ package net.momirealms.craftengine.bukkit.entity.furniture; +import net.momirealms.craftengine.bukkit.nms.CollisionEntity; +import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.util.EntityUtils; import net.momirealms.craftengine.bukkit.util.LegacyAttributeUtils; import net.momirealms.craftengine.bukkit.util.Reflections; @@ -17,6 +19,7 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.ItemDisplay; import org.bukkit.persistence.PersistentDataType; import org.jetbrains.annotations.NotNull; +import org.joml.Quaternionf; import org.joml.Vector3f; import java.lang.ref.WeakReference; @@ -55,18 +58,26 @@ public class LoadedFurniture { List hitBoxEntityIds = new ArrayList<>(); CustomFurniture.Placement placement = furniture.getPlacement(anchorType); + double yawInRadius = Math.toRadians(180 - this.location.getYaw()); + Quaternionf conjugated = QuaternionUtils.toQuaternionf(0, yawInRadius, 0).conjugate(); + + double x = location.getX(); + double y = location.getY(); + double z = location.getZ(); + float yaw = this.location.getYaw(); + List packets = new ArrayList<>(); for (FurnitureElement element : placement.elements()) { int entityId = Reflections.instance$Entity$ENTITY_COUNTER.incrementAndGet(); fakeEntityIds.add(entityId); - element.addSpawnPackets(entityId, this.location.getX(), this.location.getY(), this.location.getZ(), this.location.getYaw(), packets::add); + element.addSpawnPackets(entityId, x, y, z, yaw, conjugated, packets::add); } for (HitBox hitBox : placement.hitBoxes()) { int[] ids = hitBox.acquireEntityIds(Reflections.instance$Entity$ENTITY_COUNTER::incrementAndGet); for (int entityId : ids) { fakeEntityIds.add(entityId); hitBoxEntityIds.add(entityId); - hitBox.addSpawnPackets(ids, this.location.getX(), this.location.getY(), this.location.getZ(), this.location.getYaw(), packets::add); + hitBox.addSpawnPackets(ids, x, y, z, yaw, conjugated, packets::add); this.hitBoxes.put(entityId, hitBox); } } @@ -77,6 +88,23 @@ public class LoadedFurniture { } this.fakeEntityIds = fakeEntityIds; this.hitBoxEntityIds = hitBoxEntityIds; + + if (placement.colliders().length != 0) { + Object world = FastNMS.INSTANCE.field$CraftWorld$ServerLevel(this.location.getWorld()); + for (Collider collider : placement.colliders()) { + Vector3f offset1 = conjugated.transform(new Vector3f(collider.point1())); + Vector3f offset2 = conjugated.transform(new Vector3f(collider.point2())); + double x1 = x + offset1.x(); + double x2 = x + offset2.x(); + double y1 = y + offset1.y(); + double y2 = y + offset2.y(); + double z1 = z - offset1.z(); + double z2 = z - offset2.z(); + Object aabb = FastNMS.INSTANCE.constructor$AABB(x1, y1, z1, x2, y2, z2); + CollisionEntity entity = FastNMS.INSTANCE.createCollisionEntity(world, aabb, x, y, z, true, collider.canBeHitByProjectile()); + FastNMS.INSTANCE.method$LevelWriter$addFreshEntity(world, entity); + } + } } @NotNull diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/HappyGhastHitBox.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/HappyGhastHitBox.java index f9159c006..98b4766bd 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/HappyGhastHitBox.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/HappyGhastHitBox.java @@ -3,6 +3,7 @@ package net.momirealms.craftengine.bukkit.entity.furniture.hitbox; import net.momirealms.craftengine.core.entity.furniture.*; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.MiscUtils; +import org.joml.Quaternionf; import org.joml.Vector3f; import java.util.Map; @@ -28,7 +29,7 @@ public class HappyGhastHitBox extends AbstractHitBox { } @Override - public void addSpawnPackets(int[] entityId, double x, double y, double z, float yaw, Consumer packets) { + public void addSpawnPackets(int[] entityId, double x, double y, double z, float yaw, Quaternionf conjugated, Consumer packets) { // todo 乐魂 } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/InteractionHitBox.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/InteractionHitBox.java index f86d1ebbb..991131b1c 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/InteractionHitBox.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/InteractionHitBox.java @@ -5,7 +5,7 @@ import net.momirealms.craftengine.bukkit.util.Reflections; import net.momirealms.craftengine.core.entity.furniture.*; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.MiscUtils; -import net.momirealms.craftengine.core.util.QuaternionUtils; +import org.joml.Quaternionf; import org.joml.Vector3f; import java.util.ArrayList; @@ -46,8 +46,8 @@ public class InteractionHitBox extends AbstractHitBox { } @Override - public void addSpawnPackets(int[] entityId, double x, double y, double z, float yaw, Consumer packets) { - Vector3f offset = QuaternionUtils.toQuaternionf(0f, Math.toRadians(180f - yaw), 0f).conjugate().transform(new Vector3f(position())); + public void addSpawnPackets(int[] entityId, double x, double y, double z, float yaw, Quaternionf conjugated, Consumer packets) { + Vector3f offset = conjugated.transform(new Vector3f(position())); try { packets.accept(Reflections.constructor$ClientboundAddEntityPacket.newInstance( entityId[0], UUID.randomUUID(), x + offset.x, y + offset.y, z - offset.z, 0, yaw, diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/ShulkerHitBox.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/ShulkerHitBox.java index 0f41b384a..bc8ab0803 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/ShulkerHitBox.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/ShulkerHitBox.java @@ -5,7 +5,11 @@ import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.util.DirectionUtils; import net.momirealms.craftengine.bukkit.util.Reflections; import net.momirealms.craftengine.core.entity.furniture.*; -import net.momirealms.craftengine.core.util.*; +import net.momirealms.craftengine.core.util.Direction; +import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.MiscUtils; +import net.momirealms.craftengine.core.util.VersionHelper; +import org.joml.Quaternionf; import org.joml.Vector3f; import java.util.*; @@ -46,8 +50,8 @@ public class ShulkerHitBox extends AbstractHitBox { } @Override - public void addSpawnPackets(int[] entityIds, double x, double y, double z, float yaw, Consumer packets) { - Vector3f offset = QuaternionUtils.toQuaternionf(0f, Math.toRadians(180f - yaw), 0f).conjugate().transform(new Vector3f(position())); + public void addSpawnPackets(int[] entityIds, double x, double y, double z, float yaw, Quaternionf conjugated, Consumer packets) { + Vector3f offset = conjugated.transform(new Vector3f(position())); try { packets.accept(Reflections.constructor$ClientboundAddEntityPacket.newInstance( entityIds[0], UUID.randomUUID(), x + offset.x, y + offset.y, z - offset.z, 0, yaw, diff --git a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/Collider.java b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/Collider.java index a5f4b92c8..a32b4b213 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/Collider.java +++ b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/Collider.java @@ -3,21 +3,18 @@ package net.momirealms.craftengine.core.entity.furniture; import org.joml.Vector3f; public class Collider { - private final Vector3f position; private final Vector3f point1; private final Vector3f point2; private final boolean canBeHitByProjectile; - public Collider(boolean canBeHitByProjectile, Vector3f position, Vector3f point1, Vector3f point2) { + public Collider(boolean canBeHitByProjectile, Vector3f point1, Vector3f point2) { this.canBeHitByProjectile = canBeHitByProjectile; - this.position = position; this.point1 = point1; this.point2 = point2; } public Collider(boolean canBeHitByProjectile, Vector3f position, float width, float height) { this.canBeHitByProjectile = canBeHitByProjectile; - this.position = position; this.point1 = new Vector3f(position.x - width / 2, position.y, position.z - width / 2); this.point2 = new Vector3f(position.x + width / 2, position.y + height, position.z + width / 2); } @@ -26,10 +23,6 @@ public class Collider { return canBeHitByProjectile; } - public Vector3f position() { - return position; - } - public Vector3f point1() { return point1; } diff --git a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/FurnitureElement.java b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/FurnitureElement.java index 335e57b1e..0f048e9bf 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/FurnitureElement.java +++ b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/FurnitureElement.java @@ -21,5 +21,5 @@ public interface FurnitureElement { Vector3f position(); - void addSpawnPackets(int entityId, double x, double y, double z, float yaw, Consumer packets); + void addSpawnPackets(int entityId, double x, double y, double z, float yaw, Quaternionf conjugated, Consumer packets); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/HitBox.java b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/HitBox.java index 67eae16bf..dcbaca37e 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/HitBox.java +++ b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/HitBox.java @@ -1,6 +1,7 @@ package net.momirealms.craftengine.core.entity.furniture; import net.momirealms.craftengine.core.util.Key; +import org.joml.Quaternionf; import org.joml.Vector3f; import java.util.function.Consumer; @@ -10,7 +11,7 @@ public interface HitBox { Key type(); - void addSpawnPackets(int[] entityId, double x, double y, double z, float yaw, Consumer packets); + void addSpawnPackets(int[] entityId, double x, double y, double z, float yaw, Quaternionf conjugated, Consumer packets); int[] acquireEntityIds(Supplier entityIdSupplier); diff --git a/gradle.properties b/gradle.properties index 2d9ed58aa..19e531706 100644 --- a/gradle.properties +++ b/gradle.properties @@ -49,7 +49,7 @@ mojang_brigadier_version=1.0.18 byte_buddy_version=1.15.11 snake_yaml_version=2.3 anti_grief_version=0.13 -nms_helper_version=0.21 +nms_helper_version=0.22 # Ignite Dependencies mixinextras_version=0.4.1 mixin_version=0.15.2+mixin.0.8.7