-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(serializer-configurate4): Update configurate serializer for data…
… components
- Loading branch information
Showing
5 changed files
with
198 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
.../main/java/net/kyori/adventure/serializer/configurate4/ConfigurateDataComponentValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
52 changes: 52 additions & 0 deletions
52
.../kyori/adventure/serializer/configurate4/ConfigurateDataComponentValueTypeSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...et/kyori/adventure/serializer/configurate4/SnapshottingConfigurateDataComponentValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |