Jump to content

[1.12.2] Custom player inventory


Asmodesu

Recommended Posts

So I want to create my own version of the player's inventory and it's gui which will be advanced with more tabs further (just like CustomNPCs does). And at this part I am confused at the very beginning. I've tried to look up at the vanilla sources but it was abit complicated to figure out the whole sequence. So I need someone to point me the right stages for:

1. Creating own inventory as the container with custom item, equipment and hotbar slots.

2. Creating appropriate GUI which will handle all the actions with inventory.

3. Catching the event of opening vanilla inventory to substitute it with mine.

Thanks in advance.

Link to comment
Share on other sites

21 minutes ago, Asmodesu said:

So I want to create my own version of the player's inventory and it's gui which will be advanced with more tabs further (just like CustomNPCs does). And at this part I am confused at the very beginning. I've tried to look up at the vanilla sources but it was abit complicated to figure out the whole sequence. So I need someone to point me the right stages for:

1. Creating own inventory as the container with custom item, equipment and hotbar slots.

2. Creating appropriate GUI which will handle all the actions with inventory.

3. Catching the event of opening vanilla inventory to substitute it with mine.

Thanks in advance.

where are you going to be opening the GUI from? what will hold your container?

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

12 minutes ago, Cadiboo said:

where are you going to be opening the GUI from? what will hold your container?

If I understood your questions correctly:

1. As I wrote, GUI will be opened instead of the vanilla one when pressing "Open Inventory" key which is E by default.

2. My container will hold actual player's inventory, his equipment slots, his items hotbar and skills hotbar contents.

Edited by Asmodesu
Link to comment
Share on other sites

For the 1. and 2. question: 

You could create a Container which holds the vanilla player inventory and your custom slots.

You could try extending the vanilla GuiInventory and change it as you want.

 

Edited by Zetko
Link to comment
Share on other sites

9 hours ago, Asmodesu said:

Creating own inventory as the container with custom item, equipment and hotbar slots.

You'll have to create your own capability that stores ItemStacks and attach it to the player. You can use an IItemHandler as the actual storage if you wish, it'll ultimately be easier.

9 hours ago, Asmodesu said:

actions with inventory.

Define this. What actions.

 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

5 minutes ago, Asmodesu said:

Standard actions with inventory like drag&drop, stacking, tooltip on hovering etc.

Those are all handled automatically with the vanilla stuff.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

16 hours ago, Animefan8888 said:

You'll have to create your own capability that stores ItemStacks and attach it to the player. You can use an IItemHandler as the actual storage if you wish, it'll ultimately be easier.

So I've created my Interface that extends IItemHandler and is implemented with my default implementation which has methods for inventory handling. I've created my own capability using @CapabilityInject(myInterface.class) and created my InventoryProvider which has (de)serializeNBT methods I'm currently working on.

Am I doing it right? And how then should implement the gui itself, how should I handle packets between client and server?

Edited by Asmodesu
Link to comment
Share on other sites

5 hours ago, Asmodesu said:

Am I doing it right?

Yes that is the appropriate way.

5 hours ago, Asmodesu said:

And how then should implement the gui itself

Create a Gui class that extends the one the Players inventory uses.

5 hours ago, Asmodesu said:

how should I handle packets between client and server?

You dont need to make packets unless you wanna do something special. Minecraft already has packets for inventories.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

You have to extend at least GuiScreen or one of its subclasses, the closer to the vanilla inventory you can get the better

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

Meanwhile I'm having some issues with cloning inventory capability. MC throws a NullPointerException to the 5th line any time I'm trying to resurrect after death.

ICustomInventoryCap inventoryExt = player.getCapability(CustomInventoryProvider.CUSTOM_INVENTORY_CAP, null);
        ICustomInventoryCap oldInventoryExt = event.getOriginal().getCapability(CustomInventoryProvider.CUSTOM_INVENTORY_CAP, null);

        for (int i = 0; i < inventoryExt.getSlots(); i++) {
            System.out.println (oldInventoryExt.getStackInSlot(i).getDisplayName());
            inventoryExt.insertItem(i, oldInventoryExt.getStackInSlot(i), false);
        }

Probably I'm doing something wrong trying to clone, nevertheless I have another capability which clones itself pretty well using the same algorithm.

Link to comment
Share on other sites

What exactly is null?

Is player always non null?

is getOriginal always non null?

 

do they both have your capability 100% of the time?

 

Also you should be properly copying the itemstacks, not just copying the references.

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

1 hour ago, Cadiboo said:

What exactly is null?

Is player always non null?

is getOriginal always non null?

 

do they both have your capability 100% of the time?

 

Also you should be properly copying the itemstacks, not just copying the references.

As I said NullPointer refers to oldInventoryExt.getStackInSlot(i) so I guess it is the slot content to return null. For ur 2nd, 3rd and 4th questions the answer is "yes".

Also what do you mean by "proper copying". getStackInSlot returns itemstack and insertItem inserts itemstack directly to the inventory capability storage.

Link to comment
Share on other sites

5 hours ago, Asmodesu said:

As I said NullPointer refers to oldInventoryExt.getStackInSlot(i) so I guess it is the slot content to return null.

