2021-10-20 22:42:02 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2021-10-20 22:42:02 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.text;
|
2021-10-20 22:42:02 +00:00
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
2023-12-05 23:54:42 +00:00
|
|
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
2021-10-20 22:42:02 +00:00
|
|
|
|
|
|
|
import java.util.function.UnaryOperator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A wrapper around a normal GsonComponentSerializer to accept null components.
|
|
|
|
*/
|
2021-11-20 23:29:46 +00:00
|
|
|
public record GsonComponentSerializerWrapper(GsonComponentSerializer source) implements GsonComponentSerializer {
|
2021-10-20 22:42:02 +00:00
|
|
|
|
|
|
|
@Override
|
2023-12-05 23:54:42 +00:00
|
|
|
public @NonNull Gson serializer() {
|
2021-10-20 22:42:02 +00:00
|
|
|
return this.source.serializer();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-12-05 23:54:42 +00:00
|
|
|
public @NonNull UnaryOperator<GsonBuilder> populator() {
|
2021-10-20 22:42:02 +00:00
|
|
|
return this.source.populator();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-12-05 23:54:42 +00:00
|
|
|
public @NonNull Component deserializeFromTree(@NonNull JsonElement input) {
|
2021-10-20 22:42:02 +00:00
|
|
|
// This has yet to be an issue, so it won't be overridden unless we have to
|
|
|
|
return this.source.deserializeFromTree(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-12-05 23:54:42 +00:00
|
|
|
public @NonNull JsonElement serializeToTree(@NonNull Component component) {
|
2021-10-20 22:42:02 +00:00
|
|
|
return this.source.serializeToTree(component);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-12-05 23:54:42 +00:00
|
|
|
public @NonNull Component deserialize(@NonNull String input) {
|
2021-10-20 22:42:02 +00:00
|
|
|
// See https://github.com/KyoriPowered/adventure/issues/447
|
2021-12-29 16:29:48 +00:00
|
|
|
Component component = this.serializer().fromJson(input, Component.class);
|
|
|
|
if (component == null) {
|
|
|
|
return Component.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return component;
|
2021-10-20 22:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-12-05 23:54:42 +00:00
|
|
|
public @NonNull String serialize(@NonNull Component component) {
|
2021-10-20 22:42:02 +00:00
|
|
|
return this.source.serialize(component);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-12-05 23:54:42 +00:00
|
|
|
public @NonNull Builder toBuilder() {
|
2021-10-20 22:42:02 +00:00
|
|
|
return this.source.toBuilder();
|
|
|
|
}
|
|
|
|
}
|