Add method to check initialize state in PlatformManager, deny access to registries when uninitialized

Dieser Commit ist enthalten in:
Alexander Brandes 2023-05-20 20:52:03 +02:00
Ursprung 85f5006a0b
Commit 264185a323
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 158F5701A6AAD00C
10 geänderte Dateien mit 50 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -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<SideEffect> 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.

Datei anzeigen

@ -47,8 +47,16 @@ public final class NamespacedRegistry<V extends Keyed> extends Registry<V> {
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;
}

Datei anzeigen

@ -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<V extends Keyed> implements Iterable<V> {
private final Map<String, V> 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<V extends Keyed> implements Iterable<V> {
@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);
}

Datei anzeigen

@ -32,7 +32,7 @@ import com.sk89q.worldedit.registry.NamespacedRegistry;
public class BiomeType implements RegistryItem, Keyed, BiomePattern {
//FAWE end
public static final NamespacedRegistry<BiomeType> REGISTRY = new NamespacedRegistry<>("biome type");
public static final NamespacedRegistry<BiomeType> REGISTRY = new NamespacedRegistry<>("biome type", true);
private final String id;
private int legacyId = -1;

Datei anzeigen

@ -36,7 +36,7 @@ public class BlockCategory extends Category<BlockType> implements Keyed {
//FAWE start
private boolean[] flatMap;
//FAWE end
public static final NamespacedRegistry<BlockCategory> REGISTRY = new NamespacedRegistry<>("block tag");
public static final NamespacedRegistry<BlockCategory> REGISTRY = new NamespacedRegistry<>("block tag", true);
public BlockCategory(final String id) {
super(id);

Datei anzeigen

@ -56,7 +56,7 @@ import static com.google.common.base.Preconditions.checkArgument;
public class BlockType implements Keyed, Pattern {
//FAWE end
public static final NamespacedRegistry<BlockType> REGISTRY = new NamespacedRegistry<>("block type");
public static final NamespacedRegistry<BlockType> REGISTRY = new NamespacedRegistry<>("block type", true);
private static final Logger LOGGER = LogManagerCompat.getLogger();
private final String id;

Datei anzeigen

@ -27,7 +27,7 @@ import com.sk89q.worldedit.registry.NamespacedRegistry;
public class EntityType implements RegistryItem, Keyed {
//FAWE end
public static final NamespacedRegistry<EntityType> REGISTRY = new NamespacedRegistry<>("entity type");
public static final NamespacedRegistry<EntityType> REGISTRY = new NamespacedRegistry<>("entity type", true);
private final String id;

Datei anzeigen

@ -39,7 +39,7 @@ import javax.annotation.Nullable;
public class ItemType implements RegistryItem, Keyed {
//FAWE end
public static final NamespacedRegistry<ItemType> REGISTRY = new NamespacedRegistry<>("item type");
public static final NamespacedRegistry<ItemType> REGISTRY = new NamespacedRegistry<>("item type", true);
private final String id;
@SuppressWarnings("deprecation")

Datei anzeigen

@ -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;
}

Datei anzeigen

@ -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");
}