Skip to content

Commit

Permalink
Some minor fix-ups and cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Jun 8, 2024
1 parent 0c33c07 commit 0c98fe4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/main/java/xyz/nucleoid/plasmid/command/ChatCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
// @formatter:on

public static int switchToAll(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
var player = ctx.getSource().getPlayer();
var player = ctx.getSource().getPlayerOrThrow();
((HasChatChannel) player).setChatChannel(ChatChannel.ALL);
player.sendMessage(Text.translatable("text.plasmid.chat.switch.all").formatted(Formatting.AQUA), false);
return Command.SINGLE_SUCCESS;
}

public static int switchToTeam(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
var player = ctx.getSource().getPlayer();
var player = ctx.getSource().getPlayerOrThrow();
((HasChatChannel) player).setChatChannel(ChatChannel.TEAM);
player.sendMessage(Text.translatable("text.plasmid.chat.switch.team").formatted(Formatting.AQUA), false);
return Command.SINGLE_SUCCESS;
Expand Down
90 changes: 41 additions & 49 deletions src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.logging.LogUtils;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.NbtCompoundArgumentType;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -16,6 +17,7 @@
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.slf4j.Logger;
import xyz.nucleoid.plasmid.Plasmid;
import xyz.nucleoid.plasmid.command.argument.GameConfigArgument;
import xyz.nucleoid.plasmid.command.argument.GameSpaceArgument;
Expand All @@ -37,6 +39,8 @@
import static net.minecraft.server.command.CommandManager.literal;

public final class GameCommand {
private static final Logger LOGGER = LogUtils.getLogger();

public static final SimpleCommandExceptionType NO_GAME_OPEN = new SimpleCommandExceptionType(
Text.translatable("text.plasmid.game.join.no_game_open")
);
Expand All @@ -45,13 +49,13 @@ public final class GameCommand {
Text.translatable("text.plasmid.game.not_in_game")
);

public static final DynamicCommandExceptionType MALFORMED_CONFIG = new DynamicCommandExceptionType(error -> {
return Text.translatable("text.plasmid.game.open.malformed_config", error);
});
public static final DynamicCommandExceptionType MALFORMED_CONFIG = new DynamicCommandExceptionType(error ->
Text.stringifiedTranslatable("text.plasmid.game.open.malformed_config", error)
);

public static final DynamicCommandExceptionType PLAYER_NOT_IN_GAME = new DynamicCommandExceptionType(player -> {
return Text.translatable("text.plasmid.game.locate.player_not_in_game", player);
});
public static final DynamicCommandExceptionType PLAYER_NOT_IN_GAME = new DynamicCommandExceptionType(player ->
Text.stringifiedTranslatable("text.plasmid.game.locate.player_not_in_game", player)
);

// @formatter:off
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
Expand Down Expand Up @@ -125,7 +129,7 @@ protected static int openGame(CommandContext<ServerCommandSource> context, boole
} catch (CommandSyntaxException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("An unexpected error occurred while opening a game", e);
context.getSource().sendFeedback(() -> Text.translatable("text.plasmid.game.open.error").formatted(Formatting.RED), false);
return 0;
}
Expand All @@ -138,17 +142,13 @@ private static int openAnonymousGame(CommandContext<ServerCommandSource> context
protected static int openAnonymousGame(CommandContext<ServerCommandSource> context, boolean test) throws CommandSyntaxException {
try {
var configNbt = NbtCompoundArgumentType.getNbtCompound(context, "game_config_nbt");
var result = GameConfig.DIRECT_CODEC.parse(context.getSource().getRegistryManager().getOps(NbtOps.INSTANCE), configNbt);
if (result.error().isPresent()) {
throw MALFORMED_CONFIG.create(result.error().get());
}

var game = result.result().get();
var game = GameConfig.DIRECT_CODEC.parse(context.getSource().getRegistryManager().getOps(NbtOps.INSTANCE), configNbt)
.getOrThrow(MALFORMED_CONFIG::create);
return openGame(context, RegistryEntry.of(game), test);
} catch (CommandSyntaxException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("An unexpected error occurred while opening a game", e);
context.getSource().sendFeedback(() -> Text.translatable("text.plasmid.game.open.error").formatted(Formatting.RED), false);
return 0;
}
Expand All @@ -157,11 +157,9 @@ protected static int openAnonymousGame(CommandContext<ServerCommandSource> conte
private static int openGame(CommandContext<ServerCommandSource> context, RegistryEntry<GameConfig<?>> config, boolean test) {
var source = context.getSource();
var server = source.getServer();
var player = source.getPlayer();

var entity = source.getEntity();
var player = entity instanceof ServerPlayerEntity ? (ServerPlayerEntity) entity : null;

server.submit(() -> {
server.execute(() -> {
if (player != null) {
var currentGameSpace = GameSpaceManager.get().byPlayer(player);
if (currentGameSpace != null) {
Expand Down Expand Up @@ -214,7 +212,7 @@ private static void onOpenError(ServerCommandSource source, Throwable throwable)

MutableText message;
if (gameOpenException != null) {
message = ((GameOpenException) throwable).getReason().copy();
message = gameOpenException.getReason().copy();
} else {
message = GameTexts.Broadcast.gameOpenError();
}
Expand Down Expand Up @@ -249,23 +247,23 @@ private static int proposeGame(ServerCommandSource source, GameSpace gameSpace)
}

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

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

return Command.SINGLE_SUCCESS;
}

private static int joinAllGame(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
GameSpace gameSpace = null;

var entity = context.getSource().getEntity();
if (entity instanceof ServerPlayerEntity) {
gameSpace = GameSpaceManager.get().byPlayer((PlayerEntity) entity);
var player = context.getSource().getPlayer();
if (player != null) {
gameSpace = GameSpaceManager.get().byPlayer(player);
}

if (gameSpace == null) {
Expand Down Expand Up @@ -302,10 +300,8 @@ private static void joinAllPlayersToGame(ServerCommandSource source, GameSpace g
}

private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace) {
player.server.submit(() -> {
var results = GamePlayerJoiner.tryJoin(player, gameSpace);
results.sendErrorsTo(player);
});
var results = GamePlayerJoiner.tryJoin(player, gameSpace);
results.sendErrorsTo(player);
}

private static GameSpace getJoinableGameSpace() throws CommandSyntaxException {
Expand Down Expand Up @@ -351,18 +347,16 @@ private static int startGame(CommandContext<ServerCommandSource> context) throws
throw NOT_IN_GAME.create();
}

source.getServer().submit(() -> {
var startResult = gameSpace.requestStart();
var startResult = gameSpace.requestStart();

Text message;
if (startResult.isOk()) {
message = GameTexts.Start.startedBy(source).formatted(Formatting.GRAY);
} else {
message = startResult.errorCopy().formatted(Formatting.RED);
}
Text message;
if (startResult.isOk()) {
message = GameTexts.Start.startedBy(source).formatted(Formatting.GRAY);
} else {
message = startResult.errorCopy().formatted(Formatting.RED);
}

gameSpace.getPlayers().sendMessage(message);
});
gameSpace.getPlayers().sendMessage(message);

return Command.SINGLE_SUCCESS;
}
Expand Down Expand Up @@ -395,20 +389,18 @@ private static int stopGameConfirmed(CommandContext<ServerCommandSource> context
throw NOT_IN_GAME.create();
}

source.getServer().submit(() -> {
var playerSet = gameSpace.getPlayers().copy(source.getServer());
var playerSet = gameSpace.getPlayers().copy(source.getServer());

try {
gameSpace.close(GameCloseReason.CANCELED);
try {
gameSpace.close(GameCloseReason.CANCELED);

var message = GameTexts.Stop.stoppedBy(source);
playerSet.sendMessage(message.formatted(Formatting.GRAY));
} catch (Throwable throwable) {
Plasmid.LOGGER.error("Failed to stop game", throwable);
var message = GameTexts.Stop.stoppedBy(source);
playerSet.sendMessage(message.formatted(Formatting.GRAY));
} catch (Throwable throwable) {
Plasmid.LOGGER.error("Failed to stop game", throwable);

playerSet.sendMessage(GameTexts.Stop.genericError().formatted(Formatting.RED));
}
});
playerSet.sendMessage(GameTexts.Stop.genericError().formatted(Formatting.RED));
}

return Command.SINGLE_SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static int connectEntity(CommandContext<ServerCommandSource> context) th
}

context.getSource().sendFeedback(() -> {
var message = Text.translatable("text.plasmid.game.portal.connect.entity", portal.getId(), entity.getName());
var message = Text.translatable("text.plasmid.game.portal.connect.entity", Text.of(portal.getId()), entity.getName());
return message.formatted(Formatting.GRAY);
}, false);

Expand All @@ -90,7 +90,7 @@ private static int connectBlock(CommandContext<ServerCommandSource> context) thr
}

source.sendFeedback(() -> {
var message = Text.translatable("text.plasmid.game.portal.connect.block", portal.getId(), pos.getX(), pos.getY(), pos.getZ());
var message = Text.translatable("text.plasmid.game.portal.connect.block", Text.of(portal.getId()), pos.getX(), pos.getY(), pos.getZ());
return message.formatted(Formatting.GRAY);
}, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import java.util.function.Function;

public final class GameConfigArgument {
private static final DynamicCommandExceptionType GAME_NOT_FOUND = new DynamicCommandExceptionType(id -> {
return Text.translatable("text.plasmid.game_config.game_not_found", id);
});
private static final DynamicCommandExceptionType GAME_NOT_FOUND = new DynamicCommandExceptionType(id ->
Text.stringifiedTranslatable("text.plasmid.game_config.game_not_found", id)
);

public static RequiredArgumentBuilder<ServerCommandSource, Identifier> argument(String name) {
return CommandManager.argument(name, IdentifierArgumentType.identifier())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public GameJoinUi(ServerPlayerEntity player) {
}

private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace) {
player.server.submit(() -> {
player.server.execute(() -> {
var results = GamePlayerJoiner.tryJoin(player, gameSpace);
results.sendErrorsTo(player);
});
Expand Down

0 comments on commit 0c98fe4

Please sign in to comment.