Jump to content

[1.7.10] picking something up in custom container makes me throw it on the groun


KeeganDeathman

Recommended Posts

Whoops!

package keegan.dlstuff.container;

import keegan.dlstuff.tileentity.TileEntityGravityManipulater;
import net.minecraft.entity.player.*;
import net.minecraft.inventory.*;
import net.minecraft.item.ItemStack;

public class ContainerGravity extends Container
{

private TileEntityGravityManipulater tile;
private EntityPlayer player;

public ContainerGravity(InventoryPlayer inv, TileEntityGravityManipulater tile)
{
	this.tile = tile;
	this.player = inv.player;

	this.bindPlayerInventory(inv);

	this.addSlotToContainer(new Slot(tile, 0, 202, 202));

}

protected void bindPlayerInventory(InventoryPlayer inventoryPlayer)
{
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 9; j++)
			{
				addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 1 + j * 10, 132 + i * 18));
			}
		}

		for (int i = 0; i < 9; i++)
		{
			addSlotToContainer(new Slot(inventoryPlayer, i, 1 + i * 10, 192));
		}
}

@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;
    }

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

}

[shadow=gray,left][glow=red,2,300]KEEGAN[/glow][/shadow]

Link to comment
Share on other sites

Show your Gui class. It is probably caused by the fact that the

xSize

and

ySize

are not big enough.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Tile Entity

package keegan.dlstuff.tileentity;

import keegan.dlstuff.DLStuff;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.*;
import net.minecraft.tileentity.TileEntity;

public class TileEntityGravityManipulater extends TileEntity implements IInventory
{

private int gravityModifier;
private int warpTime;

private ItemStack[] chestContents  = new ItemStack[1];

public TileEntityGravityManipulater()
{
	gravityModifier = -5;
	warpTime = 0;
}

@Override
public void writeToNBT(NBTTagCompound tagCompound)
{
	super.writeToNBT(tagCompound);
	tagCompound.setInteger("gravityManipulater", gravityModifier);
	tagCompound.setInteger("warpTime", warpTime);
	NBTTagList itemList = new NBTTagList();
        for (int i = 0; i < this.chestContents.length; i++)
        {
            ItemStack stack = this.chestContents[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);
	gravityModifier = tagCompound.getInteger("gravityModifier");
	warpTime = tagCompound.getInteger("warpTime");
	NBTTagList tagList = tagCompound.getTagList("Inventory", 10);
        for (int i = 0; i < tagList.tagCount(); i++)
        {
            NBTTagCompound tag = tagList.getCompoundTagAt(i);
            byte slot = tag.getByte("Slot");
            if (slot >= 0 && slot < this.chestContents.length)
                this.chestContents[slot] = ItemStack.loadItemStackFromNBT(tag);
        }
}

@Override
public void updateEntity()
{

	if(getStackInSlot(0) != null && getStackInSlot(0).isItemEqual(new ItemStack(DLStuff.itemWarpDriveBattery)) && warpTime == 0)
	{
		decrStackSize(0, 1);
		warpTime = 36000;
	}
	if(warpTime > 0)
	{
		for(int i = 0; i < worldObj.playerEntities.size(); i++)
		{
			EntityPlayer player = (EntityPlayer) worldObj.playerEntities.get(i);
			if(!player.capabilities.isFlying && Math.abs(player.posX - xCoord) <= 25 && Math.abs(player.posY - yCoord) <= 26 && Math.abs(player.posZ - zCoord) <= 25)
				player.addVelocity(0, getGravityModifier(), 0);
		}
		warpTime -= 1;
		if(warpTime == 0)
		{
			worldObj.spawnEntityInWorld(new EntityItem(worldObj, xCoord, yCoord+1, zCoord, new ItemStack(DLStuff.itemEmptyWarpDriveBattery)));
		}
	}
}

public int getGravityModifier()
{
	return gravityModifier;
}

public void setGravityModifier(int gravityModifier)
{
	System.out.println("Changing gravity " + gravityModifier);
	this.gravityModifier = 0 - gravityModifier;
}

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

@Override
public ItemStack getStackInSlot(int slot)
{
	return chestContents[slot];
}

@Override
public ItemStack decrStackSize(int slot, int amt) 
{
	// TODO Auto-generated method stub
	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) 
{
	// TODO Auto-generated method stub
	ItemStack stack = getStackInSlot(slot);
	if (stack != null) {
		setInventorySlotContents(slot, null);
	}
	return stack;
}

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

}

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

@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) 
{
	// TODO Auto-generated method stub
	return true;
}


