Jump to content

[1.7.10] Problem with custom player inventory


the_salsa

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