55import io .papermc .paper .plugin .bootstrap .PluginBootstrap ;
66import io .papermc .paper .plugin .bootstrap .PluginProviderContext ;
77import org .bukkit .plugin .java .JavaPlugin ;
8+ import org .jspecify .annotations .Nullable ;
89
910import java .util .ArrayList ;
1011import java .util .List ;
12+ import java .util .Locale ;
13+ import java .util .Optional ;
14+ import java .util .regex .Pattern ;
1115
1216public final class ServiceBootstrapper implements PluginBootstrap {
1317 public static final ErrorTracker ERROR_TRACKER = ErrorTracker .contextAware ();
1418 public static final String ISSUES = "https://github.com/TheNextLvl-net/service-io/issues/new?template=bug_report.yml" ;
15- public static final boolean COMPATIBILITY_MODE = Boolean .parseBoolean (System .getenv ("COMPATIBILITY_MODE" ));
19+ public static final @ Nullable String COMPATIBILITY_MODE = System .getenv ("COMPATIBILITY_MODE" );
20+ private static final Pattern SEMVER_PATTERN = Pattern .compile ("^\\ d+\\ .\\ d+\\ .\\ d+$" );
1621
1722 @ Override
1823 public void bootstrap (final BootstrapContext context ) {
@@ -21,29 +26,53 @@ public void bootstrap(final BootstrapContext context) {
2126 @ Override
2227 public JavaPlugin createPlugin (final PluginProviderContext context ) {
2328 final var plugin = PluginBootstrap .super .createPlugin (context );
24- if ( COMPATIBILITY_MODE ) enableCompatibilityMode (context );
29+ resolveCompatibilityVersion ( context ). ifPresent ( version -> enableCompatibilityMode (context , version ) );
2530 return plugin ;
2631 }
2732
28- private void enableCompatibilityMode (final PluginProviderContext context ) {
33+ private Optional <String > resolveCompatibilityVersion (final PluginProviderContext context ) {
34+ if (COMPATIBILITY_MODE == null ) return Optional .empty ();
35+
36+ final var value = COMPATIBILITY_MODE .trim ().toLowerCase (Locale .ROOT );
37+ switch (value ) {
38+ case "" , "false" -> {
39+ return Optional .empty ();
40+ }
41+ case "1" -> {
42+ return Optional .of ("1.7.3" );
43+ }
44+ case "2" , "true" -> {
45+ return Optional .of ("2.19.0" );
46+ }
47+ }
48+ if (SEMVER_PATTERN .matcher (value ).matches ()) return Optional .of (value );
49+
50+ context .getLogger ().warn ("Ignoring COMPATIBILITY_MODE value '{}': expected false, 1, 2, true, or a version like 2.19.0" , value );
51+ return Optional .empty ();
52+ }
53+
54+ private void enableCompatibilityMode (final PluginProviderContext context , final String version ) {
2955 final var logger = context .getLogger ();
3056 try {
3157 final var meta = context .getConfiguration ();
3258
3359 final var metaClass = meta .getClass ();
3460 final var name = metaClass .getDeclaredField ("name" );
3561 final var provides = metaClass .getDeclaredField ("provides" );
62+ final var pluginVersion = metaClass .getDeclaredField ("version" );
3663
3764 name .trySetAccessible ();
3865 provides .trySetAccessible ();
66+ pluginVersion .trySetAccessible ();
3967
4068 final var providedPlugins = new ArrayList <>(meta .getProvidedPlugins ());
4169 providedPlugins .remove ("Vault" );
4270 providedPlugins .add (meta .getName ());
4371
4472 name .set (meta , "Vault" );
4573 provides .set (meta , List .copyOf (providedPlugins ));
46- logger .info ("Enabled compatibility mode, only use this if you really need to." );
74+ pluginVersion .set (meta , version );
75+ logger .info ("Enabled compatibility mode with version {}, only use this if you really need to." , version );
4776 } catch (final NoSuchFieldException | IllegalAccessException e ) {
4877 logger .warn ("Failed to initialize compatibility mode" , e );
4978 logger .warn ("Please look for similar issues or report this on GitHub: {}" , ISSUES );
0 commit comments