Search in sources :

Example 6 with BlockState

use of net.minecraft.block.BlockState in project Overloaded by CJ-MC-Mods.

the class ItemMultiTool method leftClickOnBlockServer.

public static void leftClickOnBlockServer(@Nonnull ServerPlayerEntity player, LeftClickBlockMessage message) {
    BlockPos pos = message.getPos();
    ServerWorld world = player.getLevel();
    ItemStack itemStack = player.getItemInHand(Hand.MAIN_HAND);
    if (itemStack.getItem() != ModItems.multiTool || world.isEmptyBlock(pos)) {
        return;
    }
    player.startUsingItem(Hand.MAIN_HAND);
    if (player.isShiftKeyDown()) {
        CompoundNBT tag = itemStack.getTag();
        if (tag == null) {
            tag = new CompoundNBT();
        }
        BlockState state = world.getBlockState(pos);
        Item item = Item.byBlock(state.getBlock());
        ItemStack stackToPlace = new ItemStack(() -> item, 1);
        CompoundNBT blockTag = new CompoundNBT();
        stackToPlace.save(blockTag);
        tag.put("Item", blockTag);
        itemStack.setTag(tag);
        ITextComponent component = stackToPlace.getDisplayName();
        player.displayClientMessage(new StringTextComponent("Bound tool to ").append(component), true);
    } else {
        LazyOptional<IEnergyStorage> opEnergy = itemStack.getCapability(ENERGY);
        if (!opEnergy.isPresent()) {
            Overloaded.logger.warn("MultiTool has no Energy Capability? NBT: " + itemStack.getTag());
            return;
        }
        // Used to catch item spawn to teleport
        RegistryKey<World> worldId = world.dimension();
        CommonSideEvents.enabled = true;
        CommonSideEvents.world = worldId;
        CommonSideEvents.pos = pos;
        CommonSideEvents.uuid = player.getUUID();
        IEnergyStorage energy = opEnergy.orElseThrow(() -> new RuntimeException("Impossible Error"));
        int efficiency = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.BLOCK_EFFICIENCY, itemStack);
        int unbreaking = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.UNBREAKING, itemStack);
        switch(breakAndUseEnergy(world, pos, energy, player, efficiency, unbreaking)) {
            case FAIL_REMOVE:
                player.displayClientMessage(new StringTextComponent("Unable to break block, reason unknown"), true);
                break;
            case FAIL_ENERGY:
                player.displayClientMessage(new StringTextComponent("Unable to break block, not enough energy"), true);
                break;
            case FAIL_UNBREAKABLE:
                player.displayClientMessage(new StringTextComponent("Block is unbreakable"), true);
                break;
            case FAIL_RANGE:
                player.displayClientMessage(new StringTextComponent("Block is out of range."), true);
                break;
            case SUCCESS:
                break;
        }
        CommonSideEvents.enabled = false;
    }
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) ITextComponent(net.minecraft.util.text.ITextComponent) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World) ServerWorld(net.minecraft.world.server.ServerWorld) Item(net.minecraft.item.Item) BlockItem(net.minecraft.item.BlockItem) BlockState(net.minecraft.block.BlockState) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) BlockPos(net.minecraft.util.math.BlockPos) StringTextComponent(net.minecraft.util.text.StringTextComponent) ItemStack(net.minecraft.item.ItemStack)

Example 7 with BlockState

use of net.minecraft.block.BlockState in project Overloaded by CJ-MC-Mods.

the class RenderMultiToolAssist method renderWorldLastEvent.

@SubscribeEvent
public static void renderWorldLastEvent(RenderWorldLastEvent event) {
    float partialTick = Minecraft.getInstance().getFrameTime();
    PlayerEntity player = Minecraft.getInstance().player;
    if (player.getMainHandItem().getItem() != ModItems.multiTool)
        return;
    RayTraceResult resultPick = player.pick(OverloadedConfig.INSTANCE.multiToolConfig.reach, partialTick, false);
    if (resultPick.getType() == RayTraceResult.Type.MISS || !(resultPick instanceof BlockRayTraceResult)) {
        return;
    }
    BlockRayTraceResult result = ((BlockRayTraceResult) resultPick);
    ItemStack stack = ModItems.multiTool.getSelectedBlockItemStack(player.getMainHandItem());
    BlockState state;
    if (stack.getItem() instanceof BlockItem) {
        state = ((BlockItem) stack.getItem()).getBlock().defaultBlockState();
        state = state.getStateAtViewpoint(player.getCommandSenderWorld(), result.getBlockPos(), player.getEyePosition(partialTick));
        state = state.getBlock().getStateForPlacement(new BlockItemUseContextPublic(player.getCommandSenderWorld(), player, Hand.MAIN_HAND, stack, result));
    } else {
        state = Blocks.COBBLESTONE.defaultBlockState();
    }
    switch(getAssistMode()) {
        case PLACE_PREVIEW:
            if (!stack.isEmpty() && state != null)
                renderBlockPreview(event, result, state);
            break;
        case REMOVE_PREVIEW:
            renderRemovePreview(event, result);
            break;
        case BOTH_PREVIEW:
            renderRemovePreview(event, result);
            if (!stack.isEmpty() && state != null)
                renderBlockPreview(event, result, state);
    }
}
Also used : BlockState(net.minecraft.block.BlockState) RayTraceResult(net.minecraft.util.math.RayTraceResult) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) ItemStack(net.minecraft.item.ItemStack) BlockItem(net.minecraft.item.BlockItem) BlockItemUseContextPublic(com.cjm721.overloaded.util.BlockItemUseContextPublic) ClientPlayerEntity(net.minecraft.client.entity.player.ClientPlayerEntity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 8 with BlockState

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

the class DebugModeIGrowable method debugActionBlockClicked.

@Override
public void debugActionBlockClicked(ItemStack stack, ItemUseContext context) {
    if (context.getWorld().isRemote) {
        return;
    }
    BlockPos pos = context.getPos();
    PlayerEntity player = context.getPlayer();
    World world = context.getWorld();
    // Start with the position of the block that was clicked on. '7' is gray. Btw, the chat font is not fixed width. :(
    StringBuilder outputRaw = new StringBuilder(String.format("`7%1$4d,%2$4d,%3$4d`r ", pos.getX(), pos.getY(), pos.getZ()));
    // Check if the clicked on block has the IGrowable interface.
    final IGrowable crop = WorldHelper.getBlock(world, pos, IGrowable.class).orElse(null);
    if (crop == null) {
        // If it does not, add a nicely formatted report, then skip onward.
        outputRaw.append(chatNotIG);
    } else if (world instanceof ServerWorld) {
        // Otherwise run the tests and record the results.
        BlockState state = world.getBlockState(pos);
        // canGrow
        outputRaw.append(crop.canGrow(world, pos, state, false) ? chatTrue : chatFalse);
        // canUseBonemeal
        outputRaw.append(crop.canUseBonemeal(world, world.rand, pos, state) ? chatTrue : chatFalse);
        // grow
        crop.grow((ServerWorld) world, world.rand, pos, state);
        // It's also helpful to also make clear what block was being tested.
        // '3' is dark aqua.
        outputRaw.append("`3");
        outputRaw.append(crop.toString().replaceFirst("Block", ""));
        outputRaw.append("`r");
    }
    // Ellipsis are added as a clue that there's more text.
    // '8' is dark gray.
    outputRaw.append(" `8[...]`r");
    // Create a hover box with explanatory information.
    StringTextComponent hoverComponent = new StringTextComponent(MessageUtil.colorize(chatInfo));
    HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverComponent);
    // Turn the output String into a chat message.
    StringTextComponent outputComponent = new StringTextComponent(MessageUtil.colorize(outputRaw.toString()));
    // Add the hover box to the chat message.
    outputComponent.getStyle().setHoverEvent(hoverEvent);
    // Now send the completed chat message.
    player.sendMessage(outputComponent, Util.DUMMY_UUID);
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) HoverEvent(net.minecraft.util.text.event.HoverEvent) BlockState(net.minecraft.block.BlockState) IGrowable(net.minecraft.block.IGrowable) BlockPos(net.minecraft.util.math.BlockPos) StringTextComponent(net.minecraft.util.text.StringTextComponent) ServerWorld(net.minecraft.world.server.ServerWorld) World(net.minecraft.world.World) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Example 9 with BlockState

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

the class CropHelper method checkGrowthSpace.

