Jump to content

Recommended Posts

Posted

I have looked into this quite a lot and all the usual errors are not present in my code.

Whole creating a new container and shift-clicking items in or out of the container, the items will leave a ghost item behind. After looking around its mostly the slotObject.onSlotChanged(); area that needs to be added or edited however i already have that code as it should be (i think).

 

Container.class

 

 

 

package OreManager.container;

 

import OreManager.block.TileEntityOreModel;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.inventory.Container;

import net.minecraft.inventory.Slot;

import net.minecraft.item.ItemStack;

 

public class ChangeContainer extends Container {

 

@Override

public boolean canInteractWith(EntityPlayer entityplayer) {

return TileEntity.isUseableByPlayer(entityplayer);

}

 

protected TileEntityOreModel TileEntity;

 

 

public ChangeContainer(InventoryPlayer inventoryPlayer, TileEntityOreModel TE) {

TileEntity = TE;

 

 

for(int i = 0; i < 1; i++) {

for(int j = 0; j < 1; j++) {

addSlotToContainer(new Slot(TileEntity, j + i * 3, 25 + j * 18, 18 + i * 18));

 

}

}

for(int i = 0; i < 1; i++) {

for(int j = 0; j < 1; j++) {

addSlotToContainer(new Slot(TileEntity, j + i * 3, 76 + j * 18, 18 + i * 18));

 

}

}

bindPlayerInventory(inventoryPlayer);

 

}

 

private 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, 8 + j * 18, 84 + i * 18));

}

}

for(int i = 0; i < 9; i++) {

addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));

}

 

}

 

 

@Override

public ItemStack transferStackInSlot(EntityPlayer player, int par2) {

 

 

 

ItemStack itemstack = null;

Slot slotObject = (Slot)inventorySlots.get(par2);

Slot inSlot = (Slot) this.inventorySlots.get(0);

Slot outSlot = (Slot) this.inventorySlots.get(1);

if(slotObject != null && slotObject.getHasStack()) {

ItemStack stackInSlot = slotObject.getStack();

itemstack = stackInSlot.copy();

 

if (par2 < 2) {

if(!this.mergeItemStack(stackInSlot, 2, 35, true)) {

return null;

 

}

slotObject.onSlotChange(stackInSlot, itemstack);

}

else if(!this.mergeItemStack(stackInSlot, 0, 2, false)){

return null;

}

if (!this.mergeItemStack(stackInSlot, 31, 40, false)) {

return null;

}

else if (par2 >= 31 && par2 < 40

&& !this.mergeItemStack(stackInSlot, 4, 31, false)) {

return null;

}

else if (!this.mergeItemStack(stackInSlot, 4, 40, false)) {

return null;

}

 

if(stackInSlot.stackSize == 0) {

slotObject.putStack((ItemStack)null);

 

}

else {

slotObject.onSlotChanged();

}

 

if(stackInSlot.stackSize == itemstack.stackSize) {

return null;

}

slotObject.onPickupFromSlot(player, stackInSlot);

}

return itemstack;

 

}

}

 

 

 

any other information can be provided on request.

I am still learning java so please show examples if necessary. I don't like to copy paste code, i like to know what the code does and how it does it.

 

(This is an old mod i plan to update to 1.7.10 once i finished this section of it)

 

Posted

I thought it was a pretty basic class so i haven't looked much into this one.

 

 

 

public class OreChangerGuiHandler implements IGuiHandler {

 

@Override

    public Object getServerGuiElement(int id, EntityPlayer player, World world,

                    int x, int y, int z) {

            TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

            if(tileEntity instanceof TileEntityOreModel){

                    return new ChangeContainer(player.inventory, (TileEntityOreModel) tileEntity);

            }

            return null;

    }

    @Override

