Jump to content

Recommended Posts

Posted (edited)

[SOVLED]
 

Quote

SOLVED:
I figured out why it wasn't working.

Turns out you HAVE to initialize your widgets in init(); I had already done this, but because of the way I was doing things I thought I could get away with putting it in my infoReceived method, turns out I couldn't.

 

Hello, for my system, I need to display a number for each item in a list, I have found an object just for this. It is called ExtendedList.
However, it is implemented not to be used as a widget in the same way, for instance, I had to disable the dirt background and manually set left and right values.
The problem is when I resize my Minecraft window, the scrolling becomes completely unresponsive, and the bounds of the widget go completely off.
I found a solution for problem 2, as I can reset the left and right values during render, however scrolling functionality still breaks.

Here is my code:

 

package majd123mc.mpropertymod.client.gui;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;

import majd123mc.mpropertymod.MPropertyMod;
import majd123mc.mpropertymod.blocks.AddressBlockTE;
import majd123mc.mpropertymod.client.gui.AddressBlockScreen.MoneyList.MoneyListEntry;
import majd123mc.mpropertymod.inventory.AddressBlockContainer;
import majd123mc.mpropertymod.network.CMoveAddress;
import majd123mc.mpropertymod.network.ModPacketHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.gui.widget.Widget;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.client.gui.widget.list.AbstractList.AbstractListEntry;
import net.minecraft.client.gui.widget.list.ExtendedList;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.registries.ForgeRegistries;

public class AddressBlockScreen extends ContainerScreen<AddressBlockContainer> {

    private static final ResourceLocation ADDRESS_BLOCK_GUI_TEXTURES = new ResourceLocation(MPropertyMod.MODID,
            "textures/gui/container/address_block.png");

    private Map<ResourceLocation, Integer> moneyInBank = new HashMap<>();
    private boolean isPrivateMode = false;
    private BlockPos pos;

    private boolean infoReceived = false;
    // Widgets
    private MoneyList moneyList;

    public AddressBlockScreen(AddressBlockContainer containerIn, PlayerInventory playerInventoryIn,
            ITextComponent titleIn) {
        super(containerIn, playerInventoryIn, titleIn);
    }

    protected void drawGuiContainerForegroundLayer(MatrixStack matrixStack, int x, int y) {
        String s = title.getString();
        font.drawString(matrixStack, s, (float) (this.xSize / 2 - this.font.getStringWidth(s) / 2), 6.0F, 4210752);
        for (Widget widget : this.buttons) {
            if (widget.isHovered()) {
                widget.renderToolTip(matrixStack, x - this.guiLeft, y - this.guiTop);
                break;
            }
        }

    }

