Search in sources :

Example 1 with Hand

use of net.minecraft.util.Hand in project AgriCraft by AgriCraft.

the class JournalViewPointHandler method toggle.

public boolean toggle(PlayerEntity player, Hand hand) {
    if (this.isActive(hand)) {
        this.setActive(hand, false);
        if (AgriCraft.instance.proxy().toggleDynamicCamera(this, false)) {
            this.journal = null;
            return true;
        } else {
            this.setActive(hand, true);
            this.journal = new JournalClientData(player, hand);
        }
    } else {
        Hand other = hand == Hand.MAIN_HAND ? Hand.OFF_HAND : Hand.MAIN_HAND;
        if (!this.isActive(other)) {
            this.setActive(hand, true);
            if (AgriCraft.instance.proxy().toggleDynamicCamera(this, true)) {
                this.journal = new JournalClientData(player, hand);
                return true;
            } else {
                this.setActive(hand, false);
                this.journal = null;
            }
        }
    }
    return false;
}
Also used : Hand(net.minecraft.util.Hand)

Example 2 with Hand

use of net.minecraft.util.Hand in project AgriCraft by AgriCraft.

the class VanillaSeedConversionHandler method runPlantingConversion.

protected boolean runPlantingConversion(World world, BlockPos pos, ItemStack stack, PlayerEntity player, Hand hand) {
    return !AgriCraft.instance.getConfig().convertSeedsOnlyInAnalyzer() && AgriApi.getGenomeAdapterizer().valueOf(stack).map(seed -> {
        // The player is attempting to plant a seed, convert it to an agricraft crop
        return AgriApi.getSoil(world, pos.down()).map(soil -> {
            // check if there are crop sticks above
            MutableBoolean consumed = new MutableBoolean(false);
            boolean cropSticks = AgriApi.getCrop(world, pos).map(crop -> {
                if (!crop.hasPlant() && !crop.isCrossCrop() && crop.plantGenome(seed, player)) {
                    if (player == null || !player.isCreative()) {
                        stack.shrink(1);
                        consumed.setValue(true);
                    }
                    if (player != null) {
                        player.swingArm(hand);
                    }
                }
                return true;
            }).orElse(false);
            // if there were crop sticks, return the result of the crop sticks action
            if (cropSticks) {
                return consumed.getValue();
            }
            // no crop sticks, try planting as a plant
            BlockState newState = AgriCraft.instance.getModBlockRegistry().crop_plant.getStateForPlacement(world, pos);
            if (newState != null && world.setBlockState(pos, newState, 11)) {
                boolean planted = AgriApi.getCrop(world, pos).map(crop -> crop.plantGenome(seed, player)).orElse(false);
                if (planted) {
                    // reduce stack size
                    if (player == null || !player.isCreative()) {
                        stack.shrink(1);
                    }
                    // return success
                    return true;
                } else {
                    world.setBlockState(pos, Blocks.AIR.getDefaultState());
                }
            }
            return false;
        }).orElse(false);
    }).orElse(false);
}
Also used : AgriCraft(com.infinityraider.agricraft.AgriCraft) AgriApi(com.infinityraider.agricraft.api.v1.AgriApi) PlayerEntity(net.minecraft.entity.player.PlayerEntity) LivingEntity(net.minecraft.entity.LivingEntity) Item(net.minecraft.item.Item) World(net.minecraft.world.World) Set(java.util.Set) BlockPos(net.minecraft.util.math.BlockPos) Sets(com.google.common.collect.Sets) Blocks(net.minecraft.block.Blocks) ItemStack(net.minecraft.item.ItemStack) TileEntitySeedAnalyzer(com.infinityraider.agricraft.content.core.TileEntitySeedAnalyzer) EventPriority(net.minecraftforge.eventbus.api.EventPriority) Event(net.minecraftforge.eventbus.api.Event) TileEntity(net.minecraft.tileentity.TileEntity) Hand(net.minecraft.util.Hand) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) BlockState(net.minecraft.block.BlockState) PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) IAgriSeedItem(com.infinityraider.agricraft.api.v1.content.items.IAgriSeedItem) BlockState(net.minecraft.block.BlockState) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean)

Example 3 with Hand

use of net.minecraft.util.Hand in project BluePower by Qmunity.

the class ItemSeedBag method useOn.

