Jump to content

How to have a scrollable list of options on a screen? [1.19.2] [SOLVED]


Recommended Posts

Posted (edited)

Hi! I have a custom block entity, which has a screen. I have managed to get the EditBox and a CheckBox working. Now I want to add a scrollable list of strings where the player can choose between options and finally select one, which would then open another screen according to the selection made by the player.  How to achieve this long list with a  scrollbar that would open another screen after selecting? What GUI component should I use for that? And are there any examples available somewhere? Thanks in advance. 

Edited by RInventor7
Posted (edited)

ObjectSelectionList. e.g. CreateBuffetWorldScreen.BiomeList

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted

Thanks, that did the trick. Everything works fine, but it renders the dirt background for some reason. I want to render my own texture and it actually does render below the dirt background. Is there any way to stop the dirt background from rendering?

 

Here's my code:

package com.rinventor....;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.rinventor.transportmod.MyMod;
import com.rinventor.transportmod.core.init.ModNetwork;

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.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

public class CScreen8 extends AbstractContainerScreen<CMenu8> {
	private final Player entity;
	private final Component data;
	private CScreen8.LinesList list;
	String line;
	List<String> lines;

	public CScreen8(CMenu8 container, Inventory inventory, Component text) {
		super(container, inventory, text);
		this.entity = container.entity;
		this.imageWidth = 320;
		this.imageHeight = 240;
		this.data = text;
		this.line = "";
		this.lines = new ArrayList<String>(List.of("A", "B", "C"));
	}

	private static final ResourceLocation texture = new ResourceLocation(MyMod.MOD_ID + ":textures/cmenu.png");

	@Override
	public void render(PoseStack ms, int mouseX, int mouseY, float partialTicks) {
		this.renderBackground(ms);
		super.render(ms, mouseX, mouseY, partialTicks);
		this.renderTooltip(ms, mouseX, mouseY);
		this.list.render(ms, mouseX, mouseY, partialTicks);
	}

	@Override
	protected void renderBg(PoseStack ms, float partialTicks, int gx, int gy) {
		RenderSystem.setShaderColor(1, 1, 1, 1);
		RenderSystem.setShaderTexture(0, texture);
		GuiComponent.blit(ms, this.leftPos, this.topPos, 0, 0, imageWidth, 198, imageWidth, imageHeight);
	}

	@Override
	public boolean keyPressed(int key, int b, int c) {
		if (key == 256) {
			this.minecraft.player.closeContainer();
			return true;
		}
		return super.keyPressed(key, b, c);
	}

	@Override
	protected void renderLabels(PoseStack ps, int mouseX, int mouseY) {
		String id = "menu1.transport";
		String id = data.getString().trim();
		this.font.draw(ps, Component.translatable(id).getString(), 7, 7, -16711792);
	}

	@SuppressWarnings("resource")
	@Override
	public void onClose() {
		super.onClose();
		Minecraft.getInstance().keyboardHandler.setSendRepeatsToGui(false);
	}

	@Override
	public void init() {
		super.init();
		this.minecraft.keyboardHandler.setSendRepeatsToGui(true);

		this.list = new CScreen8.LinesList();
		this.addWidget(this.list);

		this.addRenderableWidget(new Button(leftPos + imageWidth - 24, topPos + 4, 20, 20, Component.literal("<-"), e -> {
			ModNetwork.INSTANCE.sendToServer(new CScreenButtonMessage(7, ""));
			CScreenButtonMessage.handleButtonAction(entity, 7, "");
		}));
	}

	@OnlyIn(Dist.CLIENT)
	class LinesList extends ObjectSelectionList<CScreen8.LinesList.Entry> {
		LinesList() {
			super(CScreen8.this.minecraft, CScreen8.this.width-150, CScreen8.this.height-150, 50, CScreen8.this.height - 37, 16);
			for (String l : CScreen8.this.lines) {
				this.addEntry(new CScreen8.LinesList.Entry(l));
			}
		}

		protected boolean isFocused() {
			return CScreen8.this.getFocused() == this;
		}

