mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-10 01:36:19 +00:00
implement SELF biome without horrible null impl
This commit is contained in:
@@ -26,6 +26,7 @@ import com.dfsek.terra.addons.biome.pipeline.source.BiomeSource;
|
||||
import com.dfsek.terra.addons.manifest.api.AddonInitializer;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.addon.BaseAddon;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPostLoadEvent;
|
||||
import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
|
||||
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||
@@ -76,10 +77,13 @@ public class BiomePipelineAddon implements AddonInitializer {
|
||||
stageRegistry.register("BORDER", BorderMutatorTemplate::new);
|
||||
stageRegistry.register("BORDER_LIST", BorderListMutatorTemplate::new);
|
||||
})
|
||||
.then(event -> {
|
||||
Registry<Biome> biomeRegistry = event.getPack().getOrCreateRegistry(Biome.class);
|
||||
event.getPack().applyLoader(BiomeDelegate.class, new BiomeDelegateLoader(biomeRegistry));
|
||||
})
|
||||
.failThrough();
|
||||
platform.getEventManager()
|
||||
.getHandler(FunctionalEventHandler.class)
|
||||
.register(addon, ConfigPackPostLoadEvent.class)
|
||||
.then(event -> {
|
||||
Registry<Biome> biomeRegistry = event.getPack().getRegistry(Biome.class);
|
||||
event.getPack().applyLoader(BiomeDelegate.class, new BiomeDelegateLoader(biomeRegistry));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,10 @@ public interface BiomeDelegate extends StringIdentifiable {
|
||||
return false;
|
||||
}
|
||||
|
||||
default boolean isSelf() {
|
||||
return false;
|
||||
}
|
||||
|
||||
static BiomeDelegate ephemeral(String id) {
|
||||
return new BiomeDelegate() {
|
||||
private final Set<String> tags = Collections.singleton(id);
|
||||
@@ -59,4 +63,33 @@ public interface BiomeDelegate extends StringIdentifiable {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static BiomeDelegate self() {
|
||||
return new BiomeDelegate() {
|
||||
@Override
|
||||
public Biome getBiome() {
|
||||
throw new UnsupportedOperationException("Cannot get biome from self delegate");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSelf() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEphemeral() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getTags() {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return "SELF";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ public class BiomeDelegateLoader implements TypeLoader<BiomeDelegate> {
|
||||
|
||||
@Override
|
||||
public BiomeDelegate load(@NotNull AnnotatedType t, @NotNull Object c, @NotNull ConfigLoader loader) throws LoadException {
|
||||
if(c == "SELF") return BiomeDelegate.self();
|
||||
return biomeRegistry
|
||||
.get((String) c)
|
||||
.map(BiomeDelegate::from)
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import com.dfsek.terra.addons.biome.pipeline.api.BiomeDelegate;
|
||||
import com.dfsek.terra.addons.biome.pipeline.api.stage.type.BiomeMutator;
|
||||
@@ -42,14 +43,13 @@ public class BorderListMutator implements BiomeMutator {
|
||||
for(int zi = -1; zi <= 1; zi++) {
|
||||
if(xi == 0 && zi == 0) continue;
|
||||
BiomeDelegate current = viewPoint.getBiome(xi, zi);
|
||||
if(current == null) continue;
|
||||
if(current.getTags().contains(border)) {
|
||||
if(replace.containsKey(origin)) {
|
||||
BiomeDelegate biome = replace.get(origin).get(noiseSampler, x, z, seed);
|
||||
return biome == null ? origin : biome;
|
||||
return biome.isSelf() ? origin : biome;
|
||||
}
|
||||
BiomeDelegate biome = replaceDefault.get(noiseSampler, x, z, seed);
|
||||
return biome == null ? origin : biome;
|
||||
return biome.isSelf() ? origin : biome;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public class BorderListMutator implements BiomeMutator {
|
||||
public Iterable<BiomeDelegate> getBiomes(Iterable<BiomeDelegate> biomes) {
|
||||
Set<BiomeDelegate> biomeSet = new HashSet<>();
|
||||
biomes.forEach(biomeSet::add);
|
||||
biomeSet.addAll(replaceDefault.getContents().stream().filter(Objects::nonNull).toList());
|
||||
biomeSet.addAll(replaceDefault.getContents().stream().filter(Predicate.not(BiomeDelegate::isSelf)).toList());
|
||||
replace.forEach((biome, collection) -> biomeSet.addAll(collection.getContents()));
|
||||
return biomeSet;
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
|
||||
public class BorderMutator implements BiomeMutator {
|
||||
@@ -38,10 +38,9 @@ public class BorderMutator implements BiomeMutator {
|
||||
for(int zi = -1; zi <= 1; zi++) {
|
||||
if(xi == 0 && zi == 0) continue;
|
||||
BiomeDelegate current = viewPoint.getBiome(xi, zi);
|
||||
if(current == null) continue;
|
||||
if(current.getTags().contains(border)) {
|
||||
BiomeDelegate biome = replace.get(noiseSampler, x, z, seed);
|
||||
return biome == null ? origin : biome;
|
||||
return biome.isSelf() ? origin : biome;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,7 +52,15 @@ public class BorderMutator implements BiomeMutator {
|
||||
public Iterable<BiomeDelegate> getBiomes(Iterable<BiomeDelegate> biomes) {
|
||||
Set<BiomeDelegate> biomeSet = new HashSet<>();
|
||||
biomes.forEach(biomeSet::add);
|
||||
biomeSet.addAll(replace.getContents().stream().filter(Objects::nonNull).toList());
|
||||
biomeSet.addAll(
|
||||
replace
|
||||
.getContents()
|
||||
.stream()
|
||||
.filter(
|
||||
Predicate.not(BiomeDelegate::isSelf)
|
||||
)
|
||||
.toList()
|
||||
);
|
||||
return biomeSet;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,11 +37,11 @@ public class ReplaceListMutator implements BiomeMutator {
|
||||
BiomeDelegate center = viewPoint.getBiome(0, 0);
|
||||
if(replace.containsKey(center)) {
|
||||
BiomeDelegate biome = replace.get(center).get(sampler, x, z, seed);
|
||||
return biome == null ? viewPoint.getBiome(0, 0) : biome;
|
||||
return biome.isSelf() ? viewPoint.getBiome(0, 0) : biome;
|
||||
}
|
||||
if(viewPoint.getBiome(0, 0).getTags().contains(defaultTag)) {
|
||||
BiomeDelegate biome = replaceDefault.get(sampler, x, z, seed);
|
||||
return biome == null ? viewPoint.getBiome(0, 0) : biome;
|
||||
return biome.isSelf() ? viewPoint.getBiome(0, 0) : biome;
|
||||
}
|
||||
return center;
|
||||
}
|
||||
@@ -60,11 +60,11 @@ public class ReplaceListMutator implements BiomeMutator {
|
||||
}
|
||||
});
|
||||
biomeSet.addAll(replaceDefault.getContents().stream().flatMap(terraBiome -> {
|
||||
if(terraBiome == null) return reject.stream();
|
||||
if(terraBiome.isSelf()) return reject.stream();
|
||||
return Stream.of(terraBiome);
|
||||
}).toList());
|
||||
replace.forEach((biome, collection) -> biomeSet.addAll(collection.getContents().stream().map(terraBiome -> {
|
||||
if(terraBiome == null) return biome;
|
||||
if(terraBiome.isSelf()) return biome;
|
||||
return terraBiome;
|
||||
}).toList()));
|
||||
return biomeSet;
|
||||
|
||||
@@ -32,7 +32,7 @@ public class ReplaceMutator implements BiomeMutator {
|
||||
public BiomeDelegate mutate(ViewPoint viewPoint, double x, double z, long seed) {
|
||||
if(viewPoint.getBiome(0, 0).getTags().contains(replaceableTag)) {
|
||||
BiomeDelegate biome = replace.get(sampler, x, z, seed);
|
||||
return biome == null ? viewPoint.getBiome(0, 0) : biome;
|
||||
return biome.isSelf() ? viewPoint.getBiome(0, 0) : biome;
|
||||
}
|
||||
return viewPoint.getBiome(0, 0);
|
||||
}
|
||||
@@ -49,7 +49,7 @@ public class ReplaceMutator implements BiomeMutator {
|
||||
}
|
||||
});
|
||||
biomeSet.addAll(replace.getContents().stream().flatMap(terraBiome -> {
|
||||
if(terraBiome == null) return reject.stream();
|
||||
if(terraBiome.isSelf()) return reject.stream();
|
||||
return Stream.of(terraBiome);
|
||||
}).toList());
|
||||
return biomeSet;
|
||||
|
||||
Reference in New Issue
Block a user