Skip to content

Commit

Permalink
docs: Biome Tint documentation (mostly)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zepalesque committed May 24, 2024
1 parent 0a44823 commit 6c12cd6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
31 changes: 30 additions & 1 deletion src/main/java/net/zepalesque/zenith/api/biometint/BiomeTint.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,37 @@
import java.util.HashMap;
import java.util.Map;

/** A class used to easily create biome-dependent tint systems. */
public class BiomeTint {

private final DataMapType<Biome, Integer> dataMap;
private Map<Biome, Integer> tints = null;
private final int defaultColor;

/**
* @param loc The location that will be used for the {@link DataMapType}. Preferably should be the same as this tint's registered ID.
* @param defaultColor The fallback color that should be used if a biome does not exist in the tint map.
*/
public BiomeTint(ResourceLocation loc, int defaultColor) {
DataMapType<Biome, Integer> dataMap = createTintMap(loc);
this.dataMap = dataMap;
this.defaultColor = defaultColor;
}

/**
* Creates a {@link DataMapType} that should be used for this object. Whenever the player joins a world, this will be used to update the tint map.
* @param location The {@link ResourceLocation} that should be used for this data map. This will automatically be prefixed with "tint/".
* @return a new {@link DataMapType}
*/
private static DataMapType<Biome, Integer> createTintMap(ResourceLocation location) {
return DataMapType.builder(location.withPrefix("tint/"), Registries.BIOME, Codec.INT).synced(Codec.INT, true).build();
}

/**
* Returns the color for the given biome. Note that this will throw an exception if the tint map has not been initialized yet.
* @param biome The biome to check for.
* @return The color that should be used, as an integer.
*/
public int getColor(Biome biome) {
if (this.tints == null) {
throw new UnsupportedOperationException("Biome tints have not been initialized yet!");
Expand All @@ -34,13 +49,20 @@ public int getColor(Biome biome) {
}
}

public void clearTints() {
/**
* Clears or initializes the tint map. Used before adding new tints whenever the player joins a new world. Should not be called, as this is handled by Zenith.
*/
public void tintInit() {
if (this.tints == null) {
this.tints = new HashMap<>();
} else {
this.tints.clear();
}
}

/**
* Adds a tint color to the map. Used to add tints from the data map whenever the player joins a new world. Should not be called, as this is handled by Zenith.
*/
public void addTint(Biome biome, int color) {
if (this.tints == null) {
throw new UnsupportedOperationException("Biome tints have not been initialized yet!");
Expand All @@ -49,10 +71,17 @@ public void addTint(Biome biome, int color) {
}
}

/**
* Access for the {@link DataMapType} this object uses.
* @return The data map type. Should mainly be used for datagen purposes
*/
public DataMapType<Biome, Integer> getDataMap() {
return this.dataMap;
}

/**
* Registers this object's {@link DataMapType}. Should not be called, as this is handled by Zenith.
*/
public void register(RegisterDataMapTypesEvent event) {
event.register(this.dataMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.registries.datamaps.DataMapType;
import net.zepalesque.zenith.Zenith;
import net.zepalesque.zenith.api.biometint.BiomeTints;

import java.util.HashMap;
import java.util.Map;

@Mod.EventBusSubscriber(modid = Zenith.MODID)
Expand All @@ -21,7 +19,7 @@ public class BiomeTintListener {
@SubscribeEvent
public static void sendColors(ClientPlayerNetworkEvent.LoggingIn event) {
BiomeTints.TINT_REGISTRY.forEach(tint -> {
tint.clearTints();
tint.tintInit();
Registry<Biome> registry = event.getPlayer().level().registryAccess().registryOrThrow(Registries.BIOME);
for (Map.Entry<ResourceKey<Biome>, Integer> entry : registry.getDataMap(tint.getDataMap()).entrySet()) {
Biome b = registry.get(entry.getKey());
Expand Down

0 comments on commit 6c12cd6

Please sign in to comment.