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

完成新家具API部分

This commit is contained in:
XiaoMoMi
2025-12-04 00:28:45 +08:00
parent 1c95eb75bd
commit 1d74e2764f
7 changed files with 169 additions and 21 deletions

View File

@@ -25,7 +25,7 @@ public class CondIsFurniture extends Condition {
@Override
public boolean check(Event event) {
return entities.check(event, entity -> {
BukkitFurniture baseEntity = CraftEngineFurniture.getLoadedFurnitureByBaseEntity(entity);
BukkitFurniture baseEntity = CraftEngineFurniture.getLoadedFurnitureByMetaEntity(entity);
return baseEntity != null;
}, isNegated());
}

View File

@@ -22,7 +22,7 @@ public class EffRemoveFurniture extends Effect {
@Override
protected void execute(Event e) {
for (Entity entity : entities.getArray(e)) {
Furniture bukkitFurniture = CraftEngineFurniture.getLoadedFurnitureByBaseEntity(entity);
Furniture bukkitFurniture = CraftEngineFurniture.getLoadedFurnitureByMetaEntity(entity);
if (bukkitFurniture != null) {
bukkitFurniture.destroy();
}

View File

@@ -16,7 +16,7 @@ public class ExprEntityFurnitureID extends SimplePropertyExpression<Object, Stri
@Override
public @Nullable String convert(Object object) {
if (object instanceof Entity entity) {
return Optional.ofNullable(CraftEngineFurniture.getLoadedFurnitureByBaseEntity(entity))
return Optional.ofNullable(CraftEngineFurniture.getLoadedFurnitureByMetaEntity(entity))
.map(it -> it.id().toString())
.orElse(null);
}

View File

@@ -6,10 +6,12 @@ import net.momirealms.craftengine.bukkit.entity.seat.BukkitSeatManager;
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.plugin.user.BukkitServerPlayer;
import net.momirealms.craftengine.bukkit.world.BukkitWorld;
import net.momirealms.craftengine.core.entity.furniture.AnchorType;
import net.momirealms.craftengine.core.entity.furniture.Furniture;
import net.momirealms.craftengine.core.entity.furniture.FurnitureConfig;
import net.momirealms.craftengine.core.entity.furniture.FurnitureDataAccessor;
import net.momirealms.craftengine.core.entity.player.InteractionHand;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.loot.LootTable;
@@ -19,11 +21,13 @@ import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.world.World;
import net.momirealms.craftengine.core.world.WorldPosition;
import net.momirealms.sparrow.nbt.CompoundTag;
import org.bukkit.FluidCollisionMode;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.util.RayTraceResult;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -59,6 +63,45 @@ public final class CraftEngineFurniture {
return BukkitFurnitureManager.instance().furnitureById(id).orElse(null);
}
/**
* Performs ray tracing to find the furniture entity that the player is currently targeting
*
* @param player The player performing the ray trace
* @param maxDistance Maximum ray trace distance (in blocks)
* @return The furniture being targeted by the player, or null if no furniture is found
*/
@Nullable
public static BukkitFurniture rayTrace(Player player, double maxDistance) {
BukkitServerPlayer serverPlayer = BukkitAdaptors.adapt(player);
Location eyeLocation = serverPlayer.getEyeLocation();
RayTraceResult result = player.getWorld().rayTrace(eyeLocation, eyeLocation.getDirection(), maxDistance, FluidCollisionMode.NEVER, true, 0d, CraftEngineFurniture::isCollisionEntity);
if (result == null)
return null;
Entity hitEntity = result.getHitEntity();
if (hitEntity == null)
return null;
return getLoadedFurnitureByCollider(hitEntity);
}
/**
* Performs ray tracing to find the furniture entity that the player is currently targeting
*
* @param player The player performing the ray trace
* @return The furniture being targeted by the player, or null if no furniture is found
*/
@Nullable
public static BukkitFurniture rayTrace(Player player) {
BukkitServerPlayer serverPlayer = BukkitAdaptors.adapt(player);
Location eyeLocation = serverPlayer.getEyeLocation();
RayTraceResult result = player.getWorld().rayTrace(eyeLocation, eyeLocation.getDirection(), serverPlayer.getCachedInteractionRange(), FluidCollisionMode.NEVER, true, 0d, CraftEngineFurniture::isCollisionEntity);
if (result == null)
return null;
Entity hitEntity = result.getHitEntity();
if (hitEntity == null)
return null;
return getLoadedFurnitureByCollider(hitEntity);
}
/**
* Places furniture at certain location
*
@@ -70,9 +113,7 @@ public final class CraftEngineFurniture {
public static BukkitFurniture place(Location location, Key furnitureId) {
FurnitureConfig furniture = byId(furnitureId);
if (furniture == null) return null;
// fixme API
// return place(location, furnitureId, furniture.getAnyAnchorType());
return null;
return place(location, furniture, furniture.anyVariantName(), false);
}
/**
@@ -86,11 +127,22 @@ public final class CraftEngineFurniture {
@Nullable
@Deprecated(since = "0.0.66", forRemoval = true)
public static BukkitFurniture place(Location location, Key furnitureId, AnchorType anchorType) {
return place(location, furnitureId, anchorType.variantName());
}
/**
* Places furniture at certain location
*
* @param location location
* @param furnitureId furniture to place
* @param variant variant type
* @return the loaded furniture
*/
@Nullable
public static BukkitFurniture place(Location location, Key furnitureId, String variant) {
FurnitureConfig furniture = byId(furnitureId);
if (furniture == null) return null;
// fixme API
// return BukkitFurnitureManager.instance().place(location, furniture, FurnitureDataAccessor.builder().anchorType(anchorType).build(), true);
return null;
return BukkitFurnitureManager.instance().place(location, furniture, FurnitureDataAccessor.ofVariant(variant), true);
}
/**
@@ -104,9 +156,7 @@ public final class CraftEngineFurniture {
@NotNull
@Deprecated(since = "0.0.66", forRemoval = true)
public static BukkitFurniture place(Location location, FurnitureConfig furniture, AnchorType anchorType) {
// fixme API
// return BukkitFurnitureManager.instance().place(location, furniture, FurnitureDataAccessor.builder().anchorType(anchorType).build(), true);
return null;
return place(location, furniture, anchorType.variantName(), true);
}
/**
@@ -123,9 +173,23 @@ public final class CraftEngineFurniture {
public static BukkitFurniture place(Location location, Key furnitureId, AnchorType anchorType, boolean playSound) {
FurnitureConfig furniture = byId(furnitureId);
if (furniture == null) return null;
// fixme API
// return BukkitFurnitureManager.instance().place(location, furniture, FurnitureDataAccessor.builder().anchorType(anchorType).build(), playSound);
return null;
return place(location, furniture, anchorType.variantName(), playSound);
}
/**
* Places furniture at certain location
*
* @param location location
* @param furnitureId furniture to place
* @param variant variant
* @param playSound whether to play place sounds
* @return the loaded furniture
*/
@Nullable
public static BukkitFurniture place(Location location, Key furnitureId, String variant, boolean playSound) {
FurnitureConfig furniture = byId(furnitureId);
if (furniture == null) return null;
return place(location, furniture, variant, playSound);
}
/**
@@ -140,9 +204,49 @@ public final class CraftEngineFurniture {
@NotNull
@Deprecated(since = "0.0.66", forRemoval = true)
public static BukkitFurniture place(Location location, FurnitureConfig furniture, AnchorType anchorType, boolean playSound) {
// fixme API
// return BukkitFurnitureManager.instance().place(location, furniture, FurnitureDataAccessor.builder().anchorType(anchorType).build(), playSound);
return null;
return place(location, furniture, anchorType.variantName(), playSound);
}
/**
* Places furniture at certain location
*
* @param location location
* @param furniture furniture to place
* @param variant variant
* @param playSound whether to play place sounds
* @return the loaded furniture
*/
@NotNull
public static BukkitFurniture place(Location location, FurnitureConfig furniture, String variant, boolean playSound) {
return BukkitFurnitureManager.instance().place(location, furniture, FurnitureDataAccessor.ofVariant(variant), playSound);
}
/**
* Places furniture at certain location
*
* @param location location
* @param furniture furniture to place
* @param data furniture data
* @param playSound whether to play place sounds
* @return the loaded furniture
*/
@NotNull
public static BukkitFurniture place(Location location, FurnitureConfig furniture, CompoundTag data, boolean playSound) {
return BukkitFurnitureManager.instance().place(location, furniture, FurnitureDataAccessor.of(data), playSound);
}
/**
* Places furniture at certain location
*
* @param location location
* @param furniture furniture to place
* @param dataAccessor furniture data accessor
* @param playSound whether to play place sounds
* @return the loaded furniture
*/
@NotNull
public static BukkitFurniture place(Location location, FurnitureConfig furniture, FurnitureDataAccessor dataAccessor, boolean playSound) {
return BukkitFurnitureManager.instance().place(location, furniture, dataAccessor, playSound);
}
/**
@@ -178,18 +282,30 @@ public final class CraftEngineFurniture {
}
/**
* Gets the base furniture by the base entity
* Gets the furniture by the meta entity
*
* @param baseEntity base entity
* @return the loaded furniture
*/
@Nullable
public static BukkitFurniture getLoadedFurnitureByBaseEntity(@NotNull Entity baseEntity) {
public static BukkitFurniture getLoadedFurnitureByMetaEntity(@NotNull Entity baseEntity) {
return BukkitFurnitureManager.instance().loadedFurnitureByMetaEntityId(baseEntity.getEntityId());
}
/**
* Gets the base furniture by the seat entity
* Gets the furniture by the meta entity
*
* @param baseEntity base entity
* @return the loaded furniture
*/
@Nullable
@Deprecated(since = "0.0.66")
public static BukkitFurniture getLoadedFurnitureByBaseEntity(@NotNull Entity baseEntity) {
return getLoadedFurnitureByMetaEntity(baseEntity);
}
/**
* Gets the furniture by the seat entity
*
* @param seat seat entity
* @return the loaded furniture
@@ -204,6 +320,21 @@ public final class CraftEngineFurniture {
return null;
}
/**
* Gets the furniture by the collider entity
*
* @param collider collider entity
* @return the loaded furniture
*/
@Nullable
public static BukkitFurniture getLoadedFurnitureByCollider(@NotNull Entity collider) {
Object nmsEntity = FastNMS.INSTANCE.method$CraftEntity$getHandle(collider);
if (nmsEntity instanceof CollisionEntity collisionEntity) {
return BukkitFurnitureManager.instance().loadedFurnitureByColliderEntityId(collisionEntity.getId());
}
return null;
}
/**
* Removes furniture
*

View File

@@ -1,9 +1,12 @@
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<CommandSender> {
@@ -15,9 +18,14 @@ public class TestCommand extends BukkitCommandFeature<CommandSender> {
@Override
public Command.Builder<? extends CommandSender> assembleCommand(org.incendo.cloud.CommandManager<CommandSender> manager, Command.Builder<CommandSender> 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());
}
});
}

View File

@@ -20,12 +20,15 @@ items:
default:flower_basket_ground:
material: nether_brick
model: minecraft:item/custom/flower_basket_ground
item-name: <!i><l10n:item.flower_basket>
default:flower_basket_wall:
material: nether_brick
model: minecraft:item/custom/flower_basket_wall
item-name: <!i><l10n:item.flower_basket>
default:flower_basket_ceiling:
material: nether_brick
model: minecraft:item/custom/flower_basket_ceiling
item-name: <!i><l10n:item.flower_basket>
furniture:
default:flower_basket:
settings:

View File

@@ -31,6 +31,12 @@ public class FurnitureDataAccessor {
return new FurnitureDataAccessor(data);
}
public static FurnitureDataAccessor ofVariant(String variant) {
FurnitureDataAccessor accessor = new FurnitureDataAccessor(new CompoundTag());
accessor.setVariant(variant);
return accessor;
}
public CompoundTag copyTag() {
return this.data.copy();
}