Skip to content

Commit

Permalink
Use a supplier to retain the lazy-load & dynamicness of the existing …
Browse files Browse the repository at this point in the history
…system, but still retaining the inversion of control that this PR was intended to provide
  • Loading branch information
me4502 committed Jun 18, 2023
1 parent 8402d05 commit 50a3e8c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -907,6 +919,23 @@ public void initializeRegistries() {
StructureType.REGISTRY.register(name.toString(), new StructureType(name.toString()));
}
}

// BiomeCategories
Registry<Biome> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@

import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;

public abstract class Category<T extends Keyed> {
private final Set<T> set;
private final Set<T> set = new HashSet<>();
private final Supplier<Set<T>> 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<T> contents) {
public Category(final String id, final Supplier<Set<T>> contentSupplier) {
this.id = id;
this.set = Set.copyOf(contents);
this.empty = false;
this.supplier = contentSupplier;
}

public final String getId() {
Expand All @@ -45,7 +45,11 @@ public final String getId() {

public final Set<T> 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;
Expand All @@ -54,9 +58,9 @@ public final Set<T> 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<T> load();
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sk89q.worldedit.registry.NamespacedRegistry;

import java.util.Set;
import java.util.function.Supplier;

/**
* A category of biomes.
Expand All @@ -36,8 +37,8 @@ public BiomeCategory(final String id) {
super(id);
}

public BiomeCategory(final String id, final Set<BiomeType> contents) {
super(id, contents);
public BiomeCategory(final String id, final Supplier<Set<BiomeType>> contentSupplier) {
super(id, contentSupplier);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void serverStarted(StartedEngineEvent<Server> 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())));
}
});

Expand Down

0 comments on commit 50a3e8c

Please sign in to comment.