    public Object getClientGuiElement(int id, EntityPlayer player, World world,

                    int x, int y, int z) {

            TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

            if(tileEntity instanceof TileEntityOreModel){

                    return new OreChangerGUI(player.inventory, (TileEntityOreModel) tileEntity);

            }

            return null;

 

    }

 

 

 

actually thinking about it, if this is because of a serverside - clientside issue it may have something to do with my proxy classes. Back when i first started modding having a clommon proxy and a client proxy really messed up the mod i was working on and just kept throwing out errors. removing the classes made the mod work so i paid hem no mind.

Just in case ill include my current client proxy although my common proxy is completly empty apart from the basic public class commonproxy line

 

 

 

public class ClientProxy extends CommonProxy {

 

 

public void registerOreManager() {

ClientRegistry.bindTileEntitySpecialRenderer(TileEntityOreModel.class, new RenderOreManager());

 

MinecraftForgeClient.registerItemRenderer(OreManager.blockOreChanger.blockID, new ItemRenderOreManager());

 

//LanguageRegistry.instance().addStringLocalization();

 

}

}

 

 

Posted

No, your GuiHandler looks fine, and the proxies shouldn't have anything to do with it. Are you doing anything out of the ordinary in the Gui class? For reference, this is all you need for a functional container-based gui class.

 

How about your custom IInventory class, anything funky in there?

 

Otherwise, nothing really comes to mind. You do have a lot of unused and unnecessary code in your Container, stuff like:

// using loops for one iteration:
for(int i = 0; i < 1; i++) {
  for(int j = 0; j < 1; j++) {
  }
}

// unused variables:
Slot inSlot = (Slot) this.inventorySlots.get(0);
Slot outSlot = (Slot) this.inventorySlots.get(1);

Posted

Yea there is a lot of unused or unneeded code  while I'm working on it. i generally run optimizations after it actually works so I know i can test other things if what I'm doing isn't working.

 

My GUI class

 

 

public class OreChangerGUI extends GuiContainer {

public static final ResourceLocation field_110421_t = new ResourceLocation("OreManager:textures/gui/OreChanger.png");

 

public OreChangerGUI(InventoryPlayer inventoryPlayer, TileEntityOreModel TileEntity) {

super(new ChangeContainer(inventoryPlayer, TileEntity));

 

}

 

@Override

protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) {

GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

this.mc.getTextureManager().bindTexture(field_110421_t);

int k = (this.width - this. xSize) / 2;

int y = (this.height - this.ySize) / 2;

this.drawTexturedModalRect(k, y, 0, 2, xSize, ySize);

 

}

 

//@Override

protected void drawGuiContainerForgroundLayout(int param1, int param2) {

fontRenderer.drawString("OreChangerimput", 8, 6, 4210752);

fontRenderer.drawString(StatCollector.translateToLocal("oremanager:/gui/OreChanger.png"), 8, ySize - 96 + 2, 4210752);

 

}

 

 

}

 

 

 

 

and i don't actually have a custom IInventory class. I was trying to get ISidedInventory working but this lead to some strange issues so i plan to take a look into that later. Right now my TileEntity Class just extends IInventory (net.minecraft)

Posted

I dont Have any IInventory class of my own. am I'm importing the one from net.minecraft.inventory.IInventory

 

I dont know if this will help with solving this but i have uploaded all my source code to my dropbox account so it can be downloaded quickly and easily.

[edited out] Oremanager.zip [/edited out]

Posted

Ok i added the !world.isRemote but still no change, not getting any errors and i have tried it with and without the other pieces of code in that function.

 

 

 

@Override

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int metadata, float hitx, float hity, float hitz){

if (!world.isRemote){

FMLNetworkHandler.openGui(player, OreManager.instance, OreManager.guiIdOreManagerGUI, world, x, y, z);;

}

 

TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

if(tileEntity == null || player.isSneaking()) {

return false;

}

player.openGui(OreManager.instance, 0, world, x, y, z);

return true;

}

 

 

Posted

If your TileEntity implements IInventory, then that is your custom inventory class.

 

sorry i literally just realized what you meant by this (i think).

You mean my Tile Entity class IS my IInventory class ? i feel really stupid now :)

Anyway i posted a link to a github with it all on so you can see everything i have. Please ignore any junk code i got rid of the for(int i = 0; i < 1; i++) part already as that was making another part stupid to manage.

Posted

Ok i added the !world.isRemote but still no change, not getting any errors and i have tried it with and without the other pieces of code in that function.

 

 

 

@Override

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int metadata, float hitx, float hity, float hitz){

if (!world.isRemote){

FMLNetworkHandler.openGui(player, OreManager.instance, OreManager.guiIdOreManagerGUI, world, x, y, z);;

}

 

TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

if(tileEntity == null || player.isSneaking()) {

return false;

}

player.openGui(OreManager.instance, 0, world, x, y, z);

return true;

}

 

 

 

I dont know what this code is supposed to do at all.

Just use this:

 

 
if(!world.isRemote) {
        TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
        if (tileEntity != null) player.openGui(OreManager.instance, 0, world, x, y, z);
}
        return true;

Posted

 

I dont know what this code is supposed to do at all.

Just use this:

 

 
if(!world.isRemote) {
        TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
        if (tileEntity != null) player.openGui(OreManager.instance, 0, world, x, y, z);
}
        return true;

 

still same, no change at all.

 

Dont know if this helps at all but a few related bugs are:

