Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NO MERGE] Contextual Data API #2496

Draft
wants to merge 2 commits into
base: api-11
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/main/java/org/spongepowered/api/data/DataPerspective.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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;
import org.spongepowered.plugin.PluginContainer;

public interface DataPerspective {

Iterable<DataPerspective> perceives();

ValueContainer getDataPerception(DataPerspective perspective);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should just be dataPerception(...) no?


DataHolder.Mutable createDataPerception(PluginContainer plugin, DataPerspective perspective);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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;
import org.spongepowered.api.data.value.Value;

public interface DataPerspectiveResolver<V extends Value<E>, E> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this interface meant to be implemented? (DataRegistration below gives that impression).

If so, maybe it is best to make this abstract? I do not like a method that returns the key that isn't enforced to always return the same key (the instance could change).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is at the moment required to light up contextual data, otherwise we refuse to apply the data. I followed the same style we use for DataProvider already, that also has getter for the key.

But it might be more beneficial to try to intergrade this whole concept to be part of DataProvider to make them always be contextual aware, but that then touches a lot of core data API surface area.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's up to you as it's a commitment on your part but if you are up to it, I think this is a spot where we go all in and make DataProvider and the data API as a whole contextual aware. It was always meant to be, no one sat down to do it was always the issue.

Until now :).


/**
* Gets the {@link Key} this resolver supports.
*
* @return The key
*/
Key<V> key();

/**
* When multiple plugins provide the same key this is used to
* merge and pick the best.
*
* @return The merged value
*/
E merge(Iterable<E> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ static Builder builder() {
*/
Optional<DataStore> dataStore(Class<? extends DataHolder> token);

<V extends Value<E>, E> Optional<DataPerspectiveResolver<V, E>> dataPerspectiveResolverFor(Key<V> key);

/**
* Gets the registered {@link Key Keys} this controls. Note that each
* {@link Key} can only be registered/owned by a single
Expand Down Expand Up @@ -181,6 +183,8 @@ interface Builder extends org.spongepowered.api.util.Builder<DataRegistration, B
*/
Builder provider(DataProvider<?, ?> 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
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/spongepowered/api/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,7 +80,7 @@
*/
@DoNotStore
public interface Entity extends Identifiable, HoverEventSource<HoverEvent.ShowEntity>, Locatable, EntityProjectileSource, Sound.Emitter,
SerializableDataHolder.Mutable, RandomProvider, TeamMember {
SerializableDataHolder.Mutable, RandomProvider, TeamMember, DataPerspective {

/**
* Gets the {@link EntityType}.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/spongepowered/api/scoreboard/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -57,7 +58,7 @@
* For it to work, both players must have the same scoreboard, and be on a team
* registered to said scoreboard.</p>
*/
public interface Team {
public interface Team extends DataPerspective {

/**
* Creates a new {@link Builder} to build a {@link Team}.
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/spongepowered/api/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,7 +60,8 @@ public interface World<W extends World<W, L>, L extends Location<W, L>> extends
Viewer,
ArchetypeVolumeCreator,
WeatherUniverse,
RegistryHolder {
RegistryHolder,
DataPerspective {

/**
* Gets the {@link WorldProperties properties}.
Expand Down