9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-19 15:09:15 +00:00

自动计算家具最大AABB

This commit is contained in:
XiaoMoMi
2025-12-04 01:49:27 +08:00
parent 1610ec9736
commit a156942344
10 changed files with 66 additions and 30 deletions

View File

@@ -78,6 +78,11 @@ public class CustomFurnitureHitboxConfig extends AbstractFurnitureHitBoxConfig<C
} }
} }
@Override
public void collectBoundingBox(Consumer<AABB> aabbConsumer) {
aabbConsumer.accept(AABB.makeBoundingBox(this.position, this.width, this.height));
}
@Override @Override
public CustomFurnitureHitbox create(Furniture furniture) { public CustomFurnitureHitbox create(Furniture furniture) {
return new CustomFurnitureHitbox(furniture, this); return new CustomFurnitureHitbox(furniture, this);

View File

@@ -63,6 +63,11 @@ public class HappyGhastFurnitureHitboxConfig extends AbstractFurnitureHitBoxConf
} }
} }
@Override
public void collectBoundingBox(Consumer<AABB> aabbConsumer) {
aabbConsumer.accept(AABB.makeBoundingBox(this.position, 4 * this.scale, 4 * this.scale));
}
public static class Factory implements FurnitureHitBoxConfigFactory<HappyGhastFurnitureHitbox> { public static class Factory implements FurnitureHitBoxConfigFactory<HappyGhastFurnitureHitbox> {
@Override @Override

View File

@@ -77,6 +77,11 @@ public class InteractionFurnitureHitboxConfig extends AbstractFurnitureHitBoxCon
} }
} }
@Override
public void collectBoundingBox(Consumer<AABB> aabbConsumer) {
aabbConsumer.accept(AABB.makeBoundingBox(this.position, size.x, size.y));
}
@Override @Override
public InteractionFurnitureHitbox create(Furniture furniture) { public InteractionFurnitureHitbox create(Furniture furniture) {
return new InteractionFurnitureHitbox(furniture, this); return new InteractionFurnitureHitbox(furniture, this);

View File

@@ -159,6 +159,11 @@ public class ShulkerFurnitureHitboxConfig extends AbstractFurnitureHitBoxConfig<
} }
} }
@Override
public void collectBoundingBox(Consumer<AABB> aabbConsumer) {
aabbConsumer.accept(this.aabbCreator.create(position.x, position.y, position.z, 180, new Vector3f(0)));
}
public float scale() { public float scale() {
return this.scale; return this.scale;
} }

View File

