Skip to content

Commit

Permalink
Expand JoinAcceptorResult.Teleport to provide intent as one of arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Nov 10, 2024
1 parent 3cb9868 commit 8968fa8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,30 @@ default JoinAcceptorResult.Teleport teleport(ServerWorld world, Vec3d position)
default JoinAcceptorResult pass() {
return JoinAcceptorResult.PASS;
}

/**
* The result of this function only is selected for spectators, returning pass for non-spectators
* {@link GamePlayerEvents#ACCEPT} listener.
* @return a result
*/
default JoinAcceptorResult ifSpectator(Function<JoinAcceptor, JoinAcceptorResult> spectatorFunction) {
if (this.intent() == JoinIntent.SPECTATE) {
return spectatorFunction.apply(this);
} else {
return this.pass();
}
}

/**
* The result of this function only is selected for participants, returning pass for spectators
* {@link GamePlayerEvents#ACCEPT} listener.
* @return a result
*/
default JoinAcceptorResult ifParticipant(Function<JoinAcceptor, JoinAcceptorResult> participantFunction) {
if (this.intent() == JoinIntent.PLAY) {
return participantFunction.apply(this);
} else {
return this.pass();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.server.network.ServerPlayerEntity;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

public sealed interface JoinAcceptorResult permits JoinAcceptorResult.Pass, JoinAcceptorResult.Teleport {
Expand All @@ -13,10 +14,18 @@ private Pass() {
}

non-sealed interface Teleport extends JoinAcceptorResult {
Teleport thenRun(Consumer<PlayerSet> consumer);
Teleport thenRun(BiConsumer<PlayerSet, JoinIntent> consumer);

default Teleport thenRun(Consumer<PlayerSet> consumer) {
return this.thenRun((players, intent) -> consumer.accept(players));
}

default Teleport thenRunForEach(Consumer<ServerPlayerEntity> consumer) {
return this.thenRun(players -> players.forEach(consumer));
return this.thenRun((players, intent) -> players.forEach(consumer));
}

default Teleport thenRunForEach(BiConsumer<ServerPlayerEntity, JoinIntent> consumer) {
return this.thenRun((players, intent) -> players.forEach(player -> consumer.accept(player, intent)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private GameResult accept(Collection<ServerPlayerEntity> players, JoinIntent int
this.space.onAddPlayer(player);
joiningSet.add(player);
}
teleport.runCallbacks(joiningSet);
teleport.runCallbacks(joiningSet, intent);

return GameResult.ok();
} catch (Throwable throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import xyz.nucleoid.plasmid.api.util.PlayerPos;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -74,21 +75,21 @@ public JoinAcceptorResult.Teleport teleport(ServerWorld world, Vec3d position, f
public static class Teleport implements JoinAcceptorResult.Teleport {
private final Map<UUID, PlayerPos> positions;

private final List<Consumer<PlayerSet>> thenRun = new ArrayList<>();
private final List<BiConsumer<PlayerSet, JoinIntent>> thenRun = new ArrayList<>();

Teleport(Map<UUID, PlayerPos> positions) {
this.positions = positions;
}

@Override
public JoinAcceptorResult.Teleport thenRun(Consumer<PlayerSet> consumer) {
public JoinAcceptorResult.Teleport thenRun(BiConsumer<PlayerSet, JoinIntent> consumer) {
this.thenRun.add(consumer);
return this;
}

public void runCallbacks(PlayerSet players) {
public void runCallbacks(PlayerSet players, JoinIntent intent) {
for (var consumer : this.thenRun) {
consumer.accept(players);
consumer.accept(players, intent);
}
}

Expand Down

0 comments on commit 8968fa8

Please sign in to comment.