Mirror von
https://github.com/IntellectualSites/FastAsyncWorldEdit.git
synchronisiert 2024-11-16 16:10:07 +01:00
Add method to check initialize state in PlatformManager, deny access to registries when uninitialized (#2237)
* Add initialize method to PlatformManager, modify UnitTest * Prevent too early access to registries Check for worldedit init state in Registry * review: changed the error message to a generic message Co-authored-by: Octavia Togami <octavia.togami@gmail.com> --------- Co-authored-by: Octavia Togami <octavia.togami@gmail.com> (cherry picked from commit d4556dfea2cb64075ba22b0ad859e57be6ecd33f)
Dieser Commit ist enthalten in:
Ursprung
6c46da52c2
Commit
945a09b4ef
@ -19,6 +19,8 @@
|
||||
|
||||
package com.sk89q.worldedit.registry;
|
||||
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
|
||||
import com.fastasyncworldedit.core.registry.RegistryItem;
|
||||
import com.sk89q.worldedit.command.util.SuggestionHelper;
|
||||
|
||||
@ -47,8 +49,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;
|
||||
}
|
||||
|
||||
|
@ -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,10 @@ 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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
@ -328,8 +328,10 @@ public class BlockType implements Keyed, Pattern {
|
||||
|
||||
/**
|
||||
* Gets the legacy ID. Needed for legacy reasons.
|
||||
*
|
||||
* <p>
|
||||
* DO NOT USE THIS.
|
||||
* </p>
|
||||
*
|
||||
* @return legacy id or 0, if unknown
|
||||
*/
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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")
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
package com.sk89q.worldedit.util.collection;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.sk89q.worldedit.LocalConfiguration;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.event.platform.PlatformsRegisteredEvent;
|
||||
import com.sk89q.worldedit.extension.platform.Capability;
|
||||
@ -87,10 +88,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");
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren