package com.minelittlepony.unicopia.command; import java.util.HashSet; import java.util.Objects; import java.util.Set; import java.util.function.Function; import com.minelittlepony.common.util.settings.Setting; import com.minelittlepony.unicopia.Config; import com.minelittlepony.unicopia.InteractionManager; import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.network.Channel; import com.minelittlepony.unicopia.network.MsgConfigurationChange; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.SuggestionProvider; import net.minecraft.command.CommandRegistryAccess; import net.minecraft.command.CommandSource; import net.minecraft.command.argument.*; import net.minecraft.registry.Registry; import net.minecraft.registry.RegistryKey; 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.text.Text; import net.minecraft.util.Identifier; public class ConfigCommand { public static LiteralArgumentBuilder create(CommandRegistryAccess registries) { return CommandManager.literal("config").requires(s -> s.hasPermissionLevel(2)) .then(createSet("dimensionswithoutatmosphere", createSuggestion(registries, RegistryKeys.DIMENSION_TYPE))) .then(createSet("wantitneeditentityexcludelist", createSuggestion(registries, RegistryKeys.ENTITY_TYPE))); } private static LiteralArgumentBuilder createSet(String configName, SuggestionProvider suggestions) { return CommandManager.literal(configName) .then(CommandManager.literal("clear").executes(source -> { source.getSource().sendFeedback(() -> Text.translatable("command.unicopia.config.clear", configName), true); return changeProperty(source.getSource(), configName, values -> new HashSet<>()); })) .then(CommandManager.literal("add").then( CommandManager.argument("id", IdentifierArgumentType.identifier()).suggests(suggestions).executes(source -> ConfigCommand.>changeProperty(source.getSource(), configName, values -> { String value = IdentifierArgumentType.getIdentifier(source, "id").toString(); source.getSource().sendFeedback(() -> Text.translatable("command.unicopia.config.add", value, configName), true); values.add(value); return values; }))) ) .then(CommandManager.literal("remove").then( CommandManager.argument("id", IdentifierArgumentType.identifier()).suggests((context, builder) -> { return CommandSource.suggestIdentifiers(Unicopia.getConfig().getCategory("server").>get(configName).get().stream() .map(Identifier::tryParse) .filter(Objects::nonNull) .toList(), builder); }).executes(source -> ConfigCommand.>changeProperty(source.getSource(), configName, values -> { String value = IdentifierArgumentType.getIdentifier(source, "id").toString(); source.getSource().sendFeedback(() -> Text.translatable("command.unicopia.config.remove", value, configName), true); values.remove(value); return values; }))) ) .then(CommandManager.literal("list").executes(source -> ConfigCommand.>getProperty(configName, values -> { source.getSource().sendFeedback(() -> Text.translatable("command.unicopia.config.list", configName, values.size()), false); values.forEach(line -> source.getSource().sendFeedback(() -> Text.literal(line), false)); })) ); } private static SuggestionProvider createSuggestion(CommandRegistryAccess registries, RegistryKey> registryKey) { RegistryWrapper wrapper = registries.createWrapper(registryKey); return (context, builder) -> CommandSource.suggestIdentifiers(wrapper.streamKeys().map(RegistryKey::getValue), builder); } private static int changeProperty(ServerCommandSource source, String configName, Function changer) { Config config = Unicopia.getConfig(); Setting setting = config.getCategory("server").get(configName); setting.set(changer.apply(setting.get())); config.save(); MsgConfigurationChange msg = new MsgConfigurationChange(config.toSynced()); InteractionManager.getInstance().setSyncedConfig(msg.config()); source.getServer().getPlayerManager().getPlayerList().forEach(recipient -> { Channel.CONFIGURATION_CHANGE.sendToPlayer(msg, recipient); }); return 0; } private static int getProperty(String configName, UnsafeConsumer reader) throws CommandSyntaxException { Config config = Unicopia.getConfig(); Setting setting = config.getCategory("server").get(configName); reader.accept(setting.get()); return 0; } interface UnsafeConsumer { void accept(T t) throws CommandSyntaxException; } }