Skip to content

Commit

Permalink
Add quick utility methods to JoinOffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Nov 17, 2024
1 parent 7233a2f commit b8e4da9
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/api/game/GameTexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ public static MutableText inOtherGame() {
public static MutableText notAllowed() {
return Text.translatable("text.plasmid.join_result.not_allowed");
}

public static MutableText spectatorsOnly() {
return Text.translatable("text.plasmid.join_result.spectators_only");
}

public static MutableText participantsOnly() {
return Text.translatable("text.plasmid.join_result.participants_only");
}
}

public static final class Kick {
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/api/game/player/JoinOffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -83,4 +84,54 @@ default JoinOfferResult.Accept accept() {
default JoinOfferResult pass() {
return JoinOfferResult.PASS;
}

/**
* Returns an offer result that accepts this offer only if the intent is to spectate.
* <p>
* This function does not do anything on its own, but its result must be returned within a
* {@link GamePlayerEvents#OFFER} listener.
*
* @return an "accept" offer result for spectators and "reject" offer result for participants
*/
default JoinOfferResult acceptSpectators() {
return this.acceptSpectatorsOrElse((x) -> x.reject(GameTexts.Join.spectatorsOnly()));
}

/**
* Returns an offer result that accepts this offer only if the intent is to spectate.
* Others will be resolved in the orElse method.
* <p>
* This function does not do anything on its own, but its result must be returned within a
* {@link GamePlayerEvents#OFFER} listener.
*
* @return an "accept" offer result for spectators and "reject" offer result for participants
*/
default JoinOfferResult acceptSpectatorsOrElse(Function<JoinOffer, JoinOfferResult> function) {
return this.intent() == JoinIntent.SPECTATE ? this.accept() : function.apply(this);
}

/**
* Returns an offer result that accepts this offer only if the intent is to participate / play.
* <p>
* This function does not do anything on its own, but its result must be returned within a
* {@link GamePlayerEvents#OFFER} listener.
*
* @return an "accept" offer result for participants and "reject" offer result for spectators
*/
default JoinOfferResult acceptParticipants() {
return this.acceptParticipantsOrElse((x) -> x.reject(GameTexts.Join.participantsOnly()));
}

/**
* Returns an offer result that accepts this offer only if the intent is to participate / play.
* Others will be resolved in the orElse method.
* <p>
* This function does not do anything on its own, but its result must be returned within a
* {@link GamePlayerEvents#OFFER} listener.
*
* @return an "accept" offer result for spectators and "reject" offer result for participants
*/
default JoinOfferResult acceptParticipantsOrElse(Function<JoinOffer, JoinOfferResult> function) {
return this.intent() == JoinIntent.PLAY ? this.accept() : function.apply(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.util.PlayerRef;

import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;

public final class MutablePlayerSet implements PlayerSet {
private final MinecraftServer server;
private final Function<UUID, ServerPlayerEntity> playerGetter;

private final Set<UUID> players = new ObjectOpenHashSet<>();

public MutablePlayerSet(MinecraftServer server) {
this.server = server;
this.playerGetter = server.getPlayerManager()::getPlayer;
}

public MutablePlayerSet(ServerWorld world) {
this.playerGetter = (uuid) -> world.getPlayerByUuid(uuid) instanceof ServerPlayerEntity player ? player : null;
}

public MutablePlayerSet(GameSpace gameSpace) {
this.playerGetter = gameSpace.getPlayers()::getEntity;
}

public void clear() {
Expand Down Expand Up @@ -47,7 +59,7 @@ public boolean remove(UUID id) {
@Nullable
@Override
public ServerPlayerEntity getEntity(UUID id) {
return this.players.contains(id) ? this.server.getPlayerManager().getPlayer(id) : null;
return this.players.contains(id) ? this.playerGetter.apply(id) : null;
}

@Override
Expand All @@ -56,16 +68,16 @@ public boolean contains(UUID id) {
}

@Override
public Iterator<ServerPlayerEntity> iterator() {
var playerManager = this.server.getPlayerManager();
public @NotNull Iterator<ServerPlayerEntity> iterator() {
var playerGetter = this.playerGetter;
var ids = this.players.iterator();

return new AbstractIterator<>() {
@Override
protected ServerPlayerEntity computeNext() {
while (ids.hasNext()) {
var id = ids.next();
var player = playerManager.getPlayer(id);
var player = playerGetter.apply(id);
if (player != null) {
return player;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.api.util.PlayerRef;
import xyz.nucleoid.plasmid.impl.player.EmptyPlayerSet;
Expand Down Expand Up @@ -104,5 +105,6 @@ default MutablePlayerSet copy(MinecraftServer server) {
* @return an iterator over the online {@link ServerPlayerEntity} within this {@link PlayerSet}
*/
@Override
@NotNull
Iterator<ServerPlayerEntity> iterator();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xyz.nucleoid.plasmid.impl.game.manager;

import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.api.game.player.*;
import xyz.nucleoid.plasmid.impl.player.LocalJoinAcceptor;
Expand Down Expand Up @@ -174,7 +175,7 @@ public int size() {
}

@Override
public Iterator<ServerPlayerEntity> iterator() {
public @NotNull Iterator<ServerPlayerEntity> iterator() {
return this.set.iterator();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.api.game.player.PlayerSet;

Expand All @@ -26,7 +27,7 @@ public int size() {
}

@Override
public Iterator<ServerPlayerEntity> iterator() {
public @NotNull Iterator<ServerPlayerEntity> iterator() {
return this.players.getPlayerList().iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.api.game.player.PlayerSet;

Expand All @@ -26,7 +27,7 @@ public int size() {
}

@Override
public Iterator<ServerPlayerEntity> iterator() {
public @NotNull Iterator<ServerPlayerEntity> iterator() {
return this.world.getPlayers().iterator();
}
}
2 changes: 2 additions & 0 deletions src/main/resources/data/plasmid/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
"text.plasmid.join_result.in_other_game": "You must %s before joining another game!",
"text.plasmid.join_result.in_other_game.leave_this_game": "leave this game",
"text.plasmid.join_result.not_allowed": "You are not allowed to join this game!",
"text.plasmid.join_result.spectators_only": "You are not allowed to participate in this game!",
"text.plasmid.join_result.participants_only": "You are not allowed to spectate this game!",
"text.plasmid.map_workspace.workspace_not_found": "Map with id '%s' was not found!",
"text.plasmid.map.bounds.get": "The bounds for the workspace are %s to %s",
"text.plasmid.map.bounds.set": "Updated bounds for workspace",
Expand Down

0 comments on commit b8e4da9

Please sign in to comment.