Skip to content

Commit

Permalink
Fix data consistency and enhance QOL
Browse files Browse the repository at this point in the history
Active punishments are now always available
Added some missing translations and enhance messages
  • Loading branch information
JvstvsHD committed Jul 29, 2024
1 parent 194fd93 commit 78a08b6
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public interface PunishmentType {

String getName();

/**
* Gets the ID of the punishment type. This ID is unique for each punishment type.
* @since 1.2.0
* @return the ID of the punishment type.
*/
int getId();

/**
* Determines whether the punishment is a mute or not.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static StandardPunishmentType getById(int id) {
return BY_ID.get(id);
}

@Override
public int getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -89,12 +90,17 @@ public final void registerCommands(CommandManager<NecrifyUser> manager, boolean
manager.exceptionController()
.registerHandler(ArgumentParseException.class, ExceptionHandler.unwrappingHandler(UserNotFoundParseException.class))
.registerHandler(UserNotFoundParseException.class, context -> {
context.context().sender().sendMessage("commands.general.not-found", Component.text(context.exception().playerName()).color(NamedTextColor.YELLOW));
var component = getMessageProvider()
.provide("commands.general.not-found", Component.text(context.exception().playerName(), NamedTextColor.YELLOW))
.color(NamedTextColor.RED);
context.context().sender().sendMessage(component);
});
manager.exceptionController()
.registerHandler(ArgumentParseException.class, ExceptionHandler.unwrappingHandler(PunishmentParser.PunishmentParseException.class))
.registerHandler(PunishmentParser.PunishmentParseException.class, context -> {
context.context().sender().sendMessage(context.exception().getMessage());
var replacements = Arrays.stream(context.exception().getReplacements()).map(s -> Component.text(s, NamedTextColor.YELLOW)).toArray(Component[]::new);
var component = getMessageProvider().provide(context.exception().getMessage(), replacements).color(NamedTextColor.RED);
context.context().sender().sendMessage(component);
});
/*manager.exceptionController()//.registerHandler(ArgumentParseException.class, ExceptionHandler.unwrappingHandler(ArgumentParseException.class))
.registerHandler(ArgumentParseException.class, context -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
package de.jvstvshd.necrify.common.commands;

import de.jvstvshd.necrify.api.duration.PunishmentDuration;
import de.jvstvshd.necrify.api.message.MessageProvider;
import de.jvstvshd.necrify.api.punishment.Punishment;
import de.jvstvshd.necrify.api.punishment.StandardPunishmentType;
import de.jvstvshd.necrify.api.user.NecrifyUser;
import de.jvstvshd.necrify.api.user.UserDeletionReason;
import de.jvstvshd.necrify.api.user.UserManager;
import de.jvstvshd.necrify.common.AbstractNecrifyPlugin;
import de.jvstvshd.necrify.common.util.PunishmentHelper;
import de.jvstvshd.necrify.common.util.Util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
Expand All @@ -56,6 +58,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

Expand All @@ -66,6 +69,7 @@ public class NecrifyCommand {
private final UserManager userManager;
private final Logger logger;
private final ExecutorService executor;
private final MessageProvider provider;

private static final List<String> PUNISHMENT_COMMAND_OPTIONS = List.of("cancel", "remove", "info", "change");
private static final List<String> USER_COMMAND_OPTIONS = List.of("info", "delete");
Expand All @@ -76,6 +80,7 @@ public NecrifyCommand(AbstractNecrifyPlugin plugin) {
this.miniMessage = MiniMessage.miniMessage();
this.logger = plugin.getLogger();
this.executor = plugin.getExecutor();
this.provider = plugin.getMessageProvider();
}

//COMMANDS
Expand Down Expand Up @@ -215,21 +220,22 @@ public void unmuteCommand(
@Argument(value = "target", description = "Player to unmute", suggestions = "suggestOnlinePlayers") NecrifyUser target
) {
var punishments = target.getPunishments(StandardPunishmentType.TEMPORARY_MUTE, StandardPunishmentType.PERMANENT_MUTE);
System.out.println(punishments);
removePunishments(sender, "unmute", punishments);
}

@Command("necrify punishment <punishment> [option]")
@Command("necrify punishment <punishmentId> [option]")
@Permission(value = {"necrify.command.punishment", "necrify.admin"}, mode = Permission.Mode.ANY_OF)
public void punishmentCommand(
NecrifyUser sender,
@Argument(value = "punishment", description = "Punishment to manage") Punishment punishment,
@Argument(value = "option", description = "Option to manage the punishment", suggestions = "suggestPunishmentCommandOptions") @Default("info") String option
@Argument(value = "punishmentId", description = "Punishment to manage") Punishment punishmentParsed,
@Argument(value = "option", description = "Option to manage the punishment", suggestions = "suggestPunishmentCommandOptions") @Default(value = "info") String option
) {
switch (option) {
case "info" ->
sender.sendMessage(buildComponent(PunishmentHelper.buildPunishmentData(punishment, plugin.getMessageProvider()), punishment));
sender.sendMessage(buildComponent(PunishmentHelper.buildPunishmentData(punishmentParsed, plugin.getMessageProvider()), punishmentParsed));
case "cancel", "remove" -> {
punishment.cancel().whenCompleteAsync((unused, th) -> {
punishmentParsed.cancel().whenCompleteAsync((unused, th) -> {
if (th != null) {
logException(sender, th);
return;
Expand All @@ -253,11 +259,12 @@ public void userCommand(
switch (option) {
case "info" -> {
var punishments = target.getPunishments();
sender.sendMessage(provider.provide("command.user.overview",
Util.copyComponent(Objects.requireNonNullElse(target.getUsername(), "null"), provider).color(NamedTextColor.YELLOW),
Util.copyComponent(target.getUuid().toString(), provider).color(NamedTextColor.YELLOW),
Component.text(punishments.size())));
for (Punishment punishment : punishments) {
Component component = PunishmentHelper.buildPunishmentData(punishment, plugin.getMessageProvider())
.clickEvent(ClickEvent.suggestCommand(punishment.getPunishmentUuid().toString().toLowerCase(Locale.ROOT)))
.hoverEvent((HoverEventSource<Component>) op -> HoverEvent.showText(plugin.getMessageProvider().provide("commands.general.copy")
.color(NamedTextColor.GREEN)));
Component component = buildComponent(PunishmentHelper.buildPunishmentData(punishment, plugin.getMessageProvider()), punishment);
sender.sendMessage(component);
}
}
Expand Down Expand Up @@ -344,10 +351,12 @@ private void removePunishments(NecrifyUser source, String commandName, List<Puni
//Communication, messaging, logging

private Component buildComponent(Component dataComponent, Punishment punishment) {
return dataComponent.clickEvent(ClickEvent.runCommand("/punishment " + punishment.getPunishmentUuid()
.toString().toLowerCase(Locale.ROOT) + " remove"))
.hoverEvent((HoverEventSource<Component>) op -> HoverEvent.showText(Component
.text("Click to remove punishment").color(NamedTextColor.GREEN)));
var clickToRemove = provider.provide("command.punishment.click-to-remove");
return dataComponent.append(
clickToRemove
.color(NamedTextColor.RED)
.clickEvent(ClickEvent.runCommand("/necrify punishment " + punishment.getPunishmentUuid().toString().toLowerCase(Locale.ROOT) + " remove"))
.hoverEvent((HoverEventSource<Component>) op -> HoverEvent.showText(clickToRemove.color(NamedTextColor.GREEN))));
}

private Component reasonOrDefaultTo(String reason, StandardPunishmentType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ public PunishmentParser(Necrify necrify) {
var uuidString = commandInput.peekString();
var uuid = Util.fromString(uuidString);
if (uuid.isEmpty()) {
return CompletableFuture.completedFuture(ArgumentParseResult.failure(new PunishmentParseException("command.parse.uuid.invalid", uuidString)));
return CompletableFuture.completedFuture(ArgumentParseResult.failure(new PunishmentParseException("command.punishment.uuid-parse-error", uuidString)));
}
return necrify.getPunishment(uuid.get()).handle((punishment, throwable) -> {
if (throwable != null) {
return ArgumentParseResult.failure(new PunishmentParseException("error.internal"));
}
return punishment.map(ArgumentParseResult::success)
.orElseGet(() -> ArgumentParseResult.failure(new PunishmentParseException("command.parse.punishment.notfound", uuidString)));
if (punishment.isPresent()) {
commandInput.readString();
return ArgumentParseResult.success(punishment.get());
} else {
return ArgumentParseResult.failure(new PunishmentParseException("command.punishment.unknown-punishment-id", uuidString));
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public String toString() {
return "AbstractPunishment{" +
"reason=" + reason +
", service=" + service +
", user=" + user +
", userUuid=" + user.getUuid() +
", userName=" + user.getUsername() +
", punishmentUuid=" + punishmentUuid +
", messageProvider=" + messageProvider +
", validity=" + validity +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import de.jvstvshd.necrify.api.punishment.TemporalPunishment;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;

public class PunishmentHelper {

Expand All @@ -39,10 +40,13 @@ private PunishmentHelper() {
public static Component buildPunishmentData(Punishment punishment, MessageProvider provider) {
return Component.text()
.append(provider.provide("helper.type").color(NamedTextColor.AQUA),
Component.text(punishment.getType().getName()).color(NamedTextColor.YELLOW),
copyable("%s (%d)".formatted(punishment.getType().getName(), punishment.getType().getId()), NamedTextColor.YELLOW, provider),
Component.newline(),
provider.provide("helper.reason").color(NamedTextColor.AQUA),
punishment.getReason(),
Util.copyComponent(punishment.getReason(), PlainTextComponentSerializer.plainText().serialize(punishment.getReason()), provider),
Component.newline(),
provider.prefixed(Component.text("ID: ")).color(NamedTextColor.AQUA),
copyable(punishment.getPunishmentUuid().toString(), NamedTextColor.YELLOW, provider),
Component.newline(),
punishment instanceof TemporalPunishment temporalPunishment ?
buildPunishmentDataTemporal(temporalPunishment, provider) : Component.text(""),
Expand All @@ -52,12 +56,22 @@ public static Component buildPunishmentData(Punishment punishment, MessageProvid
}

public static Component buildPunishmentDataTemporal(TemporalPunishment punishment, MessageProvider provider) {
return punishment.isPermanent() ? Component.text("permanent").color(NamedTextColor.RED) : Component.text()
if (punishment.isPermanent()) {
return Component.text()
.append(provider.provide("helper.temporal.duration").color(NamedTextColor.AQUA),
Component.text("PERMANENT").color(NamedTextColor.RED))
.build();
}
return Component.text()
.append(provider.provide("helper.temporal.duration").color(NamedTextColor.AQUA),
Component.text(punishment.getDuration().remainingDuration()).color(NamedTextColor.YELLOW),
Component.newline(),
provider.provide("helper.temporal.end").color(NamedTextColor.AQUA),
Component.text(punishment.getDuration().expirationAsString()).color(NamedTextColor.YELLOW))
.build();
}

private static Component copyable(String s, NamedTextColor color, MessageProvider provider) {
return Util.copyComponent(s, provider).color(color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.event.HoverEventSource;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;

import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -64,6 +65,11 @@ public static TextComponent copyComponent(String text, MessageProvider provider)
.hoverEvent((HoverEventSource<Component>) op -> HoverEvent.showText(provider.provide("commands.general.copy").color(NamedTextColor.GREEN)));
}

public static Component copyComponent(Component base, String copy, MessageProvider provider) {
return base.clickEvent(ClickEvent.suggestCommand(copy))
.hoverEvent((HoverEventSource<Component>) op -> HoverEvent.showText(provider.provide("commands.general.copy").color(NamedTextColor.GREEN)));
}

public static Optional<UUID> fromString(String uuidString) {
UUID uuid;
try {
Expand Down
6 changes: 4 additions & 2 deletions necrify-common/src/main/resources/translations/de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ command.punishment.usage=Bitte benutze /punishment <playerinfo> <Spieler> oder <
command.punishment.not-banned=Dieser Spieler ist derzeit nicht gebannt.
command.punishment.not-muted=Dieser Spieler ist derzeit nicht gemutet.
command.punishment.cancel.success=Die Strafe wurde erfolgreich abgebrochen.
command.punishment.click-to-remove=Zum Entfernen klicken
command.punishment.punishments=Dieser Spieler hat derzeit {0} laufende Bestrafungen.
command.punishment.uuid-parse-error='{0}' ist keine valide UUID.
command.punishment.uuid-parse-error={0} ist keine valide UUID.
command.punishment.unknown-option=Unbekannte Option: {0}
command.punishment.unknown-punishment-id=Es konnte keine Strafe für die ID '{0}' gefunden werden.
command.punishment.unknown-punishment-id=Es konnte keine Strafe für die ID {0} gefunden werden.
command.tempban.usage=Bitte benutze /tempban <Spieler> <Dauer> [Grund].
command.tempban.success=Du hast den Spieler {0}/{1} für {2} bis {3} gebannt.
command.tempmute.usage=Bitte benutze /tempmute <Spieler> <Dauer> [Grund].
Expand All @@ -29,6 +30,7 @@ command.unmute.usage=Bitte benutze /unban <Spieler>.
command.unmute.success=Der Spieler ist nun nicht mehr gemutet.
command.unmute.multiple-mutes=Dieser Spieler wurde mehrfach gemutet.
command.user.delete.success=Der User wurde inklusive all seiner Strafen erfolgreich gelöscht.
command.user.overview=Der User {0} mit der UUID {1} hat {2} Strafen.
command.whitelist.usage=Bitte nutze /whitelist <Spieler> [add|remove]
command.whitelist.status=Der Whitelist-Status des Spielers {0} ist folgender: {1}.
command.whitelist.success=Der Eintrag wurde erfolgreich aktualisiert.
Expand Down
7 changes: 5 additions & 2 deletions necrify-common/src/main/resources/translations/en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ command.punishment.usage=Please use /punishment <playerinfo> <player> or <cancel
command.punishment.not-banned=This player is not banned at the moment.
command.punishment.not-muted=This player is not muted at the moment.
command.punishment.cancel.success=The punishment has been successfully cancelled.
command.punishment.click-to-remove=Click to remove
command.punishment.punishments=This player has {0} punishments.
command.punishment.uuid-parse-error=Could not parse string '{0}' as uuid.
command.punishment.uuid-parse-error=Could not parse string {0} as uuid.
command.punishment.unknown-option=Unknown option: {0}
command.punishment.unknown-punishment-id=Could not find a punishment for id '{0}'.
command.punishment.unknown-punishment-id=Could not find a punishment for id {0}.
command.tempban.usage=Please use /tempban <player> <duration> [reason].
command.tempban.success=You have banned the player {0}/{1} for {2} until {3}.
command.tempmute.usage=Please use /tempmute <player> <duration> [reason].
Expand All @@ -27,11 +28,13 @@ command.unban.multiple-bans=This player has been banned multiple times.
command.unmute.usage=Please use /unban <player>.
command.unmute.success=The player is no longer muted.
command.unmute.multiple-mutes=This player has been muted multiple times.
command.user.overview=User {0} with UUID {1} has {2} punishments.
command.whitelist.usage=Please use /whitelist <player> [add|remove]
command.whitelist.status={0}'s whitelist status: {1}.
command.whitelist.success=Updated successfully.
command.whitelist.enabled=The whitelist is now enabled.
command.whitelist.disabled=The whitelist is now disabled.
command.user.delete.success=The user has been successfully deleted including all of his punishments.
error.internal=<red>An internal error occurred. Please contact the network administration.</red>
helper.type=Type:
helper.reason=reason:
Expand Down
Loading

0 comments on commit 78a08b6

Please sign in to comment.