Search in sources :

Example 1 with InteractionHand

use of net.minecraft.world.InteractionHand in project MinecraftForge by MinecraftForge.

the class FakePlayerTest method registerCommands.

@SubscribeEvent
public void registerCommands(RegisterCommandsEvent event) {
    event.getDispatcher().register(Commands.literal("fakeplayer").then(Commands.literal("attack").then(Commands.argument("target", EntityArgument.entity()).executes(context -> {
        FakePlayerFactory.getMinecraft(context.getSource().getLevel()).attack(EntityArgument.getEntity(context, "target"));
        return 1;
    }))).then(Commands.literal("opencontainer").executes(context -> {
        ServerPlayer fakePlayer = FakePlayerFactory.getMinecraft(context.getSource().getLevel());
        InteractionHand hand = InteractionHand.MAIN_HAND;
        ItemStack stack = Items.WRITABLE_BOOK.getDefaultInstance();
        fakePlayer.setItemInHand(hand, stack);
        fakePlayer.openItemGui(stack, hand);
        return 1;
    })));
}
Also used : ServerPlayer(net.minecraft.server.level.ServerPlayer) InteractionHand(net.minecraft.world.InteractionHand) ItemStack(net.minecraft.world.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 2 with InteractionHand

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

the class EntityMyEnderman 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 && canUseItem()) {
        if (itemStack.getItem() == Items.SHEARS && getMyPet().hasBlock() && getOwner().getPlayer().isSneaking()) {
            ItemEntity entityitem = new ItemEntity(this.level, this.getX(), this.getY() + 1, this.getZ(), CraftItemStack.asNMSCopy(getMyPet().getBlock()));
            entityitem.pickupDelay = 10;
            entityitem.setDeltaMovement(entityitem.getDeltaMovement().add(0, this.random.nextFloat() * 0.05F, 0));
            makeSound("entity.sheep.shear", 1.0F, 1.0F);
            getMyPet().setBlock(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 (getMyPet().getBlock() == null && Util.isBetween(1, 255, Item.getId(itemStack.getItem())) && getOwner().getPlayer().isSneaking()) {
            getMyPet().setBlock(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 : CraftMagicNumbers(org.bukkit.craftbukkit.v1_17_R1.util.CraftMagicNumbers) MyPet(de.Keyle.MyPet.api.entity.MyPet) Util(de.Keyle.MyPet.api.Util) Items(net.minecraft.world.item.Items) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) Item(net.minecraft.world.item.Item) Behavior(de.Keyle.MyPet.api.skill.skills.Behavior) EntityDataSerializers(net.minecraft.network.syncher.EntityDataSerializers) BlockState(net.minecraft.world.level.block.state.BlockState) BehaviorImpl(de.Keyle.MyPet.skill.skills.BehaviorImpl) EntityDataAccessor(net.minecraft.network.syncher.EntityDataAccessor) InteractionResult(net.minecraft.world.InteractionResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) Player(net.minecraft.world.entity.player.Player) 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) CompatManager(de.Keyle.MyPet.compat.v1_17_R1.CompatManager) MyEnderman(de.Keyle.MyPet.api.entity.types.MyEnderman) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) ItemEntity(net.minecraft.world.entity.item.ItemEntity)

Example 3 with InteractionHand

use of net.minecraft.world.InteractionHand 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) ServerLevel(net.minecraft.server.level.ServerLevel) Configuration(de.Keyle.MyPet.api.Configuration) CompatManager(de.Keyle.MyPet.compat.v1_17_R1.CompatManager) Bukkit(org.bukkit.Bukkit) MyPet(de.Keyle.MyPet.api.entity.MyPet) Util(de.Keyle.MyPet.api.Util) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) MyFox(de.Keyle.MyPet.api.entity.types.MyFox) MyPetApi(de.Keyle.MyPet.MyPetApi) 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) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) 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) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) ItemEntity(net.minecraft.world.entity.item.ItemEntity) EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot) ItemStack(net.minecraft.world.item.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack)

Example 4 with InteractionHand

use of net.minecraft.world.InteractionHand 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) Items(net.minecraft.world.item.Items) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) MyPetApi(de.Keyle.MyPet.MyPetApi) 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) MeleeAttack(de.Keyle.MyPet.compat.v1_17_R1.entity.ai.attack.MeleeAttack) InvocationTargetException(java.lang.reflect.InvocationTargetException) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) Player(net.minecraft.world.entity.player.Player) MyGiant(de.Keyle.MyPet.api.entity.types.MyGiant) 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) CompatManager(de.Keyle.MyPet.compat.v1_17_R1.CompatManager) Bukkit(org.bukkit.Bukkit) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) ItemEntity(net.minecraft.world.entity.item.ItemEntity) EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot) ItemStack(net.minecraft.world.item.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack)

Example 5 with InteractionHand

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

the class EntityMyHusk 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) EntityDataSerializers(net.minecraft.network.syncher.EntityDataSerializers) ServerLevel(net.minecraft.server.level.ServerLevel) Configuration(de.Keyle.MyPet.api.Configuration) CompatManager(de.Keyle.MyPet.compat.v1_17_R1.CompatManager) Bukkit(org.bukkit.Bukkit) MyPet(de.Keyle.MyPet.api.entity.MyPet) Util(de.Keyle.MyPet.api.Util) EntitySize(de.Keyle.MyPet.api.entity.EntitySize) MyPetApi(de.Keyle.MyPet.MyPetApi) 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) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) Player(net.minecraft.world.entity.player.Player) MyHusk(de.Keyle.MyPet.api.entity.types.MyHusk) 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) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) ItemEntity(net.minecraft.world.entity.item.ItemEntity) EquipmentSlot(de.Keyle.MyPet.api.entity.EquipmentSlot) ItemStack(net.minecraft.world.item.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack)

Aggregations

InteractionHand (net.minecraft.world.InteractionHand)54 ItemStack (net.minecraft.world.item.ItemStack)54 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