Search in sources :

Example 6 with TextComponent

use of net.minecraft.network.chat.TextComponent in project MinecraftForge by MinecraftForge.

the class TextComponentMessageFormatHandler method handle.

public static int handle(final TranslatableComponent parent, final Consumer<FormattedText> addChild, final Object[] formatArgs, final String format) {
    try {
        final String formattedString = ForgeI18n.parseFormat(format, formatArgs);
        // See MinecraftForge/MinecraftForge#7396
        if (format.indexOf('\'') != -1) {
            final boolean onlyMissingQuotes = format.chars().filter(ch -> formattedString.indexOf((char) ch) == -1).allMatch(ch -> ch == '\'');
            if (onlyMissingQuotes) {
                return 0;
            }
        }
        TextComponent component = new TextComponent(formattedString);
        component.getStyle().applyTo(parent.getStyle());
        addChild.accept(component);
        return format.length();
    } catch (IllegalArgumentException ex) {
        return 0;
    }
}
Also used : Consumer(java.util.function.Consumer) TextComponent(net.minecraft.network.chat.TextComponent) FormattedText(net.minecraft.network.chat.FormattedText) ForgeI18n(net.minecraftforge.common.ForgeI18n) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) TextComponent(net.minecraft.network.chat.TextComponent)

Example 7 with TextComponent

use of net.minecraft.network.chat.TextComponent in project MinecraftForge by MinecraftForge.

the class PistonEventTest method pistonPre.

@SubscribeEvent
public static void pistonPre(PistonEvent.Pre event) {
    if (event.getPistonMoveType() == PistonMoveType.EXTEND) {
        Level world = (Level) event.getWorld();
        PistonStructureResolver pistonHelper = event.getStructureHelper();
        Player player = DistExecutor.safeCallWhenOn(Dist.CLIENT, () -> () -> Minecraft.getInstance().player);
        if (world.isClientSide && player != null) {
            if (pistonHelper.resolve()) {
                player.sendMessage(new TextComponent(String.format(Locale.ENGLISH, "Piston will extend moving %d blocks and destroy %d blocks", pistonHelper.getToPush().size(), pistonHelper.getToDestroy().size())), player.getUUID());
            } else {
                player.sendMessage(new TextComponent("Piston won't extend"), player.getUUID());
            }
        }
        if (pistonHelper.resolve()) {
            List<BlockPos> posList = pistonHelper.getToPush();
            for (BlockPos newPos : posList) {
                BlockState state = event.getWorld().getBlockState(newPos);
                if (state.getBlock() == Blocks.BLACK_WOOL) {
                    Block.dropResources(state, world, newPos);
                    world.setBlockAndUpdate(newPos, Blocks.AIR.defaultBlockState());
                }
            }
        }
        // Make the block move up and out of the way so long as it won't replace the piston
        BlockPos pushedBlockPos = event.getFaceOffsetPos();
        if (world.getBlockState(pushedBlockPos).getBlock() == shiftOnMove.get() && event.getDirection() != Direction.DOWN) {
            world.setBlockAndUpdate(pushedBlockPos, Blocks.AIR.defaultBlockState());
            world.setBlockAndUpdate(pushedBlockPos.above(), shiftOnMove.get().defaultBlockState());
        }
        // Block pushing cobblestone (directly, indirectly works)
        event.setCanceled(event.getWorld().getBlockState(event.getFaceOffsetPos()).getBlock() == Blocks.COBBLESTONE);
    } else {
        boolean isSticky = event.getWorld().getBlockState(event.getPos()).getBlock() == Blocks.STICKY_PISTON;
        Player player = DistExecutor.safeCallWhenOn(Dist.CLIENT, () -> () -> Minecraft.getInstance().player);
        if (event.getWorld().isClientSide() && player != null) {
            if (isSticky) {
                BlockPos targetPos = event.getFaceOffsetPos().relative(event.getDirection());
                boolean canPush = PistonBaseBlock.isPushable(event.getWorld().getBlockState(targetPos), (Level) event.getWorld(), event.getFaceOffsetPos(), event.getDirection().getOpposite(), false, event.getDirection());
                boolean isAir = event.getWorld().isEmptyBlock(targetPos);
                player.sendMessage(new TextComponent(String.format(Locale.ENGLISH, "Piston will retract moving %d blocks", !isAir && canPush ? 1 : 0)), player.getUUID());
            } else {
                player.sendMessage(new TextComponent("Piston will retract"), player.getUUID());
            }
        }
        // Offset twice to see if retraction will pull cobblestone
        event.setCanceled(event.getWorld().getBlockState(event.getFaceOffsetPos().relative(event.getDirection())).getBlock() == Blocks.COBBLESTONE && isSticky);
    }
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) Player(net.minecraft.world.entity.player.Player) BlockState(net.minecraft.world.level.block.state.BlockState) PistonStructureResolver(net.minecraft.world.level.block.piston.PistonStructureResolver) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 8 with TextComponent

use of net.minecraft.network.chat.TextComponent in project MinecraftForge by MinecraftForge.

the class EntitySelectorTest method setup.

public void setup(FMLCommonSetupEvent event) {
    EntitySelectorOptions.register("health", this::healthArgument, parser -> true, new TextComponent("Selects entities based on their current health."));
    EntitySelectorManager.register("er", new ExampleCustomSelector());
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent)

Example 9 with TextComponent

use of net.minecraft.network.chat.TextComponent in project MinecraftForge by MinecraftForge.

the class ExtendedButton method renderButton.

/**
 * Draws this button to the screen.
 */
