Skip to content

Commit 150593f

Browse files
committed
First stab at a Ticket Manager
1 parent ab6920b commit 150593f

File tree

6 files changed

+361
-2
lines changed

6 files changed

+361
-2
lines changed

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");
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
}

src/main/java/org/spongepowered/api/world/server/ServerWorld.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,12 @@ default boolean restoreSnapshot(final Vector3i position, final BlockSnapshot sna
280280
* @return The raid at that location, if present
281281
*/
282282
Optional<Raid> raidAt(Vector3i blockPosition);
283+
284+
/**
285+
* Gets the {@link ChunkManager} for this world.
286+
*
287+
* @return The chunk manager.
288+
*/
289+
ChunkManager chunkManager();
290+
283291
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
/**
28+
* Represents a chunk loading ticket.
29+
*
30+
* @param <T> The type of value this ticket holds.
31+
*/
32+
public interface Ticket<T> {
33+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.util.Ticks;
28+
import org.spongepowered.api.util.annotation.CatalogedBy;
29+
30+
/**
31+
* Represents a type of {@link Ticket chunk loading ticket} that can be obtained
32+
* to ensure chunks remain loaded in a {@link ServerWorld}.
33+
*
34+
* @param <T> The type of value that is associated with a {@link Ticket} of this
35+
* type.
36+
*/
37+
@CatalogedBy(TicketTypes.class)
38+
public interface TicketType<T> {
39+
40+
/**
41+
* Gets the lifetime of any {@link Ticket tickets} of this type.
42+
*
43+
* @return The number of {@link Ticks} any {@link Ticket tickets} of this
44+
* type will be valid for.
45+
*/
46+
Ticks lifetime();
47+
48+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.ResourceKey;
28+
import org.spongepowered.api.Sponge;
29+
import org.spongepowered.api.entity.Entity;
30+
import org.spongepowered.api.event.cause.entity.SpawnType;
31+
import org.spongepowered.api.registry.DefaultedRegistryReference;
32+
import org.spongepowered.api.registry.RegistryKey;
33+
import org.spongepowered.api.registry.RegistryTypes;
34+
import org.spongepowered.api.util.Ticks;
35+
import org.spongepowered.api.world.chunk.Chunk;
36+
import org.spongepowered.api.world.portal.Portal;
37+
import org.spongepowered.math.vector.Vector3i;
38+
39+
import java.util.function.Supplier;
40+
41+
/**
42+
* Types of {@link Ticket tickets} that can be requested via the
43+
* {@link ChunkManager}.
44+
*/
45+
public final class TicketTypes {
46+
47+
// @formatter:off
48+
49+
// SORTFIELDS:ON
50+
51+
/**
52+
* Represents {@link Ticket tickets} that ensures the target
53+
* {@link Chunk chunks} are loaded, but are not guaranteed to be loaded at
54+
* any time in the future, that is, the lifetime of such a ticket is
55+
* effectively one {@link Ticks tick}.
56+
*
57+
* <p>The position represented by the {@link Vector3i} is a <strong>chunk
58+
* position</strong>, not a block position, so when requesting a ticket
59+
* using {@link ChunkManager#requestTicket( TicketType, Vector3i, Object, int)},
60+
* the second and third parameter should be the same.</p>
61+
*/
62+
public static final DefaultedRegistryReference<TicketType<Vector3i>> STANDARD = TicketTypes.key(ResourceKey.sponge("standard"));
63+
64+
/**
65+
* Represents {@link Ticket tickets} that are intended to ensure that the
66+
* target {@link Chunk chunks} around a {@link Portal} are loaded, ready to
67+
* accept {@link Entity entities} that travel through it.
68+
*
69+
* <p>The position represented by the {@link Vector3i} is a <strong>block
70+
* position</strong>, specifically, it is intended it is that of the exit
71+
* point of a portal. When requesting a ticket using
72+
* {@link ChunkManager#requestTicket( TicketType, Vector3i, Object, int)},
73+
* the second parameter represents a chunk position, the third parameter
74+
* represents a block position.</p>
75+
*/
76+
public static final DefaultedRegistryReference<TicketType<Vector3i>> PORTAL = TicketTypes.key(ResourceKey.sponge("portal"));
77+
78+
/**
79+
* Represents {@link Ticket tickets} that are intended to ensure that the
80+
* target {@link Chunk chunks} around an {@link Entity} are loaded after
81+
* teleportation.
82+
*/
83+
public static final DefaultedRegistryReference<TicketType<Entity>> POST_TELEPORT = TicketTypes.key(ResourceKey.sponge("post_teleport"));
84+
85+
// SORTFIELDS:OFF
86+
87+
// @formatter:on
88+
89+
private TicketTypes() {
90+
}
91+
92+
private static <T> DefaultedRegistryReference<TicketType<T>> key(final ResourceKey location) {
93+
return RegistryKey.of(RegistryTypes.TICKET_TYPE, location).asDefaultedReference(() -> Sponge.game().registries());
94+
}
95+
96+
}

0 commit comments

Comments
 (0)