| 1 | package net.minecraft.client.multiplayer.chat.report;
|
| 2 |
|
| 3 | import com.mojang.authlib.exceptions.MinecraftClientException;
|
| 4 | import com.mojang.authlib.exceptions.MinecraftClientHttpException;
|
| 5 | import com.mojang.authlib.minecraft.UserApiService;
|
| 6 | import com.mojang.authlib.minecraft.report.AbuseReport;
|
| 7 | import com.mojang.authlib.minecraft.report.AbuseReportLimits;
|
| 8 | import com.mojang.authlib.services.request.AbuseReportRequest;
|
| 9 | import com.mojang.datafixers.util.Unit;
|
| 10 | import java.util.UUID;
|
| 11 | import java.util.concurrent.CompletableFuture;
|
| 12 | import java.util.concurrent.CompletionException;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.network.chat.ThrowingComponent;
|
| 17 | import net.minecraft.util.Util;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public interface AbuseReportSender {
|
| 21 | static AbuseReportSender create(final ReportEnvironment environment, final UserApiService userApiService) {
|
| 22 | return new AbuseReportSender.Services(environment, userApiService);
|
| 23 | }
|
| 24 |
|
| 25 | CompletableFuture<Unit> send(UUID id, ReportType reportType, AbuseReport report);
|
| 26 |
|
| 27 | boolean isEnabled();
|
| 28 |
|
| 29 | default AbuseReportLimits reportLimits() {
|
| 30 | return AbuseReportLimits.DEFAULTS;
|
| 31 | }
|
| 32 |
|
| 33 | @Environment(EnvType.CLIENT)
|
| 34 | class SendException extends ThrowingComponent {
|
| 35 | public SendException(final Component component, final Throwable cause) {
|
| 36 | super(component, cause);
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | @Environment(EnvType.CLIENT)
|
| 41 | record Services(ReportEnvironment environment, UserApiService userApiService) implements AbuseReportSender {
|
| 42 | private static final Component SERVICE_UNAVAILABLE_TEXT = Component.translatable("gui.abuseReport.send.service_unavailable");
|
| 43 | private static final Component HTTP_ERROR_TEXT = Component.translatable("gui.abuseReport.send.http_error");
|
| 44 | private static final Component JSON_ERROR_TEXT = Component.translatable("gui.abuseReport.send.json_error");
|
| 45 |
|
| 46 | @Override
|
| 47 | public CompletableFuture<Unit> send(final UUID id, final ReportType reportType, final AbuseReport report) {
|
| 48 | return CompletableFuture.supplyAsync(
|
| 49 | () -> {
|
| 50 | AbuseReportRequest request = new AbuseReportRequest(
|
| 51 | 1, id, report, this.environment.clientInfo(), this.environment.thirdPartyServerInfo(), this.environment.realmInfo(), reportType.backendName()
|
| 52 | );
|
| 53 |
|
| 54 | try {
|
| 55 | this.userApiService.reportAbuse(request);
|
| 56 | return Unit.INSTANCE;
|
| 57 | } catch (MinecraftClientHttpException e) {
|
| 58 | Component description = this.getHttpErrorDescription(e);
|
| 59 | throw new CompletionException(new AbuseReportSender.SendException(description, e));
|
| 60 | } catch (MinecraftClientException e) {
|
| 61 | Component description = this.getErrorDescription(e);
|
| 62 | throw new CompletionException(new AbuseReportSender.SendException(description, e));
|
| 63 | }
|
| 64 | },
|
| 65 | Util.ioPool()
|
| 66 | );
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public boolean isEnabled() {
|
| 71 | return this.userApiService.canSendReports();
|
| 72 | }
|
| 73 |
|
| 74 | private Component getHttpErrorDescription(final MinecraftClientHttpException e) {
|
| 75 | return Component.translatable("gui.abuseReport.send.error_message", e.getMessage());
|
| 76 | }
|
| 77 |
|
| 78 | private Component getErrorDescription(final MinecraftClientException e) {
|
| 79 | return switch (e.getType()) {
|
| 80 | case SERVICE_UNAVAILABLE -> SERVICE_UNAVAILABLE_TEXT;
|
| 81 | case HTTP_ERROR -> HTTP_ERROR_TEXT;
|
| 82 | case JSON_ERROR -> JSON_ERROR_TEXT;
|
| 83 | };
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public AbuseReportLimits reportLimits() {
|
| 88 | return this.userApiService.getAbuseReportLimits();
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|