From 123754f46c374d4c111d66c6ecf7f2c6613fe058 Mon Sep 17 00:00:00 2001 From: aromaa Date: Sun, 21 Apr 2024 00:39:55 +0300 Subject: [PATCH 1/2] Initial contextual data work --- .../api/data/DataPerspective.java | 13 ++++++++ .../api/data/DataPerspectiveResolver.java | 31 +++++++++++++++++++ .../api/data/DataRegistration.java | 4 +++ .../org/spongepowered/api/entity/Entity.java | 3 +- .../spongepowered/api/scoreboard/Team.java | 3 +- .../org/spongepowered/api/world/World.java | 4 ++- 6 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/spongepowered/api/data/DataPerspective.java create mode 100644 src/main/java/org/spongepowered/api/data/DataPerspectiveResolver.java diff --git a/src/main/java/org/spongepowered/api/data/DataPerspective.java b/src/main/java/org/spongepowered/api/data/DataPerspective.java new file mode 100644 index 0000000000..daf5fcdd31 --- /dev/null +++ b/src/main/java/org/spongepowered/api/data/DataPerspective.java @@ -0,0 +1,13 @@ +package org.spongepowered.api.data; + +import org.spongepowered.api.data.value.ValueContainer; +import org.spongepowered.plugin.PluginContainer; + +public interface DataPerspective { + + Iterable perceives(); + + ValueContainer getDataPerception(DataPerspective perspective); + + DataHolder.Mutable createDataPerception(PluginContainer plugin, DataPerspective perspective); +} diff --git a/src/main/java/org/spongepowered/api/data/DataPerspectiveResolver.java b/src/main/java/org/spongepowered/api/data/DataPerspectiveResolver.java new file mode 100644 index 0000000000..42bc08d230 --- /dev/null +++ b/src/main/java/org/spongepowered/api/data/DataPerspectiveResolver.java @@ -0,0 +1,31 @@ +package org.spongepowered.api.data; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.spongepowered.api.data.value.Value; + +public interface DataPerspectiveResolver, E> { + + /** + * Gets the {@link Key} this resolver supports. + * + * @return The key + */ + Key key(); + + /** + * When multiple plugins provide the same key this is used to + * merge and pick the best. + * + * @return The merged value + */ + E merge(Iterable values); + + /** + * When data holders value changes when looking at perspective of. + * + * @param dataHolder The data holder which value was overridden. + * @param perspective The perspective it is perceived from. + * @param value The value. + */ + void apply(DataHolder dataHolder, DataPerspective perspective, @Nullable E value); +} diff --git a/src/main/java/org/spongepowered/api/data/DataRegistration.java b/src/main/java/org/spongepowered/api/data/DataRegistration.java index 9f46cfbe5e..60bc6fb5ad 100644 --- a/src/main/java/org/spongepowered/api/data/DataRegistration.java +++ b/src/main/java/org/spongepowered/api/data/DataRegistration.java @@ -111,6 +111,8 @@ static Builder builder() { */ Optional dataStore(Class token); + , E> Optional> dataPerspectiveResolverFor(Key key); + /** * Gets the registered {@link Key Keys} this controls. Note that each * {@link Key} can only be registered/owned by a single @@ -181,6 +183,8 @@ interface Builder extends org.spongepowered.api.util.Builder provider) throws DuplicateProviderException; + Builder perspectiveResolver(DataPerspectiveResolver resolver); + /** * Gives the {@link Key} to this builder signifying the key is to be * registered either with an applicable {@link DataProvider} or an diff --git a/src/main/java/org/spongepowered/api/entity/Entity.java b/src/main/java/org/spongepowered/api/entity/Entity.java index 2f39680f34..a6a20da829 100644 --- a/src/main/java/org/spongepowered/api/entity/Entity.java +++ b/src/main/java/org/spongepowered/api/entity/Entity.java @@ -28,6 +28,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEventSource; +import org.spongepowered.api.data.DataPerspective; import org.spongepowered.api.data.Keys; import org.spongepowered.api.data.SerializableDataHolder; import org.spongepowered.api.data.value.ListValue; @@ -79,7 +80,7 @@ */ @DoNotStore public interface Entity extends Identifiable, HoverEventSource, Locatable, EntityProjectileSource, Sound.Emitter, - SerializableDataHolder.Mutable, RandomProvider, TeamMember { + SerializableDataHolder.Mutable, RandomProvider, TeamMember, DataPerspective { /** * Gets the {@link EntityType}. diff --git a/src/main/java/org/spongepowered/api/scoreboard/Team.java b/src/main/java/org/spongepowered/api/scoreboard/Team.java index 725c16dc6b..b4f5939480 100644 --- a/src/main/java/org/spongepowered/api/scoreboard/Team.java +++ b/src/main/java/org/spongepowered/api/scoreboard/Team.java @@ -27,6 +27,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.spongepowered.api.Sponge; +import org.spongepowered.api.data.DataPerspective; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.util.CopyableBuilder; @@ -57,7 +58,7 @@ * For it to work, both players must have the same scoreboard, and be on a team * registered to said scoreboard.

*/ -public interface Team { +public interface Team extends DataPerspective { /** * Creates a new {@link Builder} to build a {@link Team}. diff --git a/src/main/java/org/spongepowered/api/world/World.java b/src/main/java/org/spongepowered/api/world/World.java index 1a81f4bc66..ed2a540093 100644 --- a/src/main/java/org/spongepowered/api/world/World.java +++ b/src/main/java/org/spongepowered/api/world/World.java @@ -27,6 +27,7 @@ import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.ForwardingAudience; import org.spongepowered.api.Server; +import org.spongepowered.api.data.DataPerspective; import org.spongepowered.api.effect.Viewer; import org.spongepowered.api.entity.Entity; import org.spongepowered.api.entity.living.player.Player; @@ -59,7 +60,8 @@ public interface World, L extends Location> extends Viewer, ArchetypeVolumeCreator, WeatherUniverse, - RegistryHolder { + RegistryHolder, + DataPerspective { /** * Gets the {@link WorldProperties properties}. From 22676ca81183bbb78c814e694416457954d1cedc Mon Sep 17 00:00:00 2001 From: aromaa Date: Tue, 23 Apr 2024 16:28:00 +0300 Subject: [PATCH 2/2] spotlessApply --- .../api/data/DataPerspective.java | 24 +++++++++++++++++++ .../api/data/DataPerspectiveResolver.java | 24 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/main/java/org/spongepowered/api/data/DataPerspective.java b/src/main/java/org/spongepowered/api/data/DataPerspective.java index daf5fcdd31..42630300a7 100644 --- a/src/main/java/org/spongepowered/api/data/DataPerspective.java +++ b/src/main/java/org/spongepowered/api/data/DataPerspective.java @@ -1,3 +1,27 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * 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 org.spongepowered.api.data; import org.spongepowered.api.data.value.ValueContainer; diff --git a/src/main/java/org/spongepowered/api/data/DataPerspectiveResolver.java b/src/main/java/org/spongepowered/api/data/DataPerspectiveResolver.java index 42bc08d230..62a5700e12 100644 --- a/src/main/java/org/spongepowered/api/data/DataPerspectiveResolver.java +++ b/src/main/java/org/spongepowered/api/data/DataPerspectiveResolver.java @@ -1,3 +1,27 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * 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 org.spongepowered.api.data; import org.checkerframework.checker.nullness.qual.Nullable;