mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-04 00:45:57 +00:00
implement biome query API
This commit is contained in:
parent
2f2fb54dea
commit
dff2388b37
@ -0,0 +1,48 @@
|
|||||||
|
package com.dfsek.terra.addons.biome.tag;
|
||||||
|
|
||||||
|
import com.dfsek.terra.addons.biome.tag.impl.BiomeTagFlattener;
|
||||||
|
import com.dfsek.terra.addons.biome.tag.impl.BiomeTagHolder;
|
||||||
|
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.functional.FunctionalEventHandler;
|
||||||
|
import com.dfsek.terra.api.inject.annotations.Inject;
|
||||||
|
import com.dfsek.terra.api.properties.Context;
|
||||||
|
import com.dfsek.terra.api.properties.PropertyKey;
|
||||||
|
import com.dfsek.terra.api.world.biome.Biome;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
|
||||||
|
public class BiomeTagAPIAddon implements AddonInitializer {
|
||||||
|
@Inject
|
||||||
|
private Platform platform;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private BaseAddon addon;
|
||||||
|
|
||||||
|
public static PropertyKey<BiomeTagHolder> BIOME_TAG_KEY = Context.create(BiomeTagHolder.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize() {
|
||||||
|
|
||||||
|
platform.getEventManager()
|
||||||
|
.getHandler(FunctionalEventHandler.class)
|
||||||
|
.register(addon, ConfigPackPostLoadEvent.class)
|
||||||
|
.then(event -> {
|
||||||
|
Collection<Biome> biomes = event
|
||||||
|
.getPack()
|
||||||
|
.getRegistry(Biome.class)
|
||||||
|
.entries();
|
||||||
|
|
||||||
|
BiomeTagFlattener flattener = new BiomeTagFlattener(biomes
|
||||||
|
.stream()
|
||||||
|
.flatMap(biome -> biome.getTags().stream())
|
||||||
|
.toList());
|
||||||
|
|
||||||
|
biomes.forEach(biome -> biome.getContext().put(BIOME_TAG_KEY, new BiomeTagHolder(biome, flattener)));
|
||||||
|
})
|
||||||
|
.global();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.dfsek.terra.addons.biome.tag.api;
|
||||||
|
|
||||||
|
import com.dfsek.terra.addons.biome.tag.impl.SingleTagQuery;
|
||||||
|
import com.dfsek.terra.api.world.biome.Biome;
|
||||||
|
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
|
||||||
|
public final class BiomeQueries {
|
||||||
|
private BiomeQueries() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Predicate<Biome> has(String tag) {
|
||||||
|
return new SingleTagQuery(tag);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.dfsek.terra.addons.biome.tag.impl;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
public class BiomeTagFlattener {
|
||||||
|
private final List<String> tags;
|
||||||
|
|
||||||
|
public BiomeTagFlattener(List<String> tags) {
|
||||||
|
this.tags = tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int index(String tag) {
|
||||||
|
return tags.indexOf(tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return tags.size();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.dfsek.terra.addons.biome.tag.impl;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.properties.Properties;
|
||||||
|
import com.dfsek.terra.api.world.biome.Biome;
|
||||||
|
|
||||||
|
|
||||||
|
public class BiomeTagHolder implements Properties {
|
||||||
|
private final boolean[] tags;
|
||||||
|
private final BiomeTagFlattener flattener;
|
||||||
|
|
||||||
|
public BiomeTagHolder(Biome biome, BiomeTagFlattener flattener) {
|
||||||
|
this.tags = new boolean[flattener.size()];
|
||||||
|
this.flattener = flattener;
|
||||||
|
for(String tag : biome.getTags()) {
|
||||||
|
tags[flattener.index(tag)] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean get(int index) {
|
||||||
|
return tags[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
public BiomeTagFlattener getFlattener() {
|
||||||
|
return flattener;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.dfsek.terra.addons.biome.tag.impl;
|
||||||
|
|
||||||
|
import com.dfsek.terra.addons.biome.tag.BiomeTagAPIAddon;
|
||||||
|
import com.dfsek.terra.api.world.biome.Biome;
|
||||||
|
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
|
||||||
|
public class SingleTagQuery implements Predicate<Biome> {
|
||||||
|
private int tagIndex = -1;
|
||||||
|
private final String tag;
|
||||||
|
|
||||||
|
public SingleTagQuery(String tag) {
|
||||||
|
this.tag = tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean test(Biome biome) {
|
||||||
|
if(tagIndex < 0) {
|
||||||
|
tagIndex = biome
|
||||||
|
.getContext()
|
||||||
|
.get(BiomeTagAPIAddon.BIOME_TAG_KEY)
|
||||||
|
.getFlattener()
|
||||||
|
.index(tag);
|
||||||
|
}
|
||||||
|
return biome
|
||||||
|
.getContext()
|
||||||
|
.get(BiomeTagAPIAddon.BIOME_TAG_KEY)
|
||||||
|
.get(tagIndex);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
schema-version: 1
|
||||||
|
contributors:
|
||||||
|
- Terra contributors
|
||||||
|
id: biome-tag-api
|
||||||
|
version: @VERSION@
|
||||||
|
entrypoints:
|
||||||
|
- "com.dfsek.terra.addons.biome.tag.BiomeTagAPIAddon"
|
||||||
|
website:
|
||||||
|
issues: https://github.com/PolyhedralDev/Terra/issues
|
||||||
|
source: https://github.com/PolyhedralDev/Terra
|
||||||
|
docs: https://terra.polydev.org
|
||||||
|
license: MIT License
|
Loading…
x
Reference in New Issue
Block a user