Search in sources :

Example 1 with ItemAlchemyBag

use of team.cqr.cqrepoured.item.ItemAlchemyBag in project ChocolateQuestRepoured by TeamChocoQuest.

the class EntityAIPotionThrower method checkAndPerformAttack.

@Override
protected void checkAndPerformAttack(LivingEntity attackTarget) {
    if (this.entity.tickCount > this.prevTimeAttacked + this.getAttackCooldown()) {
        ItemStack stack = this.getEquippedWeapon();
        Item item = stack.getItem();
        final double x = attackTarget.getX() - this.entity.getX();
        double y = attackTarget.getY() + attackTarget.getBbHeight() * 0.5D;
        final double z = attackTarget.getZ() - this.entity.getZ();
        final double distance = Math.sqrt(x * x + z * z);
        // Throwable potions
        if (item instanceof SplashPotionItem || item instanceof LingeringPotionItem) {
            PotionEntity proj = new PotionEntity(this.world, this.entity);
            proj.setItem(stack.copy());
            y -= proj.getY();
            proj.shoot(x, y + distance * 0.06D, z, 1.F, this.entity.getRandom().nextFloat() * 0.25F);
            /*proj.motionX += this.entity.motionX;
				proj.motionZ += this.entity.motionZ;
				if (!this.entity.onGround) {
					proj.motionY += this.entity.motionY;
				}*/
            proj.setDeltaMovement(proj.getDeltaMovement().add(this.entity.getDeltaMovement()));
            proj.hasImpulse = true;
            this.entity.level.addFreshEntity(proj);
            this.entity.swing(Hand.OFF_HAND);
            this.entity.playSound(SoundEvents.SPLASH_POTION_THROW, 1.0F, 0.8F + this.random.nextFloat() * 0.4F);
            if (CQRConfig.mobs.offhandPotionsAreSingleUse) {
                stack.shrink(1);
            }
            this.prevTimeAttacked = this.entity.tickCount;
        } else if (item instanceof ItemAlchemyBag) {
            IItemHandler inventory = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).resolve().get();
            int indx = this.entity.getRandom().nextInt(inventory.getSlots());
            ItemStack st = inventory.getStackInSlot(indx);
            Set<Integer> usedIDs = new HashSet<>();
            int counter = 0;
            while (st.isEmpty() && !usedIDs.contains(indx) && counter > inventory.getSlots()) {
                indx = this.entity.getRandom().nextInt(inventory.getSlots());
                usedIDs.add(indx);
                st = inventory.getStackInSlot(indx);
                counter++;
            }
            if (!st.isEmpty()) {
                ItemStack potion = st.copy();
                // Now throw it
                if (potion.getItem() instanceof SplashPotionItem || potion.getItem() instanceof LingeringPotionItem) {
                    PotionEntity proj = new PotionEntity(this.world, this.entity);
                    proj.setItem(potion);
                    y -= proj.getY();
                    proj.shoot(x, y + distance * 0.08D, z, 1.F, this.entity.getRandom().nextFloat() * 0.25F);
                    /*proj.motionX += this.entity.motionX;
						proj.motionZ += this.entity.motionZ;
						if (!this.entity.onGround) {
							proj.motionY += this.entity.motionY;
						}*/
                    proj.setDeltaMovement(proj.getDeltaMovement().add(this.entity.getDeltaMovement()));
                    proj.hasImpulse = true;
                    this.entity.level.addFreshEntity(proj);
                    this.entity.swing(Hand.OFF_HAND);
                    this.entity.playSound(SoundEvents.SPLASH_POTION_THROW, 1.0F, 0.8F + this.random.nextFloat() * 0.4F);
                }
            }
            this.prevTimeAttacked = this.entity.tickCount;
        }
    }
}
Also used : SplashPotionItem(net.minecraft.item.SplashPotionItem) Item(net.minecraft.item.Item) LingeringPotionItem(net.minecraft.item.LingeringPotionItem) LingeringPotionItem(net.minecraft.item.LingeringPotionItem) Set(java.util.Set) HashSet(java.util.HashSet) IItemHandler(net.minecraftforge.items.IItemHandler) SplashPotionItem(net.minecraft.item.SplashPotionItem) ItemStack(net.minecraft.item.ItemStack) ItemAlchemyBag(team.cqr.cqrepoured.item.ItemAlchemyBag) PotionEntity(net.minecraft.entity.projectile.PotionEntity)

Aggregations

HashSet (java.util.HashSet)1 Set (java.util.Set)1 PotionEntity (net.minecraft.entity.projectile.PotionEntity)1 Item (net.minecraft.item.Item)1 ItemStack (net.minecraft.item.ItemStack)1 LingeringPotionItem (net.minecraft.item.LingeringPotionItem)1 SplashPotionItem (net.minecraft.item.SplashPotionItem)1 IItemHandler (net.minecraftforge.items.IItemHandler)1 ItemAlchemyBag (team.cqr.cqrepoured.item.ItemAlchemyBag)1