| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.brigadier.CommandDispatcher;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.util.Collection;
|
| 7 | import net.minecraft.commands.CommandSourceStack;
|
| 8 | import net.minecraft.commands.Commands;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.server.MinecraftServer;
|
| 11 | import net.minecraft.server.packs.repository.PackRepository;
|
| 12 | import net.minecraft.world.level.storage.WorldData;
|
| 13 | import org.slf4j.Logger;
|
| 14 |
|
| 15 | public class ReloadCommand {
|
| 16 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 17 |
|
| 18 | public static void reloadPacks(final Collection<String> selectedPacks, final CommandSourceStack source) {
|
| 19 | source.getServer().reloadResources(selectedPacks).exceptionally(throwable -> {
|
| 20 | LOGGER.warn("Failed to execute reload", throwable);
|
| 21 | source.sendFailure(Component.translatable("commands.reload.failure"));
|
| 22 | return null;
|
| 23 | });
|
| 24 | }
|
| 25 |
|
| 26 | private static Collection<String> discoverNewPacks(final PackRepository packRepository, final WorldData worldData, final Collection<String> currentPacks) {
|
| 27 | packRepository.reload();
|
| 28 | Collection<String> selected = Lists.newArrayList(currentPacks);
|
| 29 | Collection<String> disabled = worldData.getDataConfiguration().dataPacks().getDisabled();
|
| 30 |
|
| 31 | for (String pack : packRepository.getAvailableIds()) {
|
| 32 | if (!disabled.contains(pack) && !selected.contains(pack)) {
|
| 33 | selected.add(pack);
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | return selected;
|
| 38 | }
|
| 39 |
|
| 40 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 41 | dispatcher.register(Commands.literal("reload").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS)).executes(s -> {
|
| 42 | CommandSourceStack source = s.getSource();
|
| 43 | MinecraftServer server = source.getServer();
|
| 44 | PackRepository packRepository = server.getPackRepository();
|
| 45 | WorldData worldData = server.getWorldData();
|
| 46 | Collection<String> currentPacks = packRepository.getSelectedIds();
|
| 47 | Collection<String> newSelectedPacks = discoverNewPacks(packRepository, worldData, currentPacks);
|
| 48 | source.sendSuccess(() -> Component.translatable("commands.reload.success"), true);
|
| 49 | reloadPacks(newSelectedPacks, source);
|
| 50 | return 0;
|
| 51 | }));
|
| 52 | }
|
| 53 | }
|
| 54 |
|