| 1 | package net.minecraft.client.gui.screens.achievement;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.Comparator;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Set;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.Minecraft;
|
| 13 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 14 | import net.minecraft.client.gui.components.AbstractSelectionList;
|
| 15 | import net.minecraft.client.gui.components.Button;
|
| 16 | import net.minecraft.client.gui.components.ContainerObjectSelectionList;
|
| 17 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 18 | import net.minecraft.client.gui.components.Tooltip;
|
| 19 | import net.minecraft.client.gui.components.tabs.GridLayoutTab;
|
| 20 | import net.minecraft.client.gui.components.tabs.LoadingTab;
|
| 21 | import net.minecraft.client.gui.components.tabs.MenuTabBar;
|
| 22 | import net.minecraft.client.gui.components.tabs.TabManager;
|
| 23 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 24 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 25 | import net.minecraft.client.gui.screens.Screen;
|
| 26 | import net.minecraft.client.gui.screens.achievement.StatsScreen.ItemStatisticsList.HeaderEntry;
|
| 27 | import net.minecraft.client.gui.screens.achievement.StatsScreen.ItemStatisticsList.ItemRow;
|
| 28 | import net.minecraft.client.gui.screens.achievement.StatsScreen.ItemStatisticsList.ItemRowComparator;
|
| 29 | import net.minecraft.client.gui.screens.achievement.StatsScreen.MobsStatisticsList.MobRow;
|
| 30 | import net.minecraft.client.gui.screens.worldselection.CreateWorldScreen;
|
| 31 | import net.minecraft.client.input.KeyEvent;
|
| 32 | import net.minecraft.client.renderer.RenderPipelines;
|
| 33 | import net.minecraft.client.resources.language.I18n;
|
| 34 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 35 | import net.minecraft.network.chat.CommonComponents;
|
| 36 | import net.minecraft.network.chat.Component;
|
| 37 | import net.minecraft.network.protocol.game.ServerboundClientCommandPacket;
|
| 38 | import net.minecraft.network.protocol.game.ServerboundClientCommandPacket.Action;
|
| 39 | import net.minecraft.resources.Identifier;
|
| 40 | import net.minecraft.stats.Stat;
|
| 41 | import net.minecraft.stats.StatType;
|
| 42 | import net.minecraft.stats.Stats;
|
| 43 | import net.minecraft.stats.StatsCounter;
|
| 44 | import net.minecraft.world.entity.EntityType;
|
| 45 | import net.minecraft.world.item.Item;
|
| 46 | import net.minecraft.world.item.Items;
|
| 47 | import net.minecraft.world.level.block.Block;
|
| 48 | import org.jspecify.annotations.Nullable;
|
| 49 |
|
| 50 | @Environment(EnvType.CLIENT)
|
| 51 | public class StatsScreen extends Screen {
|
| 52 | private static final Component TITLE = Component.translatable("gui.stats");
|
| 53 | private static final Identifier SLOT_SPRITE = Identifier.withDefaultNamespace("container/slot");
|
| 54 | private static final Identifier HEADER_SPRITE = Identifier.withDefaultNamespace("statistics/header");
|
| 55 | private static final Identifier SORT_UP_SPRITE = Identifier.withDefaultNamespace("statistics/sort_up");
|
| 56 | private static final Identifier SORT_DOWN_SPRITE = Identifier.withDefaultNamespace("statistics/sort_down");
|
| 57 | private static final Component PENDING_TEXT = Component.translatable("multiplayer.downloadingStats");
|
| 58 | private static final Component NO_VALUE_DISPLAY = Component.translatable("stats.none");
|
| 59 | private static final Component GENERAL_BUTTON = Component.translatable("stat.generalButton");
|
| 60 | private static final Component ITEMS_BUTTON = Component.translatable("stat.itemsButton");
|
| 61 | private static final Component MOBS_BUTTON = Component.translatable("stat.mobsButton");
|
| 62 | protected final Screen lastScreen;
|
| 63 | private static final int LIST_WIDTH = 280;
|
| 64 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
|
| 65 | private final TabManager tabManager = new TabManager(x$0 -> this.addRenderableWidget(x$0), x$0 -> this.removeWidget(x$0));
|
| 66 | private @Nullable MenuTabBar tabNavigationBar;
|
| 67 | private final StatsCounter stats;
|
| 68 | private boolean isLoading = true;
|
| 69 |
|
| 70 | public StatsScreen(final Screen lastScreen, final StatsCounter stats) {
|
| 71 | super(TITLE);
|
| 72 | this.lastScreen = lastScreen;
|
| 73 | this.stats = stats;
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | protected void init() {
|
| 78 | Component loadingTitle = PENDING_TEXT;
|
| 79 | this.tabNavigationBar = MenuTabBar.builder(this.tabManager, this.width)
|
| 80 | .addTabs(
|
| 81 | new LoadingTab(this.getFont(), GENERAL_BUTTON, loadingTitle),
|
| 82 | new LoadingTab(this.getFont(), ITEMS_BUTTON, loadingTitle),
|
| 83 | new LoadingTab(this.getFont(), MOBS_BUTTON, loadingTitle)
|
| 84 | )
|
| 85 | .build();
|
| 86 | this.addRenderableWidget(this.tabNavigationBar);
|
| 87 | this.layout.addToFooter(Button.builder(CommonComponents.GUI_DONE, button -> this.onClose()).width(200).build());
|
| 88 | this.tabNavigationBar.setTabActiveState(0, true);
|
| 89 | this.tabNavigationBar.setTabActiveState(1, false);
|
| 90 | this.tabNavigationBar.setTabActiveState(2, false);
|
| 91 | this.layout.visitWidgets(button -> {
|
| 92 | button.setTabOrderGroup(1);
|
| 93 | this.addRenderableWidget(button);
|
| 94 | });
|
| 95 | this.tabNavigationBar.selectTab(0, false);
|
| 96 | this.repositionElements();
|
| 97 | this.minecraft.getConnection().send(new ServerboundClientCommandPacket(Action.REQUEST_STATS));
|
| 98 | }
|
| 99 |
|
| 100 | public void onStatsUpdated() {
|
| 101 | if (this.isLoading) {
|
| 102 | if (this.tabNavigationBar != null) {
|
| 103 | this.removeWidget(this.tabNavigationBar);
|
| 104 | }
|
| 105 |
|
| 106 | this.tabNavigationBar = MenuTabBar.builder(this.tabManager, this.width)
|
| 107 | .addTabs(
|
| 108 | new StatsScreen.StatisticsTab(GENERAL_BUTTON, new StatsScreen.GeneralStatisticsList(this.minecraft)),
|
| 109 | new StatsScreen.StatisticsTab(ITEMS_BUTTON, new StatsScreen.ItemStatisticsList(this.minecraft)),
|
| 110 | new StatsScreen.StatisticsTab(MOBS_BUTTON, new StatsScreen.MobsStatisticsList(this.minecraft))
|
| 111 | )
|
| 112 | .build();
|
| 113 | this.setFocused(this.tabNavigationBar);
|
| 114 | this.addRenderableWidget(this.tabNavigationBar);
|
| 115 | this.setTabActiveStateAndTooltip(1);
|
| 116 | this.setTabActiveStateAndTooltip(2);
|
| 117 | this.tabNavigationBar.selectTab(0, false);
|
| 118 | this.repositionElements();
|
| 119 | this.isLoading = false;
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | private void setTabActiveStateAndTooltip(final int index) {
|
| 124 | if (this.tabNavigationBar != null) {
|
| 125 | boolean active = this.tabNavigationBar.getTabs().get(index) instanceof StatsScreen.StatisticsTab statsTab && !statsTab.list.children().isEmpty();
|
| 126 | this.tabNavigationBar.setTabActiveState(index, active);
|
| 127 | if (active) {
|
| 128 | this.tabNavigationBar.setTabTooltip(index, null);
|
| 129 | } else {
|
| 130 | this.tabNavigationBar.setTabTooltip(index, Tooltip.create(Component.translatable("gui.stats.none_found")));
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | protected void repositionElements() {
|
| 137 | if (this.tabNavigationBar != null) {
|
| 138 | this.tabNavigationBar.arrangeElements(this.width);
|
| 139 | int tabAreaTop = this.tabNavigationBar.getRectangle().bottom();
|
| 140 | ScreenRectangle tabArea = new ScreenRectangle(0, tabAreaTop, this.width, this.height - this.layout.getFooterHeight() - tabAreaTop);
|
| 141 | this.tabNavigationBar.getTabs().forEach(tab -> tab.visitChildren(child -> child.setHeight(tabArea.height())));
|
| 142 | this.tabManager.setTabArea(tabArea);
|
| 143 | this.layout.setHeaderHeight(tabAreaTop);
|
| 144 | this.layout.arrangeElements();
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public boolean keyPressed(final KeyEvent event) {
|
| 150 | return this.tabNavigationBar != null && this.tabNavigationBar.keyPressed(event) ? true : super.keyPressed(event);
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a) {
|
| 155 | super.extractRenderState(graphics, xm, ym, a);
|
| 156 | graphics.blit(RenderPipelines.GUI_TEXTURED, Screen.FOOTER_SEPARATOR, 0, this.height - this.layout.getFooterHeight(), 0.0F, 0.0F, this.width, 2, 32, 2);
|
| 157 | }
|
| 158 |
|
| 159 | @Override
|
| 160 | protected void extractMenuBackground(final GuiGraphicsExtractor graphics) {
|
| 161 | graphics.blit(RenderPipelines.GUI_TEXTURED, CreateWorldScreen.TAB_HEADER_BACKGROUND, 0, 0, 0.0F, 0.0F, this.width, this.layout.getHeaderHeight(), 16, 16);
|
| 162 | this.extractMenuBackground(graphics, 0, this.layout.getHeaderHeight(), this.width, this.height);
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | public void onClose() {
|
| 167 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 168 | }
|
| 169 |
|
| 170 | private static String getTranslationKey(final Stat<Identifier> stat) {
|
| 171 | return "stat." + stat.getValue().toString().replace(':', '.');
|
| 172 | }
|
| 173 |
|
| 174 | @Environment(EnvType.CLIENT)
|
| 175 | private class GeneralStatisticsList extends ObjectSelectionList<net.minecraft.client.gui.screens.achievement.StatsScreen.GeneralStatisticsList.Entry> {
|
| 176 | public GeneralStatisticsList(final Minecraft minecraft) {
|
| 177 | super(minecraft, StatsScreen.this.width, StatsScreen.this.layout.getContentHeight(), 33, 14);
|
| 178 | ObjectArrayList<Stat<Identifier>> stats = new ObjectArrayList<>(Stats.CUSTOM.iterator());
|
| 179 | stats.sort(Comparator.comparing(k -> I18n.get(StatsScreen.getTranslationKey((Stat<Identifier>)k))));
|
| 180 |
|
| 181 | for (Stat<Identifier> stat : stats) {
|
| 182 | this.addEntry(new net.minecraft.client.gui.screens.achievement.StatsScreen.GeneralStatisticsList.Entry(this, stat));
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | @Override
|
| 187 | public int getRowWidth() {
|
| 188 | return 280;
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | protected void extractListBackground(final GuiGraphicsExtractor graphics) {
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | protected void extractListSeparators(final GuiGraphicsExtractor graphics) {
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | @Environment(EnvType.CLIENT)
|
| 201 | private class ItemStatisticsList extends ContainerObjectSelectionList<net.minecraft.client.gui.screens.achievement.StatsScreen.ItemStatisticsList.Entry> {
|
| 202 | private static final int SLOT_BG_SIZE = 18;
|
| 203 | private static final int SLOT_STAT_HEIGHT = 22;
|
| 204 | private static final int SLOT_BG_Y = 1;
|
| 205 | private static final int SORT_NONE = 0;
|
| 206 | private static final int SORT_DOWN = -1;
|
| 207 | private static final int SORT_UP = 1;
|
| 208 | protected final List<StatType<Block>> blockColumns;
|
| 209 | protected final List<StatType<Item>> itemColumns;
|
| 210 | protected final Comparator<ItemRow> itemStatSorter = new ItemRowComparator(this);
|
| 211 | protected @Nullable StatType<?> sortColumn;
|
| 212 | protected int sortOrder;
|
| 213 |
|
| 214 | public ItemStatisticsList(final Minecraft minecraft) {
|
| 215 | super(minecraft, StatsScreen.this.width, StatsScreen.this.layout.getContentHeight(), 33, 22);
|
| 216 | this.blockColumns = Lists.newArrayList();
|
| 217 | this.blockColumns.add(Stats.BLOCK_MINED);
|
| 218 | this.itemColumns = Lists.newArrayList(Stats.ITEM_BROKEN, Stats.ITEM_CRAFTED, Stats.ITEM_USED, Stats.ITEM_PICKED_UP, Stats.ITEM_DROPPED);
|
| 219 | Set<Item> items = Sets.newIdentityHashSet();
|
| 220 |
|
| 221 | for (Item item : BuiltInRegistries.ITEM) {
|
| 222 | boolean addToList = false;
|
| 223 |
|
| 224 | for (StatType<Item> type : this.itemColumns) {
|
| 225 | if (type.contains(item) && StatsScreen.this.stats.getValue(type.get(item)) > 0) {
|
| 226 | addToList = true;
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | if (addToList) {
|
| 231 | items.add(item);
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | for (Block block : BuiltInRegistries.BLOCK) {
|
| 236 | boolean addToList = false;
|
| 237 |
|
| 238 | for (StatType<Block> type : this.blockColumns) {
|
| 239 | if (type.contains(block) && StatsScreen.this.stats.getValue(type.get(block)) > 0) {
|
| 240 | addToList = true;
|
| 241 | }
|
| 242 | }
|
| 243 |
|
| 244 | if (addToList) {
|
| 245 | items.add(block.asItem());
|
| 246 | }
|
| 247 | }
|
| 248 |
|
| 249 | items.remove(Items.AIR);
|
| 250 | if (!items.isEmpty()) {
|
| 251 | this.addEntry(new HeaderEntry(this));
|
| 252 |
|
| 253 | for (Item item : items) {
|
| 254 | this.addEntry(new ItemRow(this, item));
|
| 255 | }
|
| 256 | }
|
| 257 | }
|
| 258 |
|
| 259 | @Override
|
| 260 | protected void extractListBackground(final GuiGraphicsExtractor graphics) {
|
| 261 | }
|
| 262 |
|
| 263 | private int getColumnX(final int col) {
|
| 264 | return 75 + 40 * col;
|
| 265 | }
|
| 266 |
|
| 267 | @Override
|
| 268 | public int getRowWidth() {
|
| 269 | return 280;
|
| 270 | }
|
| 271 |
|
| 272 | private StatType<?> getColumn(final int i) {
|
| 273 | return i < this.blockColumns.size() ? this.blockColumns.get(i) : this.itemColumns.get(i - this.blockColumns.size());
|
| 274 | }
|
| 275 |
|
| 276 | private int getColumnIndex(final StatType<?> column) {
|
| 277 | int i = this.blockColumns.indexOf(column);
|
| 278 | if (i >= 0) {
|
| 279 | return i;
|
| 280 | }
|
| 281 |
|
| 282 | int j = this.itemColumns.indexOf(column);
|
| 283 | return j >= 0 ? j + this.blockColumns.size() : -1;
|
| 284 | }
|
| 285 |
|
| 286 | protected void sortByColumn(final StatType<?> column) {
|
| 287 | if (column != this.sortColumn) {
|
| 288 | this.sortColumn = column;
|
| 289 | this.sortOrder = -1;
|
| 290 | } else if (this.sortOrder == -1) {
|
| 291 | this.sortOrder = 1;
|
| 292 | } else {
|
| 293 | this.sortColumn = null;
|
| 294 | this.sortOrder = 0;
|
| 295 | }
|
| 296 |
|
| 297 | this.sortItems(this.itemStatSorter);
|
| 298 | }
|
| 299 |
|
| 300 | protected void sortItems(final Comparator<ItemRow> comparator) {
|
| 301 | List<ItemRow> itemRows = this.getItemRows();
|
| 302 | itemRows.sort(comparator);
|
| 303 | this.clearEntriesExcept(this.children().getFirst());
|
| 304 |
|
| 305 | for (ItemRow newChild : itemRows) {
|
| 306 | this.addEntry(newChild);
|
| 307 | }
|
| 308 | }
|
| 309 |
|
| 310 | private List<ItemRow> getItemRows() {
|
| 311 | List<ItemRow> itemRows = new ArrayList<>();
|
| 312 | this.children().forEach(entry -> {
|
| 313 | if (entry instanceof ItemRow itemRow) {
|
| 314 | itemRows.add(itemRow);
|
| 315 | }
|
| 316 | });
|
| 317 | return itemRows;
|
| 318 | }
|
| 319 |
|
| 320 | @Override
|
| 321 | protected void extractListSeparators(final GuiGraphicsExtractor graphics) {
|
| 322 | }
|
| 323 | }
|
| 324 |
|
| 325 | @Environment(EnvType.CLIENT)
|
| 326 | private class MobsStatisticsList extends ObjectSelectionList<MobRow> {
|
| 327 | public MobsStatisticsList(final Minecraft minecraft) {
|
| 328 | super(minecraft, StatsScreen.this.width, StatsScreen.this.layout.getContentHeight(), 33, 9 * 4);
|
| 329 |
|
| 330 | for (EntityType<?> type : BuiltInRegistries.ENTITY_TYPE) {
|
| 331 | if (StatsScreen.this.stats.getValue(Stats.ENTITY_KILLED.get(type)) > 0 || StatsScreen.this.stats.getValue(Stats.ENTITY_KILLED_BY.get(type)) > 0) {
|
| 332 | this.addEntry(new MobRow(this, type));
|
| 333 | }
|
| 334 | }
|
| 335 | }
|
| 336 |
|
| 337 | @Override
|
| 338 | public int getRowWidth() {
|
| 339 | return 280;
|
| 340 | }
|
| 341 |
|
| 342 | @Override
|
| 343 | protected void extractListBackground(final GuiGraphicsExtractor graphics) {
|
| 344 | }
|
| 345 |
|
| 346 | @Override
|
| 347 | protected void extractListSeparators(final GuiGraphicsExtractor graphics) {
|
| 348 | }
|
| 349 | }
|
| 350 |
|
| 351 | @Environment(EnvType.CLIENT)
|
| 352 | private class StatisticsTab extends GridLayoutTab {
|
| 353 | protected final AbstractSelectionList<?> list;
|
| 354 |
|
| 355 | public StatisticsTab(final Component title, final AbstractSelectionList<?> list) {
|
| 356 | super(title);
|
| 357 | this.layout.addChild(list, 1, 1);
|
| 358 | this.list = list;
|
| 359 | }
|
| 360 |
|
| 361 | @Override
|
| 362 | public void doLayout(final ScreenRectangle screenRectangle) {
|
| 363 | this.list.updateSizeAndPosition(StatsScreen.this.width, StatsScreen.this.layout.getContentHeight(), StatsScreen.this.layout.getHeaderHeight());
|
| 364 | super.doLayout(screenRectangle);
|
| 365 | }
|
| 366 | }
|
| 367 | }
|
| 368 |
|