From 5606e752c2ead3f3904d1c45afb48f6388409f78 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Thu, 25 Apr 2019 19:11:58 +1000 Subject: [PATCH] Use wrappers for the Format-type components --- config/checkstyle/import-control.xml | 1 - .../worldedit/command/SelectionCommands.java | 4 ++-- .../worldedit/command/UtilityCommands.java | 19 ++++++++------- .../component/{Code.java => CodeFormat.java} | 24 ++++++++++++++----- .../formatting/component/CommandListBox.java | 5 ++-- .../formatting/component/CommandUsageBox.java | 13 +++++----- .../{Error.java => ErrorFormat.java} | 21 +++++++++++++--- .../{Label.java => LabelFormat.java} | 21 +++++++++++++--- .../util/formatting/component/MessageBox.java | 4 ++-- .../{Subtle.java => SubtleFormat.java} | 21 +++++++++++++--- .../component/TextComponentProducer.java | 23 +++++++++++++++++- 11 files changed, 117 insertions(+), 39 deletions(-) rename worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/{Code.java => CodeFormat.java} (66%) rename worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/{Error.java => ErrorFormat.java} (65%) rename worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/{Label.java => LabelFormat.java} (65%) rename worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/{Subtle.java => SubtleFormat.java} (65%) diff --git a/config/checkstyle/import-control.xml b/config/checkstyle/import-control.xml index b4a2f4808..28ccad1e4 100644 --- a/config/checkstyle/import-control.xml +++ b/config/checkstyle/import-control.xml @@ -16,7 +16,6 @@ - diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java index cd15be1ff..3c1be05fd 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java @@ -56,7 +56,7 @@ import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.util.Countable; import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.formatting.component.CommandListBox; -import com.sk89q.worldedit.util.formatting.component.Subtle; +import com.sk89q.worldedit.util.formatting.component.SubtleFormat; import com.sk89q.worldedit.util.formatting.component.TextComponentProducer; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BaseBlock; @@ -755,7 +755,7 @@ public class SelectionCommands { } else { CommandListBox box = new CommandListBox("Selection modes"); TextComponentProducer contents = box.getContents(); - contents.append(new Subtle("Select one of the modes below:").append(Component.newline()).create()); + contents.append(SubtleFormat.wrap("Select one of the modes below:")).newline(); box.appendCommand("cuboid", "Select two corners of a cuboid", "//sel cuboid"); box.appendCommand("extend", "Fast cuboid selection mode", "//sel extend"); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java index bd0c8ba53..2e2defc19 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java @@ -58,14 +58,15 @@ import com.sk89q.worldedit.util.command.CommandMapping; import com.sk89q.worldedit.util.command.Dispatcher; import com.sk89q.worldedit.util.command.PrimaryAliasComparator; import com.sk89q.worldedit.util.command.binding.Text; -import com.sk89q.worldedit.util.formatting.component.Code; +import com.sk89q.worldedit.util.formatting.component.CodeFormat; import com.sk89q.worldedit.util.formatting.component.CommandListBox; import com.sk89q.worldedit.util.formatting.component.CommandUsageBox; -import com.sk89q.worldedit.util.formatting.component.Error; -import com.sk89q.worldedit.util.formatting.component.Subtle; +import com.sk89q.worldedit.util.formatting.component.ErrorFormat; +import com.sk89q.worldedit.util.formatting.component.SubtleFormat; import com.sk89q.worldedit.util.formatting.component.TextComponentProducer; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; +import com.sk89q.worldedit.util.formatting.text.format.TextColor; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockTypes; @@ -671,17 +672,19 @@ public class UtilityCommands { // Box CommandListBox box = new CommandListBox(String.format("Help: page %d/%d ", page + 1, pageTotal)); - TextComponentProducer tip = new Subtle(""); + TextComponentProducer tip = new TextComponentProducer(); + tip.getBuilder().content("").color(TextColor.GRAY); TextComponentProducer contents = box.getContents(); if (offset >= aliases.size()) { - tip.append(new Error(String.format("There is no page %d (total number of pages is %d).", page + 1, pageTotal)).create()).append(Component.newline()); + tip.append(ErrorFormat.wrap(String.format("There is no page %d (total number of pages is %d).", page + 1, pageTotal))).newline(); } else { List list = aliases.subList(offset, Math.min(offset + perPage, aliases.size())); - tip.append(TextComponent.of("Type ")); - tip.append(new Code("//help ").append(TextComponent.of(" []")).create()); - tip.append(TextComponent.of(" for more information.")).append(Component.newline()); + tip.append("Type "); + tip.append(CodeFormat.wrap("//help ")); + tip.append(" [] for more information."); + tip.newline(); // Add each command for (CommandMapping mapping : list) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/Code.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CodeFormat.java similarity index 66% rename from worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/Code.java rename to worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CodeFormat.java index 6cbad7f3a..134239546 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/Code.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CodeFormat.java @@ -19,18 +19,30 @@ package com.sk89q.worldedit.util.formatting.component; +import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.format.TextColor; /** * Represents a fragment representing a command that is to be typed. */ -public class Code extends TextComponentProducer { +public class CodeFormat extends TextComponentProducer { - /** - * Create a new instance. - */ - public Code(String message) { - getBuilder().content(message).color(TextColor.AQUA); + private CodeFormat() { + getBuilder().content("").color(TextColor.AQUA); } + /** + * Creates a CodeFormat with the given message. + * + * @param texts The text + * @return The Component + */ + public static Component wrap(String ... texts) { + CodeFormat code = new CodeFormat(); + for (String text: texts) { + code.append(text); + } + + return code.create(); + } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CommandListBox.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CommandListBox.java index 846d08853..ac360e99c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CommandListBox.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CommandListBox.java @@ -19,7 +19,6 @@ package com.sk89q.worldedit.util.formatting.component; -import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.event.ClickEvent; import com.sk89q.worldedit.util.formatting.text.event.HoverEvent; @@ -44,7 +43,7 @@ public class CommandListBox extends MessageBox { public CommandListBox appendCommand(String alias, String description, String insertion) { if (!first) { - getContents().append(Component.newline()); + getContents().newline(); } TextComponent commandName = TextComponent.of(alias, TextColor.GOLD); if (insertion != null) { @@ -53,7 +52,7 @@ public class CommandListBox extends MessageBox { .hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to select"))); } getContents().append(commandName.append(TextComponent.of(": "))); - getContents().append(TextComponent.of(description)); + getContents().append(description); first = false; return this; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CommandUsageBox.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CommandUsageBox.java index e15d862cc..04a31df3b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CommandUsageBox.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/CommandUsageBox.java @@ -29,7 +29,6 @@ import com.sk89q.worldedit.util.command.Description; import com.sk89q.worldedit.util.command.Dispatcher; import com.sk89q.worldedit.util.command.PrimaryAliasComparator; import com.sk89q.worldedit.util.formatting.text.Component; -import com.sk89q.worldedit.util.formatting.text.TextComponent; import java.util.ArrayList; import java.util.List; @@ -88,20 +87,20 @@ public class CommandUsageBox extends TextComponentProducer { TextComponentProducer contents = new TextComponentProducer(); if (description.getUsage() != null) { - contents.append(new Label("Usage: ").create()); - contents.append(TextComponent.of(description.getUsage())); + contents.append(LabelFormat.wrap("Usage: ")); + contents.append(description.getUsage()); } else { - contents.append(new Subtle("Usage information is not available.").create()); + contents.append(SubtleFormat.wrap("Usage information is not available.")); } contents.append(Component.newline()); if (description.getHelp() != null) { - contents.append(TextComponent.of(description.getHelp())); + contents.append(description.getHelp()); } else if (description.getDescription() != null) { - contents.append(TextComponent.of(description.getDescription())); + contents.append(description.getDescription()); } else { - contents.append(new Subtle("No further help is available.").create()); + contents.append(SubtleFormat.wrap("No further help is available.")); } MessageBox box = new MessageBox("Help for " + commandString, contents); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/Error.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/ErrorFormat.java similarity index 65% rename from worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/Error.java rename to worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/ErrorFormat.java index 50cef22f2..b30ad8324 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/Error.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/ErrorFormat.java @@ -19,18 +19,33 @@ package com.sk89q.worldedit.util.formatting.component; +import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.format.TextColor; /** * Represents a fragment representing an error. */ -public class Error extends TextComponentProducer { +public class ErrorFormat extends TextComponentProducer { /** * Create a new instance. */ - public Error(String message) { - getBuilder().content(message).color(TextColor.RED); + private ErrorFormat() { + getBuilder().content("").color(TextColor.RED); } + /** + * Creates an ErrorFormat with the given message. + * + * @param texts The text + * @return The Component + */ + public static Component wrap(String ... texts) { + ErrorFormat error = new ErrorFormat(); + for (String component : texts) { + error.append(component); + } + + return error.create(); + } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/Label.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/LabelFormat.java similarity index 65% rename from worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/Label.java rename to worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/LabelFormat.java index 02d438f8d..4dda69349 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/Label.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/LabelFormat.java @@ -19,18 +19,33 @@ package com.sk89q.worldedit.util.formatting.component; +import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.format.TextColor; /** * Represents a fragment representing a label. */ -public class Label extends TextComponentProducer { +public class LabelFormat extends TextComponentProducer { /** * Create a new instance. */ - public Label(String message) { - getBuilder().content(message).color(TextColor.YELLOW); + private LabelFormat() { + getBuilder().content("").color(TextColor.YELLOW); } + /** + * Creates a LabelFormat with the given message. + * + * @param texts The text + * @return The Component + */ + public static Component wrap(String ... texts) { + LabelFormat label = new LabelFormat(); + for (String component : texts) { + label.append(component); + } + + return label.create(); + } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/MessageBox.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/MessageBox.java index 5193f0e6b..6828589bc 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/MessageBox.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/MessageBox.java @@ -47,12 +47,12 @@ public class MessageBox extends TextComponentProducer { append(TextComponent.of(createBorder(leftSide), TextColor.YELLOW)); } append(Component.space()); - append(TextComponent.of(title)); + append(title); append(Component.space()); if (rightSide > 0) { append(TextComponent.of(createBorder(rightSide), TextColor.YELLOW)); } - append(Component.newline()); + newline(); this.contents = contents; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/Subtle.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/SubtleFormat.java similarity index 65% rename from worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/Subtle.java rename to worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/SubtleFormat.java index 609824838..5987675f7 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/Subtle.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/SubtleFormat.java @@ -19,18 +19,33 @@ package com.sk89q.worldedit.util.formatting.component; +import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.format.TextColor; /** * Represents a subtle part of the message. */ -public class Subtle extends TextComponentProducer { +public class SubtleFormat extends TextComponentProducer { /** * Create a new instance. */ - public Subtle(String message) { - getBuilder().color(TextColor.GRAY).content(message); + private SubtleFormat() { + getBuilder().content("").color(TextColor.GRAY); } + /** + * Creates a SubtleFormat with the given message. + * + * @param texts The text + * @return The Component + */ + public static Component wrap(String ... texts) { + SubtleFormat subtle = new SubtleFormat(); + for (String component : texts) { + subtle.append(component); + } + + return subtle.create(); + } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/TextComponentProducer.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/TextComponentProducer.java index 2c3228ee2..32a9f8d30 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/TextComponentProducer.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/TextComponentProducer.java @@ -31,7 +31,7 @@ public class TextComponentProducer { } /** - * Adds a component as a child to this Producer + * Adds a component as a child to this Producer. * * @param component The component * @return The producer, for chaining @@ -41,6 +41,27 @@ public class TextComponentProducer { return this; } + /** + * Adds a string as a child to this Producer. + * + * @param string The text + * @return The producer, for chaining + */ + public TextComponentProducer append(String string) { + getBuilder().append(TextComponent.of(string)); + return this; + } + + /** + * Adds a newline as a child to this Producer. + * + * @return The producer, for chaining + */ + public TextComponentProducer newline() { + getBuilder().append(Component.newline()); + return this; + } + public TextComponent create() { return builder.build(); }