Example usage for net.minecraftforge.common IShearable onSheared

List of usage examples for net.minecraftforge.common IShearable onSheared

Introduction

In this page you can find the example usage for net.minecraftforge.common IShearable onSheared.

Prototype

@Nonnull
default List<ItemStack> onSheared(@Nonnull ItemStack item, IWorld world, BlockPos pos, int fortune) 

Source Link

Document

Performs the shear function on this object.

Usage

From source file:com.lothrazar.cyclicmagic.block.BlockShears.java

License:Open Source License

@Override
public void onEntityCollision(World world, BlockPos pos, IBlockState state, Entity entity) {
    if (entity instanceof IShearable) {
        IShearable sheep = (IShearable) entity;
        ItemStack fake = new ItemStack(Items.SHEARS);
        if (sheep.isShearable(fake, world, pos)) {
            List<ItemStack> drops = sheep.onSheared(fake, world, pos, FORTUNE);//since iShearable doesnt do drops, but DOES do sound/make sheep naked
            UtilItemStack.dropItemStacksInWorld(world, pos, drops);
        }//from w  w w . ja v  a  2 s  . co  m
    }
}

From source file:com.lothrazar.cyclicmagic.item.shears.EntityShearingBolt.java

License:Open Source License

@Override
protected void processImpact(RayTraceResult mop) {
    World world = getEntityWorld();// w  ww . ja  va  2  s.  c o  m
    //process entity hit if any
    if (mop.entityHit != null && mop.entityHit instanceof IShearable) {
        try {
            IShearable target = (IShearable) mop.entityHit;
            BlockPos ePos = mop.entityHit.getPosition();
            if (target.isShearable(null, world, ePos)) {
                java.util.List<ItemStack> drops = target.onSheared(null, world, ePos, FORTUNE);
                UtilItemStack.dropItemStacksInWorld(world, ePos, drops);
                UtilSound.playSound(world, ePos, SoundEvents.ENTITY_SHEEP_SHEAR, SoundCategory.NEUTRAL);
                this.setDead();
            }
        } catch (Exception e) { //keep because a modded entity could be shearable and have issues
            // https://github.com/PrinceOfAmber/Cyclic/issues/120
            ModCyclic.logger.error("Error shearing entity from projectile ", e);
        }
    }
    if (this.isDead || mop.getBlockPos() == null) {
        return;
    }
    //process block hit if its shearable
    BlockPos pos = mop.getBlockPos();
    //process block hit if its shearable
    Block block = world.getBlockState(pos).getBlock();
    if (block instanceof net.minecraftforge.common.IShearable) {
        net.minecraftforge.common.IShearable target = (net.minecraftforge.common.IShearable) block;
        if (target.isShearable(null, world, pos)) {
            java.util.List<ItemStack> drops = target.onSheared(null, world, pos, FORTUNE);
            for (ItemStack stack : drops) {
                float f = 0.7F;
                double d = world.rand.nextFloat() * f + (1.0F - f) * 0.5D;
                double d1 = world.rand.nextFloat() * f + (1.0F - f) * 0.5D;
                double d2 = world.rand.nextFloat() * f + (1.0F - f) * 0.5D;
                net.minecraft.entity.item.EntityItem entityitem = new net.minecraft.entity.item.EntityItem(
                        world, pos.getX() + d, pos.getY() + d1, pos.getZ() + d2, stack);
                // entityitem.setDefaultPickupDelay();
                if (world.isRemote == false) {
                    world.spawnEntity(entityitem);
                    world.setBlockToAir(pos);
                }
                UtilSound.playSound(world, pos, SoundEvents.ENTITY_SHEEP_SHEAR, SoundCategory.BLOCKS);
            }
            this.setDead();
        }
    }
}

From source file:com.lothrazar.cyclicmagic.item.shears.ItemShearsRanged.java

License:Open Source License

/**
 * Returns true if the item can be used on the given entity, e.g. shears on sheep. COPY from vanilla SHEARS
 *///from w  w w.  j av a  2s  .  c  o  m
