| 1 | package net.minecraft.server.jsonrpc;
|
| 2 |
|
| 3 | import net.minecraft.core.Holder;
|
| 4 | import net.minecraft.server.jsonrpc.api.PlayerDto;
|
| 5 | import net.minecraft.server.jsonrpc.internalapi.MinecraftApi;
|
| 6 | import net.minecraft.server.jsonrpc.methods.BanlistService;
|
| 7 | import net.minecraft.server.jsonrpc.methods.GameRulesService;
|
| 8 | import net.minecraft.server.jsonrpc.methods.IpBanlistService;
|
| 9 | import net.minecraft.server.jsonrpc.methods.OperatorService;
|
| 10 | import net.minecraft.server.jsonrpc.methods.ServerStateService;
|
| 11 | import net.minecraft.server.level.ServerPlayer;
|
| 12 | import net.minecraft.server.notifications.NotificationService;
|
| 13 | import net.minecraft.server.players.IpBanListEntry;
|
| 14 | import net.minecraft.server.players.NameAndId;
|
| 15 | import net.minecraft.server.players.ServerOpListEntry;
|
| 16 | import net.minecraft.server.players.UserBanListEntry;
|
| 17 | import net.minecraft.world.level.gamerules.GameRule;
|
| 18 |
|
| 19 | public class JsonRpcNotificationService implements NotificationService {
|
| 20 | private final ManagementServer managementServer;
|
| 21 | private final MinecraftApi minecraftApi;
|
| 22 |
|
| 23 | public JsonRpcNotificationService(final MinecraftApi minecraftApi, final ManagementServer managementServer) {
|
| 24 | this.minecraftApi = minecraftApi;
|
| 25 | this.managementServer = managementServer;
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public void playerJoined(final ServerPlayer player) {
|
| 30 | this.broadcastNotification(OutgoingRpcMethods.PLAYER_JOINED, PlayerDto.from(player));
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public void playerLeft(final ServerPlayer player) {
|
| 35 | this.broadcastNotification(OutgoingRpcMethods.PLAYER_LEFT, PlayerDto.from(player));
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public void serverStarted() {
|
| 40 | this.broadcastNotification(OutgoingRpcMethods.SERVER_STARTED);
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public void serverShuttingDown() {
|
| 45 | this.broadcastNotification(OutgoingRpcMethods.SERVER_SHUTTING_DOWN);
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public void serverSaveStarted() {
|
| 50 | this.broadcastNotification(OutgoingRpcMethods.SERVER_SAVE_STARTED);
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public void serverSaveCompleted() {
|
| 55 | this.broadcastNotification(OutgoingRpcMethods.SERVER_SAVE_COMPLETED);
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public void serverActivityOccured() {
|
| 60 | this.broadcastNotification(OutgoingRpcMethods.SERVER_ACTIVITY_OCCURRED);
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public void worldUpgradeStarted() {
|
| 65 | this.broadcastNotification(OutgoingRpcMethods.WORLD_UPGRADE_STARTED);
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public void worldUpgradeProgress(final float progressPercentage) {
|
| 70 | this.broadcastNotification(OutgoingRpcMethods.WORLD_UPGRADE_PROGRESS, progressPercentage);
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public void worldUpgradeFinished() {
|
| 75 | this.broadcastNotification(OutgoingRpcMethods.WORLD_UPGRADE_FINISHED);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public void worldUpgradeFailed(final String reason) {
|
| 80 | this.broadcastNotification(OutgoingRpcMethods.WORLD_UPGRADE_FAILED, reason);
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void playerOped(final ServerOpListEntry operator) {
|
| 85 | this.broadcastNotification(OutgoingRpcMethods.PLAYER_OPED, OperatorService.OperatorDto.from(operator));
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public void playerDeoped(final ServerOpListEntry operator) {
|
| 90 | this.broadcastNotification(OutgoingRpcMethods.PLAYER_DEOPED, OperatorService.OperatorDto.from(operator));
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public void playerAddedToAllowlist(final NameAndId player) {
|
| 95 | this.broadcastNotification(OutgoingRpcMethods.PLAYER_ADDED_TO_ALLOWLIST, PlayerDto.from(player));
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void playerRemovedFromAllowlist(final NameAndId player) {
|
| 100 | this.broadcastNotification(OutgoingRpcMethods.PLAYER_REMOVED_FROM_ALLOWLIST, PlayerDto.from(player));
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public void ipBanned(final IpBanListEntry ban) {
|
| 105 | this.broadcastNotification(OutgoingRpcMethods.IP_BANNED, IpBanlistService.IpBanDto.from(ban));
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public void ipUnbanned(final String ip) {
|
| 110 | this.broadcastNotification(OutgoingRpcMethods.IP_UNBANNED, ip);
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public void playerBanned(final UserBanListEntry ban) {
|
| 115 | this.broadcastNotification(OutgoingRpcMethods.PLAYER_BANNED, BanlistService.UserBanDto.from(ban));
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public void playerUnbanned(final NameAndId player) {
|
| 120 | this.broadcastNotification(OutgoingRpcMethods.PLAYER_UNBANNED, PlayerDto.from(player));
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public <T> void onGameRuleChanged(final GameRule<T> gameRule, final T value) {
|
| 125 | this.broadcastNotification(OutgoingRpcMethods.GAMERULE_CHANGED, GameRulesService.getTypedRule(this.minecraftApi, gameRule, value));
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public void statusHeartbeat() {
|
| 130 | this.broadcastNotification(OutgoingRpcMethods.STATUS_HEARTBEAT, ServerStateService.status(this.minecraftApi));
|
| 131 | }
|
| 132 |
|
| 133 | private void broadcastNotification(final Holder.Reference<? extends OutgoingRpcMethod<Void, ?>> method) {
|
| 134 | this.managementServer.forEachConnection(connection -> connection.sendNotification(method));
|
| 135 | }
|
| 136 |
|
| 137 | private <Params> void broadcastNotification(final Holder.Reference<? extends OutgoingRpcMethod<Params, ?>> method, final Params params) {
|
| 138 | this.managementServer.forEachConnection(connection -> connection.sendNotification(method, params));
|
| 139 | }
|
| 140 | }
|
| 141 |
|