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;
}
}
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);
}
}
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());
}
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());
}
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);
}
Aggregations