Search in sources :

Example 1 with ConfiguratorMode

use of mekanism.common.item.ItemConfigurator.ConfiguratorMode in project Mekanism by mekanism.

the class RenderTickHandler method onBlockHover.

@SubscribeEvent
public void onBlockHover(DrawHighlightEvent.HighlightBlock event) {
    PlayerEntity player = minecraft.player;
    if (player == null) {
        return;
    }
    BlockRayTraceResult rayTraceResult = event.getTarget();
    if (!rayTraceResult.getType().equals(Type.MISS)) {
        World world = player.getCommandSenderWorld();
        BlockPos pos = rayTraceResult.getBlockPos();
        IRenderTypeBuffer renderer = event.getBuffers();
        ActiveRenderInfo info = event.getInfo();
        MatrixStack matrix = event.getMatrix();
        IProfiler profiler = world.getProfiler();
        BlockState blockState = world.getBlockState(pos);
        boolean shouldCancel = false;
        profiler.push(ProfilerConstants.MEKANISM_OUTLINE);
        if (!blockState.isAir(world, pos) && world.getWorldBorder().isWithinBounds(pos)) {
            BlockPos actualPos = pos;
            BlockState actualState = blockState;
            if (blockState.getBlock() instanceof BlockBounding) {
                TileEntityBoundingBlock tile = WorldUtils.getTileEntity(TileEntityBoundingBlock.class, world, pos);
                if (tile != null) {
                    actualPos = tile.getMainPos();
                    actualState = world.getBlockState(actualPos);
                }
            }
            AttributeCustomSelectionBox customSelectionBox = Attribute.get(actualState, AttributeCustomSelectionBox.class);
            if (customSelectionBox != null) {
                WireFrameRenderer renderWireFrame = null;
                if (customSelectionBox.isJavaModel()) {
                    // If we use a TER to render the wire frame, grab the tile
                    TileEntity tile = WorldUtils.getTileEntity(world, actualPos);
                    if (tile != null) {
                        TileEntityRenderer<TileEntity> tileRenderer = TileEntityRendererDispatcher.instance.getRenderer(tile);
                        if (tileRenderer instanceof IWireFrameRenderer) {
                            IWireFrameRenderer wireFrameRenderer = (IWireFrameRenderer) tileRenderer;
                            if (wireFrameRenderer.hasSelectionBox(actualState)) {
                                renderWireFrame = (buffer, matrixStack, state, red, green, blue, alpha) -> {
                                    if (wireFrameRenderer.isCombined()) {
                                        renderQuadsWireFrame(state, buffer, matrixStack.last().pose(), world.random, red, green, blue, alpha);
                                    }
                                    wireFrameRenderer.renderWireFrame(tile, event.getPartialTicks(), matrixStack, buffer, red, green, blue, alpha);
                                };
                            }
                        }
                    }
                } else {
                    // Otherwise, skip getting the tile and just grab the model
                    renderWireFrame = (buffer, matrixStack, state, red, green, blue, alpha) -> renderQuadsWireFrame(state, buffer, matrixStack.last().pose(), world.random, red, green, blue, alpha);
                }
                if (renderWireFrame != null) {
                    matrix.pushPose();
                    Vector3d viewPosition = info.getPosition();
                    matrix.translate(actualPos.getX() - viewPosition.x, actualPos.getY() - viewPosition.y, actualPos.getZ() - viewPosition.z);
                    renderWireFrame.render(renderer.getBuffer(RenderType.lines()), matrix, actualState, 0, 0, 0, 0.4F);
                    matrix.popPose();
                    shouldCancel = true;
                }
            }
        }
        profiler.pop();
        ItemStack stack = player.getMainHandItem();
        if (stack.isEmpty() || !(stack.getItem() instanceof ItemConfigurator)) {
            // If we are not holding a configurator, look if we are in the offhand
            stack = player.getOffhandItem();
            if (stack.isEmpty() || !(stack.getItem() instanceof ItemConfigurator)) {
                if (shouldCancel) {
                    event.setCanceled(true);
                }
                return;
            }
        }
        profiler.push(ProfilerConstants.CONFIGURABLE_MACHINE);
        ConfiguratorMode state = ((ItemConfigurator) stack.getItem()).getMode(stack);
        if (state.isConfigurating()) {
            TransmissionType type = Objects.requireNonNull(state.getTransmission(), "Configurating state requires transmission type");
            TileEntity tile = WorldUtils.getTileEntity(world, pos);
            if (tile instanceof ISideConfiguration) {
                ISideConfiguration configurable = (ISideConfiguration) tile;
                TileComponentConfig config = configurable.getConfig();
                if (config.supports(type)) {
                    Direction face = rayTraceResult.getDirection();
                    DataType dataType = config.getDataType(type, RelativeSide.fromDirections(configurable.getDirection(), face));
                    if (dataType != null) {
                        Vector3d viewPosition = info.getPosition();
                        matrix.pushPose();
                        matrix.translate(pos.getX() - viewPosition.x, pos.getY() - viewPosition.y, pos.getZ() - viewPosition.z);
                        MekanismRenderer.renderObject(getOverlayModel(face, type), matrix, renderer.getBuffer(Atlases.translucentCullBlockSheet()), MekanismRenderer.getColorARGB(dataType.getColor(), 0.6F), MekanismRenderer.FULL_LIGHT, OverlayTexture.NO_OVERLAY, FaceDisplay.FRONT);
                        matrix.popPose();
                    }
                }
            }
        }
        profiler.pop();
        if (shouldCancel) {
            event.setCanceled(true);
        }
    }
}
Also used : AttributeCustomSelectionBox(mekanism.common.block.attribute.AttributeCustomSelectionBox) MatrixStack(com.mojang.blaze3d.matrix.MatrixStack) World(net.minecraft.world.World) TileComponentConfig(mekanism.common.tile.component.TileComponentConfig) Direction(net.minecraft.util.Direction) AbstractClientPlayerEntity(net.minecraft.client.entity.player.AbstractClientPlayerEntity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) TileEntityBoundingBlock(mekanism.common.tile.TileEntityBoundingBlock) TileEntity(net.minecraft.tileentity.TileEntity) IRenderTypeBuffer(net.minecraft.client.renderer.IRenderTypeBuffer) IWireFrameRenderer(mekanism.client.render.tileentity.IWireFrameRenderer) IWireFrameRenderer(mekanism.client.render.tileentity.IWireFrameRenderer) BlockBounding(mekanism.common.block.BlockBounding) DataType(mekanism.common.tile.component.config.DataType) BlockPos(net.minecraft.util.math.BlockPos) ActiveRenderInfo(net.minecraft.client.renderer.ActiveRenderInfo) ISideConfiguration(mekanism.common.tile.interfaces.ISideConfiguration) ItemConfigurator(mekanism.common.item.ItemConfigurator) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) ConfiguratorMode(mekanism.common.item.ItemConfigurator.ConfiguratorMode) IProfiler(net.minecraft.profiler.IProfiler) BlockState(net.minecraft.block.BlockState) Vector3d(net.minecraft.util.math.vector.Vector3d) TransmissionType(mekanism.common.lib.transmitter.TransmissionType) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 2 with ConfiguratorMode

use of mekanism.common.item.ItemConfigurator.ConfiguratorMode in project Mekanism by mekanism.

the class ItemConfigurator method useOn.

@Nonnull
@Override
public ActionResultType useOn(ItemUseContext context) {
    PlayerEntity player = context.getPlayer();
    World world = context.getLevel();
    if (!world.isClientSide && player != null) {
        BlockPos pos = context.getClickedPos();
        Direction side = context.getClickedFace();
        ItemStack stack = context.getItemInHand();
        TileEntity tile = WorldUtils.getTileEntity(world, pos);
        ConfiguratorMode mode = getMode(stack);
        if (mode.isConfigurating()) {
            // Configurate
            TransmissionType transmissionType = Objects.requireNonNull(mode.getTransmission(), "Configurating state requires transmission type");
            if (tile instanceof ISideConfiguration && ((ISideConfiguration) tile).getConfig().supports(transmissionType)) {
                ISideConfiguration config = (ISideConfiguration) tile;
                ConfigInfo info = config.getConfig().getConfig(transmissionType);
                if (info != null) {
                    RelativeSide relativeSide = RelativeSide.fromDirections(config.getDirection(), side);
                    DataType dataType = info.getDataType(relativeSide);
                    if (!player.isShiftKeyDown()) {
                        player.sendMessage(MekanismUtils.logFormat(MekanismLang.CONFIGURATOR_VIEW_MODE.translate(transmissionType, dataType.getColor(), dataType, dataType.getColor().getColoredName())), Util.NIL_UUID);
                    } else if (SecurityUtils.canAccess(player, tile)) {
                        if (!player.isCreative()) {
                            IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
                            FloatingLong energyPerConfigure = MekanismConfig.gear.configuratorEnergyPerConfigure.get();
                            if (energyContainer == null || energyContainer.extract(energyPerConfigure, Action.SIMULATE, AutomationType.MANUAL).smallerThan(energyPerConfigure)) {
                                return ActionResultType.FAIL;
                            }
                            energyContainer.extract(energyPerConfigure, Action.EXECUTE, AutomationType.MANUAL);
                        }
                        DataType old = dataType;
                        dataType = info.incrementDataType(relativeSide);
                        if (dataType != old) {
                            player.sendMessage(MekanismUtils.logFormat(MekanismLang.CONFIGURATOR_TOGGLE_MODE.translate(transmissionType, dataType.getColor(), dataType, dataType.getColor().getColoredName())), Util.NIL_UUID);
                            config.getConfig().sideChanged(transmissionType, relativeSide);
                        }
                    } else {
                        SecurityUtils.displayNoAccess(player);
                    }
                }
                return ActionResultType.SUCCESS;
            }
            if (SecurityUtils.canAccess(player, tile)) {
                Optional<IConfigurable> capability = CapabilityUtils.getCapability(tile, Capabilities.CONFIGURABLE_CAPABILITY, side).resolve();
                if (capability.isPresent()) {
                    IConfigurable config = capability.get();
                    if (player.isShiftKeyDown()) {
                        return config.onSneakRightClick(player, side);
                    }
                    return config.onRightClick(player, side);
                }
            } else {
                SecurityUtils.displayNoAccess(player);
                return ActionResultType.SUCCESS;
            }
        } else if (mode == ConfiguratorMode.EMPTY) {
            // Empty
            if (tile instanceof IMekanismInventory) {
                IMekanismInventory inv = (IMekanismInventory) tile;
                if (inv.hasInventory()) {
                    if (SecurityUtils.canAccess(player, tile)) {
                        boolean creative = player.isCreative();
                        IEnergyContainer energyContainer = creative ? null : StorageUtils.getEnergyContainer(stack, 0);
                        if (!creative && energyContainer == null) {
                            return ActionResultType.FAIL;
                        }
                        // TODO: Switch this to items being handled by TileEntityMekanism, energy handled here (via lambdas?)
                        FloatingLong energyPerItemDump = MekanismConfig.gear.configuratorEnergyPerItem.get();
                        for (IInventorySlot inventorySlot : inv.getInventorySlots(null)) {
                            if (!inventorySlot.isEmpty()) {
                                if (!creative) {
                                    if (energyContainer.extract(energyPerItemDump, Action.SIMULATE, AutomationType.MANUAL).smallerThan(energyPerItemDump)) {
                                        break;
                                    }
                                    energyContainer.extract(energyPerItemDump, Action.EXECUTE, AutomationType.MANUAL);
                                }
                                Block.popResource(world, pos, inventorySlot.getStack().copy());
                                inventorySlot.setEmpty();
                            }
                        }
                        return ActionResultType.SUCCESS;
                    } else {
                        SecurityUtils.displayNoAccess(player);
                        return ActionResultType.FAIL;
                    }
                }
            }
        } else if (mode == ConfiguratorMode.ROTATE) {
            // Rotate
            if (tile instanceof TileEntityMekanism) {
                if (SecurityUtils.canAccess(player, tile)) {
                    TileEntityMekanism tileMekanism = (TileEntityMekanism) tile;
                    if (Attribute.get(tileMekanism.getBlockType(), AttributeStateFacing.class).canRotate()) {
                        if (!player.isShiftKeyDown()) {
                            tileMekanism.setFacing(side);
                        } else if (player.isShiftKeyDown()) {
                            tileMekanism.setFacing(side.getOpposite());
                        }
                    }
                } else {
                    SecurityUtils.displayNoAccess(player);
                }
            }
            return ActionResultType.SUCCESS;
        } else if (mode == ConfiguratorMode.WRENCH) {
            // Wrench
            return ActionResultType.PASS;
        }
    }
    return ActionResultType.PASS;
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) World(net.minecraft.world.World) ConfigInfo(mekanism.common.tile.component.config.ConfigInfo) Direction(net.minecraft.util.Direction) IConfigurable(mekanism.api.IConfigurable) ConfiguratorMode(mekanism.common.item.ItemConfigurator.ConfiguratorMode) AttributeStateFacing(mekanism.common.block.attribute.AttributeStateFacing) PlayerEntity(net.minecraft.entity.player.PlayerEntity) TileEntity(net.minecraft.tileentity.TileEntity) FloatingLong(mekanism.api.math.FloatingLong) IEnergyContainer(mekanism.api.energy.IEnergyContainer) TransmissionType(mekanism.common.lib.transmitter.TransmissionType) RelativeSide(mekanism.api.RelativeSide) DataType(mekanism.common.tile.component.config.DataType) IMekanismInventory(mekanism.api.inventory.IMekanismInventory) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) ISideConfiguration(mekanism.common.tile.interfaces.ISideConfiguration) Nonnull(javax.annotation.Nonnull)

