diff --git a/common/src/main/java/com/dfsek/terra/api/TerraPlugin.java b/common/src/main/java/com/dfsek/terra/api/TerraPlugin.java index aeeb5e814..2982da04f 100644 --- a/common/src/main/java/com/dfsek/terra/api/TerraPlugin.java +++ b/common/src/main/java/com/dfsek/terra/api/TerraPlugin.java @@ -7,6 +7,7 @@ import com.dfsek.terra.api.platform.handle.WorldHandle; import com.dfsek.terra.api.platform.world.World; import com.dfsek.terra.api.registry.CheckedRegistry; import com.dfsek.terra.api.registry.LockedRegistry; +import com.dfsek.terra.api.util.JarUtil; import com.dfsek.terra.api.util.logging.DebugLogger; import com.dfsek.terra.api.util.logging.Logger; import com.dfsek.terra.config.PluginConfig; @@ -16,6 +17,9 @@ import com.dfsek.terra.profiler.Profiler; import com.dfsek.terra.world.TerraWorld; import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.jar.JarFile; /** * Represents a Terra mod/plugin instance. @@ -67,4 +71,8 @@ public interface TerraPlugin extends LoaderRegistrar { } Profiler getProfiler(); + + default JarFile getModJar() throws URISyntaxException, IOException { + return JarUtil.getJarFile(); + } } diff --git a/common/src/main/java/com/dfsek/terra/api/util/JarUtil.java b/common/src/main/java/com/dfsek/terra/api/util/JarUtil.java index 20ad875cc..981dfdb7f 100644 --- a/common/src/main/java/com/dfsek/terra/api/util/JarUtil.java +++ b/common/src/main/java/com/dfsek/terra/api/util/JarUtil.java @@ -1,9 +1,13 @@ package com.dfsek.terra.api.util; +import com.dfsek.terra.api.TerraPlugin; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URISyntaxException; +import java.net.URL; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -32,4 +36,12 @@ public class JarUtil { } } } + + public static JarFile getJarFile() throws URISyntaxException, IOException { + return new JarFile(new File(getJarURL().toURI())); + } + + public static URL getJarURL() { + return TerraPlugin.class.getProtectionDomain().getCodeSource().getLocation(); + } } diff --git a/common/src/main/java/com/dfsek/terra/config/PluginConfig.java b/common/src/main/java/com/dfsek/terra/config/PluginConfig.java index e1112dcf4..d391fed8e 100644 --- a/common/src/main/java/com/dfsek/terra/config/PluginConfig.java +++ b/common/src/main/java/com/dfsek/terra/config/PluginConfig.java @@ -69,12 +69,12 @@ public class PluginConfig implements ConfigTemplate { ConfigLoader loader = new ConfigLoader(); loader.load(this, file); if(dumpDefaultConfig) { // Don't dump default config if already loaded. - try(JarFile jar = new JarFile(new File(TerraPlugin.class.getProtectionDomain().getCodeSource().getLocation().toURI()))) { + try(JarFile jar = main.getModJar()) { JarUtil.copyResourcesToDirectory(jar, "packs", new File(main.getDataFolder(), "packs").toString()); } catch(IOException | URISyntaxException e) { main.getDebugLogger().error("Failed to dump default config files!"); e.printStackTrace(); - main.getDebugLogger().error("Report this to Terra!"); + main.getDebugLogger().error("Either you're on Forge, or this is a bug. If it's the latter, report this to Terra!"); } } } catch(ConfigException | IOException e) { diff --git a/common/src/main/java/com/dfsek/terra/config/lang/LangUtil.java b/common/src/main/java/com/dfsek/terra/config/lang/LangUtil.java index 347a1e577..87f24b13e 100644 --- a/common/src/main/java/com/dfsek/terra/config/lang/LangUtil.java +++ b/common/src/main/java/com/dfsek/terra/config/lang/LangUtil.java @@ -17,7 +17,7 @@ public final class LangUtil { public static void load(String langID, TerraPlugin main) { Logger logger = main.logger(); File file = new File(main.getDataFolder(), "lang"); - try(JarFile jar = new JarFile(new File(TerraPlugin.class.getProtectionDomain().getCodeSource().getLocation().toURI()))) { + try(JarFile jar = main.getModJar()) { copyResourcesToDirectory(jar, "lang", file.toString()); } catch(IOException | URISyntaxException e) { main.getDebugLogger().error("Failed to dump language files!"); diff --git a/platforms/forge/src/main/java/com/dfsek/terra/forge/TerraForgePlugin.java b/platforms/forge/src/main/java/com/dfsek/terra/forge/TerraForgePlugin.java index 8f2243b33..b7cab9f61 100644 --- a/platforms/forge/src/main/java/com/dfsek/terra/forge/TerraForgePlugin.java +++ b/platforms/forge/src/main/java/com/dfsek/terra/forge/TerraForgePlugin.java @@ -26,6 +26,7 @@ import com.dfsek.terra.api.registry.CheckedRegistry; import com.dfsek.terra.api.registry.LockedRegistry; import com.dfsek.terra.api.transform.NotNullValidator; import com.dfsek.terra.api.transform.Transformer; +import com.dfsek.terra.api.util.JarUtil; import com.dfsek.terra.api.util.logging.DebugLogger; import com.dfsek.terra.api.util.mutable.MutableInteger; import com.dfsek.terra.commands.CommandUtil; @@ -86,10 +87,12 @@ import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.registries.ForgeRegistries; import org.apache.commons.io.FileUtils; import org.apache.logging.log4j.LogManager; +import org.objectweb.asm.Type; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.URISyntaxException; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; @@ -97,7 +100,10 @@ import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.function.Consumer; +import java.util.jar.JarFile; +import java.util.zip.ZipFile; import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument; @@ -219,7 +225,7 @@ public class TerraForgePlugin implements TerraPlugin { generationSettings.surfaceBuilder(SurfaceBuilder.DEFAULT.configured(new SurfaceBuilderConfig(Blocks.GRASS_BLOCK.defaultBlockState(), Blocks.DIRT.defaultBlockState(), Blocks.GRAVEL.defaultBlockState()))); // It needs a surfacebuilder, even though we dont use it. generationSettings.addFeature(GenerationStage.Decoration.VEGETAL_DECORATION, POPULATOR_CONFIGURED_FEATURE); - BiomeAmbience vanillaEffects = vanilla.getSpecialEffects(); + BiomeAmbience vanillaEffects = vanilla.getSpecialEffects(); BiomeAmbience.Builder effects = new BiomeAmbience.Builder() .waterColor(colors.getOrDefault("water", vanillaEffects.getWaterColor())) .waterFogColor(colors.getOrDefault("water-fog", vanillaEffects.getWaterFogColor())) @@ -289,6 +295,22 @@ public class TerraForgePlugin implements TerraPlugin { }); } + @Override + public JarFile getModJar() throws URISyntaxException, IOException { + File modsDir = new File("./mods"); + + if(!modsDir.exists()) return JarUtil.getJarFile(); + + for(File file : Objects.requireNonNull(modsDir.listFiles((dir, name) -> name.endsWith(".jar")))) { + try(ZipFile zipFile = new ZipFile(file)) { + if(zipFile.getEntry(Type.getInternalName(TerraPlugin.class) + ".class") != null) { + return new JarFile(file); + } + } + } + return JarUtil.getJarFile(); + } + public TerraWorld getWorld(long seed) { TerraWorld world = worldMap.get(seed); if(world == null) throw new IllegalArgumentException("No world exists with seed " + seed);