Search in sources :

Example 26 with BlockState

use of net.minecraft.block.BlockState 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 27 with BlockState

use of net.minecraft.block.BlockState in project BluePower by Qmunity.

the class ItemBPPart method place.

@Override
public ActionResultType place(BlockItemUseContext context) {
    BlockState state = context.getLevel().getBlockState(context.getClickedPos());
    BlockState thisState = getBlock().getStateForPlacement(context);
    if (state.getBlock() instanceof IBPPartBlock && thisState != null && !AABBUtils.testOcclusion(((IBPPartBlock) thisState.getBlock()).getOcclusionShape(thisState), state.getShape(context.getLevel(), context.getClickedPos()))) {
        // Save the Tile Entity Data
        CompoundNBT nbt = new CompoundNBT();
        TileEntity tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
        if (tileEntity != null) {
            nbt = tileEntity.save(nbt);
        }
        // Replace with Multipart
        context.getLevel().setBlockAndUpdate(context.getClickedPos(), BPBlocks.multipart.defaultBlockState());
        tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
        if (tileEntity instanceof TileBPMultipart) {
            // Add the original State to the Multipart
            ((TileBPMultipart) tileEntity).addState(state);
            // Restore the Tile Entity Data
            TileEntity tile = ((TileBPMultipart) tileEntity).getTileForState(state);
            if (tile != null)
                tile.load(state, nbt);
            // Add the new State
            ((TileBPMultipart) tileEntity).addState(thisState);
            thisState.getBlock().setPlacedBy(context.getLevel(), context.getClickedPos(), thisState, context.getPlayer(), context.getItemInHand());
        }
        // Update Self
        state.neighborChanged(context.getLevel(), context.getClickedPos(), state.getBlock(), context.getClickedPos(), false);
        context.getItemInHand().shrink(1);
        // Place Sound
        context.getLevel().playSound(null, context.getClickedPos(), SoundEvents.STONE_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
        return ActionResultType.SUCCESS;
    } else if (state.getBlock() instanceof BlockBPMultipart && thisState != null && !AABBUtils.testOcclusion(((IBPPartBlock) thisState.getBlock()).getOcclusionShape(thisState), state.getShape(context.getLevel(), context.getClickedPos()))) {
        // Add to the Existing Multipart
        TileEntity tileEntity = context.getLevel().getBlockEntity(context.getClickedPos());
        if (tileEntity instanceof TileBPMultipart) {
            ((TileBPMultipart) tileEntity).addState(thisState);
            thisState.getBlock().setPlacedBy(context.getLevel(), context.getClickedPos(), thisState, context.getPlayer(), context.getItemInHand());
            // Update Neighbors
            for (Direction dir : Direction.values()) {
                context.getLevel().getBlockState(context.getClickedPos().relative(dir)).neighborChanged(context.getLevel(), context.getClickedPos().relative(dir), state.getBlock(), context.getClickedPos(), false);
            }
            // Update Self
            state.neighborChanged(context.getLevel(), context.getClickedPos(), state.getBlock(), context.getClickedPos(), false);
            context.getItemInHand().shrink(1);
            // Place Sound
            context.getLevel().playSound(null, context.getClickedPos(), SoundEvents.STONE_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
            return ActionResultType.SUCCESS;
        }
    }
    return super.place(context);
}
Also used : IBPPartBlock(com.bluepowermod.api.multipart.IBPPartBlock) TileEntity(net.minecraft.tileentity.TileEntity) BlockState(net.minecraft.block.BlockState) CompoundNBT(net.minecraft.nbt.CompoundNBT) BlockBPMultipart(com.bluepowermod.block.BlockBPMultipart) TileBPMultipart(com.bluepowermod.tile.TileBPMultipart) Direction(net.minecraft.util.Direction)

Example 28 with BlockState

use of net.minecraft.block.BlockState in project BluePower by Qmunity.

the class BPMultipartModel method getQuads.

@Nonnull
@Override
public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @Nonnull Random rand, @Nonnull IModelData extraData) {
    BlockRendererDispatcher brd = Minecraft.getInstance().getBlockRenderer();
    Map<BlockState, IModelData> stateInfo = extraData.getData(TileBPMultipart.STATE_INFO);
    if (stateInfo != null) {
        return stateInfo.keySet().stream().flatMap(i -> brd.getBlockModel(i).getQuads(i, side, rand, stateInfo.get(i)).stream().map(q -> stateInfo.get(i).hasProperty(TileWire.COLOR_INFO) ? transform(q, stateInfo.get(i).getData(TileWire.COLOR_INFO), stateInfo.get(i).hasProperty(TileWire.LIGHT_INFO) ? stateInfo.get(i).getData(TileWire.LIGHT_INFO) : false) : q)).collect(Collectors.toList());
    } else {
        return Collections.emptyList();
    }
}
Also used : BakedQuadBuilder(net.minecraftforge.client.model.pipeline.BakedQuadBuilder) java.util(java.util) IBakedModel(net.minecraft.client.renderer.model.IBakedModel) ItemOverrideList(net.minecraft.client.renderer.model.ItemOverrideList) Direction(net.minecraft.util.Direction) RayTraceResult(net.minecraft.util.math.RayTraceResult) Minecraft(net.minecraft.client.Minecraft) BlockState(net.minecraft.block.BlockState) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) TextureAtlasSprite(net.minecraft.client.renderer.texture.TextureAtlasSprite) MultipartUtils(com.bluepowermod.util.MultipartUtils) TileWire(com.bluepowermod.tile.tier1.TileWire) VertexTransformer(net.minecraftforge.client.model.pipeline.VertexTransformer) BlockRendererDispatcher(net.minecraft.client.renderer.BlockRendererDispatcher) IModelData(net.minecraftforge.client.model.data.IModelData) IVertexConsumer(net.minecraftforge.client.model.pipeline.IVertexConsumer) Collectors(java.util.stream.Collectors) TileBPMultipart(com.bluepowermod.tile.TileBPMultipart) Pair(com.mojang.datafixers.util.Pair) BakedQuad(net.minecraft.client.renderer.model.BakedQuad) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) VertexFormatElement(net.minecraft.client.renderer.vertex.VertexFormatElement) LightUtil(net.minecraftforge.client.model.pipeline.LightUtil) ModelResourceLocation(net.minecraft.client.renderer.model.ModelResourceLocation) BlockState(net.minecraft.block.BlockState) IModelData(net.minecraftforge.client.model.data.IModelData) BlockRendererDispatcher(net.minecraft.client.renderer.BlockRendererDispatcher) Nonnull(javax.annotation.Nonnull)

