Velocity 3.0.0 support by Kopo (#1961)

* Add Velocity 3.0.0 support

- Switched `VelocityCommand` implementation to `SimpleCommand` subinterface
- Removed old & deprecated `text` component handling API in favor of Adventure components

* Fix hover & click events not working
This commit is contained in:
Antti Koponen 2021-06-29 23:29:59 +03:00 committed by GitHub
parent 90f646bd1d
commit 6b54efef3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 27 deletions

View File

@ -73,7 +73,7 @@ subprojects {
spongeVersion = "7.3.0"
nukkitVersion = "1.0-SNAPSHOT"
bungeeVersion = "1.16-R0.4"
velocityVersion = "1.0.0-SNAPSHOT"
velocityVersion = "3.0.0-SNAPSHOT"
redisBungeeVersion = "0.6.3"
commonsTextVersion = "1.9"

View File

@ -23,6 +23,7 @@ import com.djrapitops.plan.exceptions.EnableException;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.lang.PluginLang;
import com.djrapitops.plan.settings.theme.PlanColorScheme;
import com.velocitypowered.api.command.CommandManager;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
@ -146,9 +147,12 @@ public class PlanVelocity implements PlanPlugin {
logger.warn("Attempted to register a null command!");
return;
}
proxy.getCommandManager().register(
new VelocityCommand(runnableFactory, system.getErrorLogger(), command),
command.getAliases().toArray(new String[0])
CommandManager commandManager = proxy.getCommandManager();
commandManager.register(
commandManager.metaBuilder(command.getPrimaryAlias())
.aliases(command.getAliases().toArray(new String[0]))
.build(),
new VelocityCommand(runnableFactory, system.getErrorLogger(), command)
);
}

View File

@ -17,7 +17,7 @@
package com.djrapitops.plan.commands.use;
import com.velocitypowered.api.command.CommandSource;
import net.kyori.text.TextComponent;
import net.kyori.adventure.text.Component;
import java.util.Objects;
import java.util.Optional;
@ -53,7 +53,7 @@ public class VelocityCMDSender implements CMDSender {
@Override
public void send(String message) {
commandSource.sendMessage(TextComponent.of(message));
commandSource.sendMessage(Component.text(message));
}
@Override

View File

@ -18,8 +18,8 @@ package com.djrapitops.plan.commands.use;
import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import com.velocitypowered.api.command.Command;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player;
import net.playeranalytics.plugin.scheduling.RunnableFactory;
@ -27,7 +27,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class VelocityCommand implements Command {
public class VelocityCommand implements SimpleCommand {
private final RunnableFactory runnableFactory;
private final ErrorLogger errorLogger;
@ -40,7 +40,9 @@ public class VelocityCommand implements Command {
}
@Override
public void execute(CommandSource source, String[] args) {
public void execute(final Invocation invocation) {
CommandSource source = invocation.source();
String[] args = invocation.arguments();
runnableFactory.create(() -> {
try {
command.getExecutor().accept(getSender(source), new Arguments(args));
@ -62,7 +64,9 @@ public class VelocityCommand implements Command {
}
@Override
public List<String> suggest(CommandSource source, String[] currentArgs) {
public List<String> suggest(final Invocation invocation) {
CommandSource source = invocation.source();
String[] currentArgs = invocation.arguments();
try {
return command.getArgumentResolver().apply(getSender(source), new Arguments(currentArgs));
} catch (Exception e) {

View File

@ -16,9 +16,10 @@
*/
package com.djrapitops.plan.commands.use;
import net.kyori.text.TextComponent;
import net.kyori.text.event.ClickEvent;
import net.kyori.text.event.HoverEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import org.apache.commons.text.TextStringBuilder;
import java.util.Collection;
@ -26,65 +27,77 @@ import java.util.Collection;
public class VelocityMessageBuilder implements MessageBuilder {
private final VelocityCMDSender sender;
private TextComponent.Builder builder;
private final TextComponent.Builder builder;
// Store reference to previous component to properly add hover & click events
private Component previousComponent;
public VelocityMessageBuilder(VelocityCMDSender sender) {
this.sender = sender;
builder = TextComponent.builder();
builder = Component.text();
}
@Override
public MessageBuilder addPart(String content) {
builder = builder.append(content);
if (previousComponent != null) {
builder.append(previousComponent);
}
previousComponent = Component.text(content);
return this;
}
@Override
public MessageBuilder newLine() {
builder = builder.append("\n");
if (previousComponent != null) {
builder.append(previousComponent);
}
previousComponent = Component.text("\n");
return this;
}
@Override
public MessageBuilder link(String url) {
builder = builder.clickEvent(ClickEvent.openUrl(url));
previousComponent = previousComponent.clickEvent(ClickEvent.openUrl(url));
return this;
}
@Override
public MessageBuilder command(String command) {
builder = builder.clickEvent(ClickEvent.runCommand(command));
previousComponent = previousComponent.clickEvent(ClickEvent.runCommand(command));
return this;
}
@Override
public MessageBuilder hover(String s) {
builder = builder.hoverEvent(HoverEvent.showText(TextComponent.of(s)));
previousComponent = previousComponent.hoverEvent(HoverEvent.showText(Component.text(s)));
return this;
}
@Override
public MessageBuilder hover(String... strings) {
TextComponent.Builder hoverText = TextComponent.builder();
TextComponent.Builder hoverText = Component.text();
for (String string : strings) {
hoverText.append(string);
hoverText.append(Component.text(string));
}
builder = builder.hoverEvent(HoverEvent.showText(hoverText.build()));
previousComponent = previousComponent.hoverEvent(HoverEvent.showText(hoverText.build()));
return this;
}
@Override
public MessageBuilder hover(Collection<String> lines) {
TextComponent.Builder hoverText = TextComponent.builder();
hoverText.append(new TextStringBuilder().appendWithSeparators(lines, "\n").build());
builder = builder.hoverEvent(HoverEvent.showText(hoverText.build()));
TextComponent.Builder hoverText = Component.text();
hoverText.append(Component.text(new TextStringBuilder().appendWithSeparators(lines, "\n").build()));
previousComponent = previousComponent.hoverEvent(HoverEvent.showText(hoverText.build()));
return this;
}
@Override
public MessageBuilder indent(int amount) {
for (int i = 0; i < amount; i++) {
builder = builder.append(" ");
if (previousComponent != null) {
builder.append(previousComponent);
}
previousComponent = Component.text(" ");
}
return this;
}
@ -96,6 +109,9 @@ public class VelocityMessageBuilder implements MessageBuilder {
@Override
public void send() {
if (previousComponent != null) {
builder.append(previousComponent);
}
sender.commandSource.sendMessage(builder.build());
}
}