@@ -1,12 +1,9 @@
package net.momirealms.craftengine.bukkit.plugin.command.feature; 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.bukkit.plugin.command.BukkitCommandFeature;
import net.momirealms.craftengine.core.plugin.CraftEngine; import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager; import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.incendo.cloud.Command; import org.incendo.cloud.Command;
public class TestCommand extends BukkitCommandFeature<CommandSender> { public class TestCommand extends BukkitCommandFeature<CommandSender> {
@@ -18,14 +15,9 @@ public class TestCommand extends BukkitCommandFeature<CommandSender> {
@Override @Override
public Command.Builder<? extends CommandSender> assembleCommand(org.incendo.cloud.CommandManager<CommandSender> manager, Command.Builder<CommandSender> builder) { public Command.Builder<? extends CommandSender> assembleCommand(org.incendo.cloud.CommandManager<CommandSender> manager, Command.Builder<CommandSender> builder) {
return builder return builder
.senderType(Player.class)
.handler(context -> { .handler(context -> {
// DO NOT PUSH ANY CODE FOR TEST COMMAND // DO NOT PUSH ANY CODE FOR TEST COMMAND
// 禁止推送含有实现的Test指令 // 禁止推送含有实现的Test指令
BukkitFurniture furniture = CraftEngineFurniture.rayTrace(context.sender(), 4);
if (furniture != null) {
System.out.println(furniture.id());
}
}); });
} }

View File

@@ -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.FurnitureElementConfig;
import net.momirealms.craftengine.core.entity.furniture.element.FurnitureElementConfigs; 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.FurnitureHitBoxConfig;
import net.momirealms.craftengine.core.entity.furniture.hitbox.FurnitureHitBoxTypes; import net.momirealms.craftengine.core.entity.furniture.hitbox.FurnitureHitBoxTypes;
import net.momirealms.craftengine.core.loot.LootTable; import net.momirealms.craftengine.core.loot.LootTable;
@@ -141,9 +142,25 @@ public abstract class AbstractFurnitureManager implements FurnitureManager {
hitboxes = List.of(defaultHitBox()); hitboxes = List.of(defaultHitBox());
} }
// fixme 动态计算aabb因为家具具有朝向 List<AABB> aabbs = new ArrayList<>();
AABB maxAABB = new AABB(0,0,0,1,1,1); 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( variants.put(variantName, new FurnitureVariant(
variantName, variantName,
parseCullingData(section.get("entity-culling"), maxAABB), parseCullingData(section.get("entity-culling"), maxAABB),
@@ -160,7 +177,6 @@ public abstract class AbstractFurnitureManager implements FurnitureManager {
.variants(variants) .variants(variants)
.events(EventFunctions.parseEvents(ResourceConfigUtils.get(section, "events", "event"))) .events(EventFunctions.parseEvents(ResourceConfigUtils.get(section, "events", "event")))
.lootTable(LootTable.fromMap(MiscUtils.castToMap(section.get("loot"), true))) .lootTable(LootTable.fromMap(MiscUtils.castToMap(section.get("loot"), true)))
.build(); .build();
AbstractFurnitureManager.this.byId.put(id, furniture); AbstractFurnitureManager.this.byId.put(id, furniture);
} }

View File

@@ -23,4 +23,6 @@ public interface FurnitureHitBoxConfig<H extends FurnitureHitBox> {
boolean canUseItemOn(); boolean canUseItemOn();
void prepareForPlacement(WorldPosition targetPos, Consumer<AABB> aabbConsumer); void prepareForPlacement(WorldPosition targetPos, Consumer<AABB> aabbConsumer);
void collectBoundingBox(Consumer<AABB> aabbConsumer);
} }

View File

@@ -1,6 +1,5 @@
package net.momirealms.craftengine.core.plugin.context.function; 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.entity.furniture.Furniture;
import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.Condition;
import net.momirealms.craftengine.core.plugin.context.Context; import net.momirealms.craftengine.core.plugin.context.Context;
@@ -15,7 +14,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@SuppressWarnings("deprecation")
public class ReplaceFurnitureFunction<CTX extends Context> extends AbstractConditionalFunction<CTX> { public class ReplaceFurnitureFunction<CTX extends Context> extends AbstractConditionalFunction<CTX> {
private final Key newFurnitureId; private final Key newFurnitureId;
private final NumberProvider x; private final NumberProvider x;
@@ -23,7 +21,7 @@ public class ReplaceFurnitureFunction<CTX extends Context> extends AbstractCondi
private final NumberProvider z; private final NumberProvider z;
private final NumberProvider pitch; private final NumberProvider pitch;
private final NumberProvider yaw; private final NumberProvider yaw;
private final AnchorType anchorType; private final String variant;
private final boolean dropLoot; private final boolean dropLoot;
private final boolean playSound; private final boolean playSound;
@@ -34,7 +32,7 @@ public class ReplaceFurnitureFunction<CTX extends Context> extends AbstractCondi
NumberProvider z, NumberProvider z,
NumberProvider pitch, NumberProvider pitch,
NumberProvider yaw, NumberProvider yaw,
AnchorType anchorType, String variant,
boolean dropLoot, boolean dropLoot,
boolean playSound, boolean playSound,
List<Condition<CTX>> predicates List<Condition<CTX>> predicates
@@ -46,7 +44,7 @@ public class ReplaceFurnitureFunction<CTX extends Context> extends AbstractCondi
this.z = z; this.z = z;
this.pitch = pitch; this.pitch = pitch;
this.yaw = yaw; this.yaw = yaw;
this.anchorType = anchorType; this.variant = variant;
this.dropLoot = dropLoot; this.dropLoot = dropLoot;
this.playSound = playSound; this.playSound = playSound;
} }
@@ -72,8 +70,7 @@ public class ReplaceFurnitureFunction<CTX extends Context> extends AbstractCondi
RemoveFurnitureFunction.removeFurniture(ctx, oldFurniture, dropLoot, playSound); RemoveFurnitureFunction.removeFurniture(ctx, oldFurniture, dropLoot, playSound);
// Place the new furniture // Place the new furniture
// fixme function SpawnFurnitureFunction.spawnFurniture(this.newFurnitureId, newPosition, this.variant, this.playSound);
// SpawnFurnitureFunction.spawnFurniture(this.newFurnitureId, newPosition, this.anchorType, this.playSound);
} }
} }
@@ -96,10 +93,10 @@ public class ReplaceFurnitureFunction<CTX extends Context> extends AbstractCondi
NumberProvider z = NumberProviders.fromObject(arguments.getOrDefault("z", "<arg:furniture.z>")); NumberProvider z = NumberProviders.fromObject(arguments.getOrDefault("z", "<arg:furniture.z>"));
NumberProvider pitch = NumberProviders.fromObject(arguments.getOrDefault("pitch", "<arg:furniture.pitch>")); NumberProvider pitch = NumberProviders.fromObject(arguments.getOrDefault("pitch", "<arg:furniture.pitch>"));
NumberProvider yaw = NumberProviders.fromObject(arguments.getOrDefault("yaw", "<arg:furniture.yaw>")); NumberProvider yaw = NumberProviders.fromObject(arguments.getOrDefault("yaw", "<arg:furniture.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 dropLoot = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("drop-loot", true), "drop-loot");
boolean playSound = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("play-sound", true), "play-sound"); 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));
} }
} }
} }

