mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-17 11:05:40 +00:00
Turned minecraft into a proper Mod, with a "dummy" language provider.
Also fixed the "translations" not being available early if an exception occurs by force loading the forge and MC ones. Closes #5984 Signed-off-by: cpw <[email protected]>
This commit is contained in:
18 files changed
+257
-134
No files matched your search
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2019.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.fml;
|
||||
|
||||
import net.minecraftforge.fml.loading.DefaultModInfos;
|
||||
|
||||
public class DefaultModContainers
|
||||
{
|
||||
public static final ModContainer MINECRAFT = new ModContainer(DefaultModInfos.minecraftModInfo)
|
||||
{
|
||||
@Override
|
||||
public boolean matches(final Object mod)
|
||||
{
|
||||
return mod == this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getMod()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -41,7 +41,11 @@ public class ModLoadingContext
|
||||
}
|
||||
|
||||
public ModContainer getActiveContainer() {
|
||||
return activeContainer == null ? DefaultModContainers.MINECRAFT : activeContainer;
|
||||
return activeContainer == null ? ModList.get().getModContainerById("minecraft").orElseThrow(()->new RuntimeException("Where is minecraft???!")) : activeContainer;
|
||||
}
|
||||
|
||||
public String getActiveNamespace() {
|
||||
return activeContainer == null ? "minecraft" : activeContainer.getNamespace();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,12 +20,11 @@
|
||||
package net.minecraftforge.fml;
|
||||
|
||||
import com.google.common.collect.Streams;
|
||||
import net.minecraftforge.forgespi.language.IModInfo;
|
||||
import net.minecraftforge.fml.loading.EarlyLoadingException;
|
||||
import net.minecraftforge.forgespi.language.IModInfo;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
@@ -53,7 +52,7 @@ public class ModLoadingException extends RuntimeException
|
||||
private final List<Object> context;
|
||||
|
||||
public ModLoadingException(final IModInfo modInfo, final ModLoadingStage errorStage, final String i18nMessage, final Throwable originalException, Object... context) {
|
||||
super("Mod Loading Exception : " + i18nMessage + " " + Arrays.toString(Streams.concat(Stream.of(modInfo.getModId(), errorStage, originalException), Stream.of(context)).toArray()), originalException);
|
||||
super("Mod Loading Exception", originalException);
|
||||
this.modInfo = modInfo;
|
||||
this.errorStage = errorStage;
|
||||
this.i18nMessage = i18nMessage;
|
||||
@@ -75,4 +74,9 @@ public class ModLoadingException extends RuntimeException
|
||||
public String formatToString() {
|
||||
return ForgeI18n.parseMessage(i18nMessage, Streams.concat(Stream.of(modInfo, errorStage, getCause()), context.stream()).toArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return formatToString();
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,10 @@ import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.resources.DownloadingPackFinder;
|
||||
import net.minecraft.client.resources.ClientResourcePackInfo;
|
||||
import net.minecraft.profiler.IProfiler;
|
||||
import net.minecraft.resources.*;
|
||||
import net.minecraft.resources.IFutureReloadListener;
|
||||
import net.minecraft.resources.IReloadableResourceManager;
|
||||
import net.minecraft.resources.IResourceManager;
|
||||
import net.minecraft.resources.ResourcePackList;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.client.event.ModelRegistryEvent;
|
||||
@@ -31,10 +34,17 @@ import net.minecraftforge.client.model.ModelLoaderRegistry;
|
||||
import net.minecraftforge.common.ForgeConfig;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.eventbus.api.Event;
|
||||
import net.minecraftforge.fml.*;
|
||||
import net.minecraftforge.fml.BrandingControl;
|
||||
import net.minecraftforge.fml.LoadingFailedException;
|
||||
import net.minecraftforge.fml.LogicalSidedProvider;
|
||||
import net.minecraftforge.fml.ModLoader;
|
||||
import net.minecraftforge.fml.ModLoadingWarning;
|
||||
import net.minecraftforge.fml.SidedProvider;
|
||||
import net.minecraftforge.fml.VersionChecker;
|
||||
import net.minecraftforge.fml.client.gui.LoadingErrorScreen;
|
||||
import net.minecraftforge.fml.client.registry.RenderingRegistry;
|
||||
import net.minecraftforge.fml.packs.ResourcePackLoader;
|
||||
import net.minecraftforge.fml.server.LanguageHook;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@@ -64,6 +74,7 @@ public class ClientModLoader
|
||||
ClientModLoader.mc = minecraft;
|
||||
SidedProvider.setClient(()->minecraft);
|
||||
LogicalSidedProvider.setClient(()->minecraft);
|
||||
LanguageHook.loadForgeAndMCLangs();
|
||||
earlyLoaderGUI = new EarlyLoaderGUI(minecraft.mainWindow);
|
||||
createRunnableWithCatch(() -> ModLoader.get().gatherAndInitializeMods(earlyLoaderGUI::renderTick)).run();
|
||||
ResourcePackLoader.loadResourcePacks(defaultResourcePacks);
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package net.minecraftforge.fml.mclanguageprovider;
|
||||
|
||||
import net.minecraftforge.fml.ModContainer;
|
||||
import net.minecraftforge.forgespi.language.ILifecycleEvent;
|
||||
import net.minecraftforge.forgespi.language.IModInfo;
|
||||
import net.minecraftforge.forgespi.language.IModLanguageProvider;
|
||||
import net.minecraftforge.forgespi.language.ModFileScanData;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Collections;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static net.minecraftforge.fml.Logging.LOADING;
|
||||
|
||||
public class MinecraftModLanguageProvider implements IModLanguageProvider {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
@Override
|
||||
public String name() {
|
||||
return "minecraft";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Consumer<ModFileScanData> getFileVisitor() {
|
||||
return (sd)->sd.addLanguageLoader(Collections.singletonMap("minecraft", new MinecraftModTarget()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R extends ILifecycleEvent<R>> void consumeLifecycleEvent(final Supplier<R> consumeEvent) {
|
||||
|
||||
}
|
||||
|
||||
public static class MinecraftModTarget implements IModLanguageLoader {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T loadMod(final IModInfo info, final ClassLoader modClassLoader, final ModFileScanData modFileScanResults) {
|
||||
try {
|
||||
final Class<?> mcModClass = Class.forName("net.minecraftforge.fml.mclanguageprovider.MinecraftModLanguageProvider$MinecraftModContainer", true, modClassLoader);
|
||||
return (T)mcModClass.getConstructor(IModInfo.class).newInstance(info);
|
||||
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) {
|
||||
LOGGER.fatal(LOADING,"Unable to load MinecraftModContainer, wut?", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
public static class MinecraftModContainer extends ModContainer {
|
||||
private static final String MCMODINSTANCE = "minecraft, the mod";
|
||||
|
||||
public MinecraftModContainer(final IModInfo info) {
|
||||
super(info);
|
||||
contextExtension = ()->null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(final Object mod) {
|
||||
return Objects.equals(mod, MCMODINSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getMod() {
|
||||
return MCMODINSTANCE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import static net.minecraftforge.fml.Logging.CORE;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -56,6 +57,7 @@ public class ResourcePackLoader
|
||||
public static <T extends ResourcePackInfo> void loadResourcePacks(ResourcePackList<T> resourcePacks) {
|
||||
resourcePackList = resourcePacks;
|
||||
modResourcePacks = ModList.get().getModFiles().stream().
|
||||
filter(mf->!Objects.equals(mf.getModLoader(),"minecraft")).
|
||||
map(mf -> new ModFileResourcePack(mf.getFile())).
|
||||
collect(Collectors.toMap(ModFileResourcePack::getModFile, Function.identity()));
|
||||
resourcePacks.addPackFinder(new ModPackFinder());
|
||||
@@ -69,6 +71,7 @@ public class ResourcePackLoader
|
||||
for (Entry<ModFile, ModFileResourcePack> e : modResourcePacks.entrySet())
|
||||
{
|
||||
IModInfo mod = e.getKey().getModInfos().get(0);
|
||||
if (Objects.equals(mod.getModId(), "minecraft")) continue; // skip the minecraft "mod"
|
||||
final String name = "mod:" + mod.getModId();
|
||||
final T packInfo = ResourcePackInfo.createResourcePack(name, true, e::getValue, factory, ResourcePackInfo.Priority.BOTTOM);
|
||||
if (packInfo == null) {
|
||||
|
||||
@@ -92,7 +92,7 @@ public class LanguageHook
|
||||
|
||||
}
|
||||
|
||||
static void loadForgeAndMCLangs() {
|
||||
public static void loadForgeAndMCLangs() {
|
||||
modTable = new HashMap<>(5000);
|
||||
final InputStream mc = Thread.currentThread().getContextClassLoader().getResourceAsStream("assets/minecraft/lang/en_us.json");
|
||||
final InputStream forge = Thread.currentThread().getContextClassLoader().getResourceAsStream("assets/forge/lang/en_us.json");
|
||||
|
||||
@@ -304,7 +304,7 @@ public class ForgeRegistry<V extends IForgeRegistryEntry<V>> implements IForgeRe
|
||||
|
||||
int add(int id, V value)
|
||||
{
|
||||
final String owner = ModLoadingContext.get().getActiveContainer().getNamespace();
|
||||
final String owner = ModLoadingContext.get().getActiveNamespace();
|
||||
return add(id, value, owner);
|
||||
}
|
||||
|
||||
|
||||
@@ -936,7 +936,7 @@ public class GameData
|
||||
int index = name.lastIndexOf(':');
|
||||
String oldPrefix = index == -1 ? "" : name.substring(0, index).toLowerCase(Locale.ROOT);
|
||||
name = index == -1 ? name : name.substring(index + 1);
|
||||
String prefix = ModLoadingContext.get().getActiveContainer().getNamespace();
|
||||
String prefix = ModLoadingContext.get().getActiveNamespace();
|
||||
if (warnOverrides && !oldPrefix.equals(prefix) && oldPrefix.length() > 0)
|
||||
{
|
||||
LogManager.getLogger().info("Potentially Dangerous alternative prefix `{}` for name `{}`, expected `{}`. This could be a intended override, but in most cases indicates a broken mod.", oldPrefix, name, prefix);
|
||||
|
||||
@@ -105,7 +105,7 @@ public class ObjectHolderRegistry
|
||||
|
||||
// double pass - get all the class level annotations first, then the field level annotations
|
||||
annotations.stream().filter(a -> OBJECT_HOLDER.equals(a.getAnnotationType())).filter(a -> a.getTargetType() == ElementType.TYPE)
|
||||
.forEach(data -> scanTarget(classModIds, classCache, data.getClassType(), null, (String)data.getAnnotationData().get("value"), true, data.getClassType().getClassName().startsWith("net.minecraft.")));
|
||||
.forEach(data -> scanTarget(classModIds, classCache, data.getClassType(), null, (String)data.getAnnotationData().get("value"), true, data.getClassType().getClassName().startsWith("net.minecraft.")));
|
||||
|
||||
annotations.stream().filter(a -> OBJECT_HOLDER.equals(a.getAnnotationType())).filter(a -> a.getTargetType() == ElementType.FIELD)
|
||||
.forEach(data -> scanTarget(classModIds, classCache, data.getClassType(), data.getMemberName(), (String)data.getAnnotationData().get("value"), false, false));
|
||||
|
||||
Reference in new issue
Block a user