Example 29 with BlockState

use of net.minecraft.block.BlockState in project BluePower by Qmunity.

the class RenderLamp method render.

@Override
public void render(TileLamp te, float v, MatrixStack matrixStack, IRenderTypeBuffer iRenderTypeBuffer, int partialTicks, int destroyStage) {
    if (// || Loader.isModLoaded("albedo"))
    !(te.getBlockState().getBlock() instanceof BlockLamp))
        return;
    BlockState stateLamp = te.getBlockState();
    BlockLamp bLamp = (BlockLamp) stateLamp.getBlock();
    if (te.getLevel() == null) {
        return;
    }
    int power = stateLamp.getValue(BlockLamp.POWER);
    int color = bLamp.getColor(stateLamp, te.getLevel(), te.getBlockPos(), 0);
    int redMask = 0xFF0000, greenMask = 0xFF00, blueMask = 0xFF;
    int r = (color & redMask) >> 16;
    int g = (color & greenMask) >> 8;
    int b = (color & blueMask);
    AxisAlignedBB box = stateLamp.getShape(te.getLevel(), te.getBlockPos()).bounds();
    // Define our base Glow
    box = box.equals(new AxisAlignedBB(0, 0, 0, 1, 1, 1)) ? box.inflate(0.05) : box.inflate(0.03125);
    boolean[] renderFaces = new boolean[] { true, true, true, true, true, true };
    // Remove overlapping Glow
    if (stateLamp.getShape(te.getLevel(), te.getBlockPos()).bounds().equals(new AxisAlignedBB(0, 0, 0, 1, 1, 1))) {
        for (Direction face : Direction.values()) {
            BlockState state = te.getLevel().getBlockState(te.getBlockPos().relative(face.getOpposite()));
            if (state.getBlock() instanceof BlockLamp && state.getShape(te.getLevel(), te.getBlockPos()).bounds().equals(new AxisAlignedBB(0, 0, 0, 1, 1, 1))) {
                if (state.getValue(BlockLamp.POWER) > 0) {
                    renderFaces[face.get3DDataValue()] = false;
                    double offsetx = (face.getStepX() * 0.05) > 0 ? (face.getStepX() * 0.05) : 0;
                    double offsety = (face.getStepY() * 0.05) > 0 ? (face.getStepY() * 0.05) : 0;
                    double offsetz = (face.getStepZ() * 0.05) > 0 ? (face.getStepZ() * 0.05) : 0;
                    double toffsetx = (face.getStepX() * 0.05) < 0 ? (face.getStepX() * 0.05) : 0;
                    double toffsety = (face.getStepY() * 0.05) < 0 ? (face.getStepY() * 0.05) : 0;
                    double toffsetz = (face.getStepZ() * 0.05) < 0 ? (face.getStepZ() * 0.05) : 0;
                    box = new AxisAlignedBB(box.minX + offsetx, box.minY + offsety, box.minZ + offsetz, box.maxX + toffsetx, box.maxY + toffsety, box.maxZ + toffsetz);
                }
            }
        }
    }
    matrixStack.pushPose();
    double powerDivision = power / 18D;
    com.bluepowermod.client.render.RenderHelper.drawColoredCube(box, iRenderTypeBuffer.getBuffer(BPRenderTypes.LAMP_GLOW), matrixStack, r, g, b, (int) (powerDivision * 200), 200, renderFaces);
    matrixStack.popPose();
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) BlockState(net.minecraft.block.BlockState) BlockLamp(com.bluepowermod.block.lighting.BlockLamp) Direction(net.minecraft.util.Direction)

