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

fix(furniture): 修复家具互动逻辑

This commit is contained in:
jhqwqmc
2025-04-01 08:39:17 +08:00
parent 28f6744189
commit 8c0210d343
3 changed files with 23 additions and 13 deletions

View File

@@ -360,6 +360,10 @@ public class BukkitFurnitureManager implements FurnitureManager {
try {
int collisionEntityId = (int) Reflections.method$Entity$getId.invoke(collisionEntity);
this.furnitureByCollisionEntitiesId.put(collisionEntityId, loadedFurniture);
this.furnitureByEntityId.put(collisionEntityId, loadedFurniture);
for (HitBox hitBox : loadedFurniture.placement().hitBoxes()) {
loadedFurniture.hitBoxes().put(collisionEntityId, hitBox);
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}

View File

@@ -41,6 +41,7 @@ public class LoadedFurniture {
private final Map<Integer, HitBox> hitBoxes;
private final boolean minimized;
private final boolean hasExternalModel;
private final CustomFurniture.Placement placement;
// seats
private final Set<Vector3f> occupiedSeats = Collections.synchronizedSet(new HashSet<>());
private final Vector<Entity> seats = new Vector<>();
@@ -64,6 +65,7 @@ public class LoadedFurniture {
mainEntityIds.add(this.baseEntityId);
CustomFurniture.Placement placement = furniture.getPlacement(anchorType);
this.placement = placement;
// bind external furniture
Optional<ExternalModel> optionalExternal = placement.externalModel();
if (optionalExternal.isPresent()) {
@@ -263,6 +265,14 @@ public class LoadedFurniture {
return hasExternalModel;
}
public CustomFurniture.Placement placement() {
return this.placement;
}
public Map<Integer, HitBox> hitBoxes() {
return this.hitBoxes;
}
public void spawnSeatEntityForPlayer(org.bukkit.entity.Player player, Seat seat) {
Location location = this.calculateSeatLocation(seat);
Entity seatEntity = seat.limitPlayerRotation() ?

View File

@@ -690,26 +690,22 @@ public class PacketConsumers {
Object actionType = Reflections.method$ServerboundInteractPacket$Action$getType.invoke(action);
if (actionType == null) return;
LoadedFurniture furniture = BukkitFurnitureManager.instance().getLoadedFurnitureByEntityId(entityId);
if (furniture == null) {
furniture = BukkitFurnitureManager.instance().getLoadedFurnitureByCollisionEntityId(entityId);
player.sendMessage("Interact with " + entityId + " " + actionType + " " + furniture);
if (furniture == null) return;
}
player.sendMessage("Interact with " + entityId + " " + actionType + " " + furniture);
if (furniture == null) return;
Location location = furniture.baseEntity().getLocation();
BukkitServerPlayer serverPlayer = (BukkitServerPlayer) user;
if (serverPlayer.isSpectatorMode() || serverPlayer.isAdventureMode()) return;
LoadedFurniture finalFurniture = furniture;
BukkitCraftEngine.instance().scheduler().sync().run(() -> {
if (actionType == Reflections.instance$ServerboundInteractPacket$ActionType$ATTACK) {
if (finalFurniture.isValid()) {
if (furniture.isValid()) {
if (!BukkitCraftEngine.instance().antiGrief().canBreak(player, location)) {
return;
}
FurnitureBreakEvent breakEvent = new FurnitureBreakEvent(serverPlayer.platformPlayer(), finalFurniture);
FurnitureBreakEvent breakEvent = new FurnitureBreakEvent(serverPlayer.platformPlayer(), furniture);
if (EventUtils.fireAndCheckCancel(breakEvent)) {
return;
}
CraftEngineFurniture.remove(finalFurniture, serverPlayer, !serverPlayer.isCreativeMode(), true);
CraftEngineFurniture.remove(furniture, serverPlayer, !serverPlayer.isCreativeMode(), true);
}
} else if (actionType == Reflections.instance$ServerboundInteractPacket$ActionType$INTERACT_AT) {
InteractionHand hand;
@@ -725,15 +721,15 @@ public class PacketConsumers {
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Failed to get interaction hand from interact packet", e);
}
FurnitureInteractEvent interactEvent = new FurnitureInteractEvent(serverPlayer.platformPlayer(), finalFurniture, hand, interactionPoint);
FurnitureInteractEvent interactEvent = new FurnitureInteractEvent(serverPlayer.platformPlayer(), furniture, hand, interactionPoint);
if (EventUtils.fireAndCheckCancel(interactEvent)) {
return;
}
if (player.isSneaking())
return;
finalFurniture.findFirstAvailableSeat(entityId).ifPresent(seatPos -> {
if (finalFurniture.tryOccupySeat(seatPos)) {
finalFurniture.spawnSeatEntityForPlayer(Objects.requireNonNull(player.getPlayer()), seatPos);
furniture.findFirstAvailableSeat(entityId).ifPresent(seatPos -> {
if (furniture.tryOccupySeat(seatPos)) {
furniture.spawnSeatEntityForPlayer(Objects.requireNonNull(player.getPlayer()), seatPos);
}
});
}