Skip to content

Commit

Permalink
Initial work on biome tag condition, to fix biom-specific loot.
Browse files Browse the repository at this point in the history
Sorta working, needs testing and some fixing
  • Loading branch information
GirafiStudios committed Jun 11, 2022
1 parent 183ba79 commit f970a22
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static class Tags {
public static final TagKey<Item> TURTLE_EDIBLE = tag(Aquaculture.MOD_ID, "turtle_edible");
public static final TagKey<Item> TOOLTIP = tag(Aquaculture.MOD_ID, "tooltip");

public static final TagKey<Biome> TWILIGHT = biomeTag("forge","is_twilight");
public static final TagKey<Biome> IS_TWILIGHT = biomeTag("forge","is_twilight");
public static final TagKey<Biome> EMPTY = biomeTag("aquaculture","is_twilight");

public static TagKey<Item> tag(String modID, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.gson.JsonObject;
import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -16,6 +17,7 @@
import net.minecraft.util.GsonHelper;
import net.minecraft.world.level.biome.Biome;
import net.minecraftforge.common.Tags;
import net.minecraftforge.registries.ForgeRegistries;

import javax.annotation.Nullable;
import java.util.*;
Expand Down Expand Up @@ -51,70 +53,80 @@ public boolean test(ServerLevel serverLevel, float x, float y, float z) {
} else {
BlockPos pos = new BlockPos(x, y, z);
Biome biome = serverLevel.getBiome(pos).value();
ResourceLocation biomeFromRegistry = serverLevel.registryAccess().registryOrThrow(Registry.BIOME_REGISTRY).getKey(biome);

CheckType checkType = CheckType.getOrCreate(this.include, this.exclude, this.and);

List<ResourceLocation> validBiomes = CACHE.get(checkType);
if (validBiomes == null) {
validBiomes = getValidBiomes(serverLevel, checkType);
CACHE.put(checkType, validBiomes);
Registry<Biome> biomeRegistry = serverLevel.registryAccess().registryOrThrow(Registry.BIOME_REGISTRY);
Optional<ResourceKey<Biome>> resourceKey = biomeRegistry.getResourceKey(biome);
if (resourceKey.isPresent()) {
Optional<Holder<Biome>> biomeHolder = biomeRegistry.getHolder(resourceKey.get());
if (biomeHolder.isPresent()) {
CheckType checkType = CheckType.getOrCreate(this.include, this.exclude, this.and);

List<Holder<Biome>> validBiomes = getValidBiomes(serverLevel, checkType);
/*List<ResourceLocation> validBiomes = CACHE.get(checkType); //TODO Uncomment. Temporarily commented out for easier debugging. Might need fixing too
if (validBiomes == null) {
validBiomes = getValidBiomes(serverLevel, checkType);
CACHE.put(checkType, validBiomes);
}*/
return validBiomes.contains(biomeHolder.get());
}
}
return validBiomes.contains(biomeFromRegistry);
return false;
}
}

public static List<ResourceLocation> getValidBiomes(ServerLevel serverLevel, CheckType checkType) {
public static List<Holder<Biome>> getValidBiomes(ServerLevel serverLevel, CheckType checkType) {
return getValidBiomes(serverLevel, checkType.getInclude(), checkType.getExclude(), checkType.isAnd());
}

public static List<ResourceLocation> getValidBiomes(ServerLevel serverLevel, List<TagKey<Biome>> includeList, List<TagKey<Biome>> excludeList, boolean and) {
List<ResourceLocation> biomes = Lists.newArrayList();
Optional<? extends Registry<Biome>> biomeRegistry = serverLevel.registryAccess().registry(Registry.BIOME_REGISTRY);
if (biomeRegistry.isPresent()) {
Set<TagKey<Biome>> tags = biomeRegistry.get().getTagNames().collect(Collectors.toSet());
public static List<Holder<Biome>> getValidBiomes(ServerLevel serverLevel, List<TagKey<Biome>> includeList, List<TagKey<Biome>> excludeList, boolean and) {
List<Holder<Biome>> biomes = Lists.newArrayList();
Optional<? extends Registry<Biome>> optionalBiomeRegistry = serverLevel.registryAccess().registry(Registry.BIOME_REGISTRY);
if (optionalBiomeRegistry.isPresent()) {
Registry<Biome> biomeRegistry = optionalBiomeRegistry.get();

if (includeList.isEmpty() && !excludeList.isEmpty()) { //Add all tags, when only excluding biomes
includeList.addAll(tags);
includeList.addAll(biomeRegistry.getTagNames().collect(Collectors.toSet()));
excludeList.addAll(INVALID_TYPES);
}

if (!includeList.isEmpty()) {
List<TagKey<Biome>> addBiomes = Lists.newArrayList();
for (TagKey<Biome> type : includeList) {
if (tags.contains(type)) {
addBiomes.addAll(tags);
}
List<Holder<Biome>> addBiomes = Lists.newArrayList();
for (TagKey<Biome> tagKey : includeList) {
getBiomeFromTag(biomeRegistry, tagKey).forEach(addBiomes::add);
}

if (and) {
for (TagKey<Biome> tagKey : includeList) {
if (tags.contains(tagKey)) {
addBiomes.removeIf(biome -> !tags.contains(biome));
}
}
/*for (TagKey<Biome> tagKey : includeList) { //TODO
getBiomeFromTag(biomeRegistry, tagKey).forEach(biomeHolder -> !addBiomes.removeIf());
addBiomes.removeIf(biome -> !tags.contains(biome));
}*/
}

if (includeList.stream().noneMatch(INVALID_TYPES::contains)) { //Exclude invalid tags, as long as they're not specified in include
excludeList.addAll(INVALID_TYPES);
}
for (TagKey<Biome> addBiome : addBiomes) {
if (!biomes.contains(addBiome.location())) {
biomes.add(addBiome.location());
for (Holder<Biome> addBiome : addBiomes) {
if (!biomes.contains(addBiome)) {
biomes.add(addBiome);
}
}
}
if (!excludeList.isEmpty()) {
for (TagKey<Biome> type : excludeList) {
if (tags.contains(type)) {
biomes.remove(tags);
}
for (TagKey<Biome> tagKey : excludeList) {
getBiomeFromTag(biomeRegistry, tagKey).forEach(biomes::remove);
}
}
}
for (Holder<Biome> biomeHolder : biomes) {
System.out.println(biomeHolder.unwrapKey().get());
}
return biomes;
}

public static Iterable<Holder<Biome>> getBiomeFromTag(Registry<Biome> biomeRegistry, TagKey<Biome> tagKey) {
return biomeRegistry.getTagOrEmpty(tagKey);
}

public JsonElement serialize() {
if (this == ANY) {
return JsonNull.INSTANCE;
Expand All @@ -129,12 +141,12 @@ public JsonElement serialize() {
}
if (this.include != null) {
for (TagKey<Biome> tagKey : this.include) {
object.add("include", object.getAsJsonArray(tagKey.location().toString())); //TODO Test
object.add("include", object.getAsJsonArray(tagKey.location().toString()));
}
}
if (this.exclude != null) {
for (TagKey<Biome> tagKey : this.exclude) {
object.add("exclude", object.getAsJsonArray(tagKey.location().toString())); //TODO Test
object.add("exclude", object.getAsJsonArray(tagKey.location().toString()));
}
}
object.addProperty("add", object.getAsBoolean());
Expand All @@ -154,7 +166,7 @@ public static BiomeTagPredicate deserialize(@Nullable JsonElement element) {
JsonArray includeArray = GsonHelper.getAsJsonArray(location, "include");
for (int entry = 0; entry < includeArray.size(); entry++) {
String name = includeArray.get(entry).getAsString().toLowerCase(Locale.ROOT);
TagKey<Biome> type = TagKey.create(Registry.BIOME_REGISTRY, new ResourceLocation(name)); //TODO Test
TagKey<Biome> type = TagKey.create(Registry.BIOME_REGISTRY, new ResourceLocation(name));
include.add(type);
}
}
Expand All @@ -164,7 +176,7 @@ public static BiomeTagPredicate deserialize(@Nullable JsonElement element) {
JsonArray excludeArray = GsonHelper.getAsJsonArray(location, "exclude");
for (int entry = 0; entry < excludeArray.size(); entry++) {
String name = excludeArray.get(entry).getAsString().toLowerCase(Locale.ROOT);
TagKey<Biome> type = TagKey.create(Registry.BIOME_REGISTRY, new ResourceLocation(name)); //TODO Test
TagKey<Biome> type = TagKey.create(Registry.BIOME_REGISTRY, new ResourceLocation(name));
exclude.add(type);
}
}
Expand Down Expand Up @@ -207,8 +219,10 @@ public boolean isAnd() {
public static CheckType getOrCreate(List<TagKey<Biome>> include, List<TagKey<Biome>> exclude, boolean and) {
CheckType checkType = BY_NAME.get(Objects.hash(include, exclude, and));
if (checkType == null) {
//System.out.println("Check type null");
checkType = new CheckType(include, exclude, and);
}
//checkType.include.forEach(System.out::println);
return checkType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
{
"condition": "aquaculture:biome_tag_check",
"predicate": {
"include": ["WATER", "BEACH", "SWAMP"],
"exclude": ["SNOWY"]
"include": ["forge:is_water", "minecraft:is_beach", "forge:is_swamp"],
"exclude": ["forge:is_snowy"]
}
},
{
Expand All @@ -50,8 +50,8 @@
{
"condition": "aquaculture:biome_tag_check",
"predicate": {
"include": ["OCEAN", "BEACH"],
"exclude": ["HOT", "SNOWY"]
"include": ["minecraft:is_ocean", "minecraft:is_beach"],
"exclude": ["forge:is_hot", "forge:is_snowy"]
}
}
],
Expand All @@ -64,7 +64,7 @@
{
"condition": "aquaculture:biome_tag_check",
"predicate": {
"exclude": ["OCEAN", "BEACH"]
"exclude": ["minecraft:is_ocean", "minecraft:is_beach"]
}
}
],
Expand Down

0 comments on commit f970a22

Please sign in to comment.