Search in sources :

Example 1 with AbstractCauldronBlock

use of net.minecraft.block.AbstractCauldronBlock in project carpet-extra by gnembon.

the class CauldronFillingDispenserBehavior 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 instanceof AbstractCauldronBlock) {
        // lava
        if (item == Items.LAVA_BUCKET) {
            BlockState cauldronState = Blocks.LAVA_CAULDRON.getDefaultState();
            setCauldron(world, frontBlockPos, cauldronState, SoundEvents.ITEM_BUCKET_EMPTY_LAVA);
            return new ItemStack(Items.BUCKET);
        } else // water
        if (item == Items.WATER_BUCKET) {
            BlockState cauldronState = Blocks.WATER_CAULDRON.getDefaultState().with(LeveledCauldronBlock.LEVEL, 3);
            setCauldron(world, frontBlockPos, cauldronState, SoundEvents.ITEM_BUCKET_EMPTY);
            return new ItemStack(Items.BUCKET);
        } else // powder snow
        if (item == Items.POWDER_SNOW_BUCKET) {
            BlockState cauldronState = Blocks.POWDER_SNOW_CAULDRON.getDefaultState().with(LeveledCauldronBlock.LEVEL, 3);
            setCauldron(world, frontBlockPos, cauldronState, SoundEvents.ITEM_BUCKET_EMPTY_POWDER_SNOW);
            return new ItemStack(Items.BUCKET);
        }
    }
    // fail to dispense
    this.setSuccess(false);
    return stack;
}
Also used : ServerWorld(net.minecraft.server.world.ServerWorld) Item(net.minecraft.item.Item) BlockState(net.minecraft.block.BlockState) AbstractCauldronBlock(net.minecraft.block.AbstractCauldronBlock) AbstractCauldronBlock(net.minecraft.block.AbstractCauldronBlock) LeveledCauldronBlock(net.minecraft.block.LeveledCauldronBlock) Block(net.minecraft.block.Block) DispenserBlock(net.minecraft.block.DispenserBlock) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Example 2 with AbstractCauldronBlock

use of net.minecraft.block.AbstractCauldronBlock 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)

Example 3 with AbstractCauldronBlock

use of net.minecraft.block.AbstractCauldronBlock in project carpet-extra by gnembon.

the class CarpetExtraDispenserBehaviors method getCustomDispenserBehavior.

