Skip to content

Commit

Permalink
Merge pull request #1066 from KyoriPowered/mc/1.20.5/data-components
Browse files Browse the repository at this point in the history
feat(api): item data components
  • Loading branch information
zml2008 committed Apr 24, 2024
2 parents 1677bbc + 52e6768 commit 1849c9b
Show file tree
Hide file tree
Showing 32 changed files with 1,691 additions and 126 deletions.
2 changes: 1 addition & 1 deletion annotation-processors/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

dependencies {
annotationProcessor(libs.autoService.processor)
annotationProcessor(libs.autoService)
compileOnlyApi(libs.autoService.annotations)
api(libs.jetbrainsAnnotations)
}
Expand Down
2 changes: 2 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dependencies {
compileOnlyApi(libs.jetbrainsAnnotations)
testImplementation(libs.guava)
annotationProcessor(projects.adventureAnnotationProcessors)
testCompileOnly(libs.autoService.annotations)
testAnnotationProcessor(libs.autoService)
}

applyJarMetadata("net.kyori.adventure")
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package net.kyori.adventure.nbt.api;

import net.kyori.adventure.text.event.DataComponentValue;
import net.kyori.adventure.util.Codec;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
Expand All @@ -37,7 +38,7 @@
*
* @since 4.0.0
*/
public interface BinaryTagHolder {
public interface BinaryTagHolder extends DataComponentValue.TagSerializable {
/**
* Encodes {@code nbt} using {@code codec}.
*
Expand Down Expand Up @@ -86,6 +87,11 @@ public interface BinaryTagHolder {
*/
@NotNull String string();

@Override
default @NotNull BinaryTagHolder asBinaryTag() {
return this;
}

/**
* Gets the held value as a binary tag.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.text.event;

import net.kyori.adventure.nbt.api.BinaryTagHolder;
import net.kyori.examination.Examinable;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
* A holder for the value of an item's data component.
*
* <p>The exact value is platform-specific. Serializers may provide their
* own implementations as well, and any logic to serialize or deserialize
* should be done per-serializer.</p>
*
* @since 4.17.0
* @sinceMinecraft 1.20.5
*/
public interface DataComponentValue extends Examinable {
/**
* Get a marker value to indicate that a data component's value should be removed.
*
* @return the removed holder
* @since 4.17.0
* @sinceMinecraft 1.20.5
*/
static DataComponentValue.@NotNull Removed removed() {
return RemovedDataComponentValueImpl.REMOVED;
}

/**
* Represent an {@link DataComponentValue} that can be represented as a binary tag.
*
* @since 4.17.0
* @sinceMinecraft 1.20.5
*/
interface TagSerializable extends DataComponentValue {

/**
* Convert this value into a binary tag value.
*
* @return the binary tag value
* @since 4.17.0
* @sinceMinecraft 1.20.5
*/
@NotNull BinaryTagHolder asBinaryTag();
}

/**
* Only valid in a patch-style usage, indicating that the data component with a certain key should be removed.
*
* @since 4.17.0
* @sinceMinecraft 1.20.5
*/
@ApiStatus.NonExtendable
interface Removed extends DataComponentValue {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.text.event;

import java.util.Objects;
import java.util.function.BiFunction;
import java.util.stream.Stream;
import net.kyori.adventure.internal.Internals;
import net.kyori.adventure.key.Key;
import net.kyori.examination.ExaminableProperty;
import org.jetbrains.annotations.NotNull;

import static java.util.Objects.requireNonNull;

final class DataComponentValueConversionImpl<I, O> implements DataComponentValueConverterRegistry.Conversion<I, O> {
private final Class<I> source;
private final Class<O> destination;
private final BiFunction<Key, I, O> conversion;

DataComponentValueConversionImpl(final @NotNull Class<I> source, final @NotNull Class<O> destination, final @NotNull BiFunction<Key, I, O> conversion) {
this.source = source;
this.destination = destination;
this.conversion = conversion;
}

@Override
public @NotNull Class<I> source() {
return this.source;
}

@Override
public @NotNull Class<O> destination() {
return this.destination;
}

@Override
public @NotNull O convert(final @NotNull Key key, final @NotNull I input) {
return this.conversion.apply(requireNonNull(key, "key"), requireNonNull(input, "input"));
}

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

@Override
public String toString() {
return Internals.toString(this);
}

@Override
public boolean equals(final Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
final DataComponentValueConversionImpl<?, ?> that = (DataComponentValueConversionImpl<?, ?>) other;
return Objects.equals(this.source, that.source)
&& Objects.equals(this.destination, that.destination)
&& Objects.equals(this.conversion, that.conversion);
}

@Override
public int hashCode() {
return Objects.hash(this.source, this.destination, this.conversion);
}
}
Loading

0 comments on commit 1849c9b

Please sign in to comment.