Jump to content

[SOLVED][1.7.10] Items appear in wrong slots on client in custom GUI.


don_bruce

Recommended Posts

I'm experiencing some odd behavior with one of my blocks.  Specifically, I have a TileEntity that implements IInventory that's messing up slot data.  Right now I just have it open a GUI that has the standard player inventory.  The inventory is just fine on the server (checked with println statements), but for some reason all slots are shifted by an index of 9.  By this I mean that I can have an item in the left-most slot on my hotbar, but upon opening the inventory it suddenly warps up one slot to the lower-left slot in my internal inventory.  Although the item (and tooltip) is in the upper slot, I can only grab the item if I take it out of the lower slot, which tells me the slot data is correct on the server.

 

Opening the TileEntity GUI results in no change, unless I open the Player GUI first which appears to save the 'new' slot position.  This warping happens until the item goes out of the inventory index, and sometimes results in my player becoming a literal blockhead.  I know it's linked to container slot assignments, as each non-player slot assigned to the container results in items shifting one less slot.  The code for the player slot data is copied directly from ContainerChest, and works fine on one of my custom Entities, as does the inventory code, so I can't fathom why it's not working on my TileEntity.  Hopefully someone here can tell me where to start looking.

 

Currently running Forge 1.7.10-1558

 

Class Files

 

BlockPropellerBench

 

public class BlockPropellerBench extends BlockContainer{	

public BlockPropellerBench(){
	super(Material.iron);
	this.setBlockName("PropellerBench");
	this.setCreativeTab(MFS.tabMFS);
	this.textureName = "mfs:propellerbench";
}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ){
	if(!world.isRemote && ((TileEntityCrafter) world.getTileEntity(x, y, z)).isUseableByPlayer(player)){
		player.openGui(MFS.instance, -1, world, x, y, z);
	}
	return true;
}

@Override
public TileEntity createNewTileEntity(World world, int metadata){
	return new TileEntityCrafter(4, (short) 1120);
}
}

 

 

TileEntityCrafter

 

public class TileEntityCrafter extends TileEntity implements IInventory{
public short propertyCode;
private ItemStack[] contents;

public TileEntityCrafter(int numberSlots, short defaultPropertyCode){
	this.contents = new ItemStack[numberSlots];
	this.propertyCode = defaultPropertyCode;
}

public void markDirty(){}
public void clear(){}
public void setField(int id, int value){}
public void openInventory(){this.openInventory(null);}
public void openInventory(EntityPlayer player){}
public void closeInventory(){this.closeInventory(null);}
    public void closeInventory(EntityPlayer player){}
    
public boolean hasCustomInventoryName(){return false;}
public boolean isUseableByPlayer(EntityPlayer player){return this.getDistanceFrom(player.posX, player.posY, player.posZ) < 5;}
public boolean isItemValidForSlot(int slot, ItemStack stack){return true;}
public int getField(int id){return 0;}
public int getFieldCount(){return 0;}
public int getSizeInventory(){return contents.length;}
public int getInventoryStackLimit(){return 64;}
public String getInventoryName(){return "";}
public ItemStack getStackInSlot(int slot){return this.contents[slot];}
public ItemStack getStackInSlotOnClosing(int slot){return null;}

    public void setInventorySlotContents(int slot, ItemStack item){
        this.contents[slot] = item;
        if(item != null && item.stackSize > this.getInventoryStackLimit()){
            item.stackSize = this.getInventoryStackLimit();
        }
    }

    public ItemStack decrStackSize(int slot, int number){
        if(this.contents[slot] != null){
            ItemStack itemstack;
            if(this.contents[slot].stackSize <= number){
                itemstack = this.contents[slot];
                this.contents[slot] = null;
                return itemstack;
            }else{
                itemstack = this.contents[slot].splitStack(number);
                if(this.contents[slot].stackSize == 0){
                    this.contents[slot] = null;
                }
                return itemstack;
            }
        }else{
            return null;
        }
    }
    
