Jump to content

[1.8] Issues with Slots in Containers


thejereman13

Recommended Posts

So, I simply can not seem to get my container slots to link up with the GUI I'm working on. It seems that when using addSlotToContainer() (correct as far as I can tell), it is just putting the slot at absolute coordinate values on the screen, which of course does not work with different screen sizes. I am aware that the slots should be relative to the GUI image itself so that it scales correctly, but every tutorial I have found gives me the same code, which of course isn't working for me. I'm assuming that I am either just missing something, or am going about this wrong. I am only adding the player's hotbar slots until I can get this working, but even with them in the wrong position, they aren't working anyway (e.g. picking up items out of the slots).

For Reference:

 

TileEntityArmorWorkbench

 

public class TileEntityArmorWorkbench extends TileEntity implements IInventory{

private ItemStack[] inventory;
public static String name = "armorWorkbench";
public static final int INV_SIZE = 9;

public TileEntityArmorWorkbench(){
	inventory = new ItemStack[iNV_SIZE];
}

@Override
public String getName() {
	return "Armor Workbench";
}

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

@Override
public IChatComponent getDisplayName() {
	return new ChatComponentText(name);
}

@Override
public int getSizeInventory() {
	return inventory.length;
}

@Override
public ItemStack getStackInSlot(int index) {
	return inventory[index];
}

@Override
public ItemStack decrStackSize(int index, int count) {
	ItemStack itemstack = getStackInSlot(index);
	if (itemstack != null){
		if (itemstack.stackSize <= count){
			setInventorySlotContents(index, null);
		}else{
			itemstack = itemstack.splitStack(count);
			markDirty();
		}
	}
	return itemstack;
}
public void onInventoryChanged() {
	for (int i = 0; i < getSizeInventory(); ++i){
		if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0){
			inventory[i] = null;
		}
	}

}

@Override
public ItemStack getStackInSlotOnClosing(int index) {
	ItemStack itemstack = getStackInSlot(index);
	setInventorySlotContents(index, null);
	return itemstack;
}

@Override
public void setInventorySlotContents(int index, ItemStack stack) {
	inventory[index] = stack;

	if (stack != null && stack.stackSize > getInventoryStackLimit()){
		stack.stackSize = getInventoryStackLimit();
	}
	markDirty();
}

@Override
public int getInventoryStackLimit() {
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	return player.getDistanceSq(player.posX, player.posY, player.posZ) <= 64;
}

@Override
public void openInventory(EntityPlayer player) {
	// TODO Auto-generated method stub

}

@Override
public void closeInventory(EntityPlayer player) {
	// TODO Auto-generated method stub

}

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

@Override
public int getField(int id) {
	// TODO Auto-generated method stub
	return 0;
}

@Override
public void setField(int id, int value) {
	// TODO Auto-generated method stub

}

@Override
public int getFieldCount() {
	return 0;
}

@Override
public void clear() {
	for (int i = 0; i < inventory.length; i++){
		inventory[i] = null;
	}
}

public void readFromNBT(NBTTagCompound compound) {
	NBTTagList items = compound.getTagList("ItemInventory", Constants.NBT.TAG_COMPOUND);
	for (int i = 0; i < items.tagCount(); ++i) {
		NBTTagCompound item = items.getCompoundTagAt(i);
		byte slot = item.getByte("Slot");
		if (slot >= 0 && slot < getSizeInventory()) {
			inventory[slot] = ItemStack.loadItemStackFromNBT(item);
		}
	}
}

public void writeToNBT(NBTTagCompound compound) {
	NBTTagList items = new NBTTagList();
	for (int i = 0; i < getSizeInventory(); ++i) {
		if (getStackInSlot(i) != null) {
			NBTTagCompound item = new NBTTagCompound();
			item.setByte("Slot", (byte) i);
			getStackInSlot(i).writeToNBT(item);
			items.appendTag(item);
		}
	}
	compound.setTag("ItemInventory", items);
}

}

 

 

GuiArmorWorkbench

 

