Something went wrong. Try again.
Library for my mods and adding supporter perks
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869/* * Copyright 2026 macuguita * * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * * https://joinup.ec.europa.eu/software/page/eupl * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. */package com.macuguita.lib.impl.persista;
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.time.Duration;
import dev.yumi.mc.core.api.ModContainer;import dev.yumi.mc.core.api.entrypoint.ModInitializer;import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import com.macuguita.lib.api.network.PacketRegistry;import com.macuguita.lib.impl.MacuLib;import com.macuguita.lib.impl.persista.net.ClientboundDataUpdatedPacket;import com.macuguita.lib.impl.persista.net.ServerboundDataUpdatedPacket;
public class Persista implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("(" + MacuLib.MOD_ID + ") persista-client"); public static final Duration REQUEST_TIMEOUT = Duration.ofMillis(MacuLib.CONFIG.persista.requestTimeout); private static final Duration INTERNET_CHECK_TIMEOUT = Duration.ofSeconds(5);
public static volatile boolean HAS_INTERNET = true;
@Override public void onInitialize(ModContainer mod) { PacketRegistry.registerServerboundPlayPacket(ServerboundDataUpdatedPacket.TYPE, ServerboundDataUpdatedPacket.CODEC, ServerboundDataUpdatedPacket::handle); PacketRegistry.registerClientboundPlayPacket(ClientboundDataUpdatedPacket.TYPE, ClientboundDataUpdatedPacket.CODEC);
// Probe connectivity off-thread so a dead network cannot stall startup PersistaExecutor.io().execute(Persista::checkInternet); }
private static void checkInternet() { try (var client = HttpClient.newBuilder().connectTimeout(INTERNET_CHECK_TIMEOUT).build()) { var request = HttpRequest.newBuilder(URI.create("https://sessionserver.mojang.com")) .timeout(INTERNET_CHECK_TIMEOUT) .build(); client.send(request, HttpResponse.BodyHandlers.discarding()); HAS_INTERNET = true; } catch (InterruptedException e) { Thread.currentThread().interrupt(); HAS_INTERNET = false; } catch (IOException e) { LOGGER.warn("No internet access, remote fetching disabled"); HAS_INTERNET = false; } }}