From 50a3e8c2b2f8f0a00b1514152c7326fd293450a8 Mon Sep 17 00:00:00 2001 From: Madeline Miller Date: Sun, 18 Jun 2023 16:43:01 +1000 Subject: [PATCH] Use a supplier to retain the lazy-load & dynamicness of the existing system, but still retaining the inversion of control that this PR was intended to provide --- .../impl/v1_19_R3/PaperweightAdapter.java | 29 +++++++++++++++++++ .../impl/v1_20_R1/PaperweightAdapter.java | 2 +- .../sk89q/worldedit/registry/Category.java | 27 +++++++++-------- .../worldedit/world/biome/BiomeCategory.java | 5 ++-- .../worldedit/fabric/FabricWorldEdit.java | 2 +- .../sk89q/worldedit/forge/ForgeWorldEdit.java | 2 +- .../worldedit/sponge/SpongeWorldEdit.java | 2 +- 7 files changed, 49 insertions(+), 20 deletions(-) diff --git a/worldedit-bukkit/adapters/adapter-1.19.4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_19_R3/PaperweightAdapter.java b/worldedit-bukkit/adapters/adapter-1.19.4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_19_R3/PaperweightAdapter.java index c46dc028dd..64d37df8f0 100644 --- a/worldedit-bukkit/adapters/adapter-1.19.4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_19_R3/PaperweightAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1.19.4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_19_R3/PaperweightAdapter.java @@ -56,6 +56,7 @@ import com.sk89q.worldedit.util.io.file.SafeFiles; import com.sk89q.worldedit.world.DataFixer; import com.sk89q.worldedit.world.RegenOptions; +import com.sk89q.worldedit.world.biome.BiomeCategory; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.biome.BiomeTypes; import com.sk89q.worldedit.world.block.BaseBlock; @@ -70,6 +71,8 @@ import net.minecraft.Util; import net.minecraft.core.BlockPos; import net.minecraft.core.Holder; +import net.minecraft.core.HolderSet; +import net.minecraft.core.Registry; import net.minecraft.core.SectionPos; import net.minecraft.core.registries.Registries; import net.minecraft.nbt.ByteArrayTag; @@ -184,6 +187,7 @@ import java.util.concurrent.ExecutionException; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; import javax.annotation.Nullable; import static com.google.common.base.Preconditions.checkNotNull; @@ -315,6 +319,14 @@ private static Item getItemFromType(ItemType itemType) { return DedicatedServer.getServer().registryAccess().registryOrThrow(Registries.ITEM).get(ResourceLocation.tryParse(itemType.getId())); } + public BiomeType adapt(Biome biome) { + var mcBiome = ((CraftServer) Bukkit.getServer()).getServer().registryAccess().registryOrThrow(Registries.BIOME).getKey(biome); + if (mcBiome == null) { + return null; + } + return BiomeType.REGISTRY.get(mcBiome.toString()); + } + @Override public OptionalInt getInternalBlockStateId(BlockData data) { net.minecraft.world.level.block.state.BlockState state = ((CraftBlockData) data).getState(); @@ -907,6 +919,23 @@ public void initializeRegistries() { StructureType.REGISTRY.register(name.toString(), new StructureType(name.toString())); } } + + // BiomeCategories + Registry biomeRegistry = server.registryAccess().registryOrThrow(Registries.BIOME); + biomeRegistry.getTagNames().forEach(tagKey -> { + String key = tagKey.location().toString(); + if (BiomeCategory.REGISTRY.get(key) == null) { + BiomeCategory.REGISTRY.register(key, new BiomeCategory( + key, + () -> biomeRegistry.getTag(tagKey) + .stream() + .flatMap(HolderSet.Named::stream) + .map(Holder::value) + .map(this::adapt) + .collect(Collectors.toSet())) + ); + } + }); } public boolean generateFeature(ConfiguredFeatureType type, World world, EditSession session, BlockVector3 pt) { diff --git a/worldedit-bukkit/adapters/adapter-1.20/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_20_R1/PaperweightAdapter.java b/worldedit-bukkit/adapters/adapter-1.20/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_20_R1/PaperweightAdapter.java index d173152fa4..c7fddae68e 100644 --- a/worldedit-bukkit/adapters/adapter-1.20/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_20_R1/PaperweightAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1.20/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/v1_20_R1/PaperweightAdapter.java @@ -928,7 +928,7 @@ public void initializeRegistries() { if (BiomeCategory.REGISTRY.get(key) == null) { BiomeCategory.REGISTRY.register(key, new BiomeCategory( key, - biomeRegistry.getTag(tagKey) + () -> biomeRegistry.getTag(tagKey) .stream() .flatMap(HolderSet.Named::stream) .map(Holder::value) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Category.java b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Category.java index b4928f94eb..a2ad5bbd72 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Category.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/registry/Category.java @@ -21,22 +21,22 @@ import java.util.HashSet; import java.util.Set; +import java.util.function.Supplier; public abstract class Category { - private final Set set; + private final Set set = new HashSet<>(); + private final Supplier> supplier; protected final String id; private boolean empty = true; public Category(final String id) { this.id = id; - // TODO Make this immutable in WE8 - this.set = new HashSet<>(); + this.supplier = null; } - public Category(final String id, final Set contents) { + public Category(final String id, final Supplier> contentSupplier) { this.id = id; - this.set = Set.copyOf(contents); - this.empty = false; + this.supplier = contentSupplier; } public final String getId() { @@ -45,7 +45,11 @@ public final String getId() { public final Set getAll() { if (this.empty) { - this.set.addAll(this.load()); + if (supplier != null) { + this.set.addAll(this.supplier.get()); + } else { + this.set.addAll(this.load()); + } this.empty = false; } return this.set; @@ -54,9 +58,9 @@ public final Set getAll() { /** * Loads the contents of this category from the platform. * - * @deprecated The load system will be removed in a future WorldEdit release. The registries should be populated by - * the platforms. * @return The loaded contents of the category + * @deprecated The load system will be removed in a future WorldEdit release. The registries should be populated by + * the platforms via the supplier constructor. */ @Deprecated protected abstract Set load(); @@ -71,11 +75,6 @@ public boolean contains(final T object) { return this.getAll().contains(object); } - /** - * @deprecated The load system will be removed in a future WorldEdit release. The registries should be populated by - * the platforms. - */ - @Deprecated public void invalidateCache() { this.set.clear(); this.empty = true; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeCategory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeCategory.java index 55be1c227d..a51a868237 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeCategory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/biome/BiomeCategory.java @@ -24,6 +24,7 @@ import com.sk89q.worldedit.registry.NamespacedRegistry; import java.util.Set; +import java.util.function.Supplier; /** * A category of biomes. @@ -36,8 +37,8 @@ public BiomeCategory(final String id) { super(id); } - public BiomeCategory(final String id, final Set contents) { - super(id, contents); + public BiomeCategory(final String id, final Supplier> contentSupplier) { + super(id, contentSupplier); } @Override diff --git a/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricWorldEdit.java b/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricWorldEdit.java index 5cc63ec6b6..c6ac261eda 100644 --- a/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricWorldEdit.java +++ b/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricWorldEdit.java @@ -241,7 +241,7 @@ private void setupRegistries(MinecraftServer server) { if (BiomeCategory.REGISTRY.get(key) == null) { BiomeCategory.REGISTRY.register(key, new BiomeCategory( key, - biomeRegistry.getTag(tagKey) + () -> biomeRegistry.getTag(tagKey) .stream() .flatMap(HolderSet.Named::stream) .map(Holder::value) diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java index 65d12cdc11..ae518e4f10 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeWorldEdit.java @@ -223,7 +223,7 @@ private void setupRegistries(MinecraftServer server) { if (BiomeCategory.REGISTRY.get(key) == null) { BiomeCategory.REGISTRY.register(key, new BiomeCategory( key, - biomeRegistry.getTag(tagKey) + () -> biomeRegistry.getTag(tagKey) .stream() .flatMap(HolderSet.Named::stream) .map(Holder::value) diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java index 09d789be77..78b47b2025 100644 --- a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java @@ -210,7 +210,7 @@ public void serverStarted(StartedEngineEvent event) { event.game().registry(RegistryTypes.BIOME).tags().forEach(biomeTag -> { String id = biomeTag.key().asString(); if (!BiomeCategory.REGISTRY.keySet().contains(id)) { - BiomeCategory.REGISTRY.register(id, new BiomeCategory(id, event.game().registry(RegistryTypes.BIOME).taggedValues(biomeTag).stream().map(SpongeAdapter::adapt).collect(Collectors.toSet()))); + BiomeCategory.REGISTRY.register(id, new BiomeCategory(id, () -> event.game().registry(RegistryTypes.BIOME).taggedValues(biomeTag).stream().map(SpongeAdapter::adapt).collect(Collectors.toSet()))); } });