| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.authlib.minecraft.BanDetails;
|
| 4 | import com.mojang.blaze3d.Blaze3D;
|
| 5 | import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
|
| 6 | import java.net.URI;
|
| 7 | import java.time.Duration;
|
| 8 | import java.time.Instant;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.ChatFormatting;
|
| 12 | import net.minecraft.client.multiplayer.chat.report.BanReason;
|
| 13 | import net.minecraft.network.chat.CommonComponents;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.network.chat.ComponentUtils;
|
| 16 | import net.minecraft.network.chat.Style;
|
| 17 | import net.minecraft.util.CommonLinks;
|
| 18 | import org.apache.commons.lang3.StringUtils;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class BanNoticeScreens {
|
| 22 | private static final Component TEMPORARY_BAN_TITLE = Component.translatable("gui.banned.title.temporary").withStyle(ChatFormatting.BOLD);
|
| 23 | private static final Component PERMANENT_BAN_TITLE = Component.translatable("gui.banned.title.permanent").withStyle(ChatFormatting.BOLD);
|
| 24 | public static final Component NAME_BAN_TITLE = Component.translatable("gui.banned.name.title").withStyle(ChatFormatting.BOLD);
|
| 25 | private static final Component SKIN_BAN_TITLE = Component.translatable("gui.banned.skin.title").withStyle(ChatFormatting.BOLD);
|
| 26 | private static final Component SKIN_BAN_DESCRIPTION = Component.translatable(
|
| 27 | "gui.banned.skin.description", Component.translationArg(CommonLinks.SUSPENSION_HELP)
|
| 28 | );
|
| 29 |
|
| 30 | public static ConfirmLinkScreen create(final BooleanConsumer callback, final BanDetails multiplayerBanned) {
|
| 31 | return new ConfirmLinkScreen(
|
| 32 | callback, getBannedTitle(multiplayerBanned), getBannedScreenText(multiplayerBanned), CommonLinks.SUSPENSION_HELP, CommonComponents.GUI_ACKNOWLEDGE, true
|
| 33 | );
|
| 34 | }
|
| 35 |
|
| 36 | public static ConfirmLinkScreen createSkinBan(final Runnable onClose) {
|
| 37 | URI uri = CommonLinks.SUSPENSION_HELP;
|
| 38 | return new ConfirmLinkScreen(result -> {
|
| 39 | if (result) {
|
| 40 | Blaze3D.openUri(uri);
|
| 41 | }
|
| 42 |
|
| 43 | onClose.run();
|
| 44 | }, SKIN_BAN_TITLE, SKIN_BAN_DESCRIPTION, uri, CommonComponents.GUI_ACKNOWLEDGE, true);
|
| 45 | }
|
| 46 |
|
| 47 | public static ConfirmLinkScreen createNameBan(final String name, final Runnable onClose) {
|
| 48 | URI uri = CommonLinks.SUSPENSION_HELP;
|
| 49 | return new ConfirmLinkScreen(
|
| 50 | result -> {
|
| 51 | if (result) {
|
| 52 | Blaze3D.openUri(uri);
|
| 53 | }
|
| 54 |
|
| 55 | onClose.run();
|
| 56 | },
|
| 57 | NAME_BAN_TITLE,
|
| 58 | Component.translatable(
|
| 59 | "gui.banned.name.description", Component.literal(name).withStyle(ChatFormatting.YELLOW), Component.translationArg(CommonLinks.SUSPENSION_HELP)
|
| 60 | ),
|
| 61 | uri,
|
| 62 | CommonComponents.GUI_ACKNOWLEDGE,
|
| 63 | true
|
| 64 | );
|
| 65 | }
|
| 66 |
|
| 67 | private static Component getBannedTitle(final BanDetails multiplayerBanned) {
|
| 68 | return isTemporaryBan(multiplayerBanned) ? TEMPORARY_BAN_TITLE : PERMANENT_BAN_TITLE;
|
| 69 | }
|
| 70 |
|
| 71 | private static Component getBannedScreenText(final BanDetails multiplayerBanned) {
|
| 72 | return Component.translatable(
|
| 73 | "gui.banned.description", getBanReasonText(multiplayerBanned), getBanStatusText(multiplayerBanned), Component.translationArg(CommonLinks.SUSPENSION_HELP)
|
| 74 | );
|
| 75 | }
|
| 76 |
|
| 77 | private static Component getBanReasonText(final BanDetails multiplayerBanned) {
|
| 78 | String reasonString = multiplayerBanned.reason();
|
| 79 | String reasonMessage = multiplayerBanned.reasonMessage();
|
| 80 | if (StringUtils.isNumeric(reasonString)) {
|
| 81 | int reasonId = Integer.parseInt(reasonString);
|
| 82 | BanReason reason = BanReason.byId(reasonId);
|
| 83 | Component reasonText;
|
| 84 | if (reason != null) {
|
| 85 | reasonText = ComponentUtils.mergeStyles(reason.title(), Style.EMPTY.withBold(true));
|
| 86 | } else if (reasonMessage != null) {
|
| 87 | reasonText = Component.translatable("gui.banned.description.reason_id_message", reasonId, reasonMessage).withStyle(ChatFormatting.BOLD);
|
| 88 | } else {
|
| 89 | reasonText = Component.translatable("gui.banned.description.reason_id", reasonId).withStyle(ChatFormatting.BOLD);
|
| 90 | }
|
| 91 |
|
| 92 | return Component.translatable("gui.banned.description.reason", reasonText);
|
| 93 | } else {
|
| 94 | return Component.translatable("gui.banned.description.unknownreason");
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | private static Component getBanStatusText(final BanDetails multiplayerBanned) {
|
| 99 | if (isTemporaryBan(multiplayerBanned)) {
|
| 100 | Component banDurationText = getBanDurationText(multiplayerBanned);
|
| 101 | return Component.translatable(
|
| 102 | "gui.banned.description.temporary", Component.translatable("gui.banned.description.temporary.duration", banDurationText).withStyle(ChatFormatting.BOLD)
|
| 103 | );
|
| 104 | } else {
|
| 105 | return Component.translatable("gui.banned.description.permanent").withStyle(ChatFormatting.BOLD);
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | private static Component getBanDurationText(final BanDetails multiplayerBanned) {
|
| 110 | Duration banDuration = Duration.between(Instant.now(), multiplayerBanned.expires());
|
| 111 | long durationHours = banDuration.toHours();
|
| 112 | if (durationHours > 72L) {
|
| 113 | return CommonComponents.days(banDuration.toDays());
|
| 114 | } else {
|
| 115 | return durationHours < 1L ? CommonComponents.minutes(banDuration.toMinutes()) : CommonComponents.hours(banDuration.toHours());
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | private static boolean isTemporaryBan(final BanDetails multiplayerBanned) {
|
| 120 | return multiplayerBanned.expires() != null;
|
| 121 | }
|
| 122 | }
|
| 123 |
|