Skip to content

Commit 9f90f08

Browse files
committed
Merge remote-tracking branch 'origin/api-8' into api-9
2 parents f399265 + e897193 commit 9f90f08

File tree

10 files changed

+498
-39
lines changed

10 files changed

+498
-39
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ dependencies {
8383
}
8484

8585
// Plugin spi, includes plugin-meta
86-
api("org.spongepowered:plugin-spi:0.2.0")
86+
api("org.spongepowered:plugin-spi:0.2.1-SNAPSHOT")
8787

8888
// Configurate
8989
api(platform("org.spongepowered:configurate-bom:$configurateVersion"))

src/main/java/org/spongepowered/api/command/parameter/Parameter.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import org.spongepowered.api.world.server.ServerLocation;
6666
import org.spongepowered.api.world.server.ServerWorld;
6767
import org.spongepowered.configurate.util.Types;
68-
import org.spongepowered.math.vector.Vector2d;
6968
import org.spongepowered.math.vector.Vector3d;
7069
import org.spongepowered.plugin.PluginContainer;
7170

@@ -78,6 +77,7 @@
7877
import java.time.LocalDateTime;
7978
import java.util.Arrays;
8079
import java.util.Collection;
80+
import java.util.Collections;
8181
import java.util.List;
8282
import java.util.Map;
8383
import java.util.Optional;
@@ -713,11 +713,47 @@ static Parameter.Value.Builder<ServerWorld> world() {
713713
*/
714714
static <T> Parameter.Value.Builder<T> registryElement(
715715
final TypeToken<T> type,
716-
@NonNull final Function<CommandContext, RegistryHolder> holderProvider,
716+
@NonNull final Function<CommandContext, @Nullable RegistryHolder> holderProvider,
717717
@NonNull final RegistryType<T> registryKey,
718718
@NonNull final String @NonNull... defaultNamespaces) {
719+
return Parameter.registryElement(
720+
type,
721+
Collections.singletonList(holderProvider),
722+
registryKey,
723+
defaultNamespaces
724+
);
725+
}
726+
727+
/**
728+
* Creates a builder that has the {@link ValueParameter} that allows you to
729+
* choose from cataloged types.
730+
*
731+
* <p>See {@link VariableValueParameters.RegistryEntryBuilder
732+
* #defaultNamespace(String)} for how default namespaces work.</p>
733+
*
734+
* <p>If the {@link Game} or {@link Server} scoped {@link RegistryHolder}
735+
* is required,
736+
* {@link VariableValueParameters.RegistryEntryBuilder#GLOBAL_HOLDER_PROVIDER}
737+
* or {@link VariableValueParameters.RegistryEntryBuilder#SERVER_HOLDER_PROVIDER}
738+
* may be used.</p>
739+
*
740+
* @param type The registry value type to check for choices
741+
* @param holderProviders {@link Function}s that provides the appropriate
742+
* {@link RegistryHolder} to get the appropriate {@link Registry}
743+
* @param registryKey The {@link RegistryKey} that represents the target
744+
* {@link Registry}
745+
* @param defaultNamespaces The default namespaces that will be used with the
746+
* provided value if the supplied argument is un-namespaced
747+
* @param <T> The type of registry value
748+
* @return A {@link Parameter.Value.Builder}
749+
*/
750+
static <T> Parameter.Value.Builder<T> registryElement(
751+
final @NonNull TypeToken<T> type,
752+
final @NonNull List<Function<CommandContext, @Nullable RegistryHolder>> holderProviders,
753+
final @NonNull RegistryType<T> registryKey,
754+
final @NonNull String @NonNull... defaultNamespaces) {
719755
final VariableValueParameters.RegistryEntryBuilder<? extends T> vvp =
720-
VariableValueParameters.registryEntryBuilder(holderProvider, registryKey);
756+
VariableValueParameters.registryEntryBuilder(holderProviders, registryKey);
721757
for (final String namespace : defaultNamespaces) {
722758
vvp.defaultNamespace(namespace);
723759
}

src/main/java/org/spongepowered/api/command/parameter/managed/standard/VariableValueParameters.java

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,24 @@
2828
import net.kyori.adventure.text.serializer.ComponentSerializer;
2929
import org.checkerframework.checker.nullness.qual.Nullable;
3030
import org.spongepowered.api.Game;
31-
import org.spongepowered.api.ResourceKey;
3231
import org.spongepowered.api.Server;
3332
import org.spongepowered.api.Sponge;
3433
import org.spongepowered.api.command.parameter.CommandContext;
35-
import org.spongepowered.api.command.parameter.Parameter;
3634
import org.spongepowered.api.command.parameter.managed.ValueParameter;
3735
import org.spongepowered.api.registry.DefaultedRegistryReference;
3836
import org.spongepowered.api.registry.DefaultedRegistryType;
3937
import org.spongepowered.api.registry.Registry;
4038
import org.spongepowered.api.registry.RegistryHolder;
4139
import org.spongepowered.api.registry.RegistryKey;
4240
import org.spongepowered.api.registry.RegistryType;
43-
import org.spongepowered.api.registry.RegistryTypes;
4441
import org.spongepowered.api.util.Builder;
42+
import org.spongepowered.api.world.Locatable;
43+
import org.spongepowered.api.world.World;
44+
import org.spongepowered.api.world.server.ServerWorld;
4545

4646
import java.util.Collection;
4747
import java.util.Collections;
48+
import java.util.List;
4849
import java.util.Map;
4950
import java.util.function.Function;
5051
import java.util.function.Supplier;
@@ -70,21 +71,23 @@ private VariableValueParameters() {}
7071
*/
7172
public static <T> RegistryEntryBuilder<T> registryEntryBuilder(
7273
final Function<CommandContext, @Nullable RegistryHolder> holderProvider, final RegistryType<T> registryKey) {
73-
return Sponge.game().factoryProvider().provide(Factory.class).createRegistryEntryBuilder(holderProvider, registryKey);
74+
return VariableValueParameters.registryEntryBuilder(Collections.singletonList(holderProvider), registryKey);
7475
}
7576

7677
/**
7778
* Creates a builder that can build a {@link ValueParameter} that returns
7879
* an appropriate {@link Registry registry} entry from an argument.
7980
*
80-
* @param registryProvider A {@link Function} that retrieves an appropriate
81-
* {@link Registry} to get objects from
81+
* @param holderProviders The providers for {@link RegistryHolder}s to
82+
* retrieve the selected {@link Registry} from
83+
* @param registryKey The {@link RegistryKey} that represents the target
84+
* {@link Registry} to get objects from
8285
* @param <T> The type in the {@link Registry}
8386
* @return The builder
8487
*/
8588
public static <T> RegistryEntryBuilder<T> registryEntryBuilder(
86-
final Function<CommandContext, @Nullable ? extends Registry<? extends T>> registryProvider) {
87-
return Sponge.game().factoryProvider().provide(Factory.class).createRegistryEntryBuilder(registryProvider);
89+
final List<Function<CommandContext, @Nullable RegistryHolder>> holderProviders, final RegistryType<T> registryKey) {
90+
return Sponge.game().factoryProvider().provide(Factory.class).createRegistryEntryBuilder(holderProviders, registryKey);
8891
}
8992

9093
/**
@@ -243,6 +246,37 @@ public interface RegistryEntryBuilder<T> extends Builder<ValueParameter<T>, Regi
243246
}
244247
};
245248

249+
/**
250+
* A {@link Function} that always provides the {@link World} scoped
251+
* {@link RegistryHolder} from the first {@link Locatable} in the
252+
* provided {@link CommandContext}.
253+
*/
254+
Function<CommandContext, @Nullable RegistryHolder> WORLD_FROM_LOCATABLE_HOLDER_PROVIDER =
255+
in -> in.cause().first(Locatable.class).map(Locatable::world).map(World::registries).orElse(null);
256+
257+
/**
258+
* A {@link Function} that always provides the {@link World} scoped
259+
* {@link RegistryHolder} from the first {@link ServerWorld} in the
260+
* provided {@link CommandContext}.
261+
*/
262+
Function<CommandContext, @Nullable RegistryHolder> WORLD_FROM_CAUSE_HOLDER_PROVIDER =
263+
in -> in.cause().first(ServerWorld.class).map(ServerWorld::registries).orElse(null);
264+
265+
/**
266+
* Adds an alternative function that retrieves a {@link RegistryHolder}
267+
* to attempt to get the selected {@link RegistryType}.
268+
*
269+
* <p>The order that these functions are added determines their
270+
* priority if there are multiple registries, specifically, the first
271+
* function that is provided will have the highest priority.</p>
272+
*
273+
* <p>Standard functions are available on {@link RegistryEntryBuilder}.</p>
274+
*
275+
* @param holderFunction The holder function
276+
* @return This, for chaining
277+
*/
278+
RegistryEntryBuilder<T> addHolderFunction(Function<CommandContext, @Nullable RegistryHolder> holderFunction);
279+
246280
/**
247281
* Adds a prefix that could be prepended to the input argument if it
248282
* initially does not match any of the chosen {@link RegistryKey}s. Any
@@ -622,46 +656,53 @@ public interface Factory {
622656
* and the provided {@link RegistryHolder}, which may be determined by
623657
* the current state of the {@link CommandContext}.
624658
*
625-
* <p>This element can only support <strong>one</strong>
626-
* {@link RegistryHolder}, due to the potential of conflicting
627-
* {@link ResourceKey resource keys} across multiple registries. If
628-
* testing multiple registries across multiple registry holders is
629-
* required, consider using {@link Parameter#firstOf(Iterable)} with
630-
* multiple versions of this parameter.</p>
631-
*
632-
* <p>{@link Game} and {@link Server} scoped {@link RegistryHolder}
633-
* providers are available via
634-
* {@link RegistryEntryBuilder#GLOBAL_HOLDER_PROVIDER} and
635-
* {@link RegistryEntryBuilder#SERVER_HOLDER_PROVIDER}</p>
659+
* <p>This element can support multiple functions that return
660+
* {@link RegistryHolder}s, however order matters, the {@link Registry}
661+
* from the first holder that is resolved will be used. Holders may be
662+
* added via {@link RegistryEntryBuilder#addHolderFunction(Function)}.
663+
* </p>
636664
*
637665
* @param <T> The type that the target {@link Registry} holds
638-
* @param holderProvider A {@link Function} that provides a
639-
* {@link RegistryHolder} based on the {@link CommandContext} up to
640-
* this parameter.
641666
* @param registryKey The {@link RegistryKey} that represents the target
642667
* {@link Registry} in the {@link RegistryHolder} provided via
643668
* {@code holderProvider}.
644669
* @return The {@link RegistryEntryBuilder}
645670
*/
646-
<T> RegistryEntryBuilder<T> createRegistryEntryBuilder(final Function<CommandContext, @Nullable RegistryHolder> holderProvider, final RegistryType<T> registryKey);
671+
<T> RegistryEntryBuilder<T> createRegistryEntryBuilder(final RegistryType<T> registryKey);
647672

648673
/**
649674
* Creates a {@link RegistryEntryBuilder} that retrieves objects from
650-
* the provided {@link Registry}, which provided via the given
651-
* {@link Function} which <strong>may</strong> use the current
652-
* {@link CommandContext} to determine the appropriate
653-
* {@link RegistryHolder} to retrieve the {@link Registry} from.
675+
* the {@link Registry} represented by the given {@link RegistryKey}
676+
* and the provided {@link RegistryHolder}, which may be determined by
677+
* the current state of the {@link CommandContext}.
654678
*
655-
* <p>When using a {@link RegistryTypes standard registry}, it is
656-
* recommended that consumers use
657-
* {@link #createRegistryEntryBuilder(Function, RegistryType)}
658-
* instead, providing the appropriate {@link RegistryHolder} instead.</p>
679+
* <p>This element can support multiple functions that return
680+
* {@link RegistryHolder}s, however order matters, the {@link Registry}
681+
* from the first holder that is resolved will be used. Beyond the
682+
* holders provided in this function, additional functions that resolve
683+
* holders can be added through
684+
* {@link RegistryEntryBuilder#addHolderFunction(Function)}.</p>
685+
*
686+
* <p>{@link Game} and {@link Server} scoped {@link RegistryHolder}
687+
* providers are available via
688+
* {@link RegistryEntryBuilder#GLOBAL_HOLDER_PROVIDER} and
689+
* {@link RegistryEntryBuilder#SERVER_HOLDER_PROVIDER}. {@link World}
690+
* scoped providers are available via
691+
* {@link RegistryEntryBuilder#WORLD_FROM_CAUSE_HOLDER_PROVIDER} and
692+
* {@link RegistryEntryBuilder#WORLD_FROM_LOCATABLE_HOLDER_PROVIDER}.</p>
659693
*
660694
* @param <T> The type that the target {@link Registry} holds
661-
* @param registryProvider The {@link Registry} to get the values from
695+
* @param holderProvider A {@link Function} that provides a
696+
* {@link RegistryHolder} based on the {@link CommandContext} up to
697+
* this parameter.
698+
* @param registryKey The {@link RegistryKey} that represents the target
699+
* {@link Registry} in the {@link RegistryHolder} provided via
700+
* {@code holderProvider}.
662701
* @return The {@link RegistryEntryBuilder}
663702
*/
664-
<T> RegistryEntryBuilder<T> createRegistryEntryBuilder(final Function<CommandContext, @Nullable ? extends Registry<? extends T>> registryProvider);
703+
<T> RegistryEntryBuilder<T> createRegistryEntryBuilder(
704+
final List<Function<CommandContext, @Nullable RegistryHolder>> holderProvider,
705+
final RegistryType<T> registryKey);
665706

666707
/**
667708
* Creates a {@link RegistryEntryBuilder} that retrieves objects from

src/main/java/org/spongepowered/api/event/cause/entity/damage/DamageType.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package org.spongepowered.api.event.cause.entity.damage;
2626

27+
import org.spongepowered.api.Sponge;
2728
import org.spongepowered.api.entity.Entity;
2829
import org.spongepowered.api.event.cause.entity.damage.source.DamageSource;
2930
import org.spongepowered.api.registry.DefaultedRegistryValue;
@@ -43,4 +44,26 @@
4344
@CatalogedBy(DamageTypes.class)
4445
public interface DamageType extends DefaultedRegistryValue, Nameable {
4546

47+
/**
48+
* Creates a new {@link Builder builder} to build a {@link DamageType}.
49+
*
50+
* @return A new builder
51+
*/
52+
static Builder builder() {
53+
return Sponge.game().builderProvider().provide(Builder.class);
54+
}
55+
56+
/**
57+
* A builder to create {@link DamageType}s.
58+
*/
59+
interface Builder extends org.spongepowered.api.util.Builder<DamageType, Builder> {
60+
61+
/**
62+
* Sets the name of the {@link DamageType}.
63+
*
64+
* @param name The name
65+
* @return This builder, for chaining
66+
*/
67+
Builder name(String name);
68+
}
4669
}

src/main/java/org/spongepowered/api/registry/RegistryTypes.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
import org.spongepowered.api.world.generation.structure.Structure;
146146
import org.spongepowered.api.world.portal.PortalType;
147147
import org.spongepowered.api.world.schematic.PaletteType;
148+
import org.spongepowered.api.world.server.TicketType;
148149
import org.spongepowered.api.world.teleport.TeleportHelperFilter;
149150
import org.spongepowered.api.world.weather.WeatherType;
150151

@@ -349,6 +350,8 @@ public final class RegistryTypes {
349350

350351
public static final DefaultedRegistryType<PortionType> PORTION_TYPE = RegistryTypes.spongeKeyInGame("portion_type");
351352

353+
public static final DefaultedRegistryType<QueryType> QUERY_TYPE = RegistryTypes.spongeKeyInGame("query_type");
354+
352355
public static final DefaultedRegistryType<RabbitType> RABBIT_TYPE = RegistryTypes.spongeKeyInGame("rabbit_type");
353356

354357
public static final DefaultedRegistryType<RaidStatus> RAID_STATUS = RegistryTypes.spongeKeyInGame("raid_status");
@@ -379,14 +382,14 @@ public final class RegistryTypes {
379382

380383
public static final DefaultedRegistryType<TeleportHelperFilter> TELEPORT_HELPER_FILTER = RegistryTypes.spongeKeyInGame("teleport_helper_filter");
381384

385+
public static final DefaultedRegistryType<TicketType<?>> TICKET_TYPE = RegistryTypes.spongeKeyInGame("ticket_type");
386+
382387
public static final DefaultedRegistryType<TransactionType> TRANSACTION_TYPE = RegistryTypes.spongeKeyInGame("transaction_type");
383388

384389
public static final DefaultedRegistryType<Trigger<?>> TRIGGER = RegistryTypes.spongeKeyInGame("trigger");
385390

386391
public static final DefaultedRegistryType<TropicalFishShape> TROPICAL_FISH_SHAPE = RegistryTypes.spongeKeyInGame("tropical_fish_shape");
387392

388-
public static final DefaultedRegistryType<QueryType> QUERY_TYPE = RegistryTypes.spongeKeyInGame("query_type");
389-
390393
public static final DefaultedRegistryType<Visibility> VISIBILITY = RegistryTypes.spongeKeyInGame("visibility");
391394

392395
public static final DefaultedRegistryType<WeatherType> WEATHER_TYPE = RegistryTypes.spongeKeyInGame("weather_type");

0 commit comments

Comments
 (0)