Search in sources :

Example 66 with BlockState

use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.

the class GuiPlugin method onSeedAnalyzerRightClick.

public void onSeedAnalyzerRightClick(PlayerInteractEvent.RightClickBlock event) {
    BlockPos pos = event.getPos();
    BlockState state = event.getWorld().getBlockState(pos);
    if (event.getPlayer().isSneaking()) {
        return;
    }
    if (state.getBlock() != AgriCraft.instance.getModBlockRegistry().seed_analyzer.getBlock()) {
        return;
    }
    event.setCancellationResult(ActionResultType.SUCCESS);
    event.setCanceled(true);
    if (event.getPlayer().world.isRemote) {
        return;
    }
    INamedContainerProvider containerProvider = new INamedContainerProvider() {

        @Nonnull
        @Override
        public ITextComponent getDisplayName() {
            return new TranslationTextComponent("screen.agricraft.seed_analyzer");
        }

        @Override
        public Container createMenu(int id, @Nonnull PlayerInventory playerInventory, @Nonnull PlayerEntity player) {
            return new SeedAnalyzerContainer(id, event.getWorld(), playerInventory, pos);
        }
    };
    NetworkHooks.openGui((ServerPlayerEntity) event.getPlayer(), containerProvider, pos);
}
Also used : BlockState(net.minecraft.block.BlockState) Nonnull(javax.annotation.Nonnull) SeedAnalyzerContainer(com.infinityraider.agricraft.plugins.agrigui.analyzer.SeedAnalyzerContainer) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) BlockPos(net.minecraft.util.math.BlockPos) INamedContainerProvider(net.minecraft.inventory.container.INamedContainerProvider) PlayerInventory(net.minecraft.entity.player.PlayerInventory) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity)

Example 67 with BlockState

use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.

the class ItemDynamicAgriSeed method onItemUse.

@Nonnull
@Override
public ActionResultType onItemUse(@Nonnull ItemUseContext context) {
    World world = context.getWorld();
    BlockPos pos = context.getPos();
    TileEntity tile = world.getTileEntity(pos);
    ItemStack stack = context.getItem();
    PlayerEntity player = context.getPlayer();
    // If crop sticks were clicked, attempt to plant the seed
    if (tile instanceof TileEntityCropSticks) {
        return this.attemptSeedPlant((TileEntityCropSticks) tile, stack, player);
    }
    // If a soil was clicked, check the block on top of the soil and handle accordingly
    return AgriApi.getSoil(world, pos).map(soil -> {
        BlockPos up = pos.up();
        TileEntity above = world.getTileEntity(up);
        // There are currently crop sticks on the soil, attempt to plant on the crop sticks
        if (above instanceof TileEntityCropSticks) {
            return this.attemptSeedPlant((TileEntityCropSticks) above, stack, player);
        }
        // There are currently no crop sticks, check if the place is suitable and plant the plant directly
        if (above == null && AgriCraft.instance.getConfig().allowPlantingOutsideCropSticks()) {
            BlockState newState = AgriCraft.instance.getModBlockRegistry().crop_plant.getStateForPlacement(world, up);
            if (newState != null && world.setBlockState(up, newState, 11)) {
                boolean success = AgriApi.getCrop(world, up).map(crop -> this.getGenome(context.getItem()).map(genome -> crop.plantGenome(genome, player)).map(result -> {
                    if (result) {
                        // consume item
                        if (player == null || !player.isCreative()) {
                            stack.shrink(1);
                        }
                    }
                    return result;
                }).orElse(false)).orElse(false);
                if (success) {
                    return ActionResultType.SUCCESS;
                } else {
                    world.setBlockState(up, Blocks.AIR.getDefaultState());
                }
            }
        }
        // Neither alternative option was successful, delegate the call to the super method
        return super.onItemUse(context);
    }).orElse(super.onItemUse(context));
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) NoPlant(com.infinityraider.agricraft.impl.v1.plant.NoPlant) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn) CompoundNBT(net.minecraft.nbt.CompoundNBT) IAgriGenome(com.infinityraider.agricraft.api.v1.genetics.IAgriGenome) ITooltipFlag(net.minecraft.client.util.ITooltipFlag) IWorldReader(net.minecraft.world.IWorldReader) ItemUseContext(net.minecraft.item.ItemUseContext) ITextComponent(net.minecraft.util.text.ITextComponent) Dist(net.minecraftforge.api.distmarker.Dist) ItemStack(net.minecraft.item.ItemStack) Names(com.infinityraider.agricraft.reference.Names) ItemBase(com.infinityraider.infinitylib.item.ItemBase) NonNullList(net.minecraft.util.NonNullList) BlockState(net.minecraft.block.BlockState) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) IAgriSeedItem(com.infinityraider.agricraft.api.v1.content.items.IAgriSeedItem) AgriCraft(com.infinityraider.agricraft.AgriCraft) IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) AgriApi(com.infinityraider.agricraft.api.v1.AgriApi) PlayerEntity(net.minecraft.entity.player.PlayerEntity) World(net.minecraft.world.World) BlockPos(net.minecraft.util.math.BlockPos) Blocks(net.minecraft.block.Blocks) List(java.util.List) AgriTabs(com.infinityraider.agricraft.content.AgriTabs) ItemGroup(net.minecraft.item.ItemGroup) Optional(java.util.Optional) TileEntity(net.minecraft.tileentity.TileEntity) ActionResultType(net.minecraft.util.ActionResultType) BlockState(net.minecraft.block.BlockState) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) Nonnull(javax.annotation.Nonnull)

Example 68 with BlockState

use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.

the class TileEntityCropBase method handlePlantUpdate.

protected void handlePlantUpdate() {
    if (this.getWorld() != null) {
        BlockState oldState = this.getBlockState();
        BlockState newState = oldState;
        // Update brightness
        int brightness = this.getPlant().getBrightness(this);
        if (BlockCropBase.LIGHT.fetch(newState) != brightness) {
            newState = BlockCropBase.LIGHT.apply(newState, brightness);
        }
        // Update plant state
        boolean plant = this.hasPlant() || this.hasWeeds();
        if (BlockCropBase.PLANT.fetch(newState) != plant) {
            newState = BlockCropBase.PLANT.apply(newState, plant);
        }
        // Set block state if necessary
        if (newState != oldState) {
            this.getWorld().setBlockState(this.getPos(), newState);
        }
        // Update growth requirement
        this.requirement.flush();
        this.requirement = RequirementCache.create(this);
        // Update model data
        this.getModelData().setData(PROPERTY_PLANT, this.getPlant());
        this.getModelData().setData(PROPERTY_PLANT_GROWTH, this.getGrowthStage());
        this.getModelData().setData(PROPERTY_WEED, this.getWeeds());
        this.getModelData().setData(PROPERTY_WEED_GROWTH, this.getWeedGrowthStage());
    }
}
Also used : BlockState(net.minecraft.block.BlockState)

Example 69 with BlockState

use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.

the class TileEntityCropBase method executePlantGrowthTick.

protected void executePlantGrowthTick() {
    if (!this.getGrowthStage().isFinal()) {
        BlockState state = this.getBlockState();
        if (this.calculateGrowthRate() > this.getRandom().nextDouble() && !MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Grow.Plant.Pre(this))) {
            // also do the MinecraftForge BlockEvent for crop growth
            BlockEvent.CropGrowEvent.Pre blockEvent = new BlockEvent.CropGrowEvent.Pre(this.getWorld(), this.getPos(), state);
            MinecraftForge.EVENT_BUS.post(blockEvent);
            if (blockEvent.getResult() == Event.Result.ALLOW || blockEvent.getResult() == Event.Result.DEFAULT) {
                this.setGrowthStage(this.getGrowthStage().getNextStage(this, this.getRandom()));
                this.getPlant().onGrowth(this);
                MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Grow.Plant.Post(this));
                MinecraftForge.EVENT_BUS.post(new BlockEvent.CropGrowEvent.Post(this.getWorld(), this.getPos(), state, state));
            }
        }
    }
}
Also used : NoPlant(com.infinityraider.agricraft.impl.v1.plant.NoPlant) IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) BlockState(net.minecraft.block.BlockState) AgriCropEvent(com.infinityraider.agricraft.api.v1.event.AgriCropEvent) BlockEvent(net.minecraftforge.event.world.BlockEvent)

