From 69c8401d85b66c544b376ea714befc882f1397cd Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 23 May 2021 11:52:46 +0100 Subject: Fix bug where platform would enable twice on Fabric/Forge (#116) --- .../src/main/java/me/lucko/spark/common/SparkPlatform.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'spark-common/src/main/java/me') diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java index 099ad2d..a4ae848 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java @@ -54,6 +54,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; @@ -89,8 +90,7 @@ public class SparkPlatform { private final TickStatistics tickStatistics; private Map startupGcStatistics = ImmutableMap.of(); private long serverNormalOperationStartTime; - - private SparkApi api; + private final AtomicBoolean enabled = new AtomicBoolean(false); public SparkPlatform(SparkPlugin plugin) { this.plugin = plugin; @@ -119,6 +119,10 @@ public class SparkPlatform { } public void enable() { + if (this.enabled.compareAndSet(false, true)) { + throw new RuntimeException("Platform has already been enabled!"); + } + if (this.tickHook != null) { this.tickHook.addCallback(this.tickStatistics); this.tickHook.start(); @@ -135,9 +139,9 @@ public class SparkPlatform { this.serverNormalOperationStartTime = System.currentTimeMillis(); }); - this.api = new SparkApi(this); - this.plugin.registerApi(this.api); - SparkApi.register(this.api); + SparkApi api = new SparkApi(this); + this.plugin.registerApi(api); + SparkApi.register(api); } public void disable() { -- cgit