Skip to content

Commit

Permalink
Swap the vehicles of players
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Nov 20, 2024
1 parent 4ea99c7 commit 4110df4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -55,23 +54,22 @@ public void tick() {

private void swap() {
// Collect new positions for each player
List<Vec3d> positions = new ArrayList<>(this.phase.getPlayers().size());
List<SwapData> 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);
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/io/github/haykam821/deathswap/game/SwapData.java
Original file line number Diff line number Diff line change
@@ -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<Entity> 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());
}
}

0 comments on commit 4110df4

Please sign in to comment.