Skip to content
Open
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 @@ -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;
Expand Down Expand Up @@ -294,6 +295,8 @@ public final class RegistryTypes {

public static final DefaultedRegistryType<StructureType> STRUCTURE_TYPE = RegistryTypes.minecraftKeyInGame("worldgen/structure_type");

public static final DefaultedRegistryType<TicketType> TICKET_TYPE = RegistryTypes.minecraftKeyInGame("ticket_type");

public static final DefaultedRegistryType<Trigger<?>> TRIGGER = RegistryTypes.minecraftKeyInGame("trigger_type");

public static final DefaultedRegistryType<TrimMaterial> TRIM_MATERIAL = RegistryTypes.minecraftKeyInServer("trim_material");
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/org/spongepowered/api/world/server/ChunkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -56,39 +56,37 @@ 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}.
*
* @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 <T> The type of the supplied {@code value}.
* @return The ticket, if granted.
*/
<T> Optional<Ticket<T>> requestTicket(TicketType<T> 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.
*
* <p>If this ticket is no longer valid, it cannot be renewed. Instead,
* you should {@link #requestTicket(TicketType, Vector3i, Object, int)} a new one.</p>
* you should {@link #requestTicket(TicketType, Vector3i, int)} a new one.</p>
*
* @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
Expand All @@ -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 <T> The type of value the {@link Ticket} holds
* @return A {@link Collection} of {@link Ticket tickets}
*/
<T> Collection<Ticket<T>> findTickets(TicketType<T> type);
default Collection<Ticket> 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<Ticket> findTickets(Predicate<TicketType> typePredicate);

/**
* Regenerates a chunk at the given chunk coordinate position.
Expand Down
28 changes: 25 additions & 3 deletions src/main/java/org/spongepowered/api/world/server/Ticket.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> The type of value this ticket holds.
*/
public interface Ticket<T> {
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();
}
87 changes: 58 additions & 29 deletions src/main/java/org/spongepowered/api/world/server/TicketType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> The type of value that is associated with a {@link Ticket} of this
* type.
*/
@SuppressWarnings("unchecked")
public interface TicketType<T> {
@CatalogedBy(TicketTypes.class)
public interface TicketType extends DefaultedRegistryValue<TicketType> {

static <T> Builder<T> 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.
Expand All @@ -61,41 +77,54 @@ static <T> Builder<T> builder() {
*/
Ticks lifetime();

interface Builder<T> extends ResettableBuilder<T, Builder<T>> {
interface Builder extends ResettableBuilder<TicketType, Builder> {

/**
* 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<T> 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.
* <p>
* 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<T> comparator(@Nullable Comparator<T> comparator);
Builder canExpireIfUnloaded(boolean canExpireIfUnloaded);

/**
* Sets the lifetime of the {@link TicketType type}.
*
* @param lifetime The lifetime
* @return The builder, for chaining
*/
Builder<T> lifetime(Ticks lifetime);
Builder lifetime(Ticks lifetime);

/**
* Builds a new {@link TicketType type}.
*
* @return The type
*/
TicketType<T> build();
TicketType build();
}
}
71 changes: 71 additions & 0 deletions src/main/java/org/spongepowered/api/world/server/TicketTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* This file is part of SpongeAPI, 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.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;

/**
* <!-- This file is automatically generated. Any manual changes will be overwritten. -->
*/
@SuppressWarnings("unused")
@RegistryScopes(scopes = RegistryScope.GAME)
public final class TicketTypes {

public static final DefaultedRegistryReference<TicketType> DRAGON = TicketTypes.key(ResourceKey.minecraft("dragon"));

public static final DefaultedRegistryReference<TicketType> ENDER_PEARL = TicketTypes.key(ResourceKey.minecraft("ender_pearl"));

public static final DefaultedRegistryReference<TicketType> FORCED = TicketTypes.key(ResourceKey.minecraft("forced"));

public static final DefaultedRegistryReference<TicketType> PLAYER_LOADING = TicketTypes.key(ResourceKey.minecraft("player_loading"));

public static final DefaultedRegistryReference<TicketType> PLAYER_SIMULATION = TicketTypes.key(ResourceKey.minecraft("player_simulation"));

public static final DefaultedRegistryReference<TicketType> PLAYER_SPAWN = TicketTypes.key(ResourceKey.minecraft("player_spawn"));

public static final DefaultedRegistryReference<TicketType> PORTAL = TicketTypes.key(ResourceKey.minecraft("portal"));

public static final DefaultedRegistryReference<TicketType> SPAWN_SEARCH = TicketTypes.key(ResourceKey.minecraft("spawn_search"));

public static final DefaultedRegistryReference<TicketType> UNKNOWN = TicketTypes.key(ResourceKey.minecraft("unknown"));

private TicketTypes() {
}

public static Registry<TicketType> registry() {
return Sponge.game().registry(RegistryTypes.TICKET_TYPE);
}

private static DefaultedRegistryReference<TicketType> key(final ResourceKey location) {
return RegistryKey.of(RegistryTypes.TICKET_TYPE, location).asDefaultedReference(Sponge::game);
}
}
Loading