diff --git a/Core/src/main/java/com/plotsquared/core/util/ThreadUtils.java b/Core/src/main/java/com/plotsquared/core/util/ThreadUtils.java new file mode 100644 index 000000000..c7c011e3d --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/util/ThreadUtils.java @@ -0,0 +1,57 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.core.util; + +import com.plotsquared.core.PlotSquared; +import lombok.experimental.UtilityClass; + +@UtilityClass public class ThreadUtils { + + /** + * Throws {@link IllegalStateException} if the method + * is called from the server main thread + * + * @param message Message describing the issue + */ + public void catchSync(final String message) { + if (PlotSquared.get().isMainThread(Thread.currentThread())) { + throw new IllegalStateException(message); + } + } + + /** + * Throws {@link IllegalStateException} if the method + * is not called from the server main thread + * + * @param message Message describing the issue + */ + public void catchAsync(final String message) { + if (!PlotSquared.get().isMainThread(Thread.currentThread())) { + throw new IllegalStateException(message); + } + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java b/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java index a59c1d264..7add9ef8a 100644 --- a/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java +++ b/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java @@ -27,6 +27,7 @@ package com.plotsquared.core.uuid; import com.google.common.collect.Lists; import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.util.ThreadUtils; import com.plotsquared.core.util.task.TaskManager; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -144,6 +145,7 @@ public class UUIDPipeline { * @return The mapped uuid. Will return null if the request timed out. */ @Nullable public UUID getSingle(@NotNull final String username, final long timeout) { + ThreadUtils.catchSync("Blocking UUID retrieval from the main thread"); try { this.getUUIDs(Collections.singletonList(username)).get(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException e) { @@ -162,6 +164,7 @@ public class UUIDPipeline { * @return The mapped username. Will return null if the request timeout. */ @Nullable public String getSingle(@NotNull final UUID uuid, final long timeout) { + ThreadUtils.catchSync("Blocking username retrieval from the main thread"); try { this.getNames(Collections.singletonList(uuid)).get(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException e) {