public class GuiArmorWorkbench extends GuiContainer{


private float xSize_lo;
private float ySize_lo;
public static int xCord;
public static int yCord;
private int z;
private EntityPlayer player;
private World world;
private int xSize, ySize;
private final ResourceLocation backgroundImage = new ResourceLocation(Reference.MOD_ID.toLowerCase(), "textures/client/gui/guiArmorWorkbench.png");

public GuiArmorWorkbench(InventoryPlayer invPlayer, TileEntityArmorWorkbench entity){
	super(new ContainerArmorWorkbench(invPlayer, entity));		
	this.xSize = 176;
	this.ySize = 198;

}

@Override
public void initGui(){

}

@Override
public void drawScreen(int mouseX, int mouseY, float renderPartialTicks){
	super.drawScreen(mouseX, mouseY, renderPartialTicks);
	xSize_lo = mouseX;
	ySize_lo = mouseY;

}

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

@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY){
}

@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
	//GL11.glColor4f(1F, 1F, 1F, 1F);
	this.mc.getTextureManager().bindTexture(backgroundImage);
	xCord = (this.width - this.xSize) / 2;
	yCord = (this.height - this.ySize) / 2;
	drawTexturedModalRect(xCord, yCord, 0, 0, xSize, ySize);
	Console.println("X: " + xCord + " Y: " + yCord);
}
}

 

 

ContainerArmorWorkbench

 

public class ContainerArmorWorkbench extends Container{
private TileEntityArmorWorkbench workbench;

public ContainerArmorWorkbench(InventoryPlayer invPlayer, TileEntityArmorWorkbench entity){
	int m;
	int armorOffset = 29;
	int invStartY = 117;
	int invStartX = 30;
	this.workbench = entity;
	//Add slots from hotbar
	for (int x = 0; x < 9; x++){
		addSlotToContainer(new Slot(invPlayer, x, 8 + x * 18, 174));
		Console.println("Adding hotbar to inventory");
	}
	//Add slots from inventory
	/*for (m = 0; m < 3; ++m){
		for (int j = 0; j < 9; ++j){
			this.addSlotToContainer(new Slot(invPlayer, j + m * 9 + 9,+armorOffset + 8 + j * 18, 84 + m * 18));
			Console.println("Adding inventory to inventory");
		}
	}*/

}


@Override
public boolean canInteractWith(EntityPlayer playerIn) {
	return this.workbench.isUseableByPlayer(playerIn);
}

}

 

 

ArmorWorkbench (block class)

 

public class armorWorkbench extends Block implements ITileEntityProvider{

public static String name = "armorWorkbench";
public armorWorkbench(Material materialIn) {
	super(materialIn);
}


@Override
public TileEntity createNewTileEntity(World world, int meta){
	return new TileEntityArmorWorkbench();
}

public boolean hasTileEntity(int metadata){
	return true;
}

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) {
	if (true){
		FMLNetworkHandler.openGui(playerIn, Main.instance, Main.guiIDWorkbench, worldIn, pos.getX(), pos.getY(), pos.getZ());
	}
	return true;
}

}

 

 

Gui Handler

 

public class JeremanGuiHandler implements IGuiHandler {

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world,
		int x, int y, int z) {
	TileEntity tileentity = world.getTileEntity(new BlockPos(x,y,z));

	switch (ID){
	case Main.guiIDWorkbench:
		if (tileentity instanceof TileEntityArmorWorkbench){
			return new ContainerArmorWorkbench(player.inventory, (TileEntityArmorWorkbench) tileentity);
		}

	}

	return true;
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world,
		int x, int y, int z) {
	TileEntity tileentity = world.getTileEntity(new BlockPos(x,y,z));

	switch (ID){
	case Main.guiIDWorkbench:
		if (tileentity instanceof TileEntityArmorWorkbench){
			return new GuiArmorWorkbench(player.inventory, (TileEntityArmorWorkbench) tileentity);
		}

	}

	return true;
}

}

 

Thanks in advance!

Link to comment
Share on other sites

I think your container isn't updating correctly, cause you need the "detectAndSendChanges()" methode to sync a client with the actual magi  inside a GUI. I would recommend taking a peek at a vanilla container  class to see how the methode should look like.

Projects:

Discontinued:

- N2ConfigAPI

- Meachanical Crafting Table

 

Latest:

- CollectionUtils

 

Coöperations:

- InGameConfigManager

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.