Jump to content

[SOLVED] Can't make "mayPlace" function of Slot work properly


Gapherd

Recommended Posts

I'm trying to make a new type of Dispenser, inheriting the Dropper's properties. Firstly I want the Dispenser's menu slots to permit just the Bucket item, but when the player puts another item on the slot it will first receive the item and remove it afterward, causing a flickery effect on the slots as it's shown on the gif bellow:

https://giphy.com/gifs/dy2NpO3WuKqGM1FvtM

Bellow I'm printing the relevant classes:

public class EjectorBlock extends DropperBlock {
    public EjectorBlock(Properties properties) {
        super(properties);
    }

    @Override
    public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {

        return new EjectorBlockEntity(blockPos, blockState);

    }

    @Override
    public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand interactionHand, BlockHitResult blockHitResult) {

        if (!level.isClientSide()) {

            BlockEntity blockEntity = level.getBlockEntity(blockPos);
            if (blockEntity instanceof EjectorBlockEntity) {

                player.openMenu((EjectorBlockEntity) blockEntity);

            }

        }

        return InteractionResult.sidedSuccess(level.isClientSide);

    }

}
public class EjectorBlockEntity extends DispenserBlockEntity {

    public EjectorBlockEntity(BlockPos blockPos, BlockState blockState) {

        super(ModBlockEntities.EJECTOR_BLOCK_ENTITY.get(), blockPos, blockState);

    }

    @Override
    protected Component getDefaultName() {

        return Component.literal("Ejector");

    }

    @Override
    protected AbstractContainerMenu createMenu(int id, Inventory inventory) {
        return new EjectorMenu(id, inventory, this);
    }
}
public class EjectorMenu extends DispenserMenu {

    private class BucketSlot extends Slot {

        public BucketSlot(Container container, int slot, int x, int y) {
            super(container, slot, x, y);
        }

        @Override
        public boolean mayPlace(ItemStack p_40231_) {

            return Items.BUCKET.equals(p_40231_.getItem());
            
        }

    }

    public EjectorMenu(int id, Inventory inventory, Container container) {

        super(id, inventory, container);

    }

    @Override
    protected Slot addSlot(Slot slot) {

        if (this.slots.size() < 9) {

            return super.addSlot(new BucketSlot(slot.container, slot.getSlotIndex(), slot.x, slot.y));

        }

        return super.addSlot(slot);

    }

}

 

Edited by Gapherd
The issue was solved
Link to comment
Share on other sites

  • Gapherd changed the title to Can't make "mayPlace" function of Slot work properly
2 hours ago, ChampionAsh5357 said:

Actually, the ClientSide was calling the DispenserMenu superclass instead of my implementation. So, the ClientSide would permit to move the item to the slot, and then in the SeverSide it would block. Causing the flickery effect.

I wrote a new Menu and Screen class for my Ejector, without inheriting DispenserMenu, registered them and it worked.
 

Thanks for the help.

Link to comment
Share on other sites

  • Gapherd changed the title to [SOLVED] Can't make "mayPlace" function of Slot work properly

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.