Skip to content

Commit

Permalink
Reset spawn position on dimension switch.
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Dec 7, 2023
1 parent 07b2cdb commit 47ddc64
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,33 @@ public void register() {
registerClientbound(ClientboundPackets1_20_3.SPAWN_POSITION, wrapper -> {
final Position position = wrapper.passthrough(Type.POSITION1_14);
final float angle = wrapper.passthrough(Type.FLOAT);
wrapper.user().get(SpawnPositionStorage.class).setSpawnPosition(position, angle);
wrapper.user().get(SpawnPositionStorage.class).setSpawnPosition(Pair.of(position, angle));
});
registerClientbound(ClientboundPackets1_20_3.JOIN_GAME, new PacketHandlers() {
@Override
protected void register() {
map(Type.INT); // Entity id
map(Type.BOOLEAN); // Hardcore
map(Type.STRING_ARRAY); // World List
map(Type.NAMED_COMPOUND_TAG); // Dimension registry
map(Type.VAR_INT); // Max players
map(Type.VAR_INT); // View distance
map(Type.VAR_INT); // Simulation distance
map(Type.BOOLEAN); // Reduced debug info
map(Type.BOOLEAN); // Show death screen
map(Type.BOOLEAN); // Do limited crafting
map(Type.STRING); // Dimension key

handler(spawnPositionHandler());
}
});
registerClientbound(ClientboundPackets1_20_3.RESPAWN, new PacketHandlers() {
@Override
protected void register() {
map(Type.STRING); // dimension type

handler(spawnPositionHandler());
}
});
registerClientbound(ClientboundPackets1_20_3.GAME_EVENT, wrapper -> {
final short reason = wrapper.passthrough(Type.UNSIGNED_BYTE);
Expand Down Expand Up @@ -342,14 +368,24 @@ public void register() {
registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_3.UPDATE_ENABLED_FEATURES.getId(), ClientboundConfigurationPackets1_20_2.UPDATE_ENABLED_FEATURES.getId());
}

private PacketHandler spawnPositionHandler() {
return wrapper -> {
final SpawnPositionStorage spawnPositionStorage = wrapper.user().get(SpawnPositionStorage.class);
final String world = wrapper.passthrough(Type.STRING);

if (spawnPositionStorage.isDimensionChanging(world)) {
spawnPositionStorage.setSpawnPosition(SpawnPositionStorage.DEFAULT_SPAWN_POSITION);
}
};
}

private PacketHandler resourcePackStatusHandler() {
return wrapper -> {
final ResourcepackIDStorage storage = wrapper.user().get(ResourcepackIDStorage.class);
wrapper.write(Type.UUID, storage != null ? storage.uuid() : UUID.randomUUID());
};
}


private PacketHandler resourcePackHandler() {
return wrapper -> {
final UUID uuid = wrapper.read(Type.UUID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,33 @@
import com.viaversion.viaversion.libs.fastutil.Pair;

public class SpawnPositionStorage implements StorableObject {
public static final Pair<Position, Float> DEFAULT_SPAWN_POSITION = Pair.of(new Position(8, 64, 8), 0.0F); // Default values copied from the original client

private Pair<Position, Float> spawnPosition = Pair.of(new Position(8, 64, 8), 0.0F); // Default values copied from the original client
private Pair<Position, Float> spawnPosition = DEFAULT_SPAWN_POSITION;
private String dimension;

public Pair<Position, Float> getSpawnPosition() {
return spawnPosition;
}

public void setSpawnPosition(final Position position, final float angle) {
this.spawnPosition = Pair.of(position, angle);
public void setSpawnPosition(final Pair<Position, Float> spawnPosition) {
this.spawnPosition = spawnPosition;
}

/**
* Sets the dimension and returns whether it changed or not
*
* @param dimension The new dimension
* @return Whether the dimension changed or not
*/
public boolean isDimensionChanging(final String dimension) {
final boolean changed = !this.dimension.equals(dimension);
this.dimension = dimension;
return changed;
}

public void setDimension(final String dimension) {
this.dimension = dimension;
}

}

0 comments on commit 47ddc64

Please sign in to comment.