diff --git a/src/main/java/skylands/event/ServerStartEvent.java b/src/main/java/skylands/event/ServerStartEvent.java index 75f9c3a..fb4f499 100644 --- a/src/main/java/skylands/event/ServerStartEvent.java +++ b/src/main/java/skylands/event/ServerStartEvent.java @@ -1,13 +1,17 @@ package skylands.event; +import com.google.common.collect.Lists; import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.resource.ResourcePackManager; import net.minecraft.server.MinecraftServer; import net.minecraft.util.WorldSavePath; +import net.minecraft.world.SaveProperties; import org.apache.commons.io.FileUtils; import skylands.SkylandsMod; import skylands.logic.Skylands; import java.io.File; +import java.util.Collection; public class ServerStartEvent { @@ -24,6 +28,8 @@ public static void onStarting(MinecraftServer server) { if(hubTemplate.exists() && !lock.exists()) { FileUtils.copyDirectory(hubTemplate, new File(path)); lock.createNewFile(); + SkylandsMod.LOGGER.info("Reloading datapacks from hub template..."); + reloadDatapacks(server); } } } @@ -41,4 +47,26 @@ public static void onStarting(MinecraftServer server) { }); } } + + private static void reloadDatapacks(MinecraftServer server) { + ResourcePackManager resourcePackManager = server.getDataPackManager(); + SaveProperties saveProperties = server.getSaveProperties(); + Collection collection = resourcePackManager.getEnabledNames(); + Collection dataPacks = findNewDataPacks(resourcePackManager, saveProperties, collection); + server.reloadResources(dataPacks).exceptionally(throwable -> null); + } + + private static Collection findNewDataPacks(ResourcePackManager dataPackManager, SaveProperties saveProperties, Collection enabledDataPacks) { + dataPackManager.scanPacks(); + Collection collection = Lists.newArrayList(enabledDataPacks); + Collection collection2 = saveProperties.getDataConfiguration().dataPacks().getDisabled(); + + for(String string : dataPackManager.getNames()) { + if (!collection2.contains(string) && !collection.contains(string)) { + collection.add(string); + } + } + + return collection; + } }