// get custom dispenser behavior
// this checks conditions such as the item and certain block or entity being in front of the dispenser to decide which rule to return
// if the conditions for the rule match, it returns the instance of the dispenser behavior
// returns null to fallback to vanilla (or another mod's) behavior for the given item
public static DispenserBehavior getCustomDispenserBehavior(ServerWorld world, BlockPos pos, BlockPointer pointer, DispenserBlockEntity dispenserBlockEntity, ItemStack stack, Map<Item, DispenserBehavior> VANILLA_BEHAVIORS) {
    Item item = stack.getItem();
    Direction dispenserFacing = pointer.getBlockState().get(DispenserBlock.FACING);
    BlockPos frontBlockPos = pos.offset(dispenserFacing);
    BlockState frontBlockState = world.getBlockState(frontBlockPos);
    Block frontBlock = frontBlockState.getBlock();
    Box frontBlockBox = new Box(frontBlockPos);
    // blazeMeal
    if (CarpetExtraSettings.blazeMeal && item == Items.BLAZE_POWDER && frontBlock == Blocks.NETHER_WART) {
        return BLAZE_MEAL;
    }
    // chickenShearing
    if (CarpetExtraSettings.chickenShearing && item == Items.SHEARS) {
        boolean hasShearableChickens = !world.getEntitiesByType(EntityType.CHICKEN, frontBlockBox, EntityPredicates.VALID_LIVING_ENTITY.and((chickenEntity) -> {
            return !((AnimalEntity) chickenEntity).isBaby();
        })).isEmpty();
        if (hasShearableChickens) {
            return SHEAR_CHICKEN;
        }
    }
    // dispensersCarvePumpkins
    if (CarpetExtraSettings.dispensersCarvePumpkins && item instanceof ShearsItem && frontBlock == Blocks.PUMPKIN) {
        return CARVE_PUMPKIN;
    }
    // dispensersFeedAnimals
    if (CarpetExtraSettings.dispensersFeedAnimals) {
        // check for animals that can be bred with the current item being dispensed in front of dispenser
        boolean hasFeedableAnimals = !world.getEntitiesByClass(AnimalEntity.class, frontBlockBox, EntityPredicates.VALID_LIVING_ENTITY.and((animalEntity) -> {
            return ((AnimalEntity) animalEntity).isBreedingItem(stack);
        })).isEmpty();
        if (hasFeedableAnimals) {
            return FEED_ANIMAL;
        }
        // get brown mooshrooms in front of dispenser
        boolean hasFeedableMooshrooms = !world.getEntitiesByType(EntityType.MOOSHROOM, frontBlockBox, EntityPredicates.VALID_LIVING_ENTITY.and((mooshroomEntity) -> {
            return ((MooshroomEntity) mooshroomEntity).getMooshroomType() == MooshroomEntity.Type.BROWN;
        })).isEmpty();
        // check if item is a small flower
        if (hasFeedableMooshrooms && item.getRegistryEntry().isIn(ItemTags.SMALL_FLOWERS)) {
            return FEED_MOOSHROOM;
        }
    }
    // dispensersFillMinecarts
    if (CarpetExtraSettings.dispensersFillMinecarts) {
        // check for minecarts with no riders in front of dispenser
        boolean hasMinecarts = !world.getEntitiesByType(EntityType.MINECART, frontBlockBox, EntityPredicates.NOT_MOUNTED).isEmpty();
        // if a minecart exist, return dispenser behavior according to item type
        if (hasMinecarts) {
            if (item == Items.CHEST) {
                return FILL_MINECART_CHEST;
            } else if (item == Items.FURNACE) {
                return FILL_MINECART_FURNACE;
            } else if (item == Items.TNT) {
                return FILL_MINECART_TNT;
            } else if (item == Items.HOPPER) {
                return FILL_MINECART_HOPPER;
            }
        }
    }
    // dispensersMilkAnimals
    if (CarpetExtraSettings.dispensersMilkAnimals) {
        // bucket to milk
        if (item == Items.BUCKET) {
            // check for cows, mooshrooms, or goats in front of dispenser
            boolean hasMilkable = !world.getEntitiesByClass(AnimalEntity.class, frontBlockBox, EntityPredicates.VALID_LIVING_ENTITY.and((animalEntity) -> {
                return animalEntity instanceof CowEntity || animalEntity instanceof GoatEntity;
            })).isEmpty();
            if (hasMilkable) {
                return MILK_ANIMAL;
            }
        } else // bowl to stew
        if (item == Items.BOWL) {
            // check for mooshrooms in front of dispenser
            boolean hasMooshroom = !world.getEntitiesByType(EntityType.MOOSHROOM, frontBlockBox, EntityPredicates.VALID_LIVING_ENTITY).isEmpty();
            if (hasMooshroom) {
                return MILK_MOOSHROOM;
            }
        }
    }
    // dispensersPlayRecords
    if (CarpetExtraSettings.dispensersPlayRecords && item instanceof MusicDiscItem && frontBlock == Blocks.JUKEBOX) {
        return PLAY_DISC;
    }
    // dispensersPotPlants
    if (CarpetExtraSettings.dispensersPotPlants && frontBlock instanceof FlowerPotBlock && FlowerPotHelper.isPottable(item)) {
        return FILL_FLOWER_POT;
    }
    // dispensersStripBlocks
    if (CarpetExtraSettings.dispensersStripBlocks && item instanceof AxeItem && (StripBlocksDispenserBehavior.canStrip(frontBlock) || StripBlocksDispenserBehavior.isStripResult(frontBlock))) {
        return STRIP_BLOCK;
    }
    // dispensersTillSoil
    if (CarpetExtraSettings.dispensersTillSoil && item instanceof HoeItem) {
        // check block in front of dispenser and one block down
        for (int i = 0; i < 2; i++) {
            BlockPos hoeBlockPos = frontBlockPos.down(i);
            Block hoeBlock = world.getBlockState(hoeBlockPos).getBlock();
            // check if block is in tilled blocks, or is farmland (to prevent hoe being dispensed when you don't want it to)
            if (TillSoilDispenserBehavior.TILLED_BLOCKS.contains(hoeBlock) || hoeBlock == Blocks.FARMLAND) {
                return TILL_SOIL;
            }
        }
    }
    // dispensersToggleThings
    if (CarpetExtraSettings.dispensersToggleThings && item == Items.STICK && ToggleBlockDispenserBehavior.TOGGLEABLE_BLOCKS.contains(frontBlock)) {
        return TOGGLE_BLOCK;
    }
    // dispensersUseCauldrons
    if (CarpetExtraSettings.dispensersUseCauldrons && frontBlock instanceof AbstractCauldronBlock) {
        // empty cauldron
        if (item == Items.BUCKET) {
            return CAULDRON_EMPTYING_BUCKET;
        } else // fill cauldron
        if (item == Items.LAVA_BUCKET || item == Items.WATER_BUCKET || item == Items.POWDER_SNOW_BUCKET) {
            return CAULDRON_FILLING_BUCKET;
        } else // water cauldron behaviors (leather armor, shulker boxes, banners)
        if (CauldronWaterDispenserBehavior.isWaterCauldronItem(stack)) {
            return CAULDRON_WATER;
        }
    }
    // renewableEndstone
    if (CarpetExtraSettings.renewableEndstone && item == Items.DRAGON_BREATH && frontBlock == Blocks.COBBLESTONE) {
        return DRAGON_BREATH_ENDSTONE;
    }
    // renewableNetherrack
    if (CarpetExtraSettings.renewableNetherrack && item == Items.FIRE_CHARGE && frontBlock == Blocks.COBBLESTONE) {
        return FIRE_CHARGE_NETHERRACK;
    }
    // no custom behavior, return null
    return null;
}
Also used : EntityType(net.minecraft.entity.EntityType) ItemTags(net.minecraft.tag.ItemTags) TillSoilDispenserBehavior(carpetextra.dispenser.behaviors.TillSoilDispenserBehavior) CauldronFillingDispenserBehavior(carpetextra.dispenser.behaviors.CauldronFillingDispenserBehavior) FlowerPotBlock(net.minecraft.block.FlowerPotBlock) Item(net.minecraft.item.Item) BlockPointer(net.minecraft.util.math.BlockPointer) CarvePumpkinDispenserBehavior(carpetextra.dispenser.behaviors.CarvePumpkinDispenserBehavior) GoatEntity(net.minecraft.entity.passive.GoatEntity) MooshroomEntity(net.minecraft.entity.passive.MooshroomEntity) FillMinecartDispenserBehavior(carpetextra.dispenser.behaviors.FillMinecartDispenserBehavior) Block(net.minecraft.block.Block) MusicDiscDispenserBehavior(carpetextra.dispenser.behaviors.MusicDiscDispenserBehavior) Map(java.util.Map) DragonBreathDispenserBehavior(carpetextra.dispenser.behaviors.DragonBreathDispenserBehavior) BlockState(net.minecraft.block.BlockState) CauldronEmptyingDispenserBehavior(carpetextra.dispenser.behaviors.CauldronEmptyingDispenserBehavior) DispenserBlockEntity(net.minecraft.block.entity.DispenserBlockEntity) AxeItem(net.minecraft.item.AxeItem) FireChargeDispenserBehavior(carpetextra.dispenser.behaviors.FireChargeDispenserBehavior) DispenserBehavior(net.minecraft.block.dispenser.DispenserBehavior) StripBlocksDispenserBehavior(carpetextra.dispenser.behaviors.StripBlocksDispenserBehavior) CauldronWaterDispenserBehavior(carpetextra.dispenser.behaviors.CauldronWaterDispenserBehavior) CarpetExtraSettings(carpetextra.CarpetExtraSettings) HoeItem(net.minecraft.item.HoeItem) ServerWorld(net.minecraft.server.world.ServerWorld) CowEntity(net.minecraft.entity.passive.CowEntity) Direction(net.minecraft.util.math.Direction) ItemStack(net.minecraft.item.ItemStack) AbstractMinecartEntity(net.minecraft.entity.vehicle.AbstractMinecartEntity) EntityPredicates(net.minecraft.predicate.entity.EntityPredicates) AnimalEntity(net.minecraft.entity.passive.AnimalEntity) MilkMooshroomDispenserBehavior(carpetextra.dispenser.behaviors.MilkMooshroomDispenserBehavior) MusicDiscItem(net.minecraft.item.MusicDiscItem) ShearsItem(net.minecraft.item.ShearsItem) ToggleBlockDispenserBehavior(carpetextra.dispenser.behaviors.ToggleBlockDispenserBehavior) Box(net.minecraft.util.math.Box) MilkAnimalDispenserBehavior(carpetextra.dispenser.behaviors.MilkAnimalDispenserBehavior) BlockPos(net.minecraft.util.math.BlockPos) FlowerPotDispenserBehavior(carpetextra.dispenser.behaviors.FlowerPotDispenserBehavior) Items(net.minecraft.item.Items) AbstractCauldronBlock(net.minecraft.block.AbstractCauldronBlock) Blocks(net.minecraft.block.Blocks) FeedAnimalDispenserBehavior(carpetextra.dispenser.behaviors.FeedAnimalDispenserBehavior) FeedMooshroomDispenserBehavior(carpetextra.dispenser.behaviors.FeedMooshroomDispenserBehavior) ShearChickenDispenserBehavior(carpetextra.dispenser.behaviors.ShearChickenDispenserBehavior) FlowerPotHelper(carpetextra.helpers.FlowerPotHelper) DispenserBlock(net.minecraft.block.DispenserBlock) BlazePowderDispenserBehavior(carpetextra.dispenser.behaviors.BlazePowderDispenserBehavior) AbstractCauldronBlock(net.minecraft.block.AbstractCauldronBlock) AnimalEntity(net.minecraft.entity.passive.AnimalEntity) Box(net.minecraft.util.math.Box) ShearsItem(net.minecraft.item.ShearsItem) GoatEntity(net.minecraft.entity.passive.GoatEntity) Direction(net.minecraft.util.math.Direction) AxeItem(net.minecraft.item.AxeItem) HoeItem(net.minecraft.item.HoeItem) MusicDiscItem(net.minecraft.item.MusicDiscItem) Item(net.minecraft.item.Item) AxeItem(net.minecraft.item.AxeItem) HoeItem(net.minecraft.item.HoeItem) MusicDiscItem(net.minecraft.item.MusicDiscItem) ShearsItem(net.minecraft.item.ShearsItem) BlockState(net.minecraft.block.BlockState) FlowerPotBlock(net.minecraft.block.FlowerPotBlock) Block(net.minecraft.block.Block) AbstractCauldronBlock(net.minecraft.block.AbstractCauldronBlock) DispenserBlock(net.minecraft.block.DispenserBlock) BlockPos(net.minecraft.util.math.BlockPos) MooshroomEntity(net.minecraft.entity.passive.MooshroomEntity) CowEntity(net.minecraft.entity.passive.CowEntity) FlowerPotBlock(net.minecraft.block.FlowerPotBlock)

