From 174af254e8124afce242f9965d0ae8babc7b745e Mon Sep 17 00:00:00 2001 From: bdew Date: Thu, 30 Jul 2015 13:34:34 +0300 Subject: [PATCH] Added ChatHelper - allows building chat message with less boilerplate --- build.properties | 2 +- src/net/bdew/lib/helpers/ChatHelper.scala | 60 +++++++++++++++++++ .../multiblock/tile/TileControllerGui.scala | 10 +--- 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 src/net/bdew/lib/helpers/ChatHelper.scala diff --git a/build.properties b/build.properties index 206b437..dae74ce 100644 --- a/build.properties +++ b/build.properties @@ -1,4 +1,4 @@ -bdlib.version=1.9.1 +bdlib.version=1.9.2 curseforge.id=70496 minecraft.version=1.7.10 diff --git a/src/net/bdew/lib/helpers/ChatHelper.scala b/src/net/bdew/lib/helpers/ChatHelper.scala new file mode 100644 index 0000000..0afae0a --- /dev/null +++ b/src/net/bdew/lib/helpers/ChatHelper.scala @@ -0,0 +1,60 @@ +/* + * Copyright (c) bdew, 2013 - 2015 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * http://bdew.net/minecraft-mod-public-license/ + */ + +package net.bdew.lib.helpers + +import net.minecraft.util._ + +import scala.language.implicitConversions + +class RichChatComponent(v: IChatComponent) { + def &(that: IChatComponent) = v.appendSibling(that) + + private def applyStyle(f: ChatStyle => Unit) = { + f(v.getChatStyle) + v + } + + def setColor(c: EnumChatFormatting) = applyStyle(_.setColor(c)) + def setBold(b: Boolean) = applyStyle(_.setBold(b)) + def setItalic(b: Boolean) = applyStyle(_.setItalic(b)) + def setObfuscated(b: Boolean) = applyStyle(_.setObfuscated(b)) + def setStrikethrough(b: Boolean) = applyStyle(_.setStrikethrough(b)) + def setUnderlined(b: Boolean) = applyStyle(_.setUnderlined(b)) + +} + +object ChatHelper { + + object Color { + val BLACK = EnumChatFormatting.BLACK + val DARK_BLUE = EnumChatFormatting.DARK_BLUE + val DARK_GREEN = EnumChatFormatting.DARK_GREEN + val DARK_AQUA = EnumChatFormatting.DARK_AQUA + val DARK_RED = EnumChatFormatting.DARK_RED + val DARK_PURPLE = EnumChatFormatting.DARK_PURPLE + val GOLD = EnumChatFormatting.GOLD + val GRAY = EnumChatFormatting.GRAY + val DARK_GRAY = EnumChatFormatting.DARK_GRAY + val BLUE = EnumChatFormatting.BLUE + val GREEN = EnumChatFormatting.GREEN + val AQUA = EnumChatFormatting.AQUA + val RED = EnumChatFormatting.RED + val LIGHT_PURPLE = EnumChatFormatting.LIGHT_PURPLE + val YELLOW = EnumChatFormatting.YELLOW + val WHITE = EnumChatFormatting.WHITE + } + + implicit def str2chat(s: String): ChatComponentText = new ChatComponentText(s) + implicit def str2rich(s: String): RichChatComponent = new RichChatComponent(new ChatComponentText(s)) + implicit def pimpIChatComponent(v: IChatComponent): RichChatComponent = new RichChatComponent(v) + + def C(s: String) = new ChatComponentText(s) + def L(s: String, params: IChatComponent*) = new ChatComponentTranslation(s, params: _*) +} diff --git a/src/net/bdew/lib/multiblock/tile/TileControllerGui.scala b/src/net/bdew/lib/multiblock/tile/TileControllerGui.scala index 96c89a9..bc4af81 100644 --- a/src/net/bdew/lib/multiblock/tile/TileControllerGui.scala +++ b/src/net/bdew/lib/multiblock/tile/TileControllerGui.scala @@ -10,21 +10,17 @@ package net.bdew.lib.multiblock.tile import net.minecraft.entity.player.EntityPlayer -import net.minecraft.util.{ChatComponentTranslation, ChatStyle, EnumChatFormatting} abstract class TileControllerGui extends TileController { def openGui(player: EntityPlayer) def onClick(player: EntityPlayer) = { + import net.bdew.lib.helpers.ChatHelper._ val missing = cfg.required.filter({ case (mod, cnt) => getNumOfModules(mod) < cnt }) if (missing.nonEmpty) { - player.addChatMessage(new ChatComponentTranslation("bdlib.multiblock.incomplete") - .setChatStyle(new ChatStyle().setColor(EnumChatFormatting.RED))) + player.addChatMessage(L("bdlib.multiblock.incomplete").setColor(Color.RED)) for ((mod, cnt) <- missing) - player.addChatMessage( - new ChatComponentTranslation("- %s %s", Integer.valueOf(cnt), - new ChatComponentTranslation(resources.getModuleName(mod))) - .setChatStyle(new ChatStyle().setColor(EnumChatFormatting.RED))) + player.addChatMessage(L("- %s %s", cnt.toString, L(resources.getModuleName(mod))).setColor(Color.RED)) } else openGui(player) }