Jump to content

[Solved] [1.7.10] Block Container issue with Slot shfiting


Ddogclaw

Recommended Posts

Hey guys, I've ran into a bit of an issue here. I'm trying to make a mod with a block that has an inventory, but I've done something wrong somewhere in my code (my guess is in the container class) that makes it so that all of the slots in the inventory are shifted to the left side of the screen.

 

Here's a picture of the problem (I have all the slots in my inventory filled):

errorthingy.png

 

Here are my classes:

 

[spoiler=ContainerCuilGenerator.java]

package com.ddogclaw.container;

import com.ddogclaw.godeeper.tileentity.TileEntityCuilGeneratorEntity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotFurnace;
import net.minecraft.item.ItemStack;

public class ContainerCuilGenerator extends Container
{
protected TileEntityCuilGeneratorEntity tileEntity;

public ContainerCuilGenerator(InventoryPlayer inventoryPlayer,
		TileEntityCuilGeneratorEntity te)
{
	tileEntity = te;
	int i = 0;
	int j = 0;

	for(i = 0; i < 3; i++) {
         for(j = 0; j < 3; j++) {
            addSlotToContainer(new Slot(tileEntity, j + i * 3, 62 + j * 18, 17 + i * 18));
         }
      }

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

	for (i = 0; i < 9; ++i)
	{
		this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
	}
}

protected void bindPlayerInventory(InventoryPlayer inventoryPlayer)
{

}

@Override
public boolean canInteractWith(EntityPlayer player)
{
	// TODO Auto-generated method stub
	return tileEntity.isUseableByPlayer(player);
}

// For shift-clicking, without this, it would cause massive errors
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slot)
{
	ItemStack stack = null;
	Slot slotObject = (Slot) inventorySlots.get(slot);

	// null checks and checks if the item can be stacked (maxStackSize > 1)
	if (slotObject != null && slotObject.getHasStack())
	{
		ItemStack stackInSlot = slotObject.getStack();
		stack = stackInSlot.copy();

		// Merges the item into player inventory since its in the tileEntity
		if (slot < 9)
		{
			if (!this.mergeItemStack(stackInSlot, 0, 35, true))
			{
				return null;

			}

			// places it into the tileEntity is possible since its in the
			// player inventory
		} else if (!this.mergeItemStack(stackInSlot, 0, 9, false))
		{
			return null;

		}

		if (stackInSlot.stackSize == 0)
		{
			slotObject.putStack(null);

		} else
		{
			slotObject.onSlotChanged();

		}

		if (stackInSlot.stackSize == stack.stackSize)
		{
			return null;
		}

		slotObject.onPickupFromSlot(player, stackInSlot);
	}

	return stack;
}

}

 

 

[spoiler=BlockCuilGenerator.java]

package com.ddogclaw.godeeper.block;

import com.ddogclaw.godeeper.GoDeeper;
import com.ddogclaw.godeeper.gui.GuiCuilGenerator;
import com.ddogclaw.godeeper.tileentity.TileEntityCuilGeneratorEntity;

import cofh.api.energy.IEnergyHandler;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

public class BlockCuilGenerator extends BlockContainer
{
private String name = "cuilGenerator";
public BlockCuilGenerator()
{
	super(Material.rock);
	setCreativeTab(CreativeTabs.tabBlock);
	setBlockBounds(0, 0, 0, 1, 1, 1);
	System.out.println("REGISTERING CUIL GENERATOR. JUST THOUGHT YOU SHOULD KNOW");
	GameRegistry.registerBlock(this, name);
	setBlockName(GoDeeper.MODID + "." + name);
	setBlockTextureName(GoDeeper.MODID + ":" + name);
}

@Override
public boolean hasTileEntity()
{
	return true;
}

@Override
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_)
{
	// TODO Auto-generated method stub
	return new TileEntityCuilGeneratorEntity();
}

@Override
public int getRenderType()
{
	return -1;
}

@Override
public boolean isOpaqueCube()
{
	return false;
}

@Override
public boolean renderAsNormalBlock()
{
	return false;
}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9)
{
	TileEntity tileEntity = world.getTileEntity(x,y,z);

	if (tileEntity == null)
	{
		return false;
	}

	if (world.isRemote)
	{
		System.out.println("Opening GUI");
		player.openGui(GoDeeper.instance, 1, world, x, y, z);

		return true;
	}
	return false;
}

}

 

 

[spoiler=TileEntityCuilGeneratorEntity.java]

package com.ddogclaw.godeeper.tileentity;

import com.ddogclaw.godeeper.GoDeeper;

import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

