Skip to content

Commit

Permalink
Reflection, Reflection, Reflection...
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Oct 15, 2024
1 parent 3a99526 commit e1e44fe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import dev.ithundxr.createnumismatics.registry.NumismaticsCommands;
import dev.ithundxr.createnumismatics.registry.NumismaticsCreativeModeTabs.Tabs;
import dev.ithundxr.createnumismatics.registry.NumismaticsPackets;
import dev.ithundxr.createnumismatics.util.MethodVarHandleUtils;
import dev.ithundxr.createnumismatics.util.NumismaticsUpdateCheck;
import dev.ithundxr.createnumismatics.util.Utils;
import net.minecraft.SharedConstants;
Expand Down Expand Up @@ -65,7 +66,8 @@ public class Numismatics {
}

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

ModSetup.register();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.util;

import java.lang.invoke.MethodHandles;

@SuppressWarnings("unchecked")
public class MethodVarHandleUtils {
private static final MethodHandles.Lookup lookup = MethodHandles.lookup();

public static <T> T getStaticField(Class<?> clazz, String fieldName, Class<T> type) throws NoSuchFieldException, IllegalAccessException {
return (T) lookup.findStaticVarHandle(clazz, fieldName, type).get();
}

public static <T> T getStaticField(Class<?> clazz, String fieldName, Class<T> type, T defaultValue) {
T returnValue = defaultValue;

try {
returnValue = getStaticField(clazz, fieldName, type);
} catch (NoSuchFieldException | IllegalAccessException ignored) {}

return returnValue;
}
}

0 comments on commit e1e44fe

Please sign in to comment.