Jump to content

Recommended Posts

Posted (edited)

Hey guys, so i'm having some trouble with my tile entity inventory methods, specifically:

    @Override
    public int[] getSlotsForFace(EnumFacing side) {
        return new int[] {0, 1, 2};
    }

    @Override
    public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
        return index == 0;
    }

    @Override
    public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) {
        return index == 1 || index == 2;
    }

I'm not sure if i'm just not understanding it correctly or doing it wrong, but my goal is to allow piping in from anything to go into slot 0 which works, but i want to make it so that extraction only works on slots 1 and 2 which is what i defined in canExtractItem but that doesn't seem to work it extracts from slot 0 anyways

Edited by abused_master
Solved
Posted

im using the IInventory wrapper

    private net.minecraftforge.items.IItemHandler itemHandler;

    protected net.minecraftforge.items.IItemHandler createUnSidedHandler()
    {
        return new net.minecraftforge.items.wrapper.InvWrapper(this);
    }

    @SuppressWarnings("unchecked")
    @Override
    @javax.annotation.Nullable
    public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, @javax.annotation.Nullable net.minecraft.util.EnumFacing facing)
    {
        if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
            return (T) (itemHandler == null ? (itemHandler = createUnSidedHandler()) : itemHandler);
        return super.getCapability(capability, facing);
    }

    @Override
    public boolean hasCapability(net.minecraftforge.common.capabilities.Capability<?> capability, @javax.annotation.Nullable net.minecraft.util.EnumFacing facing)
    {
        return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
    }

 

Posted (edited)
33 minutes ago, diesieben07 said:

No, you should not be implementing IInventory.

Ok, fine, switching to IItemHandler, so i have a question

in my container i was previously using SlotFurnaceOutput, do i just add SlotItemHandler instead? how do i specify its an output only slot not input?

this is my new code did i convert correctly?
 

    public static final int SIZE = 4;

    private ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE) {
        @Override
        protected void onContentsChanged(int slot) {
            TileEntityCrusher.this.markDirty();
        }
    };

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        if (nbt.hasKey("items")) {
            itemStackHandler.deserializeNBT((NBTTagCompound) nbt.getTag("items"));
        }

        this.cookTime = nbt.getInteger("CookTime");
        this.totalCookTime = nbt.getInteger("TotalCookTime");
        storage.readFromNBT(nbt);
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
        super.writeToNBT(nbt);
        nbt.setTag("items", itemStackHandler.serializeNBT());
        nbt.setInteger("cookTime", (short)this.cookTime);
        nbt.setInteger("TotalCookTime", (short)this.totalCookTime);
        return storage.writeToNBT(nbt);
    }

    public boolean canInteractWith(EntityPlayer playerIn) {
        return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
    }

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

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

 

Container:

    private TileEntityCrusher tileCrusher;

    public ContainerPulverizer(IInventory playerInv, TileEntityCrusher te) {
        super(playerInv, te);
        this.tileCrusher = te;
        IItemHandler itemHandler = this.tileCrusher.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        //input
        addSlotToContainer(new SlotItemHandler(itemHandler, 0, 56, 26));
        //output 1 & 2
        addSlotToContainer(new SlotItemHandler(itemHandler, 1, 116, 15));
        addSlotToContainer(new SlotItemHandler(itemHandler, 2, 116, 36));
        //upgrade slot
        addSlotToContainer(new SlotItemHandler(itemHandler, 3, 8, 58));

        /*
        addSlotToContainer(new Slot(iinventory, 0, 56, 26));
        addSlotToContainer(new SlotFurnaceOutput(playerInv.player, iinventory, 1, 116, 15));
        addSlotToContainer(new SlotFurnaceOutput(playerInv.player, iinventory, 2, 116, 36));
        addSlotToContainer(new Slot(iinventory, 3, 8, 58));
        */
    }

and then how would i specify piping in/out from pipes/hopper?

Edited by abused_master
typo
Posted
44 minutes ago, abused_master said:

Ok, fine, switching to IItemHandler, so i have a question

in my container i was previously using SlotFurnaceOutput, do i just add SlotItemHandler instead? how do i specify its an output only slot not input?

this is my new code did i convert correctly?

You subclass SlotItemHandler.

For example, one of these two:

https://github.com/Draco18s/ReasonableRealism/tree/master/src/main/java/com/draco18s/hardlib/api/internal/inventory

 

Note that SlotOutput will not allow any item to be inserted what so ever while OutputItemStackHandler is a wrapper around another handler that prevents input through the wrapper.

  • Like 1

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.

Posted (edited)
2 minutes ago, Draco18s said:

You subclass SlotItemHandler.

For example, one of these two:

https://github.com/Draco18s/ReasonableRealism/tree/master/src/main/java/com/draco18s/hardlib/api/internal/inventory

 

Note that SlotOutput will not allow any item to be inserted what so ever while OutputItemStackHandler is a wrapper around another handler that prevents input through the wrapper.

while doing a bit of digging through i was able to come up with this that works fine:

        addSlotToContainer(new SlotItemHandler(itemHandler, 1, 116, 15) {
            @Override
            public boolean isItemValid(@Nonnull ItemStack stack) {
                return false;
            }
        });
        addSlotToContainer(new SlotItemHandler(itemHandler, 2, 116, 36) {
            @Override
            public boolean isItemValid(@Nonnull ItemStack stack) {
                return false;
            }
        });

