package net.minecraft.server.commands; import com.google.common.collect.Lists; import com.mojang.brigadier.CommandDispatcher; import com.mojang.logging.LogUtils; import java.util.Collection; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.network.chat.Component; import net.minecraft.server.MinecraftServer; import net.minecraft.server.packs.repository.PackRepository; import net.minecraft.world.level.storage.WorldData; import org.slf4j.Logger; public class ReloadCommand { private static final Logger LOGGER = LogUtils.getLogger(); public static void reloadPacks(Collection selectedIds, CommandSourceStack source) { source.getServer().reloadResources(selectedIds).exceptionally(throwable -> { LOGGER.warn("Failed to execute reload", throwable); source.sendFailure(Component.translatable("commands.reload.failure")); return null; }); } /** * Gets a list of IDs for the selected packs as well as all packs not disabled by the world config. */ private static Collection discoverNewPacks(PackRepository packRepository, WorldData worldData, Collection selectedIds) { packRepository.reload(); Collection collection = Lists.newArrayList(selectedIds); Collection collection2 = worldData.getDataConfiguration().dataPacks().getDisabled(); for (String string : packRepository.getAvailableIds()) { if (!collection2.contains(string) && !collection.contains(string)) { collection.add(string); } } return collection; } public static void register(CommandDispatcher dispatcher) { dispatcher.register(Commands.literal("reload").requires(commandSourceStack -> commandSourceStack.hasPermission(2)).executes(commandContext -> { CommandSourceStack commandSourceStack = commandContext.getSource(); MinecraftServer minecraftServer = commandSourceStack.getServer(); PackRepository packRepository = minecraftServer.getPackRepository(); WorldData worldData = minecraftServer.getWorldData(); Collection collection = packRepository.getSelectedIds(); Collection collection2 = discoverNewPacks(packRepository, worldData, collection); commandSourceStack.sendSuccess(() -> Component.translatable("commands.reload.success"), true); reloadPacks(collection2, commandSourceStack); return 0; })); } }