    public ItemStack removeStackFromSlot(int index){
	ItemStack removedStack = getStackInSlot(index);
	setInventorySlotContents(index, null);
	return removedStack;
}

@Override
    public void readFromNBT(NBTTagCompound tagCompound){
        super.readFromNBT(tagCompound);
        this.propertyCode = tagCompound.getShort("propertyCode");
        NBTTagList nbttaglist = tagCompound.getTagList("Items", 10);
        this.contents = new ItemStack[this.getSizeInventory()];
        for (int i = 0; i < nbttaglist.tagCount(); ++i){
            NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
            int j = nbttagcompound1.getByte("Slot") & 255;
            if(j >= 0 && j < this.contents.length){
                this.contents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
            }
        }
    }
    
@Override
    public void writeToNBT(NBTTagCompound tagCompound){
        super.writeToNBT(tagCompound);
        tagCompound.setShort("propertyCode", this.propertyCode);
        NBTTagList nbttaglist = new NBTTagList();
        for(int i = 0; i < this.contents.length; ++i){
            if (this.contents[i] != null){
                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
                nbttagcompound1.setByte("Slot", (byte)i);
                this.contents[i].writeToNBT(nbttagcompound1);
                nbttaglist.appendTag(nbttagcompound1);
            }
        }
        tagCompound.setTag("Items", nbttaglist);
    }
}

 

 

GUIPropellerBench

 

public class GUIPropellerBench extends GuiContainer{
private static final ResourceLocation woodBlockIcon = new ResourceLocation("minecraft", "textures/blocks/planks_oak.png");
private static final ResourceLocation ironIngotIcon = new ResourceLocation("minecraft", "textures/items/iron_ingot.png");
private static final ResourceLocation obsidianBlockIcon = new ResourceLocation("minecraft", "textures/blocks/obsidian.png");
private static final ResourceLocation woodPropellerIcon = new ResourceLocation("mfs", "textures/items/propeller0.png");
private static final ResourceLocation ironPropellerIcon = new ResourceLocation("mfs", "textures/items/propeller1.png");
private static final ResourceLocation obsidianPropellerIcon = new ResourceLocation("mfs", "textures/items/propeller2.png");
private static final ResourceLocation background = new ResourceLocation("mfs", "textures/guis/gui_background.png");
private static final ResourceLocation foreground = new ResourceLocation("mfs", "textures/guis/propeller_bench.png");

private GuiButton tier1Button;
private GuiButton tier2Button;
private GuiButton tier3Button;
private GuiButton diameterUpButton;
private GuiButton diameterDownButton;
private GuiButton pitchUpButton;
private GuiButton pitchDownButton;

private TileEntityCrafter tile;
private byte propType;
private byte propBlades;
private byte propPitch;
private byte propDiamater;

public GUIPropellerBench(InventoryPlayer invPlayer, TileEntityCrafter tile){
	super(new ContainerPropellerBench(invPlayer, tile));
	this.allowUserInput=true;
	this.xSize = 175;
	this.ySize = 222;
	this.tile = tile;
	this.propType = (byte) (tile.propertyCode%10 + 1);
	this.propBlades = (byte) (tile.propertyCode%100/10);
	this.propPitch = (byte) (55+3*(tile.propertyCode%1000/100));
	this.propDiamater = (byte) (70+5*(tile.propertyCode/1000));
}

@Override 
public void initGui(){
	guiLeft = (this.width - this.xSize)/2;
	guiTop = (this.height - this.ySize)/2;

	buttonList.add(tier1Button = new GuiButton(0, guiLeft + 10, guiTop + 10, 20, 20, ""));
	buttonList.add(tier2Button = new GuiButton(0, guiLeft + 40, guiTop + 10, 20, 20, ""));
	buttonList.add(tier3Button = new GuiButton(0, guiLeft + 70, guiTop + 10, 20, 20, ""));
	buttonList.add(diameterDownButton = new GuiButton(0, guiLeft + 110, guiTop + 20, 15, 20, "-"));
	buttonList.add(diameterUpButton = new GuiButton(0, guiLeft + 130, guiTop + 20, 15, 20, "+"));
	buttonList.add(pitchDownButton = new GuiButton(0, guiLeft + 110, guiTop + 80, 15, 20, "-"));
	buttonList.add(pitchUpButton = new GuiButton(0, guiLeft + 130, guiTop + 80, 15, 20, "+"));


	if(propType == 1){
		//this.inventorySlots.putStackInSlot(4, new ItemStack(Blocks.planks));
	}else if(propType == 2){
		//this.inventorySlots.putStackInSlot(4, new ItemStack(Items.iron_ingot));
	}else if(propType == 3){
		//this.inventorySlots.putStackInSlot(4, new ItemStack(Blocks.obsidian));
	}
	tier1Button.enabled = propType != 1;
	tier2Button.enabled = propType != 2;
	tier3Button.enabled = propType != 3;
}

@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int x, int y){
	RenderHelper.bindTexture(background);
	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
	RenderHelper.bindTexture(foreground);
	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
}