Example 70 with BlockState

use of net.minecraft.block.BlockState in project AgriCraft by AgriCraft.

the class BlockGrate method getStateForPlacement.

@Override
@Nullable
public BlockState getStateForPlacement(BlockItemUseContext context) {
    BlockState state = this.getDefaultState();
    if (state.isValidPosition(context.getWorld(), context.getPos())) {
        BlockPos clicked = context.getPos().offset(context.getFace().getOpposite());
        BlockState target = context.getWorld().getBlockState(clicked);
        // If a grate is clicked on a grate, mimic its placement if a side face is clicked
        if (target.getBlock() instanceof BlockGrate) {
            if (context.getFace().getAxis() != AXIS.fetch(target)) {
                return AXIS.apply(OFFSET.apply(this.fluidlog(state, context.getWorld(), context.getPos()), OFFSET.fetch(target)), AXIS.fetch(target));
            }
        }
        // Determine the axis according to the face the player clicked and his look vector
        Vector3d hit = context.getHitVec();
        double offset;
        if (context.getFace().getAxis() == Direction.Axis.Y) {
            // The player clicked a horizontal face, determine the axis based on the player's orientation
            if (context.getPlacementHorizontalFacing().getAxis() == Direction.Axis.X) {
                // player is looking in the X direction
                state = AXIS.apply(state, Direction.Axis.X);
                offset = hit.getX() - ((int) hit.getX());
            } else {
                // player is looking in the Z direction
                state = AXIS.apply(state, Direction.Axis.Z);
                offset = hit.getZ() - ((int) hit.getZ());
            }
        } else {
            // The player clicked a vertical face, the axis will be Y
            state = AXIS.apply(state, Direction.Axis.Y);
            offset = hit.getY() - ((int) hit.getY());
        }
        // Finally, determine the offset by how far along the block the player clicked
        offset += offset < 0 ? 1 : 0;
        if (offset >= 11 * Constants.UNIT) {
            return OFFSET.apply(this.fluidlog(state, context.getWorld(), context.getPos()), Offset.FAR);
        } else if (offset <= 5 * Constants.UNIT) {
            return OFFSET.apply(this.fluidlog(state, context.getWorld(), context.getPos()), Offset.NEAR);
        } else {
            return OFFSET.apply(this.fluidlog(state, context.getWorld(), context.getPos()), Offset.MID);
        }
    }
    return null;
}
Also used : BlockState(net.minecraft.block.BlockState) Vector3d(net.minecraft.util.math.vector.Vector3d) BlockPos(net.minecraft.util.math.BlockPos) Nullable(javax.annotation.Nullable)

Aggregations

BlockState (net.minecraft.block.BlockState)79 BlockPos (net.minecraft.util.math.BlockPos)32 TileEntity (net.minecraft.tileentity.TileEntity)19 Direction (net.minecraft.util.Direction)16 Nonnull (javax.annotation.Nonnull)12 CompoundNBT (net.minecraft.nbt.CompoundNBT)12 World (net.minecraft.world.World)12 ChunkPos (net.minecraft.util.math.ChunkPos)11 Block (net.minecraft.block.Block)10 Nullable (javax.annotation.Nullable)9 PlayerEntity (net.minecraft.entity.player.PlayerEntity)9 ItemStack (net.minecraft.item.ItemStack)9 HashSet (java.util.HashSet)4 Blocks (net.minecraft.block.Blocks)4 ResourceLocation (net.minecraft.util.ResourceLocation)4 ServerWorld (net.minecraft.world.server.ServerWorld)4 IForgeBlockState (net.minecraftforge.common.extensions.IForgeBlockState)4 TileBPMultipart (com.bluepowermod.tile.TileBPMultipart)3 AgriCraft (com.infinityraider.agricraft.AgriCraft)3 BlockRayTraceResult (net.minecraft.util.math.BlockRayTraceResult)3