@Override
public boolean itemInteractionForEntity(ItemStack itemstack, net.minecraft.entity.player.EntityPlayer player,
        EntityLivingBase entity, net.minecraft.util.EnumHand hand) {
    if (entity.world.isRemote) {
        return false;
    }
    if (entity instanceof net.minecraftforge.common.IShearable) {
        net.minecraftforge.common.IShearable target = (net.minecraftforge.common.IShearable) entity;
        BlockPos pos = new BlockPos(entity.posX, entity.posY, entity.posZ);
        if (target.isShearable(itemstack, entity.world, pos)) {
            java.util.List<ItemStack> drops = target.onSheared(itemstack, entity.world, pos,
                    net.minecraft.enchantment.EnchantmentHelper
                            .getEnchantmentLevel(net.minecraft.init.Enchantments.FORTUNE, itemstack));
            java.util.Random rand = new java.util.Random();
            for (ItemStack stack : drops) {
                net.minecraft.entity.item.EntityItem ent = entity.entityDropItem(stack, 1.0F);
                ent.motionY += rand.nextFloat() * 0.05F;
                ent.motionX += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
                ent.motionZ += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
            }
            itemstack.damageItem(1, entity);
        }
        return true;
    }
    return false;
}

From source file:com.lothrazar.cyclicmagic.item.shears.ItemShearsRanged.java

License:Open Source License

/**
 * //from  w w w  .  ja v  a 2 s  .  com
 * COPY from vanilla SHEARS
 */
@Override
public boolean onBlockStartBreak(ItemStack itemstack, BlockPos pos,
        net.minecraft.entity.player.EntityPlayer player) {
    if (player.world.isRemote || player.capabilities.isCreativeMode) {
        return false;
    }
    Block block = player.world.getBlockState(pos).getBlock();
    if (block instanceof net.minecraftforge.common.IShearable) {
        net.minecraftforge.common.IShearable target = (net.minecraftforge.common.IShearable) block;
        if (target.isShearable(itemstack, player.world, pos)) {
            java.util.List<ItemStack> drops = target.onSheared(itemstack, player.world, pos,
                    net.minecraft.enchantment.EnchantmentHelper
                            .getEnchantmentLevel(net.minecraft.init.Enchantments.FORTUNE, itemstack));
            java.util.Random rand = new java.util.Random();
            for (ItemStack stack : drops) {
                float f = 0.7F;
                double d = rand.nextFloat() * f + (1.0F - f) * 0.5D;
                double d1 = rand.nextFloat() * f + (1.0F - f) * 0.5D;
                double d2 = rand.nextFloat() * f + (1.0F - f) * 0.5D;
                net.minecraft.entity.item.EntityItem entityitem = new net.minecraft.entity.item.EntityItem(
                        player.world, pos.getX() + d, pos.getY() + d1, pos.getZ() + d2, stack);
                entityitem.setDefaultPickupDelay();
                player.world.spawnEntity(entityitem);
            }
            itemstack.damageItem(1, player);
            player.addStat(net.minecraft.stats.StatList.getBlockStats(block));
            player.world.setBlockState(pos, Blocks.AIR.getDefaultState(), 11); //TODO: Move to IShearable implementors in 1.12+
            return true;
        }
    }
    return false;
}

From source file:jayavery.geomastery.items.ItemShears.java

License:Open Source License

@Override
public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer player, EntityLivingBase entity,
        EnumHand hand) {/* w ww.j  a  va 2 s .co  m*/

    if (entity.world.isRemote) {

        return false;
    }

    if (entity instanceof IShearable) {

        IShearable target = (IShearable) entity;

        BlockPos pos = new BlockPos(entity.posX, entity.posY, entity.posZ);

        if (target.isShearable(stack, entity.world, pos)) {

            target.onSheared(stack, entity.world, pos, 0);
            entity.dropItem(GeoItems.WOOL, this.yield.apply(entity.world.rand));

            stack.damageItem(1, entity);
        }

        return true;
    }

    return false;
}

From source file:org.spongepowered.mod.mixin.core.item.MixinItemShears.java

License:MIT License

/**
 * @author gabizou - June 21st, 2016/*from   w w w.  j a va  2  s .c o m*/
 * @reason Rewrites the forge handling of this to properly handle
 * when sheared drops are captured by whatever current phase the
 * {@link CauseTracker} is in.
 *
 * Returns true if the item can be used on the given entity, e.g. shears on sheep.
 */
