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 #3995

Draft
wants to merge 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package org.spongepowered.common.bridge.data;

import org.spongepowered.api.data.DataPerspective;
import org.spongepowered.api.effect.VanishState;

public interface VanishableBridge {
Expand All @@ -32,6 +33,8 @@ public interface VanishableBridge {

void bridge$vanishState(VanishState state);

void bridge$vanishState(VanishState state, DataPerspective perspective);

boolean bridge$isInvisible();

void bridge$setInvisible(boolean invisible);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This file is part of Sponge, 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.common.bridge.world.entity;

import org.spongepowered.common.data.contextual.ContextualDataHolder;

public interface EntityBridge_Contextual {

ContextualDataHolder bridge$contextualData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import net.kyori.adventure.text.format.NamedTextColor;
import net.minecraft.server.level.ServerPlayer;

import java.util.List;

public interface PlayerTeamBridge {

Component bridge$getDisplayName();
Expand All @@ -50,4 +52,10 @@ public interface PlayerTeamBridge {
Audience bridge$getTeamChannel(ServerPlayer player);

Audience bridge$getNonTeamChannel();

void bridge$addPlayer(ServerPlayer player);

void bridge$removePlayer(ServerPlayer player, boolean sendPackets);

List<ServerPlayer> bridge$getPlayers();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This file is part of Sponge, 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.common.bridge.world.scores;

import org.spongepowered.common.data.contextual.ContextualDataDelegate;

public interface PlayerTeamBridge_Contextual {

ContextualDataDelegate bridge$contextualData();
}
13 changes: 13 additions & 0 deletions src/main/java/org/spongepowered/common/data/SpongeDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.spongepowered.api.data.DataHolderBuilder;
import org.spongepowered.api.data.DataManager;
import org.spongepowered.api.data.DataManipulator.Mutable;
import org.spongepowered.api.data.DataPerspectiveResolver;
import org.spongepowered.api.data.DataProvider;
import org.spongepowered.api.data.Key;
import org.spongepowered.api.data.meta.BannerPatternLayer;
Expand Down Expand Up @@ -91,6 +92,7 @@
import org.spongepowered.common.data.builder.util.weighted.BaseAndVarianceBuilder;
import org.spongepowered.common.data.builder.util.weighted.FixedBuilder;
import org.spongepowered.common.data.builder.util.weighted.OptionalVarianceBuilder;
import org.spongepowered.common.data.contextual.DataPerspectiveResolverRegistry;
import org.spongepowered.common.data.datasync.entity.EntityAirSupplyConverter;
import org.spongepowered.common.data.datasync.entity.EntityBabyConverter;
import org.spongepowered.common.data.datasync.entity.EntityCustomNameConverter;
Expand Down Expand Up @@ -144,6 +146,7 @@ public final class SpongeDataManager implements DataManager {

private final DataStoreRegistry dataStoreRegistry;
private final DataProviderRegistry dataProviderRegistry;
private final DataPerspectiveResolverRegistry dataPerspectiveResolverRegistry;
private final Map<Class<?>, DataBuilder<?>> builders;
private final Map<Class<? extends DataHolder.Immutable<?>>, DataHolderBuilder.Immutable<?, ?>> immutableDataBuilderMap;
private final Map<Class<? extends DataSerializable>, List<DataContentUpdater>> updatersMap;
Expand All @@ -158,6 +161,7 @@ private SpongeDataManager() {

this.dataStoreRegistry = new DataStoreRegistry();
this.dataProviderRegistry = new DataProviderRegistry();
this.dataPerspectiveResolverRegistry = new DataPerspectiveResolverRegistry();
this.builders = new HashMap<>();
this.immutableDataBuilderMap = new MapMaker()
.concurrencyLevel(4)
Expand Down Expand Up @@ -333,6 +337,9 @@ public void registerCustomDataRegistration(final SpongeDataRegistration registra

for (final Key key : registration.keys()) {
this.registerCustomDataProviderForKey(registration, key);

final Optional<DataPerspectiveResolver<?, ?>> resolver = registration.dataPerspectiveResolverFor(key);
resolver.ifPresent(this.dataPerspectiveResolverRegistry::register);
}
}

Expand Down Expand Up @@ -363,6 +370,8 @@ public void registerDataRegistration(final SpongeDataRegistration registration)
for (DataProvider<?, ?> provider : providers) {
this.dataProviderRegistry.register(provider);
}
final Optional<DataPerspectiveResolver<?, ?>> resolver = registration.dataPerspectiveResolverFor(key);
resolver.ifPresent(this.dataPerspectiveResolverRegistry::register);
}
}

Expand All @@ -389,6 +398,10 @@ public static DataProviderRegistry getProviderRegistry() {
return SpongeDataManager.INSTANCE.dataProviderRegistry;
}

public static DataPerspectiveResolverRegistry getDataPerspectiveResolverRegistry() {
return SpongeDataManager.INSTANCE.dataPerspectiveResolverRegistry;
}

public void registerDefaultBuilders() {
this.registerBuilder(ItemStack.class, new SpongeItemStack.BuilderImpl());
this.registerBuilder(ItemStackSnapshot.class, new SpongeItemStackSnapshotDataBuilder());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.leangen.geantyref.GenericTypeReflector;
import io.leangen.geantyref.TypeToken;
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.DataPerspectiveResolver;
import org.spongepowered.api.data.DataProvider;
import org.spongepowered.api.data.DataRegistration;
import org.spongepowered.api.data.Key;
Expand All @@ -46,11 +47,13 @@ public final class SpongeDataRegistration implements DataRegistration {
final List<Key<?>> keys;
final Map<Type, DataStore> dataStoreMap;
final Multimap<Key, DataProvider> dataProviderMap;
final Map<Key, DataPerspectiveResolver> dataPerspectiveResolverMap;

SpongeDataRegistration(final SpongeDataRegistrationBuilder builder) {
this.keys = builder.keys;
this.dataStoreMap = builder.dataStoreMap;
this.dataProviderMap = builder.dataProviderMap;
this.dataPerspectiveResolverMap = builder.dataPerspectiveResolverMap;
}

@Override
Expand All @@ -69,6 +72,11 @@ public Optional<DataStore> dataStore(final Class<? extends DataHolder> token) {
return this.getDataStore0(token);
}

@Override
public <V extends Value<E>, E> Optional<DataPerspectiveResolver<V, E>> dataPerspectiveResolverFor(Key<V> key) {
return Optional.ofNullable(this.dataPerspectiveResolverMap.get(key));
}

private Optional<DataStore> getDataStore0(final Type type) {
DataStore dataStore = this.dataStoreMap.get(type);
if (dataStore != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import org.spongepowered.api.data.DataPerspectiveResolver;
import org.spongepowered.api.data.DataProvider;
import org.spongepowered.api.data.DataRegistration;
import org.spongepowered.api.data.DuplicateDataStoreException;
Expand All @@ -45,6 +46,7 @@ public final class SpongeDataRegistrationBuilder implements DataRegistration.Bui

Multimap<Key, DataProvider> dataProviderMap = HashMultimap.create();
Map<Type, DataStore> dataStoreMap = new HashMap<>();
Map<Key, DataPerspectiveResolver> dataPerspectiveResolverMap = new HashMap<>();
List<Key<?>> keys = new ArrayList<>();

@Override
Expand All @@ -61,6 +63,12 @@ public DataRegistration.Builder provider(final DataProvider<?, ?> provider) thro
return this;
}

@Override
public DataRegistration.Builder perspectiveResolver(DataPerspectiveResolver<?, ?> resolver) {
this.dataPerspectiveResolverMap.put(resolver.key(), resolver);
return this;
}

@Override
public DataRegistration.Builder dataKey(final Key<?> key) {
this.keys.add(key);
Expand Down Expand Up @@ -89,6 +97,7 @@ public DataRegistration build() {
public SpongeDataRegistrationBuilder reset() {
this.dataProviderMap = HashMultimap.create();
this.dataStoreMap = new IdentityHashMap<>();
this.dataPerspectiveResolverMap = new HashMap<>();
this.keys = new ArrayList<>();
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This file is part of Sponge, 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.common.data.contextual;

import net.minecraft.network.protocol.Packet;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.data.DataPerspective;

public interface ContextualData {

@Nullable PerspectiveContainer<?, ?> dataPerception(DataPerspective perspective);

PerspectiveContainer<?, ?> createDataPerception(DataPerspective perspective);

void linkContextualOwner(ContextualDataOwner<?> owner);
void unlinkContextualOwner(ContextualDataOwner<?> owner);

void broadcastToPerceives(Packet<?> packet);
}
Loading
Loading