Jump to content

[1.10] Capabilities


ErnstLustig

Recommended Posts

I'm still not fully understanding the new Capability system.

I know how to define what to access from which side, but what I care about is what to access depending on input or output. What I mean by that is that I might want to allow to put items in with a hopper for a specific slot, but not allow a hopper to pull the items out of that slot. How do I do that? Any examples you can point me at? I looked at a lot of code, but couldn't find the specific part where to define that.

 

Also it doesn't seem to check isItemValid if it's inserted with a hopper. So I'm not allowed to manually put items in the slot, but disallowed items land in the slot if accessed with a hopper. For reference, here is how I define the slot in the container.

 

        IItemHandler itemHandler = this.te.getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null );

        addSlotToContainer( new SlotItemHandler( itemHandler, 0, 18, 24 ){
            @Override
            public boolean isItemValid( ItemStack stack ){
                return stack.getItem() instanceof ItemDrop;
            }
        });

Link to comment
Share on other sites

Well I implemented this now and it seems to work. I tried a block where for the input only a certain item type is allowed (both manual and automatic), but it can be removed manually, but not automatically. But it seems rather hacked than a clean simple solution. I think this can be made way more simple. What do you think?

 

public class TileEntitySqueezer extends TileEntity implements ITickable {

    public static final int INPUT_SIZE = 1;
    public static final int CAPACITY = Fluid.BUCKET_VOLUME * 8;
    private FluidTank tank = new FluidTank( CAPACITY ) {
        @Override
        protected void onContentsChanged(){ TileEntitySqueezer.this.markDirty(); }
    };

    private SqueezerItemStackHandler inputStack = new SqueezerItemStackHandler( INPUT_SIZE );

    [...]

    @Override
    public boolean hasCapability( Capability<?> capability, EnumFacing facing ){
        if( capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || capability == CapabilityFluidHandler.FLUID_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 ){
            if( facing == null ){ return (T) inputStack; }
            return (T) new InsertItemStackHandler( inputStack );
        }
        if( capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY ){
            return (T) tank;
        }
        return super.getCapability( capability, facing );
    }

    public class SqueezerItemStackHandler extends ItemStackHandler{

        public SqueezerItemStackHandler(){
            super( 1 );
        }

        public SqueezerItemStackHandler( int size ){
            super( size );
        }

        @Override
        protected void onContentsChanged( int slot ){
            TileEntitySqueezer.this.markDirty();
        }

        @Override
        public ItemStack insertItem( int slot, ItemStack stack, boolean simulate ){
            if( !( stack.getItem() instanceof ItemDrop ) ){ return stack; }
            return super.insertItem( slot, stack, simulate );
        }

        public ItemStack[] getStacks(){
            return stacks;
        }
    }

    public class InsertItemStackHandler extends SqueezerItemStackHandler{

        public InsertItemStackHandler( SqueezerItemStackHandler stackhandler ){
            this.stacks = stackhandler.getStacks();
        }

        @Override
        public ItemStack extractItem( int slot, int amount, boolean simulate ){
            return null;
        }
    }

Link to comment
Share on other sites

Well I don't know how to do it better, that's why I asked. I needed two StackHandlers because I need one to be a subclass of the other with the added restriction of not extracting items, but I can't copy the StackHandler because I can't access the ItemStacks. So I need to add the method of getStacks.

 

Not sure what you mean with not using the capability at all, you mean in the Container to access the ItemStacks? But that seems like even more work to me.

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.