This cannot happen since ItemStacks cant, rather shouldn't be null. My guess is that it is your capability that is null or, you didn't use a NonNullList to store your ItemStacks.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

3 hours ago, Animefan8888 said:

This cannot happen since ItemStacks cant, rather shouldn't be null. My guess is that it is your capability that is null or, you didn't use a NonNullList to store your ItemStacks.

I think you meant an ItemStackHandler

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Just now, Draco18s said:

I think you meant an ItemStackHandler

Technically yes, but they are ultimately stored as a NonNullList<ItemStack> ?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Yes, but you can't screw that up if you're using ItemStackHandler. ;) It's already implemented.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

11 hours ago, Asmodesu said:

Also what do you mean by "proper copying". getStackInSlot returns itemstack and insertItem inserts itemstack directly to the inventory capability storage.

Yeah, but it inserts the same itemstack itemstack that is in your old inventory. What if something modifies the old inventory? Your new one will also be modified

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

Wow, another question. I have this piece of code that should render my GUI depending on current resolution.

@SubscribeEvent //render extended health bar;
    public void renderGUI(RenderGameOverlayEvent event) {
        if (event.getType() == RenderGameOverlayEvent.ElementType.TEXT) {
            Minecraft mc = Minecraft.getMinecraft();
            ScaledResolution screen = new ScaledResolution(mc);
            int scFactor = Math.min((int) (screen.getScaledWidth() / originalWidth), (int) (screen.getScaledHeight() / originalHeight));
            //int width = (int) (hpBarWidth / originalWidth * screen.getScaledWidth()), height = (int) (hpBarHeight / originalHeight * screen.getScaledHeight());
            int x = (int) (hpBarX / originalWidth * screen.getScaledWidth()), y = (int) (hpBarY / originalHeight * screen.getScaledHeight());
            int currentWidth = (int) ((hpBarWidth / mc.player.getMaxHealth()) * mc.player.getHealth());
            GlStateManager.enableAlpha();
            GlStateManager.enableBlend();
            GlStateManager.alphaFunc(516, 0.1F);
            mc.renderEngine.bindTexture(hpBar);
            GL11.glPushMatrix();
            drawTexturedModalRect(x, y, 0, 0, hpBarWidth, hpBarHeight);
            drawTexturedModalRect(x, y, 0, hpBarHeight, currentWidth, hpBarHeight);
            drawTexturedModalRect(x, y, 0, hpBarHeight * 2, hpBarWidth, hpBarHeight);
            drawTexturedModalRect(x, y, 0, hpBarHeight * 3, hpBarWidth, hpBarHeight);
            GlStateManager.scale(scFactor, scFactor, scFactor);
            GL11.glPopMatrix();
            GlStateManager.disableBlend();
            GlStateManager.disableAlpha();
        }
    }

But seems like somewhy screen.getScaledWidth() and screen.getScaledHeight() both return zero that turns all screen adaptation system to nonsense. Probably I miss something about their implementation otherwise I don't understand how can they return zero values.

Edited by Asmodesu
Link to comment
Share on other sites

12 hours ago, Asmodesu said:

Wow, another question. I have this piece of code that should render my GUI depending on current resolution.


@SubscribeEvent //render extended health bar;
    public void renderGUI(RenderGameOverlayEvent event) {
        if (event.getType() == RenderGameOverlayEvent.ElementType.TEXT) {
            Minecraft mc = Minecraft.getMinecraft();
            ScaledResolution screen = new ScaledResolution(mc);
            int scFactor = Math.min((int) (screen.getScaledWidth() / originalWidth), (int) (screen.getScaledHeight() / originalHeight));
            //int width = (int) (hpBarWidth / originalWidth * screen.getScaledWidth()), height = (int) (hpBarHeight / originalHeight * screen.getScaledHeight());
            int x = (int) (hpBarX / originalWidth * screen.getScaledWidth()), y = (int) (hpBarY / originalHeight * screen.getScaledHeight());
            int currentWidth = (int) ((hpBarWidth / mc.player.getMaxHealth()) * mc.player.getHealth());
            GlStateManager.enableAlpha();
            GlStateManager.enableBlend();
            GlStateManager.alphaFunc(516, 0.1F);
            mc.renderEngine.bindTexture(hpBar);
            GL11.glPushMatrix();
            drawTexturedModalRect(x, y, 0, 0, hpBarWidth, hpBarHeight);
            drawTexturedModalRect(x, y, 0, hpBarHeight, currentWidth, hpBarHeight);
            drawTexturedModalRect(x, y, 0, hpBarHeight * 2, hpBarWidth, hpBarHeight);
            drawTexturedModalRect(x, y, 0, hpBarHeight * 3, hpBarWidth, hpBarHeight);
            GlStateManager.scale(scFactor, scFactor, scFactor);
            GL11.glPopMatrix();
            GlStateManager.disableBlend();
            GlStateManager.disableAlpha();
        }
    }

But seems like somewhy screen.getScaledWidth() and screen.getScaledHeight() both return zero that turns all screen adaptation system to nonsense. Probably I miss something about their implementation otherwise I don't understand how can they return zero values.

Don’t use GL11 directly, use GLStateManager

 

other than that I can’t see any obvious problems, you might be using scaled resolution incorrectly? Can you post a GitHub?

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

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.