Skip to content

Commit

Permalink
Update for typetoken-avoiding helper methods (#3411)
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed May 22, 2021
1 parent e259dbb commit 47f0e36
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import io.leangen.geantyref.TypeToken;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.spongepowered.api.command.Command;
Expand All @@ -37,7 +38,6 @@
import org.spongepowered.api.command.parameter.CommandContext;
import org.spongepowered.api.command.parameter.Parameter;
import org.spongepowered.api.command.parameter.managed.ValueParameter;
import org.spongepowered.api.util.TypeTokens;
import org.spongepowered.common.command.SpongeCommandCompletion;

import java.time.Duration;
Expand All @@ -62,10 +62,10 @@ private CallbackCommand() {
public Command.Parameterized createCommand() {
this.callbacks.invalidateAll();

final Parameter.Key<Consumer<CommandCause>> key = Parameter.key("key", TypeTokens.COMMAND_CAUSE_CONSUMER);
final Parameter.Key<Consumer<CommandCause>> key = Parameter.key("key", new TypeToken<Consumer<CommandCause>>() {});
return Command.builder()
.shortDescription(Component.text("Execute a callback registered as part of a TextComponent. Primarily for internal use"))
.addParameter(Parameter.builder(TypeTokens.COMMAND_CAUSE_CONSUMER).key(key).addParser(new CallbackValueParameter()).build())
.addParameter(Parameter.builder(key).addParser(new CallbackValueParameter()).build())
.executor(context -> {
context.requireOne(key).accept(context.cause());
return CommandResult.success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@

public final class SpongeParameterFactory implements Parameter.Factory {

@Override
public <T> Parameter.Value.Builder<T> createParameterBuilder(final Parameter.Key<T> key) {
return new SpongeParameterValueBuilder<T>(key.type()).key(key);
}

@Override
public <T> Parameter.Value.@NonNull Builder<T> createParameterBuilder(final @NonNull TypeToken<T> parameterClass) {
return new SpongeParameterValueBuilder<>(parameterClass.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Supplier;

Expand All @@ -70,13 +73,88 @@ public <T, B extends Value<T>> SpongeKeyBuilder<T, B> type(final TypeToken<B> to
return (SpongeKeyBuilder<T, B>) this;
}

@Override public <T> Key.Builder<T, Value<T>> elementType(final Class<T> type) {
@Override
public <T> Key.Builder<T, Value<T>> elementType(final Class<T> type) {
Objects.requireNonNull(type, "type");
this.valueType = TypeFactory.parameterizedClass(Value.class, type);
this.elementType = type;
return (SpongeKeyBuilder<T, Value<T>>) this;
}

@Override
public <T> Key.Builder<T, Value<T>> elementType(final TypeToken<T> type) {
Objects.requireNonNull(type, "type");
this.valueType = TypeFactory.parameterizedClass(Value.class, type.getType());
this.elementType = type.getType();
return (SpongeKeyBuilder<T, Value<T>>) this;
}

@Override
public <T> Key.Builder<List<T>, ListValue<T>> listElementType(final Class<T> type) {
Objects.requireNonNull(type, "type");
this.valueType = TypeFactory.parameterizedClass(ListValue.class, type);
this.elementType = TypeFactory.parameterizedClass(List.class, type);
return (SpongeKeyBuilder<List<T>, ListValue<T>>) this;
}

@Override
public <T> Key.Builder<List<T>, ListValue<T>> listElementType(final TypeToken<T> type) {
Objects.requireNonNull(type, "type");
this.valueType = TypeFactory.parameterizedClass(ListValue.class, type.getType());
this.elementType = TypeFactory.parameterizedClass(List.class, type.getType());
return (SpongeKeyBuilder<List<T>, ListValue<T>>) this;
}

@Override
public <T> Key.Builder<Set<T>, SetValue<T>> setElementType(final Class<T> type) {
Objects.requireNonNull(type, "type");
this.valueType = TypeFactory.parameterizedClass(SetValue.class, type);
this.elementType = TypeFactory.parameterizedClass(Set.class, type);
return (SpongeKeyBuilder<Set<T>, SetValue<T>>) this;
}

@Override
public <T> Key.Builder<Set<T>, SetValue<T>> setElementType(final TypeToken<T> type) {
Objects.requireNonNull(type, "type");
this.valueType = TypeFactory.parameterizedClass(SetValue.class, type.getType());
this.elementType = TypeFactory.parameterizedClass(Set.class, type.getType());
return (SpongeKeyBuilder<Set<T>, SetValue<T>>) this;
}

@Override
public <K, V1> Key.Builder<Map<K, V1>, MapValue<K, V1>> mapElementType(final Class<K> keyType, final Class<V1> valueType) {
Objects.requireNonNull(keyType, "keyType");
Objects.requireNonNull(valueType, "valueType");
this.valueType = TypeFactory.parameterizedClass(MapValue.class, keyType, valueType);
this.elementType = TypeFactory.parameterizedClass(Map.class, keyType, valueType);
return (SpongeKeyBuilder<Map<K, V1>, MapValue<K, V1>>) this;
}

@Override
public <K, V1> Key.Builder<Map<K, V1>, MapValue<K, V1>> mapElementType(final TypeToken<K> keyType, final TypeToken<V1> valueType) {
Objects.requireNonNull(keyType, "keyType");
Objects.requireNonNull(valueType, "valueType");
this.valueType = TypeFactory.parameterizedClass(MapValue.class, keyType.getType(), valueType.getType());
this.elementType = TypeFactory.parameterizedClass(Map.class, keyType.getType(), valueType.getType());
return (SpongeKeyBuilder<Map<K, V1>, MapValue<K, V1>>) this;
}

@Override
public <T> Key.Builder<WeightedTable<T>, WeightedCollectionValue<T>> weightedCollectionElementType(final Class<T> type) {
Objects.requireNonNull(type, "type");
this.valueType = TypeFactory.parameterizedClass(WeightedCollectionValue.class, type);
this.elementType = TypeFactory.parameterizedClass(WeightedTable.class, type);
return (SpongeKeyBuilder<WeightedTable<T>, WeightedCollectionValue<T>>) this;
}

@Override
public <T> Key.Builder<WeightedTable<T>, WeightedCollectionValue<T>> weightedCollectionElementType(final TypeToken<T> type) {
Objects.requireNonNull(type, "type");
this.valueType = TypeFactory.parameterizedClass(WeightedCollectionValue.class, type.getType());
this.elementType = TypeFactory.parameterizedClass(WeightedTable.class, type.getType());
return (SpongeKeyBuilder<WeightedTable<T>, WeightedCollectionValue<T>>) this;
}

@Override
public SpongeKeyBuilder<E, V> comparator(final Comparator<? super E> comparator) {
Preconditions.checkNotNull(comparator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.spongepowered.api.data.Keys;
import org.spongepowered.api.data.persistence.DataContentUpdater;
import org.spongepowered.api.data.value.Value;
import org.spongepowered.api.util.TypeTokens;
import org.spongepowered.common.bridge.world.entity.item.ItemEntityBridge;
import org.spongepowered.common.data.ByteToBooleanContentUpdater;
import org.spongepowered.common.data.SpongeDataManager;
Expand All @@ -41,8 +40,8 @@

public final class ItemData {

public static final Key<Value<Integer>> PREVIOUS_PICKUP_DELAY = Key.builder().key(ResourceKey.sponge("power")).type(TypeTokens.INTEGER_VALUE_TOKEN).build();
public static final Key<Value<Integer>> PREVIOUS_DESPAWN_DELAY = Key.builder().key(ResourceKey.sponge("power")).type(TypeTokens.INTEGER_VALUE_TOKEN).build();
public static final Key<Value<Integer>> PREVIOUS_PICKUP_DELAY = Key.builder().key(ResourceKey.sponge("power")).elementType(Integer.class).build();
public static final Key<Value<Integer>> PREVIOUS_DESPAWN_DELAY = Key.builder().key(ResourceKey.sponge("power")).elementType(Integer.class).build();

private static final DataContentUpdater INFINITE_DELAYS_UPDATER_BYTE_TO_BOOL_FIX = new ByteToBooleanContentUpdater(1, 2, Keys.INFINITE_PICKUP_DELAY, Keys.INFINITE_DESPAWN_DELAY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import org.spongepowered.api.scheduler.Scheduler;
import org.spongepowered.api.scheduler.Task;
import org.spongepowered.api.util.Ticks;
import org.spongepowered.api.util.TypeTokens;
import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.math.vector.Vector3i;
import org.spongepowered.plugin.PluginContainer;
Expand Down Expand Up @@ -176,31 +175,31 @@ public void onRegisterData(final RegisterDataEvent event) {

// Or if it is super simple data

this.mySimpleDataKey = Key.of(this.plugin, "mysimpledata", TypeTokens.STRING_VALUE_TOKEN);
this.mySimpleDataKey = Key.from(this.plugin, "mysimpledata", String.class);
event.register(DataRegistration.of(this.mySimpleDataKey, ItemStack.class));

this.myItemTypeKey = Key.of(this.plugin, "myitemtypedata", TypeTokens.ITEM_TYPE_VALUE_TOKEN);
this.myItemTypeKey = Key.from(this.plugin, "myitemtypedata", ItemType.class);
event.register(DataRegistration.of(this.myItemTypeKey, ItemStack.class));
}

// replace with mongoDB - for web-scale
private Map<ResourceKey, Map<Vector3i, Integer>> myCustomData = new HashMap<>();

private DataTransactionResult removeData(ServerLocation serverLocation) {
private DataTransactionResult removeData(final ServerLocation serverLocation) {
final Integer removed = this.myCustomData.getOrDefault(serverLocation.worldKey(), Collections.emptyMap()).remove(serverLocation.blockPosition());
if (removed == null) {
return DataTransactionResult.failNoData();
}
return DataTransactionResult.successRemove(Value.immutableOf(this.myDataKey, removed));
}

private DataTransactionResult setData(ServerLocation serverLocation, Integer value) {
private DataTransactionResult setData(final ServerLocation serverLocation, final Integer value) {
final Map<Vector3i, Integer> worldData = this.myCustomData.computeIfAbsent(serverLocation.worldKey(), k -> new HashMap<>());
worldData.put(serverLocation.blockPosition(), value);
return DataTransactionResult.successResult(Value.immutableOf(this.myDataKey, value));
}

private Integer getData(ServerLocation serverLocation) {
private Integer getData(final ServerLocation serverLocation) {
return this.myCustomData.getOrDefault(serverLocation.worldKey(), Collections.emptyMap()).get(serverLocation.blockPosition());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.spongepowered.test.projectile;

import com.google.inject.Inject;
import io.leangen.geantyref.TypeToken;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand All @@ -50,7 +51,6 @@
import org.spongepowered.api.event.lifecycle.RegisterCommandEvent;
import org.spongepowered.api.projectile.source.ProjectileSource;
import org.spongepowered.api.registry.RegistryTypes;
import org.spongepowered.api.util.TypeTokens;
import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.math.vector.Vector3d;
Expand Down Expand Up @@ -79,7 +79,7 @@ public void enable(final CommandContext ctx) {
public void registerCommand(final RegisterCommandEvent<Command.Parameterized> event) {
final Parameter.Value<EntityType<@NonNull ?>> entityTypeParameter =
Parameter.registryElement(
TypeTokens.ENTITY_TYPE_TOKEN,
new TypeToken<EntityType<?>>() {},
(ctx) -> Sponge.game().registries(),
RegistryTypes.ENTITY_TYPE,
"minecraft",
Expand Down

0 comments on commit 47f0e36

Please sign in to comment.