| 1 | package net.minecraft.client.gui.screens.social;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import java.time.Duration;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.List;
|
| 7 | import java.util.UUID;
|
| 8 | import java.util.function.Supplier;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.ChatFormatting;
|
| 12 | import net.minecraft.SharedConstants;
|
| 13 | import net.minecraft.client.Minecraft;
|
| 14 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 15 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 16 | import net.minecraft.client.gui.components.Button;
|
| 17 | import net.minecraft.client.gui.components.ContainerObjectSelectionList;
|
| 18 | import net.minecraft.client.gui.components.ImageButton;
|
| 19 | import net.minecraft.client.gui.components.PlayerFaceExtractor;
|
| 20 | import net.minecraft.client.gui.components.Tooltip;
|
| 21 | import net.minecraft.client.gui.components.WidgetSprites;
|
| 22 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 23 | import net.minecraft.client.gui.narration.NarratableEntry;
|
| 24 | import net.minecraft.client.gui.screens.reporting.ReportPlayerScreen;
|
| 25 | import net.minecraft.client.multiplayer.chat.report.ReportingContext;
|
| 26 | import net.minecraft.client.renderer.RenderPipelines;
|
| 27 | import net.minecraft.network.chat.CommonComponents;
|
| 28 | import net.minecraft.network.chat.Component;
|
| 29 | import net.minecraft.network.chat.MutableComponent;
|
| 30 | import net.minecraft.resources.Identifier;
|
| 31 | import net.minecraft.util.ARGB;
|
| 32 | import net.minecraft.world.entity.player.PlayerSkin;
|
| 33 | import org.jspecify.annotations.Nullable;
|
| 34 |
|
| 35 | @Environment(EnvType.CLIENT)
|
| 36 | public class PlayerEntry extends ContainerObjectSelectionList.Entry<PlayerEntry> {
|
| 37 | private static final Identifier DRAFT_REPORT_SPRITE = Identifier.withDefaultNamespace("icon/draft_report");
|
| 38 | private static final Duration TOOLTIP_DELAY = Duration.ofMillis(500L);
|
| 39 | private static final WidgetSprites REPORT_BUTTON_SPRITES = new WidgetSprites(
|
| 40 | Identifier.withDefaultNamespace("social_interactions/report_button"),
|
| 41 | Identifier.withDefaultNamespace("social_interactions/report_button_disabled"),
|
| 42 | Identifier.withDefaultNamespace("social_interactions/report_button_highlighted")
|
| 43 | );
|
| 44 | private static final WidgetSprites MUTE_BUTTON_SPRITES = new WidgetSprites(
|
| 45 | Identifier.withDefaultNamespace("social_interactions/mute_button"), Identifier.withDefaultNamespace("social_interactions/mute_button_highlighted")
|
| 46 | );
|
| 47 | private static final WidgetSprites UNMUTE_BUTTON_SPRITES = new WidgetSprites(
|
| 48 | Identifier.withDefaultNamespace("social_interactions/unmute_button"), Identifier.withDefaultNamespace("social_interactions/unmute_button_highlighted")
|
| 49 | );
|
| 50 | private final Minecraft minecraft;
|
| 51 | private final List<AbstractWidget> children;
|
| 52 | private final UUID id;
|
| 53 | private final String playerName;
|
| 54 | private final Supplier<PlayerSkin> skinGetter;
|
| 55 | private boolean isRemoved;
|
| 56 | private boolean hasRecentMessages;
|
| 57 | private final boolean reportingEnabled;
|
| 58 | private boolean hasDraftReport;
|
| 59 | private final boolean chatReportable;
|
| 60 | private @Nullable Button hideButton;
|
| 61 | private @Nullable Button showButton;
|
| 62 | private @Nullable Button reportButton;
|
| 63 | private float tooltipHoverTime;
|
| 64 | private static final Component HIDDEN = Component.translatable("gui.socialInteractions.status_hidden").withStyle(ChatFormatting.ITALIC);
|
| 65 | private static final Component BLOCKED = Component.translatable("gui.socialInteractions.status_blocked").withStyle(ChatFormatting.ITALIC);
|
| 66 | private static final Component OFFLINE = Component.translatable("gui.socialInteractions.status_offline").withStyle(ChatFormatting.ITALIC);
|
| 67 | private static final Component HIDDEN_OFFLINE = Component.translatable("gui.socialInteractions.status_hidden_offline").withStyle(ChatFormatting.ITALIC);
|
| 68 | private static final Component BLOCKED_OFFLINE = Component.translatable("gui.socialInteractions.status_blocked_offline").withStyle(ChatFormatting.ITALIC);
|
| 69 | private static final Component REPORT_DISABLED_TOOLTIP = Component.translatable("gui.socialInteractions.tooltip.report.disabled");
|
| 70 | private static final Component HIDE_TEXT_TOOLTIP = Component.translatable("gui.socialInteractions.tooltip.hide");
|
| 71 | private static final Component SHOW_TEXT_TOOLTIP = Component.translatable("gui.socialInteractions.tooltip.show");
|
| 72 | private static final Component REPORT_PLAYER_TOOLTIP = Component.translatable("gui.socialInteractions.tooltip.report");
|
| 73 | private static final int SKIN_SIZE = 24;
|
| 74 | private static final int PADDING = 4;
|
| 75 | public static final int SKIN_SHADE = ARGB.color(190, 0, 0, 0);
|
| 76 | private static final int CHAT_TOGGLE_ICON_SIZE = 20;
|
| 77 | public static final int BG_FILL = ARGB.color(255, 74, 74, 74);
|
| 78 | public static final int BG_FILL_REMOVED = ARGB.color(255, 48, 48, 48);
|
| 79 | public static final int PLAYERNAME_COLOR = ARGB.color(255, 255, 255, 255);
|
| 80 | public static final int PLAYER_STATUS_COLOR = ARGB.color(140, 255, 255, 255);
|
| 81 |
|
| 82 | public PlayerEntry(
|
| 83 | final Minecraft minecraft,
|
| 84 | final SocialInteractionsScreen socialInteractionsScreen,
|
| 85 | final UUID id,
|
| 86 | final String playerName,
|
| 87 | final Supplier<PlayerSkin> skinGetter,
|
| 88 | final boolean chatReportable
|
| 89 | ) {
|
| 90 | this.minecraft = minecraft;
|
| 91 | this.id = id;
|
| 92 | this.playerName = playerName;
|
| 93 | this.skinGetter = skinGetter;
|
| 94 | ReportingContext reportingContext = minecraft.getReportingContext();
|
| 95 | this.reportingEnabled = reportingContext.sender().isEnabled();
|
| 96 | this.chatReportable = chatReportable;
|
| 97 | this.refreshHasDraftReport(reportingContext);
|
| 98 | Component hideNarration = Component.translatable("gui.socialInteractions.narration.hide", playerName);
|
| 99 | Component showNarration = Component.translatable("gui.socialInteractions.narration.show", playerName);
|
| 100 | PlayerSocialManager socialManager = minecraft.getPlayerSocialManager();
|
| 101 | boolean chatDisabledOrBlocked = !minecraft.player.chatAbilities().canReceivePlayerMessages() || socialManager.isBlocked(id);
|
| 102 | boolean notLocalPlayer = !minecraft.player.getUUID().equals(id);
|
| 103 | if (!SharedConstants.DEBUG_SOCIAL_INTERACTIONS && !notLocalPlayer) {
|
| 104 | this.children = ImmutableList.of();
|
| 105 | } else {
|
| 106 | this.reportButton = new ImageButton(
|
| 107 | 0,
|
| 108 | 0,
|
| 109 | 20,
|
| 110 | 20,
|
| 111 | REPORT_BUTTON_SPRITES,
|
| 112 | button -> reportingContext.draftReportHandled(
|
| 113 | minecraft,
|
| 114 | socialInteractionsScreen,
|
| 115 | () -> minecraft.gui.setScreen(new ReportPlayerScreen(socialInteractionsScreen, reportingContext, this, chatDisabledOrBlocked)),
|
| 116 | false
|
| 117 | ),
|
| 118 | Component.translatable("gui.socialInteractions.report")
|
| 119 | ) {
|
| 120 | @Override
|
| 121 | protected MutableComponent createNarrationMessage() {
|
| 122 | return PlayerEntry.this.getEntryNarationMessage(super.createNarrationMessage());
|
| 123 | }
|
| 124 | };
|
| 125 | this.reportButton.active = this.reportingEnabled;
|
| 126 | this.reportButton.setTooltip(this.createReportButtonTooltip());
|
| 127 | this.reportButton.setTooltipDelay(TOOLTIP_DELAY);
|
| 128 | this.hideButton = new ImageButton(0, 0, 20, 20, MUTE_BUTTON_SPRITES, button -> {
|
| 129 | socialManager.hidePlayer(id);
|
| 130 | this.onHiddenOrShown(true, Component.translatable("gui.socialInteractions.hidden_in_chat", playerName));
|
| 131 | }, Component.translatable("gui.socialInteractions.hide")) {
|
| 132 | @Override
|
| 133 | protected MutableComponent createNarrationMessage() {
|
| 134 | return PlayerEntry.this.getEntryNarationMessage(super.createNarrationMessage());
|
| 135 | }
|
| 136 | };
|
| 137 | this.hideButton.setTooltip(Tooltip.create(HIDE_TEXT_TOOLTIP, hideNarration));
|
| 138 | this.hideButton.setTooltipDelay(TOOLTIP_DELAY);
|
| 139 | this.showButton = new ImageButton(0, 0, 20, 20, UNMUTE_BUTTON_SPRITES, button -> {
|
| 140 | socialManager.showPlayer(id);
|
| 141 | this.onHiddenOrShown(false, Component.translatable("gui.socialInteractions.shown_in_chat", playerName));
|
| 142 | }, Component.translatable("gui.socialInteractions.show")) {
|
| 143 | @Override
|
| 144 | protected MutableComponent createNarrationMessage() {
|
| 145 | return PlayerEntry.this.getEntryNarationMessage(super.createNarrationMessage());
|
| 146 | }
|
| 147 | };
|
| 148 | this.showButton.setTooltip(Tooltip.create(SHOW_TEXT_TOOLTIP, showNarration));
|
| 149 | this.showButton.setTooltipDelay(TOOLTIP_DELAY);
|
| 150 | this.children = new ArrayList<>();
|
| 151 | this.children.add(this.hideButton);
|
| 152 | this.children.add(this.reportButton);
|
| 153 | this.updateHideAndShowButton(socialManager.isHidden(this.id));
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | public void refreshHasDraftReport(final ReportingContext reportingContext) {
|
| 158 | this.hasDraftReport = reportingContext.hasDraftReportFor(this.id);
|
| 159 | }
|
| 160 |
|
| 161 | private Tooltip createReportButtonTooltip() {
|
| 162 | return !this.reportingEnabled
|
| 163 | ? Tooltip.create(REPORT_DISABLED_TOOLTIP)
|
| 164 | : Tooltip.create(REPORT_PLAYER_TOOLTIP, Component.translatable("gui.socialInteractions.narration.report", this.playerName));
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 169 | int skinX = this.getContentX() + 4;
|
| 170 | int skinY = this.getContentY() + (this.getContentHeight() - 24) / 2;
|
| 171 | int textStartX = skinX + 24 + 4;
|
| 172 | Component status = this.getStatusComponent();
|
| 173 | int textStartY;
|
| 174 | if (status == CommonComponents.EMPTY) {
|
| 175 | graphics.fill(this.getContentX(), this.getContentY(), this.getContentRight(), this.getContentBottom(), BG_FILL);
|
| 176 | textStartY = this.getContentY() + (this.getContentHeight() - 9) / 2;
|
| 177 | } else {
|
| 178 | graphics.fill(this.getContentX(), this.getContentY(), this.getContentRight(), this.getContentBottom(), BG_FILL_REMOVED);
|
| 179 | textStartY = this.getContentY() + (this.getContentHeight() - (9 + 9)) / 2;
|
| 180 | graphics.text(this.minecraft.font, status, textStartX, textStartY + 12, PLAYER_STATUS_COLOR);
|
| 181 | }
|
| 182 |
|
| 183 | PlayerFaceExtractor.extractRenderState(graphics, this.skinGetter.get(), skinX, skinY, 24);
|
| 184 | graphics.text(this.minecraft.font, this.playerName, textStartX, textStartY, PLAYERNAME_COLOR);
|
| 185 | if (this.isRemoved) {
|
| 186 | graphics.fill(skinX, skinY, skinX + 24, skinY + 24, SKIN_SHADE);
|
| 187 | }
|
| 188 |
|
| 189 | if (this.hideButton != null && this.showButton != null && this.reportButton != null) {
|
| 190 | float lastHoverTime = this.tooltipHoverTime;
|
| 191 | this.hideButton.setX(this.getContentX() + (this.getContentWidth() - this.hideButton.getWidth() - 4) - 20 - 4);
|
| 192 | this.hideButton.setY(this.getContentY() + (this.getContentHeight() - this.hideButton.getHeight()) / 2);
|
| 193 | this.hideButton.extractRenderState(graphics, mouseX, mouseY, a);
|
| 194 | this.showButton.setX(this.getContentX() + (this.getContentWidth() - this.showButton.getWidth() - 4) - 20 - 4);
|
| 195 | this.showButton.setY(this.getContentY() + (this.getContentHeight() - this.showButton.getHeight()) / 2);
|
| 196 | this.showButton.extractRenderState(graphics, mouseX, mouseY, a);
|
| 197 | this.reportButton.setX(this.getContentX() + (this.getContentWidth() - this.showButton.getWidth() - 4));
|
| 198 | this.reportButton.setY(this.getContentY() + (this.getContentHeight() - this.showButton.getHeight()) / 2);
|
| 199 | this.reportButton.extractRenderState(graphics, mouseX, mouseY, a);
|
| 200 | if (lastHoverTime == this.tooltipHoverTime) {
|
| 201 | this.tooltipHoverTime = 0.0F;
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | if (this.hasDraftReport && this.reportButton != null) {
|
| 206 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, DRAFT_REPORT_SPRITE, this.reportButton.getX() + 5, this.reportButton.getY() + 1, 15, 15);
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | public List<? extends GuiEventListener> children() {
|
| 212 | return this.children;
|
| 213 | }
|
| 214 |
|
| 215 | @Override
|
| 216 | public List<? extends NarratableEntry> narratables() {
|
| 217 | return this.children;
|
| 218 | }
|
| 219 |
|
| 220 | public String getPlayerName() {
|
| 221 | return this.playerName;
|
| 222 | }
|
| 223 |
|
| 224 | public UUID getPlayerId() {
|
| 225 | return this.id;
|
| 226 | }
|
| 227 |
|
| 228 | public Supplier<PlayerSkin> getSkinGetter() {
|
| 229 | return this.skinGetter;
|
| 230 | }
|
| 231 |
|
| 232 | public void setRemoved(final boolean isRemoved) {
|
| 233 | this.isRemoved = isRemoved;
|
| 234 | }
|
| 235 |
|
| 236 | public boolean isRemoved() {
|
| 237 | return this.isRemoved;
|
| 238 | }
|
| 239 |
|
| 240 | public void setHasRecentMessages(final boolean hasRecentMessages) {
|
| 241 | this.hasRecentMessages = hasRecentMessages;
|
| 242 | }
|
| 243 |
|
| 244 | public boolean hasRecentMessages() {
|
| 245 | return this.hasRecentMessages;
|
| 246 | }
|
| 247 |
|
| 248 | public boolean isChatReportable() {
|
| 249 | return this.chatReportable;
|
| 250 | }
|
| 251 |
|
| 252 | private void onHiddenOrShown(final boolean isHidden, final Component message) {
|
| 253 | this.updateHideAndShowButton(isHidden);
|
| 254 | this.minecraft.gui.hud.getChat().addClientSystemMessage(message);
|
| 255 | this.minecraft.getNarrator().saySystemNow(message);
|
| 256 | }
|
| 257 |
|
| 258 | private void updateHideAndShowButton(final boolean isHidden) {
|
| 259 | this.showButton.visible = isHidden;
|
| 260 | this.hideButton.visible = !isHidden;
|
| 261 | this.children.set(0, isHidden ? this.showButton : this.hideButton);
|
| 262 | }
|
| 263 |
|
| 264 | private MutableComponent getEntryNarationMessage(final MutableComponent buttonNarrationMessage) {
|
| 265 | Component status = this.getStatusComponent();
|
| 266 | return status == CommonComponents.EMPTY
|
| 267 | ? Component.literal(this.playerName).append(", ").append(buttonNarrationMessage)
|
| 268 | : Component.literal(this.playerName).append(", ").append(status).append(", ").append(buttonNarrationMessage);
|
| 269 | }
|
| 270 |
|
| 271 | private Component getStatusComponent() {
|
| 272 | boolean isHidden = this.minecraft.getPlayerSocialManager().isHidden(this.id);
|
| 273 | boolean isBlocked = this.minecraft.getPlayerSocialManager().isBlocked(this.id);
|
| 274 | if (isBlocked && this.isRemoved) {
|
| 275 | return BLOCKED_OFFLINE;
|
| 276 | } else if (isHidden && this.isRemoved) {
|
| 277 | return HIDDEN_OFFLINE;
|
| 278 | } else if (isBlocked) {
|
| 279 | return BLOCKED;
|
| 280 | } else if (isHidden) {
|
| 281 | return HIDDEN;
|
| 282 | } else {
|
| 283 | return this.isRemoved ? OFFLINE : CommonComponents.EMPTY;
|
| 284 | }
|
| 285 | }
|
| 286 | }
|
| 287 |
|