Search in sources :

Example 6 with IShearable

use of net.minecraftforge.common.IShearable in project BetterWithAddons by DaedalusGame.

the class ItemPickaxeConvenient method onBlockStartBreak.

@Override
public boolean onBlockStartBreak(ItemStack itemstack, BlockPos pos, EntityPlayer player) {
    if (player.world.isRemote || player.capabilities.isCreativeMode) {
        return false;
    }
    IBlockState state = player.world.getBlockState(pos);
    Block block = state.getBlock();
    if (canShear(itemstack, state) && block instanceof IShearable) {
        IShearable target = (IShearable) block;
        if (target.isShearable(itemstack, player.world, pos)) {
            List<ItemStack> drops = target.onSheared(itemstack, player.world, pos, getEnchantmentLevel(net.minecraft.init.Enchantments.FORTUNE, itemstack));
            for (ItemStack stack : drops) {
                InventoryUtil.addItemToPlayer(player, stack);
            }
            itemstack.damageItem(1, player);
            player.addStat(getBlockStats(block));
            // TODO: Move to IShearable implementors in 1.12+
            player.world.setBlockState(pos, Blocks.AIR.getDefaultState(), 11);
            return true;
        }
    }
    return false;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) IShearable(net.minecraftforge.common.IShearable) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack)

Example 7 with IShearable

use of net.minecraftforge.common.IShearable in project SpongeForge by SpongePowered.

the class MixinItemShears method itemInteractionForEntity.

