Jump to content

[Solved] [1.10.2] Container Slots eat up items


abused_master

Recommended Posts

Hey guys, so i was working on my recipemanager for my pulverizer today, and setup my slots using the vanilla slot, but whenever i try to put an item in it just eats it up, as soon as you click 1 or a stack in the slot it disappears. I have tried to create my own Slot and extend Slot but i just get the same problem

 

Container:

package abused_master.JATMA.TE.Container;

import javax.annotation.Nullable;

import abused_master.JATMA.GUI.RemoveOnlySlot;
import abused_master.JATMA.GUI.SlotValidated;
import abused_master.JATMA.GUI.SlotValidator;
import abused_master.JATMA.TE.TilePulverizer;
import abused_master.JATMA.TE.CraftingHandlers.PulverizerRecipes;
import abused_master.JATMA.TE.CraftingHandlers.RecipePulverizer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotFurnaceFuel;
import net.minecraft.inventory.SlotFurnaceOutput;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

public class PulverizerContainer extends Container {

TilePulverizer TP;

public PulverizerContainer(InventoryPlayer playerInv, TileEntity tile) {
	super();
	TP = (TilePulverizer) tile;
    
	addSlotToContainer(new Slot(TP, 0, 56, 26));
	addSlotToContainer(new Slot(TP, 1, 116, 26));

	for (int y = 0; y < 3; ++y) {
        for (int x = 0; x < 9; ++x) {
            addSlotToContainer(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
        }
    }

    for (int x = 0; x < 9; ++x) {
        addSlotToContainer(new Slot(playerInv, x, 8 + x * 18, 142));
    }
}
@Override
public boolean canInteractWith(EntityPlayer playerIn) {
	return TP.canInteractWith(playerIn);
}

    @Nullable
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
    {
        ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(index);

        if (slot != null && slot.getHasStack())
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if (index == 2)
            {
                if (!this.mergeItemStack(itemstack1, 3, 39, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (index != 1 && index != 0)
            {
                if (RecipePulverizer.instance().getPulverizingResult(itemstack1) != null)
                {
                    if (!this.mergeItemStack(itemstack1, 0, 1, false))
                    {
                        return null;
                    }
                }
                else if (index >= 3 && index < 30)
                {
                    if (!this.mergeItemStack(itemstack1, 30, 39, false))
                    {
                        return null;
                    }
                }
                else if (index >= 30 && index < 39 && !this.mergeItemStack(itemstack1, 3, 30, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 3, 39, false))
            {
                return null;
            }

            if (itemstack1.stackSize == 0)
            {
                slot.putStack((ItemStack)null);
            }
            else
            {
                slot.onSlotChanged();
            }

            if (itemstack1.stackSize == itemstack.stackSize)
            {
                return null;
            }

            slot.onPickupFromSlot(playerIn, itemstack1);
        }

        return itemstack;
    }
}

 

Link to comment
Share on other sites

so i seem to have fixed the problem, but have ended up with 2 more

#1 whenever a block/item is inserted it stays there, i cant click it out without shift clicking it

#2 when i place just 1 item/block in and shift click it dupes it

 

Updated TE Code:

 

package abused_master.JATMA.TE;

import javax.annotation.Nullable;

import abused_master.JATMA.GUI.GuiPulverizer;
import abused_master.JATMA.Registry.ModBlocks;
import abused_master.JATMA.TE.CraftingHandlers.RecipePulverizer;
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.TileEnergyHandler;
import net.minecraft.block.BlockFurnace;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.inventory.SlotFurnaceFuel;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import scala.xml.persistent.SetStorage;

public class TilePulverizer extends TileEnergyHandler implements IInventory {

protected EnergyStorage storage = new EnergyStorage(50000);
    public static final int SIZE = 2;
    private ItemStack[] pulverizerItemStacks = new ItemStack[3];

    public TilePulverizer() {
    	super();
    }


@Override
public void readFromNBT(NBTTagCompound nbt) {
	super.readFromNBT(nbt);
	storage.readFromNBT(nbt);

	if (nbt.hasKey("items")) {
            itemStackHandler.deserializeNBT((NBTTagCompound) nbt.getTag("items"));
        }
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
	super.writeToNBT(nbt);
        nbt.setTag("items", itemStackHandler.serializeNBT());
	return storage.writeToNBT(nbt);
}

@Override
public boolean canConnectEnergy(EnumFacing from) {

	return true;
}

@Override
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate) {
	return storage.receiveEnergy(maxReceive, simulate);
}

@Override
public int extractEnergy(EnumFacing from, int maxExtract, boolean simulate) {
	return 0;
}

@Override
public int getEnergyStored(EnumFacing from) {

	return storage.getEnergyStored();
}

@Override
public int getMaxEnergyStored(EnumFacing from) {

	return storage.getMaxEnergyStored();
}

    private ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE) {
        @Override
        protected void onContentsChanged(int slot) {
            TilePulverizer.this.markDirty();
        }
    };
    
    
    public boolean canInteractWith(EntityPlayer playerIn) {
        return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
    }

    @Override
    public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return true;
        }
        return super.hasCapability(capability, facing);
    }

    @Override
    public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return (T) itemStackHandler;
        }
        return super.getCapability(capability, facing);
    } 

@Override
public String getName() {
	return ModBlocks.Pulverizer.getUnlocalizedName();
}

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

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

@Override
public ItemStack getStackInSlot(int index) {
	return this.pulverizerItemStacks[index];
}

@Override
public ItemStack decrStackSize(int index, int count) {
	return null;
}

@Override
public ItemStack removeStackFromSlot(int index) {
        return null;
}

@Override
public void setInventorySlotContents(int index, ItemStack stack) {
	boolean flag = stack != null && stack.isItemEqual(this.pulverizerItemStacks[index]) && ItemStack.areItemStackTagsEqual(stack, this.pulverizerItemStacks[index]);
        this.pulverizerItemStacks[index] = stack;

        if (stack != null && stack.stackSize > this.getInventoryStackLimit())
        {
            stack.stackSize = this.getInventoryStackLimit();
        }
        
        if (index == 0 && !flag)
        {
            this.markDirty();
        }
        
}

@Override
public int getInventoryStackLimit() {
	return 64;
}

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

@Override
public void openInventory(EntityPlayer player) {

}

@Override
public void closeInventory(EntityPlayer player) {

}

@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() {

}

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

    public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
    {
        return this.isItemValidForSlot(index, itemStackIn);
    }
    
    
    
    public void update()
    {
    	boolean flag1 = false;

        if (!this.worldObj.isRemote)
        {
            if (this.pulverizerItemStacks[1] != null && this.pulverizerItemStacks[0] != null)
            {
                if (!this.canPulverize())
                {
                }

                if (this.canPulverize())
                {
                        flag1 = true;
                    }
                }
                else
                {
                }
            }

        if (flag1)
        {
            this.markDirty();
        }
    }
    
    private boolean canPulverize()
    {
        if (this.pulverizerItemStacks[0] == null)
        {
            return false;
        }
        else
        {
            ItemStack itemstack = RecipePulverizer.instance().getPulverizingResult(this.pulverizerItemStacks[0]);
            if (itemstack == null) return false;
            if (this.pulverizerItemStacks[2] == null) return true;
            if (!this.pulverizerItemStacks[2].isItemEqual(itemstack)) return false;
            int result = pulverizerItemStacks[2].stackSize + itemstack.stackSize;
            return result <= getInventoryStackLimit() && result <= this.pulverizerItemStacks[2].getMaxStackSize();
        }
    }

    
public void pulverizeItem()
    {  
	if (this.canPulverize()) {

	ItemStack itemstack = RecipePulverizer.instance().getPulverizingResult(this.pulverizerItemStacks[0]);

        if (this.pulverizerItemStacks[2] == null)
        {
            this.pulverizerItemStacks[2] = itemstack.copy();
        }
        else if (this.pulverizerItemStacks[2].getItem() == itemstack.getItem())
        {
            this.pulverizerItemStacks[2].stackSize += itemstack.stackSize;
        }
        --this.pulverizerItemStacks[0].stackSize;

        if (this.pulverizerItemStacks[0].stackSize <= 0)
        {
            this.pulverizerItemStacks[0] = null;
           }
	}
    }
}

Link to comment
Share on other sites

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

 

Nope, added it but that one doesnt even have shift click so i cant get it out at all

Show your Gui code.

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

 

Nope, added it but that one doesnt even have shift click so i cant get it out at all

Show your Gui code.

 

as requested

package abused_master.JATMA.GUI;

import abused_master.JATMA.Info;
import abused_master.JATMA.TE.TilePulverizer;
import abused_master.JATMA.TE.Container.PulverizerContainer;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ContainerFurnace;
import net.minecraft.inventory.IInventory;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.translation.I18n;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class GuiPulverizer extends GuiContainer {

private static final ResourceLocation Pulverizer = new ResourceLocation(Info.MODID, "textures/gui/Pulverizer.png");
    public static final int WIDTH = 176;
    public static final int HEIGHT = 166;

    public GuiPulverizer(TilePulverizer tileEntity, PulverizerContainer container) {
        super(container);
        xSize = WIDTH;
        ySize = HEIGHT;
    }
    
    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
        mc.getTextureManager().bindTexture(Pulverizer);
        drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
    }
    
}

Link to comment
Share on other sites

Ok your problem is that you are trying to implement two different things. You either use an IInventory or a IItemHandler. Not both. In your container you are trying to use IInventory without setting up all required methods IE getSizeInventory, decrStackSize, and removeStackFromSlot.

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.



×
×
  • Create New...

Important Information

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