Add null annotations & null handling to ComponentUtil

This commit is contained in:
Vankka 2024-06-07 17:16:01 +03:00
parent 064fed3d43
commit 97ea72f0c7
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
2 changed files with 20 additions and 8 deletions

View File

@ -40,7 +40,7 @@ public class MinecraftComponentImpl implements MinecraftComponent {
setJson(json); setJson(json);
} }
public MinecraftComponentImpl(Component component) { public MinecraftComponentImpl(@NotNull Component component) {
setComponent(component); setComponent(component);
} }
@ -48,7 +48,7 @@ public class MinecraftComponentImpl implements MinecraftComponent {
return component; return component;
} }
public void setComponent(Component component) { public void setComponent(@NotNull Component component) {
this.component = component; this.component = component;
this.json = GsonComponentSerializer.gson().serialize(component); this.json = GsonComponentSerializer.gson().serialize(component);
} }

View File

@ -26,6 +26,9 @@ import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; 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; import java.util.Collection;
@ -36,25 +39,34 @@ public final class ComponentUtil {
private static MinecraftComponentAdapter<Component> ADAPTER; private static MinecraftComponentAdapter<Component> ADAPTER;
@NotNull
private static MinecraftComponentAdapter<Component> getAdapter() { private static MinecraftComponentAdapter<Component> getAdapter() {
return ADAPTER != null ? ADAPTER : (ADAPTER = MinecraftComponentAdapter.create(GsonComponentSerializer.class, Component.class)); return ADAPTER != null ? ADAPTER : (ADAPTER = MinecraftComponentAdapter.create(GsonComponentSerializer.class, Component.class));
} }
private ComponentUtil() {} private ComponentUtil() {}
public static boolean isEmpty(Component component) { public static boolean isEmpty(@NotNull Component component) {
return PlainTextComponentSerializer.plainText().serialize(component).isEmpty(); return PlainTextComponentSerializer.plainText().serialize(component).isEmpty();
} }
public static boolean isEmpty(MinecraftComponent component) { public static boolean isEmpty(@NotNull MinecraftComponent component) {
return component.asPlainString().isEmpty(); return component.asPlainString().isEmpty();
} }
@Contract("null -> null")
public static MinecraftComponent toAPI(Component component) { public static MinecraftComponent toAPI(Component component) {
if (component == null) {
return null;
}
return new MinecraftComponentImpl(component); 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) { if (component instanceof MinecraftComponentImpl) {
return ((MinecraftComponentImpl) component).getComponent(); return ((MinecraftComponentImpl) component).getComponent();
} else { } 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(); MinecraftComponentImpl component = MinecraftComponentImpl.empty();
MinecraftComponent.Adapter<Object> adapter = component.unrelocatedAdapter(); MinecraftComponent.Adapter<Object> adapter = component.unrelocatedAdapter();
if (adapter == null) { if (adapter == null) {
@ -80,11 +92,11 @@ public final class ComponentUtil {
return component; return component;
} }
public static Component join(Component delimiter, Collection<? extends ComponentLike> components) { public static Component join(@NotNull Component delimiter, @NotNull Collection<? extends ComponentLike> components) {
return join(delimiter, components.toArray(new ComponentLike[0])); 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(); TextComponent.Builder builder = Component.text();
for (int i = 0; i < components.length; i++) { for (int i = 0; i < components.length; i++) {
builder.append(components[i]); builder.append(components[i]);