    protected void drawGuiContainerBackgroundLayer(MatrixStack matrixStack, float partialTicks, int x, int y) {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bindTexture(ADDRESS_BLOCK_GUI_TEXTURES);
        int i = (this.width - this.xSize) / 2;
        int j = (this.height - this.ySize) / 2;
        this.blit(matrixStack, i, j, 0, 0, this.xSize, this.ySize);
    }

    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        this.renderBackground(matrixStack);
        super.render(matrixStack, mouseX, mouseY, partialTicks);
        moneyList.render(matrixStack, mouseX, mouseY, partialTicks);
        int i = guiTop;
        i = guiLeft;
        //moneyList.updateSize(moneyList.getWidth(), moneyList.getHeight(), guiTop + 36, guiTop + 95, guiLeft + 5, guiLeft + 171);
        this.renderHoveredTooltip(matrixStack, mouseX, mouseY);
    }

    public void init() {
        super.init();
        // Rename Button
        addButton(new Button(width / 2 - (175 / 4) - 40, guiTop + 14, 83, 20, new StringTextComponent("Rename"),
                (something) -> {
                    Minecraft mc = Minecraft.getInstance();

                    if (!infoReceived)
                        return;

                    TileEntity tileentity = mc.world.getTileEntity(pos);
                    if (tileentity instanceof AddressBlockTE) {
                        mc.displayGuiScreen(new EditAddressBlockScreen((AddressBlockTE) tileentity));
                    }
                }));
        // Move Button
        addButton(new Button(width / 2 + (175 / 4) - 42, guiTop + 14, 83, 20, new StringTextComponent("Move"),
                (something) -> {
                    Minecraft mc = Minecraft.getInstance();

                    if (!infoReceived)
                        return;

                    ModPacketHandler.INSTANCE.sendToServer(new CMoveAddress(pos));
                    mc.displayGuiScreen(null);
                }));
    }

    public void receiveInfo(BlockPos posIn, Map<String, Integer> money, boolean privateMode) {
        moneyInBank = new HashMap<ResourceLocation, Integer>();
        money.forEach((itemString, amount) -> moneyInBank.put(new ResourceLocation(itemString), amount));
        isPrivateMode = privateMode;
        pos = posIn;

        moneyList = new MoneyList(this, moneyInBank);
        children.add(moneyList);
        
        infoReceived = true;
    }

    public class MoneyList extends ExtendedList<MoneyListEntry> {

        Map<ResourceLocation, Integer> money;

        public MoneyList(AddressBlockScreen parent, Map<ResourceLocation, Integer> moneyIn) {

            // For future developers/me:
            // func_244606_c sets the transparency of the dirt outline,
            // func_244605_b sets the transparency of the black background,
            // x0 - left
            // x1 - right

            // 1. Minecraft 2. Width 3. Height 4. Top (y) 5. Bottom (y) 5. Entry width
            // * There are no left or right, substituted by "x0 = ... " and "x1 = ... "
            super(parent.minecraft, parent.width, parent.height, guiTop + 36, guiTop + 95, 16);
            func_244606_c(false); // Disables dirt outline
            x0 = guiLeft + 5; // left
            x1 = guiLeft + 171; // right

            // Handle money data
            if (moneyIn != null && !containsGoldNugget(moneyIn)) {
                moneyIn.put(Items.GOLD_NUGGET.getRegistryName(), 0);
            }
            money = moneyIn;
            if (moneyIn == null)
                return;
            for (Entry<ResourceLocation, Integer> entry : moneyIn.entrySet()) {
                Item item = ForgeRegistries.ITEMS.getValue(entry.getKey());
                addEntry(new MoneyListEntry(parent, item, entry.getValue()));
            }
        }
        
        public void updateSize(int widthIn, int heightIn, int topIn, int bottomIn, int leftIn, int rightIn) {
              this.width = widthIn;
              this.height = heightIn;
              this.y0 = topIn;
              this.y1 = bottomIn;
              this.x0 = leftIn;
              this.x1 = rightIn;
           }
        
        private boolean containsGoldNugget(Map<ResourceLocation, Integer> money) {
            for (Entry<ResourceLocation, Integer> entry : money.entrySet()) {
                if (entry.getKey().equals(Items.GOLD_NUGGET.getRegistryName())) {
                    return true;
                }
            }
            return false;
        }

        @Override
        protected int getScrollbarPosition() {
            return this.getRight() - 6;
        }

        @Override
        public int getRowWidth() {
            return this.width;
        }

        public class MoneyListEntry extends AbstractListEntry<MoneyListEntry> {
            AddressBlockScreen parent;
            Item item;
            int amount;

            public MoneyListEntry(AddressBlockScreen parentIn, Item itemIn, Integer amountIn) {
                parent = parentIn;
                item = itemIn;
                amount = amountIn;
            }

            @Override
            public void render(MatrixStack mStack, int entryIdx, int top, int left, final int entryWidth,
                    final int entryHeight, final int mouseX, final int mouseY, final boolean p_194999_5_,
                    final float partialTicks) {
                Minecraft mc = Minecraft.getInstance();
                FontRenderer font = mc.fontRenderer;
                parent.itemRenderer.zLevel = 100.0F;

                // Item renderer
                //if (top + 16 > width)
                //    return;
                parent.itemRenderer.renderItemIntoGUI(new ItemStack(item), left, top);

                parent.itemRenderer.zLevel = 0.0F;
                int i = width;
                font.drawString(mStack, "Test", left + 5, top + 2, 0xFFFFFF);
            }

        }

    }
}
 

Most of this code was influenced by LoadingErrorScreen as suggested by others in the forums, as some of the method arguments are labeled.

Here are some screenshots to demonstrate this (note that the code that fixes the resolution resize issue have been commented out):
 

Screenshot mod 1.png

Screenshot mod 2.png

Edited by Majd123mc
Solved
Posted

SOLVED:
I figured out why it wasn't working.

Turns out you HAVE to initialize your widgets in init(); I had already done this, but because of the way I was doing things I thought I could get away with putting it in my infoReceived method, turns out I couldn't.

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

    • Reach Out To Rapid Digital: What sapp Info: +1 41 4 80 7 14 85 Email INFO: rap iddi gita lrecov ery @ exe cs. com Hello, my name is Jayson, and I’m 35 years old from the United Kingdom. My family and I recently endured an incredibly challenging experience that I wouldn’t wish on anyone. We became victims of a cryptocurrency investment fraud scheme that saw us lose a staggering $807,000 in USDT and Bitcoins. The fraudsters had created a convincing facade, and we were lured into investing, only to discover later that the platform was a complete scam. We were left devastated, not just financially, but emotionally, as we had trusted these people and believed in the legitimacy of the investment. After the initial shock wore off, we desperately searched for ways to recover the lost funds. It seemed like an impossible task, and we felt as though there was no hope. That’s when, by sheer luck, we stumbled across a post about Rapid Digital Recovery, a cryptocurrency and funds recovery organization with a proven track record in cybersecurity and fraud recovery. We decided to reach out to them, and from the first interaction, we were impressed with their professionalism and transparency. They explained the recovery process in detail and reassured us that they had the skills and expertise to track down the perpetrators and recover our funds. This gave us a renewed sense of hope, something we hadn’t felt in months. What truly stood out during our experience with Rapid Digital Recovery was their dedication to the recovery process. The team went above and beyond, using sophisticated tracking tools and cyber forensics to gather critical information. Within a matter of weeks, they had successfully located the funds and traced the scam back to the fraudsters responsible. They worked with the authorities to ensure the criminals were held accountable for their actions. To our relief, the team at Rapid Digital Recovery was able to recover every single penny we had lost. The funds were returned in full, and the sense of closure we felt was invaluable. We couldn’t have imagined such a positive outcome in the early stages of our recovery journey, and we are deeply grateful for the work they did. If you ever find yourself in a similar situation, I highly recommend contacting Rapid Digital Recovery. Their expertise, transparency, and dedication to their clients make them the go-to choice for anyone seeking to recover lost cryptocurrency or funds. They truly gave us back our financial future.  
    • This is my first time modding anything, so maybe just skill issue. I'm using Forge 54.0.12 and Temurin 21.0.5+11-LTS I wanted to create a custom keybind and to check whether it works I'd like to send a chat message. I tried using Minecraft.getInstance().player.sendSystemMessage(Component.literal("test")); but IntelliJ couldnt resolve sendSystemMessage(...). Since I saw people using it in earlier versions, I tried the same thing with 1.20.6(- 50.1.0), where it works fine, now I can't figure out if this is intentional and whether there are other options for sending chat messages. On that note, is there more documentation than https://docs.minecraftforge.net/en/1.21.x/? It seems very incomplete compared to something like the Oracle Java docs
    • Hi, i'm having this error and I wanna fix it. we try: -Reload drivers -Eliminate .minecraft -Eliminate Java -Restart launcher -Verify if minecraft is using gpu -Mods  in .minecraft is empty -Install the latest and recomended version of forge idk what i have to do, help me pls. the lastest log is: https://mclo.gs/WAMao8x  
    • Read the FAQ, Rule #2. (https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/)  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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