Skip to content

Commit

Permalink
Initial contextual data work
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Apr 20, 2024
1 parent cb4ee93 commit 123754f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
13 changes: 13 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,13 @@
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);

DataHolder.Mutable createDataPerception(PluginContainer plugin, DataPerspective perspective);
}
Original file line number Diff line number Diff line change
@@ -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<V extends Value<E>, E> {

/**
* 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

0 comments on commit 123754f

Please sign in to comment.