Search in sources :

Example 31 with Player

use of net.minecraft.world.entity.player.Player in project MyPet by xXKeyleXx.

the class EntityMyGiant method handlePlayerInteraction.

@Override
public InteractionResult handlePlayerInteraction(Player entityhuman, InteractionHand enumhand, ItemStack itemStack) {
    if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
        return InteractionResult.CONSUME;
    }
    if (getOwner().equals(entityhuman) && itemStack != null) {
        if (itemStack.getItem() == Items.SHEARS && getOwner().getPlayer().isSneaking() && canEquip()) {
            boolean hadEquipment = false;
            for (EquipmentSlot slot : EquipmentSlot.values()) {
                ItemStack itemInSlot = CraftItemStack.asNMSCopy(getMyPet().getEquipment(slot));
                if (itemInSlot != null && itemInSlot.getItem() != Items.AIR) {
                    ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), itemInSlot);
                    entityitem.pickupDelay = 10;
                    entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
                    this.level.addFreshEntity(entityitem);
                    getMyPet().setEquipment(slot, null);
                    hadEquipment = true;
                }
            }
            if (hadEquipment) {
                if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                    try {
                        itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> entityhuman1.broadcastBreakEvent(enumhand));
                    } catch (Error e) {
                        // TODO REMOVE
                        itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> {
                            try {
                                CompatManager.ENTITY_LIVING_broadcastItemBreak.invoke(entityhuman1, enumhand);
                            } catch (IllegalAccessException | InvocationTargetException ex) {
                                ex.printStackTrace();
                            }
                        });
                    }
                }
            }
            return InteractionResult.CONSUME;
        } else if (MyPetApi.getPlatformHelper().isEquipment(CraftItemStack.asBukkitCopy(itemStack)) && getOwner().getPlayer().isSneaking() && canEquip()) {
            EquipmentSlot slot = EquipmentSlot.getSlotById(Mob.getEquipmentSlotForItem(itemStack).getFilterFlag());
            ItemStack itemInSlot = CraftItemStack.asNMSCopy(getMyPet().getEquipment(slot));
            if (itemInSlot != null && itemInSlot.getItem() != Items.AIR && itemInSlot != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), itemInSlot);
                entityitem.pickupDelay = 10;
                entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
                this.level.addFreshEntity(entityitem);
            }
            getMyPet().setEquipment(slot, CraftItemStack.asBukkitCopy(itemStack));
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            return InteractionResult.CONSUME;
        }
    }
    return InteractionResult.PASS;
}
Also used : MyPet(de.Keyle.MyPet.api.entity.MyPet) Arrays(java.util.Arrays) Util(de.Keyle.MyPet.api.Util) EntityMyPet(de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet) Items(net.minecraft.world.item.Items) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) MyPetApi(de.Keyle.MyPet.MyPetApi) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) CompatManager(de.Keyle.MyPet.compat.v1_18_R1.CompatManager) InteractionResult(net.minecraft.world.InteractionResult) EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot) ClientboundSetEquipmentPacket(net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket) ServerLevel(net.minecraft.server.level.ServerLevel) Pair(com.mojang.datafixers.util.Pair) InvocationTargetException(java.lang.reflect.InvocationTargetException) Player(net.minecraft.world.entity.player.Player) MyGiant(de.Keyle.MyPet.api.entity.types.MyGiant) MeleeAttack(de.Keyle.MyPet.compat.v1_18_R1.entity.ai.attack.MeleeAttack) ItemEntity(net.minecraft.world.entity.item.ItemEntity) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Mob(net.minecraft.world.entity.Mob) Level(net.minecraft.world.level.Level) Bukkit(org.bukkit.Bukkit) ItemEntity(net.minecraft.world.entity.item.ItemEntity) EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) ItemStack(net.minecraft.world.item.ItemStack)

Example 32 with Player

use of net.minecraft.world.entity.player.Player in project MyPet by xXKeyleXx.

the class EntityMyDrowned method handlePlayerInteraction.

/**
 * Is called when player rightclicks this MyPet
 * return:
 * true: there was a reaction on rightclick
 * false: no reaction on rightclick
 */
@Override
public InteractionResult handlePlayerInteraction(Player entityhuman, InteractionHand enumhand, ItemStack itemStack) {
    if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
        return InteractionResult.CONSUME;
    }
    if (getOwner().equals(entityhuman) && itemStack != null) {
        if (itemStack.getItem() == Items.SHEARS && getOwner().getPlayer().isSneaking() && canEquip()) {
            boolean hadEquipment = false;
            for (EquipmentSlot slot : EquipmentSlot.values()) {
                ItemStack itemInSlot = CraftItemStack.asNMSCopy(getMyPet().getEquipment(slot));
                if (itemInSlot != null && itemInSlot.getItem() != Items.AIR) {
                    ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), itemInSlot);
                    entityitem.pickupDelay = 10;
                    entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
                    this.level.addFreshEntity(entityitem);
                    getMyPet().setEquipment(slot, null);
                    hadEquipment = true;
                }
            }
            if (hadEquipment) {
                if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                    try {
                        itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> entityhuman1.broadcastBreakEvent(enumhand));
                    } catch (Error e) {
                        // TODO REMOVE
                        itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> {
                            try {
                                CompatManager.ENTITY_LIVING_broadcastItemBreak.invoke(entityhuman1, enumhand);
                            } catch (IllegalAccessException | InvocationTargetException ex) {
                                ex.printStackTrace();
                            }
                        });
                    }
                }
            }
            return InteractionResult.CONSUME;
        } else if (MyPetApi.getPlatformHelper().isEquipment(CraftItemStack.asBukkitCopy(itemStack)) && getOwner().getPlayer().isSneaking() && canEquip()) {
            EquipmentSlot slot = EquipmentSlot.getSlotById(Mob.getEquipmentSlotForItem(itemStack).getFilterFlag());
            ItemStack itemInSlot = CraftItemStack.asNMSCopy(getMyPet().getEquipment(slot));
            if (itemInSlot != null && itemInSlot.getItem() != Items.AIR && itemInSlot != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), itemInSlot);
                entityitem.pickupDelay = 10;
                entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
                this.level.addFreshEntity(entityitem);
            }
            getMyPet().setEquipment(slot, CraftItemStack.asBukkitCopy(itemStack));
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            return InteractionResult.CONSUME;
        } else if (Configuration.MyPet.Zombie.GROW_UP_ITEM.compare(itemStack) && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) {
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            getMyPet().setBaby(false);
            return InteractionResult.CONSUME;
        }
    }
    return InteractionResult.PASS;
}
Also used : Arrays(java.util.Arrays) Items(net.minecraft.world.item.Items) MyDrowned(de.Keyle.MyPet.api.entity.types.MyDrowned) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) EntityDataSerializers(net.minecraft.network.syncher.EntityDataSerializers) ServerLevel(net.minecraft.server.level.ServerLevel) Configuration(de.Keyle.MyPet.api.Configuration) Bukkit(org.bukkit.Bukkit) MyPet(de.Keyle.MyPet.api.entity.MyPet) Util(de.Keyle.MyPet.api.Util) EntityMyPet(de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) MyPetApi(de.Keyle.MyPet.MyPetApi) CompatManager(de.Keyle.MyPet.compat.v1_18_R1.CompatManager) EntityDataAccessor(net.minecraft.network.syncher.EntityDataAccessor) InteractionResult(net.minecraft.world.InteractionResult) EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot) ClientboundSetEquipmentPacket(net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket) Pair(com.mojang.datafixers.util.Pair) InvocationTargetException(java.lang.reflect.InvocationTargetException) Player(net.minecraft.world.entity.player.Player) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData) ItemEntity(net.minecraft.world.entity.item.ItemEntity) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Mob(net.minecraft.world.entity.Mob) Level(net.minecraft.world.level.Level) ItemEntity(net.minecraft.world.entity.item.ItemEntity) EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) ItemStack(net.minecraft.world.item.ItemStack)

Example 33 with Player

use of net.minecraft.world.entity.player.Player in project MyPet by xXKeyleXx.

the class EntityMyDonkey method handlePlayerInteraction.

@Override
public InteractionResult handlePlayerInteraction(Player entityhuman, InteractionHand enumhand, ItemStack itemStack) {
    if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
        return InteractionResult.CONSUME;
    }
    if (itemStack != null && canUseItem()) {
        if (itemStack.getItem() == Items.SADDLE && !getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking() && canEquip()) {
            getMyPet().setSaddle(CraftItemStack.asBukkitCopy(itemStack));
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            return InteractionResult.CONSUME;
        } else if (itemStack.getItem() == Item.byBlock(Blocks.CHEST) && getOwner().getPlayer().isSneaking() && !getMyPet().hasChest() && canEquip()) {
            getMyPet().setChest(CraftItemStack.asBukkitCopy(itemStack));
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            return InteractionResult.CONSUME;
        } else if (itemStack.getItem() == Items.SHEARS && getOwner().getPlayer().isSneaking() && canEquip()) {
            if (getMyPet().hasChest()) {
                ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), CraftItemStack.asNMSCopy(getMyPet().getChest()));
                entityitem.pickupDelay = 10;
                entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
                this.level.addFreshEntity(entityitem);
            }
            if (getMyPet().hasSaddle()) {
                ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), CraftItemStack.asNMSCopy(getMyPet().getSaddle()));
                entityitem.pickupDelay = 10;
                entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
                this.level.addFreshEntity(entityitem);
            }
            makeSound("entity.sheep.shear", 1.0F, 1.0F);
            getMyPet().setChest(null);
            getMyPet().setSaddle(null);
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                try {
                    itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> entityhuman1.broadcastBreakEvent(enumhand));
                } catch (Error e) {
                    // TODO REMOVE
                    itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> {
                        try {
                            CompatManager.ENTITY_LIVING_broadcastItemBreak.invoke(entityhuman1, enumhand);
                        } catch (IllegalAccessException | InvocationTargetException ex) {
                            ex.printStackTrace();
                        }
                    });
                }
            }
            return InteractionResult.CONSUME;
        } else if (Configuration.MyPet.Donkey.GROW_UP_ITEM.compare(itemStack) && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) {
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            getMyPet().setBaby(false);
            return InteractionResult.CONSUME;
        }
    }
    return InteractionResult.PASS;
}
Also used : Items(net.minecraft.world.item.Items) MyDonkey(de.Keyle.MyPet.api.entity.types.MyDonkey) Item(net.minecraft.world.item.Item) EntityDataSerializers(net.minecraft.network.syncher.EntityDataSerializers) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) BlockState(net.minecraft.world.level.block.state.BlockState) Configuration(de.Keyle.MyPet.api.Configuration) SoundEvents(net.minecraft.sounds.SoundEvents) MyPet(de.Keyle.MyPet.api.entity.MyPet) EntityMyPet(de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) CompatManager(de.Keyle.MyPet.compat.v1_18_R1.CompatManager) EntityDataAccessor(net.minecraft.network.syncher.EntityDataAccessor) InteractionResult(net.minecraft.world.InteractionResult) UUID(java.util.UUID) SoundType(net.minecraft.world.level.block.SoundType) Player(net.minecraft.world.entity.player.Player) Blocks(net.minecraft.world.level.block.Blocks) InvocationTargetException(java.lang.reflect.InvocationTargetException) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData) Entity(net.minecraft.world.entity.Entity) BlockPos(net.minecraft.core.BlockPos) ItemEntity(net.minecraft.world.entity.item.ItemEntity) Optional(java.util.Optional) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) ItemEntity(net.minecraft.world.entity.item.ItemEntity)

Example 34 with Player

use of net.minecraft.world.entity.player.Player in project MyPet by xXKeyleXx.

the class EntityMyZombifiedPiglin method handlePlayerInteraction.

@Override
public InteractionResult handlePlayerInteraction(Player entityhuman, InteractionHand enumhand, ItemStack itemStack) {
    if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack) == InteractionResult.CONSUME) {
        return InteractionResult.CONSUME;
    }
    if (getOwner().equals(entityhuman) && itemStack != null && canUseItem()) {
        if (itemStack.getItem() == Items.SHEARS && getOwner().getPlayer().isSneaking() && canEquip()) {
            boolean hadEquipment = false;
            for (EquipmentSlot slot : EquipmentSlot.values()) {
                ItemStack itemInSlot = CraftItemStack.asNMSCopy(getMyPet().getEquipment(slot));
                if (itemInSlot != null && itemInSlot.getItem() != Items.AIR) {
                    ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), itemInSlot);
                    entityitem.pickupDelay = 10;
                    entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
                    this.level.addFreshEntity(entityitem);
                    getMyPet().setEquipment(slot, null);
                    hadEquipment = true;
                }
            }
            if (hadEquipment) {
                if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                    try {
                        itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> entityhuman1.broadcastBreakEvent(enumhand));
                    } catch (Error e) {
                        // TODO REMOVE
                        itemStack.hurtAndBreak(1, entityhuman, (entityhuman1) -> {
                            try {
                                CompatManager.ENTITY_LIVING_broadcastItemBreak.invoke(entityhuman1, enumhand);
                            } catch (IllegalAccessException | InvocationTargetException ex) {
                                ex.printStackTrace();
                            }
                        });
                    }
                }
            }
            return InteractionResult.CONSUME;
        } else if (MyPetApi.getPlatformHelper().isEquipment(CraftItemStack.asBukkitCopy(itemStack)) && getOwner().getPlayer().isSneaking() && canEquip()) {
            EquipmentSlot slot = EquipmentSlot.getSlotById(Mob.getEquipmentSlotForItem(itemStack).getFilterFlag());
            ItemStack itemInSlot = CraftItemStack.asNMSCopy(getMyPet().getEquipment(slot));
            if (itemInSlot != null && itemInSlot.getItem() != Items.AIR && itemInSlot != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), itemInSlot);
                entityitem.pickupDelay = 10;
                entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
                this.level.addFreshEntity(entityitem);
            }
            getMyPet().setEquipment(slot, CraftItemStack.asBukkitCopy(itemStack));
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            return InteractionResult.CONSUME;
        } else if (Configuration.MyPet.ZombifiedPiglin.GROW_UP_ITEM.compare(itemStack) && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) {
            if (itemStack != ItemStack.EMPTY && !entityhuman.getAbilities().instabuild) {
                itemStack.shrink(1);
                if (itemStack.getCount() <= 0) {
                    entityhuman.getInventory().setItem(entityhuman.getInventory().selected, ItemStack.EMPTY);
                }
            }
            getMyPet().setBaby(false);
            return InteractionResult.CONSUME;
        }
    }
    return InteractionResult.PASS;
}
Also used : Arrays(java.util.Arrays) Items(net.minecraft.world.item.Items) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) EntityDataSerializers(net.minecraft.network.syncher.EntityDataSerializers) ServerLevel(net.minecraft.server.level.ServerLevel) Configuration(de.Keyle.MyPet.api.Configuration) Bukkit(org.bukkit.Bukkit) MyPet(de.Keyle.MyPet.api.entity.MyPet) Util(de.Keyle.MyPet.api.Util) EntityMyPet(de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet) PetState(de.Keyle.MyPet.api.entity.MyPet.PetState) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) MyPetApi(de.Keyle.MyPet.MyPetApi) MyZombifiedPiglin(de.Keyle.MyPet.api.entity.types.MyZombifiedPiglin) CompatManager(de.Keyle.MyPet.compat.v1_18_R1.CompatManager) EntityDataAccessor(net.minecraft.network.syncher.EntityDataAccessor) InteractionResult(net.minecraft.world.InteractionResult) EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot) ClientboundSetEquipmentPacket(net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket) Pair(com.mojang.datafixers.util.Pair) InvocationTargetException(java.lang.reflect.InvocationTargetException) Player(net.minecraft.world.entity.player.Player) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData) ItemEntity(net.minecraft.world.entity.item.ItemEntity) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Mob(net.minecraft.world.entity.Mob) Level(net.minecraft.world.level.Level) ItemEntity(net.minecraft.world.entity.item.ItemEntity) EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) ItemStack(net.minecraft.world.item.ItemStack)

Example 35 with Player

use of net.minecraft.world.entity.player.Player in project SpongeCommon by SpongePowered.

the class LivingEntityMixin method bridge$damageEntity.

@Override
public boolean bridge$damageEntity(final DamageSource damageSource, float damage) {
    if (this.shadow$isInvulnerableTo(damageSource)) {
        return false;
    }
    final boolean isHuman = (LivingEntity) (Object) this instanceof Player;
    // Sponge Start - Call platform hook for adjusting damage
    damage = this.bridge$applyModDamage((LivingEntity) (Object) this, damageSource, damage);
    // Sponge End
    final float originalDamage = damage;
    if (damage <= 0) {
        return false;
    }
    final List<DamageFunction> originalFunctions = new ArrayList<>();
    final Optional<DamageFunction> hardHatFunction = DamageEventUtil.createHardHatModifier((LivingEntity) (Object) this, damageSource);
    final Optional<DamageFunction> armorFunction = DamageEventUtil.createArmorModifiers((LivingEntity) (Object) this, damageSource);
    final Optional<DamageFunction> resistanceFunction = DamageEventUtil.createResistanceModifier((LivingEntity) (Object) this, damageSource);
    final Optional<List<DamageFunction>> armorEnchantments = DamageEventUtil.createEnchantmentModifiers((LivingEntity) (Object) this, damageSource);
    final Optional<DamageFunction> absorptionFunction = DamageEventUtil.createAbsorptionModifier((LivingEntity) (Object) this);
    final Optional<DamageFunction> shieldFunction = DamageEventUtil.createShieldFunction((LivingEntity) (Object) this, damageSource, damage);
    hardHatFunction.ifPresent(originalFunctions::add);
    shieldFunction.ifPresent(originalFunctions::add);
    armorFunction.ifPresent(originalFunctions::add);
    resistanceFunction.ifPresent(originalFunctions::add);
    armorEnchantments.ifPresent(originalFunctions::addAll);
    absorptionFunction.ifPresent(originalFunctions::add);
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        DamageEventUtil.generateCauseFor(damageSource, frame);
        final DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(frame.currentCause(), (org.spongepowered.api.entity.Entity) this, originalFunctions, originalDamage);
        if (damageSource != SpongeDamageSources.IGNORED) {
            // Basically, don't throw an event if it's our own damage source
            SpongeCommon.post(event);
        }
        if (event.isCancelled()) {
            return false;
        }
        damage = (float) event.finalDamage();
        // Sponge Start - Allow the platform to adjust damage before applying armor/etc
        damage = this.bridge$applyModDamageBeforeFunctions((LivingEntity) (Object) this, damageSource, damage);
        // Sponge End
        // Helmet
        final ItemStack helmet = this.shadow$getItemBySlot(EquipmentSlot.HEAD);
        // without using our mixin redirects in EntityFallingBlockMixin.
        if ((damageSource instanceof FallingBlockDamageSource) || damageSource == DamageSource.ANVIL || damageSource == DamageSource.FALLING_BLOCK && !helmet.isEmpty()) {
            helmet.hurtAndBreak((int) (event.baseDamage() * 4.0F + this.random.nextFloat() * event.baseDamage() * 2.0F), (LivingEntity) (Object) this, (entity) -> entity.broadcastBreakEvent(EquipmentSlot.HEAD));
        }
        boolean hurtStack = false;
        // Shield
        if (shieldFunction.isPresent()) {
            this.shadow$hurtCurrentlyUsedShield((float) event.baseDamage());
            hurtStack = true;
            if (!damageSource.isProjectile()) {
                final Entity entity = damageSource.getDirectEntity();
                if (entity instanceof LivingEntity) {
                    this.shadow$blockUsingShield((LivingEntity) entity);
                }
            }
        }
        // Armor
        if (!damageSource.isBypassArmor() && armorFunction.isPresent()) {
            this.shadow$hurtArmor(damageSource, (float) event.baseDamage());
            hurtStack = true;
        }
        // Sponge start - log inventory change due to taking damage
        if (hurtStack && isHuman) {
            PhaseTracker.SERVER.getPhaseContext().getTransactor().logPlayerInventoryChange((Player) (Object) this, PlayerInventoryTransaction.EventCreator.STANDARD);
            // capture
            ((Player) (Object) this).inventoryMenu.broadcastChanges();
        }
        // Resistance modifier post calculation
        if (resistanceFunction.isPresent()) {
            final float f2 = (float) event.damage(resistanceFunction.get().modifier()) - damage;
            if (f2 > 0.0F && f2 < 3.4028235E37F) {
                if (((LivingEntity) (Object) this) instanceof net.minecraft.server.level.ServerPlayer) {
                    ((net.minecraft.server.level.ServerPlayer) ((LivingEntity) (Object) this)).awardStat(Stats.DAMAGE_RESISTED, Math.round(f2 * 10.0F));
                } else if (damageSource.getEntity() instanceof net.minecraft.server.level.ServerPlayer) {
                    ((net.minecraft.server.level.ServerPlayer) damageSource.getEntity()).awardStat(Stats.DAMAGE_DEALT_RESISTED, Math.round(f2 * 10.0F));
                }
            }
        }
        double absorptionModifier = absorptionFunction.map(function -> event.damage(function.modifier())).orElse(0d);
        if (absorptionFunction.isPresent()) {
            absorptionModifier = event.damage(absorptionFunction.get().modifier());
        }
        final float f = (float) event.finalDamage() - (float) absorptionModifier;
        this.shadow$setAbsorptionAmount(Math.max(this.shadow$getAbsorptionAmount() + (float) absorptionModifier, 0.0F));
        if (f > 0.0F && f < 3.4028235E37F && ((LivingEntity) (Object) this) instanceof net.minecraft.server.level.ServerPlayer) {
            ((Player) (Object) this).awardStat(Stats.DAMAGE_DEALT_ABSORBED, Math.round(f * 10.0F));
        }
        if (damage != 0.0F) {
            if (isHuman) {
                ((Player) (Object) this).causeFoodExhaustion(damageSource.getFoodExhaustion());
            }
            final float f2 = this.shadow$getHealth();
            this.shadow$setHealth(f2 - damage);
            this.shadow$getCombatTracker().recordDamage(damageSource, f2, damage);
            if (isHuman) {
                if (damage < 3.4028235E37F) {
                    ((Player) (Object) this).awardStat(Stats.DAMAGE_TAKEN, Math.round(damage * 10.0F));
                }
                return true;
            }
            this.shadow$setAbsorptionAmount(this.shadow$getAbsorptionAmount() - damage);
        }
        return true;
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) Inject(org.spongepowered.asm.mixin.injection.Inject) SpongeDamageSources(org.spongepowered.common.event.cause.entity.damage.SpongeDamageSources) ParticleOptions(net.minecraft.core.particles.ParticleOptions) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) HandType(org.spongepowered.api.data.type.HandType) PlayerBridge(org.spongepowered.common.bridge.world.entity.player.PlayerBridge) SpongeCommonEventFactory(org.spongepowered.common.event.SpongeCommonEventFactory) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) Mixin(org.spongepowered.asm.mixin.Mixin) DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) PlayerInventoryTransaction(org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction) Living(org.spongepowered.api.entity.living.Living) AttributeMap(net.minecraft.world.entity.ai.attributes.AttributeMap) Transaction(org.spongepowered.api.data.Transaction) At(org.spongepowered.asm.mixin.injection.At) CombatTracker(net.minecraft.world.damagesource.CombatTracker) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) Player(net.minecraft.world.entity.player.Player) DamageEventUtil(org.spongepowered.common.util.DamageEventUtil) List(java.util.List) BlockPos(net.minecraft.core.BlockPos) SoundEvent(net.minecraft.sounds.SoundEvent) Shadow(org.spongepowered.asm.mixin.Shadow) Optional(java.util.Optional) LivingEntityBridge(org.spongepowered.common.bridge.world.entity.LivingEntityBridge) ItemStack(net.minecraft.world.item.ItemStack) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) MoveEntityEvent(org.spongepowered.api.event.entity.MoveEntityEvent) ServerLocation(org.spongepowered.api.world.server.ServerLocation) HumanEntity(org.spongepowered.common.entity.living.human.HumanEntity) SoundSource(net.minecraft.sounds.SoundSource) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) Constants(org.spongepowered.common.util.Constants) Overwrite(org.spongepowered.asm.mixin.Overwrite) ServerLevel(net.minecraft.server.level.ServerLevel) SleepingEvent(org.spongepowered.api.event.action.SleepingEvent) ArrayList(java.util.ArrayList) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) DamageSource(net.minecraft.world.damagesource.DamageSource) Stats(net.minecraft.stats.Stats) CauseStackManager(org.spongepowered.api.event.CauseStackManager) LocalCapture(org.spongepowered.asm.mixin.injection.callback.LocalCapture) Nullable(javax.annotation.Nullable) UseItemStackEvent(org.spongepowered.api.event.item.inventory.UseItemStackEvent) Ticks(org.spongepowered.api.util.Ticks) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) Redirect(org.spongepowered.asm.mixin.injection.Redirect) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) ShouldFire(org.spongepowered.common.event.ShouldFire) SpongeCommon(org.spongepowered.common.SpongeCommon) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Attribute(net.minecraft.world.entity.ai.attributes.Attribute) Cause(org.spongepowered.api.event.Cause) MovementTypes(org.spongepowered.api.event.cause.entity.MovementTypes) Entity(net.minecraft.world.entity.Entity) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) Vector3d(org.spongepowered.math.vector.Vector3d) PlatformLivingEntityBridge(org.spongepowered.common.bridge.world.entity.PlatformLivingEntityBridge) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) VecHelper(org.spongepowered.common.util.VecHelper) InteractionHand(net.minecraft.world.InteractionHand) MobEffect(net.minecraft.world.effect.MobEffect) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) LivingEntity(net.minecraft.world.entity.LivingEntity) HumanEntity(org.spongepowered.common.entity.living.human.HumanEntity) Entity(net.minecraft.world.entity.Entity) ArrayList(java.util.ArrayList) LivingEntity(net.minecraft.world.entity.LivingEntity) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) CauseStackManager(org.spongepowered.api.event.CauseStackManager) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) List(java.util.List) ArrayList(java.util.ArrayList) DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) Player(net.minecraft.world.entity.player.Player) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ItemStack(net.minecraft.world.item.ItemStack)

Aggregations

Player (net.minecraft.world.entity.player.Player)97 ItemStack (net.minecraft.world.item.ItemStack)65 Level (net.minecraft.world.level.Level)56 InteractionHand (net.minecraft.world.InteractionHand)55 InteractionResult (net.minecraft.world.InteractionResult)52 ItemEntity (net.minecraft.world.entity.item.ItemEntity)51 Items (net.minecraft.world.item.Items)51 EntitySize (de.Keyle.MyPet.api.entity.EntitySize)50 MyPet (de.Keyle.MyPet.api.entity.MyPet)50 InvocationTargetException (java.lang.reflect.InvocationTargetException)50 EntityDataAccessor (net.minecraft.network.syncher.EntityDataAccessor)42 EntityDataSerializers (net.minecraft.network.syncher.EntityDataSerializers)42 SynchedEntityData (net.minecraft.network.syncher.SynchedEntityData)42 ServerLevel (net.minecraft.server.level.ServerLevel)40 Bukkit (org.bukkit.Bukkit)39 MyPetApi (de.Keyle.MyPet.MyPetApi)38 Util (de.Keyle.MyPet.api.Util)38 Pair (com.mojang.datafixers.util.Pair)36 EquipmentSlot (de.Keyle.MyPet.api.entity.EquipmentSlot)36 Arrays (java.util.Arrays)36