Shift clicking will put items into  slot2 (if slot1 is full) even though its set not to receive items (cant place them there by normal methods).

Using a hopper with more than 1 item will destroy the stack and only leave 1 item(both on input and output).

 

 

Updated github with new code https://github.com/AceScottie/OreManager

Posted

After a little bit of searching i can safely say the issue im having is with my Container class.

I have found a little code online that i will post below which dose fix the ghost item but, however creates a few of its own.

~Firstly it only accepts ores (this is what i wanted anyway but may have to be modified for others)

~Secondly I can only shift click into the GUI, i cannot shift click them out.

~Lastly The very top left player inventory slot cannot shift click into the GUI

 

There are some lines commented out of the code, these lines were causing me to crash after a little while.

 

 

 

  public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)

    {

    ItemStack var3 = null;

    Slot var4 = (Slot)this.inventorySlots.get(par2);

    if (var4 != null && var4.getHasStack())

    {

    ItemStack var5 = var4.getStack();

    var3 = var5.copy();

    if (par2 == 2)

    {

  // if (!this.mergeItemStack(var5, 3, 39, true))

  //  {

//    return null;

  //  }

    var4.onSlotChange(var5, var3);

    }

    else if (par2 != 1 && par2 != 0)

    {

    if (FurnaceRecipes.smelting().getSmeltingResult(var5) != null)

    {

    if (!this.mergeItemStack(var5, 0, 1, false))

    {

    return null;

    }

    }

    else if (TileEntityOreModel.isItemOre(var5))

    {

    if (!this.mergeItemStack(var5, 1, 2, false))

    {

    return null;

    }

    }

    else if (par2 >= 3 && par2 < 30)

    {

  //  if (!this.mergeItemStack(var5, 30, 39, false))

  // {

//    return null;

  // }

    }

    else if (par2 >= 30 && par2 < 39 && !this.mergeItemStack(var5, 3, 30, false))

    {

    return null;

    }

    }

  //  else if (!this.mergeItemStack(var5, 3, 39, false))

  //  {

//    return null;

  //  }

    if (var5.stackSize == 0)

    {

    var4.putStack((ItemStack)null);

    }

    else

    {

    var4.onSlotChanged();

    }

    if (var5.stackSize == var3.stackSize)

    {

    return null;

    }

    var4.onPickupFromSlot(par1EntityPlayer, var5);

    }

    return var3;

    }

 

 

 

I am currently in the process of comparing my original code to this one but to make sure ill put my code here

 

 

@Override

public ItemStack transferStackInSlot(EntityPlayer player, int par2) {

 

 

 

ItemStack itemstack = null;

Slot slotObject = (Slot)inventorySlots.get(par2);

Slot inSlot = (Slot) this.inventorySlots.get(0);

Slot outSlot = (Slot) this.inventorySlots.get(1);

if(slotObject != null && slotObject.getHasStack()) {

ItemStack stackInSlot = slotObject.getStack();

itemstack = stackInSlot.copy();

 

if (par2 < 2) {

if(!this.mergeItemStack(stackInSlot, 2, 35, true)) {

return null;

 

}

slotObject.onSlotChange(stackInSlot, itemstack);

}

else if(!this.mergeItemStack(stackInSlot, 0, 2, false)){

return null;

}

if (!this.mergeItemStack(stackInSlot, 31, 40, false)) {

return null;

}

else if (par2 >= 31 && par2 < 40

&& !this.mergeItemStack(stackInSlot, 4, 31, false)) {

return null;

}

else if (!this.mergeItemStack(stackInSlot, 4, 40, false)) {

return null;

}

 

if(stackInSlot.stackSize == 0) {

slotObject.putStack((ItemStack)null);

 

}

else {

slotObject.onSlotChanged();

}

 

if(stackInSlot.stackSize == itemstack.stackSize) {

return null;

}

slotObject.onPickupFromSlot(player, stackInSlot);

}

return itemstack;

 

}

 

 

Posted

[sOLVED]

 

The problem was i had too much code :)

I had some left over code after bug ficxing an Item duplication bug (i put 9 slots in a 2 slot GUI).

Fixed it now.

 

In my Container class i have removed the following lines

 

 

 

if (!this.mergeItemStack(stackInSlot, 31, 40, false)) {

return null;

}

else if (par2 >= 31 && par2 < 40

&& !this.mergeItemStack(stackInSlot, 4, 31, false)) {

return null;

}

else if (!this.mergeItemStack(stackInSlot, 4, 40, false)) {

return null;

}

else if (par2 >= 30 && par2 < 39 && !this.mergeItemStack(stackInSlot, 3, 30, false))

    {

    return null;

    }

 

 

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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