Implemented Nukkit MessageBuilder

- Added CMDSender#supportsChatEvents because Nukkit players don't support
  hover or click events. This means that isPlayer method is not sufficient
  for finding out if the sender can produce these events.
This commit is contained in:
Risto Lahtela 2020-08-14 13:13:38 +03:00
parent 97fdc4646b
commit 007e5bf1fb
10 changed files with 117 additions and 8 deletions

View File

@ -49,4 +49,9 @@ public class BukkitPlayerCMDSender extends BukkitCMDSender {
public ChatFormatter getFormatter() {
return new PlayerChatFormatter();
}
@Override
public boolean supportsChatEvents() {
return true;
}
}

View File

@ -48,5 +48,8 @@ public class BungeePlayerCMDSender extends BungeeCMDSender {
return new PlayerChatFormatter();
}
@Override
public boolean supportsChatEvents() {
return true;
}
}

View File

@ -167,7 +167,7 @@ public class DatabaseCommands {
if (toDB.getState() != Database.State.OPEN) toDB.init();
if (sender.isPlayer()) {
if (sender.supportsChatEvents()) {
sender.buildMessage()
.addPart(colors.getMainColor() + "You are about to overwrite data in Plan " + toDB.getType().getName() + " with data in " + backupDBFile.toPath()).newLine()
.addPart("Confirm: ").addPart("§2§l[\u2714]").command("/" + mainCommand + " accept")
@ -219,7 +219,7 @@ public class DatabaseCommands {
throw new IllegalArgumentException(locale.getString(ManageLang.FAIL_SAME_DB));
}
if (sender.isPlayer()) {
if (sender.supportsChatEvents()) {
sender.buildMessage()
.addPart(colors.getMainColor() + "You are about to overwrite data in Plan " + toDB.getName() + " with data in " + fromDB.getName()).newLine()
.addPart("Confirm: ").addPart("§2§l[\u2714]").command("/" + mainCommand + " accept")
@ -274,7 +274,7 @@ public class DatabaseCommands {
DBType fromDB = arguments.get(0).flatMap(DBType::getForName)
.orElseThrow(() -> new IllegalArgumentException(locale.getString(ManageLang.FAIL_INCORRECT_DB, arguments.get(0).orElse("<MySQL/SQLite/H2>"))));
if (sender.isPlayer()) {
if (sender.supportsChatEvents()) {
sender.buildMessage()
.addPart(colors.getMainColor() + "You are about to remove all Plan-data in " + fromDB.getName()).newLine()
.addPart("Confirm: ").addPart("§2§l[\u2714]").command("/" + mainCommand + " accept")
@ -331,7 +331,7 @@ public class DatabaseCommands {
Database database = dbSystem.getDatabase();
if (sender.isPlayer()) {
if (sender.supportsChatEvents()) {
sender.buildMessage()
.addPart(colors.getMainColor() + "You are about to remove data of " + playerUUID + " from " + database.getType().getName()).newLine()
.addPart("Confirm: ").addPart("§2§l[\u2714]").command("/" + mainCommand + " accept")

View File

@ -77,7 +77,7 @@ public class LinkCommands {
}
MessageBuilder linkTo(MessageBuilder builder, CMDSender sender, String address) {
if (sender.isPlayer()) {
if (sender.supportsChatEvents()) {
builder.addPart(colors.getTertiaryColor() + "§l[Link]").link(address).hover(address);
} else {
builder.addPart(colors.getTertiaryColor() + address);

View File

@ -197,7 +197,7 @@ public class RegistrationCommands {
throw new IllegalArgumentException(locale.getString(CommandLang.USER_NOT_LINKED));
}
if (sender.isPlayer()) {
if (sender.supportsChatEvents()) {
sender.buildMessage()
.addPart(colors.getMainColor() + "You are about to unregister '" + presentUser.getUsername() + "' linked to " + presentUser.getLinkedTo()).newLine()
.addPart("Confirm: ").addPart("§2§l[\u2714]").command("/" + mainCommand + " accept")

View File

@ -44,6 +44,10 @@ public interface CMDSender {
return getPlayerName().isPresent();
}
default boolean supportsChatEvents() {
return false;
}
Optional<UUID> getUUID();
void send(String message);

View File

@ -0,0 +1,87 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan 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 Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.commands.use;
import java.util.Collection;
public class NukkitMessageBuilder implements MessageBuilder {
private final NukkitCMDSender sender;
private final StringBuilder builder;
public NukkitMessageBuilder(NukkitCMDSender sender) {
this.sender = sender;
builder = new StringBuilder();
}
@Override
public MessageBuilder addPart(String part) {
builder.append(part);
return this;
}
@Override
public MessageBuilder newLine() {
builder.append("\n");
return this;
}
@Override
public MessageBuilder link(String link) {
addPart(link);
return this;
}
@Override
public MessageBuilder command(String s) {
return this;
}
@Override
public MessageBuilder hover(String s) {
return this;
}
@Override
public MessageBuilder hover(String... strings) {
return this;
}
@Override
public MessageBuilder hover(Collection<String> collection) {
return this;
}
@Override
public MessageBuilder indent(int amount) {
for (int i = 0; i < amount; i++) {
builder.append(' ');
}
return this;
}
@Override
public MessageBuilder tabular(CharSequence charSequence) {
addPart(sender.getFormatter().table(charSequence.toString(), ":"));
return this;
}
@Override
public void send() {
sender.sender.sendMessage(builder.toString());
}
}

View File

@ -45,4 +45,9 @@ public class SpongePlayerCMDSender extends SpongeCMDSender {
public ChatFormatter getFormatter() {
return new PlayerChatFormatter();
}
@Override
public boolean supportsChatEvents() {
return true;
}
}

View File

@ -32,7 +32,7 @@ public class VelocityCMDSender implements CMDSender {
@Override
public MessageBuilder buildMessage() {
return null; /*TODO*/
return new VelocityMessageBuilder(this);
}
@Override

View File

@ -45,4 +45,9 @@ public class VelocityPlayerCMDSender extends VelocityCMDSender {
public ChatFormatter getFormatter() {
return new PlayerChatFormatter();
}
@Override
public boolean supportsChatEvents() {
return true;
}
}