Search in sources :

Example 1 with ClientboundSetEntityLinkPacket

use of net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket in project MyPet by xXKeyleXx.

the class EntityMyPig method handlePlayerInteraction.

@Override
public InteractionResult handlePlayerInteraction(final net.minecraft.world.entity.player.Player entityhuman, InteractionHand enumhand, final ItemStack itemStack) {
    if (enumhand == InteractionHand.OFF_HAND) {
        if (itemStack != null) {
            if (itemStack.getItem() == Items.LEAD) {
                ((ServerLevel) this.level).getChunkProvider().broadcastAndSend(this, new ClientboundSetEntityLinkPacket(this, null));
                entityhuman.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY);
                new BukkitRunnable() {

                    @Override
                    public void run() {
                        if (entityhuman instanceof ServerPlayer) {
                            // TODO
                            entityhuman.setItemInHand(InteractionHand.OFF_HAND, itemStack);
                            Player p = (Player) entityhuman.getBukkitEntity();
                            if (!p.isOnline()) {
                                p.saveData();
                            }
                        }
                    }
                }.runTaskLater(MyPetApi.getPlugin(), 5);
            }
        }
        return InteractionResult.CONSUME;
    }
    if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
        return InteractionResult.CONSUME;
    }
    if (getOwner().equals(entityhuman) && itemStack != null && canUseItem()) {
        if (itemStack.getItem() == Items.SADDLE && !getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
            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() == Items.SHEARS && getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
            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().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.Pig.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 : MyPet(de.Keyle.MyPet.api.entity.MyPet) ClientboundSetEntityLinkPacket(net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket) Items(net.minecraft.world.item.Items) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MyPetApi(de.Keyle.MyPet.MyPetApi) MyPig(de.Keyle.MyPet.api.entity.types.MyPig) EntityDataSerializers(net.minecraft.network.syncher.EntityDataSerializers) EntityDataAccessor(net.minecraft.network.syncher.EntityDataAccessor) InteractionResult(net.minecraft.world.InteractionResult) Player(org.bukkit.entity.Player) ServerLevel(net.minecraft.server.level.ServerLevel) InvocationTargetException(java.lang.reflect.InvocationTargetException) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData) ServerPlayer(net.minecraft.server.level.ServerPlayer) Configuration(de.Keyle.MyPet.api.Configuration) ItemEntity(net.minecraft.world.entity.item.ItemEntity) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) CompatManager(de.Keyle.MyPet.compat.v1_17_R1.CompatManager) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) Player(org.bukkit.entity.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) ItemEntity(net.minecraft.world.entity.item.ItemEntity) ClientboundSetEntityLinkPacket(net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket) ServerPlayer(net.minecraft.server.level.ServerPlayer) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ClientboundSetEntityLinkPacket

use of net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket in project MyPet by xXKeyleXx.

the class EntityMyPig method handlePlayerInteraction.

@Override
public InteractionResult handlePlayerInteraction(final net.minecraft.world.entity.player.Player entityhuman, InteractionHand enumhand, final ItemStack itemStack) {
    if (enumhand == InteractionHand.OFF_HAND) {
        if (itemStack != null) {
            if (itemStack.getItem() == Items.LEAD) {
                ((ServerLevel) this.level).getChunkSource().broadcastAndSend(this, new ClientboundSetEntityLinkPacket(this, null));
                entityhuman.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY);
                new BukkitRunnable() {

                    @Override
                    public void run() {
                        if (entityhuman instanceof ServerPlayer) {
                            // TODO
                            entityhuman.setItemInHand(InteractionHand.OFF_HAND, itemStack);
                            Player p = (Player) entityhuman.getBukkitEntity();
                            if (!p.isOnline()) {
                                p.saveData();
                            }
                        }
                    }
                }.runTaskLater(MyPetApi.getPlugin(), 5);
            }
        }
        return InteractionResult.CONSUME;
    }
    if (super.handlePlayerInteraction(entityhuman, enumhand, itemStack).consumesAction()) {
        return InteractionResult.CONSUME;
    }
    if (getOwner().equals(entityhuman) && itemStack != null && canUseItem()) {
        if (itemStack.getItem() == Items.SADDLE && !getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
            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() == Items.SHEARS && getMyPet().hasSaddle() && getOwner().getPlayer().isSneaking()) {
            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().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.Pig.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 : MyPet(de.Keyle.MyPet.api.entity.MyPet) EntityMyPet(de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet) ClientboundSetEntityLinkPacket(net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket) Items(net.minecraft.world.item.Items) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MyPetApi(de.Keyle.MyPet.MyPetApi) MyPig(de.Keyle.MyPet.api.entity.types.MyPig) EntityDataSerializers(net.minecraft.network.syncher.EntityDataSerializers) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) CompatManager(de.Keyle.MyPet.compat.v1_18_R1.CompatManager) EntityDataAccessor(net.minecraft.network.syncher.EntityDataAccessor) InteractionResult(net.minecraft.world.InteractionResult) Player(org.bukkit.entity.Player) ServerLevel(net.minecraft.server.level.ServerLevel) InvocationTargetException(java.lang.reflect.InvocationTargetException) SynchedEntityData(net.minecraft.network.syncher.SynchedEntityData) ServerPlayer(net.minecraft.server.level.ServerPlayer) Configuration(de.Keyle.MyPet.api.Configuration) ItemEntity(net.minecraft.world.entity.item.ItemEntity) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) Player(org.bukkit.entity.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) ItemEntity(net.minecraft.world.entity.item.ItemEntity) ClientboundSetEntityLinkPacket(net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket) ServerPlayer(net.minecraft.server.level.ServerPlayer) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

MyPetApi (de.Keyle.MyPet.MyPetApi)2 Configuration (de.Keyle.MyPet.api.Configuration)2 EntitySize (de.Keyle.MyPet.api.entity.EntitySize)2 MyPet (de.Keyle.MyPet.api.entity.MyPet)2 MyPig (de.Keyle.MyPet.api.entity.types.MyPig)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ClientboundSetEntityLinkPacket (net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket)2 EntityDataAccessor (net.minecraft.network.syncher.EntityDataAccessor)2 EntityDataSerializers (net.minecraft.network.syncher.EntityDataSerializers)2 SynchedEntityData (net.minecraft.network.syncher.SynchedEntityData)2 ServerLevel (net.minecraft.server.level.ServerLevel)2 ServerPlayer (net.minecraft.server.level.ServerPlayer)2 InteractionHand (net.minecraft.world.InteractionHand)2 InteractionResult (net.minecraft.world.InteractionResult)2 ItemEntity (net.minecraft.world.entity.item.ItemEntity)2 ItemStack (net.minecraft.world.item.ItemStack)2 Items (net.minecraft.world.item.Items)2 Level (net.minecraft.world.level.Level)2 Player (org.bukkit.entity.Player)2 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)2