diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/CustomFurnitureHitboxConfig.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/CustomFurnitureHitboxConfig.java index d7147708c..58e2ed92f 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/CustomFurnitureHitboxConfig.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/CustomFurnitureHitboxConfig.java @@ -78,6 +78,11 @@ public class CustomFurnitureHitboxConfig extends AbstractFurnitureHitBoxConfig aabbConsumer) { + aabbConsumer.accept(AABB.makeBoundingBox(this.position, this.width, this.height)); + } + @Override public CustomFurnitureHitbox create(Furniture furniture) { return new CustomFurnitureHitbox(furniture, this); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/HappyGhastFurnitureHitboxConfig.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/HappyGhastFurnitureHitboxConfig.java index 6264b3a5f..df7458e19 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/HappyGhastFurnitureHitboxConfig.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/HappyGhastFurnitureHitboxConfig.java @@ -63,6 +63,11 @@ public class HappyGhastFurnitureHitboxConfig extends AbstractFurnitureHitBoxConf } } + @Override + public void collectBoundingBox(Consumer aabbConsumer) { + aabbConsumer.accept(AABB.makeBoundingBox(this.position, 4 * this.scale, 4 * this.scale)); + } + public static class Factory implements FurnitureHitBoxConfigFactory { @Override diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/InteractionFurnitureHitboxConfig.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/InteractionFurnitureHitboxConfig.java index 909335b0a..3f823705a 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/InteractionFurnitureHitboxConfig.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/InteractionFurnitureHitboxConfig.java @@ -77,6 +77,11 @@ public class InteractionFurnitureHitboxConfig extends AbstractFurnitureHitBoxCon } } + @Override + public void collectBoundingBox(Consumer aabbConsumer) { + aabbConsumer.accept(AABB.makeBoundingBox(this.position, size.x, size.y)); + } + @Override public InteractionFurnitureHitbox create(Furniture furniture) { return new InteractionFurnitureHitbox(furniture, this); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/ShulkerFurnitureHitboxConfig.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/ShulkerFurnitureHitboxConfig.java index 169acd8f2..ebc750c2a 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/ShulkerFurnitureHitboxConfig.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/hitbox/ShulkerFurnitureHitboxConfig.java @@ -159,6 +159,11 @@ public class ShulkerFurnitureHitboxConfig extends AbstractFurnitureHitBoxConfig< } } + @Override + public void collectBoundingBox(Consumer aabbConsumer) { + aabbConsumer.accept(this.aabbCreator.create(position.x, position.y, position.z, 180, new Vector3f(0))); + } + public float scale() { return this.scale; } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/feature/TestCommand.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/feature/TestCommand.java index cc9bdb782..437855e70 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/feature/TestCommand.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/feature/TestCommand.java @@ -1,12 +1,9 @@ package net.momirealms.craftengine.bukkit.plugin.command.feature; -import net.momirealms.craftengine.bukkit.api.CraftEngineFurniture; -import net.momirealms.craftengine.bukkit.entity.furniture.BukkitFurniture; import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature; import net.momirealms.craftengine.core.plugin.CraftEngine; import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.incendo.cloud.Command; public class TestCommand extends BukkitCommandFeature { @@ -18,14 +15,9 @@ public class TestCommand extends BukkitCommandFeature { @Override public Command.Builder assembleCommand(org.incendo.cloud.CommandManager manager, Command.Builder builder) { return builder - .senderType(Player.class) .handler(context -> { // DO NOT PUSH ANY CODE FOR TEST COMMAND // 禁止推送含有实现的Test指令 - BukkitFurniture furniture = CraftEngineFurniture.rayTrace(context.sender(), 4); - if (furniture != null) { - System.out.println(furniture.id()); - } }); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/AbstractFurnitureManager.java b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/AbstractFurnitureManager.java index c0569fd87..e056dfc73 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/AbstractFurnitureManager.java +++ b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/AbstractFurnitureManager.java @@ -2,6 +2,7 @@ package net.momirealms.craftengine.core.entity.furniture; import net.momirealms.craftengine.core.entity.furniture.element.FurnitureElementConfig; import net.momirealms.craftengine.core.entity.furniture.element.FurnitureElementConfigs; +import net.momirealms.craftengine.core.entity.furniture.hitbox.FurnitureHitBox; import net.momirealms.craftengine.core.entity.furniture.hitbox.FurnitureHitBoxConfig; import net.momirealms.craftengine.core.entity.furniture.hitbox.FurnitureHitBoxTypes; import net.momirealms.craftengine.core.loot.LootTable; @@ -141,9 +142,25 @@ public abstract class AbstractFurnitureManager implements FurnitureManager { hitboxes = List.of(defaultHitBox()); } - // fixme 动态计算aabb,因为家具具有朝向 - AABB maxAABB = new AABB(0,0,0,1,1,1); - + List aabbs = new ArrayList<>(); + for (FurnitureHitBoxConfig hitBox : hitboxes) { + hitBox.collectBoundingBox(aabbs::add); + } + double minX = 0; + double minY = 0; + double minZ = 0; + double maxX = 0; + double maxY = 0; + double maxZ = 0; + for (AABB aabb : aabbs) { + minX = Math.min(minX, aabb.minX); + minY = Math.min(minY, aabb.minY); + minZ = Math.min(minZ, aabb.minZ); + maxX = Math.max(maxX, aabb.maxX); + maxY = Math.max(maxY, aabb.maxY); + maxZ = Math.max(maxZ, aabb.maxZ); + } + AABB maxAABB = new AABB(minX, minY, minZ, maxX, maxY, maxZ); variants.put(variantName, new FurnitureVariant( variantName, parseCullingData(section.get("entity-culling"), maxAABB), @@ -160,7 +177,6 @@ public abstract class AbstractFurnitureManager implements FurnitureManager { .variants(variants) .events(EventFunctions.parseEvents(ResourceConfigUtils.get(section, "events", "event"))) .lootTable(LootTable.fromMap(MiscUtils.castToMap(section.get("loot"), true))) - .build(); AbstractFurnitureManager.this.byId.put(id, furniture); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/hitbox/FurnitureHitBoxConfig.java b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/hitbox/FurnitureHitBoxConfig.java index 689cb130c..f2361c098 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/hitbox/FurnitureHitBoxConfig.java +++ b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/hitbox/FurnitureHitBoxConfig.java @@ -23,4 +23,6 @@ public interface FurnitureHitBoxConfig { boolean canUseItemOn(); void prepareForPlacement(WorldPosition targetPos, Consumer aabbConsumer); + + void collectBoundingBox(Consumer aabbConsumer); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/ReplaceFurnitureFunction.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/ReplaceFurnitureFunction.java index 61eec5c6c..2c4577909 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/ReplaceFurnitureFunction.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/ReplaceFurnitureFunction.java @@ -1,6 +1,5 @@ package net.momirealms.craftengine.core.plugin.context.function; -import net.momirealms.craftengine.core.entity.furniture.AnchorType; import net.momirealms.craftengine.core.entity.furniture.Furniture; import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.Context; @@ -15,7 +14,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; -@SuppressWarnings("deprecation") public class ReplaceFurnitureFunction extends AbstractConditionalFunction { private final Key newFurnitureId; private final NumberProvider x; @@ -23,7 +21,7 @@ public class ReplaceFurnitureFunction extends AbstractCondi private final NumberProvider z; private final NumberProvider pitch; private final NumberProvider yaw; - private final AnchorType anchorType; + private final String variant; private final boolean dropLoot; private final boolean playSound; @@ -34,7 +32,7 @@ public class ReplaceFurnitureFunction extends AbstractCondi NumberProvider z, NumberProvider pitch, NumberProvider yaw, - AnchorType anchorType, + String variant, boolean dropLoot, boolean playSound, List> predicates @@ -46,7 +44,7 @@ public class ReplaceFurnitureFunction extends AbstractCondi this.z = z; this.pitch = pitch; this.yaw = yaw; - this.anchorType = anchorType; + this.variant = variant; this.dropLoot = dropLoot; this.playSound = playSound; } @@ -72,8 +70,7 @@ public class ReplaceFurnitureFunction extends AbstractCondi RemoveFurnitureFunction.removeFurniture(ctx, oldFurniture, dropLoot, playSound); // Place the new furniture - // fixme function -// SpawnFurnitureFunction.spawnFurniture(this.newFurnitureId, newPosition, this.anchorType, this.playSound); + SpawnFurnitureFunction.spawnFurniture(this.newFurnitureId, newPosition, this.variant, this.playSound); } } @@ -96,10 +93,10 @@ public class ReplaceFurnitureFunction extends AbstractCondi NumberProvider z = NumberProviders.fromObject(arguments.getOrDefault("z", "")); NumberProvider pitch = NumberProviders.fromObject(arguments.getOrDefault("pitch", "")); NumberProvider yaw = NumberProviders.fromObject(arguments.getOrDefault("yaw", "")); - AnchorType anchorType = ResourceConfigUtils.getAsEnum(arguments.get("anchor-type"), AnchorType.class, null); + String variant = ResourceConfigUtils.getAsStringOrNull(ResourceConfigUtils.get(arguments, "variant", "anchor-type")); boolean dropLoot = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("drop-loot", true), "drop-loot"); boolean playSound = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("play-sound", true), "play-sound"); - return new ReplaceFurnitureFunction<>(furnitureId, x, y, z, pitch, yaw, anchorType, dropLoot, playSound, getPredicates(arguments)); + return new ReplaceFurnitureFunction<>(furnitureId, x, y, z, pitch, yaw, variant, dropLoot, playSound, getPredicates(arguments)); } } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SpawnFurnitureFunction.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SpawnFurnitureFunction.java index d29b7758e..e297cf2f8 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SpawnFurnitureFunction.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SpawnFurnitureFunction.java @@ -1,5 +1,7 @@ package net.momirealms.craftengine.core.plugin.context.function; +import net.momirealms.craftengine.core.entity.furniture.FurnitureDataAccessor; +import net.momirealms.craftengine.core.plugin.CraftEngine; import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.Context; import net.momirealms.craftengine.core.plugin.context.number.NumberProvider; @@ -55,18 +57,13 @@ public class SpawnFurnitureFunction extends AbstractConditi float pitchValue = this.pitch.getFloat(ctx); float yawValue = this.yaw.getFloat(ctx); WorldPosition position = new WorldPosition(world, xPos, yPos, zPos, pitchValue, yawValue); - // fixme api -// spawnFurniture(this.furnitureId, position, this.anchorType, this.playSound); + spawnFurniture(this.furnitureId, position, this.variant, this.playSound); }); } -// public static void spawnFurniture(Key furnitureId, WorldPosition position, AnchorType anchorType, boolean playSound) { -// CraftEngine.instance().furnitureManager().furnitureById(furnitureId).ifPresent(furniture -> { -// AnchorType anchor = Optional.ofNullable(anchorType).orElse(furniture.getAnyAnchorType()); -// FurnitureDataAccessor extraData = FurnitureDataAccessor.builder().anchorType(anchor).build(); -// CraftEngine.instance().furnitureManager().place(position, furniture, extraData, playSound); -// }); -// } + public static void spawnFurniture(Key furnitureId, WorldPosition position, String variant, boolean playSound) { + CraftEngine.instance().furnitureManager().furnitureById(furnitureId).ifPresent(furniture -> CraftEngine.instance().furnitureManager().place(position, furniture, FurnitureDataAccessor.ofVariant(variant), playSound)); + } @Override public Key type() { diff --git a/core/src/main/java/net/momirealms/craftengine/core/world/collision/AABB.java b/core/src/main/java/net/momirealms/craftengine/core/world/collision/AABB.java index dc328fc4d..94c6f52e2 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/world/collision/AABB.java +++ b/core/src/main/java/net/momirealms/craftengine/core/world/collision/AABB.java @@ -5,6 +5,7 @@ import net.momirealms.craftengine.core.world.BlockPos; import net.momirealms.craftengine.core.world.EntityHitResult; import net.momirealms.craftengine.core.world.Vec3d; import org.jetbrains.annotations.Nullable; +import org.joml.Vector3f; import java.util.Optional; @@ -68,6 +69,17 @@ public class AABB { ); } + public static AABB makeBoundingBox(Vector3f pos, double width, double height) { + return new AABB( + pos.x - width / 2, + pos.y, + pos.z - width / 2, + pos.x + width / 2, + pos.y + height, + pos.z + width / 2 + ); + } + public Optional clip(Vec3d min, Vec3d max) { double[] traceDistance = {1.0}; double deltaX = max.x - min.x;