From 6a6752fdb6f95c6a0fb20be9981584ccd7874ae4 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 1 Sep 2015 12:09:45 +0200 Subject: [PATCH] Mson.toPlain now has boolea styled parameter. Txt.getItemName. --- .../mixin/MessageMixinDefault.java | 2 +- .../massivecraft/massivecore/mson/Mson.java | 32 ++++++++++++------- .../massivecraft/massivecore/util/Txt.java | 20 ++++++++++++ 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/com/massivecraft/massivecore/mixin/MessageMixinDefault.java b/src/com/massivecraft/massivecore/mixin/MessageMixinDefault.java index 57626841..cc8dbd43 100644 --- a/src/com/massivecraft/massivecore/mixin/MessageMixinDefault.java +++ b/src/com/massivecraft/massivecore/mixin/MessageMixinDefault.java @@ -102,7 +102,7 @@ public class MessageMixinDefault extends MessageMixinAbstract { for (Mson mson : msons) { - sender.sendMessage(mson.toPlain()); + sender.sendMessage(mson.toPlain(true)); } } diff --git a/src/com/massivecraft/massivecore/mson/Mson.java b/src/com/massivecraft/massivecore/mson/Mson.java index afab08cb..c4a36a89 100644 --- a/src/com/massivecraft/massivecore/mson/Mson.java +++ b/src/com/massivecraft/massivecore/mson/Mson.java @@ -861,26 +861,29 @@ public class Mson implements Serializable return raw; } - public String toPlain() + public String toPlain(boolean styled) { StringBuilder ret = new StringBuilder(); - this.toPlain0(ret); + this.toPlain0(ret, styled); return ret.toString(); } - private void toPlain0(StringBuilder builder) + private void toPlain0(StringBuilder builder, boolean styled) { if ( ! this.getText().isEmpty()) { // Color must be put in BEFORE formatting. // http://minecraft.gamepedia.com/Formatting_codes#Formatting_codes - - if (this.getEffectiveColor() != null) builder.append(this.getEffectiveColor()); - if (this.isEffectiveBold()) builder.append(ChatColor.BOLD); - if (this.isEffectiveItalic()) builder.append(ChatColor.ITALIC); - if (this.isEffectiveUnderlined()) builder.append(ChatColor.UNDERLINE); - if (this.isEffectiveStrikethrough()) builder.append(ChatColor.STRIKETHROUGH); - if (this.isEffectiveObfuscated()) builder.append(ChatColor.MAGIC); + if (styled) + { + if (this.getEffectiveColor() != null) builder.append(this.getEffectiveColor()); + if (this.isEffectiveBold()) builder.append(ChatColor.BOLD); + if (this.isEffectiveItalic()) builder.append(ChatColor.ITALIC); + if (this.isEffectiveUnderlined()) builder.append(ChatColor.UNDERLINE); + if (this.isEffectiveStrikethrough()) builder.append(ChatColor.STRIKETHROUGH); + if (this.isEffectiveObfuscated()) builder.append(ChatColor.MAGIC); + } + builder.append(this.getText()); } @@ -888,12 +891,17 @@ public class Mson implements Serializable { for (Mson part : this.getExtra()) { - builder.append(ChatColor.RESET); - part.toPlain0(builder); + if (styled) + { + builder.append(ChatColor.RESET); + } + + part.toPlain0(builder, styled); } } } + @Override public String toString() { diff --git a/src/com/massivecraft/massivecore/util/Txt.java b/src/com/massivecraft/massivecore/util/Txt.java index 0dcb0ac8..9b926b22 100644 --- a/src/com/massivecraft/massivecore/util/Txt.java +++ b/src/com/massivecraft/massivecore/util/Txt.java @@ -16,6 +16,8 @@ import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import com.massivecraft.massivecore.Predictate; import com.massivecraft.massivecore.PredictateStartsWithIgnoreCase; @@ -412,6 +414,24 @@ public class Txt return getNicedEnum(material); } + public static String getItemName(ItemStack itemStack) + { + if (InventoryUtil.isNothing(itemStack)) return Txt.parse("Nothing"); + + ChatColor color = (itemStack.getEnchantments().size() > 0) ? ChatColor.AQUA : ChatColor.RESET; + + if (itemStack.hasItemMeta()) + { + ItemMeta itemMeta = itemStack.getItemMeta(); + if (itemMeta.hasDisplayName()) + { + return color + itemMeta.getDisplayName(); + } + } + + return color + Txt.getMaterialName(itemStack.getType()); + } + // -------------------------------------------- // // Paging and chrome-tools like titleize // -------------------------------------------- //