Jump to content

[1.7.10] Problem with custom player inventory


the_salsa
 Share

Recommended Posts

Hi there!

I'm trying to implement a custom player inventory that simply adds 2 custom slots where the crafting grid would normally be.  So far I'm at the point where the player can open the screen, but when I attempt to pick up an item from a slot it gets instantly put back.  I realize this is probably a server-client type of issue, but I can't figure out where my bug is and I've tried fixes from other related posts that I've seen but none of them seem to work.

 

In main mod file:

 

 

    @EventHandler
    public void init(FMLInitializationEvent event)
    {
        //Register GuiHandler
        NetworkRegistry.INSTANCE.registerGuiHandler(this, proxy);
    }

 

 

 

Client Proxy:

 

 

package com.the_salsa.spacemod;

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

import com.martin.firstmod.EntityMartMob;
import com.martin.firstmod.ModelMartMob;
import com.martin.firstmod.RenderMart;

import net.minecraft.client.model.ModelBiped;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.world.World;
import net.minecraftforge.client.MinecraftForgeClient;
import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.common.FMLCommonHandler;

public class ClientProxy extends CommonProxy
{
        public static final Map<Item, ModelBiped> armorModels = new HashMap<Item, ModelBiped>();
       
        /**
         * register Entity renderers
         */
        @Override
        public void registerRendering()
        {
                RenderingRegistry.registerEntityRenderingHandler(EntityBlasterBolt.class, new RenderBlasterBolt());
        }
       
        @Override
        public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
        {
                if (ID == SpaceMod.GUI_CUSTOM_INV)
                {
                        return new GuiCustomPlayerInventory(player, player.inventory, ((ExtendedPropertiesPlayer) player.getExtendedProperties("ExtendedPropertiesPlayer")).inventory);
                }
               
                return null;
        }
       
        /**
         * register Item renderers
         */
        @Override
        public void registerItemRenders()
        {      
                MinecraftForgeClient.registerItemRenderer(SpaceMod.blasterPistol, new RenderBlasterPistol());
                MinecraftForgeClient.registerItemRenderer(SpaceMod.blasterRifle, new RenderBlasterRifle());
               
                armorModels.put(SpaceMod.maneuverGear, new ModelManeuverGear(1F));
                armorModels.put(SpaceMod.oxygenHelmet, new ModelOxygenHelmet(1F));
        }
}

 

 

 

Common Proxy:

 

 

package com.the_salsa.spacemod;

import cpw.mods.fml.common.network.IGuiHandler;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.model.ModelBiped;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;

public class CommonProxy implements IGuiHandler
{
        public void registerRendering()
        {}
       
        public void registerItemRenders()
        {}

        @Override
        public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
        {
                if (ID == SpaceMod.GUI_CUSTOM_INV)
                {
                        return new ContainerOxygenTanks(player, player.inventory, ((ExtendedPropertiesPlayer) player.getExtendedProperties("ExtendedPropertiesPlayer")).inventory);
                }
               
                return null;
        }

        @Override
        public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
        {
                return null;
        }
}

 

 

 

Container:

 

 

package com.the_salsa.spacemod;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;

public class ContainerOxygenTanks extends Container
{
       
        public ContainerOxygenTanks(EntityPlayer player, InventoryPlayer inventoryP, InventoryOxygenTanks inventoryO)
        {
                //oxygen tank slots
                int i;
        int j;

        for (i = 0; i < 1; ++i)
        {
            for (j = 0; j < 2; ++j)
            {
                this.addSlotToContainer(new SlotOxygenTanks(inventoryO, j + i * 2, 88 + j * 18, 26 + i * 18));
            }
        }
       
        for (i = 0; i < 4; ++i)
        {
            final int k = i;
            this.addSlotToContainer(new SlotArmor(player, inventoryP, inventoryP.getSizeInventory() - 1 - i, 8, 8 + i * 18, i));
        }

        for (i = 0; i < 3; ++i)
        {
            for (j = 0; j < 9; ++j)
            {
                this.addSlotToContainer(new Slot(inventoryP, j + (i + 1) * 9, 8 + j * 18, 84 + i * 18));
            }
        }

        for (i = 0; i < 9; ++i)
        {
            this.addSlotToContainer(new Slot(inventoryP, i, 8 + i * 18, 142));
        }
        }
       
        @Override
        public boolean canInteractWith(EntityPlayer player)
        {
                return true;
        }
       
