Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit de525e1

Browse files
Add ExtensionPoint to prevent one-sided mods from crashing (#109)
* Add ExtensionPoint to prevent one-sided mods from crashing * Update patchwork-fml/src/main/java/net/minecraftforge/fml/ExtensionPoint.java Co-authored-by: Glitch <[email protected]> Co-authored-by: Glitch <[email protected]>
1 parent ab71e1b commit de525e1

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.fml;
21+
22+
import java.util.function.BiFunction;
23+
import java.util.function.BiPredicate;
24+
import java.util.function.Supplier;
25+
26+
import org.apache.commons.lang3.tuple.Pair;
27+
28+
import net.minecraft.client.MinecraftClient;
29+
import net.minecraft.client.gui.screen.Screen;
30+
31+
public class ExtensionPoint<T> {
32+
public static final ExtensionPoint<BiFunction<MinecraftClient, Screen, Screen>> CONFIGGUIFACTORY = new ExtensionPoint<>();
33+
// TODO: Not used by any Forge code, ModFileResourcePack is not implemented in Patchwork API
34+
// public static final ExtensionPoint<BiFunction<MinecraftClient, ModFileResourcePack, ResourcePack>> RESOURCEPACK = new ExtensionPoint<>();
35+
/**
36+
* Compatibility display test for the mod. Used for displaying compatibility
37+
* with remote servers with the same mod, and on disk saves.
38+
*
39+
* <p>The supplier provides the "local" version for sending across the network or
40+
* writing to disk. The predicate tests the version from a remote instance or
41+
* save for acceptability (Boolean is true for network, false for local save)
42+
*
43+
* <p>TODO: Fabric servers do not check for client's mod list,
44+
* there is no way to implement the DISPLAYTEST function in Patchwork.
45+
* A Server-side Forge mod will not know if a client does not have it.
46+
* Currently, this method is here purely for avoiding {@link java.lang.ClassNotFoundException}
47+
*/
48+
public static final ExtensionPoint<Pair<Supplier<String>, BiPredicate<String, Boolean>>> DISPLAYTEST = new ExtensionPoint<>();
49+
50+
// Forge's unused private field
51+
// private Class<T> type;
52+
53+
private ExtensionPoint() {
54+
}
55+
}

patchwork-fml/src/main/java/net/minecraftforge/fml/ModContainer.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,22 @@
2020
package net.minecraftforge.fml;
2121

2222
import java.util.EnumMap;
23+
import java.util.IdentityHashMap;
24+
import java.util.Map;
25+
import java.util.Optional;
26+
import java.util.function.Supplier;
2327

28+
import org.apache.logging.log4j.LogManager;
29+
import org.apache.logging.log4j.Logger;
2430
import net.minecraftforge.eventbus.api.Event;
2531
import net.minecraftforge.fml.config.ModConfig;
2632

2733
// TODO: Stub
2834
public abstract class ModContainer {
35+
protected static final Logger LOGGER = LogManager.getLogger(ModContainer.class);
2936
protected final String modId;
3037
protected final String namespace;
38+
protected final Map<ExtensionPoint, Supplier<?>> extensionPoints = new IdentityHashMap<>();
3139
protected final EnumMap<ModConfig.Type, ModConfig> configs;
3240
private net.fabricmc.loader.api.ModContainer fabricModContainer;
3341

@@ -46,6 +54,21 @@ public final String getNamespace() {
4654
return namespace;
4755
}
4856

57+
@SuppressWarnings("unchecked")
58+
public <T> Optional<T> getCustomExtension(ExtensionPoint<T> point) {
59+
return Optional.ofNullable((T) extensionPoints.getOrDefault(point, () -> null).get());
60+
}
61+
62+
public <T> void registerExtensionPoint(ExtensionPoint<T> point, Supplier<T> extension) {
63+
extensionPoints.put(point, extension);
64+
65+
if (point == ExtensionPoint.DISPLAYTEST) {
66+
LOGGER.warn(
67+
"ExtensionPoint.DISPLAYTEST is not handled by Patchwork due to the limitation of Fabric, "
68+
+ "we cannot prevent loading mods at wrong side or incompatible versions!");
69+
}
70+
}
71+
4972
public void addConfig(final ModConfig modConfig) {
5073
configs.put(modConfig.getType(), modConfig);
5174
}

patchwork-fml/src/main/java/net/minecraftforge/fml/ModLoadingContext.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package net.minecraftforge.fml;
2121

22+
import java.util.function.Supplier;
23+
2224
import net.minecraftforge.common.ForgeConfigSpec;
2325
import net.minecraftforge.fml.config.ModConfig;
2426

@@ -52,6 +54,17 @@ public String getActiveNamespace() {
5254
}
5355
}
5456

57+
/**
58+
* Register an {@link ExtensionPoint} with the mod container.
59+
*
60+
* @param point The extension point to register
61+
* @param extension An extension operator
62+
* @param <T> The type signature of the extension operator
63+
*/
64+
public <T> void registerExtensionPoint(ExtensionPoint<T> point, Supplier<T> extension) {
65+
getActiveContainer().registerExtensionPoint(point, extension);
66+
}
67+
5568
public void registerConfig(ModConfig.Type type, ForgeConfigSpec spec) {
5669
getActiveContainer().addConfig(new ModConfig(type, spec, getActiveContainer()));
5770
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.fml.network;
21+
22+
/**
23+
* Constants related to networking.
24+
*/
25+
public class FMLNetworkConstants {
26+
public static final String FMLNETMARKER = "FML";
27+
public static final int FMLNETVERSION = 2;
28+
public static final String NETVERSION = FMLNETMARKER + FMLNETVERSION;
29+
public static final String NOVERSION = "NONE";
30+
31+
/**
32+
* Return this value in your
33+
* {@link net.minecraftforge.fml.ExtensionPoint#DISPLAYTEST} function to be
34+
* ignored.
35+
*/
36+
public static final String IGNORESERVERONLY = "OHNOES\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31";
37+
}

0 commit comments

Comments
 (0)