diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/BukkitBlockBehaviors.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/BukkitBlockBehaviors.java index f13476d45..34435ad14 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/BukkitBlockBehaviors.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/BukkitBlockBehaviors.java @@ -32,7 +32,6 @@ public class BukkitBlockBehaviors extends BlockBehaviors { public static final Key TOGGLEABLE_LAMP_BLOCK = Key.from("craftengine:toggleable_lamp_block"); public static final Key SOFA_BLOCK = Key.from("craftengine:sofa_block"); public static final Key BOUNCING_BLOCK = Key.from("craftengine:bouncing_block"); - public static final Key SEAT_BLOCK = Key.from("craftengine:seat_block"); public static void init() { register(EMPTY, (block, args) -> EmptyBlockBehavior.INSTANCE); @@ -63,6 +62,5 @@ public class BukkitBlockBehaviors extends BlockBehaviors { register(TOGGLEABLE_LAMP_BLOCK, ToggleableLampBlockBehavior.FACTORY); register(SOFA_BLOCK, SofaBlockBehavior.FACTORY); register(BOUNCING_BLOCK, BouncingBlockBehavior.FACTORY); - register(SEAT_BLOCK, SeatBlockBehavior.FACTORY); } } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/SeatBlockBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/SeatBlockBehavior.java deleted file mode 100644 index 7cd3d28a2..000000000 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/SeatBlockBehavior.java +++ /dev/null @@ -1,78 +0,0 @@ -package net.momirealms.craftengine.bukkit.block.behavior; - -import net.momirealms.craftengine.bukkit.block.entity.BukkitBlockEntityTypes; -import net.momirealms.craftengine.bukkit.block.entity.SeatBlockEntity; -import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer; -import net.momirealms.craftengine.core.block.BlockBehavior; -import net.momirealms.craftengine.core.block.CustomBlock; -import net.momirealms.craftengine.core.block.ImmutableBlockState; -import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory; -import net.momirealms.craftengine.core.block.behavior.EntityBlockBehavior; -import net.momirealms.craftengine.core.block.entity.BlockEntity; -import net.momirealms.craftengine.core.block.entity.BlockEntityType; -import net.momirealms.craftengine.core.block.entity.tick.BlockEntityTicker; -import net.momirealms.craftengine.core.entity.player.InteractionResult; -import net.momirealms.craftengine.core.item.context.UseOnContext; -import net.momirealms.craftengine.core.util.ResourceConfigUtils; -import net.momirealms.craftengine.core.world.BlockPos; -import net.momirealms.craftengine.core.world.CEWorld; -import org.joml.Vector3f; - -import java.util.Map; - -public class SeatBlockBehavior extends BukkitBlockBehavior implements EntityBlockBehavior { - public static final Factory FACTORY = new Factory(); - private final Vector3f offset; - private final float yaw; - private final boolean limitPlayerRotation; - - public SeatBlockBehavior(CustomBlock customBlock, Vector3f offset, float yaw, boolean limitPlayerRotation) { - super(customBlock); - this.offset = offset; - this.yaw = yaw; - this.limitPlayerRotation = limitPlayerRotation; - } - - @Override - public InteractionResult useWithoutItem(UseOnContext context, ImmutableBlockState state) { - BukkitServerPlayer player = (BukkitServerPlayer) context.getPlayer(); - if (player == null || player.isSecondaryUseActive()) { - return InteractionResult.PASS; - } - player.swingHand(context.getHand()); - CEWorld world = context.getLevel().storageWorld(); - BlockEntity blockEntity = world.getBlockEntityAtIfLoaded(context.getClickedPos()); - if (!(blockEntity instanceof SeatBlockEntity seatBlockEntity) || !seatBlockEntity.seatEntities().isEmpty()) { - return InteractionResult.PASS; - } - seatBlockEntity.spawnSeatEntityForPlayer(player.platformPlayer(), this.offset, this.yaw, this.limitPlayerRotation); - return InteractionResult.SUCCESS_AND_CANCEL; - } - - @SuppressWarnings("unchecked") - @Override - public BlockEntityType blockEntityType() { - return (BlockEntityType) BukkitBlockEntityTypes.SEAT; - } - - @Override - public BlockEntity createBlockEntity(BlockPos pos, ImmutableBlockState state) { - return new SeatBlockEntity(pos, state); - } - - @Override - public BlockEntityTicker createBlockEntityTicker(CEWorld level, ImmutableBlockState state, BlockEntityType blockEntityType) { - return EntityBlockBehavior.createTickerHelper(SeatBlockEntity::tick); - } - - public static class Factory implements BlockBehaviorFactory { - - @Override - public BlockBehavior create(CustomBlock block, Map arguments) { - Vector3f offset = ResourceConfigUtils.getAsVector3f(arguments.getOrDefault("offset", "0,0,0"), "offset"); - float yaw = ResourceConfigUtils.getAsFloat(arguments.getOrDefault("yaw", 0f), "yaw"); - boolean limitPlayerRotation = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("limit-player-rotation", true), "limit-player-rotation"); - return new SeatBlockBehavior(block, offset, yaw, limitPlayerRotation); - } - } -} diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/entity/BukkitBlockEntityTypes.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/entity/BukkitBlockEntityTypes.java index c739d0bc4..3e778bd32 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/entity/BukkitBlockEntityTypes.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/entity/BukkitBlockEntityTypes.java @@ -6,5 +6,4 @@ import net.momirealms.craftengine.core.block.entity.BlockEntityTypes; public class BukkitBlockEntityTypes extends BlockEntityTypes { public static final BlockEntityType SIMPLE_STORAGE = register(BlockEntityTypeKeys.SIMPLE_STORAGE, SimpleStorageBlockEntity::new); - public static final BlockEntityType SEAT = register(BlockEntityTypeKeys.SEAT, SeatBlockEntity::new); } \ No newline at end of file diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/entity/SeatBlockEntity.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/entity/SeatBlockEntity.java deleted file mode 100644 index e39b6c2ae..000000000 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/entity/SeatBlockEntity.java +++ /dev/null @@ -1,212 +0,0 @@ -package net.momirealms.craftengine.bukkit.block.entity; - -import com.google.common.collect.ImmutableList; -import it.unimi.dsi.fastutil.objects.Reference2ObjectArrayMap; -import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine; -import net.momirealms.craftengine.bukkit.util.EntityUtils; -import net.momirealms.craftengine.bukkit.util.LegacyAttributeUtils; -import net.momirealms.craftengine.core.block.ImmutableBlockState; -import net.momirealms.craftengine.core.block.entity.BlockEntity; -import net.momirealms.craftengine.core.block.properties.Property; -import net.momirealms.craftengine.core.util.HorizontalDirection; -import net.momirealms.craftengine.core.util.QuaternionUtils; -import net.momirealms.craftengine.core.util.VersionHelper; -import net.momirealms.craftengine.core.world.BlockPos; -import net.momirealms.craftengine.core.world.CEWorld; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.attribute.Attribute; -import org.bukkit.entity.*; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.joml.Vector3f; - -import java.util.Map; -import java.util.Objects; - -@SuppressWarnings("DuplicatedCode") -public class SeatBlockEntity extends BlockEntity { - private final Map seatEntities = new Reference2ObjectArrayMap<>(1); - - public SeatBlockEntity(BlockPos pos, ImmutableBlockState blockState) { - super(BukkitBlockEntityTypes.SEAT, pos, blockState); - } - - public Map seatEntities() { - return this.seatEntities; - } - - public static void tick(CEWorld world, BlockPos pos, ImmutableBlockState state, SeatBlockEntity seat) { - int size = seat.seatEntities.size(); - if (size == 0) return; - if (size == 1) { - // 99.9999%的情况下会命中这里 - Map.Entry entry = seat.seatEntities.entrySet().iterator().next(); - Entity entity = entry.getKey(); - Player player = entry.getValue(); - if (VersionHelper.isFolia()) { - entity.getScheduler().run(BukkitCraftEngine.instance().javaPlugin(), t -> { - if (entity.getPassengers().isEmpty()) { - seat.tryLeavingSeat(player, entity); - seat.seatEntities.remove(entity); - } - }, null); - } else { - if (entity.getPassengers().isEmpty()) { - seat.tryLeavingSeat(player, entity); - seat.seatEntities.remove(entity); - } - } - return; - } - for (Map.Entry entry : ImmutableList.copyOf(seat.seatEntities.entrySet())) { - // 几乎不可能命中这里,除非写出bug了 - Entity entity = entry.getKey(); - Player player = entry.getValue(); - if (VersionHelper.isFolia()) { - entity.getScheduler().run(BukkitCraftEngine.instance().javaPlugin(), t -> { - if (entity.getPassengers().isEmpty()) { - seat.tryLeavingSeat(player, entity); - seat.seatEntities.remove(entity); - } - }, null); - } else { - if (entity.getPassengers().isEmpty()) { - seat.tryLeavingSeat(player, entity); - seat.seatEntities.remove(entity); - } - } - } - } - - @Override - public void preRemove() { - if (this.seatEntities.isEmpty()) return; - try { - this.seatEntities.keySet().forEach(Entity::remove); - } finally { - this.seatEntities.clear(); - } - } - - public void spawnSeatEntityForPlayer(@NotNull Player player, @NotNull Vector3f offset, float yaw, boolean limitPlayerRotation) { - if (!this.seatEntities.isEmpty() || !this.isValid()) return; - Location location = calculateSeatLocation(player, this.pos, this.blockState, offset, yaw); - Entity seatEntity = limitPlayerRotation ? - EntityUtils.spawnEntity(player.getWorld(), - VersionHelper.isOrAbove1_20_2() ? location.subtract(0, 0.9875, 0) : location.subtract(0, 0.990625, 0), - EntityType.ARMOR_STAND, - entity -> { - ArmorStand armorStand = (ArmorStand) entity; - if (VersionHelper.isOrAbove1_21_3()) { - Objects.requireNonNull(armorStand.getAttribute(Attribute.MAX_HEALTH)).setBaseValue(0.01); - } else { - LegacyAttributeUtils.setMaxHealth(armorStand); - } - armorStand.setSmall(true); - armorStand.setInvisible(true); - armorStand.setSilent(true); - armorStand.setInvulnerable(true); - armorStand.setArms(false); - armorStand.setCanTick(false); - armorStand.setAI(false); - armorStand.setGravity(false); - armorStand.setPersistent(false); - }) : - EntityUtils.spawnEntity(player.getWorld(), - VersionHelper.isOrAbove1_20_2() ? location : location.subtract(0, 0.25, 0), - EntityType.ITEM_DISPLAY, - entity -> { - ItemDisplay itemDisplay = (ItemDisplay) entity; - itemDisplay.setPersistent(false); - }); - if (!seatEntity.addPassenger(player)) { - seatEntity.remove(); - return; - } - this.seatEntities.put(seatEntity, player); - } - - private Location calculateSeatLocation(Player player, BlockPos pos, ImmutableBlockState state, Vector3f offset, float yaw) { - Location location = new Location(player.getWorld(), pos.x() + 0.5, pos.y() + 0.5, pos.z() + 0.5); - for (Property property : state.getProperties()) { - if (property.name().equals("facing") && property.valueClass() == HorizontalDirection.class) { - switch ((HorizontalDirection) state.get(property)) { - case NORTH -> location.setYaw(0); - case SOUTH -> location.setYaw(180); - case WEST -> location.setYaw(270); - case EAST -> location.setYaw(90); - } - break; - } - if (property.name().equals("facing_clockwise") && property.valueClass() == HorizontalDirection.class) { - switch ((HorizontalDirection) state.get(property)) { - case NORTH -> location.setYaw(90); - case SOUTH -> location.setYaw(270); - case WEST -> location.setYaw(0); - case EAST -> location.setYaw(180); - } - break; - } - } - Vector3f newOffset = QuaternionUtils.toQuaternionf(0, Math.toRadians(180 - location.getYaw()), 0) - .conjugate() - .transform(new Vector3f(offset)); - double newYaw = yaw + location.getYaw(); - if (newYaw < -180) newYaw += 360; - Location newLocation = location.clone(); - newLocation.setYaw((float) newYaw); - newLocation.add(newOffset.x, newOffset.y + 0.6, -newOffset.z); - return newLocation; - } - - private void tryLeavingSeat(@NotNull Player player, @NotNull Entity vehicle) { - vehicle.remove(); - if (player.getVehicle() != null) return; - Location vehicleLocation = vehicle.getLocation(); - Location originalLocation = vehicleLocation.clone(); - originalLocation.setY(this.pos.y()); - Location targetLocation = originalLocation.clone().add(vehicleLocation.getDirection().multiply(1.1)); - if (!isSafeLocation(targetLocation)) { - targetLocation = findSafeLocationNearby(originalLocation); - if (targetLocation == null) return; - } - targetLocation.setYaw(player.getLocation().getYaw()); - targetLocation.setPitch(player.getLocation().getPitch()); - if (VersionHelper.isFolia()) { - player.teleportAsync(targetLocation); - } else { - player.teleport(targetLocation); - } - } - - private boolean isSafeLocation(Location location) { - World world = location.getWorld(); - if (world == null) return false; - int x = location.getBlockX(); - int y = location.getBlockY(); - int z = location.getBlockZ(); - if (!world.getBlockAt(x, y - 1, z).getType().isSolid()) return false; - if (!world.getBlockAt(x, y, z).isPassable()) return false; - return world.getBlockAt(x, y + 1, z).isPassable(); - } - - @Nullable - private Location findSafeLocationNearby(Location center) { - World world = center.getWorld(); - if (world == null) return null; - int centerX = center.getBlockX(); - int centerY = center.getBlockY(); - int centerZ = center.getBlockZ(); - for (int dx = -1; dx <= 1; dx++) { - for (int dz = -1; dz <= 1; dz++) { - if (dx == 0 && dz == 0) continue; - int x = centerX + dx; - int z = centerZ + dz; - Location nearbyLocation = new Location(world, x + 0.5, centerY, z + 0.5); - if (isSafeLocation(nearbyLocation)) return nearbyLocation; - } - } - return null; - } -} diff --git a/common-files/src/main/resources/resources/default/configuration/blocks.yml b/common-files/src/main/resources/resources/default/configuration/blocks.yml index 23fbb2cf8..8de04abc3 100644 --- a/common-files/src/main/resources/resources/default/configuration/blocks.yml +++ b/common-files/src/main/resources/resources/default/configuration/blocks.yml @@ -590,9 +590,6 @@ items#misc: - type: bouncing_block bounce-height: 0.66 sync-player-position: false - - type: seat_block - offset: 0,-0.5,0 - limit-player-rotation: false state: id: 0 state: white_bed[facing=west,occupied=false,part=foot] @@ -640,8 +637,6 @@ items#misc: - type: sofa_block - type: bouncing_block bounce-height: 0.66 - - type: seat_block - offset: 0,-0.45,0 states: properties: facing: