Search in sources :

Example 1 with BlockDoublePlant

use of net.minecraft.block.BlockDoublePlant in project Traverse by ProfessorProspector.

the class TraverseClient method tooltip.

@SubscribeEvent
public void tooltip(RenderTooltipEvent.Pre event) {
    boolean render = false;
    ItemStack stack = event.getStack();
    if (stack.getItem() instanceof ItemBlock) {
        if (((ItemBlock) stack.getItem()).getBlock() instanceof BlockSapling)
            render = true;
        if (((ItemBlock) stack.getItem()).getBlock() instanceof BlockLeaves)
            render = true;
        if (((ItemBlock) stack.getItem()).getBlock() instanceof BlockFlower)
            render = true;
        if (((ItemBlock) stack.getItem()).getBlock() instanceof BlockDoublePlant)
            render = true;
        if (((ItemBlock) stack.getItem()).getBlock() == Blocks.WATERLILY)
            render = true;
        if (((ItemBlock) stack.getItem()).getBlock() == Blocks.VINE)
            render = true;
        if (((ItemBlock) stack.getItem()).getBlock() == Blocks.TALLGRASS)
            render = true;
        if (((ItemBlock) stack.getItem()).getBlock() == Blocks.DEADBUSH)
            render = true;
    } else {
    }
    if (!render)
        return;
    ;
    event.setCanceled(true);
    List<String> textLines = new ArrayList<>();
    textLines.addAll(event.getLines());
    int mouseX = event.getX();
    int mouseY = event.getY();
    int screenWidth = event.getScreenWidth();
    int screenHeight = event.getScreenHeight();
    int maxTextWidth = event.getMaxWidth();
    FontRenderer font = event.getFontRenderer();
    GlStateManager.disableRescaleNormal();
    RenderHelper.disableStandardItemLighting();
    GlStateManager.disableLighting();
    GlStateManager.disableDepth();
    int tooltipTextWidth = 0;
    for (String textLine : textLines) {
        int textLineWidth = font.getStringWidth(textLine);
        if (textLineWidth > tooltipTextWidth) {
            tooltipTextWidth = textLineWidth;
        }
    }
    boolean needsWrap = false;
    int titleLinesCount = 1;
    int tooltipX = mouseX + 12;
    if (tooltipX + tooltipTextWidth + 4 > screenWidth) {
        tooltipX = mouseX - 16 - tooltipTextWidth;
        if (// if the tooltip doesn't fit on the screen
        tooltipX < 4) {
            if (mouseX > screenWidth / 2) {
                tooltipTextWidth = mouseX - 12 - 8;
            } else {
                tooltipTextWidth = screenWidth - 16 - mouseX;
            }
            needsWrap = true;
        }
    }
    if (maxTextWidth > 0 && tooltipTextWidth > maxTextWidth) {
        tooltipTextWidth = maxTextWidth;
        needsWrap = true;
    }
    if (needsWrap) {
        int wrappedTooltipWidth = 0;
        List<String> wrappedTextLines = new ArrayList<>();
        for (int i = 0; i < textLines.size(); i++) {
            String textLine = textLines.get(i);
            List<String> wrappedLine = font.listFormattedStringToWidth(textLine, tooltipTextWidth);
            if (i == 0) {
                titleLinesCount = wrappedLine.size();
            }
            for (String line : wrappedLine) {
                int lineWidth = font.getStringWidth(line);
                if (lineWidth > wrappedTooltipWidth) {
                    wrappedTooltipWidth = lineWidth;
                }
                wrappedTextLines.add(line);
            }
        }
        tooltipTextWidth = wrappedTooltipWidth;
        textLines = wrappedTextLines;
        if (mouseX > screenWidth / 2) {
            tooltipX = mouseX - 16 - tooltipTextWidth;
        } else {
            tooltipX = mouseX + 12;
        }
    }
    int tooltipY = mouseY - 12;
    int tooltipHeight = 8;
    if (textLines.size() > 1) {
        tooltipHeight += (textLines.size() - 1) * 10;
        if (textLines.size() > titleLinesCount) {
            // gap between title lines and next lines
            tooltipHeight += 2;
        }
    }
    if (tooltipY + tooltipHeight + 6 > screenHeight) {
        tooltipY = screenHeight - tooltipHeight - 6;
    }
    final int zLevel = 300;
    int backgroundX = tooltipX - 3;
    int backgroundY = tooltipY - 3;
    int backgroundWidth = tooltipTextWidth + 5;
    int backgroundHeight = tooltipHeight + 6;
    final int backgroundColor = 0xF0100010;
    GuiUtils.drawGradientRect(zLevel, tooltipX - 3, tooltipY - 4, tooltipX + tooltipTextWidth + 3, tooltipY - 3, backgroundColor, backgroundColor);
    GuiUtils.drawGradientRect(zLevel, tooltipX - 3, tooltipY + tooltipHeight + 3, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 4, backgroundColor, backgroundColor);
    GuiUtils.drawGradientRect(zLevel, tooltipX - 3, tooltipY - 3, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3, backgroundColor, backgroundColor);
    GuiUtils.drawGradientRect(zLevel, tooltipX - 4, tooltipY - 3, tooltipX - 3, tooltipY + tooltipHeight + 3, backgroundColor, backgroundColor);
    GuiUtils.drawGradientRect(zLevel, tooltipX + tooltipTextWidth + 3, tooltipY - 3, tooltipX + tooltipTextWidth + 4, tooltipY + tooltipHeight + 3, backgroundColor, backgroundColor);
    final int borderColorStart = new Color(0x24B528).getRGB();
    final int borderColorEnd = (borderColorStart & 0xFEFEFE) >> 1 | borderColorStart & 0xFF000000;
    GuiUtils.drawGradientRect(zLevel, tooltipX - 3, tooltipY - 3 + 1, tooltipX - 3 + 1, tooltipY + tooltipHeight + 3 - 1, borderColorStart, borderColorEnd);
    GuiUtils.drawGradientRect(zLevel, tooltipX + tooltipTextWidth + 2, tooltipY - 3 + 1, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3 - 1, borderColorStart, borderColorEnd);
    GuiUtils.drawGradientRect(zLevel, tooltipX - 3, tooltipY - 3, tooltipX + tooltipTextWidth + 3, tooltipY - 3 + 1, borderColorStart, borderColorStart);
    GuiUtils.drawGradientRect(zLevel, tooltipX - 3, tooltipY + tooltipHeight + 2, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3, borderColorEnd, borderColorEnd);
    int tooltipTop = tooltipY;
    for (int lineNumber = 0; lineNumber < textLines.size(); ++lineNumber) {
        String line = textLines.get(lineNumber);
        font.drawStringWithShadow(line, tooltipX, tooltipY, -1);
        if (lineNumber + 1 == titleLinesCount) {
            tooltipY += 2;
        }
        tooltipY += 10;
    }
    MinecraftForge.EVENT_BUS.post(new RenderTooltipEvent.PostText(event.getStack(), textLines, tooltipX, tooltipTop, font, tooltipTextWidth, tooltipHeight));
    GlStateManager.enableLighting();
    GlStateManager.enableDepth();
    RenderHelper.enableStandardItemLighting();
    GlStateManager.enableRescaleNormal();
}
Also used : ArrayList(java.util.ArrayList) BlockDoublePlant(net.minecraft.block.BlockDoublePlant) ItemBlock(net.minecraft.item.ItemBlock) BlockFlower(net.minecraft.block.BlockFlower) BlockLeaves(net.minecraft.block.BlockLeaves) RenderTooltipEvent(net.minecraftforge.client.event.RenderTooltipEvent) BlockSapling(net.minecraft.block.BlockSapling) FontRenderer(net.minecraft.client.gui.FontRenderer) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 2 with BlockDoublePlant

