diff --git a/api/src/main/java/net/kyori/adventure/pointer/PointersSupplier.java b/api/src/main/java/net/kyori/adventure/pointer/PointersSupplier.java new file mode 100644 index 000000000..6b6d08b29 --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/pointer/PointersSupplier.java @@ -0,0 +1,127 @@ +/* + * 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.pointer; + +import java.util.function.Function; +import net.kyori.adventure.builder.AbstractBuilder; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * A supplier of {@link Pointers} that allows for the implementation of pointers + * in a static context without having to manually create a new pointers instance for + * each instance of a given type. + * + *
An example of how this could be implemented is as follows:
+ *{@code + * public class MyPointeredObject extends SomePointeredParent implements Pointered { + * private static final PointersSupplier+ * + * @paramPOINTERS = PointersSupplier.builder() + * .parent(SomePointeredParent.POINTERS) // Fallback to the parent to get pointers from. + * .resolving(Identity.UUID, MyPointeredObject::getUniqueId) + * .resolving(Identity.DISPLAY_NAME, MyPointeredObject::getDisplayName) + * .build(); + * + * @Override + * public Pointers pointers() { + * return POINTERS.view(this); + * } + * } + * }
the type of the pointer + * @return if this supplier supports a given pointer + * @since 4.17.0 + */ +
boolean supports(final @NotNull Pointer
pointer); + + /** + * Returns the resolver for a given pointer (if any). + * + * @param pointer the pointer + * @param
the type of the pointer + * @return the resolver, if any + * @since 4.17.0 + */ +
@Nullable Function super T, P> resolver(final @NotNull Pointer
pointer);
+
+ /**
+ * A builder for {@link PointersSupplier}.
+ *
+ * @param the type of the pointer
+ * @return this builder
+ * @since 4.17.0
+ */
+ @Contract("_, _ -> this")
+ @NotNull Builder pointer, final @NotNull Function boolean supports(@NotNull Pointer pointer) {
+ if (this.resolvers.containsKey(Objects.requireNonNull(pointer, "pointer"))) {
+ return true;
+ } else if (this.parent == null) {
+ return false;
+ } else {
+ return this.parent.supports(pointer);
+ }
+ }
+
+ @Override
+ @SuppressWarnings("unchecked") // all values are checked on entry
+ public @Nullable Function super T, P> resolver(@NotNull Pointer pointer) {
+ Function super T, ?> resolver = this.resolvers.get(Objects.requireNonNull(pointer, "pointer"));
+
+ if (resolver != null) {
+ return (Function super T, P>) resolver;
+ } else if (this.parent == null) {
+ return null;
+ } else {
+ return this.parent.resolver(pointer);
+ }
+ }
+
+ static final class ForwardingPointers implements Pointers {
+ private final U instance;
+ private final PointersSupplierImpl supplier;
+
+ ForwardingPointers(final @NotNull U instance, final @NotNull PointersSupplierImpl supplier) {
+ this.instance = instance;
+ this.supplier = supplier;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked") // all values are checked on entry
+ public @NotNull Builder pointer, final @NotNull Function