From 8077555bd738e0cbf9c5c8ac8aab4f07137191cc Mon Sep 17 00:00:00 2001 From: Vankka Date: Fri, 26 Jul 2024 23:39:55 +0300 Subject: [PATCH] Make MinecraftComponent immutable --- .../component/MinecraftComponentAdapter.java | 5 ++- .../MinecraftComponentAdapterUnrelocated.java | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 api/src/main/java/com/discordsrv/api/component/MinecraftComponentAdapterUnrelocated.java diff --git a/api/src/main/java/com/discordsrv/api/component/MinecraftComponentAdapter.java b/api/src/main/java/com/discordsrv/api/component/MinecraftComponentAdapter.java index 0021a24f..97f2c215 100644 --- a/api/src/main/java/com/discordsrv/api/component/MinecraftComponentAdapter.java +++ b/api/src/main/java/com/discordsrv/api/component/MinecraftComponentAdapter.java @@ -28,8 +28,9 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** - * A helper class to make {@link MinecraftComponent}s using Adventure. - * @param the Adventure Component type, relocated or unrelocated + * A persistent Adventure adapter for {@link MinecraftComponent}s, this is more efficient than using {@link MinecraftComponent#asAdventure(Class)}. + * @see MinecraftComponent#asAdventure(MinecraftComponentAdapter) + * @param the Adventure Component type, unrelocated or relocated */ public interface MinecraftComponentAdapter { diff --git a/api/src/main/java/com/discordsrv/api/component/MinecraftComponentAdapterUnrelocated.java b/api/src/main/java/com/discordsrv/api/component/MinecraftComponentAdapterUnrelocated.java new file mode 100644 index 00000000..d1a373e5 --- /dev/null +++ b/api/src/main/java/com/discordsrv/api/component/MinecraftComponentAdapterUnrelocated.java @@ -0,0 +1,38 @@ +/* + * This file is part of the DiscordSRV API, licensed under the MIT License + * Copyright (c) 2016-2024 Austin "Scarsz" Shapiro, Henri "Vankka" Schubin and DiscordSRV contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.discordsrv.api.component; + +class MinecraftComponentAdapterUnrelocated { + + public static final MinecraftComponentAdapter INSTANCE = make(); + + private static MinecraftComponentAdapter make() { + try { + return MinecraftComponentAdapter.create( + Class.forName("net.ky".concat("ori.adventure.text.serializer.gson.GsonComponentSerializer")) + ); + } catch (ClassNotFoundException ignored) {} + return null; + } +}