diff --git a/src/main/java/io/github/haykam821/deathswap/game/DeathSwapTimer.java b/src/main/java/io/github/haykam821/deathswap/game/DeathSwapTimer.java index f083836..981b778 100644 --- a/src/main/java/io/github/haykam821/deathswap/game/DeathSwapTimer.java +++ b/src/main/java/io/github/haykam821/deathswap/game/DeathSwapTimer.java @@ -14,7 +14,6 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.Formatting; -import net.minecraft.util.math.Vec3d; import xyz.nucleoid.plasmid.game.common.GlobalWidgets; import xyz.nucleoid.plasmid.game.common.widget.BossBarWidget; @@ -55,23 +54,22 @@ public void tick() { private void swap() { // Collect new positions for each player - List positions = new ArrayList<>(this.phase.getPlayers().size()); + List swapData = new ArrayList<>(this.phase.getPlayers().size()); ServerPlayerEntity previousPlayer = Iterables.getLast(this.phase.getPlayers()); - positions.add(previousPlayer.getPos()); + swapData.add(SwapData.from(previousPlayer)); for (ServerPlayerEntity player : this.phase.getPlayers()) { // Ensure positions are off by one if (player != previousPlayer) { - positions.add(player.getPos()); + swapData.add(SwapData.from(player)); } } // Teleport players to their new positions int index = 0; for (ServerPlayerEntity player : this.phase.getPlayers()) { - Vec3d position = positions.get(index); - player.teleport(position.getX(), position.getY(), position.getZ()); + swapData.get(index).apply(player); Text message = Text.translatable("text.deathswap.timer.swap", previousPlayer.getDisplayName()).formatted(NO_SWAP_FORMATTING); player.sendMessage(message, true); diff --git a/src/main/java/io/github/haykam821/deathswap/game/SwapData.java b/src/main/java/io/github/haykam821/deathswap/game/SwapData.java new file mode 100644 index 0000000..0ec1a3a --- /dev/null +++ b/src/main/java/io/github/haykam821/deathswap/game/SwapData.java @@ -0,0 +1,26 @@ +package io.github.haykam821.deathswap.game; + +import java.util.List; + +import net.minecraft.entity.Entity; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.math.Vec3d; + +public record SwapData(Vec3d pos, Entity vehicle, List passengers) { + public void apply(ServerPlayerEntity player) { + player.stopRiding(); + player.teleport(this.pos.getX(), this.pos.getY(), this.pos.getZ()); + + if (this.vehicle != null) { + player.startRiding(this.vehicle, true); + } + + for (Entity passenger : this.passengers) { + passenger.startRiding(player, true); + } + } + + public static SwapData from(ServerPlayerEntity player) { + return new SwapData(player.getPos(), player.getVehicle(), player.getPassengerList()); + } +}