@Overwrite
@Override
public boolean itemInteractionForEntity(ItemStack itemstack, EntityPlayer player, EntityLivingBase entity,
        EnumHand hand) {
    if (entity.worldObj.isRemote) {
        return false;
    }
    if (entity instanceof IShearable) {
        IShearable target = (IShearable) entity;
        BlockPos pos = new BlockPos(entity.posX, entity.posY, entity.posZ);
        if (target.isShearable(itemstack, entity.worldObj, pos)) {
            List<ItemStack> drops = target.onSheared(itemstack, entity.worldObj, pos,
                    EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, itemstack));
            // Sponge Start - Handle drops according to the current phase
            final CauseTracker causeTracker = ((IMixinWorldServer) entity.worldObj).getCauseTracker();
            final PhaseData currentData = causeTracker.getCurrentPhaseData();
            final IPhaseState currentState = currentData.state;
            final PhaseContext phaseContext = currentData.context;
            final Random random = EntityUtil.fromNative(entity).getRandom();
            final IMixinEntity mixinEntity = EntityUtil.toMixin(entity);
            final double posX = entity.posX;
            final double posY = entity.posY + 1.0F;
            final double posZ = entity.posZ;
            final Vector3d position = new Vector3d(posX, posY, posZ);
            // Now the real fun begins.
            for (ItemStack drop : drops) {
                final ItemStack item;

                if (drop.getItem() != null) {
                    // FIRST we want to throw the DropItemEvent.PRE
                    final ItemStackSnapshot snapshot = ItemStackUtil.createSnapshot(drop);
                    final List<ItemStackSnapshot> original = new ArrayList<>();
                    original.add(snapshot);
                    final DropItemEvent.Pre dropEvent = SpongeEventFactory.createDropItemEventPre(
                            Cause.of(NamedCause.source(entity)), ImmutableList.of(snapshot), original);
                    if (dropEvent.isCancelled()) {
                        continue;
                    }

                    // SECOND throw the ConstructEntityEvent
                    Transform<World> suggested = new Transform<>(mixinEntity.getWorld(), position);
                    SpawnCause cause = EntitySpawnCause.builder().entity(mixinEntity)
                            .type(SpawnTypes.DROPPED_ITEM).build();
                    ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(
                            Cause.of(NamedCause.source(cause)), EntityTypes.ITEM, suggested);
                    SpongeImpl.postEvent(event);
                    item = event.isCancelled() ? null
                            : ItemStackUtil.fromSnapshotToNative(dropEvent.getDroppedItems().get(0));
                } else {
                    continue;
                }
                if (item == null) {
                    continue;
                }
                if (item.stackSize != 0 && item.getItem() != null) {
                    if (!currentState.getPhase().ignoresItemPreMerging(currentState) && SpongeImpl
                            .getGlobalConfig().getConfig().getOptimizations().doDropsPreMergeItemDrops()) {
                        if (currentState.tracksEntitySpecificDrops()) {
                            final Multimap<UUID, ItemDropData> multimap = phaseContext
                                    .getCapturedEntityDropSupplier().get();
                            final Collection<ItemDropData> itemStacks = multimap.get(entity.getUniqueID());
                            SpongeImplHooks.addItemStackToListForSpawning(itemStacks, ItemDropData.item(item)
                                    .motion(new Vector3d((random.nextFloat() - random.nextFloat()) * 0.1F,
                                            random.nextFloat() * 0.05F,
                                            (random.nextFloat() - random.nextFloat()) * 0.1F))
                                    .position(new Vector3d(posX, posY, posZ)).build());
                            continue;
                        } else {
                            final List<ItemDropData> itemStacks = phaseContext.getCapturedItemStackSupplier()
                                    .get();
                            SpongeImplHooks.addItemStackToListForSpawning(itemStacks, ItemDropData.item(item)
                                    .position(new Vector3d(posX, posY, posZ))
                                    .motion(new Vector3d((random.nextFloat() - random.nextFloat()) * 0.1F,
                                            random.nextFloat() * 0.05F,
                                            (random.nextFloat() - random.nextFloat()) * 0.1F))
                                    .build());
                            continue;
                        }
                    }
                    EntityItem entityitem = new EntityItem(entity.worldObj, posX, posY, posZ, item);
                    entityitem.setDefaultPickupDelay();
                    entityitem.motionY += random.nextFloat() * 0.05F;
                    entityitem.motionX += (random.nextFloat() - random.nextFloat()) * 0.1F;
                    entityitem.motionZ += (random.nextFloat() - random.nextFloat()) * 0.1F;

                    // FIFTH - Capture the entity maybe?
                    if (currentState.getPhase().doesCaptureEntityDrops(currentState)) {
                        if (currentState.tracksEntitySpecificDrops()) {
                            // We are capturing per entity drop
                            phaseContext.getCapturedEntityItemDropSupplier().get().put(entity.getUniqueID(),
                                    entityitem);
                        } else {
                            // We are adding to a general list - usually for EntityPhase.State.DEATH
                            phaseContext.getCapturedItemsSupplier().get().add(entityitem);
                        }
                        // Return the item, even if it wasn't spawned in the world.
                        continue;
                    }
                    // FINALLY - Spawn the entity in the world if all else didn't fail
                    entity.worldObj.spawnEntityInWorld(entityitem);

                }
            }

            // Sponge End
            itemstack.damageItem(1, entity);
        }
        return true;
    }
    return false;
}