Created
September 9, 2024 15:53
-
-
Save Densamisten/b47d9181c05152d667720468dcc12f00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package exonihility.client.gui; | |
| import net.minecraft.client.MinecraftClient; | |
| import net.minecraft.client.gui.DrawContext; | |
| import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; | |
| import net.minecraft.client.gui.widget.EntryListWidget; | |
| import net.minecraft.client.render.entity.EntityRenderDispatcher; | |
| import net.minecraft.client.util.math.MatrixStack; | |
| import net.minecraft.entity.Entity; | |
| import org.joml.Quaternionf; | |
| public class ScrollingEntityListWidget extends EntryListWidget<ScrollingEntityListWidget.EntityEntry> { | |
| private final MinecraftClient client; | |
| public final int itemHeight; | |
| private EntityEntry selectedEntry; // Field to track the selected entry | |
| public ScrollingEntityListWidget(MinecraftClient client, int width, int height, int y, int bottom, int itemHeight) { | |
| super(client, width, height, y, itemHeight); | |
| this.client = client; | |
| this.itemHeight = itemHeight; | |
| refreshList(); // Initial population of the list | |
| } | |
| // Method to get the selected entity | |
| public Entity getSelectedEntity() { | |
| if (selectedEntry != null) { | |
| return selectedEntry.entity; | |
| } | |
| return null; | |
| } | |
| // Method to set the selected entry | |
| public void setSelected(EntityEntry entry) { | |
| this.selectedEntry = entry; | |
| } | |
| // Method to refresh the list of entities | |
| public void refreshList() { | |
| this.clearEntries(); | |
| if (client.world != null) { | |
| for (Entity entity : client.world.getEntities()) { | |
| addEntry(new EntityEntry(entity)); | |
| } | |
| } | |
| } | |
| @Override | |
| protected void appendClickableNarrations(NarrationMessageBuilder builder) { | |
| // Handle narrations for accessibility if needed | |
| } | |
| // Entry for rendering entities | |
| public class EntityEntry extends Entry<ScrollingEntityListWidget.EntityEntry> { | |
| private final Entity entity; | |
| public EntityEntry(Entity entity) { | |
| this.entity = entity; | |
| } | |
| @Override | |
| public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { | |
| // Highlight if selected or hovered | |
| if (this == selectedEntry || hovered) { | |
| context.fill(x, y, x + entryWidth, y + entryHeight, 0x80FFFFFF); // Highlight the selected or hovered entry | |
| } | |
| // Render the entity | |
| renderEntityInList(context.getMatrices(), x + entryWidth / 2, y + entryHeight - 10, 20, entity); | |
| // Render entity name | |
| context.drawText(MinecraftClient.getInstance().textRenderer, entity.getDisplayName(), x + 5, y + 5, 0xFFFFFF, false); | |
| } | |
| @Override | |
| public boolean mouseClicked(double mouseX, double mouseY, int button) { | |
| if (button == 0) { | |
| ScrollingEntityListWidget.this.setSelected(this); // Set this entry as selected when clicked | |
| return true; | |
| } | |
| return false; | |
| } | |
| } | |
| // Render method to display entities within the list | |
| private static void renderEntityInList(MatrixStack matrixStack, int posX, int posY, int scale, Entity entity) { | |
| matrixStack.push(); | |
| matrixStack.translate(posX, posY, 50); | |
| matrixStack.scale(scale, scale, scale); | |
| // Rotate the entity | |
| matrixStack.multiply(new Quaternionf().rotateX((float) Math.toRadians(180))); | |
| matrixStack.multiply(new Quaternionf().rotateY((float) Math.toRadians(135))); | |
| EntityRenderDispatcher entityRenderDispatcher = MinecraftClient.getInstance().getEntityRenderDispatcher(); | |
| entityRenderDispatcher.setRenderShadows(false); | |
| entityRenderDispatcher.render(entity, 0, 0, 0, 0.0F, 1.0F, matrixStack, MinecraftClient.getInstance().getBufferBuilders().getEntityVertexConsumers(), 15728880); | |
| matrixStack.pop(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment