Search in sources :

Example 46 with EquipmentSlot

use of de.Keyle.MyPet.api.entity.EquipmentSlot 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 47 with EquipmentSlot

use of de.Keyle.MyPet.api.entity.EquipmentSlot in project MyPet by xXKeyleXx.

the class EntityMyFox 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)) {
        if (itemStack != null && itemStack.getItem() != Items.AIR && canUseItem() && getOwner().getPlayer().isSneaking()) {
            if (itemStack.getItem() != Items.SHEARS && getOwner().getPlayer().isSneaking() && canEquip()) {
                ItemStack itemInSlot = CraftItemStack.asNMSCopy(getMyPet().getEquipment(EquipmentSlot.MainHand));
                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(EquipmentSlot.MainHand, 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.Fox.GROW_UP_ITEM.compare(itemStack) && canUseItem() && 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;
            } else 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;
            }
        }
    }
    return InteractionResult.PASS;
}
Also used : Arrays(java.util.Arrays) Items(net.minecraft.world.item.Items) EntityDataSerializers(net.minecraft.network.syncher.EntityDataSerializers) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) 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) MyFox(de.Keyle.MyPet.api.entity.types.MyFox) 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) UUID(java.util.UUID) ClientboundSetEquipmentPacket(net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket) Pair(com.mojang.datafixers.util.Pair) Player(net.minecraft.world.entity.player.Player) InvocationTargetException(java.lang.reflect.InvocationTargetException) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData) 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) EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) ItemStack(net.minecraft.world.item.ItemStack)

Example 48 with EquipmentSlot

use of de.Keyle.MyPet.api.entity.EquipmentSlot in project MyPet by xXKeyleXx.

the class EntityMyZombie method updateVisuals.

@Override
public void updateVisuals() {
    getEntityData().set(BABY_WATCHER, getMyPet().isBaby());
    Bukkit.getScheduler().runTaskLater(MyPetApi.getPlugin(), () -> {
        if (getMyPet().getStatus() == MyPet.PetState.Here) {
            for (EquipmentSlot slot : EquipmentSlot.values()) {
                setPetEquipment(slot, CraftItemStack.asNMSCopy(getMyPet().getEquipment(slot)));
            }
        }
    }, 5L);
}
Also used : EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot)

Example 49 with EquipmentSlot

use of de.Keyle.MyPet.api.entity.EquipmentSlot in project MyPet by xXKeyleXx.

the class EntityMyPigZombie method handlePlayerInteraction.

public boolean handlePlayerInteraction(EntityHuman entityhuman) {
    if (super.handlePlayerInteraction(entityhuman)) {
        return true;
    }
    ItemStack itemStack = entityhuman.inventory.getItemInHand();
    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) {
                    EntityItem entityitem = this.a(itemInSlot.cloneItemStack(), 1.0F);
                    entityitem.motY += (double) (this.random.nextFloat() * 0.05F);
                    entityitem.motX += (double) ((this.random.nextFloat() - this.random.nextFloat()) * 0.1F);
                    entityitem.motZ += (double) ((this.random.nextFloat() - this.random.nextFloat()) * 0.1F);
                    getMyPet().setEquipment(slot, null);
                    hadEquipment = true;
                }
            }
            if (hadEquipment) {
                if (!entityhuman.abilities.canInstantlyBuild) {
                    itemStack.damage(1, entityhuman);
                }
            }
            return true;
        } else if (MyPetApi.getPlatformHelper().isEquipment(CraftItemStack.asBukkitCopy(itemStack)) && getOwner().getPlayer().isSneaking() && canEquip()) {
            EquipmentSlot slot = EquipmentSlot.getSlotById(c(itemStack));
            ItemStack itemInSlot = CraftItemStack.asNMSCopy(getMyPet().getEquipment(slot));
            if (itemInSlot != null && !entityhuman.abilities.canInstantlyBuild) {
                EntityItem entityitem = this.a(itemInSlot.cloneItemStack(), 1.0F);
                entityitem.motY += (double) (this.random.nextFloat() * 0.05F);
                entityitem.motX += (double) ((this.random.nextFloat() - this.random.nextFloat()) * 0.1F);
                entityitem.motZ += (double) ((this.random.nextFloat() - this.random.nextFloat()) * 0.1F);
            }
            getMyPet().setEquipment(slot, CraftItemStack.asBukkitCopy(itemStack));
            if (!entityhuman.abilities.canInstantlyBuild) {
                if (--itemStack.count <= 0) {
                    entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, null);
                }
            }
            return true;
        } else if (Configuration.MyPet.PigZombie.GROW_UP_ITEM.compare(itemStack) && getMyPet().isBaby() && getOwner().getPlayer().isSneaking()) {
            if (!entityhuman.abilities.canInstantlyBuild) {
                if (--itemStack.count <= 0) {
                    entityhuman.inventory.setItem(entityhuman.inventory.itemInHandIndex, null);
                }
            }
            getMyPet().setBaby(false);
            return true;
        }
    }
    return false;
}
Also used : EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot) CraftItemStack(org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack)

Example 50 with EquipmentSlot

use of de.Keyle.MyPet.api.entity.EquipmentSlot in project MyPet by xXKeyleXx.

the class EntityMySkeleton method updateVisuals.

@Override
public void updateVisuals() {
    this.datawatcher.watch(13, (byte) (getMyPet().isWither() ? 1 : 0));
    Bukkit.getScheduler().runTaskLater(MyPetApi.getPlugin(), () -> {
        if (getMyPet().getStatus() == PetState.Here) {
            for (EquipmentSlot slot : EquipmentSlot.values()) {
                if (slot == EquipmentSlot.OffHand) {
                    continue;
                }
                setPetEquipment(slot.getSlotId(), CraftItemStack.asNMSCopy(getMyPet().getEquipment(slot)));
            }
        }
    }, 5L);
}
Also used : EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot)

Aggregations

EquipmentSlot (de.Keyle.MyPet.api.entity.EquipmentSlot)161 Pair (com.mojang.datafixers.util.Pair)67 MyPetApi (de.Keyle.MyPet.MyPetApi)67 Util (de.Keyle.MyPet.api.Util)67 EntitySize (de.Keyle.MyPet.api.entity.EntitySize)67 MyPet (de.Keyle.MyPet.api.entity.MyPet)67 InvocationTargetException (java.lang.reflect.InvocationTargetException)67 Arrays (java.util.Arrays)67 Bukkit (org.bukkit.Bukkit)67 Configuration (de.Keyle.MyPet.api.Configuration)35 ClientboundSetEquipmentPacket (net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket)34 ServerLevel (net.minecraft.server.level.ServerLevel)34 InteractionHand (net.minecraft.world.InteractionHand)34 InteractionResult (net.minecraft.world.InteractionResult)34 ItemEntity (net.minecraft.world.entity.item.ItemEntity)34 Player (net.minecraft.world.entity.player.Player)34 ItemStack (net.minecraft.world.item.ItemStack)34 Items (net.minecraft.world.item.Items)34 Level (net.minecraft.world.level.Level)34 Mob (net.minecraft.world.entity.Mob)30