3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-11-17 00:20:09 +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:
JOO200 2023-03-19 01:13:54 -04:00 committet von Matt
Ursprung 6c46da52c2
Commit 945a09b4ef
9 geänderte Dateien mit 40 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -19,6 +19,8 @@
package com.sk89q.worldedit.registry; package com.sk89q.worldedit.registry;
import com.sk89q.worldedit.WorldEdit;
import com.fastasyncworldedit.core.registry.RegistryItem; import com.fastasyncworldedit.core.registry.RegistryItem;
import com.sk89q.worldedit.command.util.SuggestionHelper; 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); 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) { 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; this.defaultNamespace = defaultNamespace;
} }

Datei anzeigen

@ -19,6 +19,8 @@
package com.sk89q.worldedit.registry; package com.sk89q.worldedit.registry;
import com.sk89q.worldedit.WorldEdit;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; 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 Map<String, V> map = new HashMap<>();
private final String name; private final String name;
private final boolean checkInitialized;
public Registry(final String name) { public Registry(final String name) {
this(name, false);
}
public Registry(final String name, final boolean checkInitialized) {
this.name = name; this.name = name;
this.checkInitialized = checkInitialized;
} }
public String getName() { public String getName() {
@ -53,6 +61,10 @@ public class Registry<V extends Keyed> implements Iterable<V> {
@Nullable @Nullable
public V get(final String key) { public V get(final String key) {
checkState(key.equals(key.toLowerCase(Locale.ROOT)), "key must be lowercase: %s", 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); return this.map.get(key);
} }

Datei anzeigen

@ -32,7 +32,7 @@ import com.sk89q.worldedit.registry.NamespacedRegistry;
public class BiomeType implements RegistryItem, Keyed, BiomePattern { public class BiomeType implements RegistryItem, Keyed, BiomePattern {
//FAWE end //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 final String id;
private int legacyId = -1; private int legacyId = -1;

Datei anzeigen

@ -36,7 +36,7 @@ public class BlockCategory extends Category<BlockType> implements Keyed {
//FAWE start //FAWE start
private boolean[] flatMap; private boolean[] flatMap;
//FAWE end //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) { public BlockCategory(final String id) {
super(id); super(id);

Datei anzeigen

@ -56,7 +56,7 @@ import static com.google.common.base.Preconditions.checkArgument;
public class BlockType implements Keyed, Pattern { public class BlockType implements Keyed, Pattern {
//FAWE end //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 static final Logger LOGGER = LogManagerCompat.getLogger();
private final String id; private final String id;
@ -328,8 +328,10 @@ public class BlockType implements Keyed, Pattern {
/** /**
* Gets the legacy ID. Needed for legacy reasons. * Gets the legacy ID. Needed for legacy reasons.
*
* <p> * <p>
* DO NOT USE THIS. * DO NOT USE THIS.
* </p>
* *
* @return legacy id or 0, if unknown * @return legacy id or 0, if unknown
*/ */

Datei anzeigen

@ -27,7 +27,7 @@ import com.sk89q.worldedit.registry.NamespacedRegistry;
public class EntityType implements RegistryItem, Keyed { public class EntityType implements RegistryItem, Keyed {
//FAWE end //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; private final String id;

Datei anzeigen

@ -39,7 +39,7 @@ import javax.annotation.Nullable;
public class ItemType implements RegistryItem, Keyed { public class ItemType implements RegistryItem, Keyed {
//FAWE end //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; private final String id;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")

Datei anzeigen

@ -35,6 +35,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals; 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.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -63,6 +64,7 @@ class BaseExpressionTest {
}); });
WorldEdit.getInstance().getPlatformManager().register(mockPlat); WorldEdit.getInstance().getPlatformManager().register(mockPlat);
WorldEdit.getInstance().getEventBus().post(new PlatformsRegisteredEvent()); WorldEdit.getInstance().getEventBus().post(new PlatformsRegisteredEvent());
assertTrue(WorldEdit.getInstance().getPlatformManager().isInitialized(), "Platform is not initialized");
WorldEdit.getInstance().getConfiguration().calculationTimeout = 1_000; WorldEdit.getInstance().getConfiguration().calculationTimeout = 1_000;
} }

Datei anzeigen

@ -21,6 +21,7 @@
package com.sk89q.worldedit.util.collection; package com.sk89q.worldedit.util.collection;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.event.platform.PlatformsRegisteredEvent; import com.sk89q.worldedit.event.platform.PlatformsRegisteredEvent;
import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.extension.platform.Capability;
@ -87,10 +88,17 @@ class BlockMapTest {
Stream.of(Capability.values()) Stream.of(Capability.values())
.collect(Collectors.toMap(Function.identity(), __ -> Preference.NORMAL)) .collect(Collectors.toMap(Function.identity(), __ -> Preference.NORMAL))
); );
when(MOCKED_PLATFORM.getConfiguration()).thenReturn(new LocalConfiguration() {
@Override
public void load() {
}
});
PlatformManager platformManager = WorldEdit.getInstance().getPlatformManager(); PlatformManager platformManager = WorldEdit.getInstance().getPlatformManager();
platformManager.register(MOCKED_PLATFORM); platformManager.register(MOCKED_PLATFORM);
WorldEdit.getInstance().getEventBus().post(new PlatformsRegisteredEvent()); WorldEdit.getInstance().getEventBus().post(new PlatformsRegisteredEvent());
assertTrue(WorldEdit.getInstance().getPlatformManager().isInitialized(), "Platform is not initialized");
registerBlock("minecraft:air"); registerBlock("minecraft:air");
registerBlock("minecraft:oak_wood"); registerBlock("minecraft:oak_wood");
} }