Skip to content

Commit

Permalink
feat(serializer-configurate4): Update configurate serializer for data…
Browse files Browse the repository at this point in the history
… components
  • Loading branch information
zml2008 committed Apr 24, 2024
1 parent 8cf0109 commit 0158d40
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private ConfigurateComponentSerializerImpl(final @NotNull Builder builder) {
.registerExact(new IndexSerializer<>(TypeToken.get(TextDecoration.class), TextDecoration.NAMES))
.registerExact(HoverEvent.ShowEntity.class, HoverEventShowEntitySerializer.INSTANCE)
.registerExact(HoverEvent.ShowItem.class, HoverEventShowItemSerializer.INSTANCE)
.register(ConfigurateDataComponentValue.class, ConfigurateDataComponentValueTypeSerializer.INSTANCE)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 net.kyori.adventure.text.event.DataComponentValue;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.configurate.ConfigurationNode;

/**
* A data component value that can integrate with configuration nodes.
*
* @since 4.17.0
*/
public interface ConfigurateDataComponentValue extends DataComponentValue {
/**
* Create a data component value capturing the value of an existing node.
*
* @param existing the existing node
* @return the captured value
* @since 4.17.0
*/
static @NotNull ConfigurateDataComponentValue capturingDataComponentValue(final @NotNull ConfigurationNode existing) {
return SnapshottingConfigurateDataComponentValue.create(existing);
}

/**
* Apply the contained value to the supplied node.
*
* @param node the node to apply this value to
* @since 4.17.0
*/
void applyTo(final @NotNull ConfigurationNode node);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 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 ConfigurateDataComponentValueTypeSerializer implements TypeSerializer<ConfigurateDataComponentValue> {
static final TypeSerializer<ConfigurateDataComponentValue> INSTANCE = new ConfigurateDataComponentValueTypeSerializer();

private ConfigurateDataComponentValueTypeSerializer() {
}

@Override
public ConfigurateDataComponentValue deserialize(final Type type, final ConfigurationNode node) throws SerializationException {
return ConfigurateDataComponentValue.capturingDataComponentValue(node);
}

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

obj.applyTo(node);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
*/
package net.kyori.adventure.serializer.configurate4;

import io.leangen.geantyref.TypeToken;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.api.BinaryTagHolder;
import net.kyori.adventure.text.event.HoverEvent;
Expand All @@ -36,9 +39,13 @@
final class HoverEventShowItemSerializer implements TypeSerializer<HoverEvent.ShowItem> {
static final HoverEventShowItemSerializer INSTANCE = new HoverEventShowItemSerializer();

private static final TypeToken<Map<Key, ConfigurateDataComponentValue>> COMPONENT_MAP_TYPE = new TypeToken<Map<Key, ConfigurateDataComponentValue>>() {
};

static final String ID = "id";
static final String COUNT = "count";
static final String TAG = "tag";
static final String COMPONENTS = "components";

private HoverEventShowItemSerializer() {
}
Expand All @@ -50,9 +57,16 @@ public HoverEvent.ShowItem deserialize(final @NotNull Type type, final @NotNull
throw new SerializationException("An id is required to deserialize the show_item hover event");
}
final int count = value.node(COUNT).getInt(1);
final String tag = value.node(TAG).getString();
final ConfigurationNode components = value.node(COMPONENTS);
if (!components.virtual()) {
final Map<Key, ConfigurateDataComponentValue> componentsMap = components.require(COMPONENT_MAP_TYPE);

return HoverEvent.ShowItem.showItem(id, count, tag == null ? null : BinaryTagHolder.binaryTagHolder(tag));
return HoverEvent.ShowItem.showItem(id, count, new HashMap<>(componentsMap));
} else {
// legacy (pre-1.20.5)
final String tag = value.node(TAG).getString();
return HoverEvent.ShowItem.showItem(id, count, tag == null ? null : BinaryTagHolder.binaryTagHolder(tag));
}
}

@Override
Expand All @@ -65,10 +79,15 @@ public void serialize(final @NotNull Type type, final HoverEvent.@Nullable ShowI
value.node(ID).set(Key.class, obj.item());
value.node(COUNT).set(obj.count());

if (obj.nbt() == null) {
if (!obj.dataComponents().isEmpty()) {
value.node(TAG).set(null);
} else {
value.node(COMPONENTS).set(COMPONENT_MAP_TYPE, obj.dataComponentsAs(ConfigurateDataComponentValue.class));
} else if (obj.nbt() != null) {
// legacy (pre-1.20.5)
value.node(COMPONENTS).set(null);
value.node(TAG).set(obj.nbt().string());
} else {
value.node(COMPONENTS).set(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.util.stream.Stream;
import net.kyori.examination.ExaminableProperty;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.configurate.AttributedConfigurationNode;
import org.spongepowered.configurate.BasicConfigurationNode;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.ConfigurationNode;

final class SnapshottingConfigurateDataComponentValue implements ConfigurateDataComponentValue {
private final ConfigurationNode ownedNode;

// capture the value of an existing node without exposing any mutable state
static @NotNull SnapshottingConfigurateDataComponentValue create(final ConfigurationNode existing) {
final ConfigurationNode owned;
if (existing instanceof AttributedConfigurationNode) {
owned = AttributedConfigurationNode.root(((AttributedConfigurationNode) existing).tagName(), existing.options());
} else if (existing instanceof CommentedConfigurationNode) {
owned = CommentedConfigurationNode.root(existing.options());
} else {
owned = BasicConfigurationNode.root(existing.options());
}

owned.from(existing);

return new SnapshottingConfigurateDataComponentValue(owned);
}

private SnapshottingConfigurateDataComponentValue(final ConfigurationNode owned) {
this.ownedNode = owned;
}

@Override
public void applyTo(final @NotNull ConfigurationNode node) {
node.from(this.ownedNode);
}

@Override
public @NotNull Stream<? extends ExaminableProperty> examinableProperties() {
return Stream.of(
ExaminableProperty.of("ownedNode", this.ownedNode)
);
}
}

0 comments on commit 0158d40

Please sign in to comment.