Skip to content

Commit

Permalink
Keep player view rotation on spawn point updates. Closes #398
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon committed Jan 28, 2024
1 parent 7eacac3 commit 8c786cf
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.serialization.DataResult;
import net.minecraft.Util;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;

import java.util.UUID;
Expand All @@ -24,6 +25,10 @@ public abstract class CodecExtensions {
.comapFlatMap(i -> DoubleStreamExtensions.fixedDoubleSize(i, 3)
.map(out -> new Vec3(out[0], out[1], out[2])), vec -> DoubleStream.of(vec.x, vec.y, vec.z));

public static final Codec<Vec2> VECTOR2 = DoubleStreamExtensions.CODEC
.comapFlatMap(i -> DoubleStreamExtensions.fixedDoubleSize(i, 2)
.map(out -> new Vec2((float) out[0], (float) out[1])), vec -> DoubleStream.of(vec.x, vec.y));

public static final Codec<ChunkPos> CHUNKPOS = Codec.INT_STREAM
.comapFlatMap(i -> Util.fixedSize(i, 2)
.map(arr -> new ChunkPos(arr[0], arr[1])), pos -> IntStream.of(pos.x, pos.z));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package dev.compactmods.machines.api.location;

import java.util.Optional;

import com.mojang.serialization.Codec;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;

import java.util.Optional;

public interface IDimensionalPosition {

BlockPos getBlockPosition();
Expand All @@ -21,7 +21,7 @@ public interface IDimensionalPosition {

IDimensionalPosition relative(Direction direction);

Optional<Vec3> getRotation();
Optional<Vec2> getRotation();

boolean isLoaded(MinecraftServer serv);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.util.INBTSerializable;

Expand All @@ -29,12 +30,12 @@ public class LevelBlockPosition implements INBTSerializable<CompoundTag>, IDimen

private ResourceKey<Level> dimension;
private Vec3 position;
private Vec3 rotation;
private Vec2 rotation;

public static final Codec<LevelBlockPosition> CODEC = RecordCodecBuilder.create(i -> i.group(
ResourceKey.codec(Registry.DIMENSION_REGISTRY).fieldOf("dim").forGetter(LevelBlockPosition::getDimension),
CodecExtensions.VECTOR3D.fieldOf("pos").forGetter(LevelBlockPosition::getExactPosition),
CodecExtensions.VECTOR3D.optionalFieldOf("rot", Vec3.ZERO).forGetter(x -> x.rotation)
CodecExtensions.VECTOR2.optionalFieldOf("rot", Vec2.ZERO).forGetter(x -> x.rotation)
).apply(i, LevelBlockPosition::new));

private LevelBlockPosition() {
Expand All @@ -43,29 +44,29 @@ private LevelBlockPosition() {
public LevelBlockPosition(IDimensionalBlockPosition base) {
this.dimension = base.dimensionKey();
this.position = base.getExactPosition();
this.rotation = Vec3.ZERO;
this.rotation = Vec2.ZERO;
}

public LevelBlockPosition(ResourceKey<Level> world, BlockPos positionBlock) {
this(world, Vec3.ZERO, Vec3.ZERO);
this(world, Vec3.ZERO, Vec2.ZERO);
this.position = new Vec3(positionBlock.getX(), positionBlock.getY(), positionBlock.getZ());
this.rotation = Vec3.ZERO;
this.rotation = Vec2.ZERO;
}

public LevelBlockPosition(ResourceKey<Level> world, Vec3 positionBlock) {
this(world, positionBlock, Vec3.ZERO);
this(world, positionBlock, Vec2.ZERO);
this.dimension = world;
this.rotation = Vec3.ZERO;
this.rotation = Vec2.ZERO;
}

public LevelBlockPosition(ResourceKey<Level> dim, Vec3 pos, Vec3 rotation) {
public LevelBlockPosition(ResourceKey<Level> dim, Vec3 pos, Vec2 rotation) {
this.dimension = dim;
this.position = pos;
this.rotation = rotation;
}

public static LevelBlockPosition fromEntity(LivingEntity entity) {
return new LevelBlockPosition(entity.level.dimension(), entity.position());
return new LevelBlockPosition(entity.level.dimension(), entity.position(), entity.getRotationVector());
}

public ServerLevel level(@Nonnull MinecraftServer server) {
Expand Down Expand Up @@ -97,7 +98,7 @@ public static LevelBlockPosition fromNBT(CompoundTag nbt) {
@Override
public CompoundTag serializeNBT() {
if(this.rotation == null)
this.rotation = Vec3.ZERO;
this.rotation = Vec2.ZERO;

DataResult<Tag> nbt = CODEC.encodeStart(NbtOps.INSTANCE, this);
return (CompoundTag) nbt.result().orElse(null);
Expand All @@ -120,7 +121,7 @@ public ResourceKey<Level> getDimension() {
return this.dimension;
}

public Optional<Vec3> getRotation() {
public Optional<Vec2> getRotation() {
return Optional.of(this.rotation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.compactmods.machines.api.codec.CodecExtensions;
import dev.compactmods.machines.api.location.IDimensionalPosition;
import dev.compactmods.machines.util.MathUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Registry;
Expand All @@ -13,6 +12,7 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;

import java.util.Objects;
Expand Down Expand Up @@ -77,7 +77,7 @@ public IDimensionalPosition relative(Direction direction) {
}

@Override
public Optional<Vec3> getRotation() {
public Optional<Vec2> getRotation() {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@

import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.portal.PortalInfo;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.util.ITeleporter;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;

public record SimpleTeleporter(Vec3 pos) implements ITeleporter {
public record SimpleTeleporter(Vec3 pos, Vec2 rotation) implements ITeleporter {

public static SimpleTeleporter to(Vec3 pos) {
return new SimpleTeleporter(pos);
return new SimpleTeleporter(pos, Vec2.ZERO);
}

public static SimpleTeleporter to(Vec3 pos, Vec2 rotation) {
return new SimpleTeleporter(pos, rotation);
}

@Override
public @Nullable PortalInfo getPortalInfo(Entity entity, ServerLevel destWorld, Function<ServerLevel, PortalInfo> defaultPortalInfo) {
return new PortalInfo(pos, Vec3.ZERO, rotation.y, rotation.x);
}

@Override
public Entity placeEntity(Entity entity, ServerLevel currentWorld, ServerLevel destWorld, float yaw, Function<Boolean, Entity> repositionEntity) {
entity = repositionEntity.apply(false);
entity.teleportTo(pos.x, pos.y, pos.z);
return entity;
return repositionEntity.apply(false);
}
}
3 changes: 2 additions & 1 deletion src/main/java/dev/compactmods/machines/room/Rooms.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;

import javax.naming.OperationNotSupportedException;
Expand Down Expand Up @@ -98,7 +99,7 @@ public static void resetSpawn(MinecraftServer server, ChunkPos room) throws None
final var centerPoint = Vec3.atCenterOf(roomInfo.getCenter());
final var newSpawn = centerPoint.subtract(0, (roomInfo.getSize().getInternalSize() / 2f), 0);

data.setSpawn(room, newSpawn);
data.setSpawn(room, newSpawn, Vec2.ZERO);
}

public static Optional<String> getRoomName(MinecraftServer server, ChunkPos room) throws NonexistentRoomException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.storage.DimensionDataStorage;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -124,20 +125,26 @@ public LevelBlockPosition getSpawn(ChunkPos roomChunk) {

return new LevelBlockPosition(
CompactDimension.LEVEL_KEY,
roomData.getSpawn()
roomData.getSpawn(),
roomData.getSpawnRotation()
);
}

public int getNextSpiralPosition() {
return this.roomData.size() + 1;
}

@Deprecated
public void setSpawn(ChunkPos roomChunk, Vec3 position) {
setSpawn(roomChunk, position, Vec2.ZERO);
}

public void setSpawn(ChunkPos roomChunk, Vec3 position, Vec2 rotation) {
if (!roomData.containsKey(roomChunk))
return;

RoomData roomData = this.roomData.get(roomChunk);
roomData.setSpawn(position);
roomData.setSpawn(position, rotation);

setDirty();
}
Expand Down Expand Up @@ -218,7 +225,7 @@ public NewRoomRegistration chunk(ChunkPos chunk) {
}

public void register() throws OperationNotSupportedException {
RoomData data = new RoomData(owner, center, spawn, size, Optional.empty());
RoomData data = new RoomData(owner, center, spawn, Vec2.ZERO, size, Optional.empty());
storage.register(chunk, data);
}
}
Expand All @@ -229,21 +236,24 @@ public static class RoomData {
CodecExtensions.UUID_CODEC.fieldOf("owner").forGetter(RoomData::getOwner),
BlockPos.CODEC.fieldOf("center").forGetter(RoomData::getCenter),
CodecExtensions.VECTOR3D.fieldOf("spawn").forGetter(RoomData::getSpawn),
CodecExtensions.VECTOR2.optionalFieldOf("spawnRot", Vec2.ZERO).forGetter(RoomData::getSpawnRotation),
RoomSize.CODEC.fieldOf("size").forGetter(RoomData::getSize),
Codec.STRING.optionalFieldOf("name").forGetter(RoomData::getName)
).apply(i, RoomData::new));

private final UUID owner;
private final BlockPos center;
private Vec3 spawn;
private @org.jetbrains.annotations.Nullable Vec2 spawnRotation;
private final RoomSize size;
private boolean hasCustomName;
private String name;

public RoomData(UUID owner, BlockPos center, Vec3 spawn, RoomSize size, Optional<String> name) {
public RoomData(UUID owner, BlockPos center, Vec3 spawn, Vec2 spawnRotation, RoomSize size, Optional<String> name) {
this.owner = owner;
this.center = center;
this.spawn = spawn;
this.spawnRotation = spawnRotation;
this.size = size;

name.ifPresentOrElse(n -> {
Expand Down Expand Up @@ -278,12 +288,22 @@ public Vec3 getSpawn() {
return this.spawn;
}

public Vec2 getSpawnRotation() {
return this.spawnRotation;
}

public BlockPos getCenter() {
return this.center;
}

@Deprecated(forRemoval = true)
public void setSpawn(Vec3 newSpawn) {
setSpawn(newSpawn, Vec2.ZERO);
}

public void setSpawn(Vec3 newSpawn, Vec2 rotation) {
this.spawn = newSpawn;
this.spawnRotation = rotation;
}

public AABB getRoomBounds() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public InteractionResultHolder<ItemStack> use(Level world, Player player, Intera
final CompactRoomData intern = CompactRoomData.get(playerDim);

// Use internal data to set new spawn point
intern.setSpawn(machineChunk, player.position());
intern.setSpawn(machineChunk, player.position(), player.getRotationVector());

MutableComponent tc = TranslationUtil.message(Messages.ROOM_SPAWNPOINT_SET)
.withStyle(ChatFormatting.GREEN);
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/dev/compactmods/machines/util/PlayerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.util.LazyOptional;

Expand Down Expand Up @@ -90,11 +91,11 @@ public static void teleportPlayerIntoRoom(MinecraftServer serv, Player player, C

serv.submitAsync(() -> {
Vec3 sp = spawn.getExactPosition();
Vec3 sr = spawn.getRotation().orElse(new Vec3(player.xRotO, player.yRotO, 0));
Vec2 sr = spawn.getRotation().orElse(player.getRotationVector());

if (player instanceof ServerPlayer servPlayer) {
servPlayer.changeDimension(compactDim, SimpleTeleporter.to(sp));

servPlayer.changeDimension(compactDim, SimpleTeleporter.to(sp, sr));
servPlayer.setCamera(null); // force camera update
if (grantAdvancement)
AdvancementTriggers.getTriggerForMachineClaim(roomSize).trigger(servPlayer);
}
Expand All @@ -120,11 +121,10 @@ public static void teleportPlayerOutOfMachine(ServerLevel world, @Nonnull Server

final var level = spawnPoint.level(serv);

Vec3 worldPos, entryRot;
worldPos = spawnPoint.getExactPosition();
entryRot = spawnPoint.getRotation().orElse(Vec3.ZERO);
Vec3 worldPos = spawnPoint.getExactPosition();
Vec2 entryRot = spawnPoint.getRotation().orElse(Vec2.ZERO);

serverPlayer.changeDimension(level, SimpleTeleporter.to(worldPos));
serverPlayer.changeDimension(level, SimpleTeleporter.to(worldPos, entryRot));
} else {
howDidYouGetThere(serverPlayer);

Expand Down

0 comments on commit 8c786cf

Please sign in to comment.