Skip to content

Commit

Permalink
Merge SpongeTicketType into TicketType
Browse files Browse the repository at this point in the history
  • Loading branch information
dualspiral committed Jan 3, 2021
1 parent 33a9ff3 commit 009baa0
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 32 deletions.
2 changes: 1 addition & 1 deletion SpongeAPI
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public interface TicketAccessor<T> {
throw new AssertionError("This shouldn't be callable");
}

@Accessor("timestamp") long accessor$getTimestamp();
@Accessor("createdTick") long accessor$createdTick();

@Accessor("value") T accessor$getValue();
@Accessor("key") T accessor$key();

@Invoker("isExpired") boolean accessor$isExpired(long currentTimestamp);
@Invoker("timedOut") boolean invoker$timedOut(long currentTimestamp);

@Invoker("setTimestamp") void accessor$setTimestamp(long timestamp);
@Invoker("setCreatedTick") void invoker$setCreatedTick(long timestamp);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.spongepowered.api.util.Ticks;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.api.world.server.Ticket;
import org.spongepowered.common.world.server.SpongeTicketType;
import org.spongepowered.api.world.server.TicketType;
import org.spongepowered.math.vector.Vector3i;

import java.util.Collection;
Expand All @@ -39,12 +39,12 @@ public interface TicketManagerBridge {

Ticks bridge$getTimeLeft(Ticket<?> ticket);

<S, T> Optional<Ticket<T>> bridge$registerTicket(ServerWorld world, SpongeTicketType<S, T> ticketType, Vector3i pos, T value, int distanceLimit);
<S, T> Optional<Ticket<T>> bridge$registerTicket(ServerWorld world, TicketType<T> ticketType, Vector3i pos, T value, int distanceLimit);

boolean bridge$renewTicket(Ticket<?> ticket);

boolean bridge$releaseTicket(Ticket<?> ticket);

<S, T> Collection<Ticket<T>> bridge$getTickets(SpongeTicketType<S, T> ticketType);
<T> Collection<Ticket<T>> bridge$getTickets(TicketType<T> ticketType);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.bridge.world.server;

import java.util.function.Function;

public interface TicketTypeBridge<N, T> {

void bridge$setTypeConverter(Function<T, N> converter);

N bridge$convertToNativeType(T spongeType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -832,12 +832,9 @@ public static RegistryLoader<TeleportHelperFilter> teleportHelperFilter() {

public static RegistryLoader<TicketType<?>> ticketType() {
return RegistryLoader.of(l -> {
l.add(TicketTypes.FORCED, k -> SpongeTicketType.createForChunkPos(net.minecraft.world.server.TicketType.FORCED));
l.add(TicketTypes.PORTAL, k -> SpongeTicketType.createForBlockPos(net.minecraft.world.server.TicketType.PORTAL));
l.add(TicketTypes.POST_TELEPORT,
k -> new SpongeTicketType<Integer, Entity>(net.minecraft.world.server.TicketType.POST_TELEPORT,
(entity, serverWorld) -> ((net.minecraft.entity.Entity) entity).getId(),
(id, serverWorld) -> (Entity) ((ServerWorld) serverWorld).getEntity(id)));
l.add(TicketTypes.FORCED, k -> (TicketType<?>) net.minecraft.world.server.TicketType.FORCED);
l.add(TicketTypes.PORTAL, k -> (TicketType<?>) net.minecraft.world.server.TicketType.PORTAL);
l.add(TicketTypes.POST_TELEPORT, k -> (TicketType<?>) net.minecraft.world.server.TicketType.POST_TELEPORT);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.bridge.world.server.ChunkManagerBridge;
import org.spongepowered.common.world.server.SpongeTicketType;
import org.spongepowered.math.vector.Vector3i;

import java.util.Collection;
Expand All @@ -42,11 +41,11 @@
@Mixin(ChunkManager.class)
public abstract class ChunkManagerMixin_API implements org.spongepowered.api.world.server.ChunkManager {

@Shadow @Final private net.minecraft.world.server.ServerWorld world;
@Shadow @Final private net.minecraft.world.server.ServerWorld level;

@Override
public ServerWorld getWorld() {
return (ServerWorld) this.world;
return (ServerWorld) this.level;
}

@Override
Expand All @@ -65,7 +64,7 @@ public <T> Optional<Ticket<T>> requestTicket(final TicketType<T> type, final Vec
throw new IllegalArgumentException("The radius must be positive.");
}
return ((ChunkManagerBridge) this).bridge$getTicketManager()
.bridge$registerTicket(this.getWorld(), (SpongeTicketType<?, T>) type, chunkPosition, value, radius);
.bridge$registerTicket(this.getWorld(), type, chunkPosition, value, radius);
}

@Override
Expand All @@ -80,7 +79,7 @@ public boolean releaseTicket(final Ticket<?> ticket) {

@Override
public <T> Collection<Ticket<T>> getTickets(final TicketType<T> type) {
return ((ChunkManagerBridge) this).bridge$getTicketManager().bridge$getTickets((SpongeTicketType<?, T>) type);
return ((ChunkManagerBridge) this).bridge$getTicketManager().bridge$getTickets(type);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.mixin.api.mcp.world.server;

import org.spongepowered.api.util.Ticks;
import org.spongepowered.api.world.server.TicketType;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

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

// @formatter:off
@Shadow @Final private long timeout;
// @formatter:on

@Override
public Ticks getLifetime() {
return Ticks.of(this.timeout);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
import org.spongepowered.common.accessor.world.server.TicketAccessor;
import org.spongepowered.common.bridge.world.TicketManagerBridge;
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;
import org.spongepowered.common.world.server.SpongeTicketType;
import org.spongepowered.math.vector.Vector3i;

import java.util.Collection;
Expand All @@ -68,10 +68,10 @@ public abstract class TicketManagerMixin implements TicketManagerBridge {
final net.minecraft.world.server.Ticket<?> nativeTicket = ((net.minecraft.world.server.Ticket<?>) (Object) ticket);
if (nativeTicket.getType() == TicketType.FORCED) {
final TicketAccessor<ChunkPos> ticketAccessor = ((TicketAccessor<ChunkPos>) ticket);
final ChunkPos chunkPos = ticketAccessor.accessor$getValue();
final ChunkPos chunkPos = ticketAccessor.accessor$key();
if (this.tickets.computeIfAbsent(chunkPos.toLong(), x -> SortedArraySet.create(4)).contains(nativeTicket)) {
// check to see if it's expired.
return ticketAccessor.accessor$isExpired(this.ticketTickCounter);
return ticketAccessor.invoker$timedOut(this.ticketTickCounter);
}
}
return false;
Expand All @@ -81,7 +81,7 @@ public abstract class TicketManagerMixin implements TicketManagerBridge {
@SuppressWarnings("unchecked")
public Ticks bridge$getTimeLeft(final Ticket<?> ticket) {
if (this.bridge$checkTicketValid(ticket)) {
return new SpongeTicks(((TicketAccessor<ChunkPos>) ticket).accessor$getTimestamp() - this.ticketTickCounter);
return new SpongeTicks(((TicketAccessor<ChunkPos>) ticket).accessor$createdTick() - this.ticketTickCounter);
}
return Ticks.zero();
}
Expand All @@ -91,7 +91,7 @@ public abstract class TicketManagerMixin implements TicketManagerBridge {
public boolean bridge$renewTicket(final Ticket<?> ticket) {
if (this.bridge$checkTicketValid(ticket)) {
final net.minecraft.world.server.Ticket<?> nativeTicket = (net.minecraft.world.server.Ticket<?>) (Object) ticket;
((TicketAccessor<ChunkPos>) ticket).accessor$setTimestamp(this.ticketTickCounter + nativeTicket.getType().timeout());
((TicketAccessor<ChunkPos>) ticket).invoker$setCreatedTick(this.ticketTickCounter + nativeTicket.getType().timeout());
return true;
}
return false;
Expand All @@ -100,13 +100,13 @@ public abstract class TicketManagerMixin implements TicketManagerBridge {
@Override
@SuppressWarnings("unchecked")
public <S, T> Optional<Ticket<T>> bridge$registerTicket(
final ServerWorld world, final SpongeTicketType<S, T> ticketType, final Vector3i pos, final T value, final int distanceLimit) {
final TicketType<S> type = ticketType.getWrappedType();
final ServerWorld world, final org.spongepowered.api.world.server.TicketType<T> ticketType, final Vector3i pos, final T value, final int distanceLimit) {
final TicketType<S> type = (net.minecraft.world.server.TicketType<S>) ticketType;
final net.minecraft.world.server.Ticket<?> ticketToRequest =
TicketAccessor.accessor$createInstance(
type,
Constants.ChunkTicket.MAXIMUM_CHUNK_TICKET_LEVEL - distanceLimit,
ticketType.getNativeType(value, world));
((TicketTypeBridge<S, T>) ticketType).bridge$convertToNativeType(value));
this.shadow$addTicket(VecHelper.toChunkPos(pos).toLong(), ticketToRequest);
return Optional.of((Ticket<T>) (Object) ticketToRequest);
}
Expand All @@ -122,11 +122,11 @@ public abstract class TicketManagerMixin implements TicketManagerBridge {
return false;
}

@SuppressWarnings({ "ConstantConditions", "unchecked" })
@SuppressWarnings({"ConstantConditions", "unchecked", "EqualsBetweenInconvertibleTypes"})
@Override
public <S, T> Collection<Ticket<T>> bridge$getTickets(final SpongeTicketType<S, T> ticketType) {
public <T> Collection<Ticket<T>> bridge$getTickets(final org.spongepowered.api.world.server.TicketType<T> ticketType) {
return this.tickets.values().stream()
.flatMap(x -> x.stream().filter(ticket -> ticket.getType().equals(ticketType.getWrappedType())))
.flatMap(x -> x.stream().filter(ticket -> ticket.getType().equals(ticketType)))
.map(x -> (Ticket<T>) (Object) x)
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.mixin.core.world.server;

import net.minecraft.util.Unit;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.server.TicketType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.common.bridge.world.server.TicketTypeBridge;
import org.spongepowered.common.util.VecHelper;
import org.spongepowered.math.vector.Vector3i;

import java.util.function.Function;

@Mixin(TicketType.class)
public abstract class TicketTypeMixin<N, T> implements TicketTypeBridge<N, T> {

private Function<T, N> impl$spongeToNativeType;

public void bridge$setTypeConverter(final Function<T, N> converter) {
this.impl$spongeToNativeType = converter;
}

@SuppressWarnings({"unchecked", "ConstantConditions", "rawtypes"})
@Override
public N bridge$convertToNativeType(final T spongeType) {
// I hate myself for even doing this, but hey ho.
// The intention is that people will only use Sponge registered tickets.
// If a mod provides a ticket and a plugin wants to use them, it'll have
// to use the correct type.
if (this.impl$spongeToNativeType == null) {
final TicketType<N> thisType = (TicketType<N>) (Object) this;
if (thisType == TicketType.POST_TELEPORT) {
this.impl$spongeToNativeType = (Function) entity -> ((net.minecraft.entity.Entity) entity).getId();
} else if (thisType == TicketType.START || thisType == TicketType.DRAGON) {
this.impl$spongeToNativeType = (Function) notUsed -> Unit.INSTANCE;
} else if (thisType == TicketType.FORCED || thisType == TicketType.LIGHT || thisType == TicketType.PLAYER ||
thisType == TicketType.UNKNOWN) {
this.impl$spongeToNativeType = (Function) (Function<Vector3i, ChunkPos>) (VecHelper::toChunkPos);
} else if (thisType == TicketType.PORTAL) {
this.impl$spongeToNativeType = (Function) (Function<Vector3i, BlockPos>) (VecHelper::toBlockPos);
} else {
this.impl$spongeToNativeType = t -> (N) t;
}
}
return this.impl$spongeToNativeType.apply(spongeType);
}

}
3 changes: 2 additions & 1 deletion src/mixins/resources/mixins.sponge.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@
"mcp.world.biome.provider.EndBiomeProviderMixin_API",
"mcp.world.biome.provider.NetherBiomeProvider_NoiseMixin_API",
"mcp.world.server.ChunkManagerMixin_API",
"mcp.world.server.TicketMixin_API"
"mcp.world.server.TicketMixin_API",
"mcp.world.server.TicketTypeMixin_API"
],
"client": [
"mcp.client.MinecraftMixin_API",
Expand Down
3 changes: 2 additions & 1 deletion src/mixins/resources/mixins.sponge.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@
"world.gen.DimensionSettingsMixin",
"world.DimensionTypeMixin",
"command.arguments.DimensionArgumentMixin",
"world.server.TicketManagerMixin"
"world.server.TicketManagerMixin",
"world.server.TicketTypeMixin"
],
"client": [
"client.MinecraftMixin",
Expand Down

0 comments on commit 009baa0

Please sign in to comment.