Skip to content

Commit

Permalink
Localization & cleanup
Browse files Browse the repository at this point in the history
Replaced the most hardcoded messages with a call to MessageProvider#provide and added all of them to en.properties.
A file for German will follow soon.
Cleanup: Removed most of the CompletableFuture#get calls with #whenComple(async) and replaced the parameter of the CF returned in Punishment via #punish and #cancel with a CF<Punishment>.
Bumped version to 1.0.0-beta.2 and set source/target compatibility to 17.
  • Loading branch information
JvstvsHD committed Dec 30, 2021
1 parent 3a00fa2 commit fb512ae
Show file tree
Hide file tree
Showing 25 changed files with 362 additions and 342 deletions.
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "de.jvstvshd.punishment"
version = "1.0.0-beta.1"
version = "1.0.0-beta.2"

repositories {
maven("https://nexus.velocitypowered.com/repository/maven-public/")
Expand Down Expand Up @@ -31,6 +31,8 @@ tasks.getByName<Test>("test") {

tasks.withType<JavaCompile>().configureEach {
options.encoding = "UTF-8"
sourceCompatibility = "17"
targetCompatibility = "17"
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.List;
import java.util.stream.Collectors;

import static de.jvstvshd.velocitypunishment.util.Util.INTERNAL_ERROR;
import static de.jvstvshd.velocitypunishment.util.Util.copyComponent;

public class BanCommand implements SimpleCommand {
Expand All @@ -29,40 +28,36 @@ public BanCommand(VelocityPunishmentPlugin plugin) {
public void execute(Invocation invocation) {
CommandSource source = invocation.source();
if (invocation.arguments().length < 1) {
//source.sendMessage(Component.text().append(plugin.getMessageProvider().prefix(), plugin.getMessageProvider().provide("command.ban.usage").color(NamedTextColor.RED)));
source.sendMessage(Component.text("Please use /ban <player> [reason]").color(NamedTextColor.DARK_RED));
source.sendMessage(plugin.getMessageProvider().provide("command.ban.usage", source, true).color(NamedTextColor.RED));
return;
}
var playerResolver = plugin.getPlayerResolver();
var punishmentManager = plugin.getPunishmentManager();
var parser = new PunishmentHelper();
playerResolver.getOrQueryPlayerUuid(invocation.arguments()[0], plugin.getService()).whenComplete((uuid, throwable) -> {
playerResolver.getOrQueryPlayerUuid(invocation.arguments()[0], plugin.getService()).whenCompleteAsync((uuid, throwable) -> {
if (throwable != null) {
source.sendMessage(INTERNAL_ERROR);
source.sendMessage(plugin.getMessageProvider().internalError());
throwable.printStackTrace();
return;
}
if (uuid == null) {
source.sendMessage(Component.translatable().args(Component.text(invocation.arguments()[0])).key(invocation.arguments()[0] + " could not be found.").build());
source.sendMessage(Component.translatable().args(Component.text(invocation.arguments()[0]).color(NamedTextColor.YELLOW)).key("commands.general.not-found").color(NamedTextColor.RED));
return;
}
TextComponent component = parser.parseComponent(1, invocation);
punishmentManager.createPermanentBan(uuid, component).punish().whenCompleteAsync((unused, t) -> {
TextComponent component = parser.parseComponent(1, invocation, Component.text("ban").color(NamedTextColor.DARK_RED));
punishmentManager.createPermanentBan(uuid, component).punish().whenCompleteAsync((ban, t) -> {
if (t != null) {
t.printStackTrace();
source.sendMessage(Util.INTERNAL_ERROR);
source.sendMessage(plugin.getMessageProvider().internalError());
} else {
String uuidString = uuid.toString().toLowerCase();
source.sendMessage(Component.text("You have banned the player ").color(NamedTextColor.RED)
.append(Component.text().append(copyComponent(invocation.arguments()[0]).color(NamedTextColor.YELLOW).decorate(TextDecoration.BOLD),
Component.text("/").color(NamedTextColor.WHITE),
copyComponent(uuidString).color(NamedTextColor.RED).decorate(TextDecoration.BOLD),
Component.text(" for ").color(NamedTextColor.RED),
component,
Component.text(".").color(NamedTextColor.RED))));
source.sendMessage(plugin.getMessageProvider().provide("command.ban.success", source, true, copyComponent(invocation.arguments()[0]).color(NamedTextColor.YELLOW).decorate(TextDecoration.BOLD),
copyComponent(uuidString).color(NamedTextColor.YELLOW).decorate(TextDecoration.BOLD),
component).color(NamedTextColor.RED));
source.sendMessage(plugin.getMessageProvider().provide("commands.general.punishment.id", source, true, Component.text(ban.getPunishmentUuid().toString().toLowerCase()).color(NamedTextColor.YELLOW)));
}
});
});
}, plugin.getService());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
import net.kyori.adventure.text.format.TextDecoration;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

import static de.jvstvshd.velocitypunishment.util.Util.INTERNAL_ERROR;
Expand All @@ -36,7 +33,7 @@ public MuteCommand(VelocityPunishmentPlugin plugin, ChatListener chatListener) {
public void execute(Invocation invocation) {
CommandSource source = invocation.source();
if (invocation.arguments().length < 1) {
source.sendMessage(Component.text("Please user /mute <player> [reason]").color(NamedTextColor.DARK_RED));
source.sendMessage(plugin.getMessageProvider().provide("command.mute.usage", source, true).color(NamedTextColor.RED));
return;
}
var playerResolver = plugin.getPlayerResolver();
Expand All @@ -49,35 +46,25 @@ public void execute(Invocation invocation) {
return;
}
if (uuid == null) {
source.sendMessage(Component.text(invocation.arguments()[0] + " could not be found."));
source.sendMessage(Component.translatable().args(Component.text(invocation.arguments()[0]).color(NamedTextColor.YELLOW)).key("commands.general.not-found").color(NamedTextColor.RED));
return;
}
TextComponent reason = parser.parseComponent(1, invocation);

Mute mute;
try {
mute = punishmentManager.createPermanentMute(uuid, reason);
mute.punish().get(5, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
invocation.source().sendMessage(Util.INTERNAL_ERROR);
e.printStackTrace();
return;
}
String uuidString = uuid.toString().toLowerCase();
Component component = Component.text().append(
Component.text("You have muted the player ").color(NamedTextColor.RED),
copyComponent(invocation.arguments()[0]).color(NamedTextColor.YELLOW).decorate(TextDecoration.BOLD),
Component.text("/").color(NamedTextColor.RED),
copyComponent(uuidString).color(NamedTextColor.RED).decorate(TextDecoration.BOLD).color(NamedTextColor.YELLOW),
Component.text(" for ").color(NamedTextColor.RED),
reason,
Component.text(".").color(NamedTextColor.RED)
).build();
source.sendMessage(component);
source.sendMessage(Component.text("Punishment id: " + mute.getPunishmentUuid().toString().toLowerCase()).color(NamedTextColor.YELLOW));
if (plugin.getServer().getPlayer(uuid).isPresent()) {
chatListener.getMutes().put(uuid, new ChatListener.MuteContainer().setMute(mute));
}
TextComponent reason = parser.parseComponent(1, invocation, Component.text("mute").color(NamedTextColor.DARK_RED));
punishmentManager.createPermanentMute(uuid, reason).punish().whenComplete((mute, t) -> {
if (t != null) {
t.printStackTrace();
source.sendMessage(Util.INTERNAL_ERROR);
} else {
String uuidString = uuid.toString().toLowerCase();
source.sendMessage(plugin.getMessageProvider().provide("command.mute.success", source, true, copyComponent(invocation.arguments()[0]).color(NamedTextColor.YELLOW).decorate(TextDecoration.BOLD),
copyComponent(uuidString).color(NamedTextColor.YELLOW).decorate(TextDecoration.BOLD),
reason).color(NamedTextColor.RED));
source.sendMessage(plugin.getMessageProvider().provide("commands.general.punishment.id", source, true, Component.text(mute.getPunishmentUuid().toString().toLowerCase()).color(NamedTextColor.YELLOW)));
}
if (plugin.getServer().getPlayer(uuid).isPresent()) {
chatListener.getMutes().put(uuid, new ChatListener.MuteContainer().setMute((Mute) mute));
}
});
}, plugin.getService());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeoutException;

import static de.jvstvshd.velocitypunishment.util.Util.INTERNAL_ERROR;

Expand All @@ -42,14 +44,21 @@ public PunishmentCommand(VelocityPunishmentPlugin plugin, ChatListener chatListe
this.plugin = plugin;
}

private final static List<String> options = ImmutableList.of("annul", "remove", "info", "change");
private final static List<String> PUNISHMENT_OPTIONS = ImmutableList.of("cancel", "remove", "info", "change");
private final static List<String> ALL_OPTIONS;

static {
var full = new ArrayList<>(PUNISHMENT_OPTIONS);
full.add("playerinfo");
ALL_OPTIONS = ImmutableList.copyOf(full);
}

@Override
public void execute(Invocation invocation) {
String[] arguments = invocation.arguments();
CommandSource source = invocation.source();
if (arguments.length < 2) {
source.sendMessage(Component.text("Please use /punishment <playerinfo> <player> or <punishment id> <annul|info|change|info|remove>").color(NamedTextColor.DARK_RED));
source.sendMessage(plugin.getMessageProvider().provide("command.punishment.usage", source, true).color(NamedTextColor.RED));
return;
}
var punishmentManager = plugin.getPunishmentManager();
Expand All @@ -58,48 +67,42 @@ public void execute(Invocation invocation) {
PunishmentHelper helper = new PunishmentHelper();
helper.getPlayerUuid(1, service, playerResolver, invocation).whenCompleteAsync((uuid, throwable) -> {
if (throwable != null) {
source.sendMessage(INTERNAL_ERROR);
source.sendMessage(plugin.getMessageProvider().internalError());
throwable.printStackTrace();
return;
}
if (uuid == null) {
invocation.source().sendMessage(Component.text("This player is not banned at the moment.").color(NamedTextColor.RED));
source.sendMessage(plugin.getMessageProvider().provide("command.punishment.not-banned", source, true).color(NamedTextColor.RED));
return;
}
List<Punishment> punishments;
try {
punishments = punishmentManager.getPunishments(uuid, service).get(10, TimeUnit.SECONDS);
} catch (InterruptedException | TimeoutException | ExecutionException e) {
e.printStackTrace();
source.sendMessage(Util.INTERNAL_ERROR);
return;
}
source.sendMessage(Component.text("The player has " + punishments.size() + " punishments.").color(NamedTextColor.AQUA));
for (Punishment punishment : punishments) {

Component component = helper.buildPunishmentData(punishment)
.clickEvent(ClickEvent.suggestCommand(punishment.getPunishmentUuid().toString().toLowerCase(Locale.ROOT)))
.hoverEvent((HoverEventSource<Component>) op -> HoverEvent.showText(Component.text("Click to copy punishment id")
.color(NamedTextColor.GREEN)));
source.sendMessage(component);
}
punishmentManager.getPunishments(uuid, service).whenComplete((punishments, t) -> {
if (t != null) {
source.sendMessage(plugin.getMessageProvider().internalError());
t.printStackTrace();
return;
}
source.sendMessage(plugin.getMessageProvider().provide("command.punishment.punishments", source, true, Component.text(punishments.size())).color(NamedTextColor.AQUA));
for (Punishment punishment : punishments) {
Component component = helper.buildPunishmentData(punishment, plugin.getMessageProvider(), source)
.clickEvent(ClickEvent.suggestCommand(punishment.getPunishmentUuid().toString().toLowerCase(Locale.ROOT)))
.hoverEvent((HoverEventSource<Component>) op -> HoverEvent.showText(plugin.getMessageProvider().provide("commands.general.copy")
.color(NamedTextColor.GREEN)));
source.sendMessage(component);
}
});
}, service);
return;
}
UUID uuid;
try {
uuid = Util.parseUuid(arguments[0]);
uuid = Util.parseUuid(arguments[1]);
} catch (IllegalArgumentException e) {
source.sendMessage(Component.text().append(Component.text("Could not parse string '").color(NamedTextColor.RED),
Component.text(arguments[0]).color(NamedTextColor.YELLOW),
Component.text("' as uuid.").color(NamedTextColor.RED)));
source.sendMessage(plugin.getMessageProvider().provide("command.punishment.uuid-parse-error", source, true, Component.text(arguments[0]).color(NamedTextColor.YELLOW)).color(NamedTextColor.RED));
return;
}
String option = arguments[1].toLowerCase(Locale.ROOT);
if (!options.contains(option)) {
source.sendMessage(Component.text()
.append(Component.text("Unknown option: ").color(NamedTextColor.RED),
Component.text(option).color(NamedTextColor.YELLOW)));
String option = arguments[0].toLowerCase(Locale.ROOT);
if (!PUNISHMENT_OPTIONS.contains(option)) {
source.sendMessage(plugin.getMessageProvider().provide("command.punishment.unknown-option", source, true, Component.text(option).color(NamedTextColor.YELLOW)).color(NamedTextColor.RED));
return;
}
punishmentManager.getPunishment(uuid, service).whenCompleteAsync((optional, throwable) -> {
Expand All @@ -110,28 +113,27 @@ public void execute(Invocation invocation) {
}
Punishment punishment;
if (optional.isEmpty()) {
source.sendMessage(Component.text().append(Component.text("Could not find a punishment for id '").color(NamedTextColor.RED),
Component.text(uuid.toString().toLowerCase(Locale.ROOT)).color(NamedTextColor.YELLOW),
Component.text("'.").color(NamedTextColor.RED)));
source.sendMessage(plugin.getMessageProvider().provide("command.punishment.unknown-punishment-id", source, true,
Component.text(uuid.toString().toLowerCase(Locale.ROOT)).color(NamedTextColor.YELLOW)).color(NamedTextColor.RED));
return;
}
punishment = optional.get();
switch (option) {
case "annul", "remove" -> punishment.cancel().whenCompleteAsync((unused, t) -> {
case "cancel", "remove" -> punishment.cancel().whenCompleteAsync((unused, t) -> {
if (t != null) {
t.printStackTrace();
source.sendMessage(Util.INTERNAL_ERROR);
return;
}
source.sendMessage(Component.text("The punishment was successfully annulled.").color(NamedTextColor.GREEN));
source.sendMessage(plugin.getMessageProvider().provide("punishment.remove", source, true).color(NamedTextColor.GREEN));
try {
chatListener.update(uuid);
} catch (ExecutionException | InterruptedException | TimeoutException e) {
source.sendMessage(INTERNAL_ERROR);
e.printStackTrace();
}
});
case "info" -> source.sendMessage(new PunishmentHelper().buildPunishmentData(punishment));
case "info" -> source.sendMessage(new PunishmentHelper().buildPunishmentData(punishment, plugin.getMessageProvider(), source));
case "change" -> source.sendMessage(Component.text("Soon™️"));
}
});
Expand Down Expand Up @@ -165,13 +167,9 @@ public CompletableFuture<List<String>> suggestAsync(Invocation invocation) {
public List<String> suggest(Invocation invocation) {
String[] arguments = invocation.arguments();
if (arguments.length == 0) {
return List.of("playerinfo");
return ALL_OPTIONS;
} else if (arguments.length == 1 && arguments[0].toLowerCase(Locale.ROOT).startsWith("p")) {
return List.of("playerinfo");
} else if (arguments.length == 2) {
return options;
} else if (arguments.length == 3) {
return options.stream().filter(s -> s.toLowerCase().startsWith(arguments[2])).collect(Collectors.toList());
return ALL_OPTIONS.stream().filter(s -> s.toLowerCase(Locale.ROOT).startsWith(arguments[1].toLowerCase(Locale.ROOT))).toList();
}
return new ArrayList<>();
}
Expand Down
Loading

0 comments on commit fb512ae

Please sign in to comment.