mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-03 08:25:31 +00:00
Refactor pack loading
- Combine initial load and reload logic together between each platform implementation - Should prevent pack load errors from blocking other packs from loading.
This commit is contained in:
parent
4ba71e9c27
commit
59ea5a69d8
@ -18,6 +18,9 @@
|
||||
package com.dfsek.terra;
|
||||
|
||||
import com.dfsek.tectonic.api.TypeRegistry;
|
||||
|
||||
import com.dfsek.terra.registry.master.ConfigRegistry.PackLoadFailuresException;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -147,22 +150,29 @@ public abstract class AbstractPlatform implements Platform {
|
||||
|
||||
eventManager.getHandler(FunctionalEventHandler.class)
|
||||
.register(internalAddon, PlatformInitializationEvent.class)
|
||||
.then(event -> {
|
||||
logger.info("Loading config packs...");
|
||||
try {
|
||||
configRegistry.loadAll(this);
|
||||
} catch(IOException e) {
|
||||
logger.error("Error loading config packs", e);
|
||||
}
|
||||
logger.info("Loaded packs.");
|
||||
})
|
||||
.then(event -> loadConfigPacks())
|
||||
.global();
|
||||
|
||||
|
||||
logger.info("Terra addons successfully loaded.");
|
||||
logger.info("Finished initialization.");
|
||||
}
|
||||
|
||||
protected boolean loadConfigPacks() {
|
||||
logger.info("Loading config packs...");
|
||||
ConfigRegistry configRegistry = getRawConfigRegistry();
|
||||
configRegistry.clear();
|
||||
try {
|
||||
configRegistry.loadAll(this);
|
||||
} catch(IOException e) {
|
||||
logger.error("Failed to load config packs", e);
|
||||
return false;
|
||||
} catch(PackLoadFailuresException e) {
|
||||
e.getExceptions().forEach(ex -> logger.error("Failed to load config pack", ex));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected InternalAddon loadAddons() {
|
||||
List<BaseAddon> addonList = new ArrayList<>();
|
||||
|
||||
|
@ -17,12 +17,12 @@
|
||||
|
||||
package com.dfsek.terra.registry.master;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serial;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.dfsek.terra.api.Platform;
|
||||
@ -41,14 +41,37 @@ public class ConfigRegistry extends OpenRegistryImpl<ConfigPack> {
|
||||
super(TypeKey.of(ConfigPack.class));
|
||||
}
|
||||
|
||||
public void loadAll(Platform platform) throws IOException {
|
||||
public void loadAll(Platform platform) throws IOException, PackLoadFailuresException {
|
||||
Path packsDirectory = platform.getDataFolder().toPath().resolve("packs");
|
||||
Files.createDirectories(packsDirectory);
|
||||
List<IOException> failedLoads = new ArrayList<>();
|
||||
try (Stream<Path> packs = Files.list(packsDirectory)) {
|
||||
for (Path path : packs.toList()) {
|
||||
packs.forEach(path -> {
|
||||
try {
|
||||
ConfigPack pack = new ConfigPackImpl(path, platform);
|
||||
registerChecked(pack.getRegistryKey(), pack);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
failedLoads.add(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!failedLoads.isEmpty()) {
|
||||
throw new PackLoadFailuresException(failedLoads);
|
||||
}
|
||||
}
|
||||
|
||||
public static class PackLoadFailuresException extends Exception {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 538998844645186306L;
|
||||
|
||||
private final List<Throwable> exceptions;
|
||||
|
||||
public PackLoadFailuresException(List<? extends Throwable> exceptions) {
|
||||
this.exceptions = (List<Throwable>) exceptions;
|
||||
}
|
||||
|
||||
public List<Throwable> getExceptions() {
|
||||
return exceptions;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,9 @@ package com.dfsek.terra.bukkit;
|
||||
import com.dfsek.tectonic.api.TypeRegistry;
|
||||
import com.dfsek.tectonic.api.depth.DepthTracker;
|
||||
import com.dfsek.tectonic.api.exception.LoadException;
|
||||
|
||||
import com.dfsek.terra.registry.master.ConfigRegistry.PackLoadFailuresException;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -64,13 +67,7 @@ public class PlatformImpl extends AbstractPlatform {
|
||||
@Override
|
||||
public boolean reload() {
|
||||
getTerraConfig().load(this);
|
||||
getRawConfigRegistry().clear();
|
||||
boolean succeed = true;
|
||||
try {
|
||||
getRawConfigRegistry().loadAll(this);
|
||||
} catch(IOException e) {
|
||||
succeed = false;
|
||||
}
|
||||
boolean succeed = loadConfigPacks();
|
||||
|
||||
Bukkit.getWorlds().forEach(world -> {
|
||||
if(world.getGenerator() instanceof BukkitChunkGeneratorWrapper wrapper) {
|
||||
|
@ -20,6 +20,9 @@ package com.dfsek.terra.forge;
|
||||
import ca.solostudios.strata.Versions;
|
||||
import ca.solostudios.strata.parser.tokenizer.ParseException;
|
||||
import ca.solostudios.strata.version.Version;
|
||||
|
||||
import com.dfsek.terra.registry.master.ConfigRegistry.PackLoadFailuresException;
|
||||
|
||||
import net.minecraft.MinecraftVersion;
|
||||
import net.minecraft.registry.Registry;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
@ -63,13 +66,7 @@ public class ForgePlatform extends ModPlatform {
|
||||
@Override
|
||||
public boolean reload() {
|
||||
getTerraConfig().load(this);
|
||||
getRawConfigRegistry().clear();
|
||||
boolean succeed = true;
|
||||
try {
|
||||
getRawConfigRegistry().loadAll(this);
|
||||
} catch(IOException e) {
|
||||
succeed = false;
|
||||
}
|
||||
boolean succeed = loadConfigPacks();
|
||||
|
||||
MinecraftServer server = getServer();
|
||||
|
||||
|
@ -3,6 +3,9 @@ package com.dfsek.terra.lifecycle;
|
||||
import ca.solostudios.strata.Versions;
|
||||
import ca.solostudios.strata.parser.tokenizer.ParseException;
|
||||
import ca.solostudios.strata.version.Version;
|
||||
|
||||
import com.dfsek.terra.registry.master.ConfigRegistry.PackLoadFailuresException;
|
||||
|
||||
import net.minecraft.MinecraftVersion;
|
||||
import net.minecraft.registry.Registry;
|
||||
import net.minecraft.registry.RegistryKeys;
|
||||
@ -64,13 +67,7 @@ public abstract class LifecyclePlatform extends ModPlatform {
|
||||
@Override
|
||||
public boolean reload() {
|
||||
getTerraConfig().load(this);
|
||||
getRawConfigRegistry().clear();
|
||||
boolean succeed = true;
|
||||
try {
|
||||
getRawConfigRegistry().loadAll(this);
|
||||
} catch(IOException e) {
|
||||
succeed = false;
|
||||
}
|
||||
boolean succeed = loadConfigPacks();
|
||||
|
||||
if(server != null) {
|
||||
BiomeUtil.registerBiomes(server.getRegistryManager().get(RegistryKeys.BIOME));
|
||||
|
Loading…
x
Reference in New Issue
Block a user