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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I have downloaded forge version 1.20.4 and I have Java 17 installed but my .jar will not open and the error log text file below is created     JVM info: Oracle Corporation - 17.0.10 - 17.0.10+11-LTS-240 java.net.preferIPv4Stack=true Current Time: 27/02/2024 19:43:19 Host: files.minecraftforge.net [51.79.83.165, 2607:5300:203:65a5:0:0:0:0] Host: maven.minecraftforge.net [51.79.83.165, 2607:5300:203:65a5:0:0:0:0] Host: libraries.minecraft.net [13.107.246.64, 13.107.213.64, 2620:1ec:46:0:0:0:0:64, 2620:1ec:bdf:0:0:0:0:64] Host: launchermeta.mojang.com [13.107.213.64, 13.107.246.64, 2620:1ec:bdf:0:0:0:0:64, 2620:1ec:46:0:0:0:0:64] Host: piston-meta.mojang.com [13.107.213.64, 13.107.246.64, 2620:1ec:46:0:0:0:0:64, 2620:1ec:bdf:0:0:0:0:64] java.net.UnknownHostException: No such host is known (authserver.mojang.com)     at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)     at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:933)     at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1543)     at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:852)     at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1532)     at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1384)     at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1305)     at net.minecraftforge.installer.DownloadUtils.getIps(DownloadUtils.java:290)     at net.minecraftforge.installer.SimpleInstaller.main(SimpleInstaller.java:120) Exception in thread "main"  java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because the return value of "net.minecraftforge.installer.DownloadUtils.getIps(String)" is null     at net.minecraftforge.installer.SimpleInstaller.main(SimpleInstaller.java:120)  
    • we tried both versions and both cant run on her laptop for some reason idk what to do  
    • Okay so we tried both versions and both cant run on her pc for some reason!  
    • Well. Make sure you installed a correct jdk for your forge mdk version. Make sure your JAVA_HOME and %PATH% environment-variables are setup correctly. (If you're on windows)   I downloaded an example forge mdk for my version and extracted the contents to a folder I'm working in Using Eclipse, I opened Eclipse and imported an existing gradle Project (with its root directory in the folder directory which I previously extracted my contents to). Waited for Eclipse to do its thing After it having finished, I ran genEclipseRuns, setupDecompWorkspace and setupDevWorkspace (Gradle Tasks > your mod name here > forgegradle)    If it still didn't work (and you're using Eclipse) run cleanEclipse and, in a command prompt  Also If I recall correctly, someone, somewhere said that the way you setup your workspace changes depending on your Minecraft Version. I did this in 1.8.9.
    • https://mclo.gs/ZlRMU4G Still crash, help 
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.