@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY){
        GL11.glEnable(GL11.GL_ALPHA_TEST);
	RenderHelper.bindTexture(woodPropellerIcon);
	RenderHelper.renderSquare(10, 26, 28, 12, 0, 0, false);
	RenderHelper.bindTexture(ironPropellerIcon);
	RenderHelper.renderSquare(40, 56, 28, 12, 0, 0, false);
	RenderHelper.bindTexture(obsidianPropellerIcon);
	RenderHelper.renderSquare(70, 86, 28, 12, 0, 0, false);

	if(propType == 1){
		RenderHelper.bindTexture(woodBlockIcon);
		RenderHelper.renderSquare(15, 31, 64, 48, 0, 0, false);
	}else if(propType == 2){
		RenderHelper.bindTexture(ironIngotIcon);
		RenderHelper.renderSquare(15, 31, 64, 48, 0, 0, false);
	}else if(propType == 3){
		RenderHelper.bindTexture(obsidianBlockIcon);
		RenderHelper.renderSquare(15, 31, 64, 48, 0, 0, false);
	}
}

@Override
    public void drawScreen(int mouseX, int mouseY, float renderPartialTicks){
	super.drawScreen(mouseX, mouseY, renderPartialTicks);
}
    
@Override
    protected void actionPerformed(GuiButton buttonClicked){
	super.actionPerformed(buttonClicked);
	if(buttonClicked.equals(tier1Button)){
		propType=1;
	}else if(buttonClicked.equals(tier2Button)){
		propType=2;
	}else if(buttonClicked.equals(tier3Button)){
		propType=3;
	}
	tier1Button.enabled = propType != 1;
	tier2Button.enabled = propType != 2;
	tier3Button.enabled = propType != 3;
}

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

 

 

ContainerPropellerBench

 

public class ContainerPropellerBench extends Container{
private final TileEntityCrafter tile;
private ItemStack[] items;

public ContainerPropellerBench(InventoryPlayer invPlayer, TileEntityCrafter tile){
	this.tile = tile;
	this.items = new ItemStack[tile.getSizeInventory()];
	//this.addSlotToContainer(new SlotItem(tile, 18, 40, 0, Items.iron_ingot));
	//this.addSlotToContainer(new SlotItem(tile, 18, 60, 1, Item.getItemFromBlock(Blocks.planks), Items.iron_ingot, Item.getItemFromBlock(Blocks.obsidian)));
	//this.addSlotToContainer(new SlotItem(tile, 18, 80, 2, Items.redstone));
	//this.addSlotToContainer(new SlotItem(tile, 18, 100, 3));
	//SlotItem dummy = new SlotItem(tile, 100, 120, 4);
	//dummy.enabled = false;
	//this.addSlotToContainer(dummy);

	for(int j=0; j<3; ++j){
            for(int k=0; k<9; ++k){
                this.addSlotToContainer(new Slot(invPlayer, k + j * 9 + 9, 8 + k * 18, 103 + j * 18 + 18*2 + 1));
            }
        }
	for (int j=0; j<9; ++j){
            this.addSlotToContainer(new Slot(invPlayer, j, 8 + j * 18, 161 + 18*2 + 1));
        }  
}

@Override
public boolean canInteractWith(EntityPlayer player) {
	return tile.isUseableByPlayer(player);
}

@Override
public void onContainerClosed(EntityPlayer player){
	super.onContainerClosed(player);
	if(!player.worldObj.isRemote){
		for(Object slot :  this.inventorySlots){
			if(((Slot) slot).inventory.equals(this)){
				if(((Slot) slot).getStack() != null){
					player.worldObj.spawnEntityInWorld(new EntityItem(player.worldObj, player.posX, player.posY, player.posZ, ((Slot) slot).getStack()));
				}
			}
		}
	}
}

@Override
public ItemStack transferStackInSlot(EntityPlayer player, int sourceSlotIndex){
	ItemStack item = getSlot(sourceSlotIndex).getStack();
	if(item != null){
		for(int i=0; i<inventorySlots.size(); ++i){
			Slot slot = (Slot) inventorySlots.get(i);
			if(slot.inventory.equals(tile)){
				if(slot.isItemValid(item) && item.stackSize != 0){
					if(slot.getHasStack()){
						if(slot.getStack().stackSize < slot.getSlotStackLimit()){
							slot.getStack().stackSize += item.splitStack(slot.getSlotStackLimit() - slot.getStack().stackSize).stackSize;
						}
					}else{
						slot.putStack(item.splitStack(slot.getSlotStackLimit()));
					}
				}
			}
		}
		if(item.stackSize == 0){
			((Slot) inventorySlots.get(sourceSlotIndex)).putStack(null);
		}
	}
	return null;		
}
}

 

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.