public static boolean checkGrowthSpace(World world, BlockPos pos, IAgriGrowable plant, IAgriGrowthStage stage) {
    if (world == null) {
        return false;
    }
    double height = plant.getPlantHeight(stage);
    while (height > 16) {
        int offset = ((int) height) / 16;
        BlockPos up = pos.up(offset);
        BlockState state = world.getBlockState(up);
        if (!state.getBlock().isAir(state, world, up)) {
            return false;
        }
        height -= 16;
    }
    return true;
}
Also used : BlockState(net.minecraft.block.BlockState) BlockPos(net.minecraft.util.math.BlockPos)

Example 10 with BlockState

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

the class ItemTrowel method tryNewPlant.

protected ActionResultType tryNewPlant(World world, BlockPos pos, ItemStack stack, @Nullable PlayerEntity player) {
    if (AgriCraft.instance.getConfig().allowPlantingOutsideCropSticks()) {
        BlockState newState = AgriCraft.instance.getModBlockRegistry().crop_plant.getStateForPlacement(world, pos);
        if (newState != null && world.setBlockState(pos, newState, 11)) {
            boolean success = AgriApi.getCrop(world, pos).map(crop -> {
                if (MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Trowel.Pre(crop, stack, player))) {
                    return false;
                }
                return this.getGenome(stack).map(genome -> this.getGrowthStage(stack).map(stage -> {
                    boolean result = crop.plantGenome(genome, player) && this.setGrowthStage(crop, stage);
                    if (result) {
                        this.removePlant(stack);
                        MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Trowel.Post(crop, stack, player));
                    }
                    return result;
                }).orElse(false)).orElse(false);
            }).orElse(false);
            if (success) {
                return ActionResultType.SUCCESS;
            } else {
                world.setBlockState(pos, Blocks.AIR.getDefaultState());
            }
        }
    }
    return ActionResultType.FAIL;
}
Also used : AgriNBT(com.infinityraider.agricraft.reference.AgriNBT) InfinityItemProperty(com.infinityraider.infinitylib.item.InfinityItemProperty) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) TileEntityCropSticks(com.infinityraider.agricraft.content.core.TileEntityCropSticks) NoPlant(com.infinityraider.agricraft.impl.v1.plant.NoPlant) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn) CompoundNBT(net.minecraft.nbt.CompoundNBT) IAgriGrowthStage(com.infinityraider.agricraft.api.v1.crop.IAgriGrowthStage) IAgriGenome(com.infinityraider.agricraft.api.v1.genetics.IAgriGenome) ItemUseContext(net.minecraft.item.ItemUseContext) Dist(net.minecraftforge.api.distmarker.Dist) ItemStack(net.minecraft.item.ItemStack) AgriToolTips(com.infinityraider.agricraft.reference.AgriToolTips) Names(com.infinityraider.agricraft.reference.Names) ItemBase(com.infinityraider.infinitylib.item.ItemBase) IAgriTrowelItem(com.infinityraider.agricraft.api.v1.content.items.IAgriTrowelItem) BlockState(net.minecraft.block.BlockState) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) AgriCraft(com.infinityraider.agricraft.AgriCraft) IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) ImmutableSet(com.google.common.collect.ImmutableSet) AgriApi(com.infinityraider.agricraft.api.v1.AgriApi) PlayerEntity(net.minecraft.entity.player.PlayerEntity) LivingEntity(net.minecraft.entity.LivingEntity) World(net.minecraft.world.World) Set(java.util.Set) BlockPos(net.minecraft.util.math.BlockPos) Blocks(net.minecraft.block.Blocks) MinecraftForge(net.minecraftforge.common.MinecraftForge) AgriTabs(com.infinityraider.agricraft.content.AgriTabs) ResourceLocation(net.minecraft.util.ResourceLocation) Optional(java.util.Optional) TileEntity(net.minecraft.tileentity.TileEntity) AgriCropEvent(com.infinityraider.agricraft.api.v1.event.AgriCropEvent) ActionResultType(net.minecraft.util.ActionResultType) ClientWorld(net.minecraft.client.world.ClientWorld) BlockState(net.minecraft.block.BlockState) AgriCropEvent(com.infinityraider.agricraft.api.v1.event.AgriCropEvent)

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