diff --git a/src/main/java/org/spongepowered/api/registry/RegistryTypes.java b/src/main/java/org/spongepowered/api/registry/RegistryTypes.java index b50e35caa9..3a9dc5f3a4 100644 --- a/src/main/java/org/spongepowered/api/registry/RegistryTypes.java +++ b/src/main/java/org/spongepowered/api/registry/RegistryTypes.java @@ -197,6 +197,7 @@ import org.spongepowered.api.world.generation.structure.jigsaw.ProcessorList; import org.spongepowered.api.world.generation.structure.jigsaw.ProcessorType; import org.spongepowered.api.world.schematic.PaletteType; +import org.spongepowered.api.world.server.TicketType; import org.spongepowered.api.world.server.WorldArchetypeType; import org.spongepowered.api.world.teleport.TeleportHelperFilter; import org.spongepowered.api.world.weather.WeatherType; @@ -294,6 +295,8 @@ public final class RegistryTypes { public static final DefaultedRegistryType STRUCTURE_TYPE = RegistryTypes.minecraftKeyInGame("worldgen/structure_type"); + public static final DefaultedRegistryType TICKET_TYPE = RegistryTypes.minecraftKeyInGame("ticket_type"); + public static final DefaultedRegistryType> TRIGGER = RegistryTypes.minecraftKeyInGame("trigger_type"); public static final DefaultedRegistryType TRIM_MATERIAL = RegistryTypes.minecraftKeyInServer("trim_material"); diff --git a/src/main/java/org/spongepowered/api/world/server/ChunkManager.java b/src/main/java/org/spongepowered/api/world/server/ChunkManager.java index 9e3ebbfda5..2411f17d54 100644 --- a/src/main/java/org/spongepowered/api/world/server/ChunkManager.java +++ b/src/main/java/org/spongepowered/api/world/server/ChunkManager.java @@ -33,8 +33,8 @@ import java.util.Collection; import java.util.Objects; -import java.util.Optional; import java.util.concurrent.CompletableFuture; +import java.util.function.Predicate; /** * Manages {@link WorldChunk chunks} for a {@link ServerWorld}. @@ -56,14 +56,14 @@ public interface ChunkManager { * @param ticket The ticket to check. * @return true if so */ - boolean valid(Ticket ticket); + boolean valid(Ticket ticket); /** * Gets the {@link Ticks} remaining on the supplied ticket. * * @return The {@link Ticks} */ - Ticks timeLeft(Ticket ticket); + Ticks timeLeft(Ticket ticket); /** * Request a {@link Ticket} for the given {@link TicketType}. @@ -71,24 +71,22 @@ public interface ChunkManager { * @param type The type of ticket to request. * @param chunkOrigin The chunk co-ordinates of the central {@link WorldChunk} * affected by this {@link Ticket} - * @param value The value to register the ticket with. * @param radius The radius of the area, in chunks, that this {@link Ticket} * affects. - * @param The type of the supplied {@code value}. * @return The ticket, if granted. */ - Optional> requestTicket(TicketType type, Vector3i chunkOrigin, T value, int radius); + Ticket requestTicket(TicketType type, Vector3i chunkOrigin, int radius); /** * Attempts to renew this ticket, resetting the lifetime to the default. * *

If this ticket is no longer valid, it cannot be renewed. Instead, - * you should {@link #requestTicket(TicketType, Vector3i, Object, int)} a new one.

+ * you should {@link #requestTicket(TicketType, Vector3i, int)} a new one.

* * @param ticket The ticket to attempt to renew * @return {@code true} if successful */ - boolean renewTicket(Ticket ticket); + boolean renewTicket(Ticket ticket); /** * Releases the provided {@link Ticket}, allowing the chunk position @@ -97,17 +95,28 @@ public interface ChunkManager { * * @param ticket The ticket to release. */ - boolean releaseTicket(Ticket ticket); + boolean releaseTicket(Ticket ticket); /** * Gets all currently active {@link Ticket tickets} that are of the * provided {@link TicketType}. * * @param type The {@link TicketType} to retrieve tickets for - * @param The type of value the {@link Ticket} holds * @return A {@link Collection} of {@link Ticket tickets} */ - Collection> findTickets(TicketType type); + default Collection findTickets(TicketType type) { + Objects.requireNonNull(type, "type"); + return this.findTickets(type::equals); + } + + /** + * Gets all currently active {@link Ticket tickets} whose type + * matches the given predicate. + * + * @param typePredicate The predicate on {@link TicketType} + * @return A {@link Collection} of {@link Ticket tickets} + */ + Collection findTickets(Predicate typePredicate); /** * Regenerates a chunk at the given chunk coordinate position. diff --git a/src/main/java/org/spongepowered/api/world/server/Ticket.java b/src/main/java/org/spongepowered/api/world/server/Ticket.java index 20e85217f5..25e316ff10 100644 --- a/src/main/java/org/spongepowered/api/world/server/Ticket.java +++ b/src/main/java/org/spongepowered/api/world/server/Ticket.java @@ -24,10 +24,32 @@ */ package org.spongepowered.api.world.server; +import org.spongepowered.api.world.chunk.WorldChunk; +import org.spongepowered.math.vector.Vector3i; + /** * Represents a chunk loading ticket. - * - * @param The type of value this ticket holds. */ -public interface Ticket { +public interface Ticket { + + /** + * Gets the type of this ticket. + * + * @return The type + */ + TicketType type(); + + /** + * Gets the chunk co-ordinates of the central {@link WorldChunk} affected by this ticket. + * + * @return The chunk co-ordinates + */ + Vector3i chunkOrigin(); + + /** + * Gets the radius of this ticket. + * + * @return The radius + */ + int radius(); } diff --git a/src/main/java/org/spongepowered/api/world/server/TicketType.java b/src/main/java/org/spongepowered/api/world/server/TicketType.java index f16f082d40..2ee69086ff 100644 --- a/src/main/java/org/spongepowered/api/world/server/TicketType.java +++ b/src/main/java/org/spongepowered/api/world/server/TicketType.java @@ -24,34 +24,50 @@ */ package org.spongepowered.api.world.server; -import org.checkerframework.checker.nullness.qual.NonNull; -import org.checkerframework.checker.nullness.qual.Nullable; import org.spongepowered.api.Sponge; +import org.spongepowered.api.registry.DefaultedRegistryValue; import org.spongepowered.api.util.ResettableBuilder; import org.spongepowered.api.util.Ticks; - -import java.util.Comparator; +import org.spongepowered.api.util.annotation.CatalogedBy; /** * Represents a type of {@link Ticket chunk loading ticket} that can be obtained * to ensure chunks remain loaded in a {@link ServerWorld}. - * - * @param The type of value that is associated with a {@link Ticket} of this - * type. */ -@SuppressWarnings("unchecked") -public interface TicketType { +@CatalogedBy(TicketTypes.class) +public interface TicketType extends DefaultedRegistryValue { - static Builder builder() { + static Builder builder() { return Sponge.game().builderProvider().provide(Builder.class); } /** - * The name of this type. + * @return Whether tickets of this type are persisted when the world is shut down. + */ + boolean persists(); + + /** + * @return Whether tickets of this type will load chunks. + */ + boolean loadsChunks(); + + /** + * @return Whether tickets of this type will cause chunks to update their blocks and entities. + */ + boolean simulatesChunks(); + + /** + * Gets whether tickets of this type keeps the world active. + * The world may stop processing after being inactive for a certain amount of time. * - * @return The name + * @return Whether tickets of this type keeps the world active. + */ + boolean keepsWorldActive(); + + /** + * @return Whether tickets of this type count down their remaining ticks even their chunks are not loaded. */ - String name(); + boolean canExpireIfUnloaded(); /** * Gets the lifetime of any {@link Ticket tickets} of this type. @@ -61,27 +77,40 @@ static Builder builder() { */ Ticks lifetime(); - interface Builder extends ResettableBuilder> { + interface Builder extends ResettableBuilder { /** - * Sets the name of the {@link TicketType type}. - * - * @param name The name - * @return The builder, for chaining + * @param persists Whether tickets of this type are persisted when the world is shut down. + * @return This builder, for chaining */ - Builder name(@NonNull String name); + Builder persists(boolean persists); /** - * Sets the {@link Comparator comparator} used by requested {@link Ticket tickets} - * of this type used to determine the appropriate ticket to be used during collisions. - *

- * If this call is omitted, it is up to the implementation on how the comparison is handled. - * In the official implementation, the resulting comparison value is '0'. + * @param loadsChunks Whether tickets of this type will load chunks. + * @return This builder, for chaining + */ + Builder loadsChunks(boolean loadsChunks); + + /** + * @param simulatesChunks Whether tickets of this type will cause chunks to update their blocks and entities. + * @return This builder, for chaining + */ + Builder simulatesChunks(boolean simulatesChunks); + + /** + * Sets whether tickets of this type keeps the world active. + * The world may stop processing after being inactive for a certain amount of time. * - * @param comparator The comparator - * @return The builder, for chaining + * @param keepsWorldActive Whether tickets of this type keeps the world active. + * @return This builder, for chaining + */ + Builder keepsWorldActive(boolean keepsWorldActive); + + /** + * @param canExpireIfUnloaded Whether tickets of this type count down their remaining ticks even their chunks are not loaded. + * @return This builder, for chaining */ - Builder comparator(@Nullable Comparator comparator); + Builder canExpireIfUnloaded(boolean canExpireIfUnloaded); /** * Sets the lifetime of the {@link TicketType type}. @@ -89,13 +118,13 @@ interface Builder extends ResettableBuilder> { * @param lifetime The lifetime * @return The builder, for chaining */ - Builder lifetime(Ticks lifetime); + Builder lifetime(Ticks lifetime); /** * Builds a new {@link TicketType type}. * * @return The type */ - TicketType build(); + TicketType build(); } } diff --git a/src/main/java/org/spongepowered/api/world/server/TicketTypes.java b/src/main/java/org/spongepowered/api/world/server/TicketTypes.java new file mode 100644 index 0000000000..bb6fff8aac --- /dev/null +++ b/src/main/java/org/spongepowered/api/world/server/TicketTypes.java @@ -0,0 +1,71 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.world.server; + +import org.spongepowered.api.ResourceKey; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.registry.DefaultedRegistryReference; +import org.spongepowered.api.registry.Registry; +import org.spongepowered.api.registry.RegistryKey; +import org.spongepowered.api.registry.RegistryScope; +import org.spongepowered.api.registry.RegistryScopes; +import org.spongepowered.api.registry.RegistryTypes; + +/** + * + */ +@SuppressWarnings("unused") +@RegistryScopes(scopes = RegistryScope.GAME) +public final class TicketTypes { + + public static final DefaultedRegistryReference DRAGON = TicketTypes.key(ResourceKey.minecraft("dragon")); + + public static final DefaultedRegistryReference ENDER_PEARL = TicketTypes.key(ResourceKey.minecraft("ender_pearl")); + + public static final DefaultedRegistryReference FORCED = TicketTypes.key(ResourceKey.minecraft("forced")); + + public static final DefaultedRegistryReference PLAYER_LOADING = TicketTypes.key(ResourceKey.minecraft("player_loading")); + + public static final DefaultedRegistryReference PLAYER_SIMULATION = TicketTypes.key(ResourceKey.minecraft("player_simulation")); + + public static final DefaultedRegistryReference PLAYER_SPAWN = TicketTypes.key(ResourceKey.minecraft("player_spawn")); + + public static final DefaultedRegistryReference PORTAL = TicketTypes.key(ResourceKey.minecraft("portal")); + + public static final DefaultedRegistryReference SPAWN_SEARCH = TicketTypes.key(ResourceKey.minecraft("spawn_search")); + + public static final DefaultedRegistryReference UNKNOWN = TicketTypes.key(ResourceKey.minecraft("unknown")); + + private TicketTypes() { + } + + public static Registry registry() { + return Sponge.game().registry(RegistryTypes.TICKET_TYPE); + } + + private static DefaultedRegistryReference key(final ResourceKey location) { + return RegistryKey.of(RegistryTypes.TICKET_TYPE, location).asDefaultedReference(Sponge::game); + } +}