From c52eb59d7f702dbcc90c47ad9aae7fedacf72e1b Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Mon, 22 Apr 2019 22:05:20 +1000 Subject: [PATCH] Get it all working --- build.gradle | 1 + worldedit-bukkit/build.gradle | 1 - .../worldedit/command/SelectionCommands.java | 21 +++++---- .../worldedit/command/UtilityCommands.java | 20 ++++---- .../extension/platform/CommandManager.java | 2 +- .../util/formatting/component/Code.java | 5 +- .../formatting/component/CommandListBox.java | 16 ++++++- .../formatting/component/CommandUsageBox.java | 17 ++++--- .../util/formatting/component/Error.java | 5 +- .../util/formatting/component/Label.java | 5 +- .../util/formatting/component/MessageBox.java | 22 +++++---- .../util/formatting/component/Subtle.java | 5 +- .../component/TextComponentProducer.java | 47 +++++++++++++++++++ worldedit-libs/build.gradle | 4 +- 14 files changed, 116 insertions(+), 55 deletions(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/TextComponentProducer.java diff --git a/build.gradle b/build.gradle index eaa4a49b5..63b4d9537 100644 --- a/build.gradle +++ b/build.gradle @@ -86,6 +86,7 @@ subprojects { mavenCentral() maven { url "http://maven.sk89q.com/repo/" } maven { url "http://repo.maven.apache.org/maven2" } + maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } } } diff --git a/worldedit-bukkit/build.gradle b/worldedit-bukkit/build.gradle index 14550bdb5..975aee9d9 100644 --- a/worldedit-bukkit/build.gradle +++ b/worldedit-bukkit/build.gradle @@ -43,7 +43,6 @@ shadowJar { include(dependency(':worldedit-core')) include(dependency('org.slf4j:slf4j-api')) include(dependency("org.slf4j:slf4j-jdk14")) - include(dependency("net.kyori:text-adapter-bukkit:1.0.3")) relocate ("org.bstats", "com.sk89q.worldedit.bukkit.bstats") { include(dependency("org.bstats:bstats-bukkit:1.4")) } 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 7c1b1889c..cd15be1ff 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 @@ -57,6 +57,7 @@ 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.TextComponentProducer; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockState; @@ -753,18 +754,18 @@ public class SelectionCommands { limit.ifPresent(integer -> player.print(integer + " points maximum.")); } else { CommandListBox box = new CommandListBox("Selection modes"); - Component contents = box.getContents(); - contents.append(new Subtle("Select one of the modes below:").append(Component.newline())); + TextComponentProducer contents = box.getContents(); + contents.append(new Subtle("Select one of the modes below:").append(Component.newline()).create()); - box.appendCommand("cuboid", "Select two corners of a cuboid"); - box.appendCommand("extend", "Fast cuboid selection mode"); - box.appendCommand("poly", "Select a 2D polygon with height"); - box.appendCommand("ellipsoid", "Select an ellipsoid"); - box.appendCommand("sphere", "Select a sphere"); - box.appendCommand("cyl", "Select a cylinder"); - box.appendCommand("convex", "Select a convex polyhedral"); + box.appendCommand("cuboid", "Select two corners of a cuboid", "//sel cuboid"); + box.appendCommand("extend", "Fast cuboid selection mode", "//sel extend"); + box.appendCommand("poly", "Select a 2D polygon with height", "//sel poly"); + box.appendCommand("ellipsoid", "Select an ellipsoid", "//sel ellipsoid"); + box.appendCommand("sphere", "Select a sphere", "//sel sphere"); + box.appendCommand("cyl", "Select a cylinder", "//sel cyl"); + box.appendCommand("convex", "Select a convex polyhedral", "//sel convex"); - player.print(box); + player.print(box.create()); return; } 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 989fbad6b..bd0c8ba53 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 @@ -62,12 +62,13 @@ import com.sk89q.worldedit.util.formatting.component.Code; 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.TextComponentProducer; +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockTypes; -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 java.util.ArrayList; import java.util.List; @@ -670,16 +671,16 @@ public class UtilityCommands { // Box CommandListBox box = new CommandListBox(String.format("Help: page %d/%d ", page + 1, pageTotal)); - Component contents = box.getContents(); - Component tip = contents.append(TextComponent.of("", TextColor.GRAY)); + TextComponentProducer tip = new Subtle(""); + 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))).append(Component.newline()); + tip.append(new Error(String.format("There is no page %d (total number of pages is %d).", page + 1, pageTotal)).create()).append(Component.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(" []"))); + tip.append(new Code("//help ").append(TextComponent.of(" []")).create()); tip.append(TextComponent.of(" for more information.")).append(Component.newline()); // Add each command @@ -697,10 +698,11 @@ public class UtilityCommands { } } - actor.print(box); + contents.append(tip.create()); + actor.print(box.create()); } else { CommandUsageBox box = new CommandUsageBox(callable, Joiner.on(" ").join(visited)); - actor.print(box); + actor.print(box.create()); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java index 11257efdf..45b5265bb 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java @@ -299,7 +299,7 @@ public final class CommandManager { actor.printError("You are not permitted to do that. Are you in the right mode?"); } catch (InvalidUsageException e) { if (e.isFullHelpSuggested()) { - actor.print(new CommandUsageBox(e.getCommand(), e.getCommandUsed("/", ""), locals)); + actor.print(new CommandUsageBox(e.getCommand(), e.getCommandUsed("/", ""), locals).create()); String message = e.getMessage(); if (message != null) { actor.printError(message); 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/Code.java index a1491551e..6cbad7f3a 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/Code.java @@ -19,19 +19,18 @@ package com.sk89q.worldedit.util.formatting.component; -import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.format.TextColor; /** * Represents a fragment representing a command that is to be typed. */ -public class Code extends TextComponent { +public class Code extends TextComponentProducer { /** * Create a new instance. */ public Code(String message) { - super(builder(message).color(TextColor.AQUA)); + getBuilder().content(message).color(TextColor.AQUA); } } 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 9e6ec3295..846d08853 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 @@ -21,6 +21,8 @@ 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; import com.sk89q.worldedit.util.formatting.text.format.TextColor; public class CommandListBox extends MessageBox { @@ -33,14 +35,24 @@ public class CommandListBox extends MessageBox { * @param title the title */ public CommandListBox(String title) { - super(title); + super(title, new TextComponentProducer()); } public CommandListBox appendCommand(String alias, String description) { + return appendCommand(alias, description, null); + } + + public CommandListBox appendCommand(String alias, String description, String insertion) { if (!first) { getContents().append(Component.newline()); } - getContents().append(TextComponent.of(alias, TextColor.GOLD).append(TextComponent.of(": "))); + TextComponent commandName = TextComponent.of(alias, TextColor.GOLD); + if (insertion != null) { + commandName = commandName + .clickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, insertion)) + .hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to select"))); + } + getContents().append(commandName.append(TextComponent.of(": "))); getContents().append(TextComponent.of(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 13eee701c..e15d862cc 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 @@ -39,7 +39,7 @@ import javax.annotation.Nullable; /** * A box to describe usage of a command. */ -public class CommandUsageBox extends TextComponent { +public class CommandUsageBox extends TextComponentProducer { /** * Create a new usage box. @@ -59,7 +59,6 @@ public class CommandUsageBox extends TextComponent { * @param locals list of locals to use */ public CommandUsageBox(CommandCallable command, String commandString, @Nullable CommandLocals locals) { - super(builder()); checkNotNull(command); checkNotNull(commandString); if (command instanceof Dispatcher) { @@ -82,18 +81,17 @@ public class CommandUsageBox extends TextComponent { } } - append(box); + append(box.create()); } private void attachCommandUsage(Description description, String commandString) { - MessageBox box = new MessageBox("Help for " + commandString); - Component contents = box.getContents(); + TextComponentProducer contents = new TextComponentProducer(); if (description.getUsage() != null) { - contents.append(new Label("Usage: ")); + contents.append(new Label("Usage: ").create()); contents.append(TextComponent.of(description.getUsage())); } else { - contents.append(new Subtle("Usage information is not available.")); + contents.append(new Subtle("Usage information is not available.").create()); } contents.append(Component.newline()); @@ -103,10 +101,11 @@ public class CommandUsageBox extends TextComponent { } else if (description.getDescription() != null) { contents.append(TextComponent.of(description.getDescription())); } else { - contents.append(new Subtle("No further help is available.")); + contents.append(new Subtle("No further help is available.").create()); } - append(box); + MessageBox box = new MessageBox("Help for " + commandString, contents); + append(box.create()); } } 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/Error.java index 52d9d7044..50cef22f2 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/Error.java @@ -19,19 +19,18 @@ package com.sk89q.worldedit.util.formatting.component; -import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.format.TextColor; /** * Represents a fragment representing an error. */ -public class Error extends TextComponent { +public class Error extends TextComponentProducer { /** * Create a new instance. */ public Error(String message) { - super(builder(message).color(TextColor.RED)); + getBuilder().content(message).color(TextColor.RED); } } 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/Label.java index a38adc7bb..02d438f8d 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/Label.java @@ -19,19 +19,18 @@ package com.sk89q.worldedit.util.formatting.component; -import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.format.TextColor; /** * Represents a fragment representing a label. */ -public class Label extends TextComponent { +public class Label extends TextComponentProducer { /** * Create a new instance. */ public Label(String message) { - super(builder(message).color(TextColor.YELLOW)); + getBuilder().content(message).color(TextColor.YELLOW); } } 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 b32c00995..5193f0e6b 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 @@ -28,17 +28,16 @@ import com.sk89q.worldedit.util.formatting.text.format.TextColor; /** * Makes for a box with a border above and below. */ -public class MessageBox extends TextComponent { +public class MessageBox extends TextComponentProducer { - public static final int GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH = 47; + private static final int GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH = 47; - private final Component contents = Component.empty(); + private final TextComponentProducer contents; /** * Create a new box. */ - public MessageBox(String title) { - super(builder()); + public MessageBox(String title, TextComponentProducer contents) { checkNotNull(title); int leftOver = GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH - title.length() - 2; @@ -54,7 +53,7 @@ public class MessageBox extends TextComponent { append(TextComponent.of(createBorder(rightSide), TextColor.YELLOW)); } append(Component.newline()); - append(contents); + this.contents = contents; } private String createBorder(int count) { @@ -66,12 +65,17 @@ public class MessageBox extends TextComponent { } /** - * Get the internal contents. + * Gets the message box contents producer. * - * @return the contents + * @return The contents producer */ - public Component getContents() { + public TextComponentProducer getContents() { return contents; } + @Override + public TextComponent create() { + append(contents.create()); + return super.create(); + } } 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/Subtle.java index a512138d4..609824838 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/Subtle.java @@ -19,19 +19,18 @@ package com.sk89q.worldedit.util.formatting.component; -import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.format.TextColor; /** * Represents a subtle part of the message. */ -public class Subtle extends TextComponent { +public class Subtle extends TextComponentProducer { /** * Create a new instance. */ public Subtle(String message) { - super(builder(message).color(TextColor.GRAY)); + getBuilder().color(TextColor.GRAY).content(message); } } 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 new file mode 100644 index 000000000..2c3228ee2 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/TextComponentProducer.java @@ -0,0 +1,47 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.util.formatting.component; + +import com.sk89q.worldedit.util.formatting.text.Component; +import com.sk89q.worldedit.util.formatting.text.TextComponent; + +public class TextComponentProducer { + + private TextComponent.Builder builder = TextComponent.builder().content(""); + + public TextComponent.Builder getBuilder() { + return builder; + } + + /** + * Adds a component as a child to this Producer + * + * @param component The component + * @return The producer, for chaining + */ + public TextComponentProducer append(Component component) { + getBuilder().append(component); + return this; + } + + public TextComponent create() { + return builder.build(); + } +} diff --git a/worldedit-libs/build.gradle b/worldedit-libs/build.gradle index f0a2359b5..b05c601e8 100644 --- a/worldedit-libs/build.gradle +++ b/worldedit-libs/build.gradle @@ -102,12 +102,12 @@ project("core") { } project("bukkit") { dependencies { - shade 'net.kyori:text-adapter-bukkit:1.0.3' + shade 'net.kyori:text-adapter-bukkit:2.0.0-SNAPSHOT' } } project("sponge") { dependencies { - shade 'net.kyori:text-adapter-spongeapi:1.0.3' + shade 'net.kyori:text-adapter-spongeapi:2.0.0-SNAPSHOT' } }