Example 30 with BlockState

use of net.minecraft.block.BlockState in project BluePower by Qmunity.

the class TileWire method getCapability.

@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
    List<Direction> directions = new ArrayList<>(BlockBPCableBase.FACING.getPossibleValues());
    if (level != null) {
        BlockState state = getBlockState();
        if (state.getBlock() instanceof BlockAlloyWire) {
            // Remove upward connections
            directions.remove(state.getValue(BlockAlloyWire.FACING));
            // Make sure the cable is on the same side of the block
            directions.removeIf(d -> level.getBlockState(worldPosition.relative(d)).getBlock() instanceof BlockAlloyWire && level.getBlockState(worldPosition.relative(d)).getValue(BlockAlloyWire.FACING) != state.getValue(BlockAlloyWire.FACING));
        // Make sure the cable is the same color or none
        // if(device.getInsulationColor(null) != MinecraftColor.NONE)
        // directions.removeIf(d -> {
        // TileEntity tile = world.getBlockEntity(worldPosition.relative(d));
        // return tile instanceof TileWire
        // && !(((TileWire) tile).device.getInsulationColor(d) == device.getInsulationColor(d.getOpposite())
        // || ((TileWire) tile).device.getInsulationColor(d) == MinecraftColor.NONE);
        // });
        }
    }
    if (cap == CapabilityRedstoneDevice.UNINSULATED_CAPABILITY && (side == null || directions.contains(side))) {
        if (redstoneCap == null)
            redstoneCap = LazyOptional.of(() -> device);
        return redstoneCap.cast();
    }
    return LazyOptional.empty();
}
Also used : BlockState(net.minecraft.block.BlockState) ArrayList(java.util.ArrayList) BlockAlloyWire(com.bluepowermod.block.machine.BlockAlloyWire) Direction(net.minecraft.util.Direction) Nonnull(javax.annotation.Nonnull)

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