@Override
public boolean isItemValidForSlot(int slot, ItemStack itemstack) {
	if(slot == 0 && (itemstack.isItemEqual(new ItemStack(DLStuff.itemWarpDriveBattery)) || itemstack.isItemEqual(new ItemStack(DLStuff.itemEmptyWarpDriveBattery))))
		return true;
	return false;
}

@Override
public String getInventoryName() {
	// TODO Auto-generated method stub
	return "Gravity";
}



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

}




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


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

}
}

 

Gui

package keegan.dlstuff.client.gui;

import keegan.dlstuff.DLStuff;
import keegan.dlstuff.container.ContainerGravity;
import keegan.dlstuff.network.PacketGravity;
import keegan.dlstuff.tileentity.TileEntityGravityManipulater;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.*;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

public class GuiGravityManipulater extends GuiContainer
{
private TileEntityGravityManipulater tile;
private EntityPlayer player;
private ResourceLocation Tex = new ResourceLocation("dlstuff:textures/gui/gravity.png");

private GuiTextField gravityModifierInput;
private String gravityModifier;

public GuiGravityManipulater(TileEntityGravityManipulater tile, InventoryPlayer inv)
{
	super(new ContainerGravity(inv, tile));
	this.tile = tile;
	gravityModifier = "";
}

@Override
public void initGui()
{
	super.initGui();
	this.gravityModifierInput = new GuiTextField(this.fontRendererObj, 200, 160, 80, 10);
	this.gravityModifierInput.setText(this.gravityModifier);
}

@Override
public void mouseClicked(int mouseX, int mouseY, int par3)
{
	super.mouseClicked(mouseX, mouseY, par3);
	this.gravityModifierInput.mouseClicked(mouseX, mouseY, par3);
}

@Override
protected void keyTyped(char par1, int par2)
{
	super.keyTyped(par1, par2);
	// Checks to see if were dealing with the console
	if (this.gravityModifierInput.isFocused())
	{
		this.gravityModifierInput.textboxKeyTyped(par1, par2);
		// Gets cmd
		this.gravityModifier = this.gravityModifierInput.getText();
	}
	if (par2 == 28)
	{
		this.gravityModifier = this.gravityModifierInput.getText();
		DLStuff.packetPipeline.sendToServer(new PacketGravity(tile.xCoord, tile.yCoord, tile.zCoord, Integer.parseInt(gravityModifier)));
		this.gravityModifierInput.setText(gravityModifier = "");
	}

	// Closes Screen
	if (par2 == 1 || par2 == this.mc.gameSettings.keyBindInventory.getKeyCode() && !this.gravityModifierInput.isFocused())
	{
		this.mc.thePlayer.closeScreen();
	}

}

@Override
public void updateScreen()
{
	this.gravityModifierInput.updateCursorCounter();
}

@Override
public void onGuiClosed()
{
	Keyboard.enableRepeatEvents(false);
}

@Override
public void drawScreen(int par1, int par2, float par3)
{
	super.drawScreen(par1, par2, par3);
	this.gravityModifierInput.drawTextBox();
}

@Override
protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_)
{
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	this.mc.renderEngine.bindTexture(Tex);
	int x = (this.width - 256) / 2;
	int y = (this.height - 256) / 2;
	this.drawTexturedModalRect(x, y, 0, 0, 256, 256);
}

}

[shadow=gray,left][glow=red,2,300]KEEGAN[/glow][/shadow]

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.