From 97ea72f0c711151f3560dd607f3c517839d9aabb Mon Sep 17 00:00:00 2001 From: Vankka Date: Fri, 7 Jun 2024 17:16:01 +0300 Subject: [PATCH] Add null annotations & null handling to ComponentUtil --- .../component/MinecraftComponentImpl.java | 4 ++-- .../common/component/util/ComponentUtil.java | 24 ++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/com/discordsrv/common/component/MinecraftComponentImpl.java b/common/src/main/java/com/discordsrv/common/component/MinecraftComponentImpl.java index 9943a26e..88dac7e6 100644 --- a/common/src/main/java/com/discordsrv/common/component/MinecraftComponentImpl.java +++ b/common/src/main/java/com/discordsrv/common/component/MinecraftComponentImpl.java @@ -40,7 +40,7 @@ public class MinecraftComponentImpl implements MinecraftComponent { setJson(json); } - public MinecraftComponentImpl(Component component) { + public MinecraftComponentImpl(@NotNull Component component) { setComponent(component); } @@ -48,7 +48,7 @@ public class MinecraftComponentImpl implements MinecraftComponent { return component; } - public void setComponent(Component component) { + public void setComponent(@NotNull Component component) { this.component = component; this.json = GsonComponentSerializer.gson().serialize(component); } diff --git a/common/src/main/java/com/discordsrv/common/component/util/ComponentUtil.java b/common/src/main/java/com/discordsrv/common/component/util/ComponentUtil.java index 2afcfb2f..f94e24f3 100644 --- a/common/src/main/java/com/discordsrv/common/component/util/ComponentUtil.java +++ b/common/src/main/java/com/discordsrv/common/component/util/ComponentUtil.java @@ -26,6 +26,9 @@ import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Collection; @@ -36,25 +39,34 @@ public final class ComponentUtil { private static MinecraftComponentAdapter ADAPTER; + @NotNull private static MinecraftComponentAdapter getAdapter() { return ADAPTER != null ? ADAPTER : (ADAPTER = MinecraftComponentAdapter.create(GsonComponentSerializer.class, Component.class)); } private ComponentUtil() {} - public static boolean isEmpty(Component component) { + public static boolean isEmpty(@NotNull Component component) { return PlainTextComponentSerializer.plainText().serialize(component).isEmpty(); } - public static boolean isEmpty(MinecraftComponent component) { + public static boolean isEmpty(@NotNull MinecraftComponent component) { return component.asPlainString().isEmpty(); } + @Contract("null -> null") public static MinecraftComponent toAPI(Component component) { + if (component == null) { + return null; + } return new MinecraftComponentImpl(component); } - public static Component fromAPI(MinecraftComponent component) { + @Contract("null -> null") + public static Component fromAPI(@Nullable MinecraftComponent component) { + if (component == null) { + return null; + } if (component instanceof MinecraftComponentImpl) { return ((MinecraftComponentImpl) component).getComponent(); } else { @@ -70,7 +82,7 @@ public final class ComponentUtil { } } - public static MinecraftComponent fromUnrelocated(Object unrelocatedAdventure) { + public static MinecraftComponent fromUnrelocated(@NotNull Object unrelocatedAdventure) { MinecraftComponentImpl component = MinecraftComponentImpl.empty(); MinecraftComponent.Adapter adapter = component.unrelocatedAdapter(); if (adapter == null) { @@ -80,11 +92,11 @@ public final class ComponentUtil { return component; } - public static Component join(Component delimiter, Collection components) { + public static Component join(@NotNull Component delimiter, @NotNull Collection components) { return join(delimiter, components.toArray(new ComponentLike[0])); } - public static Component join(Component delimiter, ComponentLike[] components) { + public static Component join(@NotNull Component delimiter, @NotNull ComponentLike[] components) { TextComponent.Builder builder = Component.text(); for (int i = 0; i < components.length; i++) { builder.append(components[i]);