@Override
public void renderButton(PoseStack mStack, int mouseX, int mouseY, float partial) {
    Minecraft mc = Minecraft.getInstance();
    int k = this.getYImage(this.isHovered);
    GuiUtils.drawContinuousTexturedBox(mStack, WIDGETS_LOCATION, this.x, this.y, 0, 46 + k * 20, this.width, this.height, 200, 20, 2, 3, 2, 2, this.getBlitOffset());
    this.renderBg(mStack, mc, mouseX, mouseY);
    Component buttonText = this.getMessage();
    int strWidth = mc.font.width(buttonText);
    int ellipsisWidth = mc.font.width("...");
    if (strWidth > width - 6 && strWidth > ellipsisWidth)
        // TODO, srg names make it hard to figure out how to append to an ITextProperties from this trim operation, wraping this in StringTextComponent is kinda dirty.
        buttonText = new TextComponent(mc.font.substrByWidth(buttonText, width - 6 - ellipsisWidth).getString() + "...");
    drawCenteredString(mStack, mc.font, buttonText, this.x + this.width / 2, this.y + (this.height - 8) / 2, getFGColor());
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) TextComponent(net.minecraft.network.chat.TextComponent) Component(net.minecraft.network.chat.Component) Minecraft(net.minecraft.client.Minecraft)

Example 10 with TextComponent

use of net.minecraft.network.chat.TextComponent in project MinecraftForge by MinecraftForge.

the class LoadingErrorScreen method init.

@Override
public void init() {
    super.init();
    this.clearWidgets();
    this.errorHeader = new TextComponent(ChatFormatting.RED + ForgeI18n.parseMessage("fml.loadingerrorscreen.errorheader", this.modLoadErrors.size()) + ChatFormatting.RESET);
    this.warningHeader = new TextComponent(ChatFormatting.YELLOW + ForgeI18n.parseMessage("fml.loadingerrorscreen.warningheader", this.modLoadErrors.size()) + ChatFormatting.RESET);
    int yOffset = 46;
    this.addRenderableWidget(new ExtendedButton(50, this.height - yOffset, this.width / 2 - 55, 20, new TextComponent(ForgeI18n.parseMessage("fml.button.open.mods.folder")), b -> Util.getPlatform().openFile(modsDir.toFile())));
    this.addRenderableWidget(new ExtendedButton(this.width / 2 + 5, this.height - yOffset, this.width / 2 - 55, 20, new TextComponent(ForgeI18n.parseMessage("fml.button.open.file", logFile.getFileName())), b -> Util.getPlatform().openFile(logFile.toFile())));
    if (this.modLoadErrors.isEmpty()) {
        this.addRenderableWidget(new ExtendedButton(this.width / 4, this.height - 24, this.width / 2, 20, new TextComponent(ForgeI18n.parseMessage("fml.button.continue.launch")), b -> {
            this.minecraft.setScreen(null);
        }));
    } else {
        this.addRenderableWidget(new ExtendedButton(this.width / 4, this.height - 24, this.width / 2, 20, new TextComponent(ForgeI18n.parseMessage("fml.button.open.file", dumpedLocation.getFileName())), b -> Util.getPlatform().openFile(dumpedLocation.toFile())));
    }
    this.entryList = new LoadingEntryList(this, this.modLoadErrors, this.modLoadWarnings);
    this.addWidget(this.entryList);
    this.setFocused(this.entryList);
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) Font(net.minecraft.client.gui.Font) ObjectSelectionList(net.minecraft.client.gui.components.ObjectSelectionList) PoseStack(com.mojang.blaze3d.vertex.PoseStack) Strings(com.google.common.base.Strings) Minecraft(net.minecraft.client.Minecraft) ModLoadingWarning(net.minecraftforge.fml.ModLoadingWarning) ChatFormatting(net.minecraft.ChatFormatting) Path(java.nio.file.Path) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) FMLPaths(net.minecraftforge.fml.loading.FMLPaths) Component(net.minecraft.network.chat.Component) LoadingFailedException(net.minecraftforge.fml.LoadingFailedException) File(java.io.File) Objects(java.util.Objects) Util(net.minecraft.Util) TextComponent(net.minecraft.network.chat.TextComponent) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Paths(java.nio.file.Paths) ErrorScreen(net.minecraft.client.gui.screens.ErrorScreen) FormattedCharSequence(net.minecraft.util.FormattedCharSequence) ForgeI18n(net.minecraftforge.common.ForgeI18n) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ModLoadingException(net.minecraftforge.fml.ModLoadingException) ExtendedButton(net.minecraftforge.client.gui.widget.ExtendedButton) ExtendedButton(net.minecraftforge.client.gui.widget.ExtendedButton)

Aggregations

TextComponent (net.minecraft.network.chat.TextComponent)19 Component (net.minecraft.network.chat.Component)4 List (java.util.List)3 Minecraft (net.minecraft.client.Minecraft)3 CommandSourceStack (net.minecraft.commands.CommandSourceStack)3 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)3 ServerPlayer (net.minecraft.server.level.ServerPlayer)3 AbstractContainerMenu (net.minecraft.world.inventory.AbstractContainerMenu)3 Commands (net.minecraft.commands.Commands)2 ClientboundOpenScreenPacket (net.minecraft.network.protocol.game.ClientboundOpenScreenPacket)2 ClientboundLoginDisconnectPacket (net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket)2 ResourceLocation (net.minecraft.resources.ResourceLocation)2 Player (net.minecraft.world.entity.player.Player)2 ForgeI18n (net.minecraftforge.common.ForgeI18n)2 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)2 Strings (com.google.common.base.Strings)1 Maps (com.google.common.collect.Maps)1 Multimap (com.google.common.collect.Multimap)1 PoseStack (com.mojang.blaze3d.vertex.PoseStack)1 ArgumentBuilder (com.mojang.brigadier.builder.ArgumentBuilder)1