/**
 * @author gabizou - June 21st, 2016
 * @reason Rewrites the forge handling of this to properly handle
 * when sheared drops are captured by whatever current phase the
 * {@link PhaseTracker} 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.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(itemstack, entity.world, pos)) {
            List<ItemStack> drops = target.onSheared(itemstack, entity.world, pos, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, itemstack));
            // Sponge Start - Handle drops according to the current phase
            final PhaseTracker phaseTracker = PhaseTracker.getInstance();
            final PhaseData currentData = phaseTracker.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.isEmpty()) {
                    try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
                        // FIRST we want to throw the DropItemEvent.PRE
                        final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(drop);
                        final List<ItemStackSnapshot> original = new ArrayList<>();
                        original.add(snapshot);
                        Sponge.getCauseStackManager().pushCause(entity);
                        final DropItemEvent.Pre dropEvent = SpongeEventFactory.createDropItemEventPre(Sponge.getCauseStackManager().getCurrentCause(), ImmutableList.of(snapshot), original);
                        if (dropEvent.isCancelled()) {
                            continue;
                        }
                        // SECOND throw the ConstructEntityEvent
                        Transform<World> suggested = new Transform<>(mixinEntity.getWorld(), position);
                        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
                        ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), EntityTypes.ITEM, suggested);
                        SpongeImpl.postEvent(event);
                        item = event.isCancelled() ? null : ItemStackUtil.fromSnapshotToNative(dropEvent.getDroppedItems().get(0));
                    }
                } else {
                    continue;
                }
                if (item == null) {
                    continue;
                }
                if (!item.isEmpty()) {
                    if (!currentState.ignoresItemPreMerging() && 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;
                        }
                        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.world, 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.doesCaptureEntityDrops()) {
                        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.world.spawnEntity(entityitem);
                }
            }
            // Sponge End
            itemstack.damageItem(1, entity);
        }
        return true;
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) World(org.spongepowered.api.world.World) Random(java.util.Random) CauseStackManager(org.spongepowered.api.event.CauseStackManager) IShearable(net.minecraftforge.common.IShearable) BlockPos(net.minecraft.util.math.BlockPos) UUID(java.util.UUID) EntityItem(net.minecraft.entity.item.EntityItem) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) PhaseData(org.spongepowered.common.event.tracking.PhaseData) ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Vector3d(com.flowpowered.math.vector.Vector3d) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(net.minecraft.item.ItemStack) Transform(org.spongepowered.api.entity.Transform) ItemDropData(org.spongepowered.common.event.tracking.context.ItemDropData) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 8 with IShearable

use of net.minecraftforge.common.IShearable in project EnderIO by SleepyTrousers.

the class ItemDarkSteelShears method onBlockStartBreak.

@Override
public boolean onBlockStartBreak(@Nonnull ItemStack itemstack, @Nonnull BlockPos pos, @Nonnull EntityPlayer player) {
    if (player.world.isRemote) {
        return false;
    }
    int powerStored = getStoredPower(player);
    if (powerStored < Config.darkSteelShearsPowerUsePerDamagePoint) {
        return super.onBlockStartBreak(itemstack, pos, player);
    }
    List<BlockPos> res = new ArrayList<BlockPos>();
    int x = pos.getX();
    int y = pos.getY();
    int z = pos.getZ();
    for (int dx = -Config.darkSteelShearsBlockAreaBoostWhenPowered; dx <= Config.darkSteelShearsBlockAreaBoostWhenPowered; dx++) {
        for (int dy = -Config.darkSteelShearsBlockAreaBoostWhenPowered; dy <= Config.darkSteelShearsBlockAreaBoostWhenPowered; dy++) {
            for (int dz = -Config.darkSteelShearsBlockAreaBoostWhenPowered; dz <= Config.darkSteelShearsBlockAreaBoostWhenPowered; dz++) {
                Block block2 = player.world.getBlockState(new BlockPos(x + dx, y + dy, z + dz)).getBlock();
                if (block2 instanceof IShearable && ((IShearable) block2).isShearable(itemstack, player.world, new BlockPos(x + dx, y + dy, z + dz))) {
                    res.add(new BlockPos(x + dx, y + dy, z + dz));
                }
            }
        }
    }
    NNList<BlockPos> sortedTargets = new NNList<BlockPos>(res);
    harvestComparator.refPoint = pos;
    Collections.sort(sortedTargets, harvestComparator);
    int maxBlocks = Math.min(sortedTargets.size(), powerStored / Config.darkSteelShearsPowerUsePerDamagePoint);
    for (int i = 0; i < maxBlocks; i++) {
        BlockPos bc2 = sortedTargets.get(i);
        super.onBlockStartBreak(itemstack, bc2, player);
        if (bc2 != pos) {
            player.world.setBlockToAir(bc2);
        }
    }
    return false;
}
Also used : NNList(com.enderio.core.common.util.NNList) ArrayList(java.util.ArrayList) IShearable(net.minecraftforge.common.IShearable) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos)

Example 9 with IShearable

use of net.minecraftforge.common.IShearable in project EnderIO by SleepyTrousers.

the class FlowerPicker method harvestBlock.

@Override
public IHarvestResult harvestBlock(@Nonnull final IFarmer farm, @Nonnull final BlockPos pos, @Nonnull Block block, @Nonnull IBlockState meta) {
    final World world = farm.getWorld();
    NNList<ItemStack> drops = new NNList<>();
    if (block instanceof IShearable) {
        if (!farm.hasTool(FarmingTool.SHEARS)) {
            farm.setNotification(FarmNotification.NO_SHEARS);
            return null;
        }
        ItemStack shears = farm.getTool(FarmingTool.SHEARS);
        if (!((IShearable) block).isShearable(shears, world, pos)) {
            return null;
        }
        drops.addAll(((IShearable) block).onSheared(shears, world, pos, farm.getLootingValue(FarmingTool.SHEARS)));
        farm.registerAction(FarmingAction.HARVEST, FarmingTool.SHEARS, meta, pos);
    } else {
        if (!farm.hasTool(FarmingTool.HOE)) {
            farm.setNotification(FarmNotification.NO_HOE);
            return null;
        }
        block.getDrops(drops, world, pos, meta, farm.getLootingValue(FarmingTool.HOE));
        farm.registerAction(FarmingAction.HARVEST, FarmingTool.HOE, meta, pos);
    }
    final NNList<EntityItem> result = new NNList<EntityItem>();
    NNList.wrap(drops).apply(new Callback<ItemStack>() {

        @Override
        public void apply(@Nonnull ItemStack drop) {
            result.add(new EntityItem(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, drop.copy()));
        }
    });
    world.setBlockToAir(pos);
    return new HarvestResult(result, pos);
}
Also used : IHarvestResult(crazypants.enderio.api.farm.IHarvestResult) NNList(com.enderio.core.common.util.NNList) IShearable(net.minecraftforge.common.IShearable) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Example 10 with IShearable

use of net.minecraftforge.common.IShearable in project GregTech by GregTechCE.

the class ToolUtility method applyShearBehavior.

public static boolean applyShearBehavior(ItemStack itemStack, BlockPos pos, EntityPlayer player) {
    Block block = player.world.getBlockState(pos).getBlock();
    if (block instanceof IShearable) {
        IShearable target = (IShearable) block;
        if (target.isShearable(itemStack, player.world, pos)) {
            int fortuneLevel = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, itemStack);
            List<ItemStack> drops = target.onSheared(itemStack, player.world, pos, fortuneLevel);
            dropListOfItems(player.world, pos, drops);
            player.world.setBlockState(pos, Blocks.AIR.getDefaultState(), 11);
            return true;
        }
    }
    return false;
}
Also used : IShearable(net.minecraftforge.common.IShearable) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack)

Aggregations

IShearable (net.minecraftforge.common.IShearable)16 Block (net.minecraft.block.Block)12 ItemStack (net.minecraft.item.ItemStack)11 IBlockState (net.minecraft.block.state.IBlockState)7 BlockPos (net.minecraft.util.math.BlockPos)5 EntityItem (net.minecraft.entity.item.EntityItem)4 IPlantable (net.minecraftforge.common.IPlantable)4 NNList (com.enderio.core.common.util.NNList)3 ArrayList (java.util.ArrayList)3 Entity (net.minecraft.entity.Entity)3 BlockLeaves (net.minecraft.block.BlockLeaves)2 World (net.minecraft.world.World)2 BlockBOPMushroom (biomesoplenty.common.block.BlockBOPMushroom)1 Vector3d (com.flowpowered.math.vector.Vector3d)1 IHarvestResult (crazypants.enderio.api.farm.IHarvestResult)1 FarmingTool (crazypants.enderio.base.farming.FarmingTool)1 Random (java.util.Random)1 UUID (java.util.UUID)1 IGrowable (net.minecraft.block.IGrowable)1 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1