From 3e8fae576d7afccb29f5a35d07df970bafdecd36 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 17 Sep 2022 15:59:40 +0800 Subject: stashed updates --- .../singleton/MinecraftServerInstance.java | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/com/keuin/kbackupfabric/singleton/MinecraftServerInstance.java (limited to 'src/main/java/com/keuin/kbackupfabric/singleton/MinecraftServerInstance.java') diff --git a/src/main/java/com/keuin/kbackupfabric/singleton/MinecraftServerInstance.java b/src/main/java/com/keuin/kbackupfabric/singleton/MinecraftServerInstance.java new file mode 100644 index 0000000..dc1ff52 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/singleton/MinecraftServerInstance.java @@ -0,0 +1,25 @@ +package com.keuin.kbackupfabric.singleton; + +import net.minecraft.server.MinecraftServer; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; + +/** + * Holds {@link MinecraftServer} instance. + */ +public class MinecraftServerInstance { + private static AtomicReference instance = new AtomicReference<>(); + + public static @NotNull MinecraftServer getInstance() { + return Objects.requireNonNull(instance.get()); + } + + public static void setInstance(@NotNull MinecraftServer instance) { + Objects.requireNonNull(instance); + if (!MinecraftServerInstance.instance.compareAndSet(null, instance)) { + throw new IllegalStateException("instance already exists"); + } + } +} -- cgit v1.2.3