Search in sources :

Example 11 with BlockEntity

use of net.minecraft.block.entity.BlockEntity in project wildmod by Osmiooo.

the class FrogEntitySpawnEgg method useOnBlock.

@Override
public ActionResult useOnBlock(ItemUsageContext context) {
    World world = context.getWorld();
    ItemStack itemStack = context.getStack();
    BlockPos blockPos = context.getBlockPos();
    Direction direction = context.getSide();
    BlockState blockState = world.getBlockState(blockPos);
    if (!(world instanceof ServerWorld)) {
        return ActionResult.SUCCESS;
    } else {
        if (blockState.isOf(Blocks.SPAWNER)) {
            BlockEntity blockEntity = world.getBlockEntity(blockPos);
            if (blockEntity instanceof MobSpawnerBlockEntity) {
                MobSpawnerLogic mobSpawnerLogic = ((MobSpawnerBlockEntity) blockEntity).getLogic();
                EntityType<?> entityType = this.getEntityType(itemStack.getNbt());
                mobSpawnerLogic.setEntityId(entityType);
                blockEntity.markDirty();
                world.updateListeners(blockPos, blockState, blockState, Block.NOTIFY_ALL);
                itemStack.decrement(1);
                return ActionResult.CONSUME;
            }
        }
        FrogEntity frogEntity = new FrogEntity(RegisterEntities.FROG, world);
        BlockPos blockPos3;
        if (blockState.getCollisionShape(world, blockPos).isEmpty()) {
            blockPos3 = blockPos;
        } else {
            blockPos3 = blockPos.offset(direction);
        }
        if (FrogEntity.canColdSpawn(world, blockPos3)) {
            frogEntity.setVariant(FrogEntity.Variant.COLD);
        } else if (FrogEntity.canTemperateSpawn(world, blockPos3)) {
            frogEntity.setVariant(FrogEntity.Variant.WARM);
        }
        frogEntity.setPos(blockPos3.getX() + 0.5, blockPos3.getY() + 0.1, blockPos3.getZ() + 0.5);
        frogEntity.setYaw((float) Math.random() * 360 * (float) Math.PI / 180);
        world.spawnEntity(frogEntity);
        itemStack.decrement(1);
        world.emitGameEvent(context.getPlayer(), GameEvent.ENTITY_PLACE, blockPos);
        return ActionResult.CONSUME;
    }
}
Also used : ServerWorld(net.minecraft.server.world.ServerWorld) FrogEntity(frozenblock.wild.mod.entity.FrogEntity) BlockState(net.minecraft.block.BlockState) MobSpawnerLogic(net.minecraft.world.MobSpawnerLogic) MobSpawnerBlockEntity(net.minecraft.block.entity.MobSpawnerBlockEntity) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ServerWorld(net.minecraft.server.world.ServerWorld) ItemStack(net.minecraft.item.ItemStack) Direction(net.minecraft.util.math.Direction) BlockEntity(net.minecraft.block.entity.BlockEntity) MobSpawnerBlockEntity(net.minecraft.block.entity.MobSpawnerBlockEntity)

Example 12 with BlockEntity

use of net.minecraft.block.entity.BlockEntity in project canvas by vram-guild.

the class MixinChunkRenderer method onRebuildChunk.

@Inject(method = "rebuildChunk", at = @At("HEAD"), cancellable = true, require = 1)
private void onRebuildChunk(final float x, final float y, final float z, final ChunkRenderTask chunkRenderTask, final CallbackInfo ci) {
    final TerrainRenderContext renderContext = TerrainRenderContext.POOL.get();
    final ChunkRebuildHelper help = renderContext.chunkRebuildHelper;
    help.clear();
    final ChunkRenderData chunkRenderData = ChunkRenderDataStore.claim();
    final ChunkRenderDataExt chunkDataExt = (ChunkRenderDataExt) chunkRenderData;
    final BlockPos.Mutable origin = this.origin;
    final World world = this.world;
    if (world != null) {
        chunkRenderTask.getLock().lock();
        try {
            if (chunkRenderTask.getStage() != ChunkRenderTask.Stage.COMPILING) {
                return;
            }
            chunkRenderTask.setRenderData(chunkRenderData);
        } finally {
            chunkRenderTask.getLock().unlock();
        }
        // PERF: avoid allocation
        final ChunkOcclusionGraphBuilder visibilityData = new ChunkOcclusionGraphBuilder();
        // PERF: avoid allocation
        final HashSet<BlockEntity> blockEntities = Sets.newHashSet();
        final ChunkRendererRegion vanillaRegion = chunkRenderTask.takeRegion();
        if (vanillaRegion != null) {
            final FastRenderRegion renderRegion = ((ChunkRendererRegionExt) vanillaRegion).canvas_fastRegion();
            ++chunkUpdateCount;
            help.prepareCollectors(origin.getX(), origin.getY(), origin.getZ());
            renderContext.setChunkTask(chunkRenderTask);
            /**
             * Capture the block layer result flags so our renderer can update them when more
             * than one layer is rendered for a single model. This is also where we signal the
             * renderer to prepare for a new chunk using the data we've accumulated up to this point.
             */
            renderContext.prepare((ChunkRenderer) (Object) this, origin);
            // NB: We don't use this and it probably isn't needed but leaving just in case - cost is low
            BlockModelRenderer.enableBrightnessCache();
            final BlockRenderManager blockRenderManager = MinecraftClient.getInstance().getBlockRenderManager();
            final BlockPos.Mutable searchPos = help.searchPos;
            final int xMin = origin.getX();
            final int yMin = origin.getY();
            final int zMin = origin.getZ();
            final int xMax = xMin + 16;
            final int yMax = yMin + 16;
            final int zMax = zMin + 16;
            for (int xPos = xMin; xPos < xMax; xPos++) {
                for (int yPos = yMin; yPos < yMax; yPos++) {
                    for (int zPos = zMin; zPos < zMax; zPos++) {
                        final BlockState blockState = renderRegion.getBlockState(xPos, yPos, zPos);
                        searchPos.set(xPos, yPos, zPos);
                        if (blockState.isFullOpaque(renderRegion, searchPos)) {
                            visibilityData.markClosed(searchPos);
                        }
                        if (blockState.getBlock().hasBlockEntity()) {
                            final BlockEntity blockEntity = renderRegion.getBlockEntity(searchPos, WorldChunk.CreationType.CHECK);
                            if (blockEntity != null) {
                                final BlockEntityRenderer<BlockEntity> blockEntityRenderer = BlockEntityRenderDispatcher.INSTANCE.get(blockEntity);
                                if (blockEntityRenderer != null) {
                                    // Fixes MC-112730 - no reason to render both globally and in chunk
                                    if (blockEntityRenderer.method_3563(blockEntity)) {
                                        // global renderer - like beacons
                                        blockEntities.add(blockEntity);
                                    } else {
                                        // chunk-local renderer
                                        chunkRenderData.addBlockEntity(blockEntity);
                                    }
                                }
                            }
                        }
                        BlockRenderLayer renderLayer;
                        // UGLY: we are relying on knowledge that fluid state is directly derived from block state, which
                        // may not be true in future versions and may break.  However, is significantly faster to re-use block
                        // state here vs. retrieving it again.
                        final FluidState fluidState = blockState.getFluidState();
                        if (!fluidState.isEmpty()) {
                            renderLayer = fluidState.getRenderLayer();
                            // TODO: apply appropriate shader props for fluids
                            final FluidBufferBuilder fluidBuilder = help.fluidBuilder.prepare(help.getCollector(renderLayer).get(Canvas.MATERIAL_STANDARD, ShaderProps.waterProps()), searchPos, renderLayer);
                            blockRenderManager.tesselateFluid(searchPos, renderRegion, fluidBuilder, fluidState);
                        }
                        if (blockState.getRenderType() == BlockRenderType.MODEL) {
                            renderContext.tesselateBlock(blockState, searchPos);
                        }
                    }
                }
            }
            if (!help.solidCollector.isEmpty()) {
                chunkRenderData.markBufferInitialized(BlockRenderLayer.SOLID);
                chunkDataExt.canvas_setNonEmpty(BlockRenderLayer.SOLID);
                final UploadableChunk.Solid abandoned = uploadSolid.getAndSet(help.solidCollector.packUploadSolid());
                if (abandoned != null) {
                    abandoned.cancel();
                }
            }
            if (!help.translucentCollector.isEmpty()) {
                final VertexCollectorList vcl = help.translucentCollector;
                chunkRenderData.markBufferInitialized(BlockRenderLayer.TRANSLUCENT);
                chunkDataExt.canvas_setNonEmpty(BlockRenderLayer.TRANSLUCENT);
                vcl.setViewCoordinates(x, y, z);
                chunkDataExt.canvas_collectorState(vcl.getCollectorState(null));
                final UploadableChunk.Translucent abandoned = uploadTranslucent.getAndSet(vcl.packUploadTranslucent());
                if (abandoned != null) {
                    abandoned.cancel();
                }
            }
            /**
             * Release all references. Probably not necessary but would be $#%! to debug if it is.
             */
            renderContext.release();
            BlockModelRenderer.disableBrightnessCache();
        }
        chunkRenderData.setOcclusionGraph(visibilityData.build());
        lock.lock();
        try {
            help.tileEntitiesToAdd.addAll(blockEntities);
            help.tileEntitiesToRemove.addAll(this.blockEntities);
            help.tileEntitiesToAdd.removeAll(this.blockEntities);
            help.tileEntitiesToRemove.removeAll(blockEntities);
            this.blockEntities.clear();
            this.blockEntities.addAll(blockEntities);
            renderer.updateBlockEntities(help.tileEntitiesToRemove, help.tileEntitiesToAdd);
        } finally {
            lock.unlock();
        }
    }
    ci.cancel();
}
Also used : ChunkRendererRegion(net.minecraft.client.render.chunk.ChunkRendererRegion) ChunkRebuildHelper(grondag.canvas.chunk.ChunkRebuildHelper) ChunkRenderData(net.minecraft.client.render.chunk.ChunkRenderData) FluidBufferBuilder(grondag.canvas.buffer.packing.FluidBufferBuilder) ChunkRenderDataExt(grondag.canvas.chunk.ChunkRenderDataExt) World(net.minecraft.world.World) TerrainRenderContext(grondag.canvas.apiimpl.rendercontext.TerrainRenderContext) ChunkOcclusionGraphBuilder(net.minecraft.client.render.chunk.ChunkOcclusionGraphBuilder) BlockRenderManager(net.minecraft.client.render.block.BlockRenderManager) BlockPos(net.minecraft.util.math.BlockPos) BlockEntity(net.minecraft.block.entity.BlockEntity) BlockState(net.minecraft.block.BlockState) VertexCollectorList(grondag.canvas.buffer.packing.VertexCollectorList) UploadableChunk(grondag.canvas.chunk.UploadableChunk) FastRenderRegion(grondag.canvas.chunk.FastRenderRegion) BlockRenderLayer(net.minecraft.block.BlockRenderLayer) ChunkRendererRegionExt(grondag.canvas.chunk.ChunkRendererRegionExt) FluidState(net.minecraft.fluid.FluidState) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 13 with BlockEntity

use of net.minecraft.block.entity.BlockEntity in project meteor-client by MeteorDevelopment.

the class GhostHand method onTick.

@EventHandler
private void onTick(TickEvent.Pre event) {
    if (!mc.options.useKey.isPressed() || mc.player.isSneaking())
        return;
    for (BlockEntity blockEntity : Utils.blockEntities()) {
        if (new BlockPos(mc.player.raycast(mc.interactionManager.getReachDistance(), mc.getTickDelta(), false).getPos()).equals(blockEntity.getPos()))
            return;
    }
    Vec3d nextPos = new Vec3d(0, 0, 0.1).rotateX(-(float) Math.toRadians(mc.player.getPitch())).rotateY(-(float) Math.toRadians(mc.player.getYaw()));
    for (int i = 1; i < mc.interactionManager.getReachDistance() * 10; i++) {
        BlockPos curPos = new BlockPos(mc.player.getCameraPosVec(mc.getTickDelta()).add(nextPos.multiply(i)));
        if (posList.contains(curPos))
            continue;
        posList.add(curPos);
        for (BlockEntity blockEntity : Utils.blockEntities()) {
            if (blockEntity.getPos().equals(curPos)) {
                mc.interactionManager.interactBlock(mc.player, mc.world, Hand.MAIN_HAND, new BlockHitResult(mc.player.getPos(), Direction.UP, curPos, true));
                return;
            }
        }
    }
    posList.clear();
}
Also used : BlockPos(net.minecraft.util.math.BlockPos) BlockHitResult(net.minecraft.util.hit.BlockHitResult) Vec3d(net.minecraft.util.math.Vec3d) BlockEntity(net.minecraft.block.entity.BlockEntity) EventHandler(meteordevelopment.orbit.EventHandler)

