stonecutter: improve backwards-compatibility

Builds targetting Minecraft versions below 1.21.6 will now once again contain the `getScreen` method with `Screen` as their return value instead of `MidnightConfigScreen`.
This ensures that mods compiled for older MidnightLib versions continue working as expected.

The return value has to be changed to `MidnightConfigScreen` in 1.21.6 and up due to NeoForge removing support for the `@OnlyIn` annotation – thus breaking server-only installations when referencing client-only classes like `Screen`.
This commit is contained in:
Martin Prokoph
2025-11-22 17:49:48 +01:00
parent 071f79b763
commit 4076ee2b6f
4 changed files with 22 additions and 7 deletions

View File

@@ -7,7 +7,7 @@ org.gradle.parallel=false
#org.gradle.configureondemand=true
# Mod properties
mod.version=1.9.0-beta.1
mod.version=1.9.0-beta.2
mod.group=eu.midnightdust
mod.id=midnightlib
mod.name=MidnightLib

View File

@@ -64,8 +64,8 @@ public class ButtonEntry extends ContainerObjectSelectionList.Entry<ButtonEntry>
Optional.ofNullable(this.buttons.get(0)).ifPresent(widget -> {
int idMode = this.info.entry.idMode();
if (idMode != -1) context.renderItem(idMode == 0 ?
BuiltInRegistries.ITEM./*? if >= 1.21.4 {*/ getValue /*?} else {*/ /*get*/ /*?}*/(ResourceLocation.tryParse(this.info.tempValue)).getDefaultInstance()
: BuiltInRegistries.BLOCK./*? if >= 1.21.4 {*/ getValue /*?} else {*/ /*get*/ /*?}*/(ResourceLocation.tryParse(this.info.tempValue)).asItem().getDefaultInstance(),
BuiltInRegistries.ITEM./*? if >= 1.21.4 {*/ getValue /*?} else {*/ /*get *//*?}*/(ResourceLocation.tryParse(this.info.tempValue)).getDefaultInstance()
: BuiltInRegistries.BLOCK./*? if >= 1.21.4 {*/ getValue /*?} else {*/ /*get *//*?}*/(ResourceLocation.tryParse(this.info.tempValue)).asItem().getDefaultInstance(),
widget.getX() + widget.getWidth() - 18, y + 2);
});
}

View File

@@ -29,9 +29,17 @@ import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
//? if < 1.21.6 {
/*//? fabric
import net.fabricmc.api.*;
//? neoforge
/^import net.neoforged.api.distmarker.*;^/
//? forge
/^import net.minecraftforge.api.distmarker.*;^/
*///?}
/** MidnightConfig is an incredibly lightweight, but still fully-featured config library for Minecraft mods.<br>
* Originally based on <a href="https://github.com/Minenash/TinyConfig">TinyConfig</a> by Minenash.*/
@SuppressWarnings("unchecked")
public abstract class MidnightConfig {
private static final Pattern INTEGER_ONLY = Pattern.compile("(-?[0-9]*)");
private static final Pattern DECIMAL_ONLY = Pattern.compile("-?(\\d+\\.?\\d*|\\d*\\.?\\d+|\\.)");
@@ -67,6 +75,7 @@ public abstract class MidnightConfig {
* */
protected static <T extends MidnightConfig> T createInstance(String modid, Class<? extends MidnightConfig> configClass) {
try {
//noinspection unchecked
T instance = (T) configClass.getDeclaredConstructor().newInstance();
instance.modid = modid;
instance.configClass = configClass;
@@ -149,7 +158,7 @@ public abstract class MidnightConfig {
info.function = (BiFunction<EditBox, Button, Predicate<String>>) (t, b) -> s -> {
s = s.trim();
if (!(s.isEmpty() || !isNumber || pattern.matcher(s).matches()) ||
(info.dataType == ResourceLocation.class && ResourceLocation.read(s)./*? if >= 1.21 {*/isError() /*?} else {*/ /*error().isPresent()*/ /*?}*/)) return false;
(info.dataType == ResourceLocation.class && ResourceLocation.read(s)./*? if >= 1.21 {*/isError() /*?} else {*/ /*error().isPresent() *//*?}*/)) return false;
Number value = 0; boolean inLimits = false; info.error = null;
if (!(isNumber && s.isEmpty()) && !s.equals("-") && !s.equals(".")) {
@@ -280,10 +289,16 @@ public abstract class MidnightConfig {
* @param parent The parent screen, which will be returned to when exiting the config
* @param modid The mod of which to load the config screen
* */
//? if < 1.21.6 {
/*/^? fabric {^/ @Environment(EnvType.CLIENT) /^?} else {^/ /^@OnlyIn(Dist.CLIENT) ^//^?}^/
public static Screen getScreen(Screen parent, String modid) {
*///?} else {
public static MidnightConfigScreen getScreen(Screen parent, String modid) {
//?}
return configInstances.get(modid).getScreen(parent);
}
/**
* Creates an instance of the config screen.
* This can be overridden to return a fully custom config screen.

View File

@@ -18,11 +18,11 @@ public class MidnightConfigListWidget extends ContainerObjectSelectionList<Butto
public boolean renderHeaderSeparator = true;
public MidnightConfigListWidget(Minecraft client, int width, int height, int y, int itemHeight) {
super(client, width, height, y, /*? if < 1.21 {*/ /*height + y,*/ /*?}*/ itemHeight);
super(client, width, height, y, /*? if < 1.21 {*/ /*height + y, *//*?}*/ itemHeight);
}
@Override
public int /*? if >= 1.21.4 {*/ scrollBarX() /*?} else {*/ /*getScrollbarPosition()*/ /*?}*/ {
public int /*? if >= 1.21.4 {*/ scrollBarX() /*?} else {*/ /*getScrollbarPosition() *//*?}*/ {
return this.width - 7;
}