Skip to content

Commit

Permalink
shadow color: serializer for configurate
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Dec 8, 2024
1 parent 1896d88 commit 5fae082
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.kyori.adventure.text.TranslationArgument;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.ShadowColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.ComponentSerializer;
Expand Down Expand Up @@ -108,6 +109,7 @@ private ConfigurateComponentSerializerImpl(final @NotNull Builder builder) {
.registerExact(HoverEvent.ShowEntity.class, HoverEventShowEntitySerializer.INSTANCE)
.registerExact(HoverEvent.ShowItem.class, HoverEventShowItemSerializer.INSTANCE)
.register(ConfigurateDataComponentValue.class, ConfigurateDataComponentValueTypeSerializer.INSTANCE)
.register(ShadowColor.class, ShadowColorSerializer.INSTACE)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2024 KyoriPowered
*
* 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 net.kyori.adventure.serializer.configurate4;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import net.kyori.adventure.text.format.ShadowColor;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;

final class ShadowColorSerializer implements TypeSerializer<ShadowColor> {
static final TypeSerializer<ShadowColor> INSTACE = new ShadowColorSerializer(false);

private boolean emitFloats;

private ShadowColorSerializer(final boolean emitFloats) {
this.emitFloats = emitFloats;
}

@Override
public ShadowColor deserialize(final Type type, final ConfigurationNode node) throws SerializationException {
if (node.isList()) {
final List<Float> floats = node.getList(Float.class);
if (floats.size() != 4) {
throw new SerializationException(node, ShadowColor.class, "Expected a 4-element float array ([R, G, B, A]), but got a " + floats.size() + "-long array instead.");
}
final int r = componentFromFloat(floats.get(0));
final int g = componentFromFloat(floats.get(1));
final int b = componentFromFloat(floats.get(2));
final int a = componentFromFloat(floats.get(3));

return ShadowColor.shadowColor(r, g, b, a);
} else {
return ShadowColor.shadowColor(node.getInt());
}
}

@Override
public void serialize(final Type type, final @Nullable ShadowColor obj, final ConfigurationNode node) throws SerializationException {
if (obj == null) {
node.raw(null);
return;
}

if (this.emitFloats) {
node.set(Collections.emptyList());
node.appendListNode().set(componentAsFloat(obj.red()));
node.appendListNode().set(componentAsFloat(obj.green()));
node.appendListNode().set(componentAsFloat(obj.blue()));
node.appendListNode().set(componentAsFloat(obj.alpha()));
} else {
node.set(obj.value());
}
}

static float componentAsFloat(final int element) {
return (float) element / 0xff;
}

static int componentFromFloat(final double element) {
return (int) (((float) element) * 0xff);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.ShadowColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
Expand All @@ -46,6 +47,7 @@ final class StyleSerializer implements TypeSerializer<Style> {

static final String FONT = "font";
static final String COLOR = "color";
static final String SHADOW_COLOR = "shadow_color";
static final String INSERTION = "insertion";
static final String CLICK_EVENT = "clickEvent";
static final String CLICK_EVENT_ACTION = "action";
Expand Down Expand Up @@ -75,6 +77,10 @@ private StyleSerializer() {
if (color != null) {
builder.color(color);
}
final @Nullable ShadowColor shadowColor = value.node(SHADOW_COLOR).get(ShadowColor.class);
if (shadowColor != null) {
builder.shadowColor(shadowColor);
}

for (final TextDecoration decoration : DECORATIONS) {
final TextDecoration.State state = value.node(nonNull(TextDecoration.NAMES.key(decoration), "decoration")).get(TextDecoration.State.class);
Expand Down Expand Up @@ -132,6 +138,7 @@ public void serialize(final @NotNull Type type, @Nullable Style obj, final @NotN
}
value.node(FONT).set(Key.class, obj.font());
value.node(COLOR).set(TextColor.class, obj.color());
value.node(SHADOW_COLOR).set(ShadowColor.class, obj.shadowColor());
for (final TextDecoration decoration : DECORATIONS) {
final ConfigurationNode decorationNode = value.node(nonNull(TextDecoration.NAMES.key(decoration), "decoration"));
final TextDecoration.State state = obj.decoration(decoration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.ShadowColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -84,6 +85,16 @@ void testSerializeNamedColor() {
this.assertRoundtrippable(Style.class, style, node);
}

@Test
void testSerializeShadowColor() {
final ConfigurationNode node = this.node(n -> {
n.node(StyleSerializer.SHADOW_COLOR).raw(0xCCFF0022);
});
final Style style = Style.style(ShadowColor.shadowColor(0xFF, 0x00, 0x22, 0xCC));

this.assertRoundtrippable(Style.class, style, node);
}

@Test
void testSerializeInsertion() {
final ConfigurationNode node = this.node(n -> {
Expand Down

0 comments on commit 5fae082

Please sign in to comment.