use of net.minecraft.item.LingeringPotionItem 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;
}
}
}
use of net.minecraft.item.LingeringPotionItem in project ChocolateQuestRepoured by TeamChocoQuest.
the class ItemAlchemyBag method onItemRightClick.
@Override
public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity playerIn, Hand handIn) {
if (!world.isClientSide) {
if (playerIn.isCrouching()) {
playerIn.openGui(CQRMain.INSTANCE, GuiHandler.ALCHEMY_BAG_GUI_ID, world, handIn.ordinal(), 0, 0);
return new ActionResult<>(ActionResultType.SUCCESS, playerIn.getItemInHand(handIn));
}
ItemStack stack = playerIn.getItemInHand(handIn);
Item item = stack.getItem();
if (item == this) {
IItemHandler inventory = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
for (int i = 0; i < inventory.getSlots(); i++) {
ItemStack potion = inventory.extractItem(i, 1, false);
if (!potion.isEmpty()) {
if (potion.getItem() instanceof SplashPotionItem || potion.getItem() instanceof LingeringPotionItem) {
PotionEntity entitypotion = new PotionEntity(world, playerIn);
entitypotion.setItem(potion);
entitypotion.shoot(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, -20.0F, 0.5F, 1.0F);
world.addFreshEntity(entitypotion);
world.playSound((PlayerEntity) null, playerIn.posX, playerIn.posY, playerIn.posZ, SoundEvents.SPLASH_POTION_THROW, SoundCategory.PLAYERS, 0.5F, 0.4F / (random.nextFloat() * 0.4F + 0.8F));
return new ActionResult<>(ActionResultType.SUCCESS, playerIn.getItemInHand(handIn));
}
}
}
}
}
return new ActionResult<>(ActionResultType.FAIL, playerIn.getItemInHand(handIn));
}
Aggregations