Refactor Dimension API and fix a bug ()

* Refactor dimension API to use less static var

* Refactor test code

* Fix end spawn platform generation with non-player teleport

* Fix wrong cast

* Add and refactor tests

* Fix spotless

* Mark impl as internal
This commit is contained in:
apple502j 2022-09-11 22:15:00 +09:00 committed by GitHub
parent 6a999b8eb9
commit 0dd10df6d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 109 additions and 116 deletions
fabric-dimensions-v1/src
main
testmod/java/net/fabricmc/fabric/test/dimension

View file

@ -17,38 +17,26 @@
package net.fabricmc.fabric.impl.dimension;
import com.google.common.base.Preconditions;
import org.jetbrains.annotations.ApiStatus;
import net.minecraft.entity.Entity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.TeleportTarget;
import net.fabricmc.fabric.api.dimension.v1.FabricDimensions;
@ApiStatus.Internal
public final class FabricDimensionInternals {
/**
* The target passed to the last call to {@link FabricDimensions#teleport(Entity, ServerWorld, TeleportTarget)}.
*/
private static TeleportTarget currentTarget;
private FabricDimensionInternals() {
throw new AssertionError();
}
/**
* Returns the last target set when a user of the API requested teleportation, or null.
*/
public static TeleportTarget getCustomTarget() {
return currentTarget;
}
@SuppressWarnings("unchecked")
public static <E extends Entity> E changeDimension(E teleported, ServerWorld dimension, TeleportTarget target) {
Preconditions.checkArgument(!teleported.world.isClient, "Entities can only be teleported on the server side");
Preconditions.checkArgument(Thread.currentThread() == ((ServerWorld) teleported.world).getServer().getThread(), "Entities must be teleported from the main server thread");
try {
currentTarget = target;
((Teleportable) teleported).fabric_setCustomTeleportTarget(target);
// Fast path for teleporting within the same dimension.
if (teleported.getWorld() == dimension) {
@ -66,7 +54,7 @@ public final class FabricDimensionInternals {
return (E) teleported.moveToWorld(dimension);
} finally {
currentTarget = null;
((Teleportable) teleported).fabric_setCustomTeleportTarget(null);
}
}
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.impl.dimension;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import net.minecraft.world.TeleportTarget;
@ApiStatus.Internal
public interface Teleportable {
/**
* Sets the last target set when a user of the API requested teleportation, or null.
*/
void fabric_setCustomTeleportTarget(@Nullable TeleportTarget teleportTarget);
}

View file

@ -16,34 +16,59 @@
package net.fabricmc.fabric.mixin.dimension;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.minecraft.entity.Entity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.TeleportTarget;
import net.minecraft.world.World;
import net.fabricmc.fabric.impl.dimension.FabricDimensionInternals;
import net.fabricmc.fabric.impl.dimension.Teleportable;
/**
* This mixin implements {@link Entity#getTeleportTarget(ServerWorld)} for modded dimensions, as Vanilla will
* not return a teleport target for anything but Vanilla dimensions and prevents changing teleport target in
* {@link ServerPlayerEntity#getTeleportTarget(ServerWorld)} when teleporting to END using api.
* This also prevents several End dimension-specific code when teleporting using api.
*/
@Mixin(value = {ServerPlayerEntity.class, Entity.class})
public class EntityMixin {
@SuppressWarnings("ConstantConditions")
public class EntityMixin implements Teleportable {
@Unique
@Nullable
protected TeleportTarget customTeleportTarget;
@Override
public void fabric_setCustomTeleportTarget(TeleportTarget teleportTarget) {
this.customTeleportTarget = teleportTarget;
}
@Inject(method = "getTeleportTarget", at = @At("HEAD"), cancellable = true, allow = 1)
public void getTeleportTarget(ServerWorld destination, CallbackInfoReturnable<TeleportTarget> cri) {
Entity self = (Entity) (Object) this;
public void getTeleportTarget(ServerWorld destination, CallbackInfoReturnable<TeleportTarget> cir) {
// Check if a destination has been set for the entity currently being teleported
TeleportTarget customTarget = FabricDimensionInternals.getCustomTarget();
TeleportTarget customTarget = this.customTeleportTarget;
if (customTarget != null) {
cri.setReturnValue(customTarget);
cir.setReturnValue(customTarget);
}
}
/**
* This stops the following behaviors, in 1 mixin.
* - ServerWorld#createEndSpawnPlatform in Entity
* - End-to-overworld spawning behavior in ServerPlayerEntity
* - ServerPlayerEntity#createEndSpawnPlatform in ServerPlayerEntity
*/
@Redirect(method = "moveToWorld", at = @At(value = "FIELD", target = "Lnet/minecraft/world/World;END:Lnet/minecraft/util/registry/RegistryKey;"))
private RegistryKey<World> stopEndSpecificBehavior() {
if (this.customTeleportTarget != null) return null;
return World.END;
}
}

View file

@ -1,70 +0,0 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.mixin.dimension;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
import net.fabricmc.fabric.impl.dimension.FabricDimensionInternals;
/**
* This mixin prevents END dimension specific events when using api. Specifically:
* <ol>
* <li>{@link ServerPlayerEntity#createEndSpawnPlatform(ServerWorld, BlockPos)} execution when teleporting to END</li>
* <li>"Game won" screen and {@link ServerPlayerEntity#seenCredits} flag setting by
* {@link ServerPlayerEntity#moveToWorld(ServerWorld)} when teleporting from END</li>
* </ol>
*/
@Mixin(ServerPlayerEntity.class)
public class ServerPlayerEntityMixin {
@Inject(method = "createEndSpawnPlatform", at = @At("HEAD"), cancellable = true, allow = 1)
public void getTeleportTarget(ServerWorld world, BlockPos centerPos, CallbackInfo ci) {
// Check if a destination has been set for the entity currently being teleported
if (FabricDimensionInternals.getCustomTarget() != null) {
ci.cancel();
}
}
@Redirect(
method = "moveToWorld",
slice = @Slice(
from = @At(value = "FIELD", target = "net/minecraft/world/World.END:Lnet/minecraft/util/registry/RegistryKey;", opcode = Opcodes.GETSTATIC, ordinal = 0),
to = @At(value = "FIELD", target = "net/minecraft/server/network/ServerPlayerEntity.notInAnyWorld:Z", opcode = Opcodes.GETFIELD, ordinal = 0)
),
at = @At(value = "FIELD", target = "net/minecraft/world/World.OVERWORLD:Lnet/minecraft/util/registry/RegistryKey;", opcode = Opcodes.GETSTATIC),
allow = 1
)
public RegistryKey<World> moveToWorld() {
// Check if a destination has been set for the entity currently being teleported
if (FabricDimensionInternals.getCustomTarget() != null) {
return null;
}
return World.OVERWORLD;
}
}

View file

@ -5,8 +5,7 @@
"mixins": [
"EntityMixin",
"LevelStorageMixin",
"MainMixin",
"ServerPlayerEntityMixin"
"MainMixin"
],
"injectors": {
"defaultRequire": 1

View file

@ -17,6 +17,7 @@
package net.fabricmc.fabric.test.dimension;
import static net.minecraft.entity.EntityType.COW;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
import com.mojang.brigadier.context.CommandContext;
@ -24,7 +25,9 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.block.Blocks;
import net.minecraft.command.CommandException;
import net.minecraft.command.argument.DimensionArgumentType;
import net.minecraft.entity.Entity;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
@ -37,7 +40,6 @@ import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.TeleportTarget;
import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionOptions;
import net.minecraft.world.dimension.DimensionType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
@ -45,14 +47,12 @@ import net.fabricmc.fabric.api.dimension.v1.FabricDimensions;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
public class FabricDimensionTest implements ModInitializer {
// The dimension options refer to the JSON-file in the dimension subfolder of the datapack,
// which will always share it's ID with the world that is created from it
// The dimension options refer to the JSON-file in the dimension subfolder of the data pack,
// which will always share its ID with the world that is created from it
private static final RegistryKey<DimensionOptions> DIMENSION_KEY = RegistryKey.of(Registry.DIMENSION_KEY, new Identifier("fabric_dimension", "void"));
private static RegistryKey<World> WORLD_KEY = RegistryKey.of(Registry.WORLD_KEY, DIMENSION_KEY.getValue());
private static final RegistryKey<DimensionType> DIMENSION_TYPE_KEY = RegistryKey.of(Registry.DIMENSION_TYPE_KEY, new Identifier("fabric_dimension", "void_type"));
@Override
public void onInitialize() {
Registry.register(Registry.CHUNK_GENERATOR, new Identifier("fabric_dimension", "void"), VoidChunkGenerator.CODEC);
@ -72,6 +72,7 @@ public class FabricDimensionTest implements ModInitializer {
Entity entity = COW.create(overworld);
if (entity == null) throw new AssertionError("Could not create entity!");
if (!entity.world.getRegistryKey().equals(World.OVERWORLD)) throw new AssertionError("Entity starting world isn't the overworld");
TeleportTarget target = new TeleportTarget(Vec3d.ZERO, new Vec3d(1, 1, 1), 45f, 60f);
@ -85,20 +86,37 @@ public class FabricDimensionTest implements ModInitializer {
if (!teleported.getPos().equals(target.position)) throw new AssertionError("Target Position not reached.");
});
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("fabric_dimension_test")
.executes(FabricDimensionTest.this::swapTargeted)));
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(literal("fabric_dimension_test")
.executes(FabricDimensionTest.this::swapTargeted));
// Used to test https://github.com/FabricMC/fabric/issues/2239
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("fabric_dimension_test_desync")
.executes(FabricDimensionTest.this::testDesync)));
// Used to test https://github.com/FabricMC/fabric/issues/2239
// Dedicated-only
if (environment != CommandManager.RegistrationEnvironment.INTEGRATED) {
dispatcher.register(literal("fabric_dimension_test_desync")
.executes(FabricDimensionTest.this::testDesync));
}
// Used to test https://github.com/FabricMC/fabric/issues/2238
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("fabric_dimension_test_entity")
.executes(FabricDimensionTest.this::testEntityTeleport)));
// Used to test https://github.com/FabricMC/fabric/issues/2238
dispatcher.register(literal("fabric_dimension_test_entity")
.executes(FabricDimensionTest.this::testEntityTeleport));
// Used to test teleport to vanilla dimension
dispatcher.register(literal("fabric_dimension_test_tp")
.then(argument("target", DimensionArgumentType.dimension())
.executes((context) ->
testVanillaTeleport(context, DimensionArgumentType.getDimensionArgument(context, "target")))));
});
}
private int swapTargeted(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
private int swapTargeted(CommandContext<ServerCommandSource> context) {
ServerPlayerEntity player = context.getSource().getPlayer();
if (player == null) {
context.getSource().sendFeedback(Text.literal("You must be a player to execute this command."), false);
return 1;
}
ServerWorld serverWorld = player.getWorld();
ServerWorld modWorld = getModWorld(context);
@ -121,7 +139,7 @@ public class FabricDimensionTest implements ModInitializer {
return 1;
}
private int testDesync(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
private int testDesync(CommandContext<ServerCommandSource> context) {
ServerPlayerEntity player = context.getSource().getPlayer();
if (player == null) {
@ -129,18 +147,13 @@ public class FabricDimensionTest implements ModInitializer {
return 1;
}
if (!context.getSource().getServer().isDedicated()) {
context.getSource().sendFeedback(Text.literal("This command can only be executed on dedicated servers."), false);
return 1;
}
TeleportTarget target = new TeleportTarget(player.getPos().add(5, 0, 0), player.getVelocity(), player.getYaw(), player.getPitch());
FabricDimensions.teleport(player, (ServerWorld) player.world, target);
return 1;
}
private int testEntityTeleport(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
private int testEntityTeleport(CommandContext<ServerCommandSource> context) {
ServerPlayerEntity player = context.getSource().getPlayer();
if (player == null) {
@ -165,6 +178,14 @@ public class FabricDimensionTest implements ModInitializer {
return 1;
}
private int testVanillaTeleport(CommandContext<ServerCommandSource> context, ServerWorld targetWorld) throws CommandSyntaxException {
Entity entity = context.getSource().getEntityOrThrow();
TeleportTarget target = new TeleportTarget(entity.getPos(), entity.getVelocity(), entity.getYaw(), entity.getPitch());
FabricDimensions.teleport(entity, targetWorld, target);
return 1;
}
private ServerWorld getModWorld(CommandContext<ServerCommandSource> context) {
return getWorld(context, WORLD_KEY);
}