Search in sources :

Example 1 with DyeableItem

use of net.minecraft.item.DyeableItem in project carpet-extra by gnembon.

the class CauldronWaterDispenserBehavior method dispenseSilently.

@Override
protected ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) {
    this.setSuccess(true);
    Item item = stack.getItem();
    ServerWorld world = pointer.getWorld();
    BlockPos frontBlockPos = pointer.getPos().offset(pointer.getBlockState().get(DispenserBlock.FACING));
    BlockState frontBlockState = world.getBlockState(frontBlockPos);
    Block frontBlock = frontBlockState.getBlock();
    if (frontBlock == Blocks.WATER_CAULDRON) {
        if (item == Items.POTION && PotionUtil.getPotion(stack) == Potions.WATER) {
            // check if cauldron is not full
            if (!((AbstractCauldronBlock) frontBlock).isFull(frontBlockState)) {
                // increase cauldron level
                int level = frontBlockState.get(LeveledCauldronBlock.LEVEL);
                BlockState cauldronState = frontBlockState.with(LeveledCauldronBlock.LEVEL, level + 1);
                setCauldron(world, frontBlockPos, cauldronState, SoundEvents.ITEM_BOTTLE_EMPTY, GameEvent.FLUID_PLACE);
                // return glass bottle
                return this.addOrDispense(pointer, stack, new ItemStack(Items.GLASS_BOTTLE));
            }
        } else if (item == Items.GLASS_BOTTLE) {
            // decrease cauldron level
            LeveledCauldronBlock.decrementFluidLevel(frontBlockState, world, frontBlockPos);
            // return water bottle
            return this.addOrDispense(pointer, stack, PotionUtil.setPotion(new ItemStack(Items.POTION), Potions.WATER));
        } else if (Block.getBlockFromItem(item) instanceof ShulkerBoxBlock) {
            // make sure item isn't plain shulker box
            if (item != Items.SHULKER_BOX) {
                // decrease cauldron level
                LeveledCauldronBlock.decrementFluidLevel(frontBlockState, world, frontBlockPos);
                // turn dyed shulker box into undyed shulker box
                ItemStack undyedShulkerBox = new ItemStack(Items.SHULKER_BOX);
                if (stack.hasNbt()) {
                    undyedShulkerBox.setNbt(stack.getNbt().copy());
                }
                // return undyed shulker box
                return this.addOrDispense(pointer, stack, undyedShulkerBox);
            }
        }
        if (item instanceof DyeableItem) {
            DyeableItem dyeableItem = (DyeableItem) item;
            // check if dyeable item has color
            if (dyeableItem.hasColor(stack)) {
                // decrease cauldron level
                LeveledCauldronBlock.decrementFluidLevel(frontBlockState, world, frontBlockPos);
                // remove color
                dyeableItem.removeColor(stack);
                // return undyed item
                return stack;
            }
        } else if (item instanceof BannerItem) {
            // checks if banner has layers
            if (BannerBlockEntity.getPatternCount(stack) > 0) {
                // decrease cauldron level
                LeveledCauldronBlock.decrementFluidLevel(frontBlockState, world, frontBlockPos);
                // copy banner stack, set to one item
                ItemStack cleanedBanner = stack.copy();
                cleanedBanner.setCount(1);
                // removes layer from banner (yarn name is misleading)
                BannerBlockEntity.loadFromItemStack(cleanedBanner);
                // return cleaned banner
                return this.addOrDispense(pointer, stack, cleanedBanner);
            }
        }
    } else if (frontBlock == Blocks.CAULDRON && item == Items.POTION && PotionUtil.getPotion(stack) == Potions.WATER) {
        // increase cauldron level
        BlockState cauldronState = Blocks.WATER_CAULDRON.getDefaultState();
        setCauldron(world, frontBlockPos, cauldronState, SoundEvents.ITEM_BOTTLE_EMPTY, GameEvent.FLUID_PLACE);
        // return glass bottle
        return this.addOrDispense(pointer, stack, new ItemStack(Items.GLASS_BOTTLE));
    }
    // fail to dispense
    this.setSuccess(false);
    return stack;
}
Also used : ServerWorld(net.minecraft.server.world.ServerWorld) BannerItem(net.minecraft.item.BannerItem) Item(net.minecraft.item.Item) DyeableItem(net.minecraft.item.DyeableItem) BannerItem(net.minecraft.item.BannerItem) BlockState(net.minecraft.block.BlockState) DyeableItem(net.minecraft.item.DyeableItem) ShulkerBoxBlock(net.minecraft.block.ShulkerBoxBlock) AbstractCauldronBlock(net.minecraft.block.AbstractCauldronBlock) LeveledCauldronBlock(net.minecraft.block.LeveledCauldronBlock) Block(net.minecraft.block.Block) ShulkerBoxBlock(net.minecraft.block.ShulkerBoxBlock) DispenserBlock(net.minecraft.block.DispenserBlock) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Aggregations

AbstractCauldronBlock (net.minecraft.block.AbstractCauldronBlock)1 Block (net.minecraft.block.Block)1 BlockState (net.minecraft.block.BlockState)1 DispenserBlock (net.minecraft.block.DispenserBlock)1 LeveledCauldronBlock (net.minecraft.block.LeveledCauldronBlock)1 ShulkerBoxBlock (net.minecraft.block.ShulkerBoxBlock)1 BannerItem (net.minecraft.item.BannerItem)1 DyeableItem (net.minecraft.item.DyeableItem)1 Item (net.minecraft.item.Item)1 ItemStack (net.minecraft.item.ItemStack)1 ServerWorld (net.minecraft.server.world.ServerWorld)1 BlockPos (net.minecraft.util.math.BlockPos)1