		public void setSelected(@Nullable CScreen8.LinesList.Entry entry) {
			super.setSelected(entry);
			if (entry != null) {
				CScreen8.this.line = entry.line;
			}
			// AFTER SELECTED ->
//			ModNetwork.INSTANCE.sendToServer(new CScreenButtonMessage(0, "test"));
//			CScreenButtonMessage.handleButtonAction(entity, 0, "test");
		}

		@OnlyIn(Dist.CLIENT)
		class Entry extends ObjectSelectionList.Entry<CScreen8.LinesList.Entry> {
			final String line;

			public Entry(String entry) {
				this.line = entry;
			}

			public Component getNarration() {
				return Component.translatable("narrator.select", this.line);
			}

			public void render(PoseStack p_95802_, int p_95803_, int p_95804_, int p_95805_, int p_95806_, int p_95807_, int p_95808_, int p_95809_,
					boolean p_95810_, float p_95811_) {
				GuiComponent.drawString(p_95802_, CScreen8.this.font, this.line, p_95805_ + 5, p_95804_ + 2, 16777215);
			}

			public boolean mouseClicked(double p_95798_, double p_95799_, int p_95800_) {
				if (p_95800_ == 0) {
					LinesList.this.setSelected(this);
					return true;
				}
				else {
					return false;
				}
			}
		}
	}

}

 

Posted

AbstractSelectionList.setRenderBackground()

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

  • RInventor7 changed the title to How to have a scrollable list of options on a screen? [1.19.2] [SOLVED]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Well, when I log in to the server, sometimes within an hour, sometimes within a minute, the server closes and informs me that there was a Ticking entity error. Below is the crash report
    • Try switching to Windowed or Borderless Window mode in Minecraft. These modes make it easier for the recorder to capture gameplay, as it still has access to the display without the game taking up all of the graphics resources.
    • This forum is for Forge, not NeoForge. Please go to them for support.
    • Forge version: 55.0.0 Minecraft version: 1.21.5 Downloads: As this is the start of a new version, it is recommended that you check the downloads page and use the latest version to receive any bug fixes. Downloads page Intro: Good evening! Today, we have released our initial build of Forge 55.0 for Minecraft 1.21.5. 1.21.5 is the newest member of the 1.21 family of versions, which was released yesterday on March 25, 2025. As a reminder, the first minor (X.0) of a Forge version is a beta. Forge betas are marked as such on the bottom left of the title screen and are candidates for any breaking changes. Additionally, there are a couple of important things to note about this update, which I've made sure to mention in this post as well. Feel free to chat with us about bugs or these implementation changes on GitHub and in our Discord server. As always, we will continue to keep all versions of 1.21 and 1.20 in active support as covered by our tiered support policy. Cheers, happy modding, and good luck porting! Rendering Refactor For those who tuned in to Minecraft Live on March 22, 2025, you may already know that Mojang have announced their intention to bring their new Vibrant Visuals overhaul to Java in the future. They've taken the first steps toward this by refactoring how rendering pipelines and render types are handled internally. This has, in turn, made many of Forge's rendering APIs that have existed for years obsolete, as they (for the most part) can be done directly in vanilla. If there was a rendering API that was provided by Forge which you believe should be re-implemented, we're happy to discuss on GitHub through an issue or a pull request. Deprecation of weapon-like ToolActions In 1.21.5, Minecraft added new data components for defining the characteristics of weapons in data. This includes attack speed, block tags which define efficient blocks, and more. As such, we will begin marking our ToolActions solution for this as deprecated. ToolActions were originally added to address the problem of creating modded tools that needed to perform the same actions as vanilla tools. There are still a few tool actions that will continue to be used, such as the shears tool action for example. There are some existing Forge tool actions that are currently obsolete and have no effect given the way the new data components are implemented. We will continue to work on these deprecations and invite you to chat with us on GitHub or Discord if you have any questions.
    • In summary, a full mod to adjust mining progress in such a specific way does not yet exist in its exact form, but it is possible to find mods that change certain aspects of progression (e.g. "Harder Ores").
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.