public class TileEntityCuilGeneratorEntity extends TileEntity implements IInventory
{
private String name = "cuilGeneratorTile";
public int customField;
private ItemStack[] inv = new ItemStack[9];

@Override
public void writeToNBT(NBTTagCompound tagCompound)
{
	super.writeToNBT(tagCompound);

	NBTTagList itemList = new NBTTagList();
	for(int i = 0; i < inv.length; i++)
	{
		ItemStack stack = inv[i];
		if (stack != null)
		{
			NBTTagCompound tag = new NBTTagCompound();
			tag.setByte("Slot", (byte) i);
			stack.writeToNBT(tag);
			itemList.appendTag(tag);
		}
	}
	tagCompound.setTag("Inventory", itemList);
}

@Override
public void readFromNBT(NBTTagCompound tagCompound)
{
	super.readFromNBT(tagCompound);

	NBTTagList tagList = tagCompound.getTagList("Inventory", 0);
	for (int i = 0; i < tagList.tagCount(); i++)
	{
		NBTTagCompound tag = tagList.getCompoundTagAt(i);
		byte slot = tag.getByte("Slot");
		if (slot >= 0 && slot < inv.length)
		{
			inv[slot] = ItemStack.loadItemStackFromNBT(tag);
		}
	}
}

@Override
public int getSizeInventory()
{
	// TODO Auto-generated method stub
	return inv.length;
}

@Override
public ItemStack getStackInSlot(int slot)
{
	// TODO Auto-generated method stub
	return inv[slot];
}

@Override
public ItemStack decrStackSize(int slot, int amt)
{
	ItemStack stack = getStackInSlot(slot);
	if (stack != null)
	{
		if (stack.stackSize <= amt)
		{
			setInventorySlotContents(slot, null);
		} 
		else
		{
			stack = stack.splitStack(amt);
			if (stack.stackSize == 0)
			{
				setInventorySlotContents(slot, null);
			}
		}
	}
	return stack;
}

@Override
public ItemStack getStackInSlotOnClosing(int slot)
{
	ItemStack stack = getStackInSlot(slot);
	if (stack != null)
	{
		setInventorySlotContents(slot, null);
	}
	return stack;
}

@Override
public void setInventorySlotContents(int slot, ItemStack stack)
{
	inv[slot] = stack;
	if (stack != null && stack.stackSize > getInventoryStackLimit())
	{
		stack.stackSize = getInventoryStackLimit();
	}
}

@Override
public String getInventoryName()
{
	return GoDeeper.MODID + "." + name;
}

@Override
public boolean hasCustomInventoryName()
{
	// TODO Auto-generated method stub
	return false;
}

@Override
public int getInventoryStackLimit()
{
	// TODO Auto-generated method stub
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer player)
{
	return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
}

@Override
public void openInventory()
{

}

@Override
public void closeInventory()
{

}

@Override
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_)
{
	// TODO Auto-generated method stub
	return false;
}

}

 

 

[spoiler=GuiCuilGenerator.java]

package com.ddogclaw.godeeper.gui;

import org.lwjgl.opengl.GL11;

import com.ddogclaw.container.ContainerCuilGenerator;
import com.ddogclaw.godeeper.GoDeeper;
import com.ddogclaw.godeeper.tileentity.TileEntityCuilGeneratorEntity;

import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;

public class GuiCuilGenerator extends GuiContainer
{
public static final int guiID = 1;

public GuiCuilGenerator(InventoryPlayer inventoryPlayer, TileEntityCuilGeneratorEntity tileEntity)
{
	super(new ContainerCuilGenerator(inventoryPlayer, tileEntity));
}

protected int xSize = 247;
protected int ySize = 165;

@Override
public void initGui()
{
	buttonList.clear();
}

@Override
public boolean doesGuiPauseGame()
{
	return false;
}

protected void drawGuiContainerForegroundLayer(int param1, int param2)
{
	//Text and stuff
	fontRendererObj.drawString("Current Cuil Level:", (width - xSize) / 2, (height - ySize) / 2, 4210752);
}

@Override
protected void drawGuiContainerBackgroundLayer(float par1,
		int par2, int par3)
{
	GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	this.mc.renderEngine.bindTexture(new ResourceLocation(GoDeeper.MODID + ":textures/gui/cuilGeneratorGuiBG.png"));
	int x = (width - xSize) / 2;
	int y = (height - ySize) / 2;
	this.drawTexturedModalRect(x, y, 0, 0, 247, ySize);

}
}

 

 

[spoiler=GuiHandler.java]

package com.ddogclaw.godeeper;

import com.ddogclaw.container.ContainerCuilGenerator;
import com.ddogclaw.godeeper.gui.GuiCuilGenerator;
import com.ddogclaw.godeeper.tileentity.TileEntityCuilGeneratorEntity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IGuiHandler;

public class GuiHandler implements IGuiHandler
{

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world,
		int x, int y, int z)
{
	TileEntity tileEntity = world.getTileEntity(x,y,z);
	if(tileEntity instanceof TileEntityCuilGeneratorEntity)
	{
		return new ContainerCuilGenerator(player.inventory, (TileEntityCuilGeneratorEntity) tileEntity);
}

	return null;
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world,
		int x, int y, int z)
{
	TileEntity tileEntity = world.getTileEntity(x, y, z);
	if (tileEntity instanceof TileEntityCuilGeneratorEntity)
	{
		return new GuiCuilGenerator(player.inventory, (TileEntityCuilGeneratorEntity) tileEntity);
	}
	return null;
}

}

 

 

I appreciate any help and criticism that you guys can give me. Thanks!

Link to comment
Share on other sites

Hi

 

A couple of comments

 

I notice this in your GuiCuilGenerator

 

protected int xSize = 247;

protected int ySize = 165;

 

That's bad news, you are hiding the xSize and ySize fields in the GuiContainer base class, so your class now has two fields called xSize; one that is seen by your GuiCuilGenerator code, the other that is seen by GuiContainer.  I think that's asking for trouble.

 

As for the drawing location, you are overriding initGUI() which is responsible for setting guiLeft and guiTop, but you didn't call super.initGUI, so the guiLeft and guiTop are still zero.

 

-TGG

 

 

 

 

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.