View File

@@ -1,5 +1,7 @@
package net.momirealms.craftengine.core.plugin.context.function; 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.Condition;
import net.momirealms.craftengine.core.plugin.context.Context; import net.momirealms.craftengine.core.plugin.context.Context;
import net.momirealms.craftengine.core.plugin.context.number.NumberProvider; import net.momirealms.craftengine.core.plugin.context.number.NumberProvider;
@@ -55,18 +57,13 @@ public class SpawnFurnitureFunction<CTX extends Context> extends AbstractConditi
float pitchValue = this.pitch.getFloat(ctx); float pitchValue = this.pitch.getFloat(ctx);
float yawValue = this.yaw.getFloat(ctx); float yawValue = this.yaw.getFloat(ctx);
WorldPosition position = new WorldPosition(world, xPos, yPos, zPos, pitchValue, yawValue); WorldPosition position = new WorldPosition(world, xPos, yPos, zPos, pitchValue, yawValue);
// fixme api spawnFurniture(this.furnitureId, position, this.variant, this.playSound);
// spawnFurniture(this.furnitureId, position, this.anchorType, this.playSound);
}); });
} }
// public static void spawnFurniture(Key furnitureId, WorldPosition position, AnchorType anchorType, boolean playSound) { public static void spawnFurniture(Key furnitureId, WorldPosition position, String variant, boolean playSound) {
// CraftEngine.instance().furnitureManager().furnitureById(furnitureId).ifPresent(furniture -> { CraftEngine.instance().furnitureManager().furnitureById(furnitureId).ifPresent(furniture -> CraftEngine.instance().furnitureManager().place(position, furniture, FurnitureDataAccessor.ofVariant(variant), playSound));
// AnchorType anchor = Optional.ofNullable(anchorType).orElse(furniture.getAnyAnchorType()); }
// FurnitureDataAccessor extraData = FurnitureDataAccessor.builder().anchorType(anchor).build();
// CraftEngine.instance().furnitureManager().place(position, furniture, extraData, playSound);
// });
// }
@Override @Override
public Key type() { public Key type() {

View File

@@ -5,6 +5,7 @@ import net.momirealms.craftengine.core.world.BlockPos;
import net.momirealms.craftengine.core.world.EntityHitResult; import net.momirealms.craftengine.core.world.EntityHitResult;
import net.momirealms.craftengine.core.world.Vec3d; import net.momirealms.craftengine.core.world.Vec3d;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.joml.Vector3f;
import java.util.Optional; 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<EntityHitResult> clip(Vec3d min, Vec3d max) { public Optional<EntityHitResult> clip(Vec3d min, Vec3d max) {
double[] traceDistance = {1.0}; double[] traceDistance = {1.0};
double deltaX = max.x - min.x; double deltaX = max.x - min.x;