Example 4 with AbstractCauldronBlock

use of net.minecraft.block.AbstractCauldronBlock in project carpet-extra by gnembon.

the class CauldronEmptyingDispenserBehavior method dispenseSilently.

@Override
protected ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) {
    this.setSuccess(true);
    ServerWorld world = pointer.getWorld();
    BlockPos frontBlockPos = pointer.getPos().offset(pointer.getBlockState().get(DispenserBlock.FACING));
    BlockState frontBlockState = world.getBlockState(frontBlockPos);
    Block frontBlock = frontBlockState.getBlock();
    // check if front block is cauldron and is full
    if (frontBlock instanceof AbstractCauldronBlock && ((AbstractCauldronBlock) frontBlock).isFull(frontBlockState)) {
        // lava
        if (frontBlock == Blocks.LAVA_CAULDRON) {
            setCauldron(world, frontBlockPos, SoundEvents.ITEM_BUCKET_FILL_LAVA);
            return this.addOrDispense(pointer, stack, new ItemStack(Items.LAVA_BUCKET));
        } else // water
        if (frontBlock == Blocks.WATER_CAULDRON) {
            setCauldron(world, frontBlockPos, SoundEvents.ITEM_BUCKET_FILL);
            return this.addOrDispense(pointer, stack, new ItemStack(Items.WATER_BUCKET));
        } else // powder snow
        if (frontBlock == Blocks.POWDER_SNOW_CAULDRON) {
            setCauldron(world, frontBlockPos, SoundEvents.ITEM_BUCKET_FILL_POWDER_SNOW);
            return this.addOrDispense(pointer, stack, new ItemStack(Items.POWDER_SNOW_BUCKET));
        }
    }
    // fail to dispense
    this.setSuccess(false);
    return stack;
}
Also used : ServerWorld(net.minecraft.server.world.ServerWorld) BlockState(net.minecraft.block.BlockState) AbstractCauldronBlock(net.minecraft.block.AbstractCauldronBlock) AbstractCauldronBlock(net.minecraft.block.AbstractCauldronBlock) Block(net.minecraft.block.Block) DispenserBlock(net.minecraft.block.DispenserBlock) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Aggregations