        /**
     * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
     */
        @Override
    public ItemStack transferStackInSlot(EntityPlayer p_82846_1_, int p_82846_2_)
    {
        ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(p_82846_2_);

        if (slot != null && slot.getHasStack())
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if (p_82846_2_ == 0)
            {
                if (!this.mergeItemStack(itemstack1, 9, 45, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (p_82846_2_ >= 1 && p_82846_2_ < 5)
            {
                if (!this.mergeItemStack(itemstack1, 9, 45, false))
                {
                    return null;
                }
            }
            else if (p_82846_2_ >= 5 && p_82846_2_ < 9)
            {
                if (!this.mergeItemStack(itemstack1, 9, 45, false))
                {
                    return null;
                }
            }
            else if (itemstack.getItem() instanceof ItemArmor && !((Slot)this.inventorySlots.get(5 + ((ItemArmor)itemstack.getItem()).armorType)).getHasStack())
            {
                int j = 5 + ((ItemArmor)itemstack.getItem()).armorType;

                if (!this.mergeItemStack(itemstack1, j, j + 1, false))
                {
                    return null;
                }
            }
            else if (p_82846_2_ >= 9 && p_82846_2_ < 36)
            {
                if (!this.mergeItemStack(itemstack1, 36, 45, false))
                {
                    return null;
                }
            }
            else if (p_82846_2_ >= 36 && p_82846_2_ < 45)
            {
                if (!this.mergeItemStack(itemstack1, 9, 36, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 9, 45, false))
            {
                return null;
            }

            if (itemstack1.stackSize == 0)
            {
                slot.putStack((ItemStack)null);
            }
            else
            {
                slot.onSlotChanged();
            }

            if (itemstack1.stackSize == itemstack.stackSize)
            {
                return null;
            }

            slot.onPickupFromSlot(p_82846_1_, itemstack1);
        }

        return itemstack;
    }
}

 

 

 

GuiContainer:

 

 

package com.the_salsa.spacemod;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

public class GuiCustomPlayerInventory extends GuiContainer
{
        /** x size of the inventory window in pixels. Defined as float, passed as int */
        private float xSize_lo;
        /** y size of the inventory window in pixels. Defined as float, passed as int. */
        private float ySize_lo;  
        private static final ResourceLocation iconLocation = new ResourceLocation(SpaceMod.MODID, "textures/gui/customInventory.png");
        private final InventoryOxygenTanks inventory;

        public GuiCustomPlayerInventory(EntityPlayer player, InventoryPlayer inventoryPlayer, InventoryOxygenTanks inventoryCustom)
        {
                super(new ContainerOxygenTanks(player, inventoryPlayer, inventoryCustom));
                this.inventory = inventoryCustom;
                // if need the player for something later on, store it in a local variable here as well
        }

        /**
         * Draws the screen and all the components in it.
         */
        @Override
        public void drawScreen(int par1, int par2, float par3)
        {
                super.drawScreen(par1, par2, par3);
                this.xSize_lo = (float)par1;
                this.ySize_lo = (float)par2;
        }

        /**
         * Draw the background layer for the GuiContainer (everything behind the items)
         */
        @Override
        protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
        {
                GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
                this.mc.getTextureManager().bindTexture(iconLocation);

                int k = (this.width - this.xSize) / 2;
                int l = (this.height - this.ySize) / 2;

                this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
                int i1;
                drawPlayerModel(k + 51, l + 75, 30, (float)(k + 51) - this.xSize_lo, (float)(l + 75 - 50) - this.ySize_lo, this.mc.thePlayer);
        }

        /**
         * Copied straight out of vanilla - renders the player model on screen
         */
        public static void drawPlayerModel(int par0, int par1, int par2, float par3, float par4, EntityLivingBase par5EntityLivingBase)
        {
                GL11.glEnable(GL11.GL_COLOR_MATERIAL);
                GL11.glPushMatrix();
                GL11.glTranslatef((float)par0, (float)par1, 50.0F);
                GL11.glScalef((float)(-par2), (float)par2, (float)par2);
                GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F);

                float f2 = par5EntityLivingBase.renderYawOffset;
                float f3 = par5EntityLivingBase.rotationYaw;
                float f4 = par5EntityLivingBase.rotationPitch;
                float f5 = par5EntityLivingBase.prevRotationYawHead;
                float f6 = par5EntityLivingBase.rotationYawHead;

                GL11.glRotatef(135.0F, 0.0F, 1.0F, 0.0F);
                RenderHelper.enableStandardItemLighting();
                GL11.glRotatef(-135.0F, 0.0F, 1.0F, 0.0F);
                GL11.glRotatef(-((float)Math.atan((double)(par4 / 40.0F))) * 20.0F, 1.0F, 0.0F, 0.0F);

                par5EntityLivingBase.renderYawOffset = (float)Math.atan((double)(par3 / 40.0F)) * 20.0F;
                par5EntityLivingBase.rotationYaw = (float)Math.atan((double)(par3 / 40.0F)) * 40.0F;
                par5EntityLivingBase.rotationPitch = -((float)Math.atan((double)(par4 / 40.0F))) * 20.0F;
                par5EntityLivingBase.rotationYawHead = par5EntityLivingBase.rotationYaw;
                par5EntityLivingBase.prevRotationYawHead = par5EntityLivingBase.rotationYaw;

                GL11.glTranslatef(0.0F, par5EntityLivingBase.yOffset, 0.0F);

                RenderManager.instance.playerViewY = 180.0F;
                RenderManager.instance.renderEntityWithPosYaw(par5EntityLivingBase, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F);

                par5EntityLivingBase.renderYawOffset = f2;
                par5EntityLivingBase.rotationYaw = f3;
                par5EntityLivingBase.rotationPitch = f4;
                par5EntityLivingBase.prevRotationYawHead = f5;
                par5EntityLivingBase.rotationYawHead = f6;

                GL11.glPopMatrix();
                RenderHelper.disableStandardItemLighting();
                GL11.glDisable(GL12.GL_RESCALE_NORMAL);
                OpenGlHelper.setActiveTexture(OpenGlHelper.lightmapTexUnit);
                GL11.glDisable(GL11.GL_TEXTURE_2D);
                OpenGlHelper.setActiveTexture(OpenGlHelper.defaultTexUnit);
        }
}

 

 

 

Opening the GUI (verified receiving packet works):

 

 

	public void handleOpenGui(OxygenInventoryPacket message, EntityPlayer player)
	{
		System.out.println("[PACKET] received open gui packet");
		player.openGui(SpaceMod.instance, message.guiId, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
	}

 

 

 

Can anyone help me out?  Thanks!

Link to comment
Share on other sites

@Override

        public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)

        {

                return null;

        }

 

should be returning the Gui to open

 

and use

player.openGui(mod.instance, GUI_ID, world, x, y, z);

to open the gui

 

 

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
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.

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • REM Forge requires a configured set of both JVM and program arguments. REM Add custom JVM arguments to the user_jvm_args.txt REM Add custom program arguments {such as nogui} to this file in the next line before the %* or REM  pass them to this script directly "C:\Program Files\Java\jdk-17\bin\Java.exe" @user_jvm_args.txt @libraries/net/minecraftforge/forge/1.18.2-40.1.80/win_args.txt -Xmx8G -Xms8G %* pause     so I used it.
    • I created an SOUL_JAR Item that has been registered for each mob like - mod:soul_jar_minecraft_cow etc.. but it all has different registry names, i have a model file name soul_jar.json and i want to know how to bind each of those items to this class (When mod iterates through all of vanilla mobs and registering items for them).   I didn't found any info about it because all of info is outdated, and forge docs is complicated asf (i wish there would be more examples) i also used AI but it showing me only outdated info, but theres some code where i registering my item and trying to bind it to model ->   public static void register(IEventBus eventBus){ ITEMS.register(eventBus); Set<ResourceLocation> entityKeyList = ForgeRegistries.ENTITY_TYPES.getKeys(); for (ResourceLocation k : entityKeyList) { EntityType<?> entityType = ForgeRegistries.ENTITY_TYPES.getValue(k); if (!entityType.getCategory().equals(MobCategory.MISC)) { RegistryObject<Item> SOUL_JAR = ITEMS.register("soul_jar" + '_' + k.toString().replace(':', '_').replace('.', '_'), () -> new JarItem(new Item.Properties().rarity(Rarity.RARE).tab(ModTabs.MAGICAL_OBSESSION_JARS), entityType)); if (Thread.currentThread().getThreadGroup() == SidedThreadGroups.CLIENT) { ItemModelShaper itemModelShaper = Minecraft.getInstance().getItemRenderer().getItemModelShaper(); itemModelShaper.register(SOUL_JAR.get().asItem(), new ModelResourceLocation("item.soul_jar", "inventory")); } } } } I also have more questions like how to iterate through all other mods mobs also, or render mob inside of a jar in inventory, but i will try to figure it out.
    • I looked into OctoEconomyAPI and saw that it was a JAR api for fabric. This means you have a fabric mod in your mods folder. I downloaded your mod folder and wasted 2 solid hours creating a Python script to try to find the faulty mod, and eventually just gave up. I suggest you create two installations with half or the mods in each, find which one doesn't launch, cut that one in half, and repeat until you find the fabric mod. Investigué OctoEconomyAPI y vi que era una API JAR para fabric. Esto significa que tienes un mod de Fabric en tu carpeta de mods. Descargué su carpeta de mods y desperdicié 2 horas completas creando un script de Python para tratar de encontrar el mod defectuoso, y finalmente me di por vencido. Le sugiero que cree dos instalaciones con la mitad o las modificaciones en cada una, encuentre cuál no se inicia, córtela por la mitad y repita hasta que encuentre la modificación Fabric.   (The Python script I made is here): (El script de Python que hice está aquí):  
    • I've got it to work now. Instead of using the texture manager I used the Render System:   RenderSystem.setShaderTexture(0, new ResourceLocation("mcaquests", "textures/item/questbook.png"));  
  • Topics

×
×
  • Create New...

Important Information

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