Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement creation of custom TicketTypes. #3834

Merged
merged 4 commits into from
May 18, 2024
Merged
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 @@ -26,12 +26,16 @@

import net.minecraft.server.level.TicketType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;
import org.spongepowered.common.UntransformedInvokerError;

@Mixin(TicketType.class)
public interface TicketTypeAccessor {
import java.util.Comparator;

@Accessor("name")
String accessor$name();
@Mixin(TicketType.class)
public interface TicketTypeAccessor<T> {

@Invoker("<init>")
static <T> TicketType<T> accessor$createInstance(final String name, final Comparator<T> comparator, final long lifetime) {
throw new UntransformedInvokerError();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
import org.spongepowered.api.world.generation.structure.jigsaw.ProcessorListTemplate;
import org.spongepowered.api.world.schematic.PaletteType;
import org.spongepowered.api.world.schematic.Schematic;
import org.spongepowered.api.world.server.TicketType;
import org.spongepowered.api.world.server.WorldTemplate;
import org.spongepowered.api.world.volume.stream.StreamOptions;
import org.spongepowered.common.advancement.SpongeAdvancementBuilder;
Expand Down Expand Up @@ -235,6 +236,7 @@
import org.spongepowered.common.world.schematic.SpongePaletteTypeBuilder;
import org.spongepowered.common.world.schematic.SpongeSchematicBuilder;
import org.spongepowered.common.world.server.SpongeLocatableBlockBuilder;
import org.spongepowered.common.world.server.SpongeTicketTypeBuilder;
import org.spongepowered.common.world.server.SpongeWorldTemplate;
import org.spongepowered.common.world.server.SpongeWorldTypeTemplate;
import org.spongepowered.common.world.volume.stream.SpongeStreamOptionsBuilder;
Expand Down Expand Up @@ -386,6 +388,7 @@ public void registerDefaultBuilders() {
.register(JigsawPoolTemplate.Builder.class, SpongeJigsawPoolTemplate.BuilderImpl::new)
.register(ChatTypeTemplate.Builder.class, SpongeChatTypeTemplate.BuilderImpl::new)
.register(DamageTypeTemplate.Builder.class, SpongeDamageTypeTemplate.BuilderImpl::new)
.register(TicketType.Builder.class, SpongeTicketTypeBuilder::new)
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public static void registerEarlyGlobalRegistries(final SpongeRegistryHolder hold
holder.createFrozenRegistry(RegistryTypes.RESOLVE_OPERATION, SpongeRegistryLoader.resolveOperation());
holder.createFrozenRegistry(RegistryTypes.SKIN_PART, SpongeRegistryLoader.skinPart());
holder.createFrozenRegistry(RegistryTypes.SPAWN_TYPE, SpongeRegistryLoader.spawnType());
holder.createFrozenRegistry(RegistryTypes.TICKET_TYPE, SpongeRegistryLoader.ticketType());
holder.createFrozenRegistry(RegistryTypes.TRANSACTION_TYPE, SpongeRegistryLoader.transactionType());
holder.createFrozenRegistry(RegistryTypes.WEATHER_TYPE, SpongeRegistryLoader.weather());
holder.createFrozenRegistry(RegistryTypes.DATA_FORMAT, SpongeRegistryLoader.dataFormat());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@
import org.spongepowered.api.world.portal.PortalTypes;
import org.spongepowered.api.world.schematic.PaletteType;
import org.spongepowered.api.world.schematic.PaletteTypes;
import org.spongepowered.api.world.server.TicketType;
import org.spongepowered.api.world.server.TicketTypes;
import org.spongepowered.api.world.weather.WeatherType;
import org.spongepowered.api.world.weather.WeatherTypes;
import org.spongepowered.common.accessor.world.level.levelgen.NoiseSettingsAccessor;
Expand Down Expand Up @@ -158,18 +156,14 @@
import org.spongepowered.common.map.decoration.orientation.SpongeMapDecorationOrientation;
import org.spongepowered.common.registry.RegistryLoader;
import org.spongepowered.common.util.SpongeOrientation;
import org.spongepowered.common.util.VecHelper;
import org.spongepowered.common.world.SpongeChunkRegenerateFlag;
import org.spongepowered.common.world.portal.EndPortalType;
import org.spongepowered.common.world.portal.NetherPortalType;
import org.spongepowered.common.world.portal.UnknownPortalType;
import org.spongepowered.common.world.schematic.SpongePaletteType;
import org.spongepowered.common.world.server.SpongeTicketType;
import org.spongepowered.common.world.weather.SpongeWeatherType;
import org.spongepowered.math.vector.Vector3d;
import org.spongepowered.math.vector.Vector3i;

import java.util.Comparator;
import java.util.function.Function;

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -489,14 +483,6 @@ public static RegistryLoader<SpawnType> spawnType() {
));
}

public static RegistryLoader<TicketType<?>> ticketType() {
return RegistryLoader.of(l -> {
l.add(TicketTypes.STANDARD, k -> new SpongeTicketType<Vector3i>("standard", Comparator.comparingLong(x -> VecHelper.toChunkPos(x).toLong()), 1));
l.add(TicketTypes.PORTAL, k -> (TicketType<?>) net.minecraft.server.level.TicketType.PORTAL);
l.add(TicketTypes.POST_TELEPORT, k -> (TicketType<?>) net.minecraft.server.level.TicketType.POST_TELEPORT);
});
}

public static RegistryLoader<TransactionType> transactionType() {
return RegistryLoader.of(l -> l.mapping(SpongeTransactionType::new, m -> {
m.add(TransactionTypes.DEPOSIT);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/spongepowered/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ public static final class ChunkTicket {

// Highest ticket level that will cause loading a full chunk, plus one.
public static final int MAX_FULL_CHUNK_DISTANCE = ChunkTicket.MAX_FULL_CHUNK_TICKET_LEVEL + 1;

public static final int INFINITE_TIMEOUT = 0;
}

public static final class Networking {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* This file is part of Sponge, 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.common.world.server;

import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.util.Ticks;
import org.spongepowered.api.world.server.TicketType;
import org.spongepowered.common.accessor.server.level.TicketTypeAccessor;
import org.spongepowered.common.util.Constants;

import java.util.Comparator;
import java.util.Objects;

@SuppressWarnings("unchecked")
public final class SpongeTicketTypeBuilder<T> implements TicketType.Builder<T> {

private @MonotonicNonNull String name;
private @Nullable Comparator<T> comparator;
private @MonotonicNonNull Ticks lifetime;

@Override
public TicketType.Builder<T> reset() {
this.name = null;
this.comparator = null;
this.lifetime = null;
return this;
}

@Override
public TicketType.Builder<T> name(final String name) {
this.name = Objects.requireNonNull(name, "Name cannot null");
return this;
}

@Override
public TicketType.Builder<T> comparator(final @Nullable Comparator<T> comparator) {
this.comparator = comparator;
return this;
}

@Override
public TicketType.Builder<T> lifetime(final Ticks lifetime) {
Objects.requireNonNull(lifetime, "Lifetime cannot be null");
if (!lifetime.isInfinite() && lifetime.ticks() <= 0) {
throw new IllegalArgumentException("The lifetime is required to be a positive integer");
}
this.lifetime = lifetime;
return this;
}

@Override
public TicketType<T> build() {
Objects.requireNonNull(this.name, "Name cannot null");
Objects.requireNonNull(this.lifetime, "Lifetime cannot be null");
if (this.comparator == null) {
this.comparator = (v1, v2) -> 0;
}

return (TicketType<T>) TicketTypeAccessor.accessor$createInstance(this.name, this.comparator, this.lifetime.isInfinite()
? Constants.ChunkTicket.INFINITE_TIMEOUT
: this.lifetime.ticks());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,27 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.util.Constants;

@Mixin(net.minecraft.server.level.TicketType.class)
public abstract class TicketTypeMixin_API<T> implements TicketType<T> {

// @formatter:off
@Shadow @Final private String name;
@Shadow @Final private long timeout;

// @formatter:on

@Override
public String name() {
return this.name;
}

@Override
@NonNull
public Ticks lifetime() {
return Ticks.of(this.timeout);
return this.timeout == Constants.ChunkTicket.INFINITE_TIMEOUT
? Ticks.infinite()
: Ticks.of(this.timeout);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.spongepowered.common.accessor.server.level.TicketAccessor;
import org.spongepowered.common.bridge.world.DistanceManagerBridge;
import org.spongepowered.common.bridge.world.server.TicketBridge;
import org.spongepowered.common.bridge.world.server.TicketTypeBridge;
import org.spongepowered.common.util.Constants;
import org.spongepowered.common.util.SpongeTicks;
import org.spongepowered.common.util.VecHelper;
Expand Down Expand Up @@ -102,12 +101,8 @@ public abstract class DistanceManagerMixin implements DistanceManagerBridge {
final ServerWorld world, final org.spongepowered.api.world.server.TicketType<T> ticketType,
final Vector3i pos, final T value, final int distanceLimit) {
final int distance = Mth.clamp(Constants.ChunkTicket.MAX_FULL_CHUNK_DISTANCE - distanceLimit, 0, Constants.ChunkTicket.MAX_FULL_CHUNK_TICKET_LEVEL);
final TicketType<S> type = (net.minecraft.server.level.TicketType<S>) ticketType;
final net.minecraft.server.level.Ticket<S> ticketToRequest =
TicketAccessor.accessor$createInstance(
type,
distance,
((TicketTypeBridge<S, T>) ticketType).bridge$convertToNativeType(value));
final TicketType<T> type = (net.minecraft.server.level.TicketType<T>) ticketType;
final net.minecraft.server.level.Ticket<T> ticketToRequest = TicketAccessor.accessor$createInstance(type, distance, value);
this.shadow$addTicket(VecHelper.toChunkPos(pos).toLong(), ticketToRequest);
return Optional.of(((TicketBridge) (Object) ticketToRequest).bridge$retrieveAppropriateTicket());
}
Expand All @@ -127,7 +122,7 @@ public abstract class DistanceManagerMixin implements DistanceManagerBridge {
@Override
public <T> Collection<Ticket<T>> bridge$tickets(final org.spongepowered.api.world.server.TicketType<T> ticketType) {
return this.tickets.values().stream()
.flatMap(x -> x.stream().filter(ticket -> ticket.getType().equals(ticketType)))
.flatMap(x -> x.stream().filter(ticket -> ticket.getType() == ticketType))
.map(x -> (Ticket<T>) (Object) x)
.collect(Collectors.toList());
}
Expand Down
Loading
Loading