Skip to content

Commit

Permalink
Comment out JoinIntent.ANY, add /game spectate command, add methods f…
Browse files Browse the repository at this point in the history
…or PlayerRef to work on GameSpace
  • Loading branch information
Patbox committed Nov 7, 2024
1 parent f94e227 commit f3438d8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
17 changes: 17 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/api/util/PlayerRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.api.game.GameSpace;

import java.util.UUID;
import java.util.function.Consumer;
Expand All @@ -23,6 +24,11 @@ public static PlayerRef ofUnchecked(UUID id) {
return new PlayerRef(id);
}

@Nullable
public ServerPlayerEntity getEntity(GameSpace gameSpace) {
return gameSpace.getPlayers().getEntity(this.id);
}

@Nullable
public ServerPlayerEntity getEntity(ServerWorld world) {
return this.getEntity(world.getServer());
Expand All @@ -33,6 +39,10 @@ public ServerPlayerEntity getEntity(MinecraftServer server) {
return server.getPlayerManager().getPlayer(this.id);
}

public boolean isOnline(GameSpace gameSpace) {
return this.getEntity(gameSpace) != null;
}

public boolean isOnline(ServerWorld world) {
return this.getEntity(world) != null;
}
Expand All @@ -41,6 +51,13 @@ public boolean isOnline(MinecraftServer server) {
return this.getEntity(server) != null;
}

public void ifOnline(GameSpace gameSpace, Consumer<ServerPlayerEntity> consumer) {
ServerPlayerEntity player = this.getEntity(gameSpace);
if (player != null) {
consumer.accept(player);
}
}

public void ifOnline(ServerWorld world, Consumer<ServerPlayerEntity> consumer) {
ServerPlayerEntity player = this.getEntity(world);
if (player != null) {
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/xyz/nucleoid/plasmid/impl/command/GameCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,17 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
)
)
.then(literal("join")
.executes(GameCommand::joinGame)
.executes(ctx -> GameCommand.joinGame(ctx, JoinIntent.PLAY))
.then(GameSpaceArgument.argument("game_space")
.executes(GameCommand::joinQualifiedGame)
.executes(ctx -> GameCommand.joinQualifiedGame(ctx, JoinIntent.PLAY))
)
)
.then(literal("spectate")
.executes(ctx -> GameCommand.joinGame(ctx, JoinIntent.SPECTATE))
.then(GameSpaceArgument.argument("game_space")
.executes(ctx -> GameCommand.joinQualifiedGame(ctx, JoinIntent.SPECTATE))
)
)
.then(literal("joinall")
.requires(source -> source.hasPermissionLevel(2))
.executes(GameCommand::joinAllGame)
Expand Down Expand Up @@ -198,7 +204,7 @@ private static void onOpenSuccess(ServerCommandSource source, GameSpace gameSpac
gameSpace.getPlayers().sendMessage(error);
}
} else if (player != null) {
tryJoinGame(player, gameSpace);
tryJoinGame(player, gameSpace, JoinIntent.PLAY);
}
}

Expand Down Expand Up @@ -243,14 +249,14 @@ private static int proposeGame(ServerCommandSource source, GameSpace gameSpace)
return Command.SINGLE_SUCCESS;
}

private static int joinGame(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
new GameJoinUi(context.getSource().getPlayerOrThrow()).open();
private static int joinGame(CommandContext<ServerCommandSource> context, JoinIntent intent) throws CommandSyntaxException {
new GameJoinUi(context.getSource().getPlayerOrThrow(), intent).open();
return Command.SINGLE_SUCCESS;
}

private static int joinQualifiedGame(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
private static int joinQualifiedGame(CommandContext<ServerCommandSource> context, JoinIntent intent) throws CommandSyntaxException {
var gameSpace = GameSpaceArgument.get(context, "game_space");
tryJoinGame(context.getSource().getPlayerOrThrow(), gameSpace);
tryJoinGame(context.getSource().getPlayerOrThrow(), gameSpace, intent);

return Command.SINGLE_SUCCESS;
}
Expand Down Expand Up @@ -286,15 +292,15 @@ private static void joinAllPlayersToGame(ServerCommandSource source, GameSpace g
.filter(player -> !GameSpaceManagerImpl.get().inGame(player))
.collect(Collectors.toList());

var intent = JoinIntent.ANY;
var intent = JoinIntent.PLAY;
var result = gameSpace.getPlayers().offer(players, intent);
if (result.isError()) {
source.sendError(result.errorCopy().formatted(Formatting.RED));
}
}

private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace) {
var result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY);
private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace, JoinIntent intent) {
var result = GamePlayerJoiner.tryJoin(player, gameSpace, intent);
if (result.isError()) {
player.sendMessage(result.errorCopy().formatted(Formatting.RED));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@
import java.util.Comparator;

public class GameJoinUi extends SimpleGui {
private static final GuiElementInterface EMPTY = new GuiElementBuilder(Items.GRAY_STAINED_GLASS_PANE).setName(Text.empty()).build();
private static final GuiElementInterface EMPTY = new GuiElementBuilder(Items.GRAY_STAINED_GLASS_PANE).hideTooltip().build();

private static final int NAVBAR_POS = 81;
private final ServerPlayerEntity player;
private final JoinIntent joinIntent;
private int tick;
private int page = 0;
private int pageSize;

public GameJoinUi(ServerPlayerEntity player) {
public GameJoinUi(ServerPlayerEntity player, JoinIntent intent) {
super(ScreenHandlerType.GENERIC_9X6, player, true);
this.joinIntent = intent;
this.player = player;
this.setTitle(Text.translatable("text.plasmid.ui.game_join.title"));
this.updateUi();
}

private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace) {
private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace, JoinIntent joinIntent) {
player.server.execute(() -> {
var result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY);
var result = GamePlayerJoiner.tryJoin(player, gameSpace, joinIntent);
if (result.isError()) {
player.sendMessage(result.errorCopy().formatted(Formatting.RED));
}
Expand Down Expand Up @@ -140,7 +142,7 @@ private GuiElementBuilder createIconFor(GameSpace gameSpace) {
);

element.hideDefaultTooltip();
element.setCallback((a, b, c, d) -> tryJoinGame(this.getPlayer(), gameSpace));
element.setCallback((a, b, c, d) -> tryJoinGame(this.getPlayer(), gameSpace, joinIntent));

return element;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public RegistryEntry<GameConfig<?>> game() {
public void applyTo(ServerPlayerEntity player) {
for (var gameSpace : GameSpaceManagerImpl.get().getOpenGameSpaces()) {
if (gameSpace.getMetadata().sourceConfig().equals(this.game)) {
var result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY);
var result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.PLAY);

if (result.isOk()) {
return;
Expand All @@ -45,7 +45,7 @@ public void applyTo(ServerPlayerEntity player) {
this.gameFuture = null;
GameResult result;
if (gameSpace != null) {
result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY);
result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.PLAY);
} else {
result = GamePlayerJoiner.handleJoinException(throwable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void applyTo(ServerPlayerEntity player) {
.handleAsync((gameSpace, throwable) -> {
GameResult result;
if (gameSpace != null) {
result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY);
result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.PLAY);
} else {
result = GamePlayerJoiner.handleJoinException(throwable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void applyTo(ServerPlayerEntity player) {
.handleAsync((gameSpace, throwable) -> {
GameResult result;
if (gameSpace != null) {
result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY);
result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.PLAY);
} else {
result = GamePlayerJoiner.handleJoinException(throwable);
}
Expand Down

0 comments on commit f3438d8

Please sign in to comment.