but subclassing it would be much neater so i might just go with that

Edited by abused_master
Posted (edited)
8 minutes ago, abused_master said:

so now with that done and tested everything seems to work just fine, but going back to the topic what would i use instead of canExtract/getSlotsForFace/canInsert?

Those are IInventory methods.

You don't need them.  The IItemHandler handles that. You return a different handler based on the side accessed in getCapability

Edited by Draco18s

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.

Posted
24 minutes ago, Draco18s said:

Those are IInventory methods.

You don't need them.  The IItemHandler handles that. You return a different handler based on the side accessed in getCapability

what type of handlers are available to use in such a fashion?

Posted

All right i see, but i was wondering, how would i prevent extraction from pipes completely in a certain slot, say my upgrade slot, i dont mind an upgrade being piped in but i dont want it to accidentally get piped out while exporting my outputs

Posted

Or...just not expose the capability on that side.

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.

Posted
On 4/22/2017 at 5:03 PM, diesieben07 said:

You would override extractItem in the IItemHandler

So i did that and denied extraction and then tried to ignore it in the slot impl but i could only extract on shift click and couldn't figure out why normal click doesn't extract.

Slot Class:

public class SlotInput extends SlotItemHandler {

    private final ItemStackHandlerCustom handler;

    public SlotInput(ItemStackHandlerCustom handler, int index, int xPosition, int yPosition) {
        super(handler, index, xPosition, yPosition);
        this.handler = handler;
    }

    @Override
    public boolean isItemValid(ItemStack stack) {
        return !stack.isEmpty();
    }

    @Override
    @Nonnull
    public ItemStack getStack(){
        return this.handler.getStackInSlot(this.getSlotIndex());
    }

    @Override
    public void putStack(ItemStack stack){
        this.handler.setStackInSlot(this.getSlotIndex(), stack);
        this.onSlotChanged();
    }

    @Override
    public int getItemStackLimit(ItemStack stack) {
        return stack.getMaxStackSize();
    }

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

    @Override
    public ItemStack decrStackSize(int amount) {
        return this.handler.extractItemInternal(this.getSlotIndex(), amount, false);
    }
}

 

Posted
On 4/22/2017 at 9:26 AM, Draco18s said:

You subclass SlotItemHandler.

For example, one of these two:

https://github.com/Draco18s/ReasonableRealism/tree/master/src/main/java/com/draco18s/hardlib/api/internal/inventory

 

Note that SlotOutput will not allow any item to be inserted what so ever while OutputItemStackHandler is a wrapper around another handler that prevents input through the wrapper.

Oh look. That thing.

  • Like 2

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.

Posted
On 4/27/2017 at 2:06 AM, diesieben07 said:

You should have two different handlers. One that is the "internal" handler that allows extraction and insertion with the normal methods, you use that one for the slot, accessing it without using getCapability. Then in getCapability you expose a wrapped handler that denies the operations you want.

 

On 4/27/2017 at 5:56 AM, Draco18s said:

Oh look. That thing.

Thanks guys, was able to figure it out :D

Now i have another question, this one deals with custom upgrades.

I want my Crusher to allow speed upgrades and speed up the cook time. The problem is i dont know what the best way to do it is.

I have thought about creating a method and checking each upgrade and changing the total cook time based on that, thought of incrementing the cook time by 2, 4, and 8 based on the upgrade, but i'd like to hear what the best way you guys think it should be done.

Posted

Pretty much works out the same.  Total cook time gives you more flexibility and the incremental way needs to look up what upgrade it has every tick.

But neither of those are terribly huge concerns.

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.

Posted (edited)
22 minutes ago, Draco18s said:

Pretty much works out the same.  Total cook time gives you more flexibility and the incremental way needs to look up what upgrade it has every tick.

But neither of those are terribly huge concerns.

So while testing with the total cook time i ran into a problem,

        if(inventory.getStackInSlot(3).getItem() == new ItemStack(ModItems.ItemUpgrades, 1, 1).getItem()) {
            return 120;
        }

that's for my speed upgrade 1, the problem is if i do the same for all others it only uses 1 of the returned values from all 3 if(), however when i switch the items to gold, diamond, and iron, it works fine, i'm guessing this is an issue with meta, anyone know how to solve this?

Edited by abused_master
Posted

if(item == item) { } never checks metadata. You in fact, wrap an item and metadata into an item stack only to pull the item back out, discarding the metadata.

        if(inventory.getStackInSlot(3).getItem() == ModItems.ItemUpgrades && inventory.getStackInSlot(3).getMetadata() == 1) {
            return 120;
        }

 

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.

Posted
1 minute ago, Draco18s said:

if(item == item) { } never checks metadata. You in fact, wrap an item and metadata into an item stack only to pull the item back out, discarding the metadata.


        if(inventory.getStackInSlot(3).getItem() == ModItems.ItemUpgrades && inventory.getStackInSlot(3).getMetadata() == 1) {
            return 120;
        }

 

oh i see, didn't realize this, thanks was able to get everything done :D

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.