From 287cc308d62bd2e78a1246de4648583a165bd6b0 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 19 Feb 2019 14:17:33 +0000 Subject: [PATCH] Fix parsing log entry types (#1438) --- .../java/me/lucko/luckperms/api/LogEntry.java | 29 +++++++++++++++++++ .../actionlog/LogEntryJsonSerializer.java | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/me/lucko/luckperms/api/LogEntry.java b/api/src/main/java/me/lucko/luckperms/api/LogEntry.java index 093b4913..1502aa75 100644 --- a/api/src/main/java/me/lucko/luckperms/api/LogEntry.java +++ b/api/src/main/java/me/lucko/luckperms/api/LogEntry.java @@ -102,6 +102,35 @@ public interface LogEntry extends Comparable { enum Type { USER('U'), GROUP('G'), TRACK('T'); + /** + * Parses a {@link Type} from a string. + * + * @param type the string + * @return a type + * @throws IllegalArgumentException if a type could not be parsed + * @since 4.4 + */ + public static @NonNull Type parse(String type) { + try { + return valueOf(type); + } catch (IllegalArgumentException e) { + // ignore + } + try { + return valueOf(type.charAt(0)); + } catch (IllegalArgumentException e) { + // ignore + } + throw new IllegalArgumentException("Unknown type: " + type); + } + + /** + * Returns a {@link Type} by its code. + * + * @param code the code - see {@link Type#getCode()}. + * @return a type + * @throws IllegalArgumentException if a type could not be resolved + */ public static @NonNull Type valueOf(char code) { switch (code) { case 'U': diff --git a/common/src/main/java/me/lucko/luckperms/common/actionlog/LogEntryJsonSerializer.java b/common/src/main/java/me/lucko/luckperms/common/actionlog/LogEntryJsonSerializer.java index 38fb146f..cc9949ff 100644 --- a/common/src/main/java/me/lucko/luckperms/common/actionlog/LogEntryJsonSerializer.java +++ b/common/src/main/java/me/lucko/luckperms/common/actionlog/LogEntryJsonSerializer.java @@ -58,7 +58,7 @@ public final class LogEntryJsonSerializer { builder.actor(UUID.fromString(data.get("actor").getAsString())); builder.actorName(data.get("actorName").getAsString()); - builder.type(LogEntry.Type.valueOf(data.get("type").getAsString())); + builder.type(LogEntry.Type.parse(data.get("type").getAsString())); if (data.has("acted")) { builder.actor(UUID.fromString(data.get("acted").getAsString())); }