@Override
public ActionResultType useOn(ItemUseContext context) {
    PlayerEntity player = context.getPlayer();
    World worldIn = context.getLevel();
    Hand hand = context.getHand();
    BlockPos pos = context.getClickedPos();
    if (player.isCrouching()) {
        return ActionResultType.PASS;
    }
    ItemStackHandler seedBagInvHandler = new ItemStackHandler(9);
    // Get Active hand
    Hand activeHand = Hand.MAIN_HAND;
    ItemStack seedBag = player.getItemInHand(activeHand);
    if (!(seedBag.getItem() instanceof ItemSeedBag)) {
        seedBag = player.getOffhandItem();
        activeHand = Hand.OFF_HAND;
    }
    // Get Items from the NBT Handler
    if (seedBag.hasTag())
        seedBagInvHandler.deserializeNBT(seedBag.getTag().getCompound("inv"));
    ItemStack seed = getSeedType(player.getItemInHand(hand));
    Block block = Block.byItem(seed.getItem());
    if (!seed.isEmpty() && block instanceof IPlantable) {
        IPlantable plant = (IPlantable) block;
        BlockState b = worldIn.getBlockState(pos);
        if (b.getBlock().canSustainPlant(b, worldIn, pos, Direction.UP, plant) && worldIn.isEmptyBlock(pos.relative(Direction.UP))) {
            for (int i = 0; i < 9; i++) {
                ItemStack is = seedBagInvHandler.getStackInSlot(i);
                if (!is.isEmpty()) {
                    worldIn.setBlock(pos.relative(Direction.UP), block.defaultBlockState(), 0);
                    seedBagInvHandler.extractItem(i, 1, false);
                    break;
                }
            }
            // Update items in the NBT
            if (!seedBag.hasTag())
                seedBag.setTag(new CompoundNBT());
            if (seedBag.getTag() != null) {
                seedBag.getTag().put("inv", seedBagInvHandler.serializeNBT());
            }
            return ActionResultType.SUCCESS;
        }
    }
    return ActionResultType.PASS;
}
Also used : ItemStackHandler(net.minecraftforge.items.ItemStackHandler) BlockState(net.minecraft.block.BlockState) CompoundNBT(net.minecraft.nbt.CompoundNBT) IPlantable(net.minecraftforge.common.IPlantable) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) Hand(net.minecraft.util.Hand) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity)

Example 4 with Hand

use of net.minecraft.util.Hand in project BluePower by Qmunity.

the class ItemCropSeed method useOn.

@Override
public ActionResultType useOn(ItemUseContext itemUseContext) {
    PlayerEntity player = itemUseContext.getPlayer();
    Hand hand = itemUseContext.getHand();
    Direction facing = itemUseContext.getClickedFace();
    World world = itemUseContext.getLevel();
    BlockPos pos = itemUseContext.getClickedPos();
    ItemStack itemStack = player.getItemInHand(hand);
    if (facing.ordinal() != 1) {
        return ActionResultType.PASS;
    } else if (player.mayUseItemAt(pos, facing, itemStack) && player.mayUseItemAt(pos.above(), facing, itemStack)) {
        if (world.getBlockState(pos).getBlock().canSustainPlant(world.getBlockState(pos), world, pos, Direction.UP, this) && world.isEmptyBlock(pos.above()) && world.getBlockState(pos).getBlock() instanceof FarmlandBlock) {
            world.setBlock(pos.above(), field_150925_a.defaultBlockState(), 2);
            itemStack.setCount(itemStack.getCount() - 1);
            player.setItemInHand(hand, itemStack);
            return ActionResultType.SUCCESS;
        } else {
            return ActionResultType.PASS;
        }
    } else {
        return ActionResultType.PASS;
    }
}
Also used : BlockPos(net.minecraft.util.math.BlockPos) FarmlandBlock(net.minecraft.block.FarmlandBlock) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) Hand(net.minecraft.util.Hand) Direction(net.minecraft.util.Direction) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Example 5 with Hand

use of net.minecraft.util.Hand in project Geolosys by oitsjustjose.

the class ProPickItem method onItemUse.