use of net.minecraft.block.BlockDoublePlant in project TUMAT by canitzp.

the class Vanilla method renderBlock.

@Override
public TooltipComponent renderBlock(WorldClient world, EntityPlayerSP player, BlockPos pos, EnumFacing side, TooltipComponent component, boolean shouldCalculate) {
    IBlockState state = world.getBlockState(pos);
    // Plants:
    if (state.getBlock() instanceof IPlantable) {
        IBlockState plant = ((IPlantable) state.getBlock()).getPlant(world, pos);
        if (plant != null) {
            if (ConfigBoolean.SHOW_PLANT_GROWTH_STATUS.value && plant.getBlock() instanceof BlockCrops) {
                try {
                    PropertyInteger prop = (PropertyInteger) getAgeProperty.invoke(plant.getBlock());
                    int plantStatus = state.getValue(prop);
                    float growStatus = Math.round((plantStatus / (prop.getAllowedValues().size() - 1 * 1.0F) * 100F) * 100.00F) / 100.00F;
                    ColoredText.createOneLine(component, L10n.getVanillaGrowRate(String.valueOf(growStatus)), ColoredText.Colors.BROWN_PLANT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (plant.getBlock() instanceof BlockDoublePlant && InfoUtil.hasProperty(plant, BlockDoublePlant.HALF)) {
                BlockDoublePlant.EnumBlockHalf half = plant.getValue(BlockDoublePlant.HALF);
                if (half == BlockDoublePlant.EnumBlockHalf.UPPER) {
                    IBlockState down = world.getBlockState(pos.down());
                    component.setName(new TextComponent(InfoUtil.getBlockName(down)));
                }
            }
        }
    }
    if (ConfigBoolean.SHOW_REDSTONE_STRENGTH.value) {
        int power = state.getWeakPower(world, pos, side);
        if (power > 0 || state.getBlock() instanceof BlockRedstoneWire) {
            ColoredText.createOneLine(component, L10n.getVanillaRedstoneStrength(power), ColoredText.Colors.RED_REDSTONE);
        }
        if (state.getBlock() instanceof BlockRedstoneRepeater) {
            ColoredText.createOneLine(component, L10n.getVanillaRedstoneDelay(state.getValue(BlockRedstoneRepeater.DELAY)), ColoredText.Colors.RED_REDSTONE);
            if (state.getValue(BlockRedstoneRepeater.LOCKED)) {
                TextComponent.createOneLine(component, L10n.REDSTONE_LOCKED, TextFormatting.GRAY);
            }
        }
    }
    if (ConfigBoolean.SHOW_LIGHT_LEVEL.value) {
        boolean isBlockLightSource = state.getLightValue(world, pos) != 0;
        if (!isBlockLightSource && !world.getBlockState(pos.up()).isFullCube() && !world.getBlockState(pos.up()).isFullBlock()) {
            int lightLevel = world.getLightFor(EnumSkyBlock.BLOCK, pos.up());
            TextFormatting canMobsSpawn = world.getWorldTime() % 24000 >= 13000 && lightLevel <= 7 && state.getBlock().canCreatureSpawn(state, world, pos, EntityLiving.SpawnPlacementType.ON_GROUND) ? TextFormatting.RED : TextFormatting.YELLOW;
            TextComponent.createOneLine(component, L10n.getVanillaLight(lightLevel), canMobsSpawn);
        }
        if (isBlockLightSource) {
            int lightValue = state.getLightValue(world, pos);
            TextComponent.createOneLine(component, L10n.getVanillaLightSource(lightValue), TextFormatting.YELLOW);
        }
    }
    return component;
}
Also used : TextComponent(de.canitzp.tumat.api.components.TextComponent) IBlockState(net.minecraft.block.state.IBlockState) BlockCrops(net.minecraft.block.BlockCrops) PropertyInteger(net.minecraft.block.properties.PropertyInteger) IPlantable(net.minecraftforge.common.IPlantable) BlockDoublePlant(net.minecraft.block.BlockDoublePlant) BlockRedstoneRepeater(net.minecraft.block.BlockRedstoneRepeater) TextFormatting(net.minecraft.util.text.TextFormatting) BlockRedstoneWire(net.minecraft.block.BlockRedstoneWire)

Example 3 with BlockDoublePlant

use of net.minecraft.block.BlockDoublePlant in project Almura by AlmuraDev.

the class ItemReturnHelper method onHarvestBlock.

// Sunflower Destroy Action Modifier & charging the player food/drink for harvesting custom crops.
@SubscribeEvent
public void onHarvestBlock(BlockEvent.HarvestDropsEvent event) {
    Block block = event.getState().getBlock();
    if (block instanceof CropBlock) {
        EntityPlayer player = event.getHarvester();
        if (player != null)
            // This is how we make players use more food/drink...
            player.addExhaustion(0.01F);
    }
    if (block instanceof BlockDoublePlant) {
        if (block.getMetaFromState(event.getState()) == 0) {
            // 0 = Sunflower Type.
            final ItemStack drop = GameRegistry.makeItemStack("almura:food/food/sunflowerseed", 0, 16, null);
            if (drop != ItemStack.EMPTY) {
                // Destroy the sunflower return.
                event.getDrops().clear();
                // Return Sunflower seeds, yeh!
                event.getDrops().add(drop);
            }
        }
    }
}
Also used : CropBlock(com.almuradev.content.type.block.type.crop.CropBlock) Block(net.minecraft.block.Block) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockDoublePlant(net.minecraft.block.BlockDoublePlant) CropBlock(com.almuradev.content.type.block.type.crop.CropBlock) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Aggregations

BlockDoublePlant (net.minecraft.block.BlockDoublePlant)3 ItemStack (net.minecraft.item.ItemStack)2 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)2 CropBlock (com.almuradev.content.type.block.type.crop.CropBlock)1 TextComponent (de.canitzp.tumat.api.components.TextComponent)1 ArrayList (java.util.ArrayList)1 Block (net.minecraft.block.Block)1 BlockCrops (net.minecraft.block.BlockCrops)1 BlockFlower (net.minecraft.block.BlockFlower)1 BlockLeaves (net.minecraft.block.BlockLeaves)1 BlockRedstoneRepeater (net.minecraft.block.BlockRedstoneRepeater)1 BlockRedstoneWire (net.minecraft.block.BlockRedstoneWire)1 BlockSapling (net.minecraft.block.BlockSapling)1 PropertyInteger (net.minecraft.block.properties.PropertyInteger)1 IBlockState (net.minecraft.block.state.IBlockState)1 FontRenderer (net.minecraft.client.gui.FontRenderer)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 ItemBlock (net.minecraft.item.ItemBlock)1 TextFormatting (net.minecraft.util.text.TextFormatting)1 RenderTooltipEvent (net.minecraftforge.client.event.RenderTooltipEvent)1