From 9d03ad75769ae53379cd3f08db5294e1b8b03270 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 16 Sep 2024 19:58:41 +0100 Subject: [PATCH] Fix command outputs. Fixes #435 --- .../unicopia/command/ConfigCommand.java | 7 +-- .../unicopia/command/GravityCommand.java | 14 ++--- .../unicopia/command/ManaCommand.java | 5 +- .../unicopia/command/SpeciesCommand.java | 52 ++++++++++--------- .../unicopia/command/TraitCommand.java | 5 +- 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/command/ConfigCommand.java b/src/main/java/com/minelittlepony/unicopia/command/ConfigCommand.java index 81b0abab..fb9b5ab2 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/ConfigCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/ConfigCommand.java @@ -23,7 +23,6 @@ import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.RegistryWrapper; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.Identifier; @@ -63,10 +62,8 @@ public class ConfigCommand { }))) ) .then(CommandManager.literal("list").executes(source -> ConfigCommand.>getProperty(configName, values -> { - ServerPlayerEntity player = source.getSource().getPlayerOrThrow(); - - player.sendMessage(Text.translatable("command.unicopia.config.list", configName, values.size()), false); - values.forEach(line -> player.sendMessage(Text.literal(line))); + source.getSource().sendFeedback(() -> Text.translatable("command.unicopia.config.list", configName, values.size()), false); + values.forEach(line -> source.getSource().sendFeedback(() -> Text.literal(line), false)); })) ); } diff --git a/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java b/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java index a97272e9..d071b6bf 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java @@ -50,17 +50,19 @@ class GravityCommand { l.getPhysics().setBaseGravityModifier(gravity); if (l.asEntity() instanceof PlayerEntity player) { if (source.getEntity() == player) { - player.sendMessage(Text.translatable("commands.gravity.set.self", gravity)); - } else if (source.getWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) { - player.sendMessage(Text.translatable("commands.gravity.set.other", l.asEntity().getDisplayName(), gravity)); + source.sendFeedback(() -> Text.translatable("commands.gravity.set.self", gravity), true); + } else { + if (source.getWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) { + player.sendMessage(Text.translatable("commands.gravity.set", gravity)); + } + + source.sendFeedback(() -> Text.translatable("commands.gravity.set.other", l.asEntity().getDisplayName(), gravity), true); } } return (Entity)l.asEntity(); }).toList(); - if (affected.size() == 1) { - source.sendFeedback(() -> Text.translatable("commands.gravity.set.other", affected.get(0).getDisplayName()), true); - } else { + if (affected.size() > 1) { source.sendFeedback(() -> Text.translatable("commands.gravity.set.multiple", affected.size()), true); } return 0; diff --git a/src/main/java/com/minelittlepony/unicopia/command/ManaCommand.java b/src/main/java/com/minelittlepony/unicopia/command/ManaCommand.java index 6383163a..18c6fd8b 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/ManaCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/ManaCommand.java @@ -23,7 +23,7 @@ public class ManaCommand { var pony = Pony.of(source.getSource().getPlayer()); var bar = type.getBar(pony.getMagicalReserves()); - source.getSource().getPlayer().sendMessage(Text.literal(type.name() + " is " + bar.get() + "/" + bar.getMax())); + source.getSource().sendFeedback(() -> Text.literal(type.name() + " is " + bar.get() + "/" + bar.getMax()), true); return 0; }) .then(CommandManager.argument("value", FloatArgumentType.floatArg()).executes(source -> { @@ -47,7 +47,8 @@ public class ManaCommand { pony.asWorld().playSound(null, pony.getOrigin(), USounds.Vanilla.ENTITY_PLAYER_LEVELUP, SoundCategory.PLAYERS, 1, 2); } bar.set(value); - source.getSource().getPlayer().sendMessage(Text.literal("Set " + type.name() + " to " + bar.get() + "/" + bar.getMax())); + var t = type; + source.getSource().sendFeedback(() -> Text.literal("Set " + t.name() + " to " + bar.get() + "/" + bar.getMax()), true); return 0; }))); } diff --git a/src/main/java/com/minelittlepony/unicopia/command/SpeciesCommand.java b/src/main/java/com/minelittlepony/unicopia/command/SpeciesCommand.java index e1e9e999..5fecdbe6 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/SpeciesCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/SpeciesCommand.java @@ -44,10 +44,10 @@ class SpeciesCommand { )) .then(CommandManager.literal("describe") .then(CommandManager.argument("race", Race.argument()).suggests(UCommandSuggestion.ALL_RACE_SUGGESTIONS) - .executes(context -> describe(context.getSource().getPlayer(), Race.fromArgument(context, "race"))) + .executes(context -> describe(context.getSource(), Race.fromArgument(context, "race"))) )) .then(CommandManager.literal("list") - .executes(context -> list(context.getSource().getPlayer()) + .executes(context -> list(context.getSource()) )); } @@ -70,57 +70,59 @@ class SpeciesCommand { } source.sendFeedback(() -> Text.translatable("commands.race.success.other", player.getName(), race.getDisplayName()), true); } - } else if (player.getEntityWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) { - player.sendMessage(Text.translatable("commands.race.permission"), false); + } else { + source.sendFeedback(() -> Text.translatable("commands.race.permission"), false); } return 0; } static int get(ServerCommandSource source, PlayerEntity player, boolean isSelf) { - Race spec = Pony.of(player).getSpecies(); + source.sendFeedback(() -> { + Race spec = Pony.of(player).getSpecies(); - String name = "commands.race.tell."; - name += isSelf ? "self" : "other"; + String name = "commands.race.tell."; + name += isSelf ? "self" : "other"; - player.sendMessage(Text.translatable(name, player.getName()) + return Text.translatable(name, player.getName()) .append(Text.translatable(spec.getTranslationKey()) - .styled(s -> s.withColor(Formatting.GOLD))), false); - + .styled(s -> s.withColor(Formatting.GOLD))); + }, false); return 0; } - static int list(PlayerEntity player) { - player.sendMessage(Text.translatable("commands.race.list"), false); + static int list(ServerCommandSource source) { + source.sendFeedback(() -> Text.translatable("commands.race.list"), false); + source.sendFeedback(() -> { + MutableText message = Text.literal(""); - MutableText message = Text.literal(""); - - boolean first = true; - for (Race i : Race.REGISTRY) { - if (i.availability().isGrantable() && !i.isUnset() && i.isPermitted(player)) { - message.append(Text.literal((!first ? "\n" : "") + " - ")); - message.append(i.getDisplayName()); - first = false; + boolean first = true; + for (Race i : Race.REGISTRY) { + if (i.availability().isGrantable() && !i.isUnset() && i.isPermitted(source.getPlayer())) { + message.append(Text.literal((!first ? "\n" : "") + " - ")); + message.append(i.getDisplayName()); + first = false; + } } - } - player.sendMessage(message.styled(s -> s.withColor(Formatting.GOLD)), false); + return message.styled(s -> s.withColor(Formatting.GOLD)); + }, false); return 0; } - static int describe(PlayerEntity player, Race species) { + static int describe(ServerCommandSource source, Race species) { Identifier id = Race.REGISTRY.getId(species); for (String category : new String[] { "goods", "bads" }) { - player.sendMessage(Text.translatable( + source.sendFeedback(() -> Text.translatable( String.format("gui.unicopia.tribe_selection.confirm.%s.%d.%s.%s", category), species.getAltDisplayName() ), false); for (int i = 1; i < 5; i++) { String line = String.format("gui.unicopia.tribe_selection.confirm.%s.%d.%s.%s", category, i, id.getNamespace(), id.getPath()); - player.sendMessage(Text.translatable(line).styled(s -> s.withColor(category.equals("goods") ? Formatting.YELLOW : Formatting.RED)), false); + source.sendFeedback(() -> Text.translatable(line).styled(s -> s.withColor(category.equals("goods") ? Formatting.YELLOW : Formatting.RED)), false); } } diff --git a/src/main/java/com/minelittlepony/unicopia/command/TraitCommand.java b/src/main/java/com/minelittlepony/unicopia/command/TraitCommand.java index 04bcf078..560eaecc 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/TraitCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/TraitCommand.java @@ -13,6 +13,7 @@ import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.*; import net.minecraft.util.Hand; +import net.minecraft.world.GameRules; class TraitCommand { static LiteralArgumentBuilder create() { @@ -78,7 +79,9 @@ class TraitCommand { float gravity = iplayer.getPhysics().getGravityModifier(); if (source.getPlayer() == player) { - player.sendMessage(Text.translatable(translationKey, gravity), false); + if (player.getEntityWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) { + player.sendMessage(Text.translatable(translationKey, gravity), false); + } } else { source.sendFeedback(() -> Text.translatable(translationKey + ".other", player.getName(), gravity), true); }