@EventHandler
public void onInventoryClick(PlayerInteractEvent event) {
if (event.getClickedBlock() == null) {
return;
}
if (event.getAction() != Action.LEFT_CLICK_BLOCK) {
return;
}
// TODO: Проверить, что кликнута табличка.
final Location clickedBlockLocation = event.getClickedBlock()
.getLocation();
// Find all frames nearby to the clicked sign.
final List<ItemFrame> nearbyFrames =
clickedBlockLocation.getWorld().getNearbyEntities(clickedBlockLocation, 2, 2, 2)
.stream()
.filter(entity -> entity instanceof ItemFrame)
.map(entity -> (ItemFrame) entity)
// Filter item frames that not attached to chest.
// TODO: Все фильтры внизу требуют тестирования.
.filter(frame -> frame.getLocation()
.getBlock()
.getRelative(frame.getAttachedFace())
.getType()
.equals(Material.CHEST))
// Filter frames that not under the sign.
.filter(frame -> frame.getLocation()
.getBlockY() == clickedBlockLocation.getBlockY() - 1)
// Filter frames that not directly under the sign.
// TODO: Можно сделать строже.
.filter(frame -> frame.getLocation()
.getBlockX() == clickedBlockLocation.getBlockX()
|| frame.getLocation()
.getBlockZ() == clickedBlockLocation.getBlockZ())
.toList();
if (nearbyFrames.isEmpty()) {
// Frames not found.
return;
}
if (nearbyFrames.size() > 1) {
// TODO: Если эта ситуация достигнута, то фильтры выше недостаточно строги.
// Писать в WARN об этом и выбирать ближайший frame, либо ничего не делать.
return;
}
final ItemFrame frame = nearbyFrames.get(0);
// Если эта часть кода достигнута, то frame под табличкой найден (frame).
}