Search in sources :

Example 56 with InteractionHand

use of net.minecraft.world.InteractionHand in project MyPet by xXKeyleXx.

the class EntityMyZombieVillager 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 : ResourceLocation(net.minecraft.resources.ResourceLocation) MyZombieVillager(de.Keyle.MyPet.api.entity.types.MyZombieVillager) 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) Registry(net.minecraft.core.Registry) Configuration(de.Keyle.MyPet.api.Configuration) VillagerProfession(net.minecraft.world.entity.npc.VillagerProfession) VillagerType(net.minecraft.world.entity.npc.VillagerType) 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) MyVillager(de.Keyle.MyPet.api.entity.types.MyVillager) 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) VillagerData(net.minecraft.world.entity.npc.VillagerData) 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 57 with InteractionHand

use of net.minecraft.world.InteractionHand in project SpongeCommon by SpongePowered.

the class ServerGamePacketListenerImplMixin method impl$throwAnimationAndInteractEvents.

@SuppressWarnings("ConstantConditions")
@Inject(method = "handleAnimate", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;resetLastActionTime()V"), cancellable = true)
private void impl$throwAnimationAndInteractEvents(final ServerboundSwingPacket packetIn, final CallbackInfo ci) {
    if (PhaseTracker.getInstance().getPhaseContext().isEmpty()) {
        return;
    }
    final InteractionHand hand = packetIn.getHand();
    if (!((ServerPlayerGameModeAccessor) this.player.gameMode).accessor$isDestroyingBlock()) {
        if (this.impl$ignorePackets > 0) {
            this.impl$ignorePackets--;
        } else {
            if (ShouldFire.INTERACT_ITEM_EVENT_PRIMARY) {
                final Vec3 startPos = this.player.getEyePosition(1);
                // TODO hook for blockReachDistance?
                final Vec3 endPos = startPos.add(this.player.getLookAngle().scale(5d));
                HitResult result = this.player.getLevel().clip(new ClipContext(startPos, endPos, ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, this.player));
                if (result.getType() == HitResult.Type.MISS) {
                    final ItemStack heldItem = this.player.getItemInHand(hand);
                    SpongeCommonEventFactory.callInteractItemEventPrimary(this.player, heldItem, hand);
                }
            }
        }
    }
    if (ShouldFire.ANIMATE_HAND_EVENT) {
        final HandType handType = (HandType) (Object) hand;
        final ItemStack heldItem = this.player.getItemInHand(hand);
        try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
            frame.addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(heldItem));
            frame.addContext(EventContextKeys.USED_HAND, handType);
            final AnimateHandEvent event = SpongeEventFactory.createAnimateHandEvent(frame.currentCause(), handType, (Humanoid) this.player);
            if (SpongeCommon.post(event)) {
                ci.cancel();
            }
        }
    }
}
Also used : HitResult(net.minecraft.world.phys.HitResult) ClipContext(net.minecraft.world.level.ClipContext) HandType(org.spongepowered.api.data.type.HandType) CauseStackManager(org.spongepowered.api.event.CauseStackManager) InteractionHand(net.minecraft.world.InteractionHand) Vec3(net.minecraft.world.phys.Vec3) AnimateHandEvent(org.spongepowered.api.event.entity.living.AnimateHandEvent) ItemStack(net.minecraft.world.item.ItemStack) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 58 with InteractionHand

use of net.minecraft.world.InteractionHand in project MC-Prefab by Brian-Wuest.

the class StructureConfiguration method DamageHeldItem.

protected void DamageHeldItem(Player player, Item item) {
    ItemStack stack = player.getMainHandItem().getItem() == item ? player.getMainHandItem() : player.getOffhandItem();
    InteractionHand hand = player.getMainHandItem().getItem() == item ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND;
    ItemStack copy = stack.copy();
    stack.hurtAndBreak(1, player, (player1) -> {
        player1.broadcastBreakEvent(hand);
    });
    if (stack.isEmpty()) {
        net.minecraftforge.event.ForgeEventFactory.onPlayerDestroyItem(player, copy, hand);
        EquipmentSlot slotType = hand == InteractionHand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND;
        player.setItemSlot(slotType, ItemStack.EMPTY);
    }
    player.containerMenu.broadcastChanges();
}
Also used : InteractionHand(net.minecraft.world.InteractionHand) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) ItemStack(net.minecraft.world.item.ItemStack)

Aggregations

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