Search in sources :

Example 6 with BlockItem

use of net.minecraft.item.BlockItem in project Overloaded by CJ-MC-Mods.

the class PlayerInteractionUtil method placeBlock.

@Nonnull
public static BlockPlaceResult placeBlock(@Nonnull ItemStack searchStack, @Nonnull ServerPlayerEntity player, @Nonnull World worldIn, @Nonnull BlockPos newPosition, @Nonnull Direction facing, @Nonnull IEnergyStorage energy, float hitX, float hitY, float hitZ) {
    // Can we place a block at this Pos
    BlockItem itemBlock = ((BlockItem) searchStack.getItem());
    // if (worldIn.loadedAndEntityCanStandOn(newPosition, player)) {
    // return BlockPlaceResult.FAIL_DENY;
    // }
    BlockSnapshot blockSnapshot = BlockSnapshot.create(worldIn.dimension(), worldIn, newPosition);
    BlockState placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().relative(facing.getOpposite()));
    BlockEvent.EntityPlaceEvent event = new BlockEvent.EntityPlaceEvent(blockSnapshot, placedAgainst, player);
    MinecraftForge.EVENT_BUS.post(event);
    if (event.isCanceled()) {
        return BlockPlaceResult.FAIL_DENY;
    }
    long distance = Math.round(Math.sqrt(player.blockPosition().distSqr(newPosition)));
    long cost = OverloadedConfig.INSTANCE.multiToolConfig.placeBaseCost + OverloadedConfig.INSTANCE.multiToolConfig.costPerMeterAway * distance;
    if (!player.abilities.instabuild && (cost > Integer.MAX_VALUE || cost < 0 || energy.getEnergyStored() < cost))
        return BlockPlaceResult.FAIL_ENERGY;
    LazyOptional<IItemHandler> opInventory = player.getCapability(ITEM_HANDLER_CAPABILITY, Direction.UP);
    if (!opInventory.isPresent()) {
        Overloaded.logger.warn("Player has no ItemHandler Capability? NBT: " + player.serializeNBT());
        return BlockPlaceResult.FAIL_PREREQUISITE;
    }
    IItemHandler inventory = opInventory.orElseThrow(() -> new RuntimeException("Impossible Condition"));
    int foundStackSlot = findItemStackSlot(searchStack, inventory);
    if (foundStackSlot == -1) {
        return BlockPlaceResult.FAIL_PREREQUISITE;
    }
    ItemStack foundStack = inventory.extractItem(foundStackSlot, 1, player.abilities.instabuild);
    BlockItemUseContext context = new BlockItemUseContextPublic(worldIn, player, Hand.MAIN_HAND, foundStack, new BlockRayTraceResult(new Vector3d(hitX + newPosition.getX(), hitY + newPosition.getY(), hitZ + newPosition.getZ()), facing, newPosition, false));
    ActionResultType result = ForgeHooks.onPlaceItemIntoWorld(context);
    switch(result) {
        case CONSUME:
        case SUCCESS:
            SoundType soundtype = worldIn.getBlockState(newPosition).getBlock().getSoundType(worldIn.getBlockState(newPosition), worldIn, newPosition, player);
            worldIn.playSound(null, newPosition, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
            if (!player.abilities.instabuild) {
                energy.extractEnergy((int) cost, false);
            }
            return BlockPlaceResult.SUCCESS;
        case PASS:
        case FAIL:
        default:
            inventory.insertItem(foundStackSlot, foundStack, player.abilities.instabuild);
            return BlockPlaceResult.FAIL_DENY;
    }
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) ActionResultType(net.minecraft.util.ActionResultType) BlockSnapshot(net.minecraftforge.common.util.BlockSnapshot) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) BlockItem(net.minecraft.item.BlockItem) BlockItemUseContext(net.minecraft.item.BlockItemUseContext) Vector3d(net.minecraft.util.math.vector.Vector3d) ItemStack(net.minecraft.item.ItemStack) BlockEvent(net.minecraftforge.event.world.BlockEvent) Nonnull(javax.annotation.Nonnull)

Example 7 with BlockItem

use of net.minecraft.item.BlockItem in project Overloaded by CJ-MC-Mods.

the class ItemMultiTool method rightClickWithItem.

public void rightClickWithItem(@Nonnull ServerPlayerEntity player, RightClickBlockMessage message) {
    BlockPos pos = message.getPos();
    Direction sideHit = message.getHitSide();
    float hitX = message.getHitX();
    float hitY = message.getHitY();
    float hitZ = message.getHitZ();
    ServerWorld worldIn = player.getLevel();
    ItemStack multiTool = player.getMainHandItem();
    if (multiTool.getItem() != this) {
        return;
    }
    ItemStack blockStack = getSelectedBlockItemStack(multiTool);
    if (blockStack.isEmpty()) {
        player.displayClientMessage(new StringTextComponent("No block type selected to place."), true);
        return;
    }
    if (!(blockStack.getItem() instanceof BlockItem)) {
        player.displayClientMessage(new StringTextComponent("No valid block type selected to place."), true);
        return;
    }
    LazyOptional<IEnergyStorage> opEnergy = multiTool.getCapability(ENERGY);
    if (!opEnergy.isPresent()) {
        Overloaded.logger.warn("MultiTool has no Energy Capability? NBT: " + multiTool.getTag());
        return;
    }
    IEnergyStorage energy = opEnergy.orElseThrow(() -> new RuntimeException("Impossible Condition"));
    Vector3i sideVector = sideHit.getNormal();
    BlockPos.Mutable newPosition = pos.offset(sideVector).mutable();
    switch(placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ)) {
        case FAIL_PREREQUISITE:
            player.displayClientMessage(new StringTextComponent("Do not have the required items"), true);
            return;
        case FAIL_DENY:
            player.displayClientMessage(new StringTextComponent("Unable to place blocks"), true);
            return;
        case FAIL_RANGE:
            player.displayClientMessage(new StringTextComponent("To far away"), true);
            return;
        case FAIL_ENERGY:
            player.displayClientMessage(new StringTextComponent("Not enough energy"), true);
            return;
        case SUCCESS:
    }
    if (player.isShiftKeyDown()) {
        BlockPos playerPos = player.blockPosition();
        switch(sideHit) {
            case UP:
                while (newPosition.getY() < playerPos.getY()) {
                    newPosition.move(sideHit);
                    if (placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ) != BlockPlaceResult.SUCCESS)
                        break;
                }
                break;
            case DOWN:
                while (newPosition.getY() > playerPos.getY()) {
                    newPosition.move(sideHit);
                    if (placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ) != BlockPlaceResult.SUCCESS)
                        break;
                }
                break;
            case NORTH:
                while (newPosition.getZ() > playerPos.getZ()) {
                    newPosition.move(sideHit);
                    if (placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ) != BlockPlaceResult.SUCCESS)
                        break;
                }
                break;
            case SOUTH:
                while (newPosition.getZ() < playerPos.getZ()) {
                    newPosition.move(sideHit);
                    if (placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ) != BlockPlaceResult.SUCCESS)
                        break;
                }
                break;
            case EAST:
                while (newPosition.getX() < playerPos.getX()) {
                    newPosition.move(sideHit);
                    if (placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ) != BlockPlaceResult.SUCCESS)
                        break;
                }
                break;
            case WEST:
                while (newPosition.getX() > playerPos.getX()) {
                    newPosition.move(sideHit);
                    if (placeBlock(blockStack, player, worldIn, newPosition, sideHit, energy, hitX, hitY, hitZ) != BlockPlaceResult.SUCCESS)
                        break;
                }
                break;
        }
    }
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) Vector3i(net.minecraft.util.math.vector.Vector3i) BlockPos(net.minecraft.util.math.BlockPos) StringTextComponent(net.minecraft.util.text.StringTextComponent) ItemStack(net.minecraft.item.ItemStack) BlockItem(net.minecraft.item.BlockItem)

Example 8 with BlockItem

use of net.minecraft.item.BlockItem in project NetherEx by LogicTechCorp.

the class NetherExBlocks method registerBlockWithBasicItem.

private static RegistryObject<Block> registerBlockWithBasicItem(String name, Block.Properties properties, ItemGroup group) {
    RegistryObject<Block> registryObject = registerBlock(name, () -> new Block(properties));
    ITEM_BLOCKS.register(name, () -> new BlockItem(registryObject.get(), new Item.Properties().group(group)));
    return registryObject;
}
Also used : Item(net.minecraft.item.Item) BlockItem(net.minecraft.item.BlockItem) ExperienceDroppingBlock(logictechcorp.libraryex.block.ExperienceDroppingBlock) PathBlock(logictechcorp.libraryex.block.PathBlock) FireSustainingBlock(logictechcorp.libraryex.block.FireSustainingBlock) ModBreakableBlock(logictechcorp.libraryex.block.ModBreakableBlock) BlockItem(net.minecraft.item.BlockItem)

