Jump to content

[Solved] How to create new Entity in GUI?


Hipposgrumm

Recommended Posts

I want to be able to render an entity in a GUI (like in the survival inventory). I found the renderEntityInInventory() method, but I can't figure out how to create and render a new entity. For reference, I am trying to recreate the armor stand like in the new smithing table GUI.

Smithing_Table_GUI_1.20.png

This is what I have so far:

Spoiler

Spoiler so that my code doesn't take up the entire screen, unless opened.

package gg.hipposgrumm.armor_trims.gui;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.logging.LogUtils;
import gg.hipposgrumm.armor_trims.Main;
import gg.hipposgrumm.armor_trims.config.Config;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerListener;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.slf4j.Logger;

import static net.minecraft.client.gui.screens.inventory.InventoryScreen.renderEntityInInventory;

@OnlyIn(Dist.CLIENT)
public class SmithingScreenNew extends AbstractContainerScreen<SmithingMenuNew> {
    private static final Logger LOGGER = LogUtils.getLogger();

    public static final ResourceLocation SMITHING = new ResourceLocation(Main.MODID, "textures/gui/container/smithing_new.png");
    public static final ResourceLocation SMITHING_TRIM = new ResourceLocation(Main.MODID, "textures/gui/container/smithing_new_trim.png");
    public static final ResourceLocation SMITHING_CLEAN = new ResourceLocation(Main.MODID, "textures/gui/container/smithing_new_clean.png");
    public static final ResourceLocation SMITHING_CLEAN_TRIM = new ResourceLocation(Main.MODID, "textures/gui/container/smithing_new_clean_trim.png");

    private ContainerListener containerListener;
    private ResourceLocation GUI_SMITHING;
    private ResourceLocation GUI_SMITHING_TRIM;
    private Config.As_Client.PreviewModes armorstandMode;
    private ArmorStand preview;

    public SmithingScreenNew(SmithingMenuNew p_99290_, Inventory p_99291_, Component p_99292_) {
        super(p_99290_, p_99291_, p_99292_);
        this.titleLabelX = 60;
        this.titleLabelY = 18;
    }

    protected void subInit() {}

    protected void init() {
        super.init();
        this.subInit();
        this.menu.addSlotListener(this.containerListener);
        armorstandMode = Config.previewMode();
        if (armorstandMode == Config.As_Client.PreviewModes.ARMORSTAND) {
            GUI_SMITHING = SMITHING;
            GUI_SMITHING_TRIM = SMITHING_TRIM;
            preview = new ArmorStand(this.minecraft.player.getLevel(), 0, 999999999, 0);
            preview.setNoGravity(true);
        } else if (armorstandMode == Config.As_Client.PreviewModes.FRONTFACING) {
            GUI_SMITHING = SMITHING;
            GUI_SMITHING_TRIM = SMITHING_TRIM;
            preview = new ArmorStand(this.minecraft.player.getLevel(), 0, 999999999, 0);
            preview.setNoGravity(true);
            preview.setInvisible(true);
        } else if (armorstandMode == Config.As_Client.PreviewModes.NONE) {
            GUI_SMITHING = SMITHING_CLEAN;
            GUI_SMITHING_TRIM = SMITHING_CLEAN_TRIM;
        } else {
            GUI_SMITHING = SMITHING_CLEAN;
            GUI_SMITHING_TRIM = SMITHING_CLEAN_TRIM;
            preview = new ArmorStand(this.minecraft.player.getLevel(), 0, 999999999, 0);
            preview.setNoGravity(true);
        }
    }

    public void removed() {
        super.removed();
        this.menu.removeSlotListener(this.containerListener);
    }

    protected void renderLabels(PoseStack p_99294_, int p_99295_, int p_99296_) {
        RenderSystem.disableBlend();
        super.renderLabels(p_99294_, p_99295_, p_99296_);
    }

    public void render(PoseStack p_98922_, int p_98923_, int p_98924_, float p_98925_) {
        this.renderBackground(p_98922_);
        super.render(p_98922_, p_98923_, p_98924_, p_98925_);
        RenderSystem.disableBlend();
        this.renderTooltip(p_98922_, p_98923_, p_98924_);
    }

    protected void renderBg(PoseStack p_98917_, float p_98918_, int p_98919_, int p_98920_) {
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
        RenderSystem.setShaderTexture(0, SmithingScreenNew.this.GUI_SMITHING);
        int i = (this.width - this.imageWidth) / 2;
        int j = (this.height - this.imageHeight) / 2;
        this.blit(p_98917_, i, j, 0, 0, this.imageWidth, this.imageHeight);
        this.blit(p_98917_, i + 59, j + 20, 0, this.imageHeight + (this.menu.getSlot(0).hasItem() ? 0 : 16), 110, 16);
        if ((this.menu.getSlot(0).hasItem() || this.menu.getSlot(1).hasItem() || this.menu.getSlot(3).hasItem()) && !this.menu.getSlot(2).hasItem()) {
            this.blit(p_98917_, i + 99, j + 45, this.imageWidth, 0, 28, 21);
        }
        if (this.menu.getSlot(1).hasItem() && this.menu.getSlot(1).getItem().is(Main.smithing_templates)) {
            RenderSystem.setShaderTexture(0, SmithingScreenNew.this.GUI_SMITHING_TRIM);
        } else {
            RenderSystem.setShaderTexture(0, SmithingScreenNew.this.GUI_SMITHING);
            LOGGER.warn(armorstandMode.toString());
        }
        if (this.armorstandMode != Config.As_Client.PreviewModes.NONE) {
            try {
                renderEntityInInventory(i + 51, j + 75, 30, 0, 0, preview);
            } catch (NullPointerException e) {
                LOGGER.warn("Armorstand is disabled or null.");
            }
        }
    }

    @Override
    public void onClose() {
        super.onClose();
        if (preview != null) {
            preview.remove(Entity.RemovalReason.DISCARDED);
        }
    }

    public void dataChanged(AbstractContainerMenu p_169759_, int p_169760_, int p_169761_) {}
    public void slotChanged(AbstractContainerMenu p_98910_, int p_98911_, ItemStack p_98912_) {}
}

And before anyone tells me, I have checked InventoryScreen.class (it's where I found renderEntityInInventory()). InventoryScreen.class refers to an existing entity (the player), I am trying to instantiate and refer to a NEW entity. How can I do this or is there a better way to do it than what I think I should do? (I WOULD decompile 23w05a if mcp for snapshots was a thing.)

Edited by Hipposgrumm
Case Closed

I'm not good at modding, but at least I can read a crash report (well enough). That's something, right?

Link to comment
Share on other sites

  • Hipposgrumm changed the title to How to create new Entity in GUI?

I figured it out, I had to cast preview as LivingEntity instead of ArmorStand.

From:

private ArmorStand preview;

To:

private LivingEntity preview;
Edited by Hipposgrumm
For modders hoping for help with this themselves, this is all that had to change from my original code; line 37.

I'm not good at modding, but at least I can read a crash report (well enough). That's something, right?

Link to comment
Share on other sites

  • Hipposgrumm changed the title to [Solved] How to create new Entity in GUI?

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.



×
×
  • Create New...

Important Information

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