untitled paste

unlisted ⁨1⁩ ⁨file⁩ 2022-08-15 05:50:35 UTC

DataMenuScreen

Raw
package net.codingguy32.developermod;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import javax.annotation.Nullable;
import java.util.Random;

public class DataMenuScreen extends Screen {
    Minecraft mc;
    @Nullable
    private ObjectSelectionList<?> activeList;
    private DataList dataList;

    public DataMenuScreen() {
        super(Component.literal("Data Menu"));
        this.mc = Minecraft.getInstance();
    }

    public void setActiveList(@Nullable ObjectSelectionList<?> objectSelectionList) {
        if (this.activeList != null) {
            this.removeWidget(this.activeList);
        }

        if (objectSelectionList != null) {
            this.addWidget(objectSelectionList);
            this.activeList = objectSelectionList;
        }

    }

    @Override
    public void init() {
        super.init();
        this.dataList = new DataList(this.minecraft);
        int x = this.width / 2 - 155;
        int y = this.height / 6 - 12;

        this.addRenderableWidget(new Button(this.width / 2 + 40, this.height - 52, 80, 20, Component.literal("stats"), (p_96949_) -> {
            this.setActiveList(this.dataList);
        }));
        this.addRenderableWidget(new Button(this.width / 2 - 100, y, 200, 20, CommonComponents.GUI_DONE, (p_97535_) -> {
            this.minecraft.popGuiLayer();
        }));
    }

    @Override
    public void render(PoseStack p_97530_, int p_97531_, int p_97532_, float p_97533_) {
        this.renderBackground(p_97530_);
        drawCenteredString(p_97530_, this.font, this.title, this.width / 2, 15, 16777215);
        super.render(p_97530_, p_97531_, p_97532_, p_97533_);
    }

    @OnlyIn(Dist.CLIENT)
    class DataList extends ObjectSelectionList<DataRow> {
        public DataList(Minecraft mc) {
            super(mc, DataMenuScreen.this.width, DataMenuScreen.this.height, 32, DataMenuScreen.this.height - 64, 10);
            DeveloperMOD.LOGGER.info(DataMenuScreen.this.width + "");
            Random r = new Random();
            for (int i = 0; i < 20; i++) {
                int x = r.nextInt(100000);
                this.addEntry(new DataRow(String.valueOf((char) i), x));
            }
        }

        @Override
        public void renderBackground(PoseStack poseStack) {
            DataMenuScreen.this.renderBackground(poseStack);
        }
    }

    @OnlyIn(Dist.CLIENT)
    class DataRow extends ObjectSelectionList.Entry<DataRow> {
        private final Component name, value;

        DataRow(String name, Object value) {
            this.name = Component.literal(name);
            this.value = Component.literal(value.toString());
        }

        private String getValueText() {
            return value.getString();
        }

        @Override
        public void render(PoseStack p_97011_, int p_97012_, int p_97013_, int p_97014_, int p_97015_, int p_97016_, int p_97017_, int p_97018_, boolean p_97019_, float p_97020_) {
            GuiComponent.drawString(p_97011_, DataMenuScreen.this.font, this.name, p_97014_ + 2, p_97013_ + 1, p_97012_ % 2 == 0 ? 16777215 : 9474192);
            String s = this.getValueText();
            GuiComponent.drawString(p_97011_, DataMenuScreen.this.font, s, p_97014_ + 2 + 213 - DataMenuScreen.this.font.width(s), p_97013_ + 1, p_97012_ % 2 == 0 ? 16777215 : 9474192);
        }

        @Override
        public Component getNarration() {
            return Component.translatable("narrator.select", Component.empty().append(this.name).append(" ").append(this.getValueText()));
        }
    }

}