Backport even more future ResourceLocation methods (#10427)

This commit is contained in:
Jonathing authored and GitHub committed 2025-02-18 12:02:06 -08:00
1 parent ebfd92e0e1
commit bd96ff8777
2 files changed
+94 -8

No files matched your search

@@ -1,21 +1,29 @@
--- a/net/minecraft/resources/ResourceLocation.java
+++ b/net/minecraft/resources/ResourceLocation.java
@@ -34,6 +_,7 @@
@@ -34,6 +_,8 @@
this.path = p_249394_;
}
+ /** Forge: Consider using {@link #fromNamespaceAndPath(String, String)} instead, as Mojang made this constructor private in Vanilla 1.21 */
+ /** @deprecated Forge: Consider using {@link #fromNamespaceAndPath(String, String)} instead, as Mojang made this constructor private in 1.21 */
+ @Deprecated(forRemoval = true, since = "1.20.6")
public ResourceLocation(String p_135811_, String p_135812_) {
this(assertValidNamespace(p_135811_, p_135812_), assertValidPath(p_135811_, p_135812_), (ResourceLocation.Dummy)null);
}
@@ -42,6 +_,7 @@
@@ -42,10 +_,14 @@
this(p_135814_[0], p_135814_[1]);
}
+ /** Forge: Consider using {@link #parse(String)} instead, as Mojang made this constructor private in Vanilla 1.21 */
+ /** @deprecated Forge: Consider using {@link #parse(String)} instead, as Mojang made this constructor private in 1.21 */
+ @Deprecated(forRemoval = true, since = "1.20.6")
public ResourceLocation(String p_135809_) {
this(decompose(p_135809_, ':'));
}
+ /** @deprecated Forge: Consider using {@link #bySeparator(String, char)} instead, as Mojang removed this method in 1.21 */
+ @Deprecated(forRemoval = true, since = "1.20.6")
public static ResourceLocation of(String p_135823_, char p_135824_) {
return new ResourceLocation(decompose(p_135823_, p_135824_));
}
@@ -143,6 +_,12 @@
return i;
}
@@ -29,19 +37,55 @@
public String toDebugFileName() {
return this.toString().replace('/', '_').replace(':', '_');
}
@@ -244,5 +_,15 @@
@@ -243,6 +_,51 @@
public JsonElement serialize(ResourceLocation p_135855_, Type p_135856_, JsonSerializationContext p_135857_) {
return new JsonPrimitive(p_135855_.toString());
}
+ }
+ }
+
+ /** Forge: Backport of Vanilla 1.21 method */
+ /** Forge: This is a backported method from 1.21, and is the replacement for {@link #ResourceLocation(String, String)}. */
+ public static ResourceLocation fromNamespaceAndPath(String namespace, String path) {
+ return new ResourceLocation(namespace, path);
+ }
+
+ /** Forge: Backport of Vanilla 1.21 method */
+ /** Forge: This is a backported method from 1.21, and is the replacement for {@link #ResourceLocation(String)}. */
+ public static ResourceLocation parse(String location) {
+ return new ResourceLocation(location);
+ }
+
+ /**
+ * Forge: This is a backported method from 1.21, and acts as an alternative to using {@link #parse(String)}. If you
+ * know for sure you are going to be using the {@linkplain #DEFAULT_NAMESPACE default namespace}
+ * ({@code "minecraft"}), use this.
+ */
+ public static ResourceLocation withDefaultNamespace(String path) {
+ return new ResourceLocation(DEFAULT_NAMESPACE, assertValidPath(DEFAULT_NAMESPACE, path), null);
+ }
+
+ /** Forge: This is a backported method from 1.21, and is the replacement for {@link #of(String, char)}. */
+ public static ResourceLocation bySeparator(String location, char separator) {
+ return of(location, separator);
+ }
+
+ /**
+ * Forge: This is a backported method from 1.21, and is the same as {@link #bySeparator(String, char)} but returns
+ * {@code null} if a resource location cannot be created.
+ */
+ public static @Nullable ResourceLocation tryBySeparator(String location, char separator) {
+ int i = location.indexOf(separator);
+ if (i >= 0) {
+ String s = location.substring(i + 1);
+ if (!isValidPath(s)) {
+ return null;
+ } else if (i != 0) {
+ String s1 = location.substring(0, i);
+ return isValidNamespace(s1) ? new ResourceLocation(s1, s, null) : null;
+ } else {
+ return new ResourceLocation(DEFAULT_NAMESPACE, s, null);
+ }
+ } else {
+ return isValidPath(location) ? new ResourceLocation(DEFAULT_NAMESPACE, location, null) : null;
}
}
}
@@ -0,0 +1,42 @@
package net.minecraftforge.debug.resource;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.gametest.framework.GameTestAssertException;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.gametest.GameTestHolder;
import java.util.Objects;
@Mod(ResourceLocationTest.MODID)
@GameTestHolder("forge." + ResourceLocationTest.MODID)
public final class ResourceLocationTest {
static final String MODID = "resource_location";
@SuppressWarnings("removal") // intentionally using old constructors and methods for testing purposes
@GameTest(template = "forge:empty3x3x3")
public static void backported_methods(GameTestHelper helper) {
var mcLoc = new ResourceLocation("test");
var forgeLoc = new ResourceLocation("forge", "test");
assertValueEqual(forgeLoc, ResourceLocation.fromNamespaceAndPath("forge", "test"), "resource location from namespace and path (forge)");
assertValueEqual(mcLoc, ResourceLocation.withDefaultNamespace("test"), "resource location with default namespace");
assertValueEqual(mcLoc, ResourceLocation.parse("test"), "resource location parse");
assertValueEqual(mcLoc, ResourceLocation.parse("minecraft:test"), "resource location parse (with minecraft namespace)");
assertValueEqual(forgeLoc, ResourceLocation.parse("forge:test"), "resource location parse (with forge namespace)");
assertValueEqual(mcLoc, ResourceLocation.bySeparator("minecraft/test", '/'), "resource location by separator");
assertValueEqual(ResourceLocation.of("forge/of_test", '/'), ResourceLocation.bySeparator("forge/of_test", '/'), "resource location by separator (compared to of)");
assertValueEqual(mcLoc, ResourceLocation.tryBySeparator("minecraft/test", '/'), "resource location by separator");
assertValueEqual(null, ResourceLocation.tryBySeparator("minecraft:?/awd&", '/'), "resource location by separator (invalid)");
helper.succeed();
}
// I don't want to backport IForgeGameTestHelper stuff right now, so this will do - Jonathing
private static void assertValueEqual(ResourceLocation expected, ResourceLocation actual, String name) {
if (!Objects.equals(expected, actual)) {
throw new GameTestAssertException("Expected " + name + " to be " + expected + ", but got " + actual);
}
}
}