src/main/java/cech12/ceramicbucket/CeramicBucketMod.java
@SubscribeEvent
    public static void onEntityInteract(PlayerInteractEvent.EntityInteract event) {
        //only interact with cows and fishes
        if (event.getTarget() instanceof CowEntity) {
            CowEntity cowEntity = (CowEntity) event.getTarget();
            PlayerEntity player = event.getPlayer();
            ItemStack itemstack = player.getHeldItem(event.getHand());
            if (itemstack.getItem() == CeramicBucketItems.CERAMIC_BUCKET && !player.abilities.isCreativeMode && !cowEntity.isChild()) {
                player.playSound(SoundEvents.ENTITY_COW_MILK, 1.0F, 1.0F);
                if (!event.getWorld().isRemote()) {
                    itemstack.shrink(1);
                    if (itemstack.isEmpty()) {
                        player.setHeldItem(event.getHand(), new ItemStack(CeramicBucketItems.CERAMIC_MILK_BUCKET));
                    } else if (!player.inventory.addItemStackToInventory(new ItemStack(CeramicBucketItems.CERAMIC_MILK_BUCKET))) {
                        player.dropItem(new ItemStack(CeramicBucketItems.CERAMIC_MILK_BUCKET), false);
                    }
                }
                event.setCanceled(true);
                event.setCancellationResult(ActionResultType.SUCCESS);
            }
        } else if (event.getTarget() instanceof AbstractFishEntity) {
            AbstractFishEntity fishEntity = (AbstractFishEntity) event.getTarget();
            PlayerEntity player = event.getPlayer();
            ItemStack itemstack = player.getHeldItem(event.getHand());
            if (itemstack.getItem() == CeramicBucketItems.FILLED_CERAMIC_BUCKET && ((FilledCeramicBucketItem) itemstack.getItem()).getFluid(itemstack) == Fluids.WATER && fishEntity.isAlive()) {
                fishEntity.playSound(SoundEvents.ITEM_BUCKET_FILL_FISH, 1.0F, 1.0F);
                if (!event.getWorld().isRemote()) {
                    itemstack.shrink(1);
                    //-------------------------------------------
                    //get ceramic variant
                    ItemStack bucket;
                    if (fishEntity instanceof PufferfishEntity) {
                        bucket = ((CeramicFishBucketItem) CeramicBucketItems.PUFFERFISH_CERAMIC_BUCKET).getFilledInstance();
                    } else if (fishEntity instanceof CodEntity) {
                        bucket = ((CeramicFishBucketItem) CeramicBucketItems.COD_CERAMIC_BUCKET).getFilledInstance();
                    } else if (fishEntity instanceof TropicalFishEntity) {
                        bucket = ((CeramicFishBucketItem) CeramicBucketItems.TROPICAL_FISH_CERAMIC_BUCKET).getFilledInstance();
                    } else {
                        bucket = ((CeramicFishBucketItem) CeramicBucketItems.SALMON_CERAMIC_BUCKET).getFilledInstance();
                    }
                    //setBucketData
                    if (fishEntity.hasCustomName()) {
                        bucket.setDisplayName(fishEntity.getCustomName());
                    }
                    if (fishEntity instanceof TropicalFishEntity) {
                        CompoundNBT compoundnbt = bucket.getOrCreateTag();
                        compoundnbt.putInt("BucketVariantTag", ((TropicalFishEntity)fishEntity).getVariant());
                    }
                    //-------------------------------------------
                    if (!fishEntity.world.isRemote) {
                        CriteriaTriggers.FILLED_BUCKET.trigger((ServerPlayerEntity) player, bucket);
                    }
                    if (itemstack.isEmpty()) {
                        player.setHeldItem(event.getHand(), bucket);
                    } else if (!player.inventory.addItemStackToInventory(bucket)) {
                        player.dropItem(bucket, false);
                    }
                    fishEntity.remove();
                }
                event.setCanceled(true);
                event.setCancellationResult(ActionResultType.SUCCESS);
            }
        }
    }
/**
     * Add cow and fish interaction.
     */