@Override
public ActionResultType onItemUse(ItemUseContext context) {
    PlayerEntity player = context.getPlayer();
    Hand hand = context.getHand();
    World worldIn = context.getWorld();
    BlockPos pos = context.getPos();
    Direction facing = context.getFace();
    if (player.isCrouching()) {
        this.onItemRightClick(worldIn, player, hand);
    } else {
        if (!player.isCreative()) {
            this.attemptDamageItem(player, pos, hand, worldIn);
        }
        // At surface or higher
        if (worldIn.isRemote) {
            player.swingArm(hand);
            return ActionResultType.PASS;
        }
        ItemStack stack = player.getHeldItem(hand);
        int xStart;
        int xEnd;
        int yStart;
        int yEnd;
        int zStart;
        int zEnd;
        int confAmt = CommonConfig.PRO_PICK_RANGE.get();
        int confDmt = CommonConfig.PRO_PICK_DIAMETER.get();
        switch(facing) {
            case UP:
                xStart = -(confDmt / 2);
                xEnd = confDmt / 2;
                yStart = -confAmt;
                yEnd = 0;
                zStart = -(confDmt / 2);
                zEnd = (confDmt / 2);
                prospect(player, stack, worldIn, pos, facing, xStart, xEnd, yStart, yEnd, zStart, zEnd);
                break;
            case DOWN:
                xStart = -(confDmt / 2);
                xEnd = confDmt / 2;
                yStart = 0;
                yEnd = confAmt;
                zStart = -(confDmt / 2);
                zEnd = confDmt / 2;
                prospect(player, stack, worldIn, pos, facing, xStart, xEnd, yStart, yEnd, zStart, zEnd);
                break;
            case NORTH:
                xStart = -(confDmt / 2);
                xEnd = confDmt / 2;
                yStart = -(confDmt / 2);
                yEnd = confDmt / 2;
                zStart = 0;
                zEnd = confAmt;
                prospect(player, stack, worldIn, pos, facing, xStart, xEnd, yStart, yEnd, zStart, zEnd);
                break;
            case SOUTH:
                xStart = -(confDmt / 2);
                xEnd = confDmt / 2;
                yStart = -(confDmt / 2);
                yEnd = confDmt / 2;
                zStart = -confAmt;
                zEnd = 0;
                prospect(player, stack, worldIn, pos, facing, xStart, xEnd, yStart, yEnd, zStart, zEnd);
                break;
            case EAST:
                xStart = -(confAmt);
                xEnd = 0;
                yStart = -(confDmt / 2);
                yEnd = confDmt / 2;
                zStart = -(confDmt / 2);
                zEnd = confDmt / 2;
                prospect(player, stack, worldIn, pos, facing, xStart, xEnd, yStart, yEnd, zStart, zEnd);
                break;
            case WEST:
                xStart = 0;
                xEnd = confAmt;
                yStart = -(confDmt / 2);
                yEnd = confDmt / 2;
                zStart = -(confDmt / 2);
                zEnd = confDmt / 2;
                prospect(player, stack, worldIn, pos, facing, xStart, xEnd, yStart, yEnd, zStart, zEnd);
                break;
        }
        player.swingArm(hand);
    }
    return ActionResultType.SUCCESS;
}
Also used : BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) Hand(net.minecraft.util.Hand) Direction(net.minecraft.util.Direction) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Aggregations

Hand (net.minecraft.util.Hand)7 PlayerEntity (net.minecraft.entity.player.PlayerEntity)5 ItemStack (net.minecraft.item.ItemStack)4 BlockPos (net.minecraft.util.math.BlockPos)4 World (net.minecraft.world.World)4 BlockState (net.minecraft.block.BlockState)2 Direction (net.minecraft.util.Direction)2 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)2 Sets (com.google.common.collect.Sets)1 AgriCraft (com.infinityraider.agricraft.AgriCraft)1 AgriApi (com.infinityraider.agricraft.api.v1.AgriApi)1 IAgriSeedItem (com.infinityraider.agricraft.api.v1.content.items.IAgriSeedItem)1 CapabilitySeedBagContents (com.infinityraider.agricraft.capability.CapabilitySeedBagContents)1 TileEntitySeedAnalyzer (com.infinityraider.agricraft.content.core.TileEntitySeedAnalyzer)1 Set (java.util.Set)1 Nonnull (javax.annotation.Nonnull)1 Block (net.minecraft.block.Block)1 Blocks (net.minecraft.block.Blocks)1 FarmlandBlock (net.minecraft.block.FarmlandBlock)1 AbstractClientPlayerEntity (net.minecraft.client.entity.player.AbstractClientPlayerEntity)1