Example 3 with ConfiguratorMode

use of mekanism.common.item.ItemConfigurator.ConfiguratorMode in project Mekanism by mekanism.

the class ItemConfigurator method changeMode.

@Override
public void changeMode(@Nonnull PlayerEntity player, @Nonnull ItemStack stack, int shift, boolean displayChangeMessage) {
    ConfiguratorMode mode = getMode(stack);
    ConfiguratorMode newMode = mode.adjust(shift);
    if (mode != newMode) {
        setMode(stack, player, newMode);
        if (displayChangeMessage) {
            player.sendMessage(MekanismUtils.logFormat(MekanismLang.CONFIGURE_STATE.translate(newMode)), Util.NIL_UUID);
        }
    }
}
Also used : ConfiguratorMode(mekanism.common.item.ItemConfigurator.ConfiguratorMode)

Aggregations

ConfiguratorMode (mekanism.common.item.ItemConfigurator.ConfiguratorMode)3 TransmissionType (mekanism.common.lib.transmitter.TransmissionType)2 DataType (mekanism.common.tile.component.config.DataType)2 ISideConfiguration (mekanism.common.tile.interfaces.ISideConfiguration)2 PlayerEntity (net.minecraft.entity.player.PlayerEntity)2 ItemStack (net.minecraft.item.ItemStack)2 TileEntity (net.minecraft.tileentity.TileEntity)2 Direction (net.minecraft.util.Direction)2 BlockPos (net.minecraft.util.math.BlockPos)2 World (net.minecraft.world.World)2 MatrixStack (com.mojang.blaze3d.matrix.MatrixStack)1 Nonnull (javax.annotation.Nonnull)1 IConfigurable (mekanism.api.IConfigurable)1 RelativeSide (mekanism.api.RelativeSide)1 IEnergyContainer (mekanism.api.energy.IEnergyContainer)1 IInventorySlot (mekanism.api.inventory.IInventorySlot)1 IMekanismInventory (mekanism.api.inventory.IMekanismInventory)1 FloatingLong (mekanism.api.math.FloatingLong)1 IWireFrameRenderer (mekanism.client.render.tileentity.IWireFrameRenderer)1 BlockBounding (mekanism.common.block.BlockBounding)1