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

add ALLOW_PLAYER_CHANGE_WORLD #4104

Open
wants to merge 2 commits into
base: 1.21.1
Choose a base branch
from
Open
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 @@ -45,6 +45,19 @@ public final class ServerEntityWorldChangeEvents {
}
});

/**
* Called before a player is being moved to a different world.
*/
public static final Event<AllowPlayerChange> ALLOW_PLAYER_CHANGE_WORLD = EventFactory.createArrayBacked(AllowPlayerChange.class, callbacks -> (player, origin, destination) -> {
for (AllowPlayerChange callback : callbacks) {
if (!callback.allowChangeWorld(player, origin, destination)) {
return false;
}
}

return true;
});

/**
* An event which is called after a player has been moved to a different world.
*
Expand Down Expand Up @@ -75,6 +88,19 @@ public interface AfterEntityChange {
void afterChangeWorld(Entity originalEntity, Entity newEntity, ServerWorld origin, ServerWorld destination);
}

@FunctionalInterface
public interface AllowPlayerChange {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be limited to just players, other entities are allowed to use portals.

I think its also worth looking at 1.21.2 as I know they changed a lot of this teleportation code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this event is currently only used to restrict players when crossing dimensions; other entities are not affected. I believe there are very few scenarios where restricting entities from crossing dimensions would be necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, I will read through 1.21.2 as soon as possible, and I may be able to provide feedback by tomorrow.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reviewed the relevant source code for 24w39a, and there are not many changes regarding the dimension-crossing code. Through testing, I can assure you that this event works correctly in the latest version as well.

/**
* Called before a player is being moved to a different world.
*
* @param player the player
* @param origin the original world the player is in
* @param destination the new world the player is moving to
* @return true if the player is allowed to change worlds, false otherwise
*/
boolean allowChangeWorld(ServerPlayerEntity player, ServerWorld origin, ServerWorld destination);
}

@FunctionalInterface
public interface AfterPlayerChange {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.mojang.datafixers.util.Either;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -43,6 +44,7 @@
import net.minecraft.util.Unit;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.TeleportTarget;
import net.minecraft.world.World;

import net.fabricmc.fabric.api.entity.event.v1.EntitySleepEvents;
Expand Down Expand Up @@ -77,6 +79,16 @@ private void notifyDeath(DamageSource source, CallbackInfo ci) {
ServerLivingEntityEvents.AFTER_DEATH.invoker().afterDeath((ServerPlayerEntity) (Object) this, source);
}

@Inject(method = "teleportTo", at = @At(value = "FIELD", target = "Lnet/minecraft/server/network/ServerPlayerEntity;inTeleportationState:Z", opcode = Opcodes.PUTFIELD), cancellable = true)
private void beforeWorldChanged(TeleportTarget teleportTarget, CallbackInfoReturnable<Entity> cir) {
ServerPlayerEntity player = (ServerPlayerEntity) (Object) this;
boolean allowed = ServerEntityWorldChangeEvents.ALLOW_PLAYER_CHANGE_WORLD.invoker().allowChangeWorld(player, player.getServerWorld(), teleportTarget.world());

if (!allowed) {
cir.setReturnValue(null);
}
}

/**
* This is called by {@code teleportTo}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public void onInitialize() {
LOGGER.info("Entity {} Killed: {}", entity, killed);
});

ServerEntityWorldChangeEvents.ALLOW_PLAYER_CHANGE_WORLD.register((player, origin, destination) -> {
if (player.getMainHandStack().isOf(Items.END_ROD)) {
LOGGER.info("Player {} failed to change world because of handing an end rod", player.getGameProfile().getName());
return false;
}

LOGGER.info("Allow Moving player {}: [{} -> {}]", player, origin.getRegistryKey().getValue(), destination.getRegistryKey().getValue());
return true;
});

ServerEntityWorldChangeEvents.AFTER_PLAYER_CHANGE_WORLD.register((player, origin, destination) -> {
LOGGER.info("Moved player {}: [{} -> {}]", player, origin.getRegistryKey().getValue(), destination.getRegistryKey().getValue());
});
Expand Down
Loading