Skip to content

Commit

Permalink
fix datapacks from hub template not loading on first join
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkGoosik committed Mar 30, 2023
1 parent d1b9ca7 commit 3fcdf90
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/java/skylands/event/ServerStartEvent.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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);
}
}
}
Expand All @@ -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<String> collection = resourcePackManager.getEnabledNames();
Collection<String> dataPacks = findNewDataPacks(resourcePackManager, saveProperties, collection);
server.reloadResources(dataPacks).exceptionally(throwable -> null);
}

private static Collection<String> findNewDataPacks(ResourcePackManager dataPackManager, SaveProperties saveProperties, Collection<String> enabledDataPacks) {
dataPackManager.scanPacks();
Collection<String> collection = Lists.newArrayList(enabledDataPacks);
Collection<String> collection2 = saveProperties.getDataConfiguration().dataPacks().getDisabled();

for(String string : dataPackManager.getNames()) {
if (!collection2.contains(string) && !collection.contains(string)) {
collection.add(string);
}
}

return collection;
}
}

0 comments on commit 3fcdf90

Please sign in to comment.