Skip to content

Instantly share code, notes, and snippets.

@Densamisten
Created September 9, 2024 15:45
Show Gist options
  • Select an option

  • Save Densamisten/963adebcee10e25515a18b021398ab7f to your computer and use it in GitHub Desktop.

Select an option

Save Densamisten/963adebcee10e25515a18b021398ab7f to your computer and use it in GitHub Desktop.
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 net.minecraft.text.Text;
import org.joml.Quaternionf;
public class ScrollingEntityListWidget extends EntryListWidget<ScrollingEntityListWidget.EntityEntry> {
private final MinecraftClient client;
public final int itemHeight;
private Entity selectedEntity; // Field to store the selected entity
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;
// Fetch entities directly from the current world and add them to the list
if (client.world != null) {
for (Entity entity : client.world.getEntities()) {
addEntry(new EntityEntry(entity)); // Add each entity to the list
}
}
}
// Method to get the selected entity (if needed)
public Entity getSelectedEntity() {
return selectedEntity;
}
@Override
protected void appendClickableNarrations(NarrationMessageBuilder builder) {
}
// 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 the selected entity entry
if (entity == selectedEntity) {
context.fill(x, y, x + entryWidth, y + entryHeight, 0x80FFFFFF); // Highlight with white transparency
}
renderEntityInList(context.getMatrices(), x + entryWidth / 2, y + entryHeight - 10, 30, entity);
}
// Handle click events to select the entity
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (button == 0) { // Left-click
selectedEntity = entity; // Set the selected entity
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); // Positioning the entity in the list
matrixStack.scale(scale, scale, scale); // Scaling the entity
// Flip the entity upright and apply Y-axis rotation
matrixStack.multiply(new Quaternionf().rotateX((float) Math.toRadians(180)));
matrixStack.multiply(new Quaternionf().rotateY((float) Math.toRadians(135))); // Adjust this for desired rotation
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