From cb5cca2c49f583b1dd4b9e543bef4de0d4f0116e Mon Sep 17 00:00:00 2001 From: IThundxr Date: Mon, 4 Nov 2024 11:48:33 -0500 Subject: [PATCH] Remove update check & dont let create version be inlined --- .../createnumismatics/Numismatics.java | 4 +- .../util/MethodVarHandleUtils.java | 40 ++++++++++++ .../util/NumismaticsUpdateCheck.java | 62 ------------------- 3 files changed, 42 insertions(+), 64 deletions(-) create mode 100644 common/src/main/java/dev/ithundxr/createnumismatics/util/MethodVarHandleUtils.java delete mode 100644 common/src/main/java/dev/ithundxr/createnumismatics/util/NumismaticsUpdateCheck.java diff --git a/common/src/main/java/dev/ithundxr/createnumismatics/Numismatics.java b/common/src/main/java/dev/ithundxr/createnumismatics/Numismatics.java index 15134a5..3dd9b42 100644 --- a/common/src/main/java/dev/ithundxr/createnumismatics/Numismatics.java +++ b/common/src/main/java/dev/ithundxr/createnumismatics/Numismatics.java @@ -39,7 +39,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.NumismaticsUpdateCheck; +import dev.ithundxr.createnumismatics.util.MethodVarHandleUtils; import dev.ithundxr.createnumismatics.util.Utils; import net.minecraft.SharedConstants; import net.minecraft.commands.CommandSourceStack; @@ -65,8 +65,8 @@ public class Numismatics { } public static void init() { + 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, Create.VERSION, Loader.getFormatted()); - NumismaticsUpdateCheck.execute(); ModSetup.register(); finalizeRegistrate(); diff --git a/common/src/main/java/dev/ithundxr/createnumismatics/util/MethodVarHandleUtils.java b/common/src/main/java/dev/ithundxr/createnumismatics/util/MethodVarHandleUtils.java new file mode 100644 index 0000000..14c938b --- /dev/null +++ b/common/src/main/java/dev/ithundxr/createnumismatics/util/MethodVarHandleUtils.java @@ -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 . + */ + +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 getStaticField(Class clazz, String fieldName, Class type) throws NoSuchFieldException, IllegalAccessException { + return (T) lookup.findStaticVarHandle(clazz, fieldName, type).get(); + } + + public static T getStaticField(Class clazz, String fieldName, Class type, T defaultValue) { + T returnValue = defaultValue; + + try { + returnValue = getStaticField(clazz, fieldName, type); + } catch (NoSuchFieldException | IllegalAccessException ignored) {} + + return returnValue; + } +} \ No newline at end of file diff --git a/common/src/main/java/dev/ithundxr/createnumismatics/util/NumismaticsUpdateCheck.java b/common/src/main/java/dev/ithundxr/createnumismatics/util/NumismaticsUpdateCheck.java deleted file mode 100644 index 75bd821..0000000 --- a/common/src/main/java/dev/ithundxr/createnumismatics/util/NumismaticsUpdateCheck.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 . - */ - -package dev.ithundxr.createnumismatics.util; - -import dev.ithundxr.createnumismatics.Numismatics; -import dev.ithundxr.createnumismatics.NumismaticsBuildInfo; -import dev.ithundxr.createnumismatics.multiloader.Loader; -import net.minecraft.SharedConstants; -import net.minecraft.Util; - -import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.nio.charset.StandardCharsets; -import java.time.Duration; - -public class NumismaticsUpdateCheck { - public static void execute() { - // TODO switch to nonCriticalIoPool() in 1.21.1 - Util.ioPool().submit(() -> { - String uri = String.format( - "https://update.api.ithundxr.dev/update-check?mod_id=%s&mod_version=%s&mc_version=%s&loader=%s&dev=%s", - Numismatics.MOD_ID, - NumismaticsBuildInfo.VERSION, - SharedConstants.getCurrentVersion().getName(), - Loader.getActual(), - Utils.isDevEnv() - ); - - HttpRequest request = HttpRequest.newBuilder() - .GET() - .uri(URI.create(uri)) - .build(); - - try { - HttpClient.newBuilder() - .connectTimeout(Duration.ofSeconds(10L)) - .followRedirects(HttpClient.Redirect.ALWAYS) - .build() - .send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); - } catch (IOException | InterruptedException ignored) {} - }); - } -}