Add cow and fish interaction.
Add cow and fish interaction .
@SubscribeEvent
    public static void onEntityInteract(PlayerInteractEvent.EntityInteract event) {
        if (event.getTarget() instanceof CowEntity) {
            CowEntity cowEntity = (CowEntity) event.getTarget();
            PlayerEntity player = event.getPlayer();
            ItemStack itemstack = player.getHeldItem(event.getHand());
            if (itemstack.getItem() == CeramicBucketItems.CERAMIC_BUCKET && !player.abilities.isCreativeMode && !cowEntity.isChild()) {
                player.playSound(SoundEvents.ENTITY_COW_MILK, 1.0F, 1.0F);
                if (!event.getWorld().isRemote()) {
                    itemstack.shrink(1);
                    if (itemstack.isEmpty()) {
                        player.setHeldItem(event.getHand(), new ItemStack(CeramicBucketItems.CERAMIC_MILK_BUCKET));
                    } else if (!player.inventory.addItemStackToInventory(new ItemStack(CeramicBucketItems.CERAMIC_MILK_BUCKET))) {
                        player.dropItem(new ItemStack(CeramicBucketItems.CERAMIC_MILK_BUCKET), false);
                    }
                }
                event.setCanceled(true);
                event.setCancellationResult(ActionResultType.SUCCESS);
            }
        } else if (event.getTarget() instanceof AbstractFishEntity) {
            AbstractFishEntity fishEntity = (AbstractFishEntity) event.getTarget();
            PlayerEntity player = event.getPlayer();
            ItemStack itemstack = player.getHeldItem(event.getHand());
            if (itemstack.getItem() == CeramicBucketItems.FILLED_CERAMIC_BUCKET && ((FilledCeramicBucketItem) itemstack.getItem()).getFluid(itemstack) == Fluids.WATER && fishEntity.isAlive()) {
                fishEntity.playSound(SoundEvents.ITEM_BUCKET_FILL_FISH, 1.0F, 1.0F);
                if (!event.getWorld().isRemote()) {
                    itemstack.shrink(1);
                    ItemStack bucket;
                    if (fishEntity instanceof PufferfishEntity) {
                        bucket = ((CeramicFishBucketItem) CeramicBucketItems.PUFFERFISH_CERAMIC_BUCKET).getFilledInstance();
                    } else if (fishEntity instanceof CodEntity) {
                        bucket = ((CeramicFishBucketItem) CeramicBucketItems.COD_CERAMIC_BUCKET).getFilledInstance();
                    } else if (fishEntity instanceof TropicalFishEntity) {
                        bucket = ((CeramicFishBucketItem) CeramicBucketItems.TROPICAL_FISH_CERAMIC_BUCKET).getFilledInstance();
                    } else {
                        bucket = ((CeramicFishBucketItem) CeramicBucketItems.SALMON_CERAMIC_BUCKET).getFilledInstance();
                    }
                    if (fishEntity.hasCustomName()) {
                        bucket.setDisplayName(fishEntity.getCustomName());
                    }
                    if (fishEntity instanceof TropicalFishEntity) {
                        CompoundNBT compoundnbt = bucket.getOrCreateTag();
                        compoundnbt.putInt("BucketVariantTag", ((TropicalFishEntity)fishEntity).getVariant());
                    }
                    if (!fishEntity.world.isRemote) {
                        CriteriaTriggers.FILLED_BUCKET.trigger((ServerPlayerEntity) player, bucket);
                    }
                    if (itemstack.isEmpty()) {
                        player.setHeldItem(event.getHand(), bucket);
                    } else if (!player.inventory.addItemStackToInventory(bucket)) {
                        player.dropItem(bucket, false);
                    }
                    fishEntity.remove();
                }
                event.setCanceled(true);
                event.setCancellationResult(ActionResultType.SUCCESS);
            }
        }
    }