Example 14 with BlockEntity

use of net.minecraft.block.entity.BlockEntity in project tweakermore by Fallen-Breath.

the class SignTextCopier method copySignText.

public static boolean copySignText(KeyAction action, IKeybind key) {
    MinecraftClient mc = MinecraftClient.getInstance();
    if (mc.world != null && mc.crosshairTarget != null && mc.crosshairTarget.getType() == HitResult.Type.BLOCK) {
        BlockPos blockPos = ((BlockHitResult) mc.crosshairTarget).getBlockPos();
        BlockState blockState = mc.world.getBlockState(blockPos);
        if (blockState.getBlock() instanceof AbstractSignBlock) {
            BlockEntity blockEntity = mc.world.getBlockEntity(blockPos);
            if (blockEntity instanceof SignBlockEntity) {
                String text = Joiner.on("\n").join(Arrays.stream(((SignBlockEntity) blockEntity).text).map(Text::getString).collect(Collectors.toList()));
                text = StringUtils.strip(text);
                if (!text.isEmpty()) {
                    mc.keyboard.setClipboard(text);
                    InfoUtils.printActionbarMessage("tweakermore.config.copySignTextToClipBoard.sign_copied", blockState.getBlock().getName());
                } else {
                    InfoUtils.printActionbarMessage("tweakermore.config.copySignTextToClipBoard.empty_sign", blockState.getBlock().getName());
                }
                return true;
            }
        }
    }
    InfoUtils.printActionbarMessage("tweakermore.config.copySignTextToClipBoard.no_sign");
    return false;
}
Also used : BlockState(net.minecraft.block.BlockState) SignBlockEntity(net.minecraft.block.entity.SignBlockEntity) MinecraftClient(net.minecraft.client.MinecraftClient) BlockPos(net.minecraft.util.math.BlockPos) Text(net.minecraft.text.Text) BlockHitResult(net.minecraft.util.hit.BlockHitResult) AbstractSignBlock(net.minecraft.block.AbstractSignBlock) BlockEntity(net.minecraft.block.entity.BlockEntity) SignBlockEntity(net.minecraft.block.entity.SignBlockEntity)

Example 15 with BlockEntity

use of net.minecraft.block.entity.BlockEntity in project FastAsyncWorldEdit by IntellectualSites.

the class FabricWorld method clearContainerBlockContents.

@Override
public boolean clearContainerBlockContents(BlockVector3 position) {
    checkNotNull(position);
    BlockEntity tile = getWorld().getBlockEntity(FabricAdapter.toBlockPos(position));
    if ((tile instanceof Clearable)) {
        ((Clearable) tile).clear();
        return true;
    }
    return false;
}
Also used : Clearable(net.minecraft.util.Clearable) BlockEntity(net.minecraft.block.entity.BlockEntity)

Aggregations

BlockEntity (net.minecraft.block.entity.BlockEntity)61 BlockPos (net.minecraft.util.math.BlockPos)26 BlockState (net.minecraft.block.BlockState)19 BlockHitResult (net.minecraft.util.hit.BlockHitResult)13 ItemStack (net.minecraft.item.ItemStack)10 ChestBlockEntity (net.minecraft.block.entity.ChestBlockEntity)8 Vec3d (net.minecraft.util.math.Vec3d)8 World (net.minecraft.world.World)7 Block (net.minecraft.block.Block)6 Inject (org.spongepowered.asm.mixin.injection.Inject)6 MatrixStack (net.minecraft.client.util.math.MatrixStack)5 FluidState (net.minecraft.fluid.FluidState)5 Random (java.util.Random)4 AbstractFurnaceBlockEntity (net.minecraft.block.entity.AbstractFurnaceBlockEntity)4 CommandBlockBlockEntity (net.minecraft.block.entity.CommandBlockBlockEntity)4 BlockRenderManager (net.minecraft.client.render.block.BlockRenderManager)4 ChunkRendererRegion (net.minecraft.client.render.chunk.ChunkRendererRegion)4 Inventory (net.minecraft.inventory.Inventory)4 NbtCompound (net.minecraft.nbt.NbtCompound)4 ServerWorld (net.minecraft.server.world.ServerWorld)4