|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2026 Wurst-Imperium and contributors. |
| 3 | + * |
| 4 | + * This source code is subject to the terms of the GNU General Public |
| 5 | + * License, version 3. If a copy of the GPL was not distributed with this |
| 6 | + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt |
| 7 | + */ |
| 8 | +package net.wurstclient.clickgui.screens; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | +import java.util.LinkedHashSet; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.Set; |
| 14 | + |
| 15 | +import org.lwjgl.glfw.GLFW; |
| 16 | + |
| 17 | +import net.minecraft.client.Minecraft; |
| 18 | +import net.minecraft.client.gui.GuiGraphicsExtractor; |
| 19 | +import net.minecraft.client.gui.components.Button; |
| 20 | +import net.minecraft.client.gui.components.Renderable; |
| 21 | +import net.minecraft.client.gui.screens.Screen; |
| 22 | +import net.minecraft.client.input.KeyEvent; |
| 23 | +import net.minecraft.network.chat.Component; |
| 24 | +import net.minecraft.util.CommonColors; |
| 25 | +import net.minecraft.client.multiplayer.PlayerInfo; |
| 26 | +import net.wurstclient.clickgui.widgets.MultiSelectEntryListWidget; |
| 27 | +import net.wurstclient.settings.PlayerMuteListSetting; |
| 28 | + |
| 29 | +public final class EditPlayerMuteScreen extends Screen |
| 30 | +{ |
| 31 | + private final Screen previous; |
| 32 | + private final PlayerMuteListSetting setting; |
| 33 | + private final Set<String> pendingMutedIds = new LinkedHashSet<>(); |
| 34 | + private PlayerList playerList; |
| 35 | + private Button toggleButton; |
| 36 | + |
| 37 | + public EditPlayerMuteScreen(Screen previous, PlayerMuteListSetting setting) |
| 38 | + { |
| 39 | + super(Component.literal("")); |
| 40 | + this.previous = previous; |
| 41 | + this.setting = setting; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void init() |
| 46 | + { |
| 47 | + List<PlayerInfo> players = setting.getHack().getOnlinePlayers(); |
| 48 | + pendingMutedIds.clear(); |
| 49 | + setting.getHack().getMutedOnlineIds() |
| 50 | + .forEach(id -> pendingMutedIds.add(id.toString())); |
| 51 | + playerList = new PlayerList(minecraft, this, players); |
| 52 | + addWidget(playerList); |
| 53 | + playerList.setSelectionListener(() -> { |
| 54 | + if(toggleButton != null) |
| 55 | + toggleButton.setMessage(Component.literal(toggleButtonText())); |
| 56 | + }); |
| 57 | + if(!players.isEmpty()) |
| 58 | + playerList.setSelection( |
| 59 | + List.of(players.get(0).getProfile().id().toString()), 0); |
| 60 | + |
| 61 | + int x = width / 2 - 155; |
| 62 | + addRenderableWidget(toggleButton = Button |
| 63 | + .builder(Component.literal(toggleButtonText()), |
| 64 | + button -> toggleSelected()) |
| 65 | + .bounds(x, height - 28, 100, 20).build()); |
| 66 | + addRenderableWidget( |
| 67 | + Button.builder(Component.literal("Apply"), button -> apply()) |
| 68 | + .bounds(x + 105, height - 28, 65, 20).build()); |
| 69 | + addRenderableWidget( |
| 70 | + Button.builder(Component.literal("Done"), button -> { |
| 71 | + apply(); |
| 72 | + minecraft.gui.setScreen(previous); |
| 73 | + }).bounds(x + 175, height - 28, 65, 20).build()); |
| 74 | + addRenderableWidget(Button |
| 75 | + .builder(Component.literal("Cancel"), |
| 76 | + button -> minecraft.gui.setScreen(previous)) |
| 77 | + .bounds(x + 245, height - 28, 65, 20).build()); |
| 78 | + } |
| 79 | + |
| 80 | + private void apply() |
| 81 | + { |
| 82 | + setting.getHack() |
| 83 | + .applyOnlineMuteSelection(List.copyOf(pendingMutedIds)); |
| 84 | + } |
| 85 | + |
| 86 | + private void toggleSelected() |
| 87 | + { |
| 88 | + Entry selected = playerList.getSelected(); |
| 89 | + if(selected == null) |
| 90 | + return; |
| 91 | + |
| 92 | + String id = selected.selectionKey(); |
| 93 | + if(!pendingMutedIds.add(id)) |
| 94 | + pendingMutedIds.remove(id); |
| 95 | + toggleButton.setMessage(Component.literal(toggleButtonText())); |
| 96 | + } |
| 97 | + |
| 98 | + private String toggleButtonText() |
| 99 | + { |
| 100 | + Entry selected = playerList == null ? null : playerList.getSelected(); |
| 101 | + return selected != null |
| 102 | + && pendingMutedIds.contains(selected.selectionKey()) |
| 103 | + ? "Unmute player" : "Mute player"; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void extractRenderState(GuiGraphicsExtractor context, int mouseX, |
| 108 | + int mouseY, float partialTicks) |
| 109 | + { |
| 110 | + playerList.extractRenderState(context, mouseX, mouseY, partialTicks); |
| 111 | + context.centeredText(minecraft.font, |
| 112 | + "Select a player, then Mute/Unmute and Apply", width / 2, 12, |
| 113 | + CommonColors.WHITE); |
| 114 | + for(Renderable drawable : renderables) |
| 115 | + drawable.extractRenderState(context, mouseX, mouseY, partialTicks); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public boolean keyPressed(KeyEvent context) |
| 120 | + { |
| 121 | + if(context.key() == GLFW.GLFW_KEY_ESCAPE) |
| 122 | + { |
| 123 | + minecraft.gui.setScreen(previous); |
| 124 | + return true; |
| 125 | + } |
| 126 | + return super.keyPressed(context); |
| 127 | + } |
| 128 | + |
| 129 | + private final class Entry |
| 130 | + extends MultiSelectEntryListWidget.Entry<EditPlayerMuteScreen.Entry> |
| 131 | + { |
| 132 | + private final PlayerInfo info; |
| 133 | + |
| 134 | + private Entry(PlayerList parent, PlayerInfo info) |
| 135 | + { |
| 136 | + super(parent); |
| 137 | + this.info = Objects.requireNonNull(info); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public Component getNarration() |
| 142 | + { |
| 143 | + return Component.translatable("narrator.select", |
| 144 | + info.getProfile().name()); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void extractContent(GuiGraphicsExtractor context, int mouseX, |
| 149 | + int mouseY, boolean hovered, float tickDelta) |
| 150 | + { |
| 151 | + int x = getContentX(); |
| 152 | + int y = getContentY(); |
| 153 | + boolean muted = playerList.isMuted(this); |
| 154 | + context.text(minecraft.font, muted ? "[x]" : "[ ]", x + 4, y + 4, |
| 155 | + muted ? CommonColors.GREEN : CommonColors.LIGHT_GRAY, false); |
| 156 | + context.text(minecraft.font, info.getProfile().name(), x + 28, |
| 157 | + y + 4, CommonColors.WHITE, false); |
| 158 | + context.text(minecraft.font, muted ? "Muted" : "Not muted", x + 28, |
| 159 | + y + 16, CommonColors.LIGHT_GRAY, false); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String selectionKey() |
| 164 | + { |
| 165 | + return info.getProfile().id().toString(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private final class PlayerList |
| 170 | + extends MultiSelectEntryListWidget<EditPlayerMuteScreen.Entry> |
| 171 | + { |
| 172 | + private PlayerList(Minecraft minecraft, EditPlayerMuteScreen screen, |
| 173 | + List<PlayerInfo> players) |
| 174 | + { |
| 175 | + super(minecraft, screen.width, screen.height - 72, 30, 28); |
| 176 | + for(PlayerInfo info : players) |
| 177 | + addEntry(EditPlayerMuteScreen.this.new Entry(this, info)); |
| 178 | + } |
| 179 | + |
| 180 | + private boolean isMuted(Entry entry) |
| 181 | + { |
| 182 | + return pendingMutedIds.contains(entry.selectionKey()); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + protected String getSelectionKey(EditPlayerMuteScreen.Entry entry) |
| 187 | + { |
| 188 | + return entry.selectionKey(); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments