From 264185a323d40c8ee5944977486f1bace20e1e33 Mon Sep 17 00:00:00 2001 From: Alexander Brandes Date: Sat, 20 May 2023 20:52:03 +0200 Subject: [PATCH] Add method to check initialize state in PlatformManager, deny access to registries when uninitialized --- .../extension/platform/PlatformManager.java | 14 ++++++++++++++ .../worldedit/registry/NamespacedRegistry.java | 10 +++++++++- .../com/sk89q/worldedit/registry/Registry.java | 13 +++++++++++++ .../com/sk89q/worldedit/world/biome/BiomeType.java | 2 +- .../sk89q/worldedit/world/block/BlockCategory.java | 2 +- .../com/sk89q/worldedit/world/block/BlockType.java | 2 +- .../sk89q/worldedit/world/entity/EntityType.java | 2 +- .../com/sk89q/worldedit/world/item/ItemType.java | 2 +- .../internal/expression/BaseExpressionTest.java | 2 ++ .../worldedit/util/collection/BlockMapTest.java | 7 +++++++ 10 files changed, 50 insertions(+), 6 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java index 406964c24..9f798e21c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformManager.java @@ -321,10 +321,24 @@ public class PlatformManager { return queryCapability(Capability.CONFIGURATION).getConfiguration(); } + /** + * Get the current supported {@link SideEffect}s. + * + * @return the supported side effects + * @throws NoCapablePlatformException thrown if no platform is capable + */ public Collection getSupportedSideEffects() { return queryCapability(Capability.WORLD_EDITING).getSupportedSideEffects(); } + /** + * Get the initialized state of the Platform. + * {@return if the platform manager is initialized} + */ + public boolean isInitialized() { + return initialized.get(); + } + /** * You shouldn't have been calling this anyways, but this is now deprecated. Either don't * fire this event at all, or fire the new event via the event bus if you're a platform. diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/NamespacedRegistry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/NamespacedRegistry.java index 5fca65799..e6715907c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/NamespacedRegistry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/NamespacedRegistry.java @@ -47,8 +47,16 @@ public final class NamespacedRegistry extends Registry { this(name, MINECRAFT_NAMESPACE); } + public NamespacedRegistry(final String name, final boolean checkInitialized) { + this(name, MINECRAFT_NAMESPACE, checkInitialized); + } + public NamespacedRegistry(final String name, final String defaultNamespace) { - super(name); + this(name, defaultNamespace, false); + } + + public NamespacedRegistry(final String name, final String defaultNamespace, final boolean checkInitialized) { + super(name, checkInitialized); this.defaultNamespace = defaultNamespace; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Registry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Registry.java index 2bb20010a..b29af49e8 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Registry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Registry.java @@ -19,6 +19,8 @@ package com.sk89q.worldedit.registry; +import com.sk89q.worldedit.WorldEdit; + import javax.annotation.Nullable; import java.util.Collection; import java.util.Collections; @@ -35,9 +37,15 @@ public class Registry implements Iterable { private final Map map = new HashMap<>(); private final String name; + private final boolean checkInitialized; public Registry(final String name) { + this(name, false); + } + + public Registry(final String name, final boolean checkInitialized) { this.name = name; + this.checkInitialized = checkInitialized; } public String getName() { @@ -53,6 +61,11 @@ public class Registry implements Iterable { @Nullable public V get(final String key) { checkState(key.equals(key.toLowerCase(Locale.ROOT)), "key must be lowercase: %s", key); + if (this.checkInitialized) { + checkState( + WorldEdit.getInstance().getPlatformManager().isInitialized(), + "WorldEdit is not initialized yet."); + } return this.map.get(key); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeType.java index ab503e1fb..e8501da69 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeType.java @@ -32,7 +32,7 @@ import com.sk89q.worldedit.registry.NamespacedRegistry; public class BiomeType implements RegistryItem, Keyed, BiomePattern { //FAWE end - public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("biome type"); + public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("biome type", true); private final String id; private int legacyId = -1; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockCategory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockCategory.java index 3ed6f7a3f..29d8ee53b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockCategory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockCategory.java @@ -36,7 +36,7 @@ public class BlockCategory extends Category implements Keyed { //FAWE start private boolean[] flatMap; //FAWE end - public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("block tag"); + public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("block tag", true); public BlockCategory(final String id) { super(id); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java index 043a89bf5..01a5d5c8a 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java @@ -56,7 +56,7 @@ import static com.google.common.base.Preconditions.checkArgument; public class BlockType implements Keyed, Pattern { //FAWE end - public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("block type"); + public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("block type", true); private static final Logger LOGGER = LogManagerCompat.getLogger(); private final String id; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/entity/EntityType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/entity/EntityType.java index 98a70b7e5..5a530c8a6 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/entity/EntityType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/entity/EntityType.java @@ -27,7 +27,7 @@ import com.sk89q.worldedit.registry.NamespacedRegistry; public class EntityType implements RegistryItem, Keyed { //FAWE end - public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("entity type"); + public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("entity type", true); private final String id; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemType.java index 97549320d..ecba92f8b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemType.java @@ -39,7 +39,7 @@ import javax.annotation.Nullable; public class ItemType implements RegistryItem, Keyed { //FAWE end - public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("item type"); + public static final NamespacedRegistry REGISTRY = new NamespacedRegistry<>("item type", true); private final String id; @SuppressWarnings("deprecation") diff --git a/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/BaseExpressionTest.java b/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/BaseExpressionTest.java index 0fbf9556a..3491347a4 100644 --- a/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/BaseExpressionTest.java +++ b/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/BaseExpressionTest.java @@ -35,6 +35,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -63,6 +64,7 @@ class BaseExpressionTest { }); WorldEdit.getInstance().getPlatformManager().register(mockPlat); WorldEdit.getInstance().getEventBus().post(new PlatformsRegisteredEvent()); + assertTrue(WorldEdit.getInstance().getPlatformManager().isInitialized(), "Platform is not initialized"); WorldEdit.getInstance().getConfiguration().calculationTimeout = 1_000; } diff --git a/worldedit-core/src/test/java/com/sk89q/worldedit/util/collection/BlockMapTest.java b/worldedit-core/src/test/java/com/sk89q/worldedit/util/collection/BlockMapTest.java index 1bb09ee17..595993919 100644 --- a/worldedit-core/src/test/java/com/sk89q/worldedit/util/collection/BlockMapTest.java +++ b/worldedit-core/src/test/java/com/sk89q/worldedit/util/collection/BlockMapTest.java @@ -87,10 +87,17 @@ class BlockMapTest { Stream.of(Capability.values()) .collect(Collectors.toMap(Function.identity(), __ -> Preference.NORMAL)) ); + when(MOCKED_PLATFORM.getConfiguration()).thenReturn(new LocalConfiguration() { + @Override + public void load() { + } + }); PlatformManager platformManager = WorldEdit.getInstance().getPlatformManager(); platformManager.register(MOCKED_PLATFORM); WorldEdit.getInstance().getEventBus().post(new PlatformsRegisteredEvent()); + assertTrue(WorldEdit.getInstance().getPlatformManager().isInitialized(), "Platform is not initialized"); + registerBlock("minecraft:air"); registerBlock("minecraft:oak_wood"); }