|
| 1 | +package io.atomic.cloud.paper.notify.handler; |
| 2 | + |
| 3 | +import io.atomic.cloud.common.connection.call.CallHandle; |
| 4 | +import io.atomic.cloud.common.connection.client.ClientConnection; |
| 5 | +import io.atomic.cloud.grpc.common.Notify; |
| 6 | +import io.atomic.cloud.paper.CloudPlugin; |
| 7 | +import io.atomic.cloud.paper.notify.NotifyPlugin; |
| 8 | +import io.atomic.cloud.paper.notify.permission.Permissions; |
| 9 | +import io.grpc.stub.StreamObserver; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; |
| 12 | +import org.bukkit.Bukkit; |
| 13 | + |
| 14 | +@RequiredArgsConstructor |
| 15 | +public class ReadyHandler implements StreamObserver<Notify.ReadyEvent> { |
| 16 | + |
| 17 | + private final ClientConnection connection; |
| 18 | + private CallHandle<?, ?> handle; |
| 19 | + |
| 20 | + public void enable() { |
| 21 | + NotifyPlugin.LOGGER.info("Enabling ready notifications..."); |
| 22 | + this.handle = this.connection.subscribeToReadyEvents(this); |
| 23 | + } |
| 24 | + |
| 25 | + public void cleanup() { |
| 26 | + this.handle.cancel("Closed by cleanup"); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void onNext(Notify.ReadyEvent event) { |
| 31 | + try { |
| 32 | + Bukkit.getOnlinePlayers().stream() |
| 33 | + .filter(Permissions.READY_NOTIFY::check) |
| 34 | + .forEach(player -> { |
| 35 | + if (event.getReady()) { |
| 36 | + NotifyPlugin.INSTANCE |
| 37 | + .messages() |
| 38 | + .serverReady() |
| 39 | + .send(player, Placeholder.unparsed("name", event.getName())); |
| 40 | + } else { |
| 41 | + NotifyPlugin.INSTANCE |
| 42 | + .messages() |
| 43 | + .serverNotReady() |
| 44 | + .send(player, Placeholder.unparsed("name", event.getName())); |
| 45 | + } |
| 46 | + }); |
| 47 | + } catch (Throwable throwable) { |
| 48 | + NotifyPlugin.LOGGER.info("Failed to process ready event for server {}:", event.getName()); |
| 49 | + NotifyPlugin.LOGGER.error("-> ", throwable); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void onError(Throwable throwable) { |
| 55 | + CloudPlugin.LOGGER.error("Failed to handle ready event", throwable); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void onCompleted() {} |
| 60 | +} |
0 commit comments