-
-
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.
Merge pull request #1066 from KyoriPowered/mc/1.20.5/data-components
feat(api): item data components
- Loading branch information
Showing
32 changed files
with
1,691 additions
and
126 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
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
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
80 changes: 80 additions & 0 deletions
80
api/src/main/java/net/kyori/adventure/text/event/DataComponentValue.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,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 { | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
api/src/main/java/net/kyori/adventure/text/event/DataComponentValueConversionImpl.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,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); | ||
} | ||
} |
Oops, something went wrong.