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



×
×
  • Create New...

Important Information

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