2020-04-15 14:22:03 +02:00
|
|
|
package com.minelittlepony.unicopia.command;
|
2020-01-27 11:05:22 +01:00
|
|
|
|
2020-04-15 14:22:03 +02:00
|
|
|
import com.minelittlepony.unicopia.Race;
|
2023-01-22 00:27:23 +01:00
|
|
|
import com.minelittlepony.unicopia.Unicopia;
|
2020-09-22 15:11:20 +02:00
|
|
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
2020-01-27 11:05:22 +01:00
|
|
|
import com.mojang.brigadier.CommandDispatcher;
|
|
|
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
|
|
|
|
2020-09-22 15:11:20 +02:00
|
|
|
import net.minecraft.command.argument.EntityArgumentType;
|
2022-08-27 15:07:29 +02:00
|
|
|
import net.minecraft.command.argument.RegistryKeyArgumentType;
|
2020-01-27 11:05:22 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.server.command.CommandManager;
|
2023-01-22 00:27:23 +01:00
|
|
|
import net.minecraft.server.command.CommandManager.RegistrationEnvironment;
|
2020-01-27 11:05:22 +01:00
|
|
|
import net.minecraft.server.command.ServerCommandSource;
|
2020-06-26 11:44:47 +02:00
|
|
|
import net.minecraft.text.MutableText;
|
2020-01-27 11:05:22 +01:00
|
|
|
import net.minecraft.text.Text;
|
|
|
|
import net.minecraft.util.Formatting;
|
2022-08-27 15:57:13 +02:00
|
|
|
import net.minecraft.util.Identifier;
|
2020-01-27 11:05:22 +01:00
|
|
|
import net.minecraft.world.GameRules;
|
|
|
|
|
|
|
|
class SpeciesCommand {
|
2023-01-22 00:27:23 +01:00
|
|
|
static void register(CommandDispatcher<ServerCommandSource> dispatcher, RegistrationEnvironment environment) {
|
2020-01-27 11:05:22 +01:00
|
|
|
LiteralArgumentBuilder<ServerCommandSource> builder = CommandManager.literal("race");
|
|
|
|
|
2023-01-22 00:27:23 +01:00
|
|
|
if (environment.dedicated) {
|
|
|
|
if (Unicopia.getConfig().enableCheats.get()) {
|
|
|
|
builder = builder.requires(source -> source.hasPermissionLevel(2));
|
|
|
|
} else {
|
|
|
|
builder = builder.requires(source -> source.hasPermissionLevel(4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 15:07:29 +02:00
|
|
|
RegistryKeyArgumentType<Race> raceArgument = Race.argument();
|
2021-12-28 17:22:08 +01:00
|
|
|
|
2020-01-27 11:05:22 +01:00
|
|
|
builder.then(CommandManager.literal("get")
|
|
|
|
.executes(context -> get(context.getSource(), context.getSource().getPlayer(), true))
|
|
|
|
.then(CommandManager.argument("target", EntityArgumentType.player())
|
|
|
|
.executes(context -> get(context.getSource(), EntityArgumentType.getPlayer(context, "target"), false))
|
|
|
|
));
|
|
|
|
|
|
|
|
builder.then(CommandManager.literal("set")
|
2021-12-28 17:22:08 +01:00
|
|
|
.then(CommandManager.argument("race", raceArgument)
|
2022-08-27 15:51:49 +02:00
|
|
|
.executes(context -> set(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), true))
|
2020-01-27 11:05:22 +01:00
|
|
|
.then(CommandManager.argument("target", EntityArgumentType.player())
|
2022-08-27 15:51:49 +02:00
|
|
|
.executes(context -> set(context.getSource(), EntityArgumentType.getPlayer(context, "target"), Race.fromArgument(context, "race"), false)))
|
|
|
|
));
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
builder.then(CommandManager.literal("describe")
|
2021-12-28 17:22:08 +01:00
|
|
|
.then(CommandManager.argument("race", raceArgument)
|
2022-08-27 15:51:49 +02:00
|
|
|
.executes(context -> describe(context.getSource().getPlayer(), Race.fromArgument(context, "race")))
|
|
|
|
));
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
builder.then(CommandManager.literal("list")
|
|
|
|
.executes(context -> list(context.getSource().getPlayer())
|
2022-08-27 15:51:49 +02:00
|
|
|
));
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
dispatcher.register(builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set(ServerCommandSource source, PlayerEntity player, Race race, boolean isSelf) {
|
|
|
|
|
2020-04-15 18:12:00 +02:00
|
|
|
if (race.isPermitted(player)) {
|
2020-04-26 12:07:25 +02:00
|
|
|
Pony pony = Pony.of(player);
|
|
|
|
pony.setSpecies(race);
|
2020-05-10 17:18:45 +02:00
|
|
|
pony.setDirty();
|
2020-01-27 11:05:22 +01:00
|
|
|
|
2023-01-27 15:37:25 +01:00
|
|
|
if (player == source.getPlayer()) {
|
|
|
|
source.sendFeedback(Text.translatable("commands.race.success.self", player.getName(), race.getDisplayName()), true);
|
2020-01-27 11:05:22 +01:00
|
|
|
} else {
|
|
|
|
if (player.getEntityWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) {
|
2023-01-27 15:37:25 +01:00
|
|
|
player.sendMessage(Text.translatable("commands.race.success.other"), false);
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
2022-08-27 15:07:29 +02:00
|
|
|
source.sendFeedback(Text.translatable("commands.race.success.otherself", player.getName(), race.getDisplayName()), true);
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
} else if (player.getEntityWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) {
|
2022-06-25 00:19:55 +02:00
|
|
|
player.sendMessage(Text.translatable("commands.race.permission"), false);
|
2020-01-27 11:05:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get(ServerCommandSource source, PlayerEntity player, boolean isSelf) {
|
2022-09-30 10:21:18 +02:00
|
|
|
Race spec = Pony.of(player).getActualSpecies();
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
String name = "commands.race.tell.";
|
|
|
|
name += isSelf ? "self" : "other";
|
|
|
|
|
2022-06-25 00:19:55 +02:00
|
|
|
player.sendMessage(Text.translatable(name)
|
|
|
|
.append(Text.translatable(spec.getTranslationKey())
|
2020-06-26 11:44:47 +02:00
|
|
|
.styled(s -> s.withColor(Formatting.GOLD))), false);
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int list(PlayerEntity player) {
|
2022-06-25 00:19:55 +02:00
|
|
|
player.sendMessage(Text.translatable("commands.race.list"), false);
|
2020-01-27 11:05:22 +01:00
|
|
|
|
2022-06-25 00:19:55 +02:00
|
|
|
MutableText message = Text.literal("");
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
boolean first = true;
|
2022-08-27 15:07:29 +02:00
|
|
|
for (Race i : Race.REGISTRY) {
|
2023-01-21 01:28:59 +01:00
|
|
|
if (!i.isUnset() && i.isPermitted(player)) {
|
2022-08-27 15:07:29 +02:00
|
|
|
message.append(Text.literal((!first ? "\n" : "") + " - "));
|
|
|
|
message.append(i.getDisplayName());
|
2020-01-27 11:05:22 +01:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 11:44:47 +02:00
|
|
|
player.sendMessage(message.styled(s -> s.withColor(Formatting.GOLD)), false);
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int describe(PlayerEntity player, Race species) {
|
2022-08-27 15:57:13 +02:00
|
|
|
Identifier id = Race.REGISTRY.getId(species);
|
2020-01-27 11:05:22 +01:00
|
|
|
|
2022-08-27 15:57:13 +02:00
|
|
|
player.sendMessage(Text.translatable(String.format("commands.race.describe.%s.%s.1", id.getNamespace(), id.getPath())).styled(s -> s.withColor(Formatting.YELLOW)), false);
|
|
|
|
player.sendMessage(Text.translatable(String.format("commands.race.describe.%s.%s.2", id.getNamespace(), id.getPath())), false);
|
|
|
|
player.sendMessage(Text.translatable(String.format("commands.race.describe.%s.%s.3", id.getNamespace(), id.getPath())).styled(s -> s.withColor(Formatting.RED)), false);
|
2020-01-27 11:05:22 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|