Jump to content

[Solved] [1.9] GUI Container Crashes Game When Shift+Clicking


IceMetalPunk

Recommended Posts

So I have this inventory block that shows a GUI for its inventory when right-clicked. For now, I'm essentially copying the UI of a hopper, since it needs 5 slots anyway. It all works fine if I click an item, then click again to place it in the block's inventory. But if I shift-click an item in the UI, either in the block's inventory or my own, the game crashes. I get a stack overflow that shows an infinite loop of calls to Container#retrySlotClick.

 

I tried following the code myself, but I got lost in the ambiguous variable names like slot6, itemstack11, etc.

 

Here's the container class:

 

package com.IceMetalPunk.breedingseason.gui;

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;

public class ContainerStorageChest extends Container {

public ContainerStorageChest(InventoryPlayer playerInv, IInventory blockInv) {
	int i = 51;

	// Block inventory
	for (int j = 0; j < blockInv.getSizeInventory(); ++j) {
		this.addSlotToContainer(new Slot(blockInv, j, 44 + j * 18, 20));
	}

	// Player main inventory
	for (int l = 0; l < 3; ++l) {
		for (int k = 0; k < 9; ++k) {
			this.addSlotToContainer(new Slot(playerInv, k + l * 9 + 9, 8 + k * 18, l * 18 + i));
		}
	}

	// Hotbar inventory
	for (int i1 = 0; i1 < 9; ++i1) {
		this.addSlotToContainer(new Slot(playerInv, i1, 8 + i1 * 18, 58 + i));
	}
}

@Override
public boolean canInteractWith(EntityPlayer playerIn) {
	return true;
}

}

 

Here's the GUI class:

 

package com.IceMetalPunk.breedingseason.gui;

import com.IceMetalPunk.breedingseason.BreedingSeason;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.ResourceLocation;

public class GuiStorageChest extends GuiContainer {

private ResourceLocation storageChestGUI = new ResourceLocation(
		BreedingSeason.MODID + ":textures/gui/container/storage_chest.png");
private IInventory tileEntity = null;
private InventoryPlayer playerInventory = null;

public GuiStorageChest(InventoryPlayer playerInv, IInventory blockInv) {
	super(new ContainerStorageChest(playerInv, blockInv));
	this.tileEntity = blockInv;
	this.playerInventory = playerInv;
}

@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
	GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
	mc.getTextureManager().bindTexture(storageChestGUI);
	int marginHorizontal = (width - xSize) / 2;
	int marginVertical = (height - ySize) / 2;
	drawTexturedModalRect(marginHorizontal, marginVertical, 0, 0, xSize, ySize);
}

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

	this.fontRendererObj.drawString(this.tileEntity.getDisplayName().getUnformattedText(), 8, 6, 4210752);
	this.fontRendererObj.drawString(this.playerInventory.getDisplayName().getUnformattedText(), 8,
			this.ySize - 96 + 2, 4210752);
}

}

 

And here's the tile entity class:

 

package com.IceMetalPunk.breedingseason.tileentities;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;

public class TileEntityStorageChest extends TileEntity implements IInventory {

private String customName = null;
private ItemStack[] slots = new ItemStack[5];

@Override
public String getName() {
	return this.hasCustomName() ? this.customName : "container.storage_chest";
}

@Override
public boolean hasCustomName() {
	return this.customName != null && this.customName.length() > 0;
}

public void setCustomInventoryName(String name) {
	this.customName = name;
}

@Override
public ITextComponent getDisplayName() {
	return this.hasCustomName() ? new TextComponentString(this.getName())
			: new TextComponentTranslation(this.getName());
}

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

@Override
public ItemStack getStackInSlot(int index) {
	if (index < 0 || index >= this.getSizeInventory()) {
		return null;
	} else {
		return this.slots[index];
	}
}

@Override
public ItemStack decrStackSize(int index, int count) {
	ItemStack previous = this.getStackInSlot(index);
	if (previous == null) {
		return null;
	} else if (previous.stackSize <= count) {
		this.setInventorySlotContents(index, null);
		return previous;
	} else {
		ItemStack split = previous.splitStack(count);
		if (previous.stackSize <= 0) {
			this.setInventorySlotContents(index, null);
		}
		return split;
	}
}

@Override
public ItemStack removeStackFromSlot(int index) {
	ItemStack previous = this.getStackInSlot(index);
	this.setInventorySlotContents(index, null);
	return previous;
}

@Override
public void setInventorySlotContents(int index, ItemStack stack) {
	if (index >= 0 && index < this.getSizeInventory()) {
		this.slots[index] = stack;
	}
}

@Override
public int getInventoryStackLimit() {
	return 64; // TODO: Increase limit if possible to 2048 (32 stacks)
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	return true;
}

@Override
public void openInventory(EntityPlayer player) {
}

@Override
public void closeInventory(EntityPlayer player) {
}

@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
	return true;
}

@Override
public int getField(int id) {
	return 0;
}

@Override
public void setField(int id, int value) {
}

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

@Override
public void clear() {
	for (int i = 0; i < this.slots.length; ++i) {
		this.removeStackFromSlot(i);
	}
}

}

 

Can anyone help me get this working so that shift+clicking doesn't...you know...crash the game? Thanks.

Whatever Minecraft needs, it is most likely not yet another tool tier.

Link to comment
Share on other sites

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.