|
| 1 | +/* |
| 2 | + * This file is part of SpongeAPI, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.api.world.server; |
| 26 | + |
| 27 | +import org.spongepowered.api.registry.DefaultedRegistryReference; |
| 28 | +import org.spongepowered.api.util.Ticks; |
| 29 | +import org.spongepowered.api.util.annotation.DoNotStore; |
| 30 | +import org.spongepowered.api.world.chunk.Chunk; |
| 31 | +import org.spongepowered.math.vector.Vector3i; |
| 32 | + |
| 33 | +import java.util.Collection; |
| 34 | +import java.util.Optional; |
| 35 | + |
| 36 | +/** |
| 37 | + * Manages {@link Chunk chunks} for a {@link ServerWorld}. |
| 38 | + */ |
| 39 | +@DoNotStore |
| 40 | +public interface ChunkManager { |
| 41 | + |
| 42 | + /** |
| 43 | + * Gets the {@link ServerWorld} this manager operates upon. |
| 44 | + * |
| 45 | + * @return The {@link ServerWorld} |
| 46 | + */ |
| 47 | + ServerWorld world(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Checks if the provided {@link Ticket} is valid for the world this manager |
| 51 | + * represents. |
| 52 | + * |
| 53 | + * @param ticket The ticket to check. |
| 54 | + * @return true if so |
| 55 | + */ |
| 56 | + boolean valid(Ticket<?> ticket); |
| 57 | + |
| 58 | + /** |
| 59 | + * Gets the {@link Ticks} remaining on the supplied ticket. |
| 60 | + * |
| 61 | + * @return The {@link Ticks} |
| 62 | + */ |
| 63 | + Ticks timeLeft(Ticket<?> ticket); |
| 64 | + |
| 65 | + /** |
| 66 | + * Request a {@link Ticket} for a given {@link TicketType} that supports a |
| 67 | + * chunk position. |
| 68 | + * |
| 69 | + * @param type The type of ticket to request. |
| 70 | + * @param chunkOrigin The chunk co-ordinates of the central {@link Chunk} |
| 71 | + * affected by this {@link Ticket} |
| 72 | + * @param radius The radius of the area, in chunks, that this {@link Ticket} |
| 73 | + * affects. |
| 74 | + * @return The ticket, if granted. |
| 75 | + */ |
| 76 | + default Optional<Ticket<Vector3i>> requestTicket(final DefaultedRegistryReference<TicketType<Vector3i>> type, |
| 77 | + final Vector3i chunkOrigin, final int radius) { |
| 78 | + return this.requestTicket(type.get(), chunkOrigin, radius); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Request a {@link Ticket} for a given {@link TicketType} that supports a |
| 83 | + * chunk position. |
| 84 | + * |
| 85 | + * @param type The type of ticket to request. |
| 86 | + * @param chunkOrigin The chunk co-ordinates of the central {@link Chunk} |
| 87 | + * affected by this {@link Ticket} |
| 88 | + * @param radius The radius of the area, in chunks, that this {@link Ticket} |
| 89 | + * affects. |
| 90 | + * @return The ticket, if granted. |
| 91 | + */ |
| 92 | + default Optional<Ticket<Vector3i>> requestTicket(final TicketType<Vector3i> type, final Vector3i chunkOrigin, |
| 93 | + final int radius) { |
| 94 | + return this.requestTicket(type, chunkOrigin, chunkOrigin, radius); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Request a {@link Ticket} for the given {@link TicketType}. |
| 99 | + * |
| 100 | + * @param type The type of ticket to request. |
| 101 | + * @param chunkOrigin The chunk co-ordinates of the central {@link Chunk} |
| 102 | + * affected by this {@link Ticket} |
| 103 | + * @param value The value to register the ticket with. |
| 104 | + * @param radius The radius of the area, in chunks, that this {@link Ticket} |
| 105 | + * affects. |
| 106 | + * @param <T> The type of the supplied {@code value}. |
| 107 | + * @return The ticket, if granted. |
| 108 | + */ |
| 109 | + <T> Optional<Ticket<T>> requestTicket(TicketType<T> type, Vector3i chunkOrigin, T value, int radius); |
| 110 | + |
| 111 | + /** |
| 112 | + * Request a {@link Ticket} for the given {@link TicketType}. |
| 113 | + * |
| 114 | + * @param type The type of ticket to request. |
| 115 | + * @param chunkOrigin The chunk co-ordinates of the central {@link Chunk} |
| 116 | + * affected by this {@link Ticket} |
| 117 | + * @param value The value to register the ticket with. |
| 118 | + * @param radius The radius of the area, in chunks, that this {@link Ticket} |
| 119 | + * affects. This may be capped - for the Vanilla impl, this is |
| 120 | + * limited to a radius of 33 chunks. |
| 121 | + * @param <T> The type of the supplied {@code value}. |
| 122 | + * @return The ticket, if granted. |
| 123 | + */ |
| 124 | + default <T> Optional<Ticket<T>> requestTicket(final DefaultedRegistryReference<TicketType<T>> type, |
| 125 | + final Vector3i chunkOrigin, final T value, final int radius) { |
| 126 | + return this.requestTicket(type.get(), chunkOrigin, value, radius); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Attempts to renew this ticket, resetting the lifetime to the default. |
| 131 | + * |
| 132 | + * <p>If this ticket is no longer valid, it cannot be renewed. Instead, |
| 133 | + * you should {@link #requestTicket(TicketType, Vector3i, Object, int)} a new one.</p> |
| 134 | + * |
| 135 | + * @param ticket The ticket to attempt to renew |
| 136 | + * @return {@code true} if successful |
| 137 | + */ |
| 138 | + boolean renewTicket(Ticket<?> ticket); |
| 139 | + |
| 140 | + /** |
| 141 | + * Releases the provided {@link Ticket}, allowing the chunk position |
| 142 | + * represented by the given ticket to be unloaded (if it is not being kept |
| 143 | + * loaded by other means). |
| 144 | + * |
| 145 | + * @param ticket The ticket to release. |
| 146 | + */ |
| 147 | + boolean releaseTicket(Ticket<?> ticket); |
| 148 | + |
| 149 | + /** |
| 150 | + * Gets all currently active {@link Ticket tickets} that are of the |
| 151 | + * provided {@link TicketType}. |
| 152 | + * |
| 153 | + * @param type The {@link TicketType} to retrieve tickets for |
| 154 | + * @param <T> The type of value the {@link Ticket} holds |
| 155 | + * @return A {@link Collection} of {@link Ticket tickets} |
| 156 | + */ |
| 157 | + <T> Collection<Ticket<T>> findTickets(TicketType<T> type); |
| 158 | + |
| 159 | + /** |
| 160 | + * Gets all currently active {@link Ticket tickets} that are of the |
| 161 | + * provided {@link TicketType}. |
| 162 | + * |
| 163 | + * @param type The {@link TicketType} to retrieve tickets for |
| 164 | + * @param <T> The type of value the {@link Ticket} holds |
| 165 | + * @return A {@link Collection} of {@link Ticket tickets} |
| 166 | + */ |
| 167 | + default <T> Collection<Ticket<T>> findTickets(final DefaultedRegistryReference<TicketType<T>> type) { |
| 168 | + return this.findTickets(type.get()); |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments