Skip to content

Commit

Permalink
Merge pull request #301 from RacoonDog/master
Browse files Browse the repository at this point in the history
better error reporting on initialization exceptions
  • Loading branch information
19MisterX98 authored May 31, 2024
2 parents 535128e + c3b4e6e commit 0ae7b36
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
58 changes: 39 additions & 19 deletions src/main/java/kaptainwutax/seedcrackerX/Features.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package kaptainwutax.seedcrackerX;

import com.seedfinding.mccore.version.MCVersion;
import com.seedfinding.mcfeature.Feature;
import com.seedfinding.mcfeature.decorator.DesertWell;
import com.seedfinding.mcfeature.decorator.EndGateway;
import com.seedfinding.mcfeature.structure.*;
import kaptainwutax.seedcrackerX.cracker.decorator.DeepDungeon;
import kaptainwutax.seedcrackerX.cracker.decorator.Dungeon;
import kaptainwutax.seedcrackerX.cracker.decorator.EmeraldOre;
import kaptainwutax.seedcrackerX.cracker.decorator.WarpedFungus;
import kaptainwutax.seedcrackerX.finder.Finder;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class Features {
public static final ArrayList<RegionStructure<?, ?>> STRUCTURE_TYPES = new ArrayList<>();

public static BuriedTreasure BURIED_TREASURE;
public static DesertPyramid DESERT_PYRAMID;
Expand All @@ -29,29 +36,42 @@ public class Features {
public static WarpedFungus WARPED_FUNGUS;

public static void init(MCVersion version) {
safe(() -> BURIED_TREASURE = new BuriedTreasure(version));
safe(() -> DESERT_PYRAMID = new DesertPyramid(version));
safe(() -> END_CITY = new EndCity(version));
safe(() -> JUNGLE_PYRAMID = new JunglePyramid(version));
safe(() -> MONUMENT = new Monument(version));
safe(() -> SHIPWRECK = new Shipwreck(version));
safe(() -> SWAMP_HUT = new SwampHut(version));
safe(() -> PILLAGER_OUTPOST = new PillagerOutpost(version));
safe(() -> IGLOO = new Igloo(version));

safe(() -> END_GATEWAY = new EndGateway(version));
safe(() -> DESERT_WELL = new DesertWell(version));
safe(() -> EMERALD_ORE = new EmeraldOre(version));
safe(() -> DUNGEON = new Dungeon(version));
safe(() -> DEEP_DUNGEON = new DeepDungeon(version));
safe(() -> WARPED_FUNGUS = new WarpedFungus(version));
STRUCTURE_TYPES.clear();

BURIED_TREASURE = safe(STRUCTURE_TYPES, Finder.Type.BURIED_TREASURE, () -> new BuriedTreasure(version));
DESERT_PYRAMID = safe(STRUCTURE_TYPES, Finder.Type.DESERT_TEMPLE, () -> new DesertPyramid(version));
END_CITY = safe(STRUCTURE_TYPES, Finder.Type.END_CITY, () -> new EndCity(version));
JUNGLE_PYRAMID = safe(STRUCTURE_TYPES, Finder.Type.JUNGLE_TEMPLE, () -> new JunglePyramid(version));
MONUMENT = safe(STRUCTURE_TYPES, Finder.Type.MONUMENT, () -> new Monument(version));
SHIPWRECK = safe(STRUCTURE_TYPES, Finder.Type.SHIPWRECK, () -> new Shipwreck(version));
SWAMP_HUT = safe(STRUCTURE_TYPES, Finder.Type.SWAMP_HUT, () -> new SwampHut(version));
PILLAGER_OUTPOST = safe(STRUCTURE_TYPES, Finder.Type.PILLAGER_OUTPOST, () -> new PillagerOutpost(version));
IGLOO = safe(STRUCTURE_TYPES, Finder.Type.IGLOO, () -> new Igloo(version));

END_GATEWAY = safe(Finder.Type.END_GATEWAY, () -> new EndGateway(version));
DESERT_WELL = safe(Finder.Type.DESERT_WELL, () -> new DesertWell(version));
EMERALD_ORE = safe(Finder.Type.EMERALD_ORE, () -> new EmeraldOre(version));
DUNGEON = safe(Finder.Type.DUNGEON, () -> new Dungeon(version));
DEEP_DUNGEON = safe(Finder.Type.DUNGEON, () -> new DeepDungeon(version));
WARPED_FUNGUS = safe(Finder.Type.WARPED_FUNGUS, () -> new WarpedFungus(version));

STRUCTURE_TYPES.trimToSize();
}

private static void safe(Runnable runnable) {
private static <F extends Feature<?, ?>> F safe(Finder.Type finderType, Supplier<F> lambda) {
try {
runnable.run();
} catch (Exception e) {
return lambda.get();
} catch (Throwable t) {
SeedCracker.LOGGER.error("Exception thrown loading feature", t);
finderType.enabled.set(false);
return null;
}
}

private static <F extends RegionStructure<?, ?>> F safe(List<RegionStructure<?, ?>> list, Finder.Type finderType, Supplier<F> lambda) {
F initializedFeature = safe(finderType, lambda);
if (initializedFeature != null) list.add(initializedFeature);
return initializedFeature;
}

}
4 changes: 3 additions & 1 deletion src/main/java/kaptainwutax/seedcrackerX/SeedCracker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kaptainwutax.seedcrackerX;

import com.mojang.logging.LogUtils;
import kaptainwutax.seedcrackerX.api.SeedCrackerAPI;
import kaptainwutax.seedcrackerX.config.Config;
import kaptainwutax.seedcrackerX.cracker.storage.DataStorage;
Expand All @@ -8,11 +9,12 @@
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.loader.api.FabricLoader;
import org.slf4j.Logger;

import java.util.ArrayList;

public class SeedCracker implements ModInitializer {

public static final Logger LOGGER = LogUtils.getLogger();
public static final ArrayList<SeedCrackerAPI> entrypoints = new ArrayList<>();
private static SeedCracker INSTANCE;
private final DataStorage dataStorage = new DataStorage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public class StructureSave {
private static final Logger logger = LoggerFactory.getLogger("structureSave");

public static final Path saveDir = Paths.get(FabricLoader.getInstance().getConfigDir().toFile().toString(), "SeedCrackerX saved structures");
private static final List<RegionStructure<?,?>> structureTypes = List.of(Features.IGLOO,Features.BURIED_TREASURE,
Features.PILLAGER_OUTPOST,Features.DESERT_PYRAMID, Features.JUNGLE_PYRAMID, Features.END_CITY,
Features.MONUMENT, Features.SHIPWRECK, Features.SWAMP_HUT);

public static void saveStructures(ScheduledSet<DataStorage.Entry<Feature.Data<?>>> baseData) {
try {
Expand Down Expand Up @@ -66,7 +63,7 @@ public static List<RegionStructure.Data<?>> loadStructures() {
String[] info = line.split(";");
if (info.length != 3) continue;
String structureName = info[0];
for (RegionStructure<?,?> idk : structureTypes) {
for (RegionStructure<?,?> idk : Features.STRUCTURE_TYPES) {
if (structureName.equals(idk.getName())) {
result.add(idk.at(Integer.parseInt(info[1]), Integer.parseInt(info[2])));
break;
Expand Down

0 comments on commit 0ae7b36

Please sign in to comment.