summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/singleton/MinecraftServerInstance.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/singleton/MinecraftServerInstance.java')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/singleton/MinecraftServerInstance.java25
1 files changed, 25 insertions, 0 deletions
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<MinecraftServer> 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");
+ }
+ }
+}