Skip to content

Commit e1e44fe

Browse files
committed
Reflection, Reflection, Reflection...
1 parent 3a99526 commit e1e44fe

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

common/src/main/java/dev/ithundxr/createnumismatics/Numismatics.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import dev.ithundxr.createnumismatics.registry.NumismaticsCommands;
3939
import dev.ithundxr.createnumismatics.registry.NumismaticsCreativeModeTabs.Tabs;
4040
import dev.ithundxr.createnumismatics.registry.NumismaticsPackets;
41+
import dev.ithundxr.createnumismatics.util.MethodVarHandleUtils;
4142
import dev.ithundxr.createnumismatics.util.NumismaticsUpdateCheck;
4243
import dev.ithundxr.createnumismatics.util.Utils;
4344
import net.minecraft.SharedConstants;
@@ -65,7 +66,8 @@ public class Numismatics {
6566
}
6667

6768
public static void init() {
68-
LOGGER.info("{} v{} initializing! Commit hash: {} Create version: {} on platform: {}", NAME, NumismaticsBuildInfo.VERSION, NumismaticsBuildInfo.GIT_COMMIT, Create.VERSION, Loader.getFormatted());
69+
String createVersion = MethodVarHandleUtils.getStaticField(Create.class, "VERSION", String.class, "UNKNOWN");
70+
LOGGER.info("{} v{} initializing! Commit hash: {} Create version: {} on platform: {}", NAME, NumismaticsBuildInfo.VERSION, NumismaticsBuildInfo.GIT_COMMIT, createVersion, Loader.getFormatted());
6971
NumismaticsUpdateCheck.execute();
7072

7173
ModSetup.register();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Numismatics
3+
* Copyright (c) 2024 The Railways Team
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program 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
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package dev.ithundxr.createnumismatics.util;
20+
21+
import java.lang.invoke.MethodHandles;
22+
23+
@SuppressWarnings("unchecked")
24+
public class MethodVarHandleUtils {
25+
private static final MethodHandles.Lookup lookup = MethodHandles.lookup();
26+
27+
public static <T> T getStaticField(Class<?> clazz, String fieldName, Class<T> type) throws NoSuchFieldException, IllegalAccessException {
28+
return (T) lookup.findStaticVarHandle(clazz, fieldName, type).get();
29+
}
30+
31+
public static <T> T getStaticField(Class<?> clazz, String fieldName, Class<T> type, T defaultValue) {
32+
T returnValue = defaultValue;
33+
34+
try {
35+
returnValue = getStaticField(clazz, fieldName, type);
36+
} catch (NoSuchFieldException | IllegalAccessException ignored) {}
37+
38+
return returnValue;
39+
}
40+
}

0 commit comments

Comments
 (0)