Example 9 with BlockItem

use of net.minecraft.item.BlockItem in project NetherEx by LogicTechCorp.

the class NetherExBlocks method registerBlockWithBasicItem.

private static <B extends Block> RegistryObject<B> registerBlockWithBasicItem(String name, Supplier<B> supplier, ItemGroup group) {
    RegistryObject<B> registryObject = registerBlock(name, supplier);
    ITEM_BLOCKS.register(name, () -> new BlockItem(registryObject.get(), new Item.Properties().group(group)));
    return registryObject;
}
Also used : Item(net.minecraft.item.Item) BlockItem(net.minecraft.item.BlockItem) BlockItem(net.minecraft.item.BlockItem)

Example 10 with BlockItem

use of net.minecraft.item.BlockItem in project Geolosys by oitsjustjose.

the class BlockColorInit method registerItemColors.

@SubscribeEvent
public static void registerItemColors(ColorHandlerEvent.Item evt) {
    final BlockColors blockColors = evt.getBlockColors();
    final ItemColors itemColors = evt.getItemColors();
    // Use the Block's colour handler for an ItemBlock
    IItemColor itemBlockColourHandler = (stack, tintIndex) -> {
        BlockState state = ((BlockItem) stack.getItem()).getBlock().getDefaultState();
        return blockColors.getColor(state, null, null, tintIndex);
    };
    if (itemBlockColourHandler != null) {
        itemColors.register(itemBlockColourHandler, ModBlocks.getInstance().peat);
        itemColors.register(itemBlockColourHandler, ModBlocks.getInstance().rhododendron);
    }
}
Also used : IItemColor(net.minecraft.client.renderer.color.IItemColor) BlockColors(net.minecraft.client.renderer.color.BlockColors) IItemColor(net.minecraft.client.renderer.color.IItemColor) BiomeColors(net.minecraft.world.biome.BiomeColors) ItemColors(net.minecraft.client.renderer.color.ItemColors) BlockItem(net.minecraft.item.BlockItem) GrassColors(net.minecraft.world.GrassColors) Mod(net.minecraftforge.fml.common.Mod) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) BlockState(net.minecraft.block.BlockState) ColorHandlerEvent(net.minecraftforge.client.event.ColorHandlerEvent) ModBlocks(com.oitsjustjose.geolosys.common.blocks.ModBlocks) Dist(net.minecraftforge.api.distmarker.Dist) BlockState(net.minecraft.block.BlockState) ItemColors(net.minecraft.client.renderer.color.ItemColors) BlockColors(net.minecraft.client.renderer.color.BlockColors) BlockItem(net.minecraft.item.BlockItem) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

BlockItem (net.minecraft.item.BlockItem)11 ItemStack (net.minecraft.item.ItemStack)5 Item (net.minecraft.item.Item)4 Block (net.minecraft.block.Block)2 BlockState (net.minecraft.block.BlockState)2 ResourceLocation (net.minecraft.util.ResourceLocation)2 BlockRayTraceResult (net.minecraft.util.math.BlockRayTraceResult)2 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)2 BlockBPMicroblock (com.bluepowermod.block.BlockBPMicroblock)1 ItemSaw (com.bluepowermod.item.ItemSaw)1 BlockItemUseContextPublic (com.cjm721.overloaded.util.BlockItemUseContextPublic)1 ModBlocks (com.oitsjustjose.geolosys.common.blocks.ModBlocks)1 Nonnull (javax.annotation.Nonnull)1 ExperienceDroppingBlock (logictechcorp.libraryex.block.ExperienceDroppingBlock)1 FireSustainingBlock (logictechcorp.libraryex.block.FireSustainingBlock)1 ModBreakableBlock (logictechcorp.libraryex.block.ModBreakableBlock)1 PathBlock (logictechcorp.libraryex.block.PathBlock)1 IBookshelfBlock (net.darkhax.bookshelf.block.IBookshelfBlock)1 ClientPlayerEntity (net.minecraft.client.entity.player.ClientPlayerEntity)1 BufferBuilder (net.minecraft.client.renderer.BufferBuilder)1