AbstractCauldronBlock (net.minecraft.block.AbstractCauldronBlock)4 Block (net.minecraft.block.Block)4 BlockState (net.minecraft.block.BlockState)4 DispenserBlock (net.minecraft.block.DispenserBlock)4 ItemStack (net.minecraft.item.ItemStack)4 ServerWorld (net.minecraft.server.world.ServerWorld)4 BlockPos (net.minecraft.util.math.BlockPos)4 LeveledCauldronBlock (net.minecraft.block.LeveledCauldronBlock)2 Item (net.minecraft.item.Item)2 CarpetExtraSettings (carpetextra.CarpetExtraSettings)1 BlazePowderDispenserBehavior (carpetextra.dispenser.behaviors.BlazePowderDispenserBehavior)1 CarvePumpkinDispenserBehavior (carpetextra.dispenser.behaviors.CarvePumpkinDispenserBehavior)1 CauldronEmptyingDispenserBehavior (carpetextra.dispenser.behaviors.CauldronEmptyingDispenserBehavior)1 CauldronFillingDispenserBehavior (carpetextra.dispenser.behaviors.CauldronFillingDispenserBehavior)1 CauldronWaterDispenserBehavior (carpetextra.dispenser.behaviors.CauldronWaterDispenserBehavior)1 DragonBreathDispenserBehavior (carpetextra.dispenser.behaviors.DragonBreathDispenserBehavior)1 FeedAnimalDispenserBehavior (carpetextra.dispenser.behaviors.FeedAnimalDispenserBehavior)1 FeedMooshroomDispenserBehavior (carpetextra.dispenser.behaviors.FeedMooshroomDispenserBehavior)1 FillMinecartDispenserBehavior (carpetextra.dispenser.behaviors.FillMinecartDispenserBehavior)1 FireChargeDispenserBehavior (carpetextra.dispenser.behaviors.FireChargeDispenserBehavior)1