@ SubscribeEvent public static void onEntityInteract ( PlayerInteractEvent . EntityInteract event ) { if ( event . getTarget ( ) instanceof CowEntity ) { CowEntity cowEntity = ( CowEntity ) event . getTarget ( ) ; PlayerEntity player = event . getPlayer ( ) ; ItemStack itemstack = player . getHeldItem ( event . getHand ( ) ) ; if ( itemstack . getItem ( ) == CeramicBucketItems . CERAMIC_BUCKET && ! player . abilities . isCreativeMode && ! cowEntity . isChild ( ) ) { player . playSound ( SoundEvents . ENTITY_COW_MILK , 1.0F , 1.0F ) ; if ( ! event . getWorld ( ) . isRemote ( ) ) { itemstack . shrink ( 1 ) ; if ( itemstack . isEmpty ( ) ) { player . setHeldItem ( event . getHand ( ) , new ItemStack ( CeramicBucketItems . CERAMIC_MILK_BUCKET ) ) ; } else if ( ! player . inventory . addItemStackToInventory ( new ItemStack ( CeramicBucketItems . CERAMIC_MILK_BUCKET ) ) ) { player . dropItem ( new ItemStack ( CeramicBucketItems . CERAMIC_MILK_BUCKET ) , false ) ; } } event . setCanceled ( true ) ; event . setCancellationResult ( ActionResultType . SUCCESS ) ; } } else if ( event . getTarget ( ) instanceof AbstractFishEntity ) { AbstractFishEntity fishEntity = ( AbstractFishEntity ) event . getTarget ( ) ; PlayerEntity player = event . getPlayer ( ) ; ItemStack itemstack = player . getHeldItem ( event . getHand ( ) ) ; if ( itemstack . getItem ( ) == CeramicBucketItems . FILLED_CERAMIC_BUCKET && ( ( FilledCeramicBucketItem ) itemstack . getItem ( ) ) . getFluid ( itemstack ) == Fluids . WATER && fishEntity . isAlive ( ) ) { fishEntity . playSound ( SoundEvents . ITEM_BUCKET_FILL_FISH , 1.0F , 1.0F ) ; if ( ! event . getWorld ( ) . isRemote ( ) ) { itemstack . shrink ( 1 ) ; ItemStack bucket ; if ( fishEntity instanceof PufferfishEntity ) { bucket = ( ( CeramicFishBucketItem ) CeramicBucketItems . PUFFERFISH_CERAMIC_BUCKET ) . getFilledInstance ( ) ; } else if ( fishEntity instanceof CodEntity ) { bucket = ( ( CeramicFishBucketItem ) CeramicBucketItems . COD_CERAMIC_BUCKET ) . getFilledInstance ( ) ; } else if ( fishEntity instanceof TropicalFishEntity ) { bucket = ( ( CeramicFishBucketItem ) CeramicBucketItems . TROPICAL_FISH_CERAMIC_BUCKET ) . getFilledInstance ( ) ; } else { bucket = ( ( CeramicFishBucketItem ) CeramicBucketItems . SALMON_CERAMIC_BUCKET ) . getFilledInstance ( ) ; } if ( fishEntity . hasCustomName ( ) ) { bucket . setDisplayName ( fishEntity . getCustomName ( ) ) ; } if ( fishEntity instanceof TropicalFishEntity ) { CompoundNBT compoundnbt = bucket . getOrCreateTag ( ) ; compoundnbt . putInt ( "BucketVariantTag" , ( ( TropicalFishEntity ) fishEntity ) . getVariant ( ) ) ; } if ( ! fishEntity . world . isRemote ) { CriteriaTriggers . FILLED_BUCKET . trigger ( ( ServerPlayerEntity ) player , bucket ) ; } if ( itemstack . isEmpty ( ) ) { player . setHeldItem ( event . getHand ( ) , bucket ) ; } else if ( ! player . inventory . addItemStackToInventory ( bucket ) ) { player . dropItem ( bucket , false ) ; } fishEntity . remove ( ) ; } event . setCanceled ( true ) ; event . setCancellationResult ( ActionResultType . SUCCESS ) ; } } }
Add cow and fish interaction.
Add cow and fish interaction .
//only interact with cows and fishes //------------------